blob: 9908bba5b3d351d9ac8046643677c61f6f6fb0f4 [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);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200123static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100125
Ma Lingd4906092009-03-18 20:13:27 +0800126struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300127 struct {
128 int min, max;
129 } dot, vco, n, m, m1, m2, p, p1;
130
131 struct {
132 int dot_limit;
133 int p2_slow, p2_fast;
134 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800135};
Jesse Barnes79e53942008-11-07 14:24:08 -0800136
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300137/* returns HPLL frequency in kHz */
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200138int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139{
140 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
141
142 /* Obtain SKU information */
143 mutex_lock(&dev_priv->sb_lock);
144 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
145 CCK_FUSE_HPLL_FREQ_MASK;
146 mutex_unlock(&dev_priv->sb_lock);
147
148 return vco_freq[hpll_freq] * 1000;
149}
150
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200151int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
152 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300153{
154 u32 val;
155 int divider;
156
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300157 mutex_lock(&dev_priv->sb_lock);
158 val = vlv_cck_read(dev_priv, reg);
159 mutex_unlock(&dev_priv->sb_lock);
160
161 divider = val & CCK_FREQUENCY_VALUES;
162
163 WARN((val & CCK_FREQUENCY_STATUS) !=
164 (divider << CCK_FREQUENCY_STATUS_SHIFT),
165 "%s change in progress\n", name);
166
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200167 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
168}
169
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +0200170int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
171 const char *name, u32 reg)
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200172{
173 if (dev_priv->hpll_freq == 0)
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200174 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200175
176 return vlv_get_cck_clock(dev_priv, name, reg,
177 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300178}
179
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180static void intel_update_czclk(struct drm_i915_private *dev_priv)
181{
Wayne Boyer666a4532015-12-09 12:29:35 -0800182 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300183 return;
184
185 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
186 CCK_CZ_CLOCK_CONTROL);
187
188 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
189}
190
Chris Wilson021357a2010-09-07 20:54:59 +0100191static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200192intel_fdi_link_freq(struct drm_i915_private *dev_priv,
193 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100194{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200195 if (HAS_DDI(dev_priv))
196 return pipe_config->port_clock; /* SPLL */
197 else if (IS_GEN5(dev_priv))
198 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200199 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200200 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100201}
202
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300203static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400204 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200205 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200206 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400207 .m = { .min = 96, .max = 140 },
208 .m1 = { .min = 18, .max = 26 },
209 .m2 = { .min = 6, .max = 16 },
210 .p = { .min = 4, .max = 128 },
211 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700212 .p2 = { .dot_limit = 165000,
213 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700214};
215
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300216static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200217 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200218 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200219 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200220 .m = { .min = 96, .max = 140 },
221 .m1 = { .min = 18, .max = 26 },
222 .m2 = { .min = 6, .max = 16 },
223 .p = { .min = 4, .max = 128 },
224 .p1 = { .min = 2, .max = 33 },
225 .p2 = { .dot_limit = 165000,
226 .p2_slow = 4, .p2_fast = 4 },
227};
228
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300229static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400230 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200231 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200232 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400233 .m = { .min = 96, .max = 140 },
234 .m1 = { .min = 18, .max = 26 },
235 .m2 = { .min = 6, .max = 16 },
236 .p = { .min = 4, .max = 128 },
237 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
Eric Anholt273e27c2011-03-30 13:01:10 -0700241
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300242static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400243 .dot = { .min = 20000, .max = 400000 },
244 .vco = { .min = 1400000, .max = 2800000 },
245 .n = { .min = 1, .max = 6 },
246 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100247 .m1 = { .min = 8, .max = 18 },
248 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400249 .p = { .min = 5, .max = 80 },
250 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .p2 = { .dot_limit = 200000,
252 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700253};
254
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300255static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .dot = { .min = 20000, .max = 400000 },
257 .vco = { .min = 1400000, .max = 2800000 },
258 .n = { .min = 1, .max = 6 },
259 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100260 .m1 = { .min = 8, .max = 18 },
261 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .p = { .min = 7, .max = 98 },
263 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700264 .p2 = { .dot_limit = 112000,
265 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700266};
267
Eric Anholt273e27c2011-03-30 13:01:10 -0700268
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300269static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700270 .dot = { .min = 25000, .max = 270000 },
271 .vco = { .min = 1750000, .max = 3500000},
272 .n = { .min = 1, .max = 4 },
273 .m = { .min = 104, .max = 138 },
274 .m1 = { .min = 17, .max = 23 },
275 .m2 = { .min = 5, .max = 11 },
276 .p = { .min = 10, .max = 30 },
277 .p1 = { .min = 1, .max = 3},
278 .p2 = { .dot_limit = 270000,
279 .p2_slow = 10,
280 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800281 },
Keith Packarde4b36692009-06-05 19:22:17 -0700282};
283
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300284static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700285 .dot = { .min = 22000, .max = 400000 },
286 .vco = { .min = 1750000, .max = 3500000},
287 .n = { .min = 1, .max = 4 },
288 .m = { .min = 104, .max = 138 },
289 .m1 = { .min = 16, .max = 23 },
290 .m2 = { .min = 5, .max = 11 },
291 .p = { .min = 5, .max = 80 },
292 .p1 = { .min = 1, .max = 8},
293 .p2 = { .dot_limit = 165000,
294 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700295};
296
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300297static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .dot = { .min = 20000, .max = 115000 },
299 .vco = { .min = 1750000, .max = 3500000 },
300 .n = { .min = 1, .max = 3 },
301 .m = { .min = 104, .max = 138 },
302 .m1 = { .min = 17, .max = 23 },
303 .m2 = { .min = 5, .max = 11 },
304 .p = { .min = 28, .max = 112 },
305 .p1 = { .min = 2, .max = 8 },
306 .p2 = { .dot_limit = 0,
307 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800308 },
Keith Packarde4b36692009-06-05 19:22:17 -0700309};
310
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300311static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .dot = { .min = 80000, .max = 224000 },
313 .vco = { .min = 1750000, .max = 3500000 },
314 .n = { .min = 1, .max = 3 },
315 .m = { .min = 104, .max = 138 },
316 .m1 = { .min = 17, .max = 23 },
317 .m2 = { .min = 5, .max = 11 },
318 .p = { .min = 14, .max = 42 },
319 .p1 = { .min = 2, .max = 6 },
320 .p2 = { .dot_limit = 0,
321 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800322 },
Keith Packarde4b36692009-06-05 19:22:17 -0700323};
324
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300325static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400326 .dot = { .min = 20000, .max = 400000},
327 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700328 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400329 .n = { .min = 3, .max = 6 },
330 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700331 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400332 .m1 = { .min = 0, .max = 0 },
333 .m2 = { .min = 0, .max = 254 },
334 .p = { .min = 5, .max = 80 },
335 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700336 .p2 = { .dot_limit = 200000,
337 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700338};
339
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300340static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400341 .dot = { .min = 20000, .max = 400000 },
342 .vco = { .min = 1700000, .max = 3500000 },
343 .n = { .min = 3, .max = 6 },
344 .m = { .min = 2, .max = 256 },
345 .m1 = { .min = 0, .max = 0 },
346 .m2 = { .min = 0, .max = 254 },
347 .p = { .min = 7, .max = 112 },
348 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700349 .p2 = { .dot_limit = 112000,
350 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700351};
352
Eric Anholt273e27c2011-03-30 13:01:10 -0700353/* Ironlake / Sandybridge
354 *
355 * We calculate clock using (register_value + 2) for N/M1/M2, so here
356 * the range value for them is (actual_value - 2).
357 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300358static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700359 .dot = { .min = 25000, .max = 350000 },
360 .vco = { .min = 1760000, .max = 3510000 },
361 .n = { .min = 1, .max = 5 },
362 .m = { .min = 79, .max = 127 },
363 .m1 = { .min = 12, .max = 22 },
364 .m2 = { .min = 5, .max = 9 },
365 .p = { .min = 5, .max = 80 },
366 .p1 = { .min = 1, .max = 8 },
367 .p2 = { .dot_limit = 225000,
368 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700369};
370
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300371static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700372 .dot = { .min = 25000, .max = 350000 },
373 .vco = { .min = 1760000, .max = 3510000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 79, .max = 118 },
376 .m1 = { .min = 12, .max = 22 },
377 .m2 = { .min = 5, .max = 9 },
378 .p = { .min = 28, .max = 112 },
379 .p1 = { .min = 2, .max = 8 },
380 .p2 = { .dot_limit = 225000,
381 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800382};
383
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300384static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700385 .dot = { .min = 25000, .max = 350000 },
386 .vco = { .min = 1760000, .max = 3510000 },
387 .n = { .min = 1, .max = 3 },
388 .m = { .min = 79, .max = 127 },
389 .m1 = { .min = 12, .max = 22 },
390 .m2 = { .min = 5, .max = 9 },
391 .p = { .min = 14, .max = 56 },
392 .p1 = { .min = 2, .max = 8 },
393 .p2 = { .dot_limit = 225000,
394 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800395};
396
Eric Anholt273e27c2011-03-30 13:01:10 -0700397/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300398static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700399 .dot = { .min = 25000, .max = 350000 },
400 .vco = { .min = 1760000, .max = 3510000 },
401 .n = { .min = 1, .max = 2 },
402 .m = { .min = 79, .max = 126 },
403 .m1 = { .min = 12, .max = 22 },
404 .m2 = { .min = 5, .max = 9 },
405 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400406 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700407 .p2 = { .dot_limit = 225000,
408 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800409};
410
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300411static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700412 .dot = { .min = 25000, .max = 350000 },
413 .vco = { .min = 1760000, .max = 3510000 },
414 .n = { .min = 1, .max = 3 },
415 .m = { .min = 79, .max = 126 },
416 .m1 = { .min = 12, .max = 22 },
417 .m2 = { .min = 5, .max = 9 },
418 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400419 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700420 .p2 = { .dot_limit = 225000,
421 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800422};
423
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300424static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300425 /*
426 * These are the data rate limits (measured in fast clocks)
427 * since those are the strictest limits we have. The fast
428 * clock and actual rate limits are more relaxed, so checking
429 * them would make no difference.
430 */
431 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200432 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700433 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700434 .m1 = { .min = 2, .max = 3 },
435 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300436 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300437 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700438};
439
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300440static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300441 /*
442 * These are the data rate limits (measured in fast clocks)
443 * since those are the strictest limits we have. The fast
444 * clock and actual rate limits are more relaxed, so checking
445 * them would make no difference.
446 */
447 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200448 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300449 .n = { .min = 1, .max = 1 },
450 .m1 = { .min = 2, .max = 2 },
451 .m2 = { .min = 24 << 22, .max = 175 << 22 },
452 .p1 = { .min = 2, .max = 4 },
453 .p2 = { .p2_slow = 1, .p2_fast = 14 },
454};
455
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300456static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200457 /* FIXME: find real dot limits */
458 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530459 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200460 .n = { .min = 1, .max = 1 },
461 .m1 = { .min = 2, .max = 2 },
462 /* FIXME: find real m2 limits */
463 .m2 = { .min = 2 << 22, .max = 255 << 22 },
464 .p1 = { .min = 2, .max = 4 },
465 .p2 = { .p2_slow = 1, .p2_fast = 20 },
466};
467
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200468static bool
469needs_modeset(struct drm_crtc_state *state)
470{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200471 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200472}
473
Imre Deakdccbea32015-06-22 23:35:51 +0300474/*
475 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
476 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
477 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
478 * The helpers' return value is the rate of the clock that is fed to the
479 * display engine's pipe which can be the above fast dot clock rate or a
480 * divided-down version of it.
481 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500482/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300483static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800484{
Shaohua Li21778322009-02-23 15:19:16 +0800485 clock->m = clock->m2 + 2;
486 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200487 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300488 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300489 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
490 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300491
492 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800493}
494
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200495static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
496{
497 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
498}
499
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300500static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800501{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200502 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800503 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200504 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300505 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300506 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
507 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300508
509 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800510}
511
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300512static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300513{
514 clock->m = clock->m1 * clock->m2;
515 clock->p = clock->p1 * clock->p2;
516 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300517 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300518 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
519 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300520
521 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300522}
523
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300524int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300525{
526 clock->m = clock->m1 * clock->m2;
527 clock->p = clock->p1 * clock->p2;
528 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300529 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300530 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
531 clock->n << 22);
532 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300533
534 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300535}
536
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800537#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800538/**
539 * Returns whether the given set of divisors are valid for a given refclk with
540 * the given connectors.
541 */
542
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100543static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300544 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300545 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800546{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300547 if (clock->n < limit->n.min || limit->n.max < clock->n)
548 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800549 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400550 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800551 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400552 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800553 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400554 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300555
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100556 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200557 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300558 if (clock->m1 <= clock->m2)
559 INTELPllInvalid("m1 <= m2\n");
560
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100561 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200562 !IS_GEN9_LP(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300563 if (clock->p < limit->p.min || limit->p.max < clock->p)
564 INTELPllInvalid("p out of range\n");
565 if (clock->m < limit->m.min || limit->m.max < clock->m)
566 INTELPllInvalid("m out of range\n");
567 }
568
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400570 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800571 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
572 * connector, etc., rather than just a single range.
573 */
574 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400575 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800576
577 return true;
578}
579
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300580static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300581i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300582 const struct intel_crtc_state *crtc_state,
583 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800584{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300585 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800586
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300587 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800588 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100589 * For LVDS just rely on its current settings for dual-channel.
590 * We haven't figured out how to reliably set up different
591 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800592 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100593 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300594 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800595 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300596 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 } else {
598 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300599 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800600 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300601 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300603}
604
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200605/*
606 * Returns a set of divisors for the desired target clock with the given
607 * refclk, or FALSE. The returned values represent the clock equation:
608 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
609 *
610 * Target and reference clocks are specified in kHz.
611 *
612 * If match_clock is provided, then best_clock P divider must match the P
613 * divider from @match_clock used for LVDS downclocking.
614 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300615static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300616i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300617 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300618 int target, int refclk, struct dpll *match_clock,
619 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300620{
621 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300622 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300623 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800624
Akshay Joshi0206e352011-08-16 15:34:10 -0400625 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800626
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300627 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
628
Zhao Yakui42158662009-11-20 11:24:18 +0800629 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
630 clock.m1++) {
631 for (clock.m2 = limit->m2.min;
632 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200633 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800634 break;
635 for (clock.n = limit->n.min;
636 clock.n <= limit->n.max; clock.n++) {
637 for (clock.p1 = limit->p1.min;
638 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800639 int this_err;
640
Imre Deakdccbea32015-06-22 23:35:51 +0300641 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100642 if (!intel_PLL_is_valid(to_i915(dev),
643 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000644 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800645 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800646 if (match_clock &&
647 clock.p != match_clock->p)
648 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800649
650 this_err = abs(clock.dot - target);
651 if (this_err < err) {
652 *best_clock = clock;
653 err = this_err;
654 }
655 }
656 }
657 }
658 }
659
660 return (err != target);
661}
662
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
Ma Lingd4906092009-03-18 20:13:27 +0800673static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300674pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200675 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200678{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300679 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300680 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200681 int err = target;
682
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200683 memset(best_clock, 0, sizeof(*best_clock));
684
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200691 for (clock.n = limit->n.min;
692 clock.n <= limit->n.max; clock.n++) {
693 for (clock.p1 = limit->p1.min;
694 clock.p1 <= limit->p1.max; clock.p1++) {
695 int this_err;
696
Imre Deakdccbea32015-06-22 23:35:51 +0300697 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100698 if (!intel_PLL_is_valid(to_i915(dev),
699 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800700 &clock))
701 continue;
702 if (match_clock &&
703 clock.p != match_clock->p)
704 continue;
705
706 this_err = abs(clock.dot - target);
707 if (this_err < err) {
708 *best_clock = clock;
709 err = this_err;
710 }
711 }
712 }
713 }
714 }
715
716 return (err != target);
717}
718
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200719/*
720 * Returns a set of divisors for the desired target clock with the given
721 * refclk, or FALSE. The returned values represent the clock equation:
722 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200723 *
724 * Target and reference clocks are specified in kHz.
725 *
726 * If match_clock is provided, then best_clock P divider must match the P
727 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200728 */
Ma Lingd4906092009-03-18 20:13:27 +0800729static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300730g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200731 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300732 int target, int refclk, struct dpll *match_clock,
733 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800734{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300735 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300736 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800737 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300738 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400739 /* approximately equals target * 0.00585 */
740 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800741
742 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300743
744 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
745
Ma Lingd4906092009-03-18 20:13:27 +0800746 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200747 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800748 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200749 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800750 for (clock.m1 = limit->m1.max;
751 clock.m1 >= limit->m1.min; clock.m1--) {
752 for (clock.m2 = limit->m2.max;
753 clock.m2 >= limit->m2.min; clock.m2--) {
754 for (clock.p1 = limit->p1.max;
755 clock.p1 >= limit->p1.min; clock.p1--) {
756 int this_err;
757
Imre Deakdccbea32015-06-22 23:35:51 +0300758 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100759 if (!intel_PLL_is_valid(to_i915(dev),
760 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000761 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800762 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000763
764 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800765 if (this_err < err_most) {
766 *best_clock = clock;
767 err_most = this_err;
768 max_n = clock.n;
769 found = true;
770 }
771 }
772 }
773 }
774 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800775 return found;
776}
Ma Lingd4906092009-03-18 20:13:27 +0800777
Imre Deakd5dd62b2015-03-17 11:40:03 +0200778/*
779 * Check if the calculated PLL configuration is more optimal compared to the
780 * best configuration and error found so far. Return the calculated error.
781 */
782static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300783 const struct dpll *calculated_clock,
784 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200785 unsigned int best_error_ppm,
786 unsigned int *error_ppm)
787{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200788 /*
789 * For CHV ignore the error and consider only the P value.
790 * Prefer a bigger P value based on HW requirements.
791 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100792 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200793 *error_ppm = 0;
794
795 return calculated_clock->p > best_clock->p;
796 }
797
Imre Deak24be4e42015-03-17 11:40:04 +0200798 if (WARN_ON_ONCE(!target_freq))
799 return false;
800
Imre Deakd5dd62b2015-03-17 11:40:03 +0200801 *error_ppm = div_u64(1000000ULL *
802 abs(target_freq - calculated_clock->dot),
803 target_freq);
804 /*
805 * Prefer a better P value over a better (smaller) error if the error
806 * is small. Ensure this preference for future configurations too by
807 * setting the error to 0.
808 */
809 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
810 *error_ppm = 0;
811
812 return true;
813 }
814
815 return *error_ppm + 10 < best_error_ppm;
816}
817
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200818/*
819 * Returns a set of divisors for the desired target clock with the given
820 * refclk, or FALSE. The returned values represent the clock equation:
821 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
822 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800823static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300824vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200825 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300826 int target, int refclk, struct dpll *match_clock,
827 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700828{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200829 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300830 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300831 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300832 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300833 /* min update 19.2 MHz */
834 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300835 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700836
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300837 target *= 5; /* fast clock */
838
839 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700840
841 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300842 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300843 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300844 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300845 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300846 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700847 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300848 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200849 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300850
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300851 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
852 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300853
Imre Deakdccbea32015-06-22 23:35:51 +0300854 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300855
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100856 if (!intel_PLL_is_valid(to_i915(dev),
857 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300858 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300859 continue;
860
Imre Deakd5dd62b2015-03-17 11:40:03 +0200861 if (!vlv_PLL_is_optimal(dev, target,
862 &clock,
863 best_clock,
864 bestppm, &ppm))
865 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300866
Imre Deakd5dd62b2015-03-17 11:40:03 +0200867 *best_clock = clock;
868 bestppm = ppm;
869 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700870 }
871 }
872 }
873 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700874
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300875 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700876}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700877
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300883static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300884chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200885 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300888{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300890 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200891 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300892 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300893 uint64_t m2;
894 int found = false;
895
896 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200897 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300898
899 /*
900 * Based on hardware doc, the n always set to 1, and m1 always
901 * set to 2. If requires to support 200Mhz refclk, we need to
902 * revisit this because n may not 1 anymore.
903 */
904 clock.n = 1, clock.m1 = 2;
905 target *= 5; /* fast clock */
906
907 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
908 for (clock.p2 = limit->p2.p2_fast;
909 clock.p2 >= limit->p2.p2_slow;
910 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200911 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300912
913 clock.p = clock.p1 * clock.p2;
914
915 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
916 clock.n) << 22, refclk * clock.m1);
917
918 if (m2 > INT_MAX/clock.m1)
919 continue;
920
921 clock.m2 = m2;
922
Imre Deakdccbea32015-06-22 23:35:51 +0300923 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300924
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100925 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300926 continue;
927
Imre Deak9ca3ba02015-03-17 11:40:05 +0200928 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
929 best_error_ppm, &error_ppm))
930 continue;
931
932 *best_clock = clock;
933 best_error_ppm = error_ppm;
934 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300935 }
936 }
937
938 return found;
939}
940
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200941bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300942 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200943{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200944 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300945 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200946
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200947 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200948 target_clock, refclk, NULL, best_clock);
949}
950
Ville Syrjälä525b9312016-10-31 22:37:02 +0200951bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300952{
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300953 /* Be paranoid as we can arrive here with only partial
954 * state retrieved from the hardware during setup.
955 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100956 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300957 * as Haswell has gained clock readout/fastboot support.
958 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000959 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300960 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700961 *
962 * FIXME: The intel_crtc->active here should be switched to
963 * crtc->state->active once we have proper CRTC states wired up
964 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300965 */
Ville Syrjälä525b9312016-10-31 22:37:02 +0200966 return crtc->active && crtc->base.primary->state->fb &&
967 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300968}
969
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200970enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
971 enum pipe pipe)
972{
Ville Syrjälä98187832016-10-31 22:37:10 +0200973 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200974
Ville Syrjäläe2af48c2016-10-31 22:37:05 +0200975 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200976}
977
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +0000978static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300979{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200980 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300981 u32 line1, line2;
982 u32 line_mask;
983
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100984 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300985 line_mask = DSL_LINEMASK_GEN2;
986 else
987 line_mask = DSL_LINEMASK_GEN3;
988
989 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +0200990 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300991 line2 = I915_READ(reg) & line_mask;
992
993 return line1 == line2;
994}
995
Keith Packardab7ad7f2010-10-03 00:33:06 -0700996/*
997 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300998 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700999 *
1000 * After disabling a pipe, we can't wait for vblank in the usual way,
1001 * spinning on the vblank interrupt status bit, since we won't actually
1002 * see an interrupt when the pipe is disabled.
1003 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001004 * On Gen4 and above:
1005 * wait for the pipe register state bit to turn off
1006 *
1007 * Otherwise:
1008 * wait for the display line value to settle (it usually
1009 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001010 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001011 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001012static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001013{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001014 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001015 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001016 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001017
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001018 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001019 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001020
Keith Packardab7ad7f2010-10-03 00:33:06 -07001021 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001022 if (intel_wait_for_register(dev_priv,
1023 reg, I965_PIPECONF_ACTIVE, 0,
1024 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001025 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001026 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001027 /* Wait for the display line to settle */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001028 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001029 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001030 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001031}
1032
Jesse Barnesb24e7172011-01-04 15:09:30 -08001033/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001034void assert_pll(struct drm_i915_private *dev_priv,
1035 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001036{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001037 u32 val;
1038 bool cur_state;
1039
Ville Syrjälä649636e2015-09-22 19:50:01 +03001040 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001041 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001042 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001043 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001044 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001045}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001046
Jani Nikula23538ef2013-08-27 15:12:22 +03001047/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001048void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001049{
1050 u32 val;
1051 bool cur_state;
1052
Ville Syrjäläa5805162015-05-26 20:42:30 +03001053 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001054 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001055 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001056
1057 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001058 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001059 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001060 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001061}
Jani Nikula23538ef2013-08-27 15:12:22 +03001062
Jesse Barnes040484a2011-01-03 12:14:26 -08001063static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1064 enum pipe pipe, bool state)
1065{
Jesse Barnes040484a2011-01-03 12:14:26 -08001066 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001067 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1068 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001069
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001070 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001071 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001072 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001073 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001074 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001075 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001076 cur_state = !!(val & FDI_TX_ENABLE);
1077 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001078 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001079 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001080 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001081}
1082#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1083#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1084
1085static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1086 enum pipe pipe, bool state)
1087{
Jesse Barnes040484a2011-01-03 12:14:26 -08001088 u32 val;
1089 bool cur_state;
1090
Ville Syrjälä649636e2015-09-22 19:50:01 +03001091 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001092 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001093 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001094 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001095 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001096}
1097#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1098#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1099
1100static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1101 enum pipe pipe)
1102{
Jesse Barnes040484a2011-01-03 12:14:26 -08001103 u32 val;
1104
1105 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001106 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001107 return;
1108
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001109 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001110 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001111 return;
1112
Ville Syrjälä649636e2015-09-22 19:50:01 +03001113 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001114 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 -08001115}
1116
Daniel Vetter55607e82013-06-16 21:42:39 +02001117void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1118 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001119{
Jesse Barnes040484a2011-01-03 12:14:26 -08001120 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001121 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001122
Ville Syrjälä649636e2015-09-22 19:50:01 +03001123 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001124 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001125 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001126 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001127 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001128}
1129
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001130void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001131{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001132 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001133 u32 val;
1134 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001135 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001136
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001137 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001138 return;
1139
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001140 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001141 u32 port_sel;
1142
Imre Deak44cb7342016-08-10 14:07:29 +03001143 pp_reg = PP_CONTROL(0);
1144 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001145
1146 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1147 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1148 panel_pipe = PIPE_B;
1149 /* XXX: else fix for eDP */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001150 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001151 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001152 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001153 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001154 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001155 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001156 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1157 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001158 }
1159
1160 val = I915_READ(pp_reg);
1161 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001162 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001163 locked = false;
1164
Rob Clarke2c719b2014-12-15 13:56:32 -05001165 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001166 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001167 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001168}
1169
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001170static void assert_cursor(struct drm_i915_private *dev_priv,
1171 enum pipe pipe, bool state)
1172{
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001173 bool cur_state;
1174
Jani Nikula2a307c22016-11-30 17:43:04 +02001175 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001176 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001177 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001178 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001179
Rob Clarke2c719b2014-12-15 13:56:32 -05001180 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001181 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001182 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001183}
1184#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1185#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1186
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001187void assert_pipe(struct drm_i915_private *dev_priv,
1188 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001189{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001190 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001191 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1192 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001193 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001194
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001195 /* if we need the pipe quirk it must be always on */
1196 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1197 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
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));
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001280 } else if (INTEL_GEN(dev_priv) >= 5) {
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;
Daniel Vetter87442f72013-06-06 00:52:17 +02001552
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001553 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001554
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001555 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001556 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001557 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001558
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001559 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001560 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001561 /*
1562 * It appears to be important that we don't enable this
1563 * for the current pipe before otherwise configuring the
1564 * PLL. No idea how this should be handled if multiple
1565 * DVO outputs are enabled simultaneosly.
1566 */
1567 dpll |= DPLL_DVO_2X_MODE;
1568 I915_WRITE(DPLL(!crtc->pipe),
1569 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1570 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001571
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001572 /*
1573 * Apparently we need to have VGA mode enabled prior to changing
1574 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1575 * dividers, even though the register value does change.
1576 */
1577 I915_WRITE(reg, 0);
1578
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001579 I915_WRITE(reg, dpll);
1580
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001581 /* Wait for the clocks to stabilize. */
1582 POSTING_READ(reg);
1583 udelay(150);
1584
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001585 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001586 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001587 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001588 } else {
1589 /* The pixel multiplier can only be updated once the
1590 * DPLL is enabled and the clocks are stable.
1591 *
1592 * So write it again.
1593 */
1594 I915_WRITE(reg, dpll);
1595 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001596
1597 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001598 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001599 POSTING_READ(reg);
1600 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001601 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001602 POSTING_READ(reg);
1603 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001604 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001605 POSTING_READ(reg);
1606 udelay(150); /* wait for warmup */
1607}
1608
1609/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001610 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001611 * @dev_priv: i915 private structure
1612 * @pipe: pipe PLL to disable
1613 *
1614 * Disable the PLL for @pipe, making sure the pipe is off first.
1615 *
1616 * Note! This is for pre-ILK only.
1617 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001618static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001619{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001620 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001621 enum pipe pipe = crtc->pipe;
1622
1623 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001624 if (IS_I830(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001625 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001626 !intel_num_dvo_pipes(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001627 I915_WRITE(DPLL(PIPE_B),
1628 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1629 I915_WRITE(DPLL(PIPE_A),
1630 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1631 }
1632
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001633 /* Don't disable pipe or pipe PLLs if needed */
1634 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1635 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001636 return;
1637
1638 /* Make sure the pipe isn't still relying on us */
1639 assert_pipe_disabled(dev_priv, pipe);
1640
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001641 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001642 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001643}
1644
Jesse Barnesf6071162013-10-01 10:41:38 -07001645static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1646{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001647 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001648
1649 /* Make sure the pipe isn't still relying on us */
1650 assert_pipe_disabled(dev_priv, pipe);
1651
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001652 val = DPLL_INTEGRATED_REF_CLK_VLV |
1653 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1654 if (pipe != PIPE_A)
1655 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1656
Jesse Barnesf6071162013-10-01 10:41:38 -07001657 I915_WRITE(DPLL(pipe), val);
1658 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001659}
1660
1661static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1662{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001663 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001664 u32 val;
1665
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001666 /* Make sure the pipe isn't still relying on us */
1667 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001668
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001669 val = DPLL_SSC_REF_CLK_CHV |
1670 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001671 if (pipe != PIPE_A)
1672 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001673
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001674 I915_WRITE(DPLL(pipe), val);
1675 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001676
Ville Syrjäläa5805162015-05-26 20:42:30 +03001677 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001678
1679 /* Disable 10bit clock to display controller */
1680 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1681 val &= ~DPIO_DCLKP_EN;
1682 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1683
Ville Syrjäläa5805162015-05-26 20:42:30 +03001684 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001685}
1686
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001687void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001688 struct intel_digital_port *dport,
1689 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001690{
1691 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001692 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001693
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001694 switch (dport->port) {
1695 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001696 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001697 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001698 break;
1699 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001700 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001701 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001702 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001703 break;
1704 case PORT_D:
1705 port_mask = DPLL_PORTD_READY_MASK;
1706 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001707 break;
1708 default:
1709 BUG();
1710 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001711
Chris Wilson370004d2016-06-30 15:32:56 +01001712 if (intel_wait_for_register(dev_priv,
1713 dpll_reg, port_mask, expected_mask,
1714 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001715 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1716 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001717}
1718
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001719static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1720 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001721{
Ville Syrjälä98187832016-10-31 22:37:10 +02001722 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1723 pipe);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001724 i915_reg_t reg;
1725 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001726
Jesse Barnes040484a2011-01-03 12:14:26 -08001727 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001728 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001729
1730 /* FDI must be feeding us bits for PCH ports */
1731 assert_fdi_tx_enabled(dev_priv, pipe);
1732 assert_fdi_rx_enabled(dev_priv, pipe);
1733
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001734 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001735 /* Workaround: Set the timing override bit before enabling the
1736 * pch transcoder. */
1737 reg = TRANS_CHICKEN2(pipe);
1738 val = I915_READ(reg);
1739 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1740 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001741 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001742
Daniel Vetterab9412b2013-05-03 11:49:46 +02001743 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001744 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001745 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001746
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001747 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001748 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001749 * Make the BPC in transcoder be consistent with
1750 * that in pipeconf reg. For HDMI we must use 8bpc
1751 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001752 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001753 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001754 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001755 val |= PIPECONF_8BPC;
1756 else
1757 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001758 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001759
1760 val &= ~TRANS_INTERLACE_MASK;
1761 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001762 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001763 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001764 val |= TRANS_LEGACY_INTERLACED_ILK;
1765 else
1766 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001767 else
1768 val |= TRANS_PROGRESSIVE;
1769
Jesse Barnes040484a2011-01-03 12:14:26 -08001770 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001771 if (intel_wait_for_register(dev_priv,
1772 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1773 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001774 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001775}
1776
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001777static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001778 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001779{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001780 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001781
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001782 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001783 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001784 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001785
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001786 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001787 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001788 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001789 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001790
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001791 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001792 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001793
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001794 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1795 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001796 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001797 else
1798 val |= TRANS_PROGRESSIVE;
1799
Daniel Vetterab9412b2013-05-03 11:49:46 +02001800 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001801 if (intel_wait_for_register(dev_priv,
1802 LPT_TRANSCONF,
1803 TRANS_STATE_ENABLE,
1804 TRANS_STATE_ENABLE,
1805 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001806 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001807}
1808
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001809static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1810 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001811{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001812 i915_reg_t reg;
1813 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001814
1815 /* FDI relies on the transcoder */
1816 assert_fdi_tx_disabled(dev_priv, pipe);
1817 assert_fdi_rx_disabled(dev_priv, pipe);
1818
Jesse Barnes291906f2011-02-02 12:28:03 -08001819 /* Ports must be off as well */
1820 assert_pch_ports_disabled(dev_priv, pipe);
1821
Daniel Vetterab9412b2013-05-03 11:49:46 +02001822 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001823 val = I915_READ(reg);
1824 val &= ~TRANS_ENABLE;
1825 I915_WRITE(reg, val);
1826 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001827 if (intel_wait_for_register(dev_priv,
1828 reg, TRANS_STATE_ENABLE, 0,
1829 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001830 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001831
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001832 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001833 /* Workaround: Clear the timing override chicken bit again. */
1834 reg = TRANS_CHICKEN2(pipe);
1835 val = I915_READ(reg);
1836 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1837 I915_WRITE(reg, val);
1838 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001839}
1840
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001841void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001842{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001843 u32 val;
1844
Daniel Vetterab9412b2013-05-03 11:49:46 +02001845 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001846 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001847 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001849 if (intel_wait_for_register(dev_priv,
1850 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1851 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001852 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001853
1854 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001856 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001858}
1859
Ville Syrjälä65f21302016-10-14 20:02:53 +03001860enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1861{
1862 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1863
1864 WARN_ON(!crtc->config->has_pch_encoder);
1865
1866 if (HAS_PCH_LPT(dev_priv))
1867 return TRANSCODER_A;
1868 else
1869 return (enum transcoder) crtc->pipe;
1870}
1871
Jesse Barnes92f25842011-01-04 15:09:34 -08001872/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001873 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001874 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001875 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001876 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001877 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001878 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001879static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001880{
Paulo Zanoni03722642014-01-17 13:51:09 -02001881 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001882 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001883 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001884 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001885 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001886 u32 val;
1887
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001888 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1889
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001890 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001891 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001892 assert_sprites_disabled(dev_priv, pipe);
1893
Jesse Barnesb24e7172011-01-04 15:09:30 -08001894 /*
1895 * A pipe without a PLL won't actually be able to drive bits from
1896 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1897 * need the check.
1898 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001899 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001900 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001901 assert_dsi_pll_enabled(dev_priv);
1902 else
1903 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001904 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001905 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001906 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001907 assert_fdi_rx_pll_enabled(dev_priv,
1908 (enum pipe) intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001909 assert_fdi_tx_pll_enabled(dev_priv,
1910 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001911 }
1912 /* FIXME: assert CPU port conditions for SNB+ */
1913 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001914
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001915 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001916 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001917 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001918 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1919 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001920 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001921 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001922
1923 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001924 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001925
1926 /*
1927 * Until the pipe starts DSL will read as 0, which would cause
1928 * an apparent vblank timestamp jump, which messes up also the
1929 * frame count when it's derived from the timestamps. So let's
1930 * wait for the pipe to start properly before we call
1931 * drm_crtc_vblank_on()
1932 */
1933 if (dev->max_vblank_count == 0 &&
1934 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1935 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936}
1937
1938/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001939 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001940 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001942 * Disable the pipe of @crtc, making sure that various hardware
1943 * specific requirements are met, if applicable, e.g. plane
1944 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001945 *
1946 * Will wait until the pipe has shut down before returning.
1947 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001948static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001949{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001950 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001951 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001952 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001953 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 u32 val;
1955
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001956 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1957
Jesse Barnesb24e7172011-01-04 15:09:30 -08001958 /*
1959 * Make sure planes won't keep trying to pump pixels to us,
1960 * or we might hang the display.
1961 */
1962 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001963 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001964 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001965
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001966 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001967 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001968 if ((val & PIPECONF_ENABLE) == 0)
1969 return;
1970
Ville Syrjälä67adc642014-08-15 01:21:57 +03001971 /*
1972 * Double wide has implications for planes
1973 * so best keep it disabled when not needed.
1974 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001975 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03001976 val &= ~PIPECONF_DOUBLE_WIDE;
1977
1978 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001979 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
1980 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03001981 val &= ~PIPECONF_ENABLE;
1982
1983 I915_WRITE(reg, val);
1984 if ((val & PIPECONF_ENABLE) == 0)
1985 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001986}
1987
Ville Syrjälä832be822016-01-12 21:08:33 +02001988static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1989{
1990 return IS_GEN2(dev_priv) ? 2048 : 4096;
1991}
1992
Ville Syrjälä27ba3912016-02-15 22:54:40 +02001993static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
1994 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001995{
1996 switch (fb_modifier) {
1997 case DRM_FORMAT_MOD_NONE:
1998 return cpp;
1999 case I915_FORMAT_MOD_X_TILED:
2000 if (IS_GEN2(dev_priv))
2001 return 128;
2002 else
2003 return 512;
2004 case I915_FORMAT_MOD_Y_TILED:
2005 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2006 return 128;
2007 else
2008 return 512;
2009 case I915_FORMAT_MOD_Yf_TILED:
2010 switch (cpp) {
2011 case 1:
2012 return 64;
2013 case 2:
2014 case 4:
2015 return 128;
2016 case 8:
2017 case 16:
2018 return 256;
2019 default:
2020 MISSING_CASE(cpp);
2021 return cpp;
2022 }
2023 break;
2024 default:
2025 MISSING_CASE(fb_modifier);
2026 return cpp;
2027 }
2028}
2029
Ville Syrjälä832be822016-01-12 21:08:33 +02002030unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2031 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002032{
Ville Syrjälä832be822016-01-12 21:08:33 +02002033 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2034 return 1;
2035 else
2036 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002037 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002038}
2039
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002040/* Return the tile dimensions in pixel units */
2041static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2042 unsigned int *tile_width,
2043 unsigned int *tile_height,
2044 uint64_t fb_modifier,
2045 unsigned int cpp)
2046{
2047 unsigned int tile_width_bytes =
2048 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2049
2050 *tile_width = tile_width_bytes / cpp;
2051 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2052}
2053
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002054unsigned int
Chris Wilson24dbf512017-02-15 10:59:18 +00002055intel_fb_align_height(struct drm_i915_private *dev_priv,
2056 unsigned int height,
2057 uint32_t pixel_format,
2058 uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002059{
Ville Syrjälä832be822016-01-12 21:08:33 +02002060 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
Chris Wilson24dbf512017-02-15 10:59:18 +00002061 unsigned int tile_height = intel_tile_height(dev_priv, fb_modifier, cpp);
Ville Syrjälä832be822016-01-12 21:08:33 +02002062
2063 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002064}
2065
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002066unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2067{
2068 unsigned int size = 0;
2069 int i;
2070
2071 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2072 size += rot_info->plane[i].width * rot_info->plane[i].height;
2073
2074 return size;
2075}
2076
Daniel Vetter75c82a52015-10-14 16:51:04 +02002077static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002078intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2079 const struct drm_framebuffer *fb,
2080 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002081{
Chris Wilson7b92c042017-01-14 00:28:26 +00002082 view->type = I915_GGTT_VIEW_NORMAL;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002083 if (drm_rotation_90_or_270(rotation)) {
Chris Wilson7b92c042017-01-14 00:28:26 +00002084 view->type = I915_GGTT_VIEW_ROTATED;
Chris Wilson8bab11932017-01-14 00:28:25 +00002085 view->rotated = to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002086 }
2087}
2088
Ville Syrjälä603525d2016-01-12 21:08:37 +02002089static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002090{
2091 if (INTEL_INFO(dev_priv)->gen >= 9)
2092 return 256 * 1024;
Jani Nikulac0f86832016-12-07 12:13:04 +02002093 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002094 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002095 return 128 * 1024;
2096 else if (INTEL_INFO(dev_priv)->gen >= 4)
2097 return 4 * 1024;
2098 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002099 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002100}
2101
Ville Syrjälä603525d2016-01-12 21:08:37 +02002102static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2103 uint64_t fb_modifier)
2104{
2105 switch (fb_modifier) {
2106 case DRM_FORMAT_MOD_NONE:
2107 return intel_linear_alignment(dev_priv);
2108 case I915_FORMAT_MOD_X_TILED:
2109 if (INTEL_INFO(dev_priv)->gen >= 9)
2110 return 256 * 1024;
2111 return 0;
2112 case I915_FORMAT_MOD_Y_TILED:
2113 case I915_FORMAT_MOD_Yf_TILED:
2114 return 1 * 1024 * 1024;
2115 default:
2116 MISSING_CASE(fb_modifier);
2117 return 0;
2118 }
2119}
2120
Chris Wilson058d88c2016-08-15 10:49:06 +01002121struct i915_vma *
2122intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002123{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002124 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002125 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002126 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002127 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002128 struct i915_vma *vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002129 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002130
Matt Roperebcdd392014-07-09 16:22:11 -07002131 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2132
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002133 alignment = intel_surf_alignment(dev_priv, fb->modifier);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002134
Ville Syrjälä3465c582016-02-15 22:54:43 +02002135 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002136
Chris Wilson693db182013-03-05 14:52:39 +00002137 /* Note that the w/a also requires 64 PTE of padding following the
2138 * bo. We currently fill all unused PTE with the shadow page and so
2139 * we should always have valid PTE following the scanout preventing
2140 * the VT-d warning.
2141 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002142 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002143 alignment = 256 * 1024;
2144
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002145 /*
2146 * Global gtt pte registers are special registers which actually forward
2147 * writes to a chunk of system memory. Which means that there is no risk
2148 * that the register values disappear as soon as we call
2149 * intel_runtime_pm_put(), so it is correct to wrap only the
2150 * pin/unpin/fence and not more.
2151 */
2152 intel_runtime_pm_get(dev_priv);
2153
Chris Wilson058d88c2016-08-15 10:49:06 +01002154 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
Chris Wilson49ef5292016-08-18 17:17:00 +01002155 if (IS_ERR(vma))
2156 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002157
Chris Wilson05a20d02016-08-18 17:16:55 +01002158 if (i915_vma_is_map_and_fenceable(vma)) {
Chris Wilson49ef5292016-08-18 17:17:00 +01002159 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2160 * fence, whereas 965+ only requires a fence if using
2161 * framebuffer compression. For simplicity, we always, when
2162 * possible, install a fence as the cost is not that onerous.
2163 *
2164 * If we fail to fence the tiled scanout, then either the
2165 * modeset will reject the change (which is highly unlikely as
2166 * the affected systems, all but one, do not have unmappable
2167 * space) or we will not be able to enable full powersaving
2168 * techniques (also likely not to apply due to various limits
2169 * FBC and the like impose on the size of the buffer, which
2170 * presumably we violated anyway with this unmappable buffer).
2171 * Anyway, it is presumably better to stumble onwards with
2172 * something and try to run the system in a "less than optimal"
2173 * mode that matches the user configuration.
2174 */
2175 if (i915_vma_get_fence(vma) == 0)
2176 i915_vma_pin_fence(vma);
Vivek Kasireddy98072162015-10-29 18:54:38 -07002177 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002178
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002179 i915_vma_get(vma);
Chris Wilson49ef5292016-08-18 17:17:00 +01002180err:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002181 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002182 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002183}
2184
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002185void intel_unpin_fb_vma(struct i915_vma *vma)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002186{
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002187 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002188
Chris Wilson49ef5292016-08-18 17:17:00 +01002189 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002190 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002191 i915_vma_put(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002192}
2193
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002194static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2195 unsigned int rotation)
2196{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002197 if (drm_rotation_90_or_270(rotation))
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002198 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2199 else
2200 return fb->pitches[plane];
2201}
2202
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002203/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002204 * Convert the x/y offsets into a linear offset.
2205 * Only valid with 0/180 degree rotation, which is fine since linear
2206 * offset is only used with linear buffers on pre-hsw and tiled buffers
2207 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2208 */
2209u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002210 const struct intel_plane_state *state,
2211 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002212{
Ville Syrjälä29490562016-01-20 18:02:50 +02002213 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002214 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002215 unsigned int pitch = fb->pitches[plane];
2216
2217 return y * pitch + x * cpp;
2218}
2219
2220/*
2221 * Add the x/y offsets derived from fb->offsets[] to the user
2222 * specified plane src x/y offsets. The resulting x/y offsets
2223 * specify the start of scanout from the beginning of the gtt mapping.
2224 */
2225void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002226 const struct intel_plane_state *state,
2227 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002228
2229{
Ville Syrjälä29490562016-01-20 18:02:50 +02002230 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2231 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002232
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002233 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002234 *x += intel_fb->rotated[plane].x;
2235 *y += intel_fb->rotated[plane].y;
2236 } else {
2237 *x += intel_fb->normal[plane].x;
2238 *y += intel_fb->normal[plane].y;
2239 }
2240}
2241
2242/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002243 * Input tile dimensions and pitch must already be
2244 * rotated to match x and y, and in pixel units.
2245 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002246static u32 _intel_adjust_tile_offset(int *x, int *y,
2247 unsigned int tile_width,
2248 unsigned int tile_height,
2249 unsigned int tile_size,
2250 unsigned int pitch_tiles,
2251 u32 old_offset,
2252 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002253{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002254 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002255 unsigned int tiles;
2256
2257 WARN_ON(old_offset & (tile_size - 1));
2258 WARN_ON(new_offset & (tile_size - 1));
2259 WARN_ON(new_offset > old_offset);
2260
2261 tiles = (old_offset - new_offset) / tile_size;
2262
2263 *y += tiles / pitch_tiles * tile_height;
2264 *x += tiles % pitch_tiles * tile_width;
2265
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002266 /* minimize x in case it got needlessly big */
2267 *y += *x / pitch_pixels * tile_height;
2268 *x %= pitch_pixels;
2269
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002270 return new_offset;
2271}
2272
2273/*
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002274 * Adjust the tile offset by moving the difference into
2275 * the x/y offsets.
2276 */
2277static u32 intel_adjust_tile_offset(int *x, int *y,
2278 const struct intel_plane_state *state, int plane,
2279 u32 old_offset, u32 new_offset)
2280{
2281 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2282 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002283 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002284 unsigned int rotation = state->base.rotation;
2285 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2286
2287 WARN_ON(new_offset > old_offset);
2288
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002289 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002290 unsigned int tile_size, tile_width, tile_height;
2291 unsigned int pitch_tiles;
2292
2293 tile_size = intel_tile_size(dev_priv);
2294 intel_tile_dims(dev_priv, &tile_width, &tile_height,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002295 fb->modifier, cpp);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002296
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002297 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002298 pitch_tiles = pitch / tile_height;
2299 swap(tile_width, tile_height);
2300 } else {
2301 pitch_tiles = pitch / (tile_width * cpp);
2302 }
2303
2304 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2305 tile_size, pitch_tiles,
2306 old_offset, new_offset);
2307 } else {
2308 old_offset += *y * pitch + *x * cpp;
2309
2310 *y = (old_offset - new_offset) / pitch;
2311 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2312 }
2313
2314 return new_offset;
2315}
2316
2317/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002318 * Computes the linear offset to the base tile and adjusts
2319 * x, y. bytes per pixel is assumed to be a power-of-two.
2320 *
2321 * In the 90/270 rotated case, x and y are assumed
2322 * to be already rotated to match the rotated GTT view, and
2323 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002324 *
2325 * This function is used when computing the derived information
2326 * under intel_framebuffer, so using any of that information
2327 * here is not allowed. Anything under drm_framebuffer can be
2328 * used. This is why the user has to pass in the pitch since it
2329 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002330 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002331static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2332 int *x, int *y,
2333 const struct drm_framebuffer *fb, int plane,
2334 unsigned int pitch,
2335 unsigned int rotation,
2336 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002337{
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002338 uint64_t fb_modifier = fb->modifier;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002339 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002340 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002341
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002342 if (alignment)
2343 alignment--;
2344
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002345 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002346 unsigned int tile_size, tile_width, tile_height;
2347 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002348
Ville Syrjäläd8433102016-01-12 21:08:35 +02002349 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002350 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2351 fb_modifier, cpp);
2352
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002353 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002354 pitch_tiles = pitch / tile_height;
2355 swap(tile_width, tile_height);
2356 } else {
2357 pitch_tiles = pitch / (tile_width * cpp);
2358 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002359
Ville Syrjäläd8433102016-01-12 21:08:35 +02002360 tile_rows = *y / tile_height;
2361 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002362
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002363 tiles = *x / tile_width;
2364 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002365
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002366 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2367 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002368
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002369 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2370 tile_size, pitch_tiles,
2371 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002372 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002373 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002374 offset_aligned = offset & ~alignment;
2375
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002376 *y = (offset & alignment) / pitch;
2377 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002378 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002379
2380 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002381}
2382
Ville Syrjälä6687c902015-09-15 13:16:41 +03002383u32 intel_compute_tile_offset(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002384 const struct intel_plane_state *state,
2385 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002386{
Ville Syrjälä29490562016-01-20 18:02:50 +02002387 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2388 const struct drm_framebuffer *fb = state->base.fb;
2389 unsigned int rotation = state->base.rotation;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002390 int pitch = intel_fb_pitch(fb, plane, rotation);
Ville Syrjälä8d970652016-01-28 16:30:28 +02002391 u32 alignment;
2392
2393 /* AUX_DIST needs only 4K alignment */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002394 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
Ville Syrjälä8d970652016-01-28 16:30:28 +02002395 alignment = 4096;
2396 else
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002397 alignment = intel_surf_alignment(dev_priv, fb->modifier);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002398
2399 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2400 rotation, alignment);
2401}
2402
2403/* Convert the fb->offset[] linear offset into x/y offsets */
2404static void intel_fb_offset_to_xy(int *x, int *y,
2405 const struct drm_framebuffer *fb, int plane)
2406{
Ville Syrjälä353c8592016-12-14 23:30:57 +02002407 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002408 unsigned int pitch = fb->pitches[plane];
2409 u32 linear_offset = fb->offsets[plane];
2410
2411 *y = linear_offset / pitch;
2412 *x = linear_offset % pitch / cpp;
2413}
2414
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002415static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2416{
2417 switch (fb_modifier) {
2418 case I915_FORMAT_MOD_X_TILED:
2419 return I915_TILING_X;
2420 case I915_FORMAT_MOD_Y_TILED:
2421 return I915_TILING_Y;
2422 default:
2423 return I915_TILING_NONE;
2424 }
2425}
2426
Ville Syrjälä6687c902015-09-15 13:16:41 +03002427static int
2428intel_fill_fb_info(struct drm_i915_private *dev_priv,
2429 struct drm_framebuffer *fb)
2430{
2431 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2432 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2433 u32 gtt_offset_rotated = 0;
2434 unsigned int max_size = 0;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +02002435 int i, num_planes = fb->format->num_planes;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002436 unsigned int tile_size = intel_tile_size(dev_priv);
2437
2438 for (i = 0; i < num_planes; i++) {
2439 unsigned int width, height;
2440 unsigned int cpp, size;
2441 u32 offset;
2442 int x, y;
2443
Ville Syrjälä353c8592016-12-14 23:30:57 +02002444 cpp = fb->format->cpp[i];
Ville Syrjälä145fcb12016-11-18 21:53:06 +02002445 width = drm_framebuffer_plane_width(fb->width, fb, i);
2446 height = drm_framebuffer_plane_height(fb->height, fb, i);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002447
2448 intel_fb_offset_to_xy(&x, &y, fb, i);
2449
2450 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002451 * The fence (if used) is aligned to the start of the object
2452 * so having the framebuffer wrap around across the edge of the
2453 * fenced region doesn't really work. We have no API to configure
2454 * the fence start offset within the object (nor could we probably
2455 * on gen2/3). So it's just easier if we just require that the
2456 * fb layout agrees with the fence layout. We already check that the
2457 * fb stride matches the fence stride elsewhere.
2458 */
2459 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2460 (x + width) * cpp > fb->pitches[i]) {
2461 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2462 i, fb->offsets[i]);
2463 return -EINVAL;
2464 }
2465
2466 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002467 * First pixel of the framebuffer from
2468 * the start of the normal gtt mapping.
2469 */
2470 intel_fb->normal[i].x = x;
2471 intel_fb->normal[i].y = y;
2472
2473 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2474 fb, 0, fb->pitches[i],
Daniel Vettercc926382016-08-15 10:41:47 +02002475 DRM_ROTATE_0, tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002476 offset /= tile_size;
2477
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002478 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002479 unsigned int tile_width, tile_height;
2480 unsigned int pitch_tiles;
2481 struct drm_rect r;
2482
2483 intel_tile_dims(dev_priv, &tile_width, &tile_height,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002484 fb->modifier, cpp);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002485
2486 rot_info->plane[i].offset = offset;
2487 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2488 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2489 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2490
2491 intel_fb->rotated[i].pitch =
2492 rot_info->plane[i].height * tile_height;
2493
2494 /* how many tiles does this plane need */
2495 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2496 /*
2497 * If the plane isn't horizontally tile aligned,
2498 * we need one more tile.
2499 */
2500 if (x != 0)
2501 size++;
2502
2503 /* rotate the x/y offsets to match the GTT view */
2504 r.x1 = x;
2505 r.y1 = y;
2506 r.x2 = x + width;
2507 r.y2 = y + height;
2508 drm_rect_rotate(&r,
2509 rot_info->plane[i].width * tile_width,
2510 rot_info->plane[i].height * tile_height,
Daniel Vettercc926382016-08-15 10:41:47 +02002511 DRM_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002512 x = r.x1;
2513 y = r.y1;
2514
2515 /* rotate the tile dimensions to match the GTT view */
2516 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2517 swap(tile_width, tile_height);
2518
2519 /*
2520 * We only keep the x/y offsets, so push all of the
2521 * gtt offset into the x/y offsets.
2522 */
Ander Conselvan de Oliveira46a1bd22017-01-20 16:28:44 +02002523 _intel_adjust_tile_offset(&x, &y,
2524 tile_width, tile_height,
2525 tile_size, pitch_tiles,
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002526 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002527
2528 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2529
2530 /*
2531 * First pixel of the framebuffer from
2532 * the start of the rotated gtt mapping.
2533 */
2534 intel_fb->rotated[i].x = x;
2535 intel_fb->rotated[i].y = y;
2536 } else {
2537 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2538 x * cpp, tile_size);
2539 }
2540
2541 /* how many tiles in total needed in the bo */
2542 max_size = max(max_size, offset + size);
2543 }
2544
2545 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2546 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2547 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2548 return -EINVAL;
2549 }
2550
2551 return 0;
2552}
2553
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002554static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002555{
2556 switch (format) {
2557 case DISPPLANE_8BPP:
2558 return DRM_FORMAT_C8;
2559 case DISPPLANE_BGRX555:
2560 return DRM_FORMAT_XRGB1555;
2561 case DISPPLANE_BGRX565:
2562 return DRM_FORMAT_RGB565;
2563 default:
2564 case DISPPLANE_BGRX888:
2565 return DRM_FORMAT_XRGB8888;
2566 case DISPPLANE_RGBX888:
2567 return DRM_FORMAT_XBGR8888;
2568 case DISPPLANE_BGRX101010:
2569 return DRM_FORMAT_XRGB2101010;
2570 case DISPPLANE_RGBX101010:
2571 return DRM_FORMAT_XBGR2101010;
2572 }
2573}
2574
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002575static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2576{
2577 switch (format) {
2578 case PLANE_CTL_FORMAT_RGB_565:
2579 return DRM_FORMAT_RGB565;
2580 default:
2581 case PLANE_CTL_FORMAT_XRGB_8888:
2582 if (rgb_order) {
2583 if (alpha)
2584 return DRM_FORMAT_ABGR8888;
2585 else
2586 return DRM_FORMAT_XBGR8888;
2587 } else {
2588 if (alpha)
2589 return DRM_FORMAT_ARGB8888;
2590 else
2591 return DRM_FORMAT_XRGB8888;
2592 }
2593 case PLANE_CTL_FORMAT_XRGB_2101010:
2594 if (rgb_order)
2595 return DRM_FORMAT_XBGR2101010;
2596 else
2597 return DRM_FORMAT_XRGB2101010;
2598 }
2599}
2600
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002601static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002602intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2603 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002604{
2605 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002606 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002607 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002608 struct drm_i915_gem_object *obj = NULL;
2609 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002610 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002611 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2612 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2613 PAGE_SIZE);
2614
2615 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002616
Chris Wilsonff2652e2014-03-10 08:07:02 +00002617 if (plane_config->size == 0)
2618 return false;
2619
Paulo Zanoni3badb492015-09-23 12:52:23 -03002620 /* If the FB is too big, just don't use it since fbdev is not very
2621 * important and we should probably use that space with FBC or other
2622 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002623 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002624 return false;
2625
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002626 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00002627 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002628 base_aligned,
2629 base_aligned,
2630 size_aligned);
Chris Wilson24dbf512017-02-15 10:59:18 +00002631 mutex_unlock(&dev->struct_mutex);
2632 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002633 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002634
Chris Wilson3e510a82016-08-05 10:14:23 +01002635 if (plane_config->tiling == I915_TILING_X)
2636 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002637
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002638 mode_cmd.pixel_format = fb->format->format;
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002639 mode_cmd.width = fb->width;
2640 mode_cmd.height = fb->height;
2641 mode_cmd.pitches[0] = fb->pitches[0];
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002642 mode_cmd.modifier[0] = fb->modifier;
Daniel Vetter18c52472015-02-10 17:16:09 +00002643 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002644
Chris Wilson24dbf512017-02-15 10:59:18 +00002645 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002646 DRM_DEBUG_KMS("intel fb init failed\n");
2647 goto out_unref_obj;
2648 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002649
Jesse Barnes484b41d2014-03-07 08:57:55 -08002650
Daniel Vetterf6936e22015-03-26 12:17:05 +01002651 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002652 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002653
2654out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002655 i915_gem_object_put(obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002656 return false;
2657}
2658
Daniel Vetter5a21b662016-05-24 17:13:53 +02002659/* Update plane->state->fb to match plane->fb after driver-internal updates */
2660static void
2661update_state_fb(struct drm_plane *plane)
2662{
2663 if (plane->fb == plane->state->fb)
2664 return;
2665
2666 if (plane->state->fb)
2667 drm_framebuffer_unreference(plane->state->fb);
2668 plane->state->fb = plane->fb;
2669 if (plane->state->fb)
2670 drm_framebuffer_reference(plane->state->fb);
2671}
2672
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002673static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002674intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2675 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002676{
2677 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002678 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002679 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002680 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002681 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002682 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002683 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2684 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002685 struct intel_plane_state *intel_state =
2686 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002687 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002688
Damien Lespiau2d140302015-02-05 17:22:18 +00002689 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002690 return;
2691
Daniel Vetterf6936e22015-03-26 12:17:05 +01002692 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002693 fb = &plane_config->fb->base;
2694 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002695 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002696
Damien Lespiau2d140302015-02-05 17:22:18 +00002697 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002698
2699 /*
2700 * Failed to alloc the obj, check to see if we should share
2701 * an fb with another CRTC instead
2702 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002703 for_each_crtc(dev, c) {
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002704 struct intel_plane_state *state;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002705
2706 if (c == &intel_crtc->base)
2707 continue;
2708
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002709 if (!to_intel_crtc(c)->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002710 continue;
2711
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002712 state = to_intel_plane_state(c->primary->state);
2713 if (!state->vma)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002714 continue;
2715
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002716 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2717 fb = c->primary->fb;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002718 drm_framebuffer_reference(fb);
2719 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002720 }
2721 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002722
Matt Roper200757f2015-12-03 11:37:36 -08002723 /*
2724 * We've failed to reconstruct the BIOS FB. Current display state
2725 * indicates that the primary plane is visible, but has a NULL FB,
2726 * which will lead to problems later if we don't fix it up. The
2727 * simplest solution is to just disable the primary plane now and
2728 * pretend the BIOS never had it enabled.
2729 */
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +01002730 plane_state->visible = false;
Matt Roper200757f2015-12-03 11:37:36 -08002731 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002732 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002733 intel_plane->disable_plane(primary, &intel_crtc->base);
2734
Daniel Vetter88595ac2015-03-26 12:42:24 +01002735 return;
2736
2737valid_fb:
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002738 mutex_lock(&dev->struct_mutex);
2739 intel_state->vma =
2740 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2741 mutex_unlock(&dev->struct_mutex);
2742 if (IS_ERR(intel_state->vma)) {
2743 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2744 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2745
2746 intel_state->vma = NULL;
2747 drm_framebuffer_unreference(fb);
2748 return;
2749 }
2750
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002751 plane_state->src_x = 0;
2752 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002753 plane_state->src_w = fb->width << 16;
2754 plane_state->src_h = fb->height << 16;
2755
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002756 plane_state->crtc_x = 0;
2757 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002758 plane_state->crtc_w = fb->width;
2759 plane_state->crtc_h = fb->height;
2760
Rob Clark1638d302016-11-05 11:08:08 -04002761 intel_state->base.src = drm_plane_state_src(plane_state);
2762 intel_state->base.dst = drm_plane_state_dest(plane_state);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002763
Daniel Vetter88595ac2015-03-26 12:42:24 +01002764 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002765 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002766 dev_priv->preserve_bios_swizzle = true;
2767
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002768 drm_framebuffer_reference(fb);
2769 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002770 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002771 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002772 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2773 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002774}
2775
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002776static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2777 unsigned int rotation)
2778{
Ville Syrjälä353c8592016-12-14 23:30:57 +02002779 int cpp = fb->format->cpp[plane];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002780
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002781 switch (fb->modifier) {
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002782 case DRM_FORMAT_MOD_NONE:
2783 case I915_FORMAT_MOD_X_TILED:
2784 switch (cpp) {
2785 case 8:
2786 return 4096;
2787 case 4:
2788 case 2:
2789 case 1:
2790 return 8192;
2791 default:
2792 MISSING_CASE(cpp);
2793 break;
2794 }
2795 break;
2796 case I915_FORMAT_MOD_Y_TILED:
2797 case I915_FORMAT_MOD_Yf_TILED:
2798 switch (cpp) {
2799 case 8:
2800 return 2048;
2801 case 4:
2802 return 4096;
2803 case 2:
2804 case 1:
2805 return 8192;
2806 default:
2807 MISSING_CASE(cpp);
2808 break;
2809 }
2810 break;
2811 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002812 MISSING_CASE(fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002813 }
2814
2815 return 2048;
2816}
2817
2818static int skl_check_main_surface(struct intel_plane_state *plane_state)
2819{
2820 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2821 const struct drm_framebuffer *fb = plane_state->base.fb;
2822 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002823 int x = plane_state->base.src.x1 >> 16;
2824 int y = plane_state->base.src.y1 >> 16;
2825 int w = drm_rect_width(&plane_state->base.src) >> 16;
2826 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002827 int max_width = skl_max_plane_width(fb, 0, rotation);
2828 int max_height = 4096;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002829 u32 alignment, offset, aux_offset = plane_state->aux.offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002830
2831 if (w > max_width || h > max_height) {
2832 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2833 w, h, max_width, max_height);
2834 return -EINVAL;
2835 }
2836
2837 intel_add_fb_offsets(&x, &y, plane_state, 0);
2838 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2839
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002840 alignment = intel_surf_alignment(dev_priv, fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002841
2842 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002843 * AUX surface offset is specified as the distance from the
2844 * main surface offset, and it must be non-negative. Make
2845 * sure that is what we will get.
2846 */
2847 if (offset > aux_offset)
2848 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2849 offset, aux_offset & ~(alignment - 1));
2850
2851 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002852 * When using an X-tiled surface, the plane blows up
2853 * if the x offset + width exceed the stride.
2854 *
2855 * TODO: linear and Y-tiled seem fine, Yf untested,
2856 */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002857 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
Ville Syrjälä353c8592016-12-14 23:30:57 +02002858 int cpp = fb->format->cpp[0];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002859
2860 while ((x + w) * cpp > fb->pitches[0]) {
2861 if (offset == 0) {
2862 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2863 return -EINVAL;
2864 }
2865
2866 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2867 offset, offset - alignment);
2868 }
2869 }
2870
2871 plane_state->main.offset = offset;
2872 plane_state->main.x = x;
2873 plane_state->main.y = y;
2874
2875 return 0;
2876}
2877
Ville Syrjälä8d970652016-01-28 16:30:28 +02002878static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2879{
2880 const struct drm_framebuffer *fb = plane_state->base.fb;
2881 unsigned int rotation = plane_state->base.rotation;
2882 int max_width = skl_max_plane_width(fb, 1, rotation);
2883 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02002884 int x = plane_state->base.src.x1 >> 17;
2885 int y = plane_state->base.src.y1 >> 17;
2886 int w = drm_rect_width(&plane_state->base.src) >> 17;
2887 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002888 u32 offset;
2889
2890 intel_add_fb_offsets(&x, &y, plane_state, 1);
2891 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2892
2893 /* FIXME not quite sure how/if these apply to the chroma plane */
2894 if (w > max_width || h > max_height) {
2895 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2896 w, h, max_width, max_height);
2897 return -EINVAL;
2898 }
2899
2900 plane_state->aux.offset = offset;
2901 plane_state->aux.x = x;
2902 plane_state->aux.y = y;
2903
2904 return 0;
2905}
2906
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002907int skl_check_plane_surface(struct intel_plane_state *plane_state)
2908{
2909 const struct drm_framebuffer *fb = plane_state->base.fb;
2910 unsigned int rotation = plane_state->base.rotation;
2911 int ret;
2912
Ville Syrjäläa5e4c7d2016-11-07 22:20:54 +02002913 if (!plane_state->base.visible)
2914 return 0;
2915
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002916 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002917 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02002918 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03002919 fb->width << 16, fb->height << 16,
2920 DRM_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002921
Ville Syrjälä8d970652016-01-28 16:30:28 +02002922 /*
2923 * Handle the AUX surface first since
2924 * the main surface setup depends on it.
2925 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002926 if (fb->format->format == DRM_FORMAT_NV12) {
Ville Syrjälä8d970652016-01-28 16:30:28 +02002927 ret = skl_check_nv12_aux_surface(plane_state);
2928 if (ret)
2929 return ret;
2930 } else {
2931 plane_state->aux.offset = ~0xfff;
2932 plane_state->aux.x = 0;
2933 plane_state->aux.y = 0;
2934 }
2935
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002936 ret = skl_check_main_surface(plane_state);
2937 if (ret)
2938 return ret;
2939
2940 return 0;
2941}
2942
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002943static void i9xx_update_primary_plane(struct drm_plane *primary,
2944 const struct intel_crtc_state *crtc_state,
2945 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002946{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00002947 struct drm_i915_private *dev_priv = to_i915(primary->dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2949 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes81255562010-08-02 12:07:50 -07002950 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002951 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002952 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002953 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002954 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002955 int x = plane_state->base.src.x1 >> 16;
2956 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002957
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002958 dspcntr = DISPPLANE_GAMMA_ENABLE;
2959
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002960 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002961
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00002962 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002963 if (intel_crtc->pipe == PIPE_B)
2964 dspcntr |= DISPPLANE_SEL_PIPE_B;
2965
2966 /* pipesrc and dspsize control the size that is scaled from,
2967 * which should always be the user's requested size.
2968 */
2969 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002970 ((crtc_state->pipe_src_h - 1) << 16) |
2971 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002972 I915_WRITE(DSPPOS(plane), 0);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002973 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002974 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002975 ((crtc_state->pipe_src_h - 1) << 16) |
2976 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002977 I915_WRITE(PRIMPOS(plane), 0);
2978 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002979 }
2980
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002981 switch (fb->format->format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02002982 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002983 dspcntr |= DISPPLANE_8BPP;
2984 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002985 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002986 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002987 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002988 case DRM_FORMAT_RGB565:
2989 dspcntr |= DISPPLANE_BGRX565;
2990 break;
2991 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002992 dspcntr |= DISPPLANE_BGRX888;
2993 break;
2994 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002995 dspcntr |= DISPPLANE_RGBX888;
2996 break;
2997 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002998 dspcntr |= DISPPLANE_BGRX101010;
2999 break;
3000 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003001 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003002 break;
3003 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003004 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07003005 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003006
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003007 if (INTEL_GEN(dev_priv) >= 4 &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003008 fb->modifier == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003009 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003010
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003011 if (rotation & DRM_ROTATE_180)
3012 dspcntr |= DISPPLANE_ROTATE_180;
3013
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003014 if (rotation & DRM_REFLECT_X)
3015 dspcntr |= DISPPLANE_MIRROR;
3016
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01003017 if (IS_G4X(dev_priv))
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003018 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3019
Ville Syrjälä29490562016-01-20 18:02:50 +02003020 intel_add_fb_offsets(&x, &y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003021
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003022 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetterc2c75132012-07-05 12:17:30 +02003023 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003024 intel_compute_tile_offset(&x, &y, plane_state, 0);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003025
Ville Syrjäläf22aa142016-11-14 18:53:58 +02003026 if (rotation & DRM_ROTATE_180) {
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003027 x += crtc_state->pipe_src_w - 1;
3028 y += crtc_state->pipe_src_h - 1;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003029 } else if (rotation & DRM_REFLECT_X) {
3030 x += crtc_state->pipe_src_w - 1;
Sonika Jindal48404c12014-08-22 14:06:04 +05303031 }
3032
Ville Syrjälä29490562016-01-20 18:02:50 +02003033 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003034
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003035 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä6687c902015-09-15 13:16:41 +03003036 intel_crtc->dspaddr_offset = linear_offset;
3037
Paulo Zanoni2db33662015-09-14 15:20:03 -03003038 intel_crtc->adjusted_x = x;
3039 intel_crtc->adjusted_y = y;
3040
Sonika Jindal48404c12014-08-22 14:06:04 +05303041 I915_WRITE(reg, dspcntr);
3042
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003043 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003044 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003045 I915_WRITE(DSPSURF(plane),
Chris Wilsonbe1e3412017-01-16 15:21:27 +00003046 intel_plane_ggtt_offset(plane_state) +
Ville Syrjälä6687c902015-09-15 13:16:41 +03003047 intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003048 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003049 I915_WRITE(DSPLINOFF(plane), linear_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003050 } else {
3051 I915_WRITE(DSPADDR(plane),
Chris Wilsonbe1e3412017-01-16 15:21:27 +00003052 intel_plane_ggtt_offset(plane_state) +
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003053 intel_crtc->dspaddr_offset);
3054 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003055 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003056}
3057
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003058static void i9xx_disable_primary_plane(struct drm_plane *primary,
3059 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003060{
3061 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003062 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003064 int plane = intel_crtc->plane;
3065
3066 I915_WRITE(DSPCNTR(plane), 0);
3067 if (INTEL_INFO(dev_priv)->gen >= 4)
3068 I915_WRITE(DSPSURF(plane), 0);
3069 else
3070 I915_WRITE(DSPADDR(plane), 0);
3071 POSTING_READ(DSPCNTR(plane));
3072}
3073
3074static void ironlake_update_primary_plane(struct drm_plane *primary,
3075 const struct intel_crtc_state *crtc_state,
3076 const struct intel_plane_state *plane_state)
3077{
3078 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003079 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3081 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003082 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003083 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003084 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003085 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003086 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003087 int x = plane_state->base.src.x1 >> 16;
3088 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003089
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003090 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003091 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003092
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003093 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003094 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
3095
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003096 switch (fb->format->format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02003097 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07003098 dspcntr |= DISPPLANE_8BPP;
3099 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003100 case DRM_FORMAT_RGB565:
3101 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003102 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003103 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003104 dspcntr |= DISPPLANE_BGRX888;
3105 break;
3106 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003107 dspcntr |= DISPPLANE_RGBX888;
3108 break;
3109 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003110 dspcntr |= DISPPLANE_BGRX101010;
3111 break;
3112 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003113 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003114 break;
3115 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003116 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07003117 }
3118
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003119 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003120 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003121
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003122 if (rotation & DRM_ROTATE_180)
3123 dspcntr |= DISPPLANE_ROTATE_180;
3124
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003125 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03003126 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003127
Ville Syrjälä29490562016-01-20 18:02:50 +02003128 intel_add_fb_offsets(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003129
Daniel Vetterc2c75132012-07-05 12:17:30 +02003130 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003131 intel_compute_tile_offset(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003132
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003133 /* HSW+ does this automagically in hardware */
3134 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3135 rotation & DRM_ROTATE_180) {
3136 x += crtc_state->pipe_src_w - 1;
3137 y += crtc_state->pipe_src_h - 1;
Sonika Jindal48404c12014-08-22 14:06:04 +05303138 }
3139
Ville Syrjälä29490562016-01-20 18:02:50 +02003140 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003141
Paulo Zanoni2db33662015-09-14 15:20:03 -03003142 intel_crtc->adjusted_x = x;
3143 intel_crtc->adjusted_y = y;
3144
Sonika Jindal48404c12014-08-22 14:06:04 +05303145 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003146
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003147 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003148 I915_WRITE(DSPSURF(plane),
Chris Wilsonbe1e3412017-01-16 15:21:27 +00003149 intel_plane_ggtt_offset(plane_state) +
Ville Syrjälä6687c902015-09-15 13:16:41 +03003150 intel_crtc->dspaddr_offset);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003151 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00003152 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3153 } else {
3154 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3155 I915_WRITE(DSPLINOFF(plane), linear_offset);
3156 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07003157 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003158}
3159
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003160u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3161 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00003162{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003163 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
3164 return 64;
3165 } else {
3166 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00003167
Ville Syrjälä27ba3912016-02-15 22:54:40 +02003168 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00003169 }
3170}
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{
3202 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3203 u32 stride = intel_fb_pitch(fb, plane, rotation);
3204
3205 /*
3206 * The stride is either expressed as a multiple of 64 bytes chunks for
3207 * linear buffers or in number of tiles for tiled buffers.
3208 */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003209 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä353c8592016-12-14 23:30:57 +02003210 int cpp = fb->format->cpp[plane];
Ville Syrjäläd2196772016-01-28 18:33:11 +02003211
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003212 stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003213 } else {
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003214 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003215 fb->format->format);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003216 }
3217
3218 return stride;
3219}
3220
Chandra Konduru6156a452015-04-27 13:48:39 -07003221u32 skl_plane_ctl_format(uint32_t pixel_format)
3222{
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
3262u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3263{
Chandra Konduru6156a452015-04-27 13:48:39 -07003264 switch (fb_modifier) {
3265 case DRM_FORMAT_MOD_NONE:
3266 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
3280u32 skl_plane_ctl_rotation(unsigned int rotation)
3281{
Chandra Konduru6156a452015-04-27 13:48:39 -07003282 switch (rotation) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003283 case DRM_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003284 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303285 /*
3286 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3287 * while i915 HW rotation is clockwise, thats why this swapping.
3288 */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003289 case DRM_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303290 return PLANE_CTL_ROTATE_270;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003291 case DRM_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003292 return PLANE_CTL_ROTATE_180;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003293 case DRM_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
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003302static void skylake_update_primary_plane(struct drm_plane *plane,
3303 const struct intel_crtc_state *crtc_state,
3304 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003305{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003306 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003307 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3309 struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003310 enum plane_id plane_id = to_intel_plane(plane)->id;
3311 enum pipe pipe = to_intel_plane(plane)->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003312 u32 plane_ctl;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003313 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003314 u32 stride = skl_plane_stride(fb, 0, rotation);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003315 u32 surf_addr = plane_state->main.offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003316 int scaler_id = plane_state->scaler_id;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003317 int src_x = plane_state->main.x;
3318 int src_y = plane_state->main.y;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003319 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3320 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3321 int dst_x = plane_state->base.dst.x1;
3322 int dst_y = plane_state->base.dst.y1;
3323 int dst_w = drm_rect_width(&plane_state->base.dst);
3324 int dst_h = drm_rect_height(&plane_state->base.dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003325
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003326 plane_ctl = PLANE_CTL_ENABLE;
3327
3328 if (IS_GEMINILAKE(dev_priv)) {
3329 I915_WRITE(PLANE_COLOR_CTL(pipe, plane_id),
3330 PLANE_COLOR_PIPE_GAMMA_ENABLE |
Ander Conselvan de Oliveira3bb56da2017-02-17 14:06:29 +02003331 PLANE_COLOR_PIPE_CSC_ENABLE |
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003332 PLANE_COLOR_PLANE_GAMMA_DISABLE);
3333 } else {
3334 plane_ctl |=
3335 PLANE_CTL_PIPE_GAMMA_ENABLE |
3336 PLANE_CTL_PIPE_CSC_ENABLE |
3337 PLANE_CTL_PLANE_GAMMA_DISABLE;
3338 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003339
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003340 plane_ctl |= skl_plane_ctl_format(fb->format->format);
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003341 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
Chandra Konduru6156a452015-04-27 13:48:39 -07003342 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003343
Ville Syrjälä6687c902015-09-15 13:16:41 +03003344 /* Sizes are 0 based */
3345 src_w--;
3346 src_h--;
3347 dst_w--;
3348 dst_h--;
3349
Paulo Zanoni4c0b8a82016-08-19 19:03:23 -03003350 intel_crtc->dspaddr_offset = surf_addr;
3351
Ville Syrjälä6687c902015-09-15 13:16:41 +03003352 intel_crtc->adjusted_x = src_x;
3353 intel_crtc->adjusted_y = src_y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003354
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003355 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3356 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3357 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3358 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
Chandra Konduru6156a452015-04-27 13:48:39 -07003359
3360 if (scaler_id >= 0) {
3361 uint32_t ps_ctrl = 0;
3362
3363 WARN_ON(!dst_w || !dst_h);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003364 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
Chandra Konduru6156a452015-04-27 13:48:39 -07003365 crtc_state->scaler_state.scalers[scaler_id].mode;
3366 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3367 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3368 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3369 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003370 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003371 } else {
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003372 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
Chandra Konduru6156a452015-04-27 13:48:39 -07003373 }
3374
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003375 I915_WRITE(PLANE_SURF(pipe, plane_id),
Chris Wilsonbe1e3412017-01-16 15:21:27 +00003376 intel_plane_ggtt_offset(plane_state) + surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003377
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003378 POSTING_READ(PLANE_SURF(pipe, plane_id));
Damien Lespiau70d21f02013-07-03 21:06:04 +01003379}
3380
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003381static void skylake_disable_primary_plane(struct drm_plane *primary,
3382 struct drm_crtc *crtc)
3383{
3384 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003385 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003386 enum plane_id plane_id = to_intel_plane(primary)->id;
3387 enum pipe pipe = to_intel_plane(primary)->pipe;
Lyude62e0fb82016-08-22 12:50:08 -04003388
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003389 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3390 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3391 POSTING_READ(PLANE_SURF(pipe, plane_id));
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003392}
3393
Jesse Barnes17638cd2011-06-24 12:19:23 -07003394/* Assume fb object is pinned & idle & fenced and just update base pointers */
3395static int
3396intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3397 int x, int y, enum mode_set_atomic state)
3398{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003399 /* Support for kgdboc is disabled, this needs a major rework. */
3400 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003401
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003402 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003403}
3404
Daniel Vetter5a21b662016-05-24 17:13:53 +02003405static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3406{
3407 struct intel_crtc *crtc;
3408
Chris Wilson91c8a322016-07-05 10:40:23 +01003409 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003410 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3411}
3412
Ville Syrjälä75147472014-11-24 18:28:11 +02003413static void intel_update_primary_planes(struct drm_device *dev)
3414{
Ville Syrjälä75147472014-11-24 18:28:11 +02003415 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003416
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003417 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003418 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003419 struct intel_plane_state *plane_state =
3420 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003421
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003422 if (plane_state->base.visible)
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003423 plane->update_plane(&plane->base,
3424 to_intel_crtc_state(crtc->state),
3425 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003426 }
3427}
3428
Maarten Lankhorst73974892016-08-05 23:28:27 +03003429static int
3430__intel_display_resume(struct drm_device *dev,
3431 struct drm_atomic_state *state)
3432{
3433 struct drm_crtc_state *crtc_state;
3434 struct drm_crtc *crtc;
3435 int i, ret;
3436
3437 intel_modeset_setup_hw_state(dev);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003438 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003439
3440 if (!state)
3441 return 0;
3442
3443 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3444 /*
3445 * Force recalculation even if we restore
3446 * current state. With fast modeset this may not result
3447 * in a modeset when the state is compatible.
3448 */
3449 crtc_state->mode_changed = true;
3450 }
3451
3452 /* ignore any reset values/BIOS leftovers in the WM registers */
3453 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3454
3455 ret = drm_atomic_commit(state);
3456
3457 WARN_ON(ret == -EDEADLK);
3458 return ret;
3459}
3460
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003461static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3462{
Ville Syrjäläae981042016-08-05 23:28:30 +03003463 return intel_has_gpu_reset(dev_priv) &&
3464 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003465}
3466
Chris Wilsonc0336662016-05-06 15:40:21 +01003467void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003468{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003469 struct drm_device *dev = &dev_priv->drm;
3470 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3471 struct drm_atomic_state *state;
3472 int ret;
3473
Maarten Lankhorst73974892016-08-05 23:28:27 +03003474 /*
3475 * Need mode_config.mutex so that we don't
3476 * trample ongoing ->detect() and whatnot.
3477 */
3478 mutex_lock(&dev->mode_config.mutex);
3479 drm_modeset_acquire_init(ctx, 0);
3480 while (1) {
3481 ret = drm_modeset_lock_all_ctx(dev, ctx);
3482 if (ret != -EDEADLK)
3483 break;
3484
3485 drm_modeset_backoff(ctx);
3486 }
3487
3488 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003489 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003490 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003491 return;
3492
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003493 /*
3494 * Disabling the crtcs gracefully seems nicer. Also the
3495 * g33 docs say we should at least disable all the planes.
3496 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003497 state = drm_atomic_helper_duplicate_state(dev, ctx);
3498 if (IS_ERR(state)) {
3499 ret = PTR_ERR(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003500 DRM_ERROR("Duplicating state failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003501 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003502 }
3503
3504 ret = drm_atomic_helper_disable_all(dev, ctx);
3505 if (ret) {
3506 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003507 drm_atomic_state_put(state);
3508 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003509 }
3510
3511 dev_priv->modeset_restore_state = state;
3512 state->acquire_ctx = ctx;
Ville Syrjälä75147472014-11-24 18:28:11 +02003513}
3514
Chris Wilsonc0336662016-05-06 15:40:21 +01003515void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003516{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003517 struct drm_device *dev = &dev_priv->drm;
3518 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3519 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3520 int ret;
3521
Daniel Vetter5a21b662016-05-24 17:13:53 +02003522 /*
3523 * Flips in the rings will be nuked by the reset,
3524 * so complete all pending flips so that user space
3525 * will get its events and not get stuck.
3526 */
3527 intel_complete_page_flips(dev_priv);
3528
Maarten Lankhorst73974892016-08-05 23:28:27 +03003529 dev_priv->modeset_restore_state = NULL;
3530
Ville Syrjälä75147472014-11-24 18:28:11 +02003531 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003532 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003533 if (!state) {
3534 /*
3535 * Flips in the rings have been nuked by the reset,
3536 * so update the base address of all primary
3537 * planes to the the last fb to make sure we're
3538 * showing the correct fb after a reset.
3539 *
3540 * FIXME: Atomic will make this obsolete since we won't schedule
3541 * CS-based flips (which might get lost in gpu resets) any more.
3542 */
3543 intel_update_primary_planes(dev);
3544 } else {
3545 ret = __intel_display_resume(dev, state);
3546 if (ret)
3547 DRM_ERROR("Restoring old state failed with %i\n", ret);
3548 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003549 } else {
3550 /*
3551 * The display has been reset as well,
3552 * so need a full re-initialization.
3553 */
3554 intel_runtime_pm_disable_interrupts(dev_priv);
3555 intel_runtime_pm_enable_interrupts(dev_priv);
3556
Imre Deak51f59202016-09-14 13:04:13 +03003557 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003558 intel_modeset_init_hw(dev);
3559
3560 spin_lock_irq(&dev_priv->irq_lock);
3561 if (dev_priv->display.hpd_irq_setup)
3562 dev_priv->display.hpd_irq_setup(dev_priv);
3563 spin_unlock_irq(&dev_priv->irq_lock);
3564
3565 ret = __intel_display_resume(dev, state);
3566 if (ret)
3567 DRM_ERROR("Restoring old state failed with %i\n", ret);
3568
3569 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003570 }
3571
Chris Wilson08536952016-10-14 13:18:18 +01003572 if (state)
3573 drm_atomic_state_put(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003574 drm_modeset_drop_locks(ctx);
3575 drm_modeset_acquire_fini(ctx);
3576 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003577}
3578
Chris Wilson8af29b02016-09-09 14:11:47 +01003579static bool abort_flip_on_reset(struct intel_crtc *crtc)
3580{
3581 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3582
3583 if (i915_reset_in_progress(error))
3584 return true;
3585
3586 if (crtc->reset_count != i915_reset_count(error))
3587 return true;
3588
3589 return false;
3590}
3591
Chris Wilson7d5e3792014-03-04 13:15:08 +00003592static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3593{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003594 struct drm_device *dev = crtc->dev;
3595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003596 bool pending;
3597
Chris Wilson8af29b02016-09-09 14:11:47 +01003598 if (abort_flip_on_reset(intel_crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +02003599 return false;
3600
3601 spin_lock_irq(&dev->event_lock);
3602 pending = to_intel_crtc(crtc)->flip_work != NULL;
3603 spin_unlock_irq(&dev->event_lock);
3604
3605 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003606}
3607
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003608static void intel_update_pipe_config(struct intel_crtc *crtc,
3609 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003610{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003611 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003612 struct intel_crtc_state *pipe_config =
3613 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003614
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003615 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3616 crtc->base.mode = crtc->base.state->mode;
3617
3618 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3619 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3620 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003621
3622 /*
3623 * Update pipe size and adjust fitter if needed: the reason for this is
3624 * that in compute_mode_changes we check the native mode (not the pfit
3625 * mode) to see if we can flip rather than do a full mode set. In the
3626 * fastboot case, we'll flip, but if we don't update the pipesrc and
3627 * pfit state, we'll end up with a big fb scanned out into the wrong
3628 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003629 */
3630
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003631 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003632 ((pipe_config->pipe_src_w - 1) << 16) |
3633 (pipe_config->pipe_src_h - 1));
3634
3635 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003636 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003637 skl_detach_scalers(crtc);
3638
3639 if (pipe_config->pch_pfit.enabled)
3640 skylake_pfit_enable(crtc);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003641 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003642 if (pipe_config->pch_pfit.enabled)
3643 ironlake_pfit_enable(crtc);
3644 else if (old_crtc_state->pch_pfit.enabled)
3645 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003646 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003647}
3648
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003649static void intel_fdi_normal_train(struct drm_crtc *crtc)
3650{
3651 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003652 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3654 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003655 i915_reg_t reg;
3656 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003657
3658 /* enable normal train */
3659 reg = FDI_TX_CTL(pipe);
3660 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003661 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003662 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3663 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003664 } else {
3665 temp &= ~FDI_LINK_TRAIN_NONE;
3666 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003667 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003668 I915_WRITE(reg, temp);
3669
3670 reg = FDI_RX_CTL(pipe);
3671 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003672 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003673 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3674 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3675 } else {
3676 temp &= ~FDI_LINK_TRAIN_NONE;
3677 temp |= FDI_LINK_TRAIN_NONE;
3678 }
3679 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3680
3681 /* wait one idle pattern time */
3682 POSTING_READ(reg);
3683 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003684
3685 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003686 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003687 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3688 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003689}
3690
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003691/* The FDI link training functions for ILK/Ibexpeak. */
3692static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3693{
3694 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003695 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3697 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003698 i915_reg_t reg;
3699 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003700
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003701 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003702 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003703
Adam Jacksone1a44742010-06-25 15:32:14 -04003704 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3705 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003706 reg = FDI_RX_IMR(pipe);
3707 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003708 temp &= ~FDI_RX_SYMBOL_LOCK;
3709 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003710 I915_WRITE(reg, temp);
3711 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003712 udelay(150);
3713
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003714 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003715 reg = FDI_TX_CTL(pipe);
3716 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003717 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003718 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003719 temp &= ~FDI_LINK_TRAIN_NONE;
3720 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003721 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003722
Chris Wilson5eddb702010-09-11 13:48:45 +01003723 reg = FDI_RX_CTL(pipe);
3724 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003725 temp &= ~FDI_LINK_TRAIN_NONE;
3726 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003727 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3728
3729 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003730 udelay(150);
3731
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003732 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003733 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3734 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3735 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003736
Chris Wilson5eddb702010-09-11 13:48:45 +01003737 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003738 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003739 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003740 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3741
3742 if ((temp & FDI_RX_BIT_LOCK)) {
3743 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003744 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003745 break;
3746 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003747 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003748 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003749 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003750
3751 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003752 reg = FDI_TX_CTL(pipe);
3753 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003754 temp &= ~FDI_LINK_TRAIN_NONE;
3755 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003756 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003757
Chris Wilson5eddb702010-09-11 13:48:45 +01003758 reg = FDI_RX_CTL(pipe);
3759 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003760 temp &= ~FDI_LINK_TRAIN_NONE;
3761 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003762 I915_WRITE(reg, temp);
3763
3764 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003765 udelay(150);
3766
Chris Wilson5eddb702010-09-11 13:48:45 +01003767 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003768 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003769 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003770 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3771
3772 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003773 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003774 DRM_DEBUG_KMS("FDI train 2 done.\n");
3775 break;
3776 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003777 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003778 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003779 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003780
3781 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003782
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003783}
3784
Akshay Joshi0206e352011-08-16 15:34:10 -04003785static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003786 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3787 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3788 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3789 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3790};
3791
3792/* The FDI link training functions for SNB/Cougarpoint. */
3793static void gen6_fdi_link_train(struct drm_crtc *crtc)
3794{
3795 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003796 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3798 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003799 i915_reg_t reg;
3800 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003801
Adam Jacksone1a44742010-06-25 15:32:14 -04003802 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3803 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003804 reg = FDI_RX_IMR(pipe);
3805 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003806 temp &= ~FDI_RX_SYMBOL_LOCK;
3807 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003808 I915_WRITE(reg, temp);
3809
3810 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003811 udelay(150);
3812
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003813 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003814 reg = FDI_TX_CTL(pipe);
3815 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003816 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003817 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003818 temp &= ~FDI_LINK_TRAIN_NONE;
3819 temp |= FDI_LINK_TRAIN_PATTERN_1;
3820 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3821 /* SNB-B */
3822 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003823 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003824
Daniel Vetterd74cf322012-10-26 10:58:13 +02003825 I915_WRITE(FDI_RX_MISC(pipe),
3826 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3827
Chris Wilson5eddb702010-09-11 13:48:45 +01003828 reg = FDI_RX_CTL(pipe);
3829 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003830 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003831 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3832 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3833 } else {
3834 temp &= ~FDI_LINK_TRAIN_NONE;
3835 temp |= FDI_LINK_TRAIN_PATTERN_1;
3836 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003837 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3838
3839 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003840 udelay(150);
3841
Akshay Joshi0206e352011-08-16 15:34:10 -04003842 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003843 reg = FDI_TX_CTL(pipe);
3844 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003845 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3846 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003847 I915_WRITE(reg, temp);
3848
3849 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003850 udelay(500);
3851
Sean Paulfa37d392012-03-02 12:53:39 -05003852 for (retry = 0; retry < 5; retry++) {
3853 reg = FDI_RX_IIR(pipe);
3854 temp = I915_READ(reg);
3855 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3856 if (temp & FDI_RX_BIT_LOCK) {
3857 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3858 DRM_DEBUG_KMS("FDI train 1 done.\n");
3859 break;
3860 }
3861 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003862 }
Sean Paulfa37d392012-03-02 12:53:39 -05003863 if (retry < 5)
3864 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003865 }
3866 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003867 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003868
3869 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003870 reg = FDI_TX_CTL(pipe);
3871 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003872 temp &= ~FDI_LINK_TRAIN_NONE;
3873 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003874 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003875 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3876 /* SNB-B */
3877 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3878 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003879 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003880
Chris Wilson5eddb702010-09-11 13:48:45 +01003881 reg = FDI_RX_CTL(pipe);
3882 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003883 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003884 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3885 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3886 } else {
3887 temp &= ~FDI_LINK_TRAIN_NONE;
3888 temp |= FDI_LINK_TRAIN_PATTERN_2;
3889 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003890 I915_WRITE(reg, temp);
3891
3892 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003893 udelay(150);
3894
Akshay Joshi0206e352011-08-16 15:34:10 -04003895 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003896 reg = FDI_TX_CTL(pipe);
3897 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003898 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3899 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003900 I915_WRITE(reg, temp);
3901
3902 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003903 udelay(500);
3904
Sean Paulfa37d392012-03-02 12:53:39 -05003905 for (retry = 0; retry < 5; retry++) {
3906 reg = FDI_RX_IIR(pipe);
3907 temp = I915_READ(reg);
3908 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3909 if (temp & FDI_RX_SYMBOL_LOCK) {
3910 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3911 DRM_DEBUG_KMS("FDI train 2 done.\n");
3912 break;
3913 }
3914 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003915 }
Sean Paulfa37d392012-03-02 12:53:39 -05003916 if (retry < 5)
3917 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003918 }
3919 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003920 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003921
3922 DRM_DEBUG_KMS("FDI train done.\n");
3923}
3924
Jesse Barnes357555c2011-04-28 15:09:55 -07003925/* Manual link training for Ivy Bridge A0 parts */
3926static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3927{
3928 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003929 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3931 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003932 i915_reg_t reg;
3933 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003934
3935 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3936 for train result */
3937 reg = FDI_RX_IMR(pipe);
3938 temp = I915_READ(reg);
3939 temp &= ~FDI_RX_SYMBOL_LOCK;
3940 temp &= ~FDI_RX_BIT_LOCK;
3941 I915_WRITE(reg, temp);
3942
3943 POSTING_READ(reg);
3944 udelay(150);
3945
Daniel Vetter01a415f2012-10-27 15:58:40 +02003946 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3947 I915_READ(FDI_RX_IIR(pipe)));
3948
Jesse Barnes139ccd32013-08-19 11:04:55 -07003949 /* Try each vswing and preemphasis setting twice before moving on */
3950 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3951 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003952 reg = FDI_TX_CTL(pipe);
3953 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003954 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3955 temp &= ~FDI_TX_ENABLE;
3956 I915_WRITE(reg, temp);
3957
3958 reg = FDI_RX_CTL(pipe);
3959 temp = I915_READ(reg);
3960 temp &= ~FDI_LINK_TRAIN_AUTO;
3961 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3962 temp &= ~FDI_RX_ENABLE;
3963 I915_WRITE(reg, temp);
3964
3965 /* enable CPU FDI TX and PCH FDI RX */
3966 reg = FDI_TX_CTL(pipe);
3967 temp = I915_READ(reg);
3968 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003969 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003970 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003971 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003972 temp |= snb_b_fdi_train_param[j/2];
3973 temp |= FDI_COMPOSITE_SYNC;
3974 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3975
3976 I915_WRITE(FDI_RX_MISC(pipe),
3977 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3978
3979 reg = FDI_RX_CTL(pipe);
3980 temp = I915_READ(reg);
3981 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3982 temp |= FDI_COMPOSITE_SYNC;
3983 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3984
3985 POSTING_READ(reg);
3986 udelay(1); /* should be 0.5us */
3987
3988 for (i = 0; i < 4; i++) {
3989 reg = FDI_RX_IIR(pipe);
3990 temp = I915_READ(reg);
3991 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3992
3993 if (temp & FDI_RX_BIT_LOCK ||
3994 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3995 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3996 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3997 i);
3998 break;
3999 }
4000 udelay(1); /* should be 0.5us */
4001 }
4002 if (i == 4) {
4003 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4004 continue;
4005 }
4006
4007 /* Train 2 */
4008 reg = FDI_TX_CTL(pipe);
4009 temp = I915_READ(reg);
4010 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4011 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4012 I915_WRITE(reg, temp);
4013
4014 reg = FDI_RX_CTL(pipe);
4015 temp = I915_READ(reg);
4016 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4017 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004018 I915_WRITE(reg, temp);
4019
4020 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004021 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004022
Jesse Barnes139ccd32013-08-19 11:04:55 -07004023 for (i = 0; i < 4; i++) {
4024 reg = FDI_RX_IIR(pipe);
4025 temp = I915_READ(reg);
4026 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004027
Jesse Barnes139ccd32013-08-19 11:04:55 -07004028 if (temp & FDI_RX_SYMBOL_LOCK ||
4029 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4030 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4031 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4032 i);
4033 goto train_done;
4034 }
4035 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004036 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004037 if (i == 4)
4038 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004039 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004040
Jesse Barnes139ccd32013-08-19 11:04:55 -07004041train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004042 DRM_DEBUG_KMS("FDI train done.\n");
4043}
4044
Daniel Vetter88cefb62012-08-12 19:27:14 +02004045static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004046{
Daniel Vetter88cefb62012-08-12 19:27:14 +02004047 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004048 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004049 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004050 i915_reg_t reg;
4051 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004052
Jesse Barnes0e23b992010-09-10 11:10:00 -07004053 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004054 reg = FDI_RX_CTL(pipe);
4055 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004056 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004057 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004058 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004059 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4060
4061 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004062 udelay(200);
4063
4064 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004065 temp = I915_READ(reg);
4066 I915_WRITE(reg, temp | FDI_PCDCLK);
4067
4068 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004069 udelay(200);
4070
Paulo Zanoni20749732012-11-23 15:30:38 -02004071 /* Enable CPU FDI TX PLL, always on for Ironlake */
4072 reg = FDI_TX_CTL(pipe);
4073 temp = I915_READ(reg);
4074 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4075 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004076
Paulo Zanoni20749732012-11-23 15:30:38 -02004077 POSTING_READ(reg);
4078 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004079 }
4080}
4081
Daniel Vetter88cefb62012-08-12 19:27:14 +02004082static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4083{
4084 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004085 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004086 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004087 i915_reg_t reg;
4088 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004089
4090 /* Switch from PCDclk to Rawclk */
4091 reg = FDI_RX_CTL(pipe);
4092 temp = I915_READ(reg);
4093 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4094
4095 /* Disable CPU FDI TX PLL */
4096 reg = FDI_TX_CTL(pipe);
4097 temp = I915_READ(reg);
4098 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4099
4100 POSTING_READ(reg);
4101 udelay(100);
4102
4103 reg = FDI_RX_CTL(pipe);
4104 temp = I915_READ(reg);
4105 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4106
4107 /* Wait for the clocks to turn off. */
4108 POSTING_READ(reg);
4109 udelay(100);
4110}
4111
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004112static void ironlake_fdi_disable(struct drm_crtc *crtc)
4113{
4114 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004115 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4117 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004118 i915_reg_t reg;
4119 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004120
4121 /* disable CPU FDI tx and PCH FDI rx */
4122 reg = FDI_TX_CTL(pipe);
4123 temp = I915_READ(reg);
4124 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4125 POSTING_READ(reg);
4126
4127 reg = FDI_RX_CTL(pipe);
4128 temp = I915_READ(reg);
4129 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004130 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004131 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4132
4133 POSTING_READ(reg);
4134 udelay(100);
4135
4136 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004137 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004138 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004139
4140 /* still set train pattern 1 */
4141 reg = FDI_TX_CTL(pipe);
4142 temp = I915_READ(reg);
4143 temp &= ~FDI_LINK_TRAIN_NONE;
4144 temp |= FDI_LINK_TRAIN_PATTERN_1;
4145 I915_WRITE(reg, temp);
4146
4147 reg = FDI_RX_CTL(pipe);
4148 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004149 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004150 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4151 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4152 } else {
4153 temp &= ~FDI_LINK_TRAIN_NONE;
4154 temp |= FDI_LINK_TRAIN_PATTERN_1;
4155 }
4156 /* BPC in FDI rx is consistent with that in PIPECONF */
4157 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004158 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004159 I915_WRITE(reg, temp);
4160
4161 POSTING_READ(reg);
4162 udelay(100);
4163}
4164
Chris Wilson49d73912016-11-29 09:50:08 +00004165bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004166{
4167 struct intel_crtc *crtc;
4168
4169 /* Note that we don't need to be called with mode_config.lock here
4170 * as our list of CRTC objects is static for the lifetime of the
4171 * device and so cannot disappear as we iterate. Similarly, we can
4172 * happily treat the predicates as racy, atomic checks as userspace
4173 * cannot claim and pin a new fb without at least acquring the
4174 * struct_mutex and so serialising with us.
4175 */
Chris Wilson49d73912016-11-29 09:50:08 +00004176 for_each_intel_crtc(&dev_priv->drm, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00004177 if (atomic_read(&crtc->unpin_work_count) == 0)
4178 continue;
4179
Daniel Vetter5a21b662016-05-24 17:13:53 +02004180 if (crtc->flip_work)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004181 intel_wait_for_vblank(dev_priv, crtc->pipe);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004182
4183 return true;
4184 }
4185
4186 return false;
4187}
4188
Daniel Vetter5a21b662016-05-24 17:13:53 +02004189static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004190{
4191 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004192 struct intel_flip_work *work = intel_crtc->flip_work;
4193
4194 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004195
4196 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07004197 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004198
4199 drm_crtc_vblank_put(&intel_crtc->base);
4200
Daniel Vetter5a21b662016-05-24 17:13:53 +02004201 wake_up_all(&dev_priv->pending_flip_queue);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004202 trace_i915_flip_complete(intel_crtc->plane,
4203 work->pending_flip_obj);
Andrey Ryabinin05c41f92017-01-26 17:32:11 +03004204
4205 queue_work(dev_priv->wq, &work->unpin_work);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004206}
4207
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004208static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004209{
Chris Wilson0f911282012-04-17 10:05:38 +01004210 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004211 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004212 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004213
Daniel Vetter2c10d572012-12-20 21:24:07 +01004214 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004215
4216 ret = wait_event_interruptible_timeout(
4217 dev_priv->pending_flip_queue,
4218 !intel_crtc_has_pending_flip(crtc),
4219 60*HZ);
4220
4221 if (ret < 0)
4222 return ret;
4223
Daniel Vetter5a21b662016-05-24 17:13:53 +02004224 if (ret == 0) {
4225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4226 struct intel_flip_work *work;
4227
4228 spin_lock_irq(&dev->event_lock);
4229 work = intel_crtc->flip_work;
4230 if (work && !is_mmio_work(work)) {
4231 WARN_ONCE(1, "Removing stuck page flip\n");
4232 page_flip_completed(intel_crtc);
4233 }
4234 spin_unlock_irq(&dev->event_lock);
4235 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004236
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004237 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004238}
4239
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004240void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004241{
4242 u32 temp;
4243
4244 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4245
4246 mutex_lock(&dev_priv->sb_lock);
4247
4248 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4249 temp |= SBI_SSCCTL_DISABLE;
4250 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4251
4252 mutex_unlock(&dev_priv->sb_lock);
4253}
4254
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004255/* Program iCLKIP clock to the desired frequency */
4256static void lpt_program_iclkip(struct drm_crtc *crtc)
4257{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004258 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004259 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004260 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4261 u32 temp;
4262
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004263 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004264
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004265 /* The iCLK virtual clock root frequency is in MHz,
4266 * but the adjusted_mode->crtc_clock in in KHz. To get the
4267 * divisors, it is necessary to divide one by another, so we
4268 * convert the virtual clock precision to KHz here for higher
4269 * precision.
4270 */
4271 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004272 u32 iclk_virtual_root_freq = 172800 * 1000;
4273 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004274 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004275
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004276 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4277 clock << auxdiv);
4278 divsel = (desired_divisor / iclk_pi_range) - 2;
4279 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004280
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004281 /*
4282 * Near 20MHz is a corner case which is
4283 * out of range for the 7-bit divisor
4284 */
4285 if (divsel <= 0x7f)
4286 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004287 }
4288
4289 /* This should not happen with any sane values */
4290 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4291 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4292 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4293 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4294
4295 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 +03004296 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004297 auxdiv,
4298 divsel,
4299 phasedir,
4300 phaseinc);
4301
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004302 mutex_lock(&dev_priv->sb_lock);
4303
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004304 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004305 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004306 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4307 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4308 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4309 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4310 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4311 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004312 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004313
4314 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004315 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004316 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4317 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004318 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004319
4320 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004321 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004322 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004323 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004324
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004325 mutex_unlock(&dev_priv->sb_lock);
4326
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004327 /* Wait for initialization time */
4328 udelay(24);
4329
4330 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4331}
4332
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004333int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4334{
4335 u32 divsel, phaseinc, auxdiv;
4336 u32 iclk_virtual_root_freq = 172800 * 1000;
4337 u32 iclk_pi_range = 64;
4338 u32 desired_divisor;
4339 u32 temp;
4340
4341 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4342 return 0;
4343
4344 mutex_lock(&dev_priv->sb_lock);
4345
4346 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4347 if (temp & SBI_SSCCTL_DISABLE) {
4348 mutex_unlock(&dev_priv->sb_lock);
4349 return 0;
4350 }
4351
4352 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4353 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4354 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4355 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4356 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4357
4358 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4359 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4360 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4361
4362 mutex_unlock(&dev_priv->sb_lock);
4363
4364 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4365
4366 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4367 desired_divisor << auxdiv);
4368}
4369
Daniel Vetter275f01b22013-05-03 11:49:47 +02004370static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4371 enum pipe pch_transcoder)
4372{
4373 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004374 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004375 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004376
4377 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4378 I915_READ(HTOTAL(cpu_transcoder)));
4379 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4380 I915_READ(HBLANK(cpu_transcoder)));
4381 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4382 I915_READ(HSYNC(cpu_transcoder)));
4383
4384 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4385 I915_READ(VTOTAL(cpu_transcoder)));
4386 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4387 I915_READ(VBLANK(cpu_transcoder)));
4388 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4389 I915_READ(VSYNC(cpu_transcoder)));
4390 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4391 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4392}
4393
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004394static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004395{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004396 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004397 uint32_t temp;
4398
4399 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004400 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004401 return;
4402
4403 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4404 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4405
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004406 temp &= ~FDI_BC_BIFURCATION_SELECT;
4407 if (enable)
4408 temp |= FDI_BC_BIFURCATION_SELECT;
4409
4410 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004411 I915_WRITE(SOUTH_CHICKEN1, temp);
4412 POSTING_READ(SOUTH_CHICKEN1);
4413}
4414
4415static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4416{
4417 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004418
4419 switch (intel_crtc->pipe) {
4420 case PIPE_A:
4421 break;
4422 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004423 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004424 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004425 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004426 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004427
4428 break;
4429 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004430 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004431
4432 break;
4433 default:
4434 BUG();
4435 }
4436}
4437
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004438/* Return which DP Port should be selected for Transcoder DP control */
4439static enum port
4440intel_trans_dp_port_sel(struct drm_crtc *crtc)
4441{
4442 struct drm_device *dev = crtc->dev;
4443 struct intel_encoder *encoder;
4444
4445 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004446 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004447 encoder->type == INTEL_OUTPUT_EDP)
4448 return enc_to_dig_port(&encoder->base)->port;
4449 }
4450
4451 return -1;
4452}
4453
Jesse Barnesf67a5592011-01-05 10:31:48 -08004454/*
4455 * Enable PCH resources required for PCH ports:
4456 * - PCH PLLs
4457 * - FDI training & RX/TX
4458 * - update transcoder timings
4459 * - DP transcoding bits
4460 * - transcoder
4461 */
4462static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004463{
4464 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004465 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004466 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4467 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004468 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004469
Daniel Vetterab9412b2013-05-03 11:49:46 +02004470 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004471
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004472 if (IS_IVYBRIDGE(dev_priv))
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004473 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4474
Daniel Vettercd986ab2012-10-26 10:58:12 +02004475 /* Write the TU size bits before fdi link training, so that error
4476 * detection works. */
4477 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4478 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4479
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004480 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004481 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004482
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004483 /* We need to program the right clock selection before writing the pixel
4484 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004485 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004486 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004487
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004488 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004489 temp |= TRANS_DPLL_ENABLE(pipe);
4490 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004491 if (intel_crtc->config->shared_dpll ==
4492 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004493 temp |= sel;
4494 else
4495 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004496 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004497 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004498
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004499 /* XXX: pch pll's can be enabled any time before we enable the PCH
4500 * transcoder, and we actually should do this to not upset any PCH
4501 * transcoder that already use the clock when we share it.
4502 *
4503 * Note that enable_shared_dpll tries to do the right thing, but
4504 * get_shared_dpll unconditionally resets the pll - we need that to have
4505 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004506 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004507
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004508 /* set transcoder timing, panel must allow it */
4509 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004510 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004511
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004512 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004513
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004514 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004515 if (HAS_PCH_CPT(dev_priv) &&
4516 intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004517 const struct drm_display_mode *adjusted_mode =
4518 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004519 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004520 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004521 temp = I915_READ(reg);
4522 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004523 TRANS_DP_SYNC_MASK |
4524 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004525 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004526 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004527
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004528 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004529 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004530 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004531 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004532
4533 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004534 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004535 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004536 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004537 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004538 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004539 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004540 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004541 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004542 break;
4543 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004544 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004545 }
4546
Chris Wilson5eddb702010-09-11 13:48:45 +01004547 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004548 }
4549
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004550 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004551}
4552
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004553static void lpt_pch_enable(struct drm_crtc *crtc)
4554{
4555 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004556 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004558 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004559
Daniel Vetterab9412b2013-05-03 11:49:46 +02004560 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004561
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004562 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004563
Paulo Zanoni0540e482012-10-31 18:12:40 -02004564 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004565 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004566
Paulo Zanoni937bb612012-10-31 18:12:47 -02004567 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004568}
4569
Daniel Vettera1520312013-05-03 11:49:50 +02004570static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004571{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004572 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004573 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004574 u32 temp;
4575
4576 temp = I915_READ(dslreg);
4577 udelay(500);
4578 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004579 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004580 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004581 }
4582}
4583
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004584static int
4585skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4586 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4587 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004588{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004589 struct intel_crtc_scaler_state *scaler_state =
4590 &crtc_state->scaler_state;
4591 struct intel_crtc *intel_crtc =
4592 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004593 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004594
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004595 need_scaling = drm_rotation_90_or_270(rotation) ?
Chandra Konduru6156a452015-04-27 13:48:39 -07004596 (src_h != dst_w || src_w != dst_h):
4597 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004598
4599 /*
4600 * if plane is being disabled or scaler is no more required or force detach
4601 * - free scaler binded to this plane/crtc
4602 * - in order to do this, update crtc->scaler_usage
4603 *
4604 * Here scaler state in crtc_state is set free so that
4605 * scaler can be assigned to other user. Actual register
4606 * update to free the scaler is done in plane/panel-fit programming.
4607 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4608 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004609 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004610 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004611 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004612 scaler_state->scalers[*scaler_id].in_use = 0;
4613
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004614 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4615 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4616 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004617 scaler_state->scaler_users);
4618 *scaler_id = -1;
4619 }
4620 return 0;
4621 }
4622
4623 /* range checks */
4624 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4625 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4626
4627 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4628 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004629 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004630 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004631 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004632 return -EINVAL;
4633 }
4634
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004635 /* mark this plane as a scaler user in crtc_state */
4636 scaler_state->scaler_users |= (1 << scaler_user);
4637 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4638 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4639 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4640 scaler_state->scaler_users);
4641
4642 return 0;
4643}
4644
4645/**
4646 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4647 *
4648 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004649 *
4650 * Return
4651 * 0 - scaler_usage updated successfully
4652 * error - requested scaling cannot be supported or other error condition
4653 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004654int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004655{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004656 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004657
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004658 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004659 &state->scaler_state.scaler_id, DRM_ROTATE_0,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004660 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004661 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004662}
4663
4664/**
4665 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4666 *
4667 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004668 * @plane_state: atomic plane state to update
4669 *
4670 * Return
4671 * 0 - scaler_usage updated successfully
4672 * error - requested scaling cannot be supported or other error condition
4673 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004674static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4675 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004676{
4677
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004678 struct intel_plane *intel_plane =
4679 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004680 struct drm_framebuffer *fb = plane_state->base.fb;
4681 int ret;
4682
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004683 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004684
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004685 ret = skl_update_scaler(crtc_state, force_detach,
4686 drm_plane_index(&intel_plane->base),
4687 &plane_state->scaler_id,
4688 plane_state->base.rotation,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004689 drm_rect_width(&plane_state->base.src) >> 16,
4690 drm_rect_height(&plane_state->base.src) >> 16,
4691 drm_rect_width(&plane_state->base.dst),
4692 drm_rect_height(&plane_state->base.dst));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004693
4694 if (ret || plane_state->scaler_id < 0)
4695 return ret;
4696
Chandra Kondurua1b22782015-04-07 15:28:45 -07004697 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004698 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004699 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4700 intel_plane->base.base.id,
4701 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004702 return -EINVAL;
4703 }
4704
4705 /* Check src format */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004706 switch (fb->format->format) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004707 case DRM_FORMAT_RGB565:
4708 case DRM_FORMAT_XBGR8888:
4709 case DRM_FORMAT_XRGB8888:
4710 case DRM_FORMAT_ABGR8888:
4711 case DRM_FORMAT_ARGB8888:
4712 case DRM_FORMAT_XRGB2101010:
4713 case DRM_FORMAT_XBGR2101010:
4714 case DRM_FORMAT_YUYV:
4715 case DRM_FORMAT_YVYU:
4716 case DRM_FORMAT_UYVY:
4717 case DRM_FORMAT_VYUY:
4718 break;
4719 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004720 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4721 intel_plane->base.base.id, intel_plane->base.name,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004722 fb->base.id, fb->format->format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004723 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004724 }
4725
Chandra Kondurua1b22782015-04-07 15:28:45 -07004726 return 0;
4727}
4728
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004729static void skylake_scaler_disable(struct intel_crtc *crtc)
4730{
4731 int i;
4732
4733 for (i = 0; i < crtc->num_scalers; i++)
4734 skl_detach_scaler(crtc, i);
4735}
4736
4737static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004738{
4739 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004740 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004741 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004742 struct intel_crtc_scaler_state *scaler_state =
4743 &crtc->config->scaler_state;
4744
4745 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4746
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004747 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004748 int id;
4749
4750 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4751 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4752 return;
4753 }
4754
4755 id = scaler_state->scaler_id;
4756 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4757 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4758 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4759 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4760
4761 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004762 }
4763}
4764
Jesse Barnesb074cec2013-04-25 12:55:02 -07004765static void ironlake_pfit_enable(struct intel_crtc *crtc)
4766{
4767 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004768 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004769 int pipe = crtc->pipe;
4770
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004771 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004772 /* Force use of hard-coded filter coefficients
4773 * as some pre-programmed values are broken,
4774 * e.g. x201.
4775 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004776 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07004777 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4778 PF_PIPE_SEL_IVB(pipe));
4779 else
4780 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004781 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4782 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004783 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004784}
4785
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004786void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004787{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004788 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004789 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004790
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004791 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004792 return;
4793
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004794 /*
4795 * We can only enable IPS after we enable a plane and wait for a vblank
4796 * This function is called from post_plane_update, which is run after
4797 * a vblank wait.
4798 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004799
Paulo Zanonid77e4532013-09-24 13:52:55 -03004800 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004801 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004802 mutex_lock(&dev_priv->rps.hw_lock);
4803 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4804 mutex_unlock(&dev_priv->rps.hw_lock);
4805 /* Quoting Art Runyan: "its not safe to expect any particular
4806 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004807 * mailbox." Moreover, the mailbox may return a bogus state,
4808 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004809 */
4810 } else {
4811 I915_WRITE(IPS_CTL, IPS_ENABLE);
4812 /* The bit only becomes 1 in the next vblank, so this wait here
4813 * is essentially intel_wait_for_vblank. If we don't have this
4814 * and don't wait for vblanks until the end of crtc_enable, then
4815 * the HW state readout code will complain that the expected
4816 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004817 if (intel_wait_for_register(dev_priv,
4818 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4819 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004820 DRM_ERROR("Timed out waiting for IPS enable\n");
4821 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004822}
4823
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004824void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004825{
4826 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004827 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004828
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004829 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004830 return;
4831
4832 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004833 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004834 mutex_lock(&dev_priv->rps.hw_lock);
4835 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4836 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004837 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004838 if (intel_wait_for_register(dev_priv,
4839 IPS_CTL, IPS_ENABLE, 0,
4840 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004841 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004842 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004843 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004844 POSTING_READ(IPS_CTL);
4845 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004846
4847 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004848 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004849}
4850
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004851static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004852{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004853 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004854 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004855 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004856
4857 mutex_lock(&dev->struct_mutex);
4858 dev_priv->mm.interruptible = false;
4859 (void) intel_overlay_switch_off(intel_crtc->overlay);
4860 dev_priv->mm.interruptible = true;
4861 mutex_unlock(&dev->struct_mutex);
4862 }
4863
4864 /* Let userspace switch the overlay on again. In most cases userspace
4865 * has to recompute where to put it anyway.
4866 */
4867}
4868
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004869/**
4870 * intel_post_enable_primary - Perform operations after enabling primary plane
4871 * @crtc: the CRTC whose primary plane was just enabled
4872 *
4873 * Performs potentially sleeping operations that must be done after the primary
4874 * plane is enabled, such as updating FBC and IPS. Note that this may be
4875 * called due to an explicit primary plane update, or due to an implicit
4876 * re-enable that is caused when a sprite plane is updated to no longer
4877 * completely hide the primary plane.
4878 */
4879static void
4880intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004881{
4882 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004883 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4885 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004886
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004887 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004888 * FIXME IPS should be fine as long as one plane is
4889 * enabled, but in practice it seems to have problems
4890 * when going from primary only to sprite only and vice
4891 * versa.
4892 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004893 hsw_enable_ips(intel_crtc);
4894
Daniel Vetterf99d7062014-06-19 16:01:59 +02004895 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004896 * Gen2 reports pipe underruns whenever all planes are disabled.
4897 * So don't enable underrun reporting before at least some planes
4898 * are enabled.
4899 * FIXME: Need to fix the logic to work when we turn off all planes
4900 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004901 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004902 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004903 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4904
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004905 /* Underruns don't always raise interrupts, so check manually. */
4906 intel_check_cpu_fifo_underruns(dev_priv);
4907 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004908}
4909
Ville Syrjälä2622a082016-03-09 19:07:26 +02004910/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004911static void
4912intel_pre_disable_primary(struct drm_crtc *crtc)
4913{
4914 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004915 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4917 int pipe = intel_crtc->pipe;
4918
4919 /*
4920 * Gen2 reports pipe underruns whenever all planes are disabled.
4921 * So diasble underrun reporting before all the planes get disabled.
4922 * FIXME: Need to fix the logic to work when we turn off all planes
4923 * but leave the pipe running.
4924 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004925 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004926 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4927
4928 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004929 * FIXME IPS should be fine as long as one plane is
4930 * enabled, but in practice it seems to have problems
4931 * when going from primary only to sprite only and vice
4932 * versa.
4933 */
4934 hsw_disable_ips(intel_crtc);
4935}
4936
4937/* FIXME get rid of this and use pre_plane_update */
4938static void
4939intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4940{
4941 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004942 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004943 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4944 int pipe = intel_crtc->pipe;
4945
4946 intel_pre_disable_primary(crtc);
4947
4948 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004949 * Vblank time updates from the shadow to live plane control register
4950 * are blocked if the memory self-refresh mode is active at that
4951 * moment. So to make sure the plane gets truly disabled, disable
4952 * first the self-refresh mode. The self-refresh enable bit in turn
4953 * will be checked/applied by the HW only at the next frame start
4954 * event which is after the vblank start event, so we need to have a
4955 * wait-for-vblank between disabling the plane and the pipe.
4956 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +02004957 if (HAS_GMCH_DISPLAY(dev_priv) &&
4958 intel_set_memory_cxsr(dev_priv, false))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004959 intel_wait_for_vblank(dev_priv, pipe);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004960}
4961
Daniel Vetter5a21b662016-05-24 17:13:53 +02004962static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4963{
4964 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4965 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4966 struct intel_crtc_state *pipe_config =
4967 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004968 struct drm_plane *primary = crtc->base.primary;
4969 struct drm_plane_state *old_pri_state =
4970 drm_atomic_get_existing_plane_state(old_state, primary);
4971
Chris Wilson5748b6a2016-08-04 16:32:38 +01004972 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004973
4974 crtc->wm.cxsr_allowed = true;
4975
4976 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02004977 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004978
4979 if (old_pri_state) {
4980 struct intel_plane_state *primary_state =
4981 to_intel_plane_state(primary->state);
4982 struct intel_plane_state *old_primary_state =
4983 to_intel_plane_state(old_pri_state);
4984
4985 intel_fbc_post_update(crtc);
4986
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004987 if (primary_state->base.visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02004988 (needs_modeset(&pipe_config->base) ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004989 !old_primary_state->base.visible))
Daniel Vetter5a21b662016-05-24 17:13:53 +02004990 intel_post_enable_primary(&crtc->base);
4991 }
4992}
4993
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004994static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004995{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004996 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004997 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004998 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004999 struct intel_crtc_state *pipe_config =
5000 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005001 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5002 struct drm_plane *primary = crtc->base.primary;
5003 struct drm_plane_state *old_pri_state =
5004 drm_atomic_get_existing_plane_state(old_state, primary);
5005 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005006 struct intel_atomic_state *old_intel_state =
5007 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005008
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005009 if (old_pri_state) {
5010 struct intel_plane_state *primary_state =
5011 to_intel_plane_state(primary->state);
5012 struct intel_plane_state *old_primary_state =
5013 to_intel_plane_state(old_pri_state);
5014
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02005015 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01005016
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005017 if (old_primary_state->base.visible &&
5018 (modeset || !primary_state->base.visible))
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005019 intel_pre_disable_primary(&crtc->base);
5020 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005021
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005022 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03005023 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005024
Ville Syrjälä2622a082016-03-09 19:07:26 +02005025 /*
5026 * Vblank time updates from the shadow to live plane control register
5027 * are blocked if the memory self-refresh mode is active at that
5028 * moment. So to make sure the plane gets truly disabled, disable
5029 * first the self-refresh mode. The self-refresh enable bit in turn
5030 * will be checked/applied by the HW only at the next frame start
5031 * event which is after the vblank start event, so we need to have a
5032 * wait-for-vblank between disabling the plane and the pipe.
5033 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +02005034 if (old_crtc_state->base.active &&
5035 intel_set_memory_cxsr(dev_priv, false))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005036 intel_wait_for_vblank(dev_priv, crtc->pipe);
Ville Syrjälä852eb002015-06-24 22:00:07 +03005037 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005038
Matt Ropered4a6a72016-02-23 17:20:13 -08005039 /*
5040 * IVB workaround: must disable low power watermarks for at least
5041 * one frame before enabling scaling. LP watermarks can be re-enabled
5042 * when scaling is disabled.
5043 *
5044 * WaCxSRDisabledForSpriteScaling:ivb
5045 */
Ville Syrjäläddd2b792016-11-28 19:37:04 +02005046 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005047 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005048
5049 /*
5050 * If we're doing a modeset, we're done. No need to do any pre-vblank
5051 * watermark programming here.
5052 */
5053 if (needs_modeset(&pipe_config->base))
5054 return;
5055
5056 /*
5057 * For platforms that support atomic watermarks, program the
5058 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5059 * will be the intermediate values that are safe for both pre- and
5060 * post- vblank; when vblank happens, the 'active' values will be set
5061 * to the final 'target' values and we'll do this again to get the
5062 * optimal watermarks. For gen9+ platforms, the values we program here
5063 * will be the final target values which will get automatically latched
5064 * at vblank time; no further programming will be necessary.
5065 *
5066 * If a platform hasn't been transitioned to atomic watermarks yet,
5067 * we'll continue to update watermarks the old way, if flags tell
5068 * us to.
5069 */
5070 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005071 dev_priv->display.initial_watermarks(old_intel_state,
5072 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005073 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005074 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005075}
5076
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005077static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005078{
5079 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005081 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005082 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005083
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005084 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005085
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005086 drm_for_each_plane_mask(p, dev, plane_mask)
5087 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005088
Daniel Vetterf99d7062014-06-19 16:01:59 +02005089 /*
5090 * FIXME: Once we grow proper nuclear flip support out of this we need
5091 * to compute the mask of flip planes precisely. For the time being
5092 * consider this a flip to a NULL plane.
5093 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01005094 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005095}
5096
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005097static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005098 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005099 struct drm_atomic_state *old_state)
5100{
5101 struct drm_connector_state *old_conn_state;
5102 struct drm_connector *conn;
5103 int i;
5104
5105 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5106 struct drm_connector_state *conn_state = conn->state;
5107 struct intel_encoder *encoder =
5108 to_intel_encoder(conn_state->best_encoder);
5109
5110 if (conn_state->crtc != crtc)
5111 continue;
5112
5113 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005114 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005115 }
5116}
5117
5118static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005119 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005120 struct drm_atomic_state *old_state)
5121{
5122 struct drm_connector_state *old_conn_state;
5123 struct drm_connector *conn;
5124 int i;
5125
5126 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5127 struct drm_connector_state *conn_state = conn->state;
5128 struct intel_encoder *encoder =
5129 to_intel_encoder(conn_state->best_encoder);
5130
5131 if (conn_state->crtc != crtc)
5132 continue;
5133
5134 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005135 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005136 }
5137}
5138
5139static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005140 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005141 struct drm_atomic_state *old_state)
5142{
5143 struct drm_connector_state *old_conn_state;
5144 struct drm_connector *conn;
5145 int i;
5146
5147 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5148 struct drm_connector_state *conn_state = conn->state;
5149 struct intel_encoder *encoder =
5150 to_intel_encoder(conn_state->best_encoder);
5151
5152 if (conn_state->crtc != crtc)
5153 continue;
5154
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005155 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005156 intel_opregion_notify_encoder(encoder, true);
5157 }
5158}
5159
5160static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005161 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005162 struct drm_atomic_state *old_state)
5163{
5164 struct drm_connector_state *old_conn_state;
5165 struct drm_connector *conn;
5166 int i;
5167
5168 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5169 struct intel_encoder *encoder =
5170 to_intel_encoder(old_conn_state->best_encoder);
5171
5172 if (old_conn_state->crtc != crtc)
5173 continue;
5174
5175 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005176 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005177 }
5178}
5179
5180static void intel_encoders_post_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
5188 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5189 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 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005196 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005197 }
5198}
5199
5200static void intel_encoders_post_pll_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
5208 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5209 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_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005216 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005217 }
5218}
5219
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005220static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5221 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005222{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005223 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005224 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005225 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5227 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005228 struct intel_atomic_state *old_intel_state =
5229 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005230
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005231 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005232 return;
5233
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005234 /*
5235 * Sometimes spurious CPU pipe underruns happen during FDI
5236 * training, at least with VGA+HDMI cloning. Suppress them.
5237 *
5238 * On ILK we get an occasional spurious CPU pipe underruns
5239 * between eDP port A enable and vdd enable. Also PCH port
5240 * enable seems to result in the occasional CPU pipe underrun.
5241 *
5242 * Spurious PCH underruns also occur during PCH enabling.
5243 */
5244 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5245 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005246 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005247 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5248
5249 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005250 intel_prepare_shared_dpll(intel_crtc);
5251
Ville Syrjälä37a56502016-06-22 21:57:04 +03005252 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305253 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005254
5255 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005256 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005257
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005258 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005259 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005260 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005261 }
5262
5263 ironlake_set_pipeconf(crtc);
5264
Jesse Barnesf67a5592011-01-05 10:31:48 -08005265 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005266
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005267 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005268
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005269 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005270 /* Note: FDI PLL enabling _must_ be done before we enable the
5271 * cpu pipes, hence this is separate from all the other fdi/pch
5272 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005273 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005274 } else {
5275 assert_fdi_tx_disabled(dev_priv, pipe);
5276 assert_fdi_rx_disabled(dev_priv, pipe);
5277 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005278
Jesse Barnesb074cec2013-04-25 12:55:02 -07005279 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005280
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005281 /*
5282 * On ILK+ LUT must be loaded before the pipe is running but with
5283 * clocks enabled
5284 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005285 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005286
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005287 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005288 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005289 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005290
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005291 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005292 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005293
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005294 assert_vblank_disabled(crtc);
5295 drm_crtc_vblank_on(crtc);
5296
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005297 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005298
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005299 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005300 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005301
5302 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5303 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005304 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005305 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005306 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005307}
5308
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005309/* IPS only exists on ULT machines and is tied to pipe A. */
5310static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5311{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005312 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005313}
5314
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005315static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5316 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005317{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005318 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005319 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005321 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005322 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005323 struct intel_atomic_state *old_intel_state =
5324 to_intel_atomic_state(old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005325
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005326 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005327 return;
5328
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005329 if (intel_crtc->config->has_pch_encoder)
5330 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5331 false);
5332
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005333 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005334
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005335 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005336 intel_enable_shared_dpll(intel_crtc);
5337
Ville Syrjälä37a56502016-06-22 21:57:04 +03005338 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305339 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005340
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005341 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005342 intel_set_pipe_timings(intel_crtc);
5343
Jani Nikulabc58be62016-03-18 17:05:39 +02005344 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005345
Jani Nikula4d1de972016-03-18 17:05:42 +02005346 if (cpu_transcoder != TRANSCODER_EDP &&
5347 !transcoder_is_dsi(cpu_transcoder)) {
5348 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005349 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005350 }
5351
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005352 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005353 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005354 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005355 }
5356
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005357 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005358 haswell_set_pipeconf(crtc);
5359
Jani Nikula391bf042016-03-18 17:05:40 +02005360 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005361
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005362 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005363
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005364 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005365
Daniel Vetter6b698512015-11-28 11:05:39 +01005366 if (intel_crtc->config->has_pch_encoder)
5367 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5368 else
5369 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5370
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005371 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005372
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005373 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005374 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005375
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005376 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305377 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005378
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005379 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005380 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005381 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005382 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005383
5384 /*
5385 * On ILK+ LUT must be loaded before the pipe is running but with
5386 * clocks enabled
5387 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005388 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005389
Paulo Zanoni1f544382012-10-24 11:32:00 -02005390 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005391 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305392 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005393
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005394 if (dev_priv->display.initial_watermarks != NULL)
Ville Syrjälä3125d392016-11-28 19:37:03 +02005395 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005396
5397 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005398 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005399 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005400
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005401 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005402 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005403
Ville Syrjälä00370712016-11-14 19:44:06 +02005404 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Dave Airlie0e32b392014-05-02 14:02:48 +10005405 intel_ddi_set_vc_payload_alloc(crtc, true);
5406
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005407 assert_vblank_disabled(crtc);
5408 drm_crtc_vblank_on(crtc);
5409
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005410 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005411
Daniel Vetter6b698512015-11-28 11:05:39 +01005412 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005413 intel_wait_for_vblank(dev_priv, pipe);
5414 intel_wait_for_vblank(dev_priv, pipe);
Daniel Vetter6b698512015-11-28 11:05:39 +01005415 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005416 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5417 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005418 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005419
Paulo Zanonie4916942013-09-20 16:21:19 -03005420 /* If we change the relative order between pipe/planes enabling, we need
5421 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005422 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005423 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005424 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5425 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005426 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005427}
5428
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005429static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005430{
5431 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005432 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005433 int pipe = crtc->pipe;
5434
5435 /* To avoid upsetting the power well on haswell only disable the pfit if
5436 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005437 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005438 I915_WRITE(PF_CTL(pipe), 0);
5439 I915_WRITE(PF_WIN_POS(pipe), 0);
5440 I915_WRITE(PF_WIN_SZ(pipe), 0);
5441 }
5442}
5443
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005444static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5445 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005446{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005447 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005448 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005449 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5451 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005452
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005453 /*
5454 * Sometimes spurious CPU pipe underruns happen when the
5455 * pipe is already disabled, but FDI RX/TX is still enabled.
5456 * Happens at least with VGA+HDMI cloning. Suppress them.
5457 */
5458 if (intel_crtc->config->has_pch_encoder) {
5459 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005460 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005461 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005462
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005463 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005464
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005465 drm_crtc_vblank_off(crtc);
5466 assert_vblank_disabled(crtc);
5467
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005468 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005469
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005470 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005471
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005472 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005473 ironlake_fdi_disable(crtc);
5474
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005475 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005476
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005477 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005478 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005479
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005480 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005481 i915_reg_t reg;
5482 u32 temp;
5483
Daniel Vetterd925c592013-06-05 13:34:04 +02005484 /* disable TRANS_DP_CTL */
5485 reg = TRANS_DP_CTL(pipe);
5486 temp = I915_READ(reg);
5487 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5488 TRANS_DP_PORT_SEL_MASK);
5489 temp |= TRANS_DP_PORT_SEL_NONE;
5490 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005491
Daniel Vetterd925c592013-06-05 13:34:04 +02005492 /* disable DPLL_SEL */
5493 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005494 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005495 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005496 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005497
Daniel Vetterd925c592013-06-05 13:34:04 +02005498 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005499 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005500
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005501 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005502 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005503}
5504
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005505static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5506 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005507{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005508 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005509 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005511 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005512
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005513 if (intel_crtc->config->has_pch_encoder)
5514 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5515 false);
5516
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005517 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005518
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005519 drm_crtc_vblank_off(crtc);
5520 assert_vblank_disabled(crtc);
5521
Jani Nikula4d1de972016-03-18 17:05:42 +02005522 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005523 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005524 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005525
Ville Syrjälä00370712016-11-14 19:44:06 +02005526 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005527 intel_ddi_set_vc_payload_alloc(crtc, false);
5528
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005529 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305530 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005531
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005532 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005533 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005534 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005535 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005536
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005537 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305538 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005539
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005540 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005541
Maarten Lankhorstb7076542016-08-23 16:18:08 +02005542 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005543 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5544 true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005545}
5546
Jesse Barnes2dd24552013-04-25 12:55:01 -07005547static void i9xx_pfit_enable(struct intel_crtc *crtc)
5548{
5549 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005550 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005551 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005552
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005553 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005554 return;
5555
Daniel Vetterc0b03412013-05-28 12:05:54 +02005556 /*
5557 * The panel fitter should only be adjusted whilst the pipe is disabled,
5558 * according to register description and PRM.
5559 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005560 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5561 assert_pipe_disabled(dev_priv, crtc->pipe);
5562
Jesse Barnesb074cec2013-04-25 12:55:02 -07005563 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5564 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005565
5566 /* Border color in case we don't scale up to the full screen. Black by
5567 * default, change to something else for debugging. */
5568 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005569}
5570
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005571enum intel_display_power_domain intel_port_to_power_domain(enum port port)
Dave Airlied05410f2014-06-05 13:22:59 +10005572{
5573 switch (port) {
5574 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005575 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005576 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005577 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005578 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005579 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005580 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005581 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005582 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005583 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005584 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005585 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005586 return POWER_DOMAIN_PORT_OTHER;
5587 }
5588}
5589
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005590static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5591 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005592{
5593 struct drm_device *dev = crtc->dev;
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005594 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005595 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5597 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005598 u64 mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005599 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005600
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005601 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005602 return 0;
5603
Imre Deak77d22dc2014-03-05 16:20:52 +02005604 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5605 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005606 if (crtc_state->pch_pfit.enabled ||
5607 crtc_state->pch_pfit.force_thru)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005608 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
Imre Deak77d22dc2014-03-05 16:20:52 +02005609
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005610 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5611 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5612
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005613 mask |= BIT_ULL(intel_encoder->power_domain);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005614 }
Imre Deak319be8a2014-03-04 19:22:57 +02005615
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005616 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5617 mask |= BIT(POWER_DOMAIN_AUDIO);
5618
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005619 if (crtc_state->shared_dpll)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005620 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005621
Imre Deak77d22dc2014-03-05 16:20:52 +02005622 return mask;
5623}
5624
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005625static u64
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005626modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5627 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005628{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005629 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5631 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005632 u64 domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005633
5634 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005635 intel_crtc->enabled_power_domains = new_domains =
5636 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005637
Daniel Vetter5a21b662016-05-24 17:13:53 +02005638 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005639
5640 for_each_power_domain(domain, domains)
5641 intel_display_power_get(dev_priv, domain);
5642
Daniel Vetter5a21b662016-05-24 17:13:53 +02005643 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005644}
5645
5646static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005647 u64 domains)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005648{
5649 enum intel_display_power_domain domain;
5650
5651 for_each_power_domain(domain, domains)
5652 intel_display_power_put(dev_priv, domain);
5653}
5654
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005655static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5656 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005657{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005658 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005659 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005660 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005662 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005663
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005664 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005665 return;
5666
Ville Syrjälä37a56502016-06-22 21:57:04 +03005667 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305668 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005669
5670 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005671 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005672
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005673 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01005674 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005675
5676 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5677 I915_WRITE(CHV_CANVAS(pipe), 0);
5678 }
5679
Daniel Vetter5b18e572014-04-24 23:55:06 +02005680 i9xx_set_pipeconf(intel_crtc);
5681
Jesse Barnes89b667f2013-04-18 14:51:36 -07005682 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005683
Daniel Vettera72e4c92014-09-30 10:56:47 +02005684 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005685
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005686 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005687
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005688 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03005689 chv_prepare_pll(intel_crtc, intel_crtc->config);
5690 chv_enable_pll(intel_crtc, intel_crtc->config);
5691 } else {
5692 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5693 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005694 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005695
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005696 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005697
Jesse Barnes2dd24552013-04-25 12:55:01 -07005698 i9xx_pfit_enable(intel_crtc);
5699
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005700 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005701
Ville Syrjälä432081b2016-10-31 22:37:03 +02005702 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005703 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005704
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005705 assert_vblank_disabled(crtc);
5706 drm_crtc_vblank_on(crtc);
5707
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005708 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005709}
5710
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005711static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5712{
5713 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005714 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005715
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005716 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5717 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005718}
5719
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005720static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5721 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005722{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005723 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005724 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005725 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03005727 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005728
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005729 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005730 return;
5731
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005732 i9xx_set_pll_dividers(intel_crtc);
5733
Ville Syrjälä37a56502016-06-22 21:57:04 +03005734 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305735 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005736
5737 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005738 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005739
Daniel Vetter5b18e572014-04-24 23:55:06 +02005740 i9xx_set_pipeconf(intel_crtc);
5741
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005742 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005743
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005744 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005745 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005746
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005747 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005748
Daniel Vetterf6736a12013-06-05 13:34:30 +02005749 i9xx_enable_pll(intel_crtc);
5750
Jesse Barnes2dd24552013-04-25 12:55:01 -07005751 i9xx_pfit_enable(intel_crtc);
5752
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005753 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005754
Ville Syrjälä432081b2016-10-31 22:37:03 +02005755 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005756 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005757
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005758 assert_vblank_disabled(crtc);
5759 drm_crtc_vblank_on(crtc);
5760
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005761 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005762}
5763
Daniel Vetter87476d62013-04-11 16:29:06 +02005764static void i9xx_pfit_disable(struct intel_crtc *crtc)
5765{
5766 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005767 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02005768
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005769 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005770 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005771
5772 assert_pipe_disabled(dev_priv, crtc->pipe);
5773
Daniel Vetter328d8e82013-05-08 10:36:31 +02005774 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5775 I915_READ(PFIT_CONTROL));
5776 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005777}
5778
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005779static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5780 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005781{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005782 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005783 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005784 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5786 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005787
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005788 /*
5789 * On gen2 planes are double buffered but the pipe isn't, so we must
5790 * wait for planes to fully turn off before disabling the pipe.
5791 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005792 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005793 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005794
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005795 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005796
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005797 drm_crtc_vblank_off(crtc);
5798 assert_vblank_disabled(crtc);
5799
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005800 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005801
Daniel Vetter87476d62013-04-11 16:29:06 +02005802 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005803
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005804 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005805
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005806 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005807 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005808 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01005809 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005810 vlv_disable_pll(dev_priv, pipe);
5811 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005812 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005813 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005814
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005815 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03005816
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005817 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005818 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005819}
5820
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005821static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005822{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005823 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005825 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005826 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005827 u64 domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005828 struct drm_atomic_state *state;
5829 struct intel_crtc_state *crtc_state;
5830 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005831
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005832 if (!intel_crtc->active)
5833 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005834
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +01005835 if (crtc->primary->state->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02005836 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02005837
Ville Syrjälä2622a082016-03-09 19:07:26 +02005838 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01005839
5840 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +01005841 crtc->primary->state->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02005842 }
5843
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005844 state = drm_atomic_state_alloc(crtc->dev);
Ander Conselvan de Oliveira31bb2ef2017-01-20 16:28:45 +02005845 if (!state) {
5846 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5847 crtc->base.id, crtc->name);
5848 return;
5849 }
5850
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005851 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
5852
5853 /* Everything's already locked, -EDEADLK can't happen. */
5854 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5855 ret = drm_atomic_add_affected_connectors(state, crtc);
5856
5857 WARN_ON(IS_ERR(crtc_state) || ret);
5858
5859 dev_priv->display.crtc_disable(crtc_state, state);
5860
Chris Wilson08536952016-10-14 13:18:18 +01005861 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005862
Ville Syrjälä78108b72016-05-27 20:59:19 +03005863 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5864 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005865
5866 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5867 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07005868 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01005869 crtc->enabled = false;
5870 crtc->state->connector_mask = 0;
5871 crtc->state->encoder_mask = 0;
5872
5873 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5874 encoder->base.crtc = NULL;
5875
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02005876 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005877 intel_update_watermarks(intel_crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02005878 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005879
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005880 domains = intel_crtc->enabled_power_domains;
5881 for_each_power_domain(domain, domains)
5882 intel_display_power_put(dev_priv, domain);
5883 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005884
5885 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5886 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02005887}
5888
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005889/*
5890 * turn all crtc's off, but do not adjust state
5891 * This has to be paired with a call to intel_modeset_setup_hw_state.
5892 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005893int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005894{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005895 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005896 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005897 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005898
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005899 state = drm_atomic_helper_suspend(dev);
5900 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005901 if (ret)
5902 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01005903 else
5904 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02005905 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02005906}
5907
Chris Wilsonea5b2132010-08-04 13:50:23 +01005908void intel_encoder_destroy(struct drm_encoder *encoder)
5909{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005910 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005911
Chris Wilsonea5b2132010-08-04 13:50:23 +01005912 drm_encoder_cleanup(encoder);
5913 kfree(intel_encoder);
5914}
5915
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005916/* Cross check the actual hw state with our own modeset state tracking (and it's
5917 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02005918static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005919{
Daniel Vetter5a21b662016-05-24 17:13:53 +02005920 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005921
5922 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5923 connector->base.base.id,
5924 connector->base.name);
5925
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005926 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005927 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005928 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005929
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005930 I915_STATE_WARN(!crtc,
5931 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005932
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005933 if (!crtc)
5934 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005935
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005936 I915_STATE_WARN(!crtc->state->active,
5937 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005938
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005939 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005940 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005941
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005942 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005943 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10005944
Maarten Lankhorste85376c2015-08-27 13:13:31 +02005945 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005946 "attached encoder crtc differs from connector crtc\n");
5947 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02005948 I915_STATE_WARN(crtc && crtc->state->active,
5949 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02005950 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02005951 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005952 }
5953}
5954
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005955int intel_connector_init(struct intel_connector *connector)
5956{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01005957 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005958
Maarten Lankhorst5350a032016-01-04 12:53:15 +01005959 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005960 return -ENOMEM;
5961
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03005962 return 0;
5963}
5964
5965struct intel_connector *intel_connector_alloc(void)
5966{
5967 struct intel_connector *connector;
5968
5969 connector = kzalloc(sizeof *connector, GFP_KERNEL);
5970 if (!connector)
5971 return NULL;
5972
5973 if (intel_connector_init(connector) < 0) {
5974 kfree(connector);
5975 return NULL;
5976 }
5977
5978 return connector;
5979}
5980
Daniel Vetterf0947c32012-07-02 13:10:34 +02005981/* Simple connector->get_hw_state implementation for encoders that support only
5982 * one connector and no cloning and hence the encoder state determines the state
5983 * of the connector. */
5984bool intel_connector_get_hw_state(struct intel_connector *connector)
5985{
Daniel Vetter24929352012-07-02 20:28:59 +02005986 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005987 struct intel_encoder *encoder = connector->encoder;
5988
5989 return encoder->get_hw_state(encoder, &pipe);
5990}
5991
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005992static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005993{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03005994 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
5995 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02005996
5997 return 0;
5998}
5999
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006000static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006001 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006002{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006003 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006004 struct drm_atomic_state *state = pipe_config->base.state;
6005 struct intel_crtc *other_crtc;
6006 struct intel_crtc_state *other_crtc_state;
6007
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006008 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6009 pipe_name(pipe), pipe_config->fdi_lanes);
6010 if (pipe_config->fdi_lanes > 4) {
6011 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6012 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006013 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006014 }
6015
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006016 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006017 if (pipe_config->fdi_lanes > 2) {
6018 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6019 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006020 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006021 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006022 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006023 }
6024 }
6025
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00006026 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006027 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006028
6029 /* Ivybridge 3 pipe is really complicated */
6030 switch (pipe) {
6031 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006032 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006033 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006034 if (pipe_config->fdi_lanes <= 2)
6035 return 0;
6036
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006037 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006038 other_crtc_state =
6039 intel_atomic_get_crtc_state(state, other_crtc);
6040 if (IS_ERR(other_crtc_state))
6041 return PTR_ERR(other_crtc_state);
6042
6043 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006044 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6045 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006046 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006047 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006048 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006049 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006050 if (pipe_config->fdi_lanes > 2) {
6051 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6052 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006053 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006054 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006055
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006056 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006057 other_crtc_state =
6058 intel_atomic_get_crtc_state(state, other_crtc);
6059 if (IS_ERR(other_crtc_state))
6060 return PTR_ERR(other_crtc_state);
6061
6062 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006063 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006064 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006065 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006066 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006067 default:
6068 BUG();
6069 }
6070}
6071
Daniel Vettere29c22c2013-02-21 00:00:16 +01006072#define RETRY 1
6073static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006074 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006075{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006076 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006077 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006078 int lane, link_bw, fdi_dotclock, ret;
6079 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006080
Daniel Vettere29c22c2013-02-21 00:00:16 +01006081retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006082 /* FDI is a binary signal running at ~2.7GHz, encoding
6083 * each output octet as 10 bits. The actual frequency
6084 * is stored as a divider into a 100MHz clock, and the
6085 * mode pixel clock is stored in units of 1KHz.
6086 * Hence the bw of each lane in terms of the mode signal
6087 * is:
6088 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006089 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006090
Damien Lespiau241bfc32013-09-25 16:45:37 +01006091 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006092
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006093 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006094 pipe_config->pipe_bpp);
6095
6096 pipe_config->fdi_lanes = lane;
6097
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006098 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006099 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006100
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006101 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006102 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006103 pipe_config->pipe_bpp -= 2*3;
6104 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6105 pipe_config->pipe_bpp);
6106 needs_recompute = true;
6107 pipe_config->bw_constrained = true;
6108
6109 goto retry;
6110 }
6111
6112 if (needs_recompute)
6113 return RETRY;
6114
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006115 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006116}
6117
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006118static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6119 struct intel_crtc_state *pipe_config)
6120{
6121 if (pipe_config->pipe_bpp > 24)
6122 return false;
6123
6124 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006125 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006126 return true;
6127
6128 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006129 * We compare against max which means we must take
6130 * the increased cdclk requirement into account when
6131 * calculating the new cdclk.
6132 *
6133 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006134 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006135 return pipe_config->pixel_rate <=
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006136 dev_priv->max_cdclk_freq * 95 / 100;
6137}
6138
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006139static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006140 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006141{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006142 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006143 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006144
Jani Nikulad330a952014-01-21 11:24:25 +02006145 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006146 hsw_crtc_supports_ips(crtc) &&
6147 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006148}
6149
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006150static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6151{
6152 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6153
6154 /* GDG double wide on either pipe, otherwise pipe A only */
6155 return INTEL_INFO(dev_priv)->gen < 4 &&
6156 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6157}
6158
Ville Syrjäläceb99322017-01-20 20:22:05 +02006159static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6160{
6161 uint32_t pixel_rate;
6162
6163 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6164
6165 /*
6166 * We only use IF-ID interlacing. If we ever use
6167 * PF-ID we'll need to adjust the pixel_rate here.
6168 */
6169
6170 if (pipe_config->pch_pfit.enabled) {
6171 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6172 uint32_t pfit_size = pipe_config->pch_pfit.size;
6173
6174 pipe_w = pipe_config->pipe_src_w;
6175 pipe_h = pipe_config->pipe_src_h;
6176
6177 pfit_w = (pfit_size >> 16) & 0xFFFF;
6178 pfit_h = pfit_size & 0xFFFF;
6179 if (pipe_w < pfit_w)
6180 pipe_w = pfit_w;
6181 if (pipe_h < pfit_h)
6182 pipe_h = pfit_h;
6183
6184 if (WARN_ON(!pfit_w || !pfit_h))
6185 return pixel_rate;
6186
6187 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6188 pfit_w * pfit_h);
6189 }
6190
6191 return pixel_rate;
6192}
6193
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006194static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
6195{
6196 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
6197
6198 if (HAS_GMCH_DISPLAY(dev_priv))
6199 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6200 crtc_state->pixel_rate =
6201 crtc_state->base.adjusted_mode.crtc_clock;
6202 else
6203 crtc_state->pixel_rate =
6204 ilk_pipe_pixel_rate(crtc_state);
6205}
6206
Daniel Vettera43f6e02013-06-07 23:10:32 +02006207static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006208 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006209{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006210 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006211 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006212 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006213 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006214
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006215 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006216 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006217
6218 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006219 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006220 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006221 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006222 if (intel_crtc_supports_double_wide(crtc) &&
6223 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006224 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006225 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006226 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006227 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006228
Ville Syrjäläf3261152016-05-24 21:34:18 +03006229 if (adjusted_mode->crtc_clock > clock_limit) {
6230 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6231 adjusted_mode->crtc_clock, clock_limit,
6232 yesno(pipe_config->double_wide));
6233 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006234 }
Chris Wilson89749352010-09-12 18:25:19 +01006235
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006236 /*
6237 * Pipe horizontal size must be even in:
6238 * - DVO ganged mode
6239 * - LVDS dual channel mode
6240 * - Double wide pipe
6241 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006242 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006243 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6244 pipe_config->pipe_src_w &= ~1;
6245
Damien Lespiau8693a822013-05-03 18:48:11 +01006246 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6247 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006248 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006249 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006250 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006251 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006252
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006253 intel_crtc_compute_pixel_rate(pipe_config);
6254
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006255 if (HAS_IPS(dev_priv))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006256 hsw_compute_ips_config(crtc, pipe_config);
6257
Daniel Vetter877d48d2013-04-19 11:24:43 +02006258 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006259 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006260
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006261 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006262}
6263
Zhenyu Wang2c072452009-06-05 15:38:42 +08006264static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006265intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006266{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006267 while (*num > DATA_LINK_M_N_MASK ||
6268 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006269 *num >>= 1;
6270 *den >>= 1;
6271 }
6272}
6273
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006274static void compute_m_n(unsigned int m, unsigned int n,
6275 uint32_t *ret_m, uint32_t *ret_n)
6276{
6277 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6278 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6279 intel_reduce_m_n_ratio(ret_m, ret_n);
6280}
6281
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006282void
6283intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6284 int pixel_clock, int link_clock,
6285 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006286{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006287 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006288
6289 compute_m_n(bits_per_pixel * pixel_clock,
6290 link_clock * nlanes * 8,
6291 &m_n->gmch_m, &m_n->gmch_n);
6292
6293 compute_m_n(pixel_clock, link_clock,
6294 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006295}
6296
Chris Wilsona7615032011-01-12 17:04:08 +00006297static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6298{
Jani Nikulad330a952014-01-21 11:24:25 +02006299 if (i915.panel_use_ssc >= 0)
6300 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006301 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006302 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006303}
6304
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006305static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006306{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006307 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006308}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006309
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006310static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6311{
6312 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006313}
6314
Daniel Vetterf47709a2013-03-28 10:42:02 +01006315static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006316 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006317 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08006318{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006319 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006320 u32 fp, fp2 = 0;
6321
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006322 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006323 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006324 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006325 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006326 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006327 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006328 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006329 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006330 }
6331
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006332 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006333
Daniel Vetterf47709a2013-03-28 10:42:02 +01006334 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006335 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006336 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006337 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006338 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006339 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006340 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006341 }
6342}
6343
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006344static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6345 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006346{
6347 u32 reg_val;
6348
6349 /*
6350 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6351 * and set it to a reasonable value instead.
6352 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006353 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006354 reg_val &= 0xffffff00;
6355 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006356 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006357
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006358 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006359 reg_val &= 0x8cffffff;
6360 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006361 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006362
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006363 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006364 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006365 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006366
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006367 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006368 reg_val &= 0x00ffffff;
6369 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006370 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006371}
6372
Daniel Vetterb5518422013-05-03 11:49:48 +02006373static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6374 struct intel_link_m_n *m_n)
6375{
6376 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006377 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006378 int pipe = crtc->pipe;
6379
Daniel Vettere3b95f12013-05-03 11:49:49 +02006380 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6381 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6382 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6383 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006384}
6385
6386static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006387 struct intel_link_m_n *m_n,
6388 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006389{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006390 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006391 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006392 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006393
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006394 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02006395 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6396 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6397 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6398 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006399 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6400 * for gen < 8) and if DRRS is supported (to make sure the
6401 * registers are not unnecessarily accessed).
6402 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006403 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6404 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006405 I915_WRITE(PIPE_DATA_M2(transcoder),
6406 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6407 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6408 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6409 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6410 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006411 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006412 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6413 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6414 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6415 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006416 }
6417}
6418
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306419void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006420{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306421 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6422
6423 if (m_n == M1_N1) {
6424 dp_m_n = &crtc->config->dp_m_n;
6425 dp_m2_n2 = &crtc->config->dp_m2_n2;
6426 } else if (m_n == M2_N2) {
6427
6428 /*
6429 * M2_N2 registers are not supported. Hence m2_n2 divider value
6430 * needs to be programmed into M1_N1.
6431 */
6432 dp_m_n = &crtc->config->dp_m2_n2;
6433 } else {
6434 DRM_ERROR("Unsupported divider value\n");
6435 return;
6436 }
6437
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006438 if (crtc->config->has_pch_encoder)
6439 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006440 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306441 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006442}
6443
Daniel Vetter251ac862015-06-18 10:30:24 +02006444static void vlv_compute_dpll(struct intel_crtc *crtc,
6445 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006446{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006447 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006448 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006449 if (crtc->pipe != PIPE_A)
6450 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006451
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006452 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006453 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006454 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6455 DPLL_EXT_BUFFER_ENABLE_VLV;
6456
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006457 pipe_config->dpll_hw_state.dpll_md =
6458 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6459}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006460
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006461static void chv_compute_dpll(struct intel_crtc *crtc,
6462 struct intel_crtc_state *pipe_config)
6463{
6464 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006465 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006466 if (crtc->pipe != PIPE_A)
6467 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6468
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006469 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006470 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006471 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6472
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006473 pipe_config->dpll_hw_state.dpll_md =
6474 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006475}
6476
Ville Syrjäläd288f652014-10-28 13:20:22 +02006477static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006478 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006479{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006480 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006481 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006482 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006483 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006484 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006485 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006486
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006487 /* Enable Refclk */
6488 I915_WRITE(DPLL(pipe),
6489 pipe_config->dpll_hw_state.dpll &
6490 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6491
6492 /* No need to actually set up the DPLL with DSI */
6493 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6494 return;
6495
Ville Syrjäläa5805162015-05-26 20:42:30 +03006496 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01006497
Ville Syrjäläd288f652014-10-28 13:20:22 +02006498 bestn = pipe_config->dpll.n;
6499 bestm1 = pipe_config->dpll.m1;
6500 bestm2 = pipe_config->dpll.m2;
6501 bestp1 = pipe_config->dpll.p1;
6502 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006503
Jesse Barnes89b667f2013-04-18 14:51:36 -07006504 /* See eDP HDMI DPIO driver vbios notes doc */
6505
6506 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006507 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006508 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006509
6510 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006511 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006512
6513 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006514 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006515 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006516 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006517
6518 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006519 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006520
6521 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006522 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6523 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6524 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006525 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006526
6527 /*
6528 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6529 * but we don't support that).
6530 * Note: don't use the DAC post divider as it seems unstable.
6531 */
6532 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006533 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006534
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006535 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006536 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006537
Jesse Barnes89b667f2013-04-18 14:51:36 -07006538 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006539 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006540 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6541 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006542 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006543 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006544 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006545 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006546 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006547
Ville Syrjälä37a56502016-06-22 21:57:04 +03006548 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006549 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006550 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006551 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006552 0x0df40000);
6553 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006554 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006555 0x0df70000);
6556 } else { /* HDMI or VGA */
6557 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006558 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006559 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006560 0x0df70000);
6561 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006562 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006563 0x0df40000);
6564 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006565
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006566 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006567 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03006568 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006569 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006570 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006571
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006572 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006573 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006574}
6575
Ville Syrjäläd288f652014-10-28 13:20:22 +02006576static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006577 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006578{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006579 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006580 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006581 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006582 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306583 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006584 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306585 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306586 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006587
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006588 /* Enable Refclk and SSC */
6589 I915_WRITE(DPLL(pipe),
6590 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6591
6592 /* No need to actually set up the DPLL with DSI */
6593 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6594 return;
6595
Ville Syrjäläd288f652014-10-28 13:20:22 +02006596 bestn = pipe_config->dpll.n;
6597 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6598 bestm1 = pipe_config->dpll.m1;
6599 bestm2 = pipe_config->dpll.m2 >> 22;
6600 bestp1 = pipe_config->dpll.p1;
6601 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306602 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306603 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306604 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006605
Ville Syrjäläa5805162015-05-26 20:42:30 +03006606 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006607
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006608 /* p1 and p2 divider */
6609 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6610 5 << DPIO_CHV_S1_DIV_SHIFT |
6611 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6612 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6613 1 << DPIO_CHV_K_DIV_SHIFT);
6614
6615 /* Feedback post-divider - m2 */
6616 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6617
6618 /* Feedback refclk divider - n and m1 */
6619 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6620 DPIO_CHV_M1_DIV_BY_2 |
6621 1 << DPIO_CHV_N_DIV_SHIFT);
6622
6623 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03006624 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006625
6626 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306627 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6628 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6629 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6630 if (bestm2_frac)
6631 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6632 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006633
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306634 /* Program digital lock detect threshold */
6635 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6636 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6637 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6638 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6639 if (!bestm2_frac)
6640 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6641 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6642
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006643 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306644 if (vco == 5400000) {
6645 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6646 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6647 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6648 tribuf_calcntr = 0x9;
6649 } else if (vco <= 6200000) {
6650 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6651 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6652 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6653 tribuf_calcntr = 0x9;
6654 } else if (vco <= 6480000) {
6655 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6656 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6657 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6658 tribuf_calcntr = 0x8;
6659 } else {
6660 /* Not supported. Apply the same limits as in the max case */
6661 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6662 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6663 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6664 tribuf_calcntr = 0;
6665 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006666 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6667
Ville Syrjälä968040b2015-03-11 22:52:08 +02006668 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306669 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6670 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6671 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6672
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006673 /* AFC Recal */
6674 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6675 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6676 DPIO_AFC_RECAL);
6677
Ville Syrjäläa5805162015-05-26 20:42:30 +03006678 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006679}
6680
Ville Syrjäläd288f652014-10-28 13:20:22 +02006681/**
6682 * vlv_force_pll_on - forcibly enable just the PLL
6683 * @dev_priv: i915 private structure
6684 * @pipe: pipe PLL to enable
6685 * @dpll: PLL configuration
6686 *
6687 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6688 * in cases where we need the PLL enabled even when @pipe is not going to
6689 * be enabled.
6690 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006691int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006692 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006693{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006694 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006695 struct intel_crtc_state *pipe_config;
6696
6697 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6698 if (!pipe_config)
6699 return -ENOMEM;
6700
6701 pipe_config->base.crtc = &crtc->base;
6702 pipe_config->pixel_multiplier = 1;
6703 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006704
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006705 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006706 chv_compute_dpll(crtc, pipe_config);
6707 chv_prepare_pll(crtc, pipe_config);
6708 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006709 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006710 vlv_compute_dpll(crtc, pipe_config);
6711 vlv_prepare_pll(crtc, pipe_config);
6712 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006713 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00006714
6715 kfree(pipe_config);
6716
6717 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006718}
6719
6720/**
6721 * vlv_force_pll_off - forcibly disable just the PLL
6722 * @dev_priv: i915 private structure
6723 * @pipe: pipe PLL to disable
6724 *
6725 * Disable the PLL for @pipe. To be used in cases where we need
6726 * the PLL enabled even when @pipe is not going to be enabled.
6727 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006728void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006729{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006730 if (IS_CHERRYVIEW(dev_priv))
6731 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006732 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02006733 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02006734}
6735
Daniel Vetter251ac862015-06-18 10:30:24 +02006736static void i9xx_compute_dpll(struct intel_crtc *crtc,
6737 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006738 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006739{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006740 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006741 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006742 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006743
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006744 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306745
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006746 dpll = DPLL_VGA_MODE_DIS;
6747
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006748 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006749 dpll |= DPLLB_MODE_LVDS;
6750 else
6751 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006752
Jani Nikula73f67aa2016-12-07 22:48:09 +02006753 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6754 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006755 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02006756 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006757 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006758
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03006759 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6760 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006761 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006762
Ville Syrjälä37a56502016-06-22 21:57:04 +03006763 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006764 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006765
6766 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006767 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006768 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6769 else {
6770 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006771 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006772 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6773 }
6774 switch (clock->p2) {
6775 case 5:
6776 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6777 break;
6778 case 7:
6779 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6780 break;
6781 case 10:
6782 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6783 break;
6784 case 14:
6785 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6786 break;
6787 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006788 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006789 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6790
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006791 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006792 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006793 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02006794 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006795 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6796 else
6797 dpll |= PLL_REF_INPUT_DREFCLK;
6798
6799 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006800 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006801
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006802 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006803 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02006804 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006805 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006806 }
6807}
6808
Daniel Vetter251ac862015-06-18 10:30:24 +02006809static void i8xx_compute_dpll(struct intel_crtc *crtc,
6810 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006811 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006812{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006813 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006814 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006815 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006816 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006817
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006818 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306819
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006820 dpll = DPLL_VGA_MODE_DIS;
6821
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006822 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006823 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6824 } else {
6825 if (clock->p1 == 2)
6826 dpll |= PLL_P1_DIVIDE_BY_TWO;
6827 else
6828 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6829 if (clock->p2 == 4)
6830 dpll |= PLL_P2_DIVIDE_BY_4;
6831 }
6832
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006833 if (!IS_I830(dev_priv) &&
6834 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006835 dpll |= DPLL_DVO_2X_MODE;
6836
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006837 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02006838 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006839 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6840 else
6841 dpll |= PLL_REF_INPUT_DREFCLK;
6842
6843 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006844 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006845}
6846
Daniel Vetter8a654f32013-06-01 17:16:22 +02006847static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006848{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006849 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006850 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006851 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006852 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006853 uint32_t crtc_vtotal, crtc_vblank_end;
6854 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006855
6856 /* We need to be careful not to changed the adjusted mode, for otherwise
6857 * the hw state checker will get angry at the mismatch. */
6858 crtc_vtotal = adjusted_mode->crtc_vtotal;
6859 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006860
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006861 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006862 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006863 crtc_vtotal -= 1;
6864 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006865
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006866 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006867 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6868 else
6869 vsyncshift = adjusted_mode->crtc_hsync_start -
6870 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006871 if (vsyncshift < 0)
6872 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006873 }
6874
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006875 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006876 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006877
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006878 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006879 (adjusted_mode->crtc_hdisplay - 1) |
6880 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006881 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006882 (adjusted_mode->crtc_hblank_start - 1) |
6883 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006884 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006885 (adjusted_mode->crtc_hsync_start - 1) |
6886 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6887
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006888 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006889 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006890 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006891 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006892 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006893 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006894 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006895 (adjusted_mode->crtc_vsync_start - 1) |
6896 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6897
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006898 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6899 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6900 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6901 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01006902 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006903 (pipe == PIPE_B || pipe == PIPE_C))
6904 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6905
Jani Nikulabc58be62016-03-18 17:05:39 +02006906}
6907
6908static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
6909{
6910 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006911 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02006912 enum pipe pipe = intel_crtc->pipe;
6913
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006914 /* pipesrc controls the size that is scaled from, which should
6915 * always be the user's requested size.
6916 */
6917 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006918 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6919 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006920}
6921
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006922static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006923 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006924{
6925 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006926 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006927 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6928 uint32_t tmp;
6929
6930 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006931 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6932 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006933 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006934 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6935 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006936 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006937 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6938 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006939
6940 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006941 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6942 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006943 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006944 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6945 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006946 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006947 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6948 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006949
6950 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006951 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6952 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6953 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006954 }
Jani Nikulabc58be62016-03-18 17:05:39 +02006955}
6956
6957static void intel_get_pipe_src_size(struct intel_crtc *crtc,
6958 struct intel_crtc_state *pipe_config)
6959{
6960 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006961 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02006962 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006963
6964 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006965 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6966 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6967
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006968 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6969 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006970}
6971
Daniel Vetterf6a83282014-02-11 15:28:57 -08006972void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006973 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006974{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006975 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6976 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6977 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6978 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006979
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006980 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6981 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6982 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6983 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006984
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006985 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02006986 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03006987
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006988 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02006989
6990 mode->hsync = drm_mode_hsync(mode);
6991 mode->vrefresh = drm_mode_vrefresh(mode);
6992 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03006993}
6994
Daniel Vetter84b046f2013-02-19 18:48:54 +01006995static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6996{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006997 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01006998 uint32_t pipeconf;
6999
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007000 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007001
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007002 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7003 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7004 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007005
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007006 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007007 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007008
Daniel Vetterff9ce462013-04-24 14:57:17 +02007009 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007010 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7011 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007012 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007013 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007014 pipeconf |= PIPECONF_DITHER_EN |
7015 PIPECONF_DITHER_TYPE_SP;
7016
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007017 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007018 case 18:
7019 pipeconf |= PIPECONF_6BPC;
7020 break;
7021 case 24:
7022 pipeconf |= PIPECONF_8BPC;
7023 break;
7024 case 30:
7025 pipeconf |= PIPECONF_10BPC;
7026 break;
7027 default:
7028 /* Case prevented by intel_choose_pipe_bpp_dither. */
7029 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007030 }
7031 }
7032
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +00007033 if (HAS_PIPE_CXSR(dev_priv)) {
Daniel Vetter84b046f2013-02-19 18:48:54 +01007034 if (intel_crtc->lowfreq_avail) {
7035 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7036 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7037 } else {
7038 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007039 }
7040 }
7041
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007042 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007043 if (INTEL_GEN(dev_priv) < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007044 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007045 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7046 else
7047 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7048 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007049 pipeconf |= PIPECONF_PROGRESSIVE;
7050
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007051 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007052 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007053 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007054
Daniel Vetter84b046f2013-02-19 18:48:54 +01007055 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7056 POSTING_READ(PIPECONF(intel_crtc->pipe));
7057}
7058
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007059static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7060 struct intel_crtc_state *crtc_state)
7061{
7062 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007063 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007064 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007065 int refclk = 48000;
7066
7067 memset(&crtc_state->dpll_hw_state, 0,
7068 sizeof(crtc_state->dpll_hw_state));
7069
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007070 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007071 if (intel_panel_use_ssc(dev_priv)) {
7072 refclk = dev_priv->vbt.lvds_ssc_freq;
7073 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7074 }
7075
7076 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007077 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007078 limit = &intel_limits_i8xx_dvo;
7079 } else {
7080 limit = &intel_limits_i8xx_dac;
7081 }
7082
7083 if (!crtc_state->clock_set &&
7084 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7085 refclk, NULL, &crtc_state->dpll)) {
7086 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7087 return -EINVAL;
7088 }
7089
7090 i8xx_compute_dpll(crtc, crtc_state, NULL);
7091
7092 return 0;
7093}
7094
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007095static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7096 struct intel_crtc_state *crtc_state)
7097{
7098 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007099 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007100 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007101 int refclk = 96000;
7102
7103 memset(&crtc_state->dpll_hw_state, 0,
7104 sizeof(crtc_state->dpll_hw_state));
7105
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007106 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007107 if (intel_panel_use_ssc(dev_priv)) {
7108 refclk = dev_priv->vbt.lvds_ssc_freq;
7109 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7110 }
7111
7112 if (intel_is_dual_link_lvds(dev))
7113 limit = &intel_limits_g4x_dual_channel_lvds;
7114 else
7115 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007116 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7117 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007118 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007119 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007120 limit = &intel_limits_g4x_sdvo;
7121 } else {
7122 /* The option is for other outputs */
7123 limit = &intel_limits_i9xx_sdvo;
7124 }
7125
7126 if (!crtc_state->clock_set &&
7127 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7128 refclk, NULL, &crtc_state->dpll)) {
7129 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7130 return -EINVAL;
7131 }
7132
7133 i9xx_compute_dpll(crtc, crtc_state, NULL);
7134
7135 return 0;
7136}
7137
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007138static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7139 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007140{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007141 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007142 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007143 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007144 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007145
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007146 memset(&crtc_state->dpll_hw_state, 0,
7147 sizeof(crtc_state->dpll_hw_state));
7148
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007149 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007150 if (intel_panel_use_ssc(dev_priv)) {
7151 refclk = dev_priv->vbt.lvds_ssc_freq;
7152 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7153 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007154
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007155 limit = &intel_limits_pineview_lvds;
7156 } else {
7157 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007158 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007159
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007160 if (!crtc_state->clock_set &&
7161 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7162 refclk, NULL, &crtc_state->dpll)) {
7163 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7164 return -EINVAL;
7165 }
7166
7167 i9xx_compute_dpll(crtc, crtc_state, NULL);
7168
7169 return 0;
7170}
7171
7172static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7173 struct intel_crtc_state *crtc_state)
7174{
7175 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007176 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007177 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007178 int refclk = 96000;
7179
7180 memset(&crtc_state->dpll_hw_state, 0,
7181 sizeof(crtc_state->dpll_hw_state));
7182
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007183 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007184 if (intel_panel_use_ssc(dev_priv)) {
7185 refclk = dev_priv->vbt.lvds_ssc_freq;
7186 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007187 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007188
7189 limit = &intel_limits_i9xx_lvds;
7190 } else {
7191 limit = &intel_limits_i9xx_sdvo;
7192 }
7193
7194 if (!crtc_state->clock_set &&
7195 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7196 refclk, NULL, &crtc_state->dpll)) {
7197 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7198 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007199 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007200
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007201 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007202
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007203 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007204}
7205
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007206static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7207 struct intel_crtc_state *crtc_state)
7208{
7209 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007210 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007211
7212 memset(&crtc_state->dpll_hw_state, 0,
7213 sizeof(crtc_state->dpll_hw_state));
7214
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007215 if (!crtc_state->clock_set &&
7216 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7217 refclk, NULL, &crtc_state->dpll)) {
7218 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7219 return -EINVAL;
7220 }
7221
7222 chv_compute_dpll(crtc, crtc_state);
7223
7224 return 0;
7225}
7226
7227static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7228 struct intel_crtc_state *crtc_state)
7229{
7230 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007231 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007232
7233 memset(&crtc_state->dpll_hw_state, 0,
7234 sizeof(crtc_state->dpll_hw_state));
7235
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007236 if (!crtc_state->clock_set &&
7237 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7238 refclk, NULL, &crtc_state->dpll)) {
7239 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7240 return -EINVAL;
7241 }
7242
7243 vlv_compute_dpll(crtc, crtc_state);
7244
7245 return 0;
7246}
7247
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007248static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007249 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007250{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007251 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007252 uint32_t tmp;
7253
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007254 if (INTEL_GEN(dev_priv) <= 3 &&
7255 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007256 return;
7257
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007258 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007259 if (!(tmp & PFIT_ENABLE))
7260 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007261
Daniel Vetter06922822013-07-11 13:35:40 +02007262 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007263 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007264 if (crtc->pipe != PIPE_B)
7265 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007266 } else {
7267 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7268 return;
7269 }
7270
Daniel Vetter06922822013-07-11 13:35:40 +02007271 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007272 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007273}
7274
Jesse Barnesacbec812013-09-20 11:29:32 -07007275static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007276 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007277{
7278 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007279 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07007280 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007281 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07007282 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007283 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007284
Ville Syrjäläb5219732016-03-15 16:40:01 +02007285 /* In case of DSI, DPLL will not be used */
7286 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05307287 return;
7288
Ville Syrjäläa5805162015-05-26 20:42:30 +03007289 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007290 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007291 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007292
7293 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7294 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7295 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7296 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7297 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7298
Imre Deakdccbea32015-06-22 23:35:51 +03007299 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007300}
7301
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007302static void
7303i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7304 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007305{
7306 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007307 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007308 u32 val, base, offset;
7309 int pipe = crtc->pipe, plane = crtc->plane;
7310 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007311 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007312 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007313 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007314
Damien Lespiau42a7b082015-02-05 19:35:13 +00007315 val = I915_READ(DSPCNTR(plane));
7316 if (!(val & DISPLAY_PLANE_ENABLE))
7317 return;
7318
Damien Lespiaud9806c92015-01-21 14:07:19 +00007319 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007320 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007321 DRM_DEBUG_KMS("failed to alloc fb\n");
7322 return;
7323 }
7324
Damien Lespiau1b842c82015-01-21 13:50:54 +00007325 fb = &intel_fb->base;
7326
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02007327 fb->dev = dev;
7328
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007329 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00007330 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007331 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02007332 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00007333 }
7334 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007335
7336 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007337 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02007338 fb->format = drm_format_info(fourcc);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007339
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007340 if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007341 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007342 offset = I915_READ(DSPTILEOFF(plane));
7343 else
7344 offset = I915_READ(DSPLINOFF(plane));
7345 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7346 } else {
7347 base = I915_READ(DSPADDR(plane));
7348 }
7349 plane_config->base = base;
7350
7351 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007352 fb->width = ((val >> 16) & 0xfff) + 1;
7353 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007354
7355 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007356 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007357
Chris Wilson24dbf512017-02-15 10:59:18 +00007358 aligned_height = intel_fb_align_height(dev_priv,
7359 fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02007360 fb->format->format,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02007361 fb->modifier);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007362
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007363 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007364
Damien Lespiau2844a922015-01-20 12:51:48 +00007365 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7366 pipe_name(pipe), plane, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02007367 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00007368 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007369
Damien Lespiau2d140302015-02-05 17:22:18 +00007370 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007371}
7372
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007373static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007374 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007375{
7376 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007377 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007378 int pipe = pipe_config->cpu_transcoder;
7379 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007380 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007381 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007382 int refclk = 100000;
7383
Ville Syrjäläb5219732016-03-15 16:40:01 +02007384 /* In case of DSI, DPLL will not be used */
7385 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7386 return;
7387
Ville Syrjäläa5805162015-05-26 20:42:30 +03007388 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007389 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7390 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7391 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7392 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007393 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007394 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007395
7396 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007397 clock.m2 = (pll_dw0 & 0xff) << 22;
7398 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7399 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007400 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7401 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7402 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7403
Imre Deakdccbea32015-06-22 23:35:51 +03007404 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007405}
7406
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007407static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007408 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007409{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007410 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02007411 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007412 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02007413 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007414
Imre Deak17290502016-02-12 18:55:11 +02007415 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7416 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02007417 return false;
7418
Daniel Vettere143a212013-07-04 12:01:15 +02007419 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02007420 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02007421
Imre Deak17290502016-02-12 18:55:11 +02007422 ret = false;
7423
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007424 tmp = I915_READ(PIPECONF(crtc->pipe));
7425 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02007426 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007427
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007428 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7429 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007430 switch (tmp & PIPECONF_BPC_MASK) {
7431 case PIPECONF_6BPC:
7432 pipe_config->pipe_bpp = 18;
7433 break;
7434 case PIPECONF_8BPC:
7435 pipe_config->pipe_bpp = 24;
7436 break;
7437 case PIPECONF_10BPC:
7438 pipe_config->pipe_bpp = 30;
7439 break;
7440 default:
7441 break;
7442 }
7443 }
7444
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007445 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007446 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007447 pipe_config->limited_color_range = true;
7448
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007449 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03007450 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7451
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007452 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02007453 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007454
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007455 i9xx_get_pfit_config(crtc, pipe_config);
7456
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007457 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02007458 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007459 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02007460 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7461 else
7462 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02007463 pipe_config->pixel_multiplier =
7464 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7465 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007466 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007467 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02007468 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02007469 tmp = I915_READ(DPLL(crtc->pipe));
7470 pipe_config->pixel_multiplier =
7471 ((tmp & SDVO_MULTIPLIER_MASK)
7472 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7473 } else {
7474 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7475 * port and will be fixed up in the encoder->get_config
7476 * function. */
7477 pipe_config->pixel_multiplier = 1;
7478 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007479 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007480 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007481 /*
7482 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7483 * on 830. Filter it out here so that we don't
7484 * report errors due to that.
7485 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007486 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007487 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7488
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007489 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7490 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007491 } else {
7492 /* Mask out read-only status bits. */
7493 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7494 DPLL_PORTC_READY_MASK |
7495 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007496 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007497
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007498 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007499 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007500 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07007501 vlv_crtc_clock_get(crtc, pipe_config);
7502 else
7503 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007504
Ville Syrjälä0f646142015-08-26 19:39:18 +03007505 /*
7506 * Normally the dotclock is filled in by the encoder .get_config()
7507 * but in case the pipe is enabled w/o any ports we need a sane
7508 * default.
7509 */
7510 pipe_config->base.adjusted_mode.crtc_clock =
7511 pipe_config->port_clock / pipe_config->pixel_multiplier;
7512
Imre Deak17290502016-02-12 18:55:11 +02007513 ret = true;
7514
7515out:
7516 intel_display_power_put(dev_priv, power_domain);
7517
7518 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007519}
7520
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007521static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007522{
Jesse Barnes13d83a62011-08-03 12:59:20 -07007523 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04007524 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007525 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007526 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007527 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007528 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007529 bool has_ck505 = false;
7530 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04007531 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007532
7533 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007534 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007535 switch (encoder->type) {
7536 case INTEL_OUTPUT_LVDS:
7537 has_panel = true;
7538 has_lvds = true;
7539 break;
7540 case INTEL_OUTPUT_EDP:
7541 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007542 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007543 has_cpu_edp = true;
7544 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007545 default:
7546 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007547 }
7548 }
7549
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007550 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007551 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007552 can_ssc = has_ck505;
7553 } else {
7554 has_ck505 = false;
7555 can_ssc = true;
7556 }
7557
Lyude1c1a24d2016-06-14 11:04:09 -04007558 /* Check if any DPLLs are using the SSC source */
7559 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7560 u32 temp = I915_READ(PCH_DPLL(i));
7561
7562 if (!(temp & DPLL_VCO_ENABLE))
7563 continue;
7564
7565 if ((temp & PLL_REF_INPUT_MASK) ==
7566 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7567 using_ssc_source = true;
7568 break;
7569 }
7570 }
7571
7572 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7573 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007574
7575 /* Ironlake: try to setup display ref clock before DPLL
7576 * enabling. This is only under driver's control after
7577 * PCH B stepping, previous chipset stepping should be
7578 * ignoring this setting.
7579 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007580 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007581
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007582 /* As we must carefully and slowly disable/enable each source in turn,
7583 * compute the final state we want first and check if we need to
7584 * make any changes at all.
7585 */
7586 final = val;
7587 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007588 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007589 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007590 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007591 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7592
Daniel Vetter8c07eb62016-06-09 18:39:07 +02007593 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007594 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02007595 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007596
Keith Packard199e5d72011-09-22 12:01:57 -07007597 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007598 final |= DREF_SSC_SOURCE_ENABLE;
7599
7600 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7601 final |= DREF_SSC1_ENABLE;
7602
7603 if (has_cpu_edp) {
7604 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7605 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7606 else
7607 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7608 } else
7609 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04007610 } else if (using_ssc_source) {
7611 final |= DREF_SSC_SOURCE_ENABLE;
7612 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007613 }
7614
7615 if (final == val)
7616 return;
7617
7618 /* Always enable nonspread source */
7619 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7620
7621 if (has_ck505)
7622 val |= DREF_NONSPREAD_CK505_ENABLE;
7623 else
7624 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7625
7626 if (has_panel) {
7627 val &= ~DREF_SSC_SOURCE_MASK;
7628 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007629
Keith Packard199e5d72011-09-22 12:01:57 -07007630 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007631 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007632 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007633 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007634 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007635 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007636
7637 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007638 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007639 POSTING_READ(PCH_DREF_CONTROL);
7640 udelay(200);
7641
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007642 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007643
7644 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007645 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007646 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007647 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007648 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007649 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007650 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007651 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007652 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007653
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007654 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007655 POSTING_READ(PCH_DREF_CONTROL);
7656 udelay(200);
7657 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04007658 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07007659
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007660 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007661
7662 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007663 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007664
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007665 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007666 POSTING_READ(PCH_DREF_CONTROL);
7667 udelay(200);
7668
Lyude1c1a24d2016-06-14 11:04:09 -04007669 if (!using_ssc_source) {
7670 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07007671
Lyude1c1a24d2016-06-14 11:04:09 -04007672 /* Turn off the SSC source */
7673 val &= ~DREF_SSC_SOURCE_MASK;
7674 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007675
Lyude1c1a24d2016-06-14 11:04:09 -04007676 /* Turn off SSC1 */
7677 val &= ~DREF_SSC1_ENABLE;
7678
7679 I915_WRITE(PCH_DREF_CONTROL, val);
7680 POSTING_READ(PCH_DREF_CONTROL);
7681 udelay(200);
7682 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07007683 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007684
7685 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007686}
7687
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007688static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007689{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007690 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007691
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007692 tmp = I915_READ(SOUTH_CHICKEN2);
7693 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7694 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007695
Imre Deakcf3598c2016-06-28 13:37:31 +03007696 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7697 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007698 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007699
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007700 tmp = I915_READ(SOUTH_CHICKEN2);
7701 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7702 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007703
Imre Deakcf3598c2016-06-28 13:37:31 +03007704 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7705 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007706 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007707}
7708
7709/* WaMPhyProgramming:hsw */
7710static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7711{
7712 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007713
7714 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7715 tmp &= ~(0xFF << 24);
7716 tmp |= (0x12 << 24);
7717 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7718
Paulo Zanonidde86e22012-12-01 12:04:25 -02007719 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7720 tmp |= (1 << 11);
7721 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7722
7723 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7724 tmp |= (1 << 11);
7725 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7726
Paulo Zanonidde86e22012-12-01 12:04:25 -02007727 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7728 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7729 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7730
7731 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7732 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7733 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7734
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007735 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7736 tmp &= ~(7 << 13);
7737 tmp |= (5 << 13);
7738 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007739
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007740 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7741 tmp &= ~(7 << 13);
7742 tmp |= (5 << 13);
7743 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007744
7745 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7746 tmp &= ~0xFF;
7747 tmp |= 0x1C;
7748 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7749
7750 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7751 tmp &= ~0xFF;
7752 tmp |= 0x1C;
7753 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7754
7755 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7756 tmp &= ~(0xFF << 16);
7757 tmp |= (0x1C << 16);
7758 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7759
7760 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7761 tmp &= ~(0xFF << 16);
7762 tmp |= (0x1C << 16);
7763 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7764
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007765 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7766 tmp |= (1 << 27);
7767 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007768
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007769 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7770 tmp |= (1 << 27);
7771 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007772
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007773 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7774 tmp &= ~(0xF << 28);
7775 tmp |= (4 << 28);
7776 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007777
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007778 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7779 tmp &= ~(0xF << 28);
7780 tmp |= (4 << 28);
7781 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007782}
7783
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007784/* Implements 3 different sequences from BSpec chapter "Display iCLK
7785 * Programming" based on the parameters passed:
7786 * - Sequence to enable CLKOUT_DP
7787 * - Sequence to enable CLKOUT_DP without spread
7788 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7789 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007790static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7791 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007792{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007793 uint32_t reg, tmp;
7794
7795 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7796 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007797 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7798 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007799 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007800
Ville Syrjäläa5805162015-05-26 20:42:30 +03007801 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007802
7803 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7804 tmp &= ~SBI_SSCCTL_DISABLE;
7805 tmp |= SBI_SSCCTL_PATHALT;
7806 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7807
7808 udelay(24);
7809
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007810 if (with_spread) {
7811 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7812 tmp &= ~SBI_SSCCTL_PATHALT;
7813 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007814
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007815 if (with_fdi) {
7816 lpt_reset_fdi_mphy(dev_priv);
7817 lpt_program_fdi_mphy(dev_priv);
7818 }
7819 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007820
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007821 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007822 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7823 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7824 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007825
Ville Syrjäläa5805162015-05-26 20:42:30 +03007826 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007827}
7828
Paulo Zanoni47701c32013-07-23 11:19:25 -03007829/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007830static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03007831{
Paulo Zanoni47701c32013-07-23 11:19:25 -03007832 uint32_t reg, tmp;
7833
Ville Syrjäläa5805162015-05-26 20:42:30 +03007834 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03007835
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007836 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03007837 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7838 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7839 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7840
7841 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7842 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7843 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7844 tmp |= SBI_SSCCTL_PATHALT;
7845 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7846 udelay(32);
7847 }
7848 tmp |= SBI_SSCCTL_DISABLE;
7849 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7850 }
7851
Ville Syrjäläa5805162015-05-26 20:42:30 +03007852 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03007853}
7854
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007855#define BEND_IDX(steps) ((50 + (steps)) / 5)
7856
7857static const uint16_t sscdivintphase[] = {
7858 [BEND_IDX( 50)] = 0x3B23,
7859 [BEND_IDX( 45)] = 0x3B23,
7860 [BEND_IDX( 40)] = 0x3C23,
7861 [BEND_IDX( 35)] = 0x3C23,
7862 [BEND_IDX( 30)] = 0x3D23,
7863 [BEND_IDX( 25)] = 0x3D23,
7864 [BEND_IDX( 20)] = 0x3E23,
7865 [BEND_IDX( 15)] = 0x3E23,
7866 [BEND_IDX( 10)] = 0x3F23,
7867 [BEND_IDX( 5)] = 0x3F23,
7868 [BEND_IDX( 0)] = 0x0025,
7869 [BEND_IDX( -5)] = 0x0025,
7870 [BEND_IDX(-10)] = 0x0125,
7871 [BEND_IDX(-15)] = 0x0125,
7872 [BEND_IDX(-20)] = 0x0225,
7873 [BEND_IDX(-25)] = 0x0225,
7874 [BEND_IDX(-30)] = 0x0325,
7875 [BEND_IDX(-35)] = 0x0325,
7876 [BEND_IDX(-40)] = 0x0425,
7877 [BEND_IDX(-45)] = 0x0425,
7878 [BEND_IDX(-50)] = 0x0525,
7879};
7880
7881/*
7882 * Bend CLKOUT_DP
7883 * steps -50 to 50 inclusive, in steps of 5
7884 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7885 * change in clock period = -(steps / 10) * 5.787 ps
7886 */
7887static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7888{
7889 uint32_t tmp;
7890 int idx = BEND_IDX(steps);
7891
7892 if (WARN_ON(steps % 5 != 0))
7893 return;
7894
7895 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7896 return;
7897
7898 mutex_lock(&dev_priv->sb_lock);
7899
7900 if (steps % 10 != 0)
7901 tmp = 0xAAAAAAAB;
7902 else
7903 tmp = 0x00000000;
7904 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7905
7906 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7907 tmp &= 0xffff0000;
7908 tmp |= sscdivintphase[idx];
7909 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7910
7911 mutex_unlock(&dev_priv->sb_lock);
7912}
7913
7914#undef BEND_IDX
7915
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007916static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007917{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007918 struct intel_encoder *encoder;
7919 bool has_vga = false;
7920
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007921 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007922 switch (encoder->type) {
7923 case INTEL_OUTPUT_ANALOG:
7924 has_vga = true;
7925 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007926 default:
7927 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007928 }
7929 }
7930
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007931 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007932 lpt_bend_clkout_dp(dev_priv, 0);
7933 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007934 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007935 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02007936 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03007937}
7938
Paulo Zanonidde86e22012-12-01 12:04:25 -02007939/*
7940 * Initialize reference clocks when the driver loads
7941 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007942void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007943{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007944 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007945 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007946 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007947 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007948}
7949
Daniel Vetter6ff93602013-04-19 11:24:36 +02007950static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007951{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007952 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03007953 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7954 int pipe = intel_crtc->pipe;
7955 uint32_t val;
7956
Daniel Vetter78114072013-06-13 00:54:57 +02007957 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007958
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007959 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007960 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007961 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007962 break;
7963 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007964 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007965 break;
7966 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007967 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007968 break;
7969 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007970 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007971 break;
7972 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007973 /* Case prevented by intel_choose_pipe_bpp_dither. */
7974 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007975 }
7976
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007977 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007978 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007980 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007981 val |= PIPECONF_INTERLACED_ILK;
7982 else
7983 val |= PIPECONF_PROGRESSIVE;
7984
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007985 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007986 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007987
Paulo Zanonic8203562012-09-12 10:06:29 -03007988 I915_WRITE(PIPECONF(pipe), val);
7989 POSTING_READ(PIPECONF(pipe));
7990}
7991
Daniel Vetter6ff93602013-04-19 11:24:36 +02007992static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007993{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007994 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007996 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02007997 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007998
Jani Nikula391bf042016-03-18 17:05:40 +02007999 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008000 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8001
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008002 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008003 val |= PIPECONF_INTERLACED_ILK;
8004 else
8005 val |= PIPECONF_PROGRESSIVE;
8006
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008007 I915_WRITE(PIPECONF(cpu_transcoder), val);
8008 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008009}
8010
Jani Nikula391bf042016-03-18 17:05:40 +02008011static void haswell_set_pipemisc(struct drm_crtc *crtc)
8012{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008013 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8015
8016 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8017 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008018
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008019 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008020 case 18:
8021 val |= PIPEMISC_DITHER_6_BPC;
8022 break;
8023 case 24:
8024 val |= PIPEMISC_DITHER_8_BPC;
8025 break;
8026 case 30:
8027 val |= PIPEMISC_DITHER_10_BPC;
8028 break;
8029 case 36:
8030 val |= PIPEMISC_DITHER_12_BPC;
8031 break;
8032 default:
8033 /* Case prevented by pipe_config_set_bpp. */
8034 BUG();
8035 }
8036
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008037 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008038 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8039
Jani Nikula391bf042016-03-18 17:05:40 +02008040 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008041 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008042}
8043
Paulo Zanonid4b19312012-11-29 11:29:32 -02008044int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8045{
8046 /*
8047 * Account for spread spectrum to avoid
8048 * oversubscribing the link. Max center spread
8049 * is 2.5%; use 5% for safety's sake.
8050 */
8051 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008052 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008053}
8054
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008055static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008056{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008057 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008058}
8059
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008060static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8061 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008062 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008063{
8064 struct drm_crtc *crtc = &intel_crtc->base;
8065 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008066 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008067 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008068 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008069
Chris Wilsonc1858122010-12-03 21:35:48 +00008070 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008071 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008072 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008073 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008074 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008075 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008076 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008077 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008078 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008079
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008080 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008081
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008082 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8083 fp |= FP_CB_TUNE;
8084
8085 if (reduced_clock) {
8086 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8087
8088 if (reduced_clock->m < factor * reduced_clock->n)
8089 fp2 |= FP_CB_TUNE;
8090 } else {
8091 fp2 = fp;
8092 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008093
Chris Wilson5eddb702010-09-11 13:48:45 +01008094 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008095
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008096 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008097 dpll |= DPLLB_MODE_LVDS;
8098 else
8099 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008100
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008101 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008102 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008103
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008104 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8105 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008106 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008107
Ville Syrjälä37a56502016-06-22 21:57:04 +03008108 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008109 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008110
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03008111 /*
8112 * The high speed IO clock is only really required for
8113 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8114 * possible to share the DPLL between CRT and HDMI. Enabling
8115 * the clock needlessly does no real harm, except use up a
8116 * bit of power potentially.
8117 *
8118 * We'll limit this to IVB with 3 pipes, since it has only two
8119 * DPLLs and so DPLL sharing is the only way to get three pipes
8120 * driving PCH ports at the same time. On SNB we could do this,
8121 * and potentially avoid enabling the second DPLL, but it's not
8122 * clear if it''s a win or loss power wise. No point in doing
8123 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8124 */
8125 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8126 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8127 dpll |= DPLL_SDVO_HIGH_SPEED;
8128
Eric Anholta07d6782011-03-30 13:01:08 -07008129 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008130 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008131 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008132 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008133
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008134 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008135 case 5:
8136 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8137 break;
8138 case 7:
8139 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8140 break;
8141 case 10:
8142 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8143 break;
8144 case 14:
8145 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8146 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008147 }
8148
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008149 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8150 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008151 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008152 else
8153 dpll |= PLL_REF_INPUT_DREFCLK;
8154
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008155 dpll |= DPLL_VCO_ENABLE;
8156
8157 crtc_state->dpll_hw_state.dpll = dpll;
8158 crtc_state->dpll_hw_state.fp0 = fp;
8159 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008160}
8161
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008162static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8163 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008164{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008165 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008166 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008167 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008168 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008169 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008170 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008171 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008172
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008173 memset(&crtc_state->dpll_hw_state, 0,
8174 sizeof(crtc_state->dpll_hw_state));
8175
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008176 crtc->lowfreq_avail = false;
8177
8178 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8179 if (!crtc_state->has_pch_encoder)
8180 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008181
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008182 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008183 if (intel_panel_use_ssc(dev_priv)) {
8184 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8185 dev_priv->vbt.lvds_ssc_freq);
8186 refclk = dev_priv->vbt.lvds_ssc_freq;
8187 }
8188
8189 if (intel_is_dual_link_lvds(dev)) {
8190 if (refclk == 100000)
8191 limit = &intel_limits_ironlake_dual_lvds_100m;
8192 else
8193 limit = &intel_limits_ironlake_dual_lvds;
8194 } else {
8195 if (refclk == 100000)
8196 limit = &intel_limits_ironlake_single_lvds_100m;
8197 else
8198 limit = &intel_limits_ironlake_single_lvds;
8199 }
8200 } else {
8201 limit = &intel_limits_ironlake_dac;
8202 }
8203
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008204 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008205 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8206 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008207 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8208 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008209 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008210
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008211 ironlake_compute_dpll(crtc, crtc_state,
8212 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008213
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008214 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8215 if (pll == NULL) {
8216 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8217 pipe_name(crtc->pipe));
8218 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008219 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008220
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008221 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008222 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008223 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008224
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008225 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008226}
8227
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008228static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8229 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008230{
8231 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008232 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008233 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008234
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008235 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8236 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8237 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8238 & ~TU_SIZE_MASK;
8239 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8240 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8241 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8242}
8243
8244static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8245 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008246 struct intel_link_m_n *m_n,
8247 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008248{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008249 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008250 enum pipe pipe = crtc->pipe;
8251
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008252 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008253 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8254 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8255 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8256 & ~TU_SIZE_MASK;
8257 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8258 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8259 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008260 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8261 * gen < 8) and if DRRS is supported (to make sure the
8262 * registers are not unnecessarily read).
8263 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008264 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008265 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008266 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8267 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8268 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8269 & ~TU_SIZE_MASK;
8270 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8271 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8272 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8273 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008274 } else {
8275 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8276 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8277 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8278 & ~TU_SIZE_MASK;
8279 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8280 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8281 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8282 }
8283}
8284
8285void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008286 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008287{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008288 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008289 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8290 else
8291 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008292 &pipe_config->dp_m_n,
8293 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008294}
8295
Daniel Vetter72419202013-04-04 13:28:53 +02008296static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008297 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008298{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008299 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008300 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008301}
8302
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008303static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008304 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008305{
8306 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008307 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07008308 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8309 uint32_t ps_ctrl = 0;
8310 int id = -1;
8311 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008312
Chandra Kondurua1b22782015-04-07 15:28:45 -07008313 /* find scaler attached to this pipe */
8314 for (i = 0; i < crtc->num_scalers; i++) {
8315 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8316 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8317 id = i;
8318 pipe_config->pch_pfit.enabled = true;
8319 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8320 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8321 break;
8322 }
8323 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008324
Chandra Kondurua1b22782015-04-07 15:28:45 -07008325 scaler_state->scaler_id = id;
8326 if (id >= 0) {
8327 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8328 } else {
8329 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008330 }
8331}
8332
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008333static void
8334skylake_get_initial_plane_config(struct intel_crtc *crtc,
8335 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008336{
8337 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008338 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00008339 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008340 int pipe = crtc->pipe;
8341 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008342 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008343 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008344 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008345
Damien Lespiaud9806c92015-01-21 14:07:19 +00008346 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008347 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008348 DRM_DEBUG_KMS("failed to alloc fb\n");
8349 return;
8350 }
8351
Damien Lespiau1b842c82015-01-21 13:50:54 +00008352 fb = &intel_fb->base;
8353
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008354 fb->dev = dev;
8355
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008356 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008357 if (!(val & PLANE_CTL_ENABLE))
8358 goto error;
8359
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008360 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8361 fourcc = skl_format_to_fourcc(pixel_format,
8362 val & PLANE_CTL_ORDER_RGBX,
8363 val & PLANE_CTL_ALPHA_MASK);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008364 fb->format = drm_format_info(fourcc);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008365
Damien Lespiau40f46282015-02-27 11:15:21 +00008366 tiling = val & PLANE_CTL_TILED_MASK;
8367 switch (tiling) {
8368 case PLANE_CTL_TILED_LINEAR:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008369 fb->modifier = DRM_FORMAT_MOD_NONE;
Damien Lespiau40f46282015-02-27 11:15:21 +00008370 break;
8371 case PLANE_CTL_TILED_X:
8372 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008373 fb->modifier = I915_FORMAT_MOD_X_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008374 break;
8375 case PLANE_CTL_TILED_Y:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008376 fb->modifier = I915_FORMAT_MOD_Y_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008377 break;
8378 case PLANE_CTL_TILED_YF:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008379 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008380 break;
8381 default:
8382 MISSING_CASE(tiling);
8383 goto error;
8384 }
8385
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008386 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8387 plane_config->base = base;
8388
8389 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8390
8391 val = I915_READ(PLANE_SIZE(pipe, 0));
8392 fb->height = ((val >> 16) & 0xfff) + 1;
8393 fb->width = ((val >> 0) & 0x1fff) + 1;
8394
8395 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008396 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02008397 fb->format->format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008398 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8399
Chris Wilson24dbf512017-02-15 10:59:18 +00008400 aligned_height = intel_fb_align_height(dev_priv,
8401 fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02008402 fb->format->format,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008403 fb->modifier);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008404
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008405 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008406
8407 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8408 pipe_name(pipe), fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008409 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008410 plane_config->size);
8411
Damien Lespiau2d140302015-02-05 17:22:18 +00008412 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008413 return;
8414
8415error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01008416 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008417}
8418
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008419static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008420 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008421{
8422 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008423 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008424 uint32_t tmp;
8425
8426 tmp = I915_READ(PF_CTL(crtc->pipe));
8427
8428 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008429 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008430 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8431 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008432
8433 /* We currently do not free assignements of panel fitters on
8434 * ivb/hsw (since we don't use the higher upscaling modes which
8435 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008436 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008437 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8438 PF_PIPE_SEL_IVB(crtc->pipe));
8439 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008440 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008441}
8442
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008443static void
8444ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8445 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008446{
8447 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008448 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008449 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008450 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008451 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008452 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008453 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008454 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008455
Damien Lespiau42a7b082015-02-05 19:35:13 +00008456 val = I915_READ(DSPCNTR(pipe));
8457 if (!(val & DISPLAY_PLANE_ENABLE))
8458 return;
8459
Damien Lespiaud9806c92015-01-21 14:07:19 +00008460 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008461 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008462 DRM_DEBUG_KMS("failed to alloc fb\n");
8463 return;
8464 }
8465
Damien Lespiau1b842c82015-01-21 13:50:54 +00008466 fb = &intel_fb->base;
8467
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008468 fb->dev = dev;
8469
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008470 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00008471 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008472 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008473 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00008474 }
8475 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008476
8477 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008478 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008479 fb->format = drm_format_info(fourcc);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008480
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008481 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01008482 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008483 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008484 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008485 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008486 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008487 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008488 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008489 }
8490 plane_config->base = base;
8491
8492 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008493 fb->width = ((val >> 16) & 0xfff) + 1;
8494 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008495
8496 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008497 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008498
Chris Wilson24dbf512017-02-15 10:59:18 +00008499 aligned_height = intel_fb_align_height(dev_priv,
8500 fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02008501 fb->format->format,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008502 fb->modifier);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008503
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008504 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008505
Damien Lespiau2844a922015-01-20 12:51:48 +00008506 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8507 pipe_name(pipe), fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008508 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00008509 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008510
Damien Lespiau2d140302015-02-05 17:22:18 +00008511 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008512}
8513
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008514static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008515 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008516{
8517 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008518 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008519 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008520 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008521 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008522
Imre Deak17290502016-02-12 18:55:11 +02008523 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8524 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008525 return false;
8526
Daniel Vettere143a212013-07-04 12:01:15 +02008527 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008528 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008529
Imre Deak17290502016-02-12 18:55:11 +02008530 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008531 tmp = I915_READ(PIPECONF(crtc->pipe));
8532 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008533 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008534
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008535 switch (tmp & PIPECONF_BPC_MASK) {
8536 case PIPECONF_6BPC:
8537 pipe_config->pipe_bpp = 18;
8538 break;
8539 case PIPECONF_8BPC:
8540 pipe_config->pipe_bpp = 24;
8541 break;
8542 case PIPECONF_10BPC:
8543 pipe_config->pipe_bpp = 30;
8544 break;
8545 case PIPECONF_12BPC:
8546 pipe_config->pipe_bpp = 36;
8547 break;
8548 default:
8549 break;
8550 }
8551
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008552 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8553 pipe_config->limited_color_range = true;
8554
Daniel Vetterab9412b2013-05-03 11:49:46 +02008555 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008556 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008557 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008558
Daniel Vetter88adfff2013-03-28 10:42:01 +01008559 pipe_config->has_pch_encoder = true;
8560
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008561 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8562 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8563 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008564
8565 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008566
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008567 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03008568 /*
8569 * The pipe->pch transcoder and pch transcoder->pll
8570 * mapping is fixed.
8571 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008572 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008573 } else {
8574 tmp = I915_READ(PCH_DPLL_SEL);
8575 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008576 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008577 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008578 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008579 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008580
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008581 pipe_config->shared_dpll =
8582 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8583 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008584
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02008585 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8586 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008587
8588 tmp = pipe_config->dpll_hw_state.dpll;
8589 pipe_config->pixel_multiplier =
8590 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8591 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008592
8593 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008594 } else {
8595 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008596 }
8597
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008598 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008599 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008600
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008601 ironlake_get_pfit_config(crtc, pipe_config);
8602
Imre Deak17290502016-02-12 18:55:11 +02008603 ret = true;
8604
8605out:
8606 intel_display_power_put(dev_priv, power_domain);
8607
8608 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008609}
8610
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008611static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8612{
Chris Wilson91c8a322016-07-05 10:40:23 +01008613 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008614 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008615
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008616 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008617 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008618 pipe_name(crtc->pipe));
8619
Rob Clarke2c719b2014-12-15 13:56:32 -05008620 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8621 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03008622 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8623 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03008624 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008625 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008626 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008627 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -05008628 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008629 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008630 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008631 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008632 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008633 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008634 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008635
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008636 /*
8637 * In theory we can still leave IRQs enabled, as long as only the HPD
8638 * interrupts remain enabled. We used to check for that, but since it's
8639 * gen-specific and since we only disable LCPLL after we fully disable
8640 * the interrupts, the check below should be enough.
8641 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008642 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008643}
8644
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008645static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8646{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008647 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008648 return I915_READ(D_COMP_HSW);
8649 else
8650 return I915_READ(D_COMP_BDW);
8651}
8652
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008653static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8654{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008655 if (IS_HASWELL(dev_priv)) {
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008656 mutex_lock(&dev_priv->rps.hw_lock);
8657 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8658 val))
Chris Wilson79cf2192016-08-24 11:16:07 +01008659 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008660 mutex_unlock(&dev_priv->rps.hw_lock);
8661 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008662 I915_WRITE(D_COMP_BDW, val);
8663 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008664 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008665}
8666
8667/*
8668 * This function implements pieces of two sequences from BSpec:
8669 * - Sequence for display software to disable LCPLL
8670 * - Sequence for display software to allow package C8+
8671 * The steps implemented here are just the steps that actually touch the LCPLL
8672 * register. Callers should take care of disabling all the display engine
8673 * functions, doing the mode unset, fixing interrupts, etc.
8674 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008675static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8676 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008677{
8678 uint32_t val;
8679
8680 assert_can_disable_lcpll(dev_priv);
8681
8682 val = I915_READ(LCPLL_CTL);
8683
8684 if (switch_to_fclk) {
8685 val |= LCPLL_CD_SOURCE_FCLK;
8686 I915_WRITE(LCPLL_CTL, val);
8687
Imre Deakf53dd632016-06-28 13:37:32 +03008688 if (wait_for_us(I915_READ(LCPLL_CTL) &
8689 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008690 DRM_ERROR("Switching to FCLK failed\n");
8691
8692 val = I915_READ(LCPLL_CTL);
8693 }
8694
8695 val |= LCPLL_PLL_DISABLE;
8696 I915_WRITE(LCPLL_CTL, val);
8697 POSTING_READ(LCPLL_CTL);
8698
Chris Wilson24d84412016-06-30 15:33:07 +01008699 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008700 DRM_ERROR("LCPLL still locked\n");
8701
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008702 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008703 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008704 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008705 ndelay(100);
8706
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008707 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8708 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008709 DRM_ERROR("D_COMP RCOMP still in progress\n");
8710
8711 if (allow_power_down) {
8712 val = I915_READ(LCPLL_CTL);
8713 val |= LCPLL_POWER_DOWN_ALLOW;
8714 I915_WRITE(LCPLL_CTL, val);
8715 POSTING_READ(LCPLL_CTL);
8716 }
8717}
8718
8719/*
8720 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8721 * source.
8722 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008723static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008724{
8725 uint32_t val;
8726
8727 val = I915_READ(LCPLL_CTL);
8728
8729 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8730 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8731 return;
8732
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008733 /*
8734 * Make sure we're not on PC8 state before disabling PC8, otherwise
8735 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008736 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008737 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008738
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008739 if (val & LCPLL_POWER_DOWN_ALLOW) {
8740 val &= ~LCPLL_POWER_DOWN_ALLOW;
8741 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008742 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008743 }
8744
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008745 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008746 val |= D_COMP_COMP_FORCE;
8747 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008748 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008749
8750 val = I915_READ(LCPLL_CTL);
8751 val &= ~LCPLL_PLL_DISABLE;
8752 I915_WRITE(LCPLL_CTL, val);
8753
Chris Wilson93220c02016-06-30 15:33:08 +01008754 if (intel_wait_for_register(dev_priv,
8755 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8756 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008757 DRM_ERROR("LCPLL not locked yet\n");
8758
8759 if (val & LCPLL_CD_SOURCE_FCLK) {
8760 val = I915_READ(LCPLL_CTL);
8761 val &= ~LCPLL_CD_SOURCE_FCLK;
8762 I915_WRITE(LCPLL_CTL, val);
8763
Imre Deakf53dd632016-06-28 13:37:32 +03008764 if (wait_for_us((I915_READ(LCPLL_CTL) &
8765 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008766 DRM_ERROR("Switching back to LCPLL failed\n");
8767 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008768
Mika Kuoppala59bad942015-01-16 11:34:40 +02008769 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjälä4c75b942016-10-31 22:37:12 +02008770 intel_update_cdclk(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008771}
8772
Paulo Zanoni765dab672014-03-07 20:08:18 -03008773/*
8774 * Package states C8 and deeper are really deep PC states that can only be
8775 * reached when all the devices on the system allow it, so even if the graphics
8776 * device allows PC8+, it doesn't mean the system will actually get to these
8777 * states. Our driver only allows PC8+ when going into runtime PM.
8778 *
8779 * The requirements for PC8+ are that all the outputs are disabled, the power
8780 * well is disabled and most interrupts are disabled, and these are also
8781 * requirements for runtime PM. When these conditions are met, we manually do
8782 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8783 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8784 * hang the machine.
8785 *
8786 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8787 * the state of some registers, so when we come back from PC8+ we need to
8788 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8789 * need to take care of the registers kept by RC6. Notice that this happens even
8790 * if we don't put the device in PCI D3 state (which is what currently happens
8791 * because of the runtime PM support).
8792 *
8793 * For more, read "Display Sequences for Package C8" on the hardware
8794 * documentation.
8795 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008796void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008797{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008798 uint32_t val;
8799
Paulo Zanonic67a4702013-08-19 13:18:09 -03008800 DRM_DEBUG_KMS("Enabling package C8+\n");
8801
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008802 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03008803 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8804 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8805 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8806 }
8807
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008808 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008809 hsw_disable_lcpll(dev_priv, true, true);
8810}
8811
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03008812void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03008813{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008814 uint32_t val;
8815
Paulo Zanonic67a4702013-08-19 13:18:09 -03008816 DRM_DEBUG_KMS("Disabling package C8+\n");
8817
8818 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008819 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008820
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008821 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03008822 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8823 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8824 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8825 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03008826}
8827
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008828static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8829 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008830{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03008831 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +02008832 if (!intel_ddi_pll_select(crtc, crtc_state))
8833 return -EINVAL;
8834 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03008835
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008836 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02008837
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008838 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008839}
8840
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308841static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8842 enum port port,
8843 struct intel_crtc_state *pipe_config)
8844{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008845 enum intel_dpll_id id;
8846
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308847 switch (port) {
8848 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +02008849 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308850 break;
8851 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +02008852 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308853 break;
8854 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +02008855 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308856 break;
8857 default:
8858 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008859 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308860 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008861
8862 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05308863}
8864
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008865static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8866 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008867 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008868{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008869 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02008870 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008871
8872 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008873 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008874
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008875 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008876 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008877
8878 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00008879}
8880
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008881static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8882 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008883 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008884{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008885 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008886 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008887
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008888 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008889 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008890 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008891 break;
8892 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008893 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008894 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01008895 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008896 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02008897 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02008898 case PORT_CLK_SEL_LCPLL_810:
8899 id = DPLL_ID_LCPLL_810;
8900 break;
8901 case PORT_CLK_SEL_LCPLL_1350:
8902 id = DPLL_ID_LCPLL_1350;
8903 break;
8904 case PORT_CLK_SEL_LCPLL_2700:
8905 id = DPLL_ID_LCPLL_2700;
8906 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008907 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07008908 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008909 /* fall through */
8910 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008911 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008912 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008913
8914 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01008915}
8916
Jani Nikulacf304292016-03-18 17:05:41 +02008917static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8918 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02008919 u64 *power_domain_mask)
Jani Nikulacf304292016-03-18 17:05:41 +02008920{
8921 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008922 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02008923 enum intel_display_power_domain power_domain;
8924 u32 tmp;
8925
Imre Deakd9a7bc62016-05-12 16:18:50 +03008926 /*
8927 * The pipe->transcoder mapping is fixed with the exception of the eDP
8928 * transcoder handled below.
8929 */
Jani Nikulacf304292016-03-18 17:05:41 +02008930 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8931
8932 /*
8933 * XXX: Do intel_display_power_get_if_enabled before reading this (for
8934 * consistency and less surprising code; it's in always on power).
8935 */
8936 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8937 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8938 enum pipe trans_edp_pipe;
8939 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8940 default:
8941 WARN(1, "unknown pipe linked to edp transcoder\n");
8942 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8943 case TRANS_DDI_EDP_INPUT_A_ON:
8944 trans_edp_pipe = PIPE_A;
8945 break;
8946 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8947 trans_edp_pipe = PIPE_B;
8948 break;
8949 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8950 trans_edp_pipe = PIPE_C;
8951 break;
8952 }
8953
8954 if (trans_edp_pipe == crtc->pipe)
8955 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8956 }
8957
8958 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
8959 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
8960 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02008961 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikulacf304292016-03-18 17:05:41 +02008962
8963 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
8964
8965 return tmp & PIPECONF_ENABLE;
8966}
8967
Jani Nikula4d1de972016-03-18 17:05:42 +02008968static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
8969 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02008970 u64 *power_domain_mask)
Jani Nikula4d1de972016-03-18 17:05:42 +02008971{
8972 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008973 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +02008974 enum intel_display_power_domain power_domain;
8975 enum port port;
8976 enum transcoder cpu_transcoder;
8977 u32 tmp;
8978
Jani Nikula4d1de972016-03-18 17:05:42 +02008979 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
8980 if (port == PORT_A)
8981 cpu_transcoder = TRANSCODER_DSI_A;
8982 else
8983 cpu_transcoder = TRANSCODER_DSI_C;
8984
8985 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
8986 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
8987 continue;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02008988 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikula4d1de972016-03-18 17:05:42 +02008989
Imre Deakdb18b6a2016-03-24 12:41:40 +02008990 /*
8991 * The PLL needs to be enabled with a valid divider
8992 * configuration, otherwise accessing DSI registers will hang
8993 * the machine. See BSpec North Display Engine
8994 * registers/MIPI[BXT]. We can break out here early, since we
8995 * need the same DSI PLL to be enabled for both DSI ports.
8996 */
8997 if (!intel_dsi_pll_is_enabled(dev_priv))
8998 break;
8999
Jani Nikula4d1de972016-03-18 17:05:42 +02009000 /* XXX: this works for video mode only */
9001 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9002 if (!(tmp & DPI_ENABLE))
9003 continue;
9004
9005 tmp = I915_READ(MIPI_CTRL(port));
9006 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9007 continue;
9008
9009 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +02009010 break;
9011 }
9012
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009013 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +02009014}
9015
Daniel Vetter26804af2014-06-25 22:01:55 +03009016static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009017 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009018{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009019 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009020 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009021 enum port port;
9022 uint32_t tmp;
9023
9024 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9025
9026 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9027
Rodrigo Vivib976dc52017-01-23 10:32:37 -08009028 if (IS_GEN9_BC(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009029 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009030 else if (IS_GEN9_LP(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309031 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009032 else
9033 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009034
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009035 pll = pipe_config->shared_dpll;
9036 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009037 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9038 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009039 }
9040
Daniel Vetter26804af2014-06-25 22:01:55 +03009041 /*
9042 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9043 * DDI E. So just check whether this pipe is wired to DDI E and whether
9044 * the PCH transcoder is on.
9045 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009046 if (INTEL_GEN(dev_priv) < 9 &&
Damien Lespiauca370452013-12-03 13:56:24 +00009047 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009048 pipe_config->has_pch_encoder = true;
9049
9050 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9051 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9052 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9053
9054 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9055 }
9056}
9057
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009058static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009059 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009060{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009061 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02009062 enum intel_display_power_domain power_domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009063 u64 power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009064 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009065
Imre Deak17290502016-02-12 18:55:11 +02009066 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9067 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009068 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009069 power_domain_mask = BIT_ULL(power_domain);
Imre Deak17290502016-02-12 18:55:11 +02009070
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009071 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009072
Jani Nikulacf304292016-03-18 17:05:41 +02009073 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009074
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009075 if (IS_GEN9_LP(dev_priv) &&
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009076 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9077 WARN_ON(active);
9078 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +02009079 }
9080
Jani Nikulacf304292016-03-18 17:05:41 +02009081 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009082 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009083
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009084 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +02009085 haswell_get_ddi_port_state(crtc, pipe_config);
9086 intel_get_pipe_timings(crtc, pipe_config);
9087 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009088
Jani Nikulabc58be62016-03-18 17:05:39 +02009089 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009090
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009091 pipe_config->gamma_mode =
9092 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9093
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009094 if (INTEL_GEN(dev_priv) >= 9) {
Nabendu Maiti1c74eea2016-11-29 11:23:14 +05309095 intel_crtc_init_scalers(crtc, pipe_config);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009096
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009097 pipe_config->scaler_state.scaler_id = -1;
9098 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9099 }
9100
Imre Deak17290502016-02-12 18:55:11 +02009101 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9102 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009103 power_domain_mask |= BIT_ULL(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009104 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009105 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009106 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009107 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009108 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009109
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009110 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -08009111 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9112 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009113
Jani Nikula4d1de972016-03-18 17:05:42 +02009114 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9115 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009116 pipe_config->pixel_multiplier =
9117 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9118 } else {
9119 pipe_config->pixel_multiplier = 1;
9120 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009121
Imre Deak17290502016-02-12 18:55:11 +02009122out:
9123 for_each_power_domain(power_domain, power_domain_mask)
9124 intel_display_power_put(dev_priv, power_domain);
9125
Jani Nikulacf304292016-03-18 17:05:41 +02009126 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009127}
9128
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009129static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
9130 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009131{
9132 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009133 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +01009134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009135 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009136
Ville Syrjälä936e71e2016-07-26 19:06:59 +03009137 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009138 unsigned int width = plane_state->base.crtc_w;
9139 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009140 unsigned int stride = roundup_pow_of_two(width) * 4;
9141
9142 switch (stride) {
9143 default:
9144 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9145 width, stride);
9146 stride = 256;
9147 /* fallthrough */
9148 case 256:
9149 case 512:
9150 case 1024:
9151 case 2048:
9152 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009153 }
9154
Ville Syrjälädc41c152014-08-13 11:57:05 +03009155 cntl |= CURSOR_ENABLE |
9156 CURSOR_GAMMA_ENABLE |
9157 CURSOR_FORMAT_ARGB |
9158 CURSOR_STRIDE(stride);
9159
9160 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009161 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009162
Ville Syrjälädc41c152014-08-13 11:57:05 +03009163 if (intel_crtc->cursor_cntl != 0 &&
9164 (intel_crtc->cursor_base != base ||
9165 intel_crtc->cursor_size != size ||
9166 intel_crtc->cursor_cntl != cntl)) {
9167 /* On these chipsets we can only modify the base/size/stride
9168 * whilst the cursor is disabled.
9169 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009170 I915_WRITE(CURCNTR(PIPE_A), 0);
9171 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +03009172 intel_crtc->cursor_cntl = 0;
9173 }
9174
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009175 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009176 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009177 intel_crtc->cursor_base = base;
9178 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009179
9180 if (intel_crtc->cursor_size != size) {
9181 I915_WRITE(CURSIZE, size);
9182 intel_crtc->cursor_size = size;
9183 }
9184
Chris Wilson4b0e3332014-05-30 16:35:26 +03009185 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +03009186 I915_WRITE(CURCNTR(PIPE_A), cntl);
9187 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +03009188 intel_crtc->cursor_cntl = cntl;
9189 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009190}
9191
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009192static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
9193 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009194{
9195 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009196 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +01009197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9198 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +02009199 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009200
Ville Syrjälä936e71e2016-07-26 19:06:59 +03009201 if (plane_state && plane_state->base.visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +03009202 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009203 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309204 case 64:
9205 cntl |= CURSOR_MODE_64_ARGB_AX;
9206 break;
9207 case 128:
9208 cntl |= CURSOR_MODE_128_ARGB_AX;
9209 break;
9210 case 256:
9211 cntl |= CURSOR_MODE_256_ARGB_AX;
9212 break;
9213 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009214 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309215 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009216 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009217 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009218
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009219 if (HAS_DDI(dev_priv))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009220 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009221
Ville Syrjäläf22aa142016-11-14 18:53:58 +02009222 if (plane_state->base.rotation & DRM_ROTATE_180)
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009223 cntl |= CURSOR_ROTATE_180;
9224 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009225
Chris Wilson4b0e3332014-05-30 16:35:26 +03009226 if (intel_crtc->cursor_cntl != cntl) {
9227 I915_WRITE(CURCNTR(pipe), cntl);
9228 POSTING_READ(CURCNTR(pipe));
9229 intel_crtc->cursor_cntl = cntl;
9230 }
9231
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009232 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009233 I915_WRITE(CURBASE(pipe), base);
9234 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009235
9236 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009237}
9238
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009239/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009240static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009241 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009242{
9243 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009244 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9246 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009247 u32 base = intel_crtc->cursor_addr;
9248 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009249
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009250 if (plane_state) {
9251 int x = plane_state->base.crtc_x;
9252 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009253
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009254 if (x < 0) {
9255 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9256 x = -x;
9257 }
9258 pos |= x << CURSOR_X_SHIFT;
9259
9260 if (y < 0) {
9261 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9262 y = -y;
9263 }
9264 pos |= y << CURSOR_Y_SHIFT;
9265
9266 /* ILK+ do this automagically */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01009267 if (HAS_GMCH_DISPLAY(dev_priv) &&
Ville Syrjäläf22aa142016-11-14 18:53:58 +02009268 plane_state->base.rotation & DRM_ROTATE_180) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009269 base += (plane_state->base.crtc_h *
9270 plane_state->base.crtc_w - 1) * 4;
9271 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009272 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009273
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009274 I915_WRITE(CURPOS(pipe), pos);
9275
Jani Nikula2a307c22016-11-30 17:43:04 +02009276 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009277 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009278 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009279 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009280}
9281
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009282static bool cursor_size_ok(struct drm_i915_private *dev_priv,
Ville Syrjälädc41c152014-08-13 11:57:05 +03009283 uint32_t width, uint32_t height)
9284{
9285 if (width == 0 || height == 0)
9286 return false;
9287
9288 /*
9289 * 845g/865g are special in that they are only limited by
9290 * the width of their cursors, the height is arbitrary up to
9291 * the precision of the register. Everything else requires
9292 * square cursors, limited to a few power-of-two sizes.
9293 */
Jani Nikula2a307c22016-11-30 17:43:04 +02009294 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009295 if ((width & 63) != 0)
9296 return false;
9297
Jani Nikula2a307c22016-11-30 17:43:04 +02009298 if (width > (IS_I845G(dev_priv) ? 64 : 512))
Ville Syrjälädc41c152014-08-13 11:57:05 +03009299 return false;
9300
9301 if (height > 1023)
9302 return false;
9303 } else {
9304 switch (width | height) {
9305 case 256:
9306 case 128:
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009307 if (IS_GEN2(dev_priv))
Ville Syrjälädc41c152014-08-13 11:57:05 +03009308 return false;
9309 case 64:
9310 break;
9311 default:
9312 return false;
9313 }
9314 }
9315
9316 return true;
9317}
9318
Jesse Barnes79e53942008-11-07 14:24:08 -08009319/* VESA 640x480x72Hz mode to set on the pipe */
9320static struct drm_display_mode load_detect_mode = {
9321 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9322 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9323};
9324
Daniel Vettera8bb6812014-02-10 18:00:39 +01009325struct drm_framebuffer *
Chris Wilson24dbf512017-02-15 10:59:18 +00009326intel_framebuffer_create(struct drm_i915_gem_object *obj,
9327 struct drm_mode_fb_cmd2 *mode_cmd)
Chris Wilsond2dff872011-04-19 08:36:26 +01009328{
9329 struct intel_framebuffer *intel_fb;
9330 int ret;
9331
9332 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009333 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009334 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +01009335
Chris Wilson24dbf512017-02-15 10:59:18 +00009336 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009337 if (ret)
9338 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009339
9340 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009341
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009342err:
9343 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009344 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009345}
9346
9347static u32
9348intel_framebuffer_pitch_for_width(int width, int bpp)
9349{
9350 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9351 return ALIGN(pitch, 64);
9352}
9353
9354static u32
9355intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9356{
9357 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009358 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009359}
9360
9361static struct drm_framebuffer *
9362intel_framebuffer_create_for_mode(struct drm_device *dev,
9363 struct drm_display_mode *mode,
9364 int depth, int bpp)
9365{
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009366 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009367 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009368 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009369
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00009370 obj = i915_gem_object_create(to_i915(dev),
Chris Wilsond2dff872011-04-19 08:36:26 +01009371 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +01009372 if (IS_ERR(obj))
9373 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009374
9375 mode_cmd.width = mode->hdisplay;
9376 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009377 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9378 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009379 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009380
Chris Wilson24dbf512017-02-15 10:59:18 +00009381 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009382 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +01009383 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009384
9385 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009386}
9387
9388static struct drm_framebuffer *
9389mode_fits_in_fbdev(struct drm_device *dev,
9390 struct drm_display_mode *mode)
9391{
Daniel Vetter06957262015-08-10 13:34:08 +02009392#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +01009393 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +01009394 struct drm_i915_gem_object *obj;
9395 struct drm_framebuffer *fb;
9396
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009397 if (!dev_priv->fbdev)
9398 return NULL;
9399
9400 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009401 return NULL;
9402
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009403 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009404 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009405
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009406 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009407 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
Ville Syrjälä272725c2016-12-14 23:32:20 +02009408 fb->format->cpp[0] * 8))
Chris Wilsond2dff872011-04-19 08:36:26 +01009409 return NULL;
9410
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009411 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009412 return NULL;
9413
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009414 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +01009415 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009416#else
9417 return NULL;
9418#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009419}
9420
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009421static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9422 struct drm_crtc *crtc,
9423 struct drm_display_mode *mode,
9424 struct drm_framebuffer *fb,
9425 int x, int y)
9426{
9427 struct drm_plane_state *plane_state;
9428 int hdisplay, vdisplay;
9429 int ret;
9430
9431 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9432 if (IS_ERR(plane_state))
9433 return PTR_ERR(plane_state);
9434
9435 if (mode)
Daniel Vetter196cd5d2017-01-25 07:26:56 +01009436 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009437 else
9438 hdisplay = vdisplay = 0;
9439
9440 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9441 if (ret)
9442 return ret;
9443 drm_atomic_set_fb_for_plane(plane_state, fb);
9444 plane_state->crtc_x = 0;
9445 plane_state->crtc_y = 0;
9446 plane_state->crtc_w = hdisplay;
9447 plane_state->crtc_h = vdisplay;
9448 plane_state->src_x = x << 16;
9449 plane_state->src_y = y << 16;
9450 plane_state->src_w = hdisplay << 16;
9451 plane_state->src_h = vdisplay << 16;
9452
9453 return 0;
9454}
9455
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009456bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009457 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009458 struct intel_load_detect_pipe *old,
9459 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009460{
9461 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009462 struct intel_encoder *intel_encoder =
9463 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009464 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009465 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009466 struct drm_crtc *crtc = NULL;
9467 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02009468 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94352cf2012-07-05 22:51:56 +02009469 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009470 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009471 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009472 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009473 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009474 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009475
Chris Wilsond2dff872011-04-19 08:36:26 +01009476 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009477 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009478 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009479
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009480 old->restore_state = NULL;
9481
Rob Clark51fd3712013-11-19 12:10:12 -05009482retry:
9483 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9484 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009485 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009486
Jesse Barnes79e53942008-11-07 14:24:08 -08009487 /*
9488 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009489 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009490 * - if the connector already has an assigned crtc, use it (but make
9491 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009492 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009493 * - try to find the first unused crtc that can drive this connector,
9494 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009495 */
9496
9497 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009498 if (connector->state->crtc) {
9499 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009500
Rob Clark51fd3712013-11-19 12:10:12 -05009501 ret = drm_modeset_lock(&crtc->mutex, ctx);
9502 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009503 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +01009504
9505 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009506 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -08009507 }
9508
9509 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009510 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009511 i++;
9512 if (!(encoder->possible_crtcs & (1 << i)))
9513 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009514
9515 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9516 if (ret)
9517 goto fail;
9518
9519 if (possible_crtc->state->enable) {
9520 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +03009521 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009522 }
Ville Syrjäläa4592492014-08-11 13:15:36 +03009523
9524 crtc = possible_crtc;
9525 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009526 }
9527
9528 /*
9529 * If we didn't find an unused CRTC, don't use any.
9530 */
9531 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009532 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009533 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009534 }
9535
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009536found:
9537 intel_crtc = to_intel_crtc(crtc);
9538
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009539 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9540 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009541 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009542
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009543 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009544 restore_state = drm_atomic_state_alloc(dev);
9545 if (!state || !restore_state) {
9546 ret = -ENOMEM;
9547 goto fail;
9548 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009549
9550 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009551 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009552
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009553 connector_state = drm_atomic_get_connector_state(state, connector);
9554 if (IS_ERR(connector_state)) {
9555 ret = PTR_ERR(connector_state);
9556 goto fail;
9557 }
9558
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009559 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9560 if (ret)
9561 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009562
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009563 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9564 if (IS_ERR(crtc_state)) {
9565 ret = PTR_ERR(crtc_state);
9566 goto fail;
9567 }
9568
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009569 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009570
Chris Wilson64927112011-04-20 07:25:26 +01009571 if (!mode)
9572 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009573
Chris Wilsond2dff872011-04-19 08:36:26 +01009574 /* We need a framebuffer large enough to accommodate all accesses
9575 * that the plane may generate whilst we perform load detection.
9576 * We can not rely on the fbcon either being present (we get called
9577 * during its initialisation to detect all boot displays, or it may
9578 * not even exist) or that it is large enough to satisfy the
9579 * requested mode.
9580 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009581 fb = mode_fits_in_fbdev(dev, mode);
9582 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009583 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009584 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +01009585 } else
9586 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009587 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009588 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009589 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009590 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009591
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009592 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9593 if (ret)
9594 goto fail;
9595
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009596 drm_framebuffer_unreference(fb);
9597
9598 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9599 if (ret)
9600 goto fail;
9601
9602 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9603 if (!ret)
9604 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9605 if (!ret)
9606 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9607 if (ret) {
9608 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9609 goto fail;
9610 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +03009611
Maarten Lankhorst3ba86072016-02-29 09:18:57 +01009612 ret = drm_atomic_commit(state);
9613 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +01009614 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009615 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009616 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009617
9618 old->restore_state = restore_state;
Chris Wilson7abbd112017-01-19 11:37:49 +00009619 drm_atomic_state_put(state);
Chris Wilson71731882011-04-19 23:10:58 +01009620
Jesse Barnes79e53942008-11-07 14:24:08 -08009621 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02009622 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009623 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009624
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009625fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +01009626 if (state) {
9627 drm_atomic_state_put(state);
9628 state = NULL;
9629 }
9630 if (restore_state) {
9631 drm_atomic_state_put(restore_state);
9632 restore_state = NULL;
9633 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009634
Rob Clark51fd3712013-11-19 12:10:12 -05009635 if (ret == -EDEADLK) {
9636 drm_modeset_backoff(ctx);
9637 goto retry;
9638 }
9639
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009640 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009641}
9642
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009643void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009644 struct intel_load_detect_pipe *old,
9645 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009646{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009647 struct intel_encoder *intel_encoder =
9648 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009649 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009650 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009651 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009652
Chris Wilsond2dff872011-04-19 08:36:26 +01009653 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009654 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009655 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009656
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009657 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +01009658 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009659
9660 ret = drm_atomic_commit(state);
Chris Wilson08536952016-10-14 13:18:18 +01009661 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009662 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +01009663 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009664}
9665
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009666static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009667 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009668{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009669 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009670 u32 dpll = pipe_config->dpll_hw_state.dpll;
9671
9672 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009673 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009674 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009675 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009676 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009677 return 96000;
9678 else
9679 return 48000;
9680}
9681
Jesse Barnes79e53942008-11-07 14:24:08 -08009682/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009683static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009684 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009685{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009686 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009687 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009688 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009689 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009690 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009691 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +03009692 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009693 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009694
9695 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009696 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009697 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009698 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009699
9700 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009701 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009702 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9703 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009704 } else {
9705 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9706 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9707 }
9708
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009709 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009710 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009711 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9712 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009713 else
9714 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009715 DPLL_FPA01_P1_POST_DIV_SHIFT);
9716
9717 switch (dpll & DPLL_MODE_MASK) {
9718 case DPLLB_MODE_DAC_SERIAL:
9719 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9720 5 : 10;
9721 break;
9722 case DPLLB_MODE_LVDS:
9723 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9724 7 : 14;
9725 break;
9726 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009727 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009728 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009729 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009730 }
9731
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009732 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +03009733 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009734 else
Imre Deakdccbea32015-06-22 23:35:51 +03009735 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009736 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009737 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009738 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009739
9740 if (is_lvds) {
9741 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9742 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009743
9744 if (lvds & LVDS_CLKB_POWER_UP)
9745 clock.p2 = 7;
9746 else
9747 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009748 } else {
9749 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9750 clock.p1 = 2;
9751 else {
9752 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9753 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9754 }
9755 if (dpll & PLL_P2_DIVIDE_BY_4)
9756 clock.p2 = 4;
9757 else
9758 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009759 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009760
Imre Deakdccbea32015-06-22 23:35:51 +03009761 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009762 }
9763
Ville Syrjälä18442d02013-09-13 16:00:08 +03009764 /*
9765 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009766 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009767 * encoder's get_config() function.
9768 */
Imre Deakdccbea32015-06-22 23:35:51 +03009769 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009770}
9771
Ville Syrjälä6878da02013-09-13 15:59:11 +03009772int intel_dotclock_calculate(int link_freq,
9773 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009774{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009775 /*
9776 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009777 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009778 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009779 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009780 *
9781 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009782 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009783 */
9784
Ville Syrjälä6878da02013-09-13 15:59:11 +03009785 if (!m_n->link_n)
9786 return 0;
9787
9788 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9789}
9790
Ville Syrjälä18442d02013-09-13 16:00:08 +03009791static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009792 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009793{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02009794 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +03009795
9796 /* read out port_clock from the DPLL */
9797 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009798
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009799 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02009800 * In case there is an active pipe without active ports,
9801 * we may need some idea for the dotclock anyway.
9802 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009803 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009804 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +02009805 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +03009806 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009807}
9808
9809/** Returns the currently programmed mode of the given pipe. */
9810struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9811 struct drm_crtc *crtc)
9812{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009813 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009815 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009816 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00009817 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009818 int htot = I915_READ(HTOTAL(cpu_transcoder));
9819 int hsync = I915_READ(HSYNC(cpu_transcoder));
9820 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9821 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009822 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009823
9824 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9825 if (!mode)
9826 return NULL;
9827
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00009828 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9829 if (!pipe_config) {
9830 kfree(mode);
9831 return NULL;
9832 }
9833
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009834 /*
9835 * Construct a pipe_config sufficient for getting the clock info
9836 * back out of crtc_clock_get.
9837 *
9838 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9839 * to use a real value here instead.
9840 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00009841 pipe_config->cpu_transcoder = (enum transcoder) pipe;
9842 pipe_config->pixel_multiplier = 1;
9843 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9844 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9845 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
9846 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009847
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00009848 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009849 mode->hdisplay = (htot & 0xffff) + 1;
9850 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9851 mode->hsync_start = (hsync & 0xffff) + 1;
9852 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9853 mode->vdisplay = (vtot & 0xffff) + 1;
9854 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9855 mode->vsync_start = (vsync & 0xffff) + 1;
9856 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9857
9858 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009859
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00009860 kfree(pipe_config);
9861
Jesse Barnes79e53942008-11-07 14:24:08 -08009862 return mode;
9863}
9864
9865static void intel_crtc_destroy(struct drm_crtc *crtc)
9866{
9867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009868 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02009869 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009870
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009871 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +02009872 work = intel_crtc->flip_work;
9873 intel_crtc->flip_work = NULL;
9874 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009875
Daniel Vetter5a21b662016-05-24 17:13:53 +02009876 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02009877 cancel_work_sync(&work->mmio_work);
9878 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02009879 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009880 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009881
9882 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009883
Jesse Barnes79e53942008-11-07 14:24:08 -08009884 kfree(intel_crtc);
9885}
9886
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009887static void intel_unpin_work_fn(struct work_struct *__work)
9888{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02009889 struct intel_flip_work *work =
9890 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02009891 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
9892 struct drm_device *dev = crtc->base.dev;
9893 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009894
Daniel Vetter5a21b662016-05-24 17:13:53 +02009895 if (is_mmio_work(work))
9896 flush_work(&work->mmio_work);
9897
9898 mutex_lock(&dev->struct_mutex);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00009899 intel_unpin_fb_vma(work->old_vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +01009900 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +02009901 mutex_unlock(&dev->struct_mutex);
9902
Chris Wilsone8a261e2016-07-20 13:31:49 +01009903 i915_gem_request_put(work->flip_queued_req);
9904
Chris Wilson5748b6a2016-08-04 16:32:38 +01009905 intel_frontbuffer_flip_complete(to_i915(dev),
9906 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +02009907 intel_fbc_post_update(crtc);
9908 drm_framebuffer_unreference(work->old_fb);
9909
9910 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
9911 atomic_dec(&crtc->unpin_work_count);
9912
9913 kfree(work);
9914}
9915
9916/* Is 'a' after or equal to 'b'? */
9917static bool g4x_flip_count_after_eq(u32 a, u32 b)
9918{
9919 return !((a - b) & 0x80000000);
9920}
9921
9922static bool __pageflip_finished_cs(struct intel_crtc *crtc,
9923 struct intel_flip_work *work)
9924{
9925 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009926 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02009927
Chris Wilson8af29b02016-09-09 14:11:47 +01009928 if (abort_flip_on_reset(crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +02009929 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02009930
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02009931 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +02009932 * The relevant registers doen't exist on pre-ctg.
9933 * As the flip done interrupt doesn't trigger for mmio
9934 * flips on gmch platforms, a flip count check isn't
9935 * really needed there. But since ctg has the registers,
9936 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02009937 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01009938 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +02009939 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02009940
Daniel Vetter5a21b662016-05-24 17:13:53 +02009941 /*
9942 * BDW signals flip done immediately if the plane
9943 * is disabled, even if the plane enable is already
9944 * armed to occur at the next vblank :(
9945 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +02009946
Daniel Vetter5a21b662016-05-24 17:13:53 +02009947 /*
9948 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9949 * used the same base address. In that case the mmio flip might
9950 * have completed, but the CS hasn't even executed the flip yet.
9951 *
9952 * A flip count check isn't enough as the CS might have updated
9953 * the base address just after start of vblank, but before we
9954 * managed to process the interrupt. This means we'd complete the
9955 * CS flip too soon.
9956 *
9957 * Combining both checks should get us a good enough result. It may
9958 * still happen that the CS flip has been executed, but has not
9959 * yet actually completed. But in case the base address is the same
9960 * anyway, we don't really care.
9961 */
9962 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9963 crtc->flip_work->gtt_offset &&
9964 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
9965 crtc->flip_work->flip_count);
9966}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02009967
Daniel Vetter5a21b662016-05-24 17:13:53 +02009968static bool
9969__pageflip_finished_mmio(struct intel_crtc *crtc,
9970 struct intel_flip_work *work)
9971{
9972 /*
9973 * MMIO work completes when vblank is different from
9974 * flip_queued_vblank.
9975 *
9976 * Reset counter value doesn't matter, this is handled by
9977 * i915_wait_request finishing early, so no need to handle
9978 * reset here.
9979 */
9980 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009981}
9982
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02009983
9984static bool pageflip_finished(struct intel_crtc *crtc,
9985 struct intel_flip_work *work)
9986{
9987 if (!atomic_read(&work->pending))
9988 return false;
9989
9990 smp_rmb();
9991
Daniel Vetter5a21b662016-05-24 17:13:53 +02009992 if (is_mmio_work(work))
9993 return __pageflip_finished_mmio(crtc, work);
9994 else
9995 return __pageflip_finished_cs(crtc, work);
9996}
9997
9998void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
9999{
Chris Wilson91c8a322016-07-05 10:40:23 +010010000 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020010001 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010002 struct intel_flip_work *work;
10003 unsigned long flags;
10004
10005 /* Ignore early vblank irqs */
10006 if (!crtc)
10007 return;
10008
Daniel Vetterf3260382014-09-15 14:55:23 +020010009 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010010 * This is called both by irq handlers and the reset code (to complete
10011 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000010012 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020010013 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010014 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010015
10016 if (work != NULL &&
10017 !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010018 pageflip_finished(crtc, work))
10019 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010020
10021 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010022}
10023
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010024void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010025{
Chris Wilson91c8a322016-07-05 10:40:23 +010010026 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020010027 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010028 struct intel_flip_work *work;
10029 unsigned long flags;
10030
10031 /* Ignore early vblank irqs */
10032 if (!crtc)
10033 return;
10034
10035 /*
10036 * This is called both by irq handlers and the reset code (to complete
10037 * lost pageflips) so needs the full irqsave spinlocks.
10038 */
10039 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010040 work = crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010041
Daniel Vetter5a21b662016-05-24 17:13:53 +020010042 if (work != NULL &&
10043 is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010044 pageflip_finished(crtc, work))
10045 page_flip_completed(crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020010046
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010047 spin_unlock_irqrestore(&dev->event_lock, flags);
10048}
10049
Daniel Vetter5a21b662016-05-24 17:13:53 +020010050static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10051 struct intel_flip_work *work)
10052{
10053 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
10054
10055 /* Ensure that the work item is consistent when activating it ... */
10056 smp_mb__before_atomic();
10057 atomic_set(&work->pending, 1);
10058}
10059
10060static int intel_gen2_queue_flip(struct drm_device *dev,
10061 struct drm_crtc *crtc,
10062 struct drm_framebuffer *fb,
10063 struct drm_i915_gem_object *obj,
10064 struct drm_i915_gem_request *req,
10065 uint32_t flags)
10066{
Daniel Vetter5a21b662016-05-24 17:13:53 +020010067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010068 u32 flip_mask, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010069
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010070 cs = intel_ring_begin(req, 6);
10071 if (IS_ERR(cs))
10072 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010073
10074 /* Can't queue multiple flips, so wait for the previous
10075 * one to finish before executing the next.
10076 */
10077 if (intel_crtc->plane)
10078 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10079 else
10080 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010081 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10082 *cs++ = MI_NOOP;
10083 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10084 *cs++ = fb->pitches[0];
10085 *cs++ = intel_crtc->flip_work->gtt_offset;
10086 *cs++ = 0; /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020010087
10088 return 0;
10089}
10090
10091static int intel_gen3_queue_flip(struct drm_device *dev,
10092 struct drm_crtc *crtc,
10093 struct drm_framebuffer *fb,
10094 struct drm_i915_gem_object *obj,
10095 struct drm_i915_gem_request *req,
10096 uint32_t flags)
10097{
Daniel Vetter5a21b662016-05-24 17:13:53 +020010098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010099 u32 flip_mask, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010100
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010101 cs = intel_ring_begin(req, 6);
10102 if (IS_ERR(cs))
10103 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010104
10105 if (intel_crtc->plane)
10106 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10107 else
10108 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010109 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10110 *cs++ = MI_NOOP;
10111 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10112 *cs++ = fb->pitches[0];
10113 *cs++ = intel_crtc->flip_work->gtt_offset;
10114 *cs++ = MI_NOOP;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010115
10116 return 0;
10117}
10118
10119static int intel_gen4_queue_flip(struct drm_device *dev,
10120 struct drm_crtc *crtc,
10121 struct drm_framebuffer *fb,
10122 struct drm_i915_gem_object *obj,
10123 struct drm_i915_gem_request *req,
10124 uint32_t flags)
10125{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010126 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010128 u32 pf, pipesrc, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010129
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010130 cs = intel_ring_begin(req, 4);
10131 if (IS_ERR(cs))
10132 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010133
10134 /* i965+ uses the linear or tiled offsets from the
10135 * Display Registers (which do not change across a page-flip)
10136 * so we need only reprogram the base address.
10137 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010138 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10139 *cs++ = fb->pitches[0];
10140 *cs++ = intel_crtc->flip_work->gtt_offset |
10141 intel_fb_modifier_to_tiling(fb->modifier);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010142
10143 /* XXX Enabling the panel-fitter across page-flip is so far
10144 * untested on non-native modes, so ignore it for now.
10145 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10146 */
10147 pf = 0;
10148 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010149 *cs++ = pf | pipesrc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010150
10151 return 0;
10152}
10153
10154static int intel_gen6_queue_flip(struct drm_device *dev,
10155 struct drm_crtc *crtc,
10156 struct drm_framebuffer *fb,
10157 struct drm_i915_gem_object *obj,
10158 struct drm_i915_gem_request *req,
10159 uint32_t flags)
10160{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010161 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010163 u32 pf, pipesrc, *cs;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010164
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010165 cs = intel_ring_begin(req, 4);
10166 if (IS_ERR(cs))
10167 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010168
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010169 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10170 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10171 *cs++ = intel_crtc->flip_work->gtt_offset;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010172
10173 /* Contrary to the suggestions in the documentation,
10174 * "Enable Panel Fitter" does not seem to be required when page
10175 * flipping with a non-native mode, and worse causes a normal
10176 * modeset to fail.
10177 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10178 */
10179 pf = 0;
10180 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010181 *cs++ = pf | pipesrc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010182
10183 return 0;
10184}
10185
10186static int intel_gen7_queue_flip(struct drm_device *dev,
10187 struct drm_crtc *crtc,
10188 struct drm_framebuffer *fb,
10189 struct drm_i915_gem_object *obj,
10190 struct drm_i915_gem_request *req,
10191 uint32_t flags)
10192{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010193 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010195 u32 *cs, plane_bit = 0;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010196 int len, ret;
10197
10198 switch (intel_crtc->plane) {
10199 case PLANE_A:
10200 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10201 break;
10202 case PLANE_B:
10203 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10204 break;
10205 case PLANE_C:
10206 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10207 break;
10208 default:
10209 WARN_ONCE(1, "unknown plane in flip command\n");
10210 return -ENODEV;
10211 }
10212
10213 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010010214 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010215 len += 6;
10216 /*
10217 * On Gen 8, SRM is now taking an extra dword to accommodate
10218 * 48bits addresses, and we need a NOOP for the batch size to
10219 * stay even.
10220 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010221 if (IS_GEN8(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010222 len += 2;
10223 }
10224
10225 /*
10226 * BSpec MI_DISPLAY_FLIP for IVB:
10227 * "The full packet must be contained within the same cache line."
10228 *
10229 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10230 * cacheline, if we ever start emitting more commands before
10231 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10232 * then do the cacheline alignment, and finally emit the
10233 * MI_DISPLAY_FLIP.
10234 */
10235 ret = intel_ring_cacheline_align(req);
10236 if (ret)
10237 return ret;
10238
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010239 cs = intel_ring_begin(req, len);
10240 if (IS_ERR(cs))
10241 return PTR_ERR(cs);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010242
10243 /* Unmask the flip-done completion message. Note that the bspec says that
10244 * we should do this for both the BCS and RCS, and that we must not unmask
10245 * more than one flip event at any time (or ensure that one flip message
10246 * can be sent by waiting for flip-done prior to queueing new flips).
10247 * Experimentation says that BCS works despite DERRMR masking all
10248 * flip-done completion events and that unmasking all planes at once
10249 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10250 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10251 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010010252 if (req->engine->id == RCS) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010253 *cs++ = MI_LOAD_REGISTER_IMM(1);
10254 *cs++ = i915_mmio_reg_offset(DERRMR);
10255 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10256 DERRMR_PIPEB_PRI_FLIP_DONE |
10257 DERRMR_PIPEC_PRI_FLIP_DONE);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010258 if (IS_GEN8(dev_priv))
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010259 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10260 MI_SRM_LRM_GLOBAL_GTT;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010261 else
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010262 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10263 *cs++ = i915_mmio_reg_offset(DERRMR);
10264 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010265 if (IS_GEN8(dev_priv)) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010266 *cs++ = 0;
10267 *cs++ = MI_NOOP;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010268 }
10269 }
10270
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000010271 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10272 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10273 *cs++ = intel_crtc->flip_work->gtt_offset;
10274 *cs++ = MI_NOOP;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010275
10276 return 0;
10277}
10278
10279static bool use_mmio_flip(struct intel_engine_cs *engine,
10280 struct drm_i915_gem_object *obj)
10281{
10282 /*
10283 * This is not being used for older platforms, because
10284 * non-availability of flip done interrupt forces us to use
10285 * CS flips. Older platforms derive flip done using some clever
10286 * tricks involving the flip_pending status bits and vblank irqs.
10287 * So using MMIO flips there would disrupt this mechanism.
10288 */
10289
10290 if (engine == NULL)
10291 return true;
10292
10293 if (INTEL_GEN(engine->i915) < 5)
10294 return false;
10295
10296 if (i915.use_mmio_flip < 0)
10297 return false;
10298 else if (i915.use_mmio_flip > 0)
10299 return true;
10300 else if (i915.enable_execlists)
10301 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010010302
Chris Wilsond07f0e52016-10-28 13:58:44 +010010303 return engine != i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010304}
10305
10306static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10307 unsigned int rotation,
10308 struct intel_flip_work *work)
10309{
10310 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010311 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010312 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10313 const enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +020010314 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010315
10316 ctl = I915_READ(PLANE_CTL(pipe, 0));
10317 ctl &= ~PLANE_CTL_TILED_MASK;
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010318 switch (fb->modifier) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010319 case DRM_FORMAT_MOD_NONE:
10320 break;
10321 case I915_FORMAT_MOD_X_TILED:
10322 ctl |= PLANE_CTL_TILED_X;
10323 break;
10324 case I915_FORMAT_MOD_Y_TILED:
10325 ctl |= PLANE_CTL_TILED_Y;
10326 break;
10327 case I915_FORMAT_MOD_Yf_TILED:
10328 ctl |= PLANE_CTL_TILED_YF;
10329 break;
10330 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010331 MISSING_CASE(fb->modifier);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010332 }
10333
10334 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010335 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10336 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10337 */
10338 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10339 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10340
10341 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10342 POSTING_READ(PLANE_SURF(pipe, 0));
10343}
10344
10345static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10346 struct intel_flip_work *work)
10347{
10348 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010349 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020010350 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010351 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10352 u32 dspcntr;
10353
10354 dspcntr = I915_READ(reg);
10355
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010356 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010357 dspcntr |= DISPPLANE_TILED;
10358 else
10359 dspcntr &= ~DISPPLANE_TILED;
10360
10361 I915_WRITE(reg, dspcntr);
10362
10363 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10364 POSTING_READ(DSPSURF(intel_crtc->plane));
10365}
10366
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010367static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000010368{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010369 struct intel_flip_work *work =
10370 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010371 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10372 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10373 struct intel_framebuffer *intel_fb =
10374 to_intel_framebuffer(crtc->base.primary->fb);
10375 struct drm_i915_gem_object *obj = intel_fb->obj;
10376
Chris Wilsond07f0e52016-10-28 13:58:44 +010010377 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010378
10379 intel_pipe_update_start(crtc);
10380
10381 if (INTEL_GEN(dev_priv) >= 9)
10382 skl_do_mmio_flip(crtc, work->rotation, work);
10383 else
10384 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10385 ilk_do_mmio_flip(crtc, work);
10386
10387 intel_pipe_update_end(crtc, work);
10388}
10389
10390static int intel_default_queue_flip(struct drm_device *dev,
10391 struct drm_crtc *crtc,
10392 struct drm_framebuffer *fb,
10393 struct drm_i915_gem_object *obj,
10394 struct drm_i915_gem_request *req,
10395 uint32_t flags)
10396{
10397 return -ENODEV;
10398}
10399
10400static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10401 struct intel_crtc *intel_crtc,
10402 struct intel_flip_work *work)
10403{
10404 u32 addr, vblank;
10405
10406 if (!atomic_read(&work->pending))
10407 return false;
10408
10409 smp_rmb();
10410
10411 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10412 if (work->flip_ready_vblank == 0) {
10413 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010010414 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010415 return false;
10416
10417 work->flip_ready_vblank = vblank;
10418 }
10419
10420 if (vblank - work->flip_ready_vblank < 3)
10421 return false;
10422
10423 /* Potential stall - if we see that the flip has happened,
10424 * assume a missed interrupt. */
10425 if (INTEL_GEN(dev_priv) >= 4)
10426 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10427 else
10428 addr = I915_READ(DSPADDR(intel_crtc->plane));
10429
10430 /* There is a potential issue here with a false positive after a flip
10431 * to the same address. We could address this by checking for a
10432 * non-incrementing frame counter.
10433 */
10434 return addr == work->gtt_offset;
10435}
10436
10437void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10438{
Chris Wilson91c8a322016-07-05 10:40:23 +010010439 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020010440 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010441 struct intel_flip_work *work;
10442
10443 WARN_ON(!in_interrupt());
10444
10445 if (crtc == NULL)
10446 return;
10447
10448 spin_lock(&dev->event_lock);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010449 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010450
10451 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010452 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010453 WARN_ONCE(1,
10454 "Kicking stuck page flip: queued at %d, now %d\n",
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010455 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10456 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010457 work = NULL;
10458 }
10459
10460 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020010461 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010462 intel_queue_rps_boost_for_request(work->flip_queued_req);
10463 spin_unlock(&dev->event_lock);
10464}
10465
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010010466__maybe_unused
Daniel Vetter5a21b662016-05-24 17:13:53 +020010467static int intel_crtc_page_flip(struct drm_crtc *crtc,
10468 struct drm_framebuffer *fb,
10469 struct drm_pending_vblank_event *event,
10470 uint32_t page_flip_flags)
10471{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010472 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010473 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010474 struct drm_framebuffer *old_fb = crtc->primary->fb;
10475 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10476 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10477 struct drm_plane *primary = crtc->primary;
10478 enum pipe pipe = intel_crtc->pipe;
10479 struct intel_flip_work *work;
10480 struct intel_engine_cs *engine;
10481 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010010482 struct drm_i915_gem_request *request;
Chris Wilson058d88c2016-08-15 10:49:06 +010010483 struct i915_vma *vma;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010484 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010485
Daniel Vetter5a21b662016-05-24 17:13:53 +020010486 /*
10487 * drm_mode_page_flip_ioctl() should already catch this, but double
10488 * check to be safe. In the future we may enable pageflipping from
10489 * a disabled primary plane.
10490 */
10491 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10492 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010493
Daniel Vetter5a21b662016-05-24 17:13:53 +020010494 /* Can't change pixel format via MI display flips. */
Ville Syrjälädbd4d572016-11-18 21:53:10 +020010495 if (fb->format != crtc->primary->fb->format)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010496 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010497
Daniel Vetter5a21b662016-05-24 17:13:53 +020010498 /*
10499 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10500 * Note that pitch changes could also affect these register.
10501 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010502 if (INTEL_GEN(dev_priv) > 3 &&
Daniel Vetter5a21b662016-05-24 17:13:53 +020010503 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10504 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10505 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010506
Daniel Vetter5a21b662016-05-24 17:13:53 +020010507 if (i915_terminally_wedged(&dev_priv->gpu_error))
10508 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010509
Daniel Vetter5a21b662016-05-24 17:13:53 +020010510 work = kzalloc(sizeof(*work), GFP_KERNEL);
10511 if (work == NULL)
10512 return -ENOMEM;
10513
10514 work->event = event;
10515 work->crtc = crtc;
10516 work->old_fb = old_fb;
10517 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010518
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020010519 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010520 if (ret)
10521 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020010522
Daniel Vetter5a21b662016-05-24 17:13:53 +020010523 /* We borrow the event spin lock for protecting flip_work */
10524 spin_lock_irq(&dev->event_lock);
10525 if (intel_crtc->flip_work) {
10526 /* Before declaring the flip queue wedged, check if
10527 * the hardware completed the operation behind our backs.
10528 */
10529 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10530 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10531 page_flip_completed(intel_crtc);
10532 } else {
10533 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10534 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020010535
Daniel Vetter5a21b662016-05-24 17:13:53 +020010536 drm_crtc_vblank_put(crtc);
10537 kfree(work);
10538 return -EBUSY;
10539 }
10540 }
10541 intel_crtc->flip_work = work;
10542 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080010543
Daniel Vetter5a21b662016-05-24 17:13:53 +020010544 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10545 flush_workqueue(dev_priv->wq);
10546
10547 /* Reference the objects for the scheduled work. */
10548 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010549
10550 crtc->primary->fb = fb;
10551 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020010552
Chris Wilson25dc5562016-07-20 13:31:52 +010010553 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010554
10555 ret = i915_mutex_lock_interruptible(dev);
10556 if (ret)
10557 goto cleanup;
10558
Chris Wilson8af29b02016-09-09 14:11:47 +010010559 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
10560 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010561 ret = -EIO;
Matthew Auldddbb2712016-11-28 10:36:48 +000010562 goto unlock;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010563 }
10564
10565 atomic_inc(&intel_crtc->unpin_work_count);
10566
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010567 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020010568 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10569
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010010570 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053010571 engine = dev_priv->engine[BCS];
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010572 if (fb->modifier != old_fb->modifier)
Daniel Vetter5a21b662016-05-24 17:13:53 +020010573 /* vlv: DISPLAY_FLIP fails to change tiling */
10574 engine = NULL;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010010575 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053010576 engine = dev_priv->engine[BCS];
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010577 } else if (INTEL_GEN(dev_priv) >= 7) {
Chris Wilsond07f0e52016-10-28 13:58:44 +010010578 engine = i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010579 if (engine == NULL || engine->id != RCS)
Akash Goel3b3f1652016-10-13 22:44:48 +053010580 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020010581 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +053010582 engine = dev_priv->engine[RCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020010583 }
10584
10585 mmio_flip = use_mmio_flip(engine, obj);
10586
Chris Wilson058d88c2016-08-15 10:49:06 +010010587 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10588 if (IS_ERR(vma)) {
10589 ret = PTR_ERR(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010590 goto cleanup_pending;
Chris Wilson058d88c2016-08-15 10:49:06 +010010591 }
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010592
Chris Wilsonbe1e3412017-01-16 15:21:27 +000010593 work->old_vma = to_intel_plane_state(primary->state)->vma;
10594 to_intel_plane_state(primary->state)->vma = vma;
10595
10596 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010597 work->rotation = crtc->primary->state->rotation;
10598
Paulo Zanoni1f0613162016-08-17 16:41:44 -030010599 /*
10600 * There's the potential that the next frame will not be compatible with
10601 * FBC, so we want to call pre_update() before the actual page flip.
10602 * The problem is that pre_update() caches some information about the fb
10603 * object, so we want to do this only after the object is pinned. Let's
10604 * be on the safe side and do this immediately before scheduling the
10605 * flip.
10606 */
10607 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10608 to_intel_plane_state(primary->state));
10609
Daniel Vetter5a21b662016-05-24 17:13:53 +020010610 if (mmio_flip) {
10611 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
Imre Deak6277c8d2016-09-20 14:58:19 +030010612 queue_work(system_unbound_wq, &work->mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010613 } else {
Chris Wilsone8a9c582016-12-18 15:37:20 +000010614 request = i915_gem_request_alloc(engine,
10615 dev_priv->kernel_context);
Chris Wilson8e637172016-08-02 22:50:26 +010010616 if (IS_ERR(request)) {
10617 ret = PTR_ERR(request);
10618 goto cleanup_unpin;
10619 }
10620
Chris Wilsona2bc4692016-09-09 14:11:56 +010010621 ret = i915_gem_request_await_object(request, obj, false);
Chris Wilson8e637172016-08-02 22:50:26 +010010622 if (ret)
10623 goto cleanup_request;
10624
Daniel Vetter5a21b662016-05-24 17:13:53 +020010625 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10626 page_flip_flags);
10627 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010010628 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020010629
10630 intel_mark_page_flip_active(intel_crtc, work);
10631
Chris Wilson8e637172016-08-02 22:50:26 +010010632 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010633 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010634 }
10635
Chris Wilson92117f02016-11-28 14:36:48 +000010636 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010637 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10638 to_intel_plane(primary)->frontbuffer_bit);
10639 mutex_unlock(&dev->struct_mutex);
10640
Chris Wilson5748b6a2016-08-04 16:32:38 +010010641 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020010642 to_intel_plane(primary)->frontbuffer_bit);
10643
10644 trace_i915_flip_request(intel_crtc->plane, obj);
10645
10646 return 0;
10647
Chris Wilson8e637172016-08-02 22:50:26 +010010648cleanup_request:
10649 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010650cleanup_unpin:
Chris Wilsonbe1e3412017-01-16 15:21:27 +000010651 to_intel_plane_state(primary->state)->vma = work->old_vma;
10652 intel_unpin_fb_vma(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010653cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020010654 atomic_dec(&intel_crtc->unpin_work_count);
Matthew Auldddbb2712016-11-28 10:36:48 +000010655unlock:
Daniel Vetter5a21b662016-05-24 17:13:53 +020010656 mutex_unlock(&dev->struct_mutex);
10657cleanup:
10658 crtc->primary->fb = old_fb;
10659 update_state_fb(crtc->primary);
10660
Chris Wilsonf0cd5182016-10-28 13:58:43 +010010661 i915_gem_object_put(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010662 drm_framebuffer_unreference(work->old_fb);
10663
10664 spin_lock_irq(&dev->event_lock);
10665 intel_crtc->flip_work = NULL;
10666 spin_unlock_irq(&dev->event_lock);
10667
10668 drm_crtc_vblank_put(crtc);
10669free_work:
10670 kfree(work);
10671
10672 if (ret == -EIO) {
10673 struct drm_atomic_state *state;
10674 struct drm_plane_state *plane_state;
10675
10676out_hang:
10677 state = drm_atomic_state_alloc(dev);
10678 if (!state)
10679 return -ENOMEM;
10680 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
10681
10682retry:
10683 plane_state = drm_atomic_get_plane_state(state, primary);
10684 ret = PTR_ERR_OR_ZERO(plane_state);
10685 if (!ret) {
10686 drm_atomic_set_fb_for_plane(plane_state, fb);
10687
10688 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10689 if (!ret)
10690 ret = drm_atomic_commit(state);
10691 }
10692
10693 if (ret == -EDEADLK) {
10694 drm_modeset_backoff(state->acquire_ctx);
10695 drm_atomic_state_clear(state);
10696 goto retry;
10697 }
10698
Chris Wilson08536952016-10-14 13:18:18 +010010699 drm_atomic_state_put(state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010700
10701 if (ret == 0 && event) {
10702 spin_lock_irq(&dev->event_lock);
10703 drm_crtc_send_vblank_event(crtc, event);
10704 spin_unlock_irq(&dev->event_lock);
10705 }
10706 }
10707 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010708}
10709
Daniel Vetter5a21b662016-05-24 17:13:53 +020010710
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010711/**
10712 * intel_wm_need_update - Check whether watermarks need updating
10713 * @plane: drm plane
10714 * @state: new plane state
10715 *
10716 * Check current plane state versus the new one to determine whether
10717 * watermarks need to be recalculated.
10718 *
10719 * Returns true or false.
10720 */
10721static bool intel_wm_need_update(struct drm_plane *plane,
10722 struct drm_plane_state *state)
10723{
Matt Roperd21fbe82015-09-24 15:53:12 -070010724 struct intel_plane_state *new = to_intel_plane_state(state);
10725 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10726
10727 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010728 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010729 return true;
10730
10731 if (!cur->base.fb || !new->base.fb)
10732 return false;
10733
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010734 if (cur->base.fb->modifier != new->base.fb->modifier ||
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010735 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010736 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10737 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10738 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10739 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010740 return true;
10741
10742 return false;
10743}
10744
Matt Roperd21fbe82015-09-24 15:53:12 -070010745static bool needs_scaling(struct intel_plane_state *state)
10746{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010747 int src_w = drm_rect_width(&state->base.src) >> 16;
10748 int src_h = drm_rect_height(&state->base.src) >> 16;
10749 int dst_w = drm_rect_width(&state->base.dst);
10750 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070010751
10752 return (src_w != dst_w || src_h != dst_h);
10753}
10754
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010755int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
10756 struct drm_plane_state *plane_state)
10757{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010758 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010759 struct drm_crtc *crtc = crtc_state->crtc;
10760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10761 struct drm_plane *plane = plane_state->plane;
10762 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080010763 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010764 struct intel_plane_state *old_plane_state =
10765 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010766 bool mode_changed = needs_modeset(crtc_state);
10767 bool was_crtc_enabled = crtc->state->active;
10768 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010769 bool turn_off, turn_on, visible, was_visible;
10770 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030010771 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010772
Tvrtko Ursulin55b8f2a2016-10-14 09:17:22 +010010773 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010774 ret = skl_update_scaler_plane(
10775 to_intel_crtc_state(crtc_state),
10776 to_intel_plane_state(plane_state));
10777 if (ret)
10778 return ret;
10779 }
10780
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010781 was_visible = old_plane_state->base.visible;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010782 visible = plane_state->visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010783
10784 if (!was_crtc_enabled && WARN_ON(was_visible))
10785 was_visible = false;
10786
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010787 /*
10788 * Visibility is calculated as if the crtc was on, but
10789 * after scaler setup everything depends on it being off
10790 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030010791 *
10792 * FIXME this is wrong for watermarks. Watermarks should also
10793 * be computed as if the pipe would be active. Perhaps move
10794 * per-plane wm computation to the .check_plane() hook, and
10795 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010796 */
10797 if (!is_crtc_enabled)
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010798 plane_state->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010799
10800 if (!was_visible && !visible)
10801 return 0;
10802
Maarten Lankhorste8861672016-02-24 11:24:26 +010010803 if (fb != old_plane_state->base.fb)
10804 pipe_config->fb_changed = true;
10805
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010806 turn_off = was_visible && (!visible || mode_changed);
10807 turn_on = visible && (!was_visible || mode_changed);
10808
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010809 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030010810 intel_crtc->base.base.id,
10811 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010812 plane->base.id, plane->name,
10813 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010814
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010815 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
10816 plane->base.id, plane->name,
10817 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010818 turn_off, turn_on, mode_changed);
10819
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010820 if (turn_on) {
10821 pipe_config->update_wm_pre = true;
10822
10823 /* must disable cxsr around plane enable/disable */
10824 if (plane->type != DRM_PLANE_TYPE_CURSOR)
10825 pipe_config->disable_cxsr = true;
10826 } else if (turn_off) {
10827 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010828
Ville Syrjälä852eb002015-06-24 22:00:07 +030010829 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010010830 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010831 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030010832 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010833 /* FIXME bollocks */
10834 pipe_config->update_wm_pre = true;
10835 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030010836 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010837
Matt Ropered4a6a72016-02-23 17:20:13 -080010838 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010839 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010840 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080010841 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
10842
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070010843 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010010844 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010845
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010846 /*
10847 * WaCxSRDisabledForSpriteScaling:ivb
10848 *
10849 * cstate->update_wm was already set above, so this flag will
10850 * take effect when we commit and program watermarks.
10851 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010010852 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010853 needs_scaling(to_intel_plane_state(plane_state)) &&
10854 !needs_scaling(old_plane_state))
10855 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010856
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010857 return 0;
10858}
10859
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010860static bool encoders_cloneable(const struct intel_encoder *a,
10861 const struct intel_encoder *b)
10862{
10863 /* masks could be asymmetric, so check both ways */
10864 return a == b || (a->cloneable & (1 << b->type) &&
10865 b->cloneable & (1 << a->type));
10866}
10867
10868static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10869 struct intel_crtc *crtc,
10870 struct intel_encoder *encoder)
10871{
10872 struct intel_encoder *source_encoder;
10873 struct drm_connector *connector;
10874 struct drm_connector_state *connector_state;
10875 int i;
10876
10877 for_each_connector_in_state(state, connector, connector_state, i) {
10878 if (connector_state->crtc != &crtc->base)
10879 continue;
10880
10881 source_encoder =
10882 to_intel_encoder(connector_state->best_encoder);
10883 if (!encoders_cloneable(encoder, source_encoder))
10884 return false;
10885 }
10886
10887 return true;
10888}
10889
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010890static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10891 struct drm_crtc_state *crtc_state)
10892{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010893 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010894 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010896 struct intel_crtc_state *pipe_config =
10897 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010898 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020010899 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010900 bool mode_changed = needs_modeset(crtc_state);
10901
Ville Syrjälä852eb002015-06-24 22:00:07 +030010902 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010903 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020010904
Maarten Lankhorstad421372015-06-15 12:33:42 +020010905 if (mode_changed && crtc_state->enable &&
10906 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010907 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020010908 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10909 pipe_config);
10910 if (ret)
10911 return ret;
10912 }
10913
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010914 if (crtc_state->color_mgmt_changed) {
10915 ret = intel_color_check(crtc, crtc_state);
10916 if (ret)
10917 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010010918
10919 /*
10920 * Changing color management on Intel hardware is
10921 * handled as part of planes update.
10922 */
10923 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010924 }
10925
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010926 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010927 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010010928 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080010929 if (ret) {
10930 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070010931 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080010932 }
10933 }
10934
10935 if (dev_priv->display.compute_intermediate_wm &&
10936 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10937 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10938 return 0;
10939
10940 /*
10941 * Calculate 'intermediate' watermarks that satisfy both the
10942 * old state and the new state. We can program these
10943 * immediately.
10944 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010945 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080010946 intel_crtc,
10947 pipe_config);
10948 if (ret) {
10949 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
10950 return ret;
10951 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070010952 } else if (dev_priv->display.compute_intermediate_wm) {
10953 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10954 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010955 }
10956
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010957 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010958 if (mode_changed)
10959 ret = skl_update_scaler_crtc(pipe_config);
10960
10961 if (!ret)
Ander Conselvan de Oliveira6ebc6922017-02-23 09:15:59 +020010962 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010963 pipe_config);
10964 }
10965
10966 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010967}
10968
Jani Nikula65b38e02015-04-13 11:26:56 +030010969static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010970 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020010971 .atomic_begin = intel_begin_crtc_commit,
10972 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010973 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010974};
10975
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010976static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10977{
10978 struct intel_connector *connector;
10979
10980 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020010981 if (connector->base.state->crtc)
10982 drm_connector_unreference(&connector->base);
10983
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010984 if (connector->base.encoder) {
10985 connector->base.state->best_encoder =
10986 connector->base.encoder;
10987 connector->base.state->crtc =
10988 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020010989
10990 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010991 } else {
10992 connector->base.state->best_encoder = NULL;
10993 connector->base.state->crtc = NULL;
10994 }
10995 }
10996}
10997
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010998static void
Robin Schroereba905b2014-05-18 02:24:50 +020010999connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011000 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011001{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011002 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011003 int bpp = pipe_config->pipe_bpp;
11004
11005 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011006 connector->base.base.id,
11007 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011008
11009 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011010 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011011 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011012 bpp, info->bpc * 3);
11013 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011014 }
11015
Mario Kleiner196f9542016-07-06 12:05:45 +020011016 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030011017 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020011018 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11019 bpp);
11020 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011021 }
11022}
11023
11024static int
11025compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011026 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011027{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011028 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011029 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011030 struct drm_connector *connector;
11031 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011032 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011033
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011034 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11035 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011036 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011037 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011038 bpp = 12*3;
11039 else
11040 bpp = 8*3;
11041
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011042
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011043 pipe_config->pipe_bpp = bpp;
11044
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011045 state = pipe_config->base.state;
11046
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011047 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011048 for_each_connector_in_state(state, connector, connector_state, i) {
11049 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011050 continue;
11051
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011052 connected_sink_compute_bpp(to_intel_connector(connector),
11053 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011054 }
11055
11056 return bpp;
11057}
11058
Daniel Vetter644db712013-09-19 14:53:58 +020011059static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11060{
11061 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11062 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011063 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011064 mode->crtc_hdisplay, mode->crtc_hsync_start,
11065 mode->crtc_hsync_end, mode->crtc_htotal,
11066 mode->crtc_vdisplay, mode->crtc_vsync_start,
11067 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11068}
11069
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011070static inline void
11071intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011072 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011073{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011074 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11075 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011076 m_n->gmch_m, m_n->gmch_n,
11077 m_n->link_m, m_n->link_n, m_n->tu);
11078}
11079
Daniel Vetterc0b03412013-05-28 12:05:54 +020011080static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011081 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011082 const char *context)
11083{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011084 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011085 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011086 struct drm_plane *plane;
11087 struct intel_plane *intel_plane;
11088 struct intel_plane_state *state;
11089 struct drm_framebuffer *fb;
11090
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000011091 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11092 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011093
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011094 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11095 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020011096 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011097
11098 if (pipe_config->has_pch_encoder)
11099 intel_dump_m_n_config(pipe_config, "fdi",
11100 pipe_config->fdi_lanes,
11101 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011102
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011103 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011104 intel_dump_m_n_config(pipe_config, "dp m_n",
11105 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000011106 if (pipe_config->has_drrs)
11107 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11108 pipe_config->lane_count,
11109 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011110 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011111
Daniel Vetter55072d12014-11-20 16:10:28 +010011112 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011113 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010011114
Daniel Vetterc0b03412013-05-28 12:05:54 +020011115 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011116 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011117 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011118 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11119 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011120 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011121 pipe_config->port_clock,
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011122 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11123 pipe_config->pixel_rate);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011124
11125 if (INTEL_GEN(dev_priv) >= 9)
11126 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11127 crtc->num_scalers,
11128 pipe_config->scaler_state.scaler_users,
11129 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011130
11131 if (HAS_GMCH_DISPLAY(dev_priv))
11132 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11133 pipe_config->gmch_pfit.control,
11134 pipe_config->gmch_pfit.pgm_ratios,
11135 pipe_config->gmch_pfit.lvds_border_bits);
11136 else
11137 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11138 pipe_config->pch_pfit.pos,
11139 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000011140 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011141
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011142 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11143 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011144
Ander Conselvan de Oliveiraf50b79f2016-12-29 17:22:12 +020011145 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011146
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011147 DRM_DEBUG_KMS("planes on this crtc\n");
11148 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011149 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011150 intel_plane = to_intel_plane(plane);
11151 if (intel_plane->pipe != crtc->pipe)
11152 continue;
11153
11154 state = to_intel_plane_state(plane->state);
11155 fb = state->base.fb;
11156 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030011157 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11158 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011159 continue;
11160 }
11161
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011162 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11163 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011164 fb->base.id, fb->width, fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +020011165 drm_get_format_name(fb->format->format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011166 if (INTEL_GEN(dev_priv) >= 9)
11167 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11168 state->scaler_id,
11169 state->base.src.x1 >> 16,
11170 state->base.src.y1 >> 16,
11171 drm_rect_width(&state->base.src) >> 16,
11172 drm_rect_height(&state->base.src) >> 16,
11173 state->base.dst.x1, state->base.dst.y1,
11174 drm_rect_width(&state->base.dst),
11175 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011176 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011177}
11178
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011179static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011180{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011181 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011182 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011183 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011184 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011185
11186 /*
11187 * Walk the connector list instead of the encoder
11188 * list to detect the problem on ddi platforms
11189 * where there's just one encoder per digital port.
11190 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011191 drm_for_each_connector(connector, dev) {
11192 struct drm_connector_state *connector_state;
11193 struct intel_encoder *encoder;
11194
11195 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11196 if (!connector_state)
11197 connector_state = connector->state;
11198
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011199 if (!connector_state->best_encoder)
11200 continue;
11201
11202 encoder = to_intel_encoder(connector_state->best_encoder);
11203
11204 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011205
11206 switch (encoder->type) {
11207 unsigned int port_mask;
11208 case INTEL_OUTPUT_UNKNOWN:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011209 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011210 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030011211 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011212 case INTEL_OUTPUT_HDMI:
11213 case INTEL_OUTPUT_EDP:
11214 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11215
11216 /* the same port mustn't appear more than once */
11217 if (used_ports & port_mask)
11218 return false;
11219
11220 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011221 break;
11222 case INTEL_OUTPUT_DP_MST:
11223 used_mst_ports |=
11224 1 << enc_to_mst(&encoder->base)->primary->port;
11225 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011226 default:
11227 break;
11228 }
11229 }
11230
Ville Syrjälä477321e2016-07-28 17:50:40 +030011231 /* can't mix MST and SST/HDMI on the same port */
11232 if (used_ports & used_mst_ports)
11233 return false;
11234
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011235 return true;
11236}
11237
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011238static void
11239clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11240{
11241 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011242 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011243 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011244 struct intel_shared_dpll *shared_dpll;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011245 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011246
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011247 /* FIXME: before the switch to atomic started, a new pipe_config was
11248 * kzalloc'd. Code that depends on any field being zero should be
11249 * fixed, so that the crtc_state can be safely duplicated. For now,
11250 * only fields that are know to not cause problems are preserved. */
11251
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011252 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011253 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011254 shared_dpll = crtc_state->shared_dpll;
11255 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011256 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011257
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011258 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011259
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011260 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011261 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011262 crtc_state->shared_dpll = shared_dpll;
11263 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011264 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011265}
11266
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011267static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011268intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011269 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011270{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011271 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020011272 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011273 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011274 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011275 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011276 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011277 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011278
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011279 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011280
Daniel Vettere143a212013-07-04 12:01:15 +020011281 pipe_config->cpu_transcoder =
11282 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011283
Imre Deak2960bc92013-07-30 13:36:32 +030011284 /*
11285 * Sanitize sync polarity flags based on requested ones. If neither
11286 * positive or negative polarity is requested, treat this as meaning
11287 * negative polarity.
11288 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011289 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011290 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011291 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011292
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011293 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011294 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011295 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011296
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011297 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11298 pipe_config);
11299 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011300 goto fail;
11301
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011302 /*
11303 * Determine the real pipe dimensions. Note that stereo modes can
11304 * increase the actual pipe size due to the frame doubling and
11305 * insertion of additional space for blanks between the frame. This
11306 * is stored in the crtc timings. We use the requested mode to do this
11307 * computation to clearly distinguish it from the adjusted mode, which
11308 * can be changed by the connectors in the below retry loop.
11309 */
Daniel Vetter196cd5d2017-01-25 07:26:56 +010011310 drm_mode_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011311 &pipe_config->pipe_src_w,
11312 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011313
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011314 for_each_connector_in_state(state, connector, connector_state, i) {
11315 if (connector_state->crtc != crtc)
11316 continue;
11317
11318 encoder = to_intel_encoder(connector_state->best_encoder);
11319
Ville Syrjäläe25148d2016-06-22 21:57:09 +030011320 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11321 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11322 goto fail;
11323 }
11324
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011325 /*
11326 * Determine output_types before calling the .compute_config()
11327 * hooks so that the hooks can use this information safely.
11328 */
11329 pipe_config->output_types |= 1 << encoder->type;
11330 }
11331
Daniel Vettere29c22c2013-02-21 00:00:16 +010011332encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011333 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011334 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011335 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011336
Daniel Vetter135c81b2013-07-21 21:37:09 +020011337 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011338 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11339 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011340
Daniel Vetter7758a112012-07-08 19:40:39 +020011341 /* Pass our mode to the connectors and the CRTC to give them a chance to
11342 * adjust it according to limitations or connector properties, and also
11343 * a chance to reject the mode entirely.
11344 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011345 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011346 if (connector_state->crtc != crtc)
11347 continue;
11348
11349 encoder = to_intel_encoder(connector_state->best_encoder);
11350
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020011351 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020011352 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011353 goto fail;
11354 }
11355 }
11356
Daniel Vetterff9a6752013-06-01 17:16:21 +020011357 /* Set default port clock if not overwritten by the encoder. Needs to be
11358 * done afterwards in case the encoder adjusts the mode. */
11359 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011360 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011361 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011362
Daniel Vettera43f6e02013-06-07 23:10:32 +020011363 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011364 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011365 DRM_DEBUG_KMS("CRTC fixup failed\n");
11366 goto fail;
11367 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011368
11369 if (ret == RETRY) {
11370 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11371 ret = -EINVAL;
11372 goto fail;
11373 }
11374
11375 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11376 retry = false;
11377 goto encoder_retry;
11378 }
11379
Daniel Vettere8fa4272015-08-12 11:43:34 +020011380 /* Dithering seems to not pass-through bits correctly when it should, so
Manasi Navare611032b2017-01-24 08:21:49 -080011381 * only enable it on 6bpc panels and when its not a compliance
11382 * test requesting 6bpc video pattern.
11383 */
11384 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11385 !pipe_config->dither_force_disable;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020011386 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011387 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011388
Daniel Vetter7758a112012-07-08 19:40:39 +020011389fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011390 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011391}
11392
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011393static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020011394intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011395{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011396 struct drm_crtc *crtc;
11397 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020011398 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011399
Ville Syrjälä76688512014-01-10 11:28:06 +020011400 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020011401 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020011402 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020011403
11404 /* Update hwmode for vblank functions */
11405 if (crtc->state->active)
11406 crtc->hwmode = crtc->state->adjusted_mode;
11407 else
11408 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020011409
11410 /*
11411 * Update legacy state to satisfy fbc code. This can
11412 * be removed when fbc uses the atomic state.
11413 */
11414 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11415 struct drm_plane_state *plane_state = crtc->primary->state;
11416
11417 crtc->primary->fb = plane_state->fb;
11418 crtc->x = plane_state->src_x >> 16;
11419 crtc->y = plane_state->src_y >> 16;
11420 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011421 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011422}
11423
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011424static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011425{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011426 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011427
11428 if (clock1 == clock2)
11429 return true;
11430
11431 if (!clock1 || !clock2)
11432 return false;
11433
11434 diff = abs(clock1 - clock2);
11435
11436 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11437 return true;
11438
11439 return false;
11440}
11441
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011442static bool
11443intel_compare_m_n(unsigned int m, unsigned int n,
11444 unsigned int m2, unsigned int n2,
11445 bool exact)
11446{
11447 if (m == m2 && n == n2)
11448 return true;
11449
11450 if (exact || !m || !n || !m2 || !n2)
11451 return false;
11452
11453 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11454
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011455 if (n > n2) {
11456 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011457 m2 <<= 1;
11458 n2 <<= 1;
11459 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011460 } else if (n < n2) {
11461 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011462 m <<= 1;
11463 n <<= 1;
11464 }
11465 }
11466
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011467 if (n != n2)
11468 return false;
11469
11470 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011471}
11472
11473static bool
11474intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11475 struct intel_link_m_n *m2_n2,
11476 bool adjust)
11477{
11478 if (m_n->tu == m2_n2->tu &&
11479 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11480 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11481 intel_compare_m_n(m_n->link_m, m_n->link_n,
11482 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11483 if (adjust)
11484 *m2_n2 = *m_n;
11485
11486 return true;
11487 }
11488
11489 return false;
11490}
11491
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011492static void __printf(3, 4)
11493pipe_config_err(bool adjust, const char *name, const char *format, ...)
11494{
11495 char *level;
11496 unsigned int category;
11497 struct va_format vaf;
11498 va_list args;
11499
11500 if (adjust) {
11501 level = KERN_DEBUG;
11502 category = DRM_UT_KMS;
11503 } else {
11504 level = KERN_ERR;
11505 category = DRM_UT_NONE;
11506 }
11507
11508 va_start(args, format);
11509 vaf.fmt = format;
11510 vaf.va = &args;
11511
11512 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11513
11514 va_end(args);
11515}
11516
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011517static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011518intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011519 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011520 struct intel_crtc_state *pipe_config,
11521 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011522{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011523 bool ret = true;
11524
Daniel Vetter66e985c2013-06-05 13:34:20 +020011525#define PIPE_CONF_CHECK_X(name) \
11526 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011527 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011528 "(expected 0x%08x, found 0x%08x)\n", \
11529 current_config->name, \
11530 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011531 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011532 }
11533
Daniel Vetter08a24032013-04-19 11:25:34 +020011534#define PIPE_CONF_CHECK_I(name) \
11535 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011536 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter08a24032013-04-19 11:25:34 +020011537 "(expected %i, found %i)\n", \
11538 current_config->name, \
11539 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011540 ret = false; \
11541 }
11542
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011543#define PIPE_CONF_CHECK_P(name) \
11544 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011545 pipe_config_err(adjust, __stringify(name), \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011546 "(expected %p, found %p)\n", \
11547 current_config->name, \
11548 pipe_config->name); \
11549 ret = false; \
11550 }
11551
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011552#define PIPE_CONF_CHECK_M_N(name) \
11553 if (!intel_compare_link_m_n(&current_config->name, \
11554 &pipe_config->name,\
11555 adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011556 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011557 "(expected tu %i gmch %i/%i link %i/%i, " \
11558 "found tu %i, gmch %i/%i link %i/%i)\n", \
11559 current_config->name.tu, \
11560 current_config->name.gmch_m, \
11561 current_config->name.gmch_n, \
11562 current_config->name.link_m, \
11563 current_config->name.link_n, \
11564 pipe_config->name.tu, \
11565 pipe_config->name.gmch_m, \
11566 pipe_config->name.gmch_n, \
11567 pipe_config->name.link_m, \
11568 pipe_config->name.link_n); \
11569 ret = false; \
11570 }
11571
Daniel Vetter55c561a2016-03-30 11:34:36 +020011572/* This is required for BDW+ where there is only one set of registers for
11573 * switching between high and low RR.
11574 * This macro can be used whenever a comparison has to be made between one
11575 * hw state and multiple sw state variables.
11576 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011577#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11578 if (!intel_compare_link_m_n(&current_config->name, \
11579 &pipe_config->name, adjust) && \
11580 !intel_compare_link_m_n(&current_config->alt_name, \
11581 &pipe_config->name, adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011582 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011583 "(expected tu %i gmch %i/%i link %i/%i, " \
11584 "or tu %i gmch %i/%i link %i/%i, " \
11585 "found tu %i, gmch %i/%i link %i/%i)\n", \
11586 current_config->name.tu, \
11587 current_config->name.gmch_m, \
11588 current_config->name.gmch_n, \
11589 current_config->name.link_m, \
11590 current_config->name.link_n, \
11591 current_config->alt_name.tu, \
11592 current_config->alt_name.gmch_m, \
11593 current_config->alt_name.gmch_n, \
11594 current_config->alt_name.link_m, \
11595 current_config->alt_name.link_n, \
11596 pipe_config->name.tu, \
11597 pipe_config->name.gmch_m, \
11598 pipe_config->name.gmch_n, \
11599 pipe_config->name.link_m, \
11600 pipe_config->name.link_n); \
11601 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011602 }
11603
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011604#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11605 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011606 pipe_config_err(adjust, __stringify(name), \
11607 "(%x) (expected %i, found %i)\n", \
11608 (mask), \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011609 current_config->name & (mask), \
11610 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011611 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011612 }
11613
Ville Syrjälä5e550652013-09-06 23:29:07 +030011614#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11615 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011616 pipe_config_err(adjust, __stringify(name), \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011617 "(expected %i, found %i)\n", \
11618 current_config->name, \
11619 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011620 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011621 }
11622
Daniel Vetterbb760062013-06-06 14:55:52 +020011623#define PIPE_CONF_QUIRK(quirk) \
11624 ((current_config->quirks | pipe_config->quirks) & (quirk))
11625
Daniel Vettereccb1402013-05-22 00:50:22 +020011626 PIPE_CONF_CHECK_I(cpu_transcoder);
11627
Daniel Vetter08a24032013-04-19 11:25:34 +020011628 PIPE_CONF_CHECK_I(has_pch_encoder);
11629 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011630 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020011631
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011632 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030011633 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011634
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011635 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011636 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011637
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011638 if (current_config->has_drrs)
11639 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11640 } else
11641 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011642
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011643 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020011644
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11649 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11650 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011651
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11656 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11657 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011658
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011659 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020011660 PIPE_CONF_CHECK_I(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011661 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010011662 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011663 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011664 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011665
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011666 PIPE_CONF_CHECK_I(has_audio);
11667
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011668 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011669 DRM_MODE_FLAG_INTERLACE);
11670
Daniel Vetterbb760062013-06-06 14:55:52 +020011671 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011672 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011673 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011674 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011675 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011676 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011677 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011678 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011679 DRM_MODE_FLAG_NVSYNC);
11680 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011681
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011682 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020011683 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011684 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020011685 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011686 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020011687
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011688 if (!adjust) {
11689 PIPE_CONF_CHECK_I(pipe_src_w);
11690 PIPE_CONF_CHECK_I(pipe_src_h);
11691
11692 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11693 if (current_config->pch_pfit.enabled) {
11694 PIPE_CONF_CHECK_X(pch_pfit.pos);
11695 PIPE_CONF_CHECK_X(pch_pfit.size);
11696 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011697
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011698 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011699 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011700 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070011701
Jesse Barnese59150d2014-01-07 13:30:45 -080011702 /* BDW+ don't expose a synchronous way to read the state */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011703 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080011704 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011705
Ville Syrjälä282740f2013-09-04 18:30:03 +030011706 PIPE_CONF_CHECK_I(double_wide);
11707
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011708 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011709 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011710 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011711 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11712 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011713 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010011714 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011715 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11716 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11717 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011718
Ville Syrjälä47eacba2016-04-12 22:14:35 +030011719 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11720 PIPE_CONF_CHECK_X(dsi_pll.div);
11721
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011722 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011723 PIPE_CONF_CHECK_I(pipe_bpp);
11724
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011725 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011726 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011727
Daniel Vetter66e985c2013-06-05 13:34:20 +020011728#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011729#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011730#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011731#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011732#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011733#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011734
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011735 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011736}
11737
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011738static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11739 const struct intel_crtc_state *pipe_config)
11740{
11741 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011742 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011743 &pipe_config->fdi_m_n);
11744 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11745
11746 /*
11747 * FDI already provided one idea for the dotclock.
11748 * Yell if the encoder disagrees.
11749 */
11750 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11751 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11752 fdi_dotclock, dotclock);
11753 }
11754}
11755
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011756static void verify_wm_state(struct drm_crtc *crtc,
11757 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000011758{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011759 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000011760 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011761 struct skl_pipe_wm hw_wm, *sw_wm;
11762 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11763 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11765 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011766 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000011767
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011768 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000011769 return;
11770
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011771 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020011772 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011773
Damien Lespiau08db6652014-11-04 17:06:52 +000011774 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11775 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11776
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011777 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070011778 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011779 hw_plane_wm = &hw_wm.planes[plane];
11780 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000011781
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011782 /* Watermarks */
11783 for (level = 0; level <= max_level; level++) {
11784 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11785 &sw_plane_wm->wm[level]))
11786 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000011787
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011788 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",
11789 pipe_name(pipe), plane + 1, level,
11790 sw_plane_wm->wm[level].plane_en,
11791 sw_plane_wm->wm[level].plane_res_b,
11792 sw_plane_wm->wm[level].plane_res_l,
11793 hw_plane_wm->wm[level].plane_en,
11794 hw_plane_wm->wm[level].plane_res_b,
11795 hw_plane_wm->wm[level].plane_res_l);
11796 }
11797
11798 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11799 &sw_plane_wm->trans_wm)) {
11800 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",
11801 pipe_name(pipe), plane + 1,
11802 sw_plane_wm->trans_wm.plane_en,
11803 sw_plane_wm->trans_wm.plane_res_b,
11804 sw_plane_wm->trans_wm.plane_res_l,
11805 hw_plane_wm->trans_wm.plane_en,
11806 hw_plane_wm->trans_wm.plane_res_b,
11807 hw_plane_wm->trans_wm.plane_res_l);
11808 }
11809
11810 /* DDB */
11811 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11812 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11813
11814 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011815 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 -040011816 pipe_name(pipe), plane + 1,
11817 sw_ddb_entry->start, sw_ddb_entry->end,
11818 hw_ddb_entry->start, hw_ddb_entry->end);
11819 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011820 }
11821
Lyude27082492016-08-24 07:48:10 +020011822 /*
11823 * cursor
11824 * If the cursor plane isn't active, we may not have updated it's ddb
11825 * allocation. In that case since the ddb allocation will be updated
11826 * once the plane becomes visible, we can skip this check
11827 */
11828 if (intel_crtc->cursor_addr) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011829 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11830 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011831
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011832 /* Watermarks */
11833 for (level = 0; level <= max_level; level++) {
11834 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11835 &sw_plane_wm->wm[level]))
11836 continue;
11837
11838 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",
11839 pipe_name(pipe), level,
11840 sw_plane_wm->wm[level].plane_en,
11841 sw_plane_wm->wm[level].plane_res_b,
11842 sw_plane_wm->wm[level].plane_res_l,
11843 hw_plane_wm->wm[level].plane_en,
11844 hw_plane_wm->wm[level].plane_res_b,
11845 hw_plane_wm->wm[level].plane_res_l);
11846 }
11847
11848 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11849 &sw_plane_wm->trans_wm)) {
11850 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11851 pipe_name(pipe),
11852 sw_plane_wm->trans_wm.plane_en,
11853 sw_plane_wm->trans_wm.plane_res_b,
11854 sw_plane_wm->trans_wm.plane_res_l,
11855 hw_plane_wm->trans_wm.plane_en,
11856 hw_plane_wm->trans_wm.plane_res_b,
11857 hw_plane_wm->trans_wm.plane_res_l);
11858 }
11859
11860 /* DDB */
11861 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11862 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
11863
11864 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011865 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020011866 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011867 sw_ddb_entry->start, sw_ddb_entry->end,
11868 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020011869 }
Damien Lespiau08db6652014-11-04 17:06:52 +000011870 }
11871}
11872
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011873static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011874verify_connector_state(struct drm_device *dev,
11875 struct drm_atomic_state *state,
11876 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011877{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011878 struct drm_connector *connector;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011879 struct drm_connector_state *old_conn_state;
11880 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011881
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011882 for_each_connector_in_state(state, connector, old_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011883 struct drm_encoder *encoder = connector->encoder;
11884 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011885
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011886 if (state->crtc != crtc)
11887 continue;
11888
Daniel Vetter5a21b662016-05-24 17:13:53 +020011889 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011890
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011891 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011892 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011893 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011894}
11895
11896static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011897verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011898{
11899 struct intel_encoder *encoder;
11900 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011901
Damien Lespiaub2784e12014-08-05 11:29:37 +010011902 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011903 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011904 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011905
11906 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11907 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011908 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011909
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011910 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011911 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011912 continue;
11913 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011914
11915 I915_STATE_WARN(connector->base.state->crtc !=
11916 encoder->base.crtc,
11917 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011918 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011919
Rob Clarke2c719b2014-12-15 13:56:32 -050011920 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011921 "encoder's enabled state mismatch "
11922 "(expected %i, found %i)\n",
11923 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011924
11925 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011926 bool active;
11927
11928 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011929 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011930 "encoder detached but still enabled on pipe %c.\n",
11931 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011932 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011933 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011934}
11935
11936static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011937verify_crtc_state(struct drm_crtc *crtc,
11938 struct drm_crtc_state *old_crtc_state,
11939 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011940{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011941 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011942 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011943 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011944 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11945 struct intel_crtc_state *pipe_config, *sw_config;
11946 struct drm_atomic_state *old_state;
11947 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011948
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011949 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020011950 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011951 pipe_config = to_intel_crtc_state(old_crtc_state);
11952 memset(pipe_config, 0, sizeof(*pipe_config));
11953 pipe_config->base.crtc = crtc;
11954 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011955
Ville Syrjälä78108b72016-05-27 20:59:19 +030011956 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011957
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011958 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011959
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011960 /* hw state is inconsistent with the pipe quirk */
11961 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11962 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
11963 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011964
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011965 I915_STATE_WARN(new_crtc_state->active != active,
11966 "crtc active state doesn't match with hw state "
11967 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011968
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011969 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
11970 "transitional active state does not match atomic hw state "
11971 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011972
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011973 for_each_encoder_on_crtc(dev, crtc, encoder) {
11974 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011975
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011976 active = encoder->get_hw_state(encoder, &pipe);
11977 I915_STATE_WARN(active != new_crtc_state->active,
11978 "[ENCODER:%i] active %i with crtc active %i\n",
11979 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011980
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011981 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
11982 "Encoder connected to wrong pipe %c\n",
11983 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011984
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011985 if (active) {
11986 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011987 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011988 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011989 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011990
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011991 intel_crtc_compute_pixel_rate(pipe_config);
11992
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011993 if (!new_crtc_state->active)
11994 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011995
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011996 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011997
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011998 sw_config = to_intel_crtc_state(crtc->state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011999 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012000 pipe_config, false)) {
12001 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12002 intel_dump_pipe_config(intel_crtc, pipe_config,
12003 "[hw state]");
12004 intel_dump_pipe_config(intel_crtc, sw_config,
12005 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012006 }
12007}
12008
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012009static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012010verify_single_dpll_state(struct drm_i915_private *dev_priv,
12011 struct intel_shared_dpll *pll,
12012 struct drm_crtc *crtc,
12013 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012014{
12015 struct intel_dpll_hw_state dpll_hw_state;
12016 unsigned crtc_mask;
12017 bool active;
12018
12019 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12020
12021 DRM_DEBUG_KMS("%s\n", pll->name);
12022
12023 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12024
12025 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12026 I915_STATE_WARN(!pll->on && pll->active_mask,
12027 "pll in active use but not on in sw tracking\n");
12028 I915_STATE_WARN(pll->on && !pll->active_mask,
12029 "pll is on but not used by any active crtc\n");
12030 I915_STATE_WARN(pll->on != active,
12031 "pll on state mismatch (expected %i, found %i)\n",
12032 pll->on, active);
12033 }
12034
12035 if (!crtc) {
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012036 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012037 "more active pll users than references: %x vs %x\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012038 pll->active_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012039
12040 return;
12041 }
12042
12043 crtc_mask = 1 << drm_crtc_index(crtc);
12044
12045 if (new_state->active)
12046 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12047 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12048 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12049 else
12050 I915_STATE_WARN(pll->active_mask & crtc_mask,
12051 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12052 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12053
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012054 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012055 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012056 crtc_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012057
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012058 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012059 &dpll_hw_state,
12060 sizeof(dpll_hw_state)),
12061 "pll hw state mismatch\n");
12062}
12063
12064static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012065verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12066 struct drm_crtc_state *old_crtc_state,
12067 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012068{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012069 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012070 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12071 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12072
12073 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012074 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012075
12076 if (old_state->shared_dpll &&
12077 old_state->shared_dpll != new_state->shared_dpll) {
12078 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12079 struct intel_shared_dpll *pll = old_state->shared_dpll;
12080
12081 I915_STATE_WARN(pll->active_mask & crtc_mask,
12082 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12083 pipe_name(drm_crtc_index(crtc)));
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012084 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012085 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12086 pipe_name(drm_crtc_index(crtc)));
12087 }
12088}
12089
12090static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012091intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012092 struct drm_atomic_state *state,
12093 struct drm_crtc_state *old_state,
12094 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012095{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012096 if (!needs_modeset(new_state) &&
12097 !to_intel_crtc_state(new_state)->update_pipe)
12098 return;
12099
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012100 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012101 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012102 verify_crtc_state(crtc, old_state, new_state);
12103 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012104}
12105
12106static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012107verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012108{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012109 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012110 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012111
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012112 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012113 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012114}
Daniel Vetter53589012013-06-05 13:34:16 +020012115
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012116static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012117intel_modeset_verify_disabled(struct drm_device *dev,
12118 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012119{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012120 verify_encoder_state(dev);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012121 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012122 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020012123}
12124
Ville Syrjälä80715b22014-05-15 20:23:23 +030012125static void update_scanline_offset(struct intel_crtc *crtc)
12126{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012127 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012128
12129 /*
12130 * The scanline counter increments at the leading edge of hsync.
12131 *
12132 * On most platforms it starts counting from vtotal-1 on the
12133 * first active line. That means the scanline counter value is
12134 * always one less than what we would expect. Ie. just after
12135 * start of vblank, which also occurs at start of hsync (on the
12136 * last active line), the scanline counter will read vblank_start-1.
12137 *
12138 * On gen2 the scanline counter starts counting from 1 instead
12139 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12140 * to keep the value positive), instead of adding one.
12141 *
12142 * On HSW+ the behaviour of the scanline counter depends on the output
12143 * type. For DP ports it behaves like most other platforms, but on HDMI
12144 * there's an extra 1 line difference. So we need to add two instead of
12145 * one to the value.
12146 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012147 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012148 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012149 int vtotal;
12150
Ville Syrjälä124abe02015-09-08 13:40:45 +030012151 vtotal = adjusted_mode->crtc_vtotal;
12152 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012153 vtotal /= 2;
12154
12155 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012156 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030012157 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012158 crtc->scanline_offset = 2;
12159 } else
12160 crtc->scanline_offset = 1;
12161}
12162
Maarten Lankhorstad421372015-06-15 12:33:42 +020012163static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012164{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012165 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012166 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012167 struct drm_crtc *crtc;
12168 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012169 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012170
12171 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012172 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012173
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012174 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012176 struct intel_shared_dpll *old_dpll =
12177 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012178
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012179 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012180 continue;
12181
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012182 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012183
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012184 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012185 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012186
Ander Conselvan de Oliveiraa1c414e2016-12-29 17:22:07 +020012187 intel_release_shared_dpll(old_dpll, intel_crtc, state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012188 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012189}
12190
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012191/*
12192 * This implements the workaround described in the "notes" section of the mode
12193 * set sequence documentation. When going from no pipes or single pipe to
12194 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12195 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12196 */
12197static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12198{
12199 struct drm_crtc_state *crtc_state;
12200 struct intel_crtc *intel_crtc;
12201 struct drm_crtc *crtc;
12202 struct intel_crtc_state *first_crtc_state = NULL;
12203 struct intel_crtc_state *other_crtc_state = NULL;
12204 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12205 int i;
12206
12207 /* look at all crtc's that are going to be enabled in during modeset */
12208 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12209 intel_crtc = to_intel_crtc(crtc);
12210
12211 if (!crtc_state->active || !needs_modeset(crtc_state))
12212 continue;
12213
12214 if (first_crtc_state) {
12215 other_crtc_state = to_intel_crtc_state(crtc_state);
12216 break;
12217 } else {
12218 first_crtc_state = to_intel_crtc_state(crtc_state);
12219 first_pipe = intel_crtc->pipe;
12220 }
12221 }
12222
12223 /* No workaround needed? */
12224 if (!first_crtc_state)
12225 return 0;
12226
12227 /* w/a possibly needed, check how many crtc's are already enabled. */
12228 for_each_intel_crtc(state->dev, intel_crtc) {
12229 struct intel_crtc_state *pipe_config;
12230
12231 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12232 if (IS_ERR(pipe_config))
12233 return PTR_ERR(pipe_config);
12234
12235 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12236
12237 if (!pipe_config->base.active ||
12238 needs_modeset(&pipe_config->base))
12239 continue;
12240
12241 /* 2 or more enabled crtcs means no need for w/a */
12242 if (enabled_pipe != INVALID_PIPE)
12243 return 0;
12244
12245 enabled_pipe = intel_crtc->pipe;
12246 }
12247
12248 if (enabled_pipe != INVALID_PIPE)
12249 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12250 else if (other_crtc_state)
12251 other_crtc_state->hsw_workaround_pipe = first_pipe;
12252
12253 return 0;
12254}
12255
Ville Syrjälä8d965612016-11-14 18:35:10 +020012256static int intel_lock_all_pipes(struct drm_atomic_state *state)
12257{
12258 struct drm_crtc *crtc;
12259
12260 /* Add all pipes to the state */
12261 for_each_crtc(state->dev, crtc) {
12262 struct drm_crtc_state *crtc_state;
12263
12264 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12265 if (IS_ERR(crtc_state))
12266 return PTR_ERR(crtc_state);
12267 }
12268
12269 return 0;
12270}
12271
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012272static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12273{
12274 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012275
Ville Syrjälä8d965612016-11-14 18:35:10 +020012276 /*
12277 * Add all pipes to the state, and force
12278 * a modeset on all the active ones.
12279 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012280 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012281 struct drm_crtc_state *crtc_state;
12282 int ret;
12283
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012284 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12285 if (IS_ERR(crtc_state))
12286 return PTR_ERR(crtc_state);
12287
12288 if (!crtc_state->active || needs_modeset(crtc_state))
12289 continue;
12290
12291 crtc_state->mode_changed = true;
12292
12293 ret = drm_atomic_add_affected_connectors(state, crtc);
12294 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012295 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012296
12297 ret = drm_atomic_add_affected_planes(state, crtc);
12298 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012299 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012300 }
12301
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012302 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012303}
12304
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012305static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012306{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012307 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012308 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012309 struct drm_crtc *crtc;
12310 struct drm_crtc_state *crtc_state;
12311 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012312
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012313 if (!check_digital_port_conflicts(state)) {
12314 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12315 return -EINVAL;
12316 }
12317
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012318 intel_state->modeset = true;
12319 intel_state->active_crtcs = dev_priv->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012320 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12321 intel_state->cdclk.actual = dev_priv->cdclk.actual;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012322
12323 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12324 if (crtc_state->active)
12325 intel_state->active_crtcs |= 1 << i;
12326 else
12327 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070012328
12329 if (crtc_state->active != crtc->state->active)
12330 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012331 }
12332
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012333 /*
12334 * See if the config requires any additional preparation, e.g.
12335 * to adjust global state with pipes off. We need to do this
12336 * here so we can get the modeset_pipe updated config for the new
12337 * mode set on this crtc. For other crtcs we need to use the
12338 * adjusted_mode bits in the crtc directly.
12339 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012340 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030012341 ret = dev_priv->display.modeset_calc_cdclk(state);
12342 if (ret < 0)
12343 return ret;
12344
Ville Syrjälä8d965612016-11-14 18:35:10 +020012345 /*
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012346 * Writes to dev_priv->cdclk.logical must protected by
Ville Syrjälä8d965612016-11-14 18:35:10 +020012347 * holding all the crtc locks, even if we don't end up
12348 * touching the hardware
12349 */
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012350 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12351 &intel_state->cdclk.logical)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012352 ret = intel_lock_all_pipes(state);
12353 if (ret < 0)
12354 return ret;
12355 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012356
Ville Syrjälä8d965612016-11-14 18:35:10 +020012357 /* All pipes must be switched off while we change the cdclk. */
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012358 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12359 &intel_state->cdclk.actual)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012360 ret = intel_modeset_all_pipes(state);
12361 if (ret < 0)
12362 return ret;
12363 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010012364
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012365 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12366 intel_state->cdclk.logical.cdclk,
12367 intel_state->cdclk.actual.cdclk);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012368 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012369 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012370 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012371
Maarten Lankhorstad421372015-06-15 12:33:42 +020012372 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012373
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012374 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012375 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012376
Maarten Lankhorstad421372015-06-15 12:33:42 +020012377 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012378}
12379
Matt Roperaa363132015-09-24 15:53:18 -070012380/*
12381 * Handle calculation of various watermark data at the end of the atomic check
12382 * phase. The code here should be run after the per-crtc and per-plane 'check'
12383 * handlers to ensure that all derived state has been updated.
12384 */
Matt Roper55994c22016-05-12 07:06:08 -070012385static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070012386{
12387 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070012388 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070012389
12390 /* Is there platform-specific watermark information to calculate? */
12391 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070012392 return dev_priv->display.compute_global_watermarks(state);
12393
12394 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070012395}
12396
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012397/**
12398 * intel_atomic_check - validate state object
12399 * @dev: drm device
12400 * @state: state to validate
12401 */
12402static int intel_atomic_check(struct drm_device *dev,
12403 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012404{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012405 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070012406 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012407 struct drm_crtc *crtc;
12408 struct drm_crtc_state *crtc_state;
12409 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012410 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012411
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012412 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012413 if (ret)
12414 return ret;
12415
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012416 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012417 struct intel_crtc_state *pipe_config =
12418 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012419
12420 /* Catch I915_MODE_FLAG_INHERITED */
12421 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12422 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012423
Daniel Vetter26495482015-07-15 14:15:52 +020012424 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012425 continue;
12426
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012427 if (!crtc_state->enable) {
12428 any_ms = true;
12429 continue;
12430 }
12431
Daniel Vetter26495482015-07-15 14:15:52 +020012432 /* FIXME: For only active_changed we shouldn't need to do any
12433 * state recomputation at all. */
12434
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012435 ret = drm_atomic_add_affected_connectors(state, crtc);
12436 if (ret)
12437 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012438
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012439 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012440 if (ret) {
12441 intel_dump_pipe_config(to_intel_crtc(crtc),
12442 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012443 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012444 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012445
Jani Nikula73831232015-11-19 10:26:30 +020012446 if (i915.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012447 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012448 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012449 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012450 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012451 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020012452 }
12453
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012454 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020012455 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012456
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012457 ret = drm_atomic_add_affected_planes(state, crtc);
12458 if (ret)
12459 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012460
Daniel Vetter26495482015-07-15 14:15:52 +020012461 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12462 needs_modeset(crtc_state) ?
12463 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012464 }
12465
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012466 if (any_ms) {
12467 ret = intel_modeset_checks(state);
12468
12469 if (ret)
12470 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012471 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012472 intel_state->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012473 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012474
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012475 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070012476 if (ret)
12477 return ret;
12478
Paulo Zanonif51be2e2016-01-19 11:35:50 -020012479 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070012480 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012481}
12482
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012483static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010012484 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012485{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012486 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012487 struct drm_crtc_state *crtc_state;
12488 struct drm_crtc *crtc;
12489 int i, ret;
12490
Daniel Vetter5a21b662016-05-24 17:13:53 +020012491 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12492 if (state->legacy_cursor_update)
12493 continue;
12494
12495 ret = intel_crtc_wait_for_pending_flips(crtc);
12496 if (ret)
12497 return ret;
12498
12499 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12500 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012501 }
12502
Maarten Lankhorstf9356752015-08-18 13:40:05 +020012503 ret = mutex_lock_interruptible(&dev->struct_mutex);
12504 if (ret)
12505 return ret;
12506
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012507 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010012508 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012509
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012510 return ret;
12511}
12512
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012513u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12514{
12515 struct drm_device *dev = crtc->base.dev;
12516
12517 if (!dev->max_vblank_count)
12518 return drm_accurate_vblank_count(&crtc->base);
12519
12520 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12521}
12522
Daniel Vetter5a21b662016-05-24 17:13:53 +020012523static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12524 struct drm_i915_private *dev_priv,
12525 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010012526{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012527 unsigned last_vblank_count[I915_MAX_PIPES];
12528 enum pipe pipe;
12529 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012530
Daniel Vetter5a21b662016-05-24 17:13:53 +020012531 if (!crtc_mask)
12532 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012533
Daniel Vetter5a21b662016-05-24 17:13:53 +020012534 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020012535 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12536 pipe);
Maarten Lankhorste8861672016-02-24 11:24:26 +010012537
Daniel Vetter5a21b662016-05-24 17:13:53 +020012538 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010012539 continue;
12540
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012541 ret = drm_crtc_vblank_get(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012542 if (WARN_ON(ret != 0)) {
12543 crtc_mask &= ~(1 << pipe);
12544 continue;
12545 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012546
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012547 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012548 }
12549
12550 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020012551 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12552 pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012553 long lret;
12554
12555 if (!((1 << pipe) & crtc_mask))
12556 continue;
12557
12558 lret = wait_event_timeout(dev->vblank[pipe].queue,
12559 last_vblank_count[pipe] !=
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012560 drm_crtc_vblank_count(&crtc->base),
Daniel Vetter5a21b662016-05-24 17:13:53 +020012561 msecs_to_jiffies(50));
12562
12563 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
12564
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012565 drm_crtc_vblank_put(&crtc->base);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012566 }
12567}
12568
Daniel Vetter5a21b662016-05-24 17:13:53 +020012569static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012570{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012571 /* fb updated, need to unpin old fb */
12572 if (crtc_state->fb_changed)
12573 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012574
Daniel Vetter5a21b662016-05-24 17:13:53 +020012575 /* wm changes, need vblank before final wm's */
12576 if (crtc_state->update_wm_post)
12577 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012578
Daniel Vetter5a21b662016-05-24 17:13:53 +020012579 /*
12580 * cxsr is re-enabled after vblank.
12581 * This is already handled by crtc_state->update_wm_post,
12582 * but added for clarity.
12583 */
12584 if (crtc_state->disable_cxsr)
12585 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012586
Daniel Vetter5a21b662016-05-24 17:13:53 +020012587 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010012588}
12589
Lyude896e5bb2016-08-24 07:48:09 +020012590static void intel_update_crtc(struct drm_crtc *crtc,
12591 struct drm_atomic_state *state,
12592 struct drm_crtc_state *old_crtc_state,
12593 unsigned int *crtc_vblank_mask)
12594{
12595 struct drm_device *dev = crtc->dev;
12596 struct drm_i915_private *dev_priv = to_i915(dev);
12597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12598 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
12599 bool modeset = needs_modeset(crtc->state);
12600
12601 if (modeset) {
12602 update_scanline_offset(intel_crtc);
12603 dev_priv->display.crtc_enable(pipe_config, state);
12604 } else {
12605 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
12606 }
12607
12608 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12609 intel_fbc_enable(
12610 intel_crtc, pipe_config,
12611 to_intel_plane_state(crtc->primary->state));
12612 }
12613
12614 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12615
12616 if (needs_vblank_wait(pipe_config))
12617 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12618}
12619
12620static void intel_update_crtcs(struct drm_atomic_state *state,
12621 unsigned int *crtc_vblank_mask)
12622{
12623 struct drm_crtc *crtc;
12624 struct drm_crtc_state *old_crtc_state;
12625 int i;
12626
12627 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12628 if (!crtc->state->active)
12629 continue;
12630
12631 intel_update_crtc(crtc, state, old_crtc_state,
12632 crtc_vblank_mask);
12633 }
12634}
12635
Lyude27082492016-08-24 07:48:10 +020012636static void skl_update_crtcs(struct drm_atomic_state *state,
12637 unsigned int *crtc_vblank_mask)
12638{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012639 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020012640 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12641 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040012642 struct intel_crtc *intel_crtc;
Lyude27082492016-08-24 07:48:10 +020012643 struct drm_crtc_state *old_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040012644 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020012645 unsigned int updated = 0;
12646 bool progress;
12647 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012648 int i;
12649
12650 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12651
12652 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
12653 /* ignore allocations for crtc's that have been turned off. */
12654 if (crtc->state->active)
12655 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012656
12657 /*
12658 * Whenever the number of active pipes changes, we need to make sure we
12659 * update the pipes in the right order so that their ddb allocations
12660 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12661 * cause pipe underruns and other bad stuff.
12662 */
12663 do {
Lyude27082492016-08-24 07:48:10 +020012664 progress = false;
12665
12666 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12667 bool vbl_wait = false;
12668 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040012669
12670 intel_crtc = to_intel_crtc(crtc);
12671 cstate = to_intel_crtc_state(crtc->state);
12672 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020012673
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012674 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020012675 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012676
12677 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
Lyude27082492016-08-24 07:48:10 +020012678 continue;
12679
12680 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012681 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012682
12683 /*
12684 * If this is an already active pipe, it's DDB changed,
12685 * and this isn't the last pipe that needs updating
12686 * then we need to wait for a vblank to pass for the
12687 * new ddb allocation to take effect.
12688 */
Lyudece0ba282016-09-15 10:46:35 -040012689 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010012690 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Lyude27082492016-08-24 07:48:10 +020012691 !crtc->state->active_changed &&
12692 intel_state->wm_results.dirty_pipes != updated)
12693 vbl_wait = true;
12694
12695 intel_update_crtc(crtc, state, old_crtc_state,
12696 crtc_vblank_mask);
12697
12698 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012699 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020012700
12701 progress = true;
12702 }
12703 } while (progress);
12704}
12705
Chris Wilsonba318c62017-02-02 20:47:41 +000012706static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12707{
12708 struct intel_atomic_state *state, *next;
12709 struct llist_node *freed;
12710
12711 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12712 llist_for_each_entry_safe(state, next, freed, freed)
12713 drm_atomic_state_put(&state->base);
12714}
12715
12716static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12717{
12718 struct drm_i915_private *dev_priv =
12719 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12720
12721 intel_atomic_helper_free_state(dev_priv);
12722}
12723
Daniel Vetter94f05022016-06-14 18:01:00 +020012724static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012725{
Daniel Vetter94f05022016-06-14 18:01:00 +020012726 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012727 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012728 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012729 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012730 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012731 struct intel_crtc_state *intel_cstate;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012732 bool hw_check = intel_state->modeset;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020012733 u64 put_domains[I915_MAX_PIPES] = {};
Daniel Vetter5a21b662016-05-24 17:13:53 +020012734 unsigned crtc_vblank_mask = 0;
Chris Wilsone95433c2016-10-28 13:58:27 +010012735 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012736
Daniel Vetterea0000f2016-06-13 16:13:46 +020012737 drm_atomic_helper_wait_for_dependencies(state);
12738
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012739 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012740 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012741
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012742 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12744
Daniel Vetter5a21b662016-05-24 17:13:53 +020012745 if (needs_modeset(crtc->state) ||
12746 to_intel_crtc_state(crtc->state)->update_pipe) {
12747 hw_check = true;
12748
12749 put_domains[to_intel_crtc(crtc)->pipe] =
12750 modeset_get_crtc_power_domains(crtc,
12751 to_intel_crtc_state(crtc->state));
12752 }
12753
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012754 if (!needs_modeset(crtc->state))
12755 continue;
12756
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012757 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010012758
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012759 if (old_crtc_state->active) {
12760 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorst4a806552016-08-09 17:04:01 +020012761 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012762 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020012763 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012764 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020012765
12766 /*
12767 * Underruns don't always raise
12768 * interrupts, so check manually.
12769 */
12770 intel_check_cpu_fifo_underruns(dev_priv);
12771 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010012772
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012773 if (!crtc->state->active) {
12774 /*
12775 * Make sure we don't call initial_watermarks
12776 * for ILK-style watermark updates.
12777 */
12778 if (dev_priv->display.atomic_update_watermarks)
12779 dev_priv->display.initial_watermarks(intel_state,
12780 to_intel_crtc_state(crtc->state));
12781 else
12782 intel_update_watermarks(intel_crtc);
12783 }
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012784 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012785 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012786
Daniel Vetterea9d7582012-07-10 10:42:52 +020012787 /* Only after disabling all output pipelines that will be changed can we
12788 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012789 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012790
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012791 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012792 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010012793
Ville Syrjäläb0587e42017-01-26 21:52:01 +020012794 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010012795
Lyude656d1b82016-08-17 15:55:54 -040012796 /*
12797 * SKL workaround: bspec recommends we disable the SAGV when we
12798 * have more then one pipe enabled
12799 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030012800 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012801 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012802
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012803 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012804 }
Daniel Vetter47fab732012-10-26 10:58:18 +020012805
Lyude896e5bb2016-08-24 07:48:09 +020012806 /* Complete the events for pipes that have now been disabled */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012807 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020012808 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012809
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012810 /* Complete events for now disable pipes here. */
12811 if (modeset && !crtc->state->active && crtc->state->event) {
12812 spin_lock_irq(&dev->event_lock);
12813 drm_crtc_send_vblank_event(crtc, crtc->state->event);
12814 spin_unlock_irq(&dev->event_lock);
12815
12816 crtc->state->event = NULL;
12817 }
Matt Ropered4a6a72016-02-23 17:20:13 -080012818 }
12819
Lyude896e5bb2016-08-24 07:48:09 +020012820 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
12821 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
12822
Daniel Vetter94f05022016-06-14 18:01:00 +020012823 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12824 * already, but still need the state for the delayed optimization. To
12825 * fix this:
12826 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12827 * - schedule that vblank worker _before_ calling hw_done
12828 * - at the start of commit_tail, cancel it _synchrously
12829 * - switch over to the vblank wait helper in the core after that since
12830 * we don't need out special handling any more.
12831 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020012832 if (!state->legacy_cursor_update)
12833 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
12834
12835 /*
12836 * Now that the vblank has passed, we can go ahead and program the
12837 * optimal watermarks on platforms that need two-step watermark
12838 * programming.
12839 *
12840 * TODO: Move this (and other cleanup) to an async worker eventually.
12841 */
12842 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12843 intel_cstate = to_intel_crtc_state(crtc->state);
12844
12845 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010012846 dev_priv->display.optimize_watermarks(intel_state,
12847 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012848 }
12849
12850 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
12851 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12852
12853 if (put_domains[i])
12854 modeset_put_power_domains(dev_priv, put_domains[i]);
12855
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012856 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012857 }
12858
Paulo Zanoni56feca92016-09-22 18:00:28 -030012859 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012860 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012861
Daniel Vetter94f05022016-06-14 18:01:00 +020012862 drm_atomic_helper_commit_hw_done(state);
12863
Daniel Vetter5a21b662016-05-24 17:13:53 +020012864 if (intel_state->modeset)
12865 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
12866
12867 mutex_lock(&dev->struct_mutex);
12868 drm_atomic_helper_cleanup_planes(dev, state);
12869 mutex_unlock(&dev->struct_mutex);
12870
Daniel Vetterea0000f2016-06-13 16:13:46 +020012871 drm_atomic_helper_commit_cleanup_done(state);
12872
Chris Wilson08536952016-10-14 13:18:18 +010012873 drm_atomic_state_put(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012874
Mika Kuoppala75714942015-12-16 09:26:48 +020012875 /* As one of the primary mmio accessors, KMS has a high likelihood
12876 * of triggering bugs in unclaimed access. After we finish
12877 * modesetting, see if an error has been flagged, and if so
12878 * enable debugging for the next modeset - and hope we catch
12879 * the culprit.
12880 *
12881 * XXX note that we assume display power is on at this point.
12882 * This might hold true now but we need to add pm helper to check
12883 * unclaimed only when the hardware is on, as atomic commits
12884 * can happen also when the device is completely off.
12885 */
12886 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Chris Wilsonba318c62017-02-02 20:47:41 +000012887
12888 intel_atomic_helper_free_state(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020012889}
12890
12891static void intel_atomic_commit_work(struct work_struct *work)
12892{
Chris Wilsonc004a902016-10-28 13:58:45 +010012893 struct drm_atomic_state *state =
12894 container_of(work, struct drm_atomic_state, commit_work);
12895
Daniel Vetter94f05022016-06-14 18:01:00 +020012896 intel_atomic_commit_tail(state);
12897}
12898
Chris Wilsonc004a902016-10-28 13:58:45 +010012899static int __i915_sw_fence_call
12900intel_atomic_commit_ready(struct i915_sw_fence *fence,
12901 enum i915_sw_fence_notify notify)
12902{
12903 struct intel_atomic_state *state =
12904 container_of(fence, struct intel_atomic_state, commit_ready);
12905
12906 switch (notify) {
12907 case FENCE_COMPLETE:
12908 if (state->base.commit_work.func)
12909 queue_work(system_unbound_wq, &state->base.commit_work);
12910 break;
12911
12912 case FENCE_FREE:
Chris Wilsoneb955ee2017-01-23 21:29:39 +000012913 {
12914 struct intel_atomic_helper *helper =
12915 &to_i915(state->base.dev)->atomic_helper;
12916
12917 if (llist_add(&state->freed, &helper->free_list))
12918 schedule_work(&helper->free_work);
12919 break;
12920 }
Chris Wilsonc004a902016-10-28 13:58:45 +010012921 }
12922
12923 return NOTIFY_DONE;
12924}
12925
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012926static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12927{
12928 struct drm_plane_state *old_plane_state;
12929 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012930 int i;
12931
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012932 for_each_plane_in_state(state, plane, old_plane_state, i)
12933 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
12934 intel_fb_obj(plane->state->fb),
12935 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012936}
12937
Daniel Vetter94f05022016-06-14 18:01:00 +020012938/**
12939 * intel_atomic_commit - commit validated state object
12940 * @dev: DRM device
12941 * @state: the top-level driver state object
12942 * @nonblock: nonblocking commit
12943 *
12944 * This function commits a top-level state object that has been validated
12945 * with drm_atomic_helper_check().
12946 *
Daniel Vetter94f05022016-06-14 18:01:00 +020012947 * RETURNS
12948 * Zero for success or -errno.
12949 */
12950static int intel_atomic_commit(struct drm_device *dev,
12951 struct drm_atomic_state *state,
12952 bool nonblock)
12953{
12954 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012955 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020012956 int ret = 0;
12957
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020012958 /*
12959 * The intel_legacy_cursor_update() fast path takes care
12960 * of avoiding the vblank waits for simple cursor
12961 * movement and flips. For cursor on/off and size changes,
12962 * we want to perform the vblank waits so that watermark
12963 * updates happen during the correct frames. Gen9+ have
12964 * double buffered watermarks and so shouldn't need this.
12965 */
12966 if (INTEL_GEN(dev_priv) < 9)
12967 state->legacy_cursor_update = false;
12968
Daniel Vetter94f05022016-06-14 18:01:00 +020012969 ret = drm_atomic_helper_setup_commit(state, nonblock);
12970 if (ret)
12971 return ret;
12972
Chris Wilsonc004a902016-10-28 13:58:45 +010012973 drm_atomic_state_get(state);
12974 i915_sw_fence_init(&intel_state->commit_ready,
12975 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020012976
Chris Wilsond07f0e52016-10-28 13:58:44 +010012977 ret = intel_atomic_prepare_commit(dev, state);
Daniel Vetter94f05022016-06-14 18:01:00 +020012978 if (ret) {
12979 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Chris Wilsonc004a902016-10-28 13:58:45 +010012980 i915_sw_fence_commit(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020012981 return ret;
12982 }
12983
12984 drm_atomic_helper_swap_state(state, true);
12985 dev_priv->wm.distrust_bios_wm = false;
Ander Conselvan de Oliveira3c0fb582016-12-29 17:22:08 +020012986 intel_shared_dpll_swap_state(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012987 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020012988
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012989 if (intel_state->modeset) {
12990 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
12991 sizeof(intel_state->min_pixclk));
12992 dev_priv->active_crtcs = intel_state->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012993 dev_priv->cdclk.logical = intel_state->cdclk.logical;
12994 dev_priv->cdclk.actual = intel_state->cdclk.actual;
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012995 }
12996
Chris Wilson08536952016-10-14 13:18:18 +010012997 drm_atomic_state_get(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010012998 INIT_WORK(&state->commit_work,
12999 nonblock ? intel_atomic_commit_work : NULL);
13000
13001 i915_sw_fence_commit(&intel_state->commit_ready);
13002 if (!nonblock) {
13003 i915_sw_fence_wait(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020013004 intel_atomic_commit_tail(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010013005 }
Mika Kuoppala75714942015-12-16 09:26:48 +020013006
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013007 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013008}
13009
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013010void intel_crtc_restore_mode(struct drm_crtc *crtc)
13011{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013012 struct drm_device *dev = crtc->dev;
13013 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013014 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013015 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013016
13017 state = drm_atomic_state_alloc(dev);
13018 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013019 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13020 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013021 return;
13022 }
13023
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013024 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013025
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013026retry:
13027 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13028 ret = PTR_ERR_OR_ZERO(crtc_state);
13029 if (!ret) {
13030 if (!crtc_state->active)
13031 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013032
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013033 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013034 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013035 }
13036
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013037 if (ret == -EDEADLK) {
13038 drm_atomic_state_clear(state);
13039 drm_modeset_backoff(state->acquire_ctx);
13040 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013041 }
13042
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013043out:
Chris Wilson08536952016-10-14 13:18:18 +010013044 drm_atomic_state_put(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013045}
13046
Bob Paauwea8784872016-07-15 14:59:02 +010013047/*
13048 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13049 * drm_atomic_helper_legacy_gamma_set() directly.
13050 */
13051static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13052 u16 *red, u16 *green, u16 *blue,
13053 uint32_t size)
13054{
13055 struct drm_device *dev = crtc->dev;
13056 struct drm_mode_config *config = &dev->mode_config;
13057 struct drm_crtc_state *state;
13058 int ret;
13059
13060 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13061 if (ret)
13062 return ret;
13063
13064 /*
13065 * Make sure we update the legacy properties so this works when
13066 * atomic is not enabled.
13067 */
13068
13069 state = crtc->state;
13070
13071 drm_object_property_set_value(&crtc->base,
13072 config->degamma_lut_property,
13073 (state->degamma_lut) ?
13074 state->degamma_lut->base.id : 0);
13075
13076 drm_object_property_set_value(&crtc->base,
13077 config->ctm_property,
13078 (state->ctm) ?
13079 state->ctm->base.id : 0);
13080
13081 drm_object_property_set_value(&crtc->base,
13082 config->gamma_lut_property,
13083 (state->gamma_lut) ?
13084 state->gamma_lut->base.id : 0);
13085
13086 return 0;
13087}
13088
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013089static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010013090 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013091 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013092 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013093 .destroy = intel_crtc_destroy,
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010013094 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013095 .atomic_duplicate_state = intel_crtc_duplicate_state,
13096 .atomic_destroy_state = intel_crtc_destroy_state,
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +010013097 .set_crc_source = intel_crtc_set_crc_source,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013098};
13099
Matt Roper6beb8c232014-12-01 15:40:14 -080013100/**
13101 * intel_prepare_plane_fb - Prepare fb for usage on plane
13102 * @plane: drm plane to prepare for
13103 * @fb: framebuffer to prepare for presentation
13104 *
13105 * Prepares a framebuffer for usage on a display plane. Generally this
13106 * involves pinning the underlying object and updating the frontbuffer tracking
13107 * bits. Some older platforms need special physical address handling for
13108 * cursor planes.
13109 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013110 * Must be called with struct_mutex held.
13111 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013112 * Returns 0 on success, negative error code on failure.
13113 */
13114int
13115intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013116 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013117{
Chris Wilsonc004a902016-10-28 13:58:45 +010013118 struct intel_atomic_state *intel_state =
13119 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000013120 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013121 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013122 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013123 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010013124 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013125
Chris Wilson57822dc2017-02-22 11:40:48 +000013126 if (obj) {
13127 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13128 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13129 const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13130
13131 ret = i915_gem_object_attach_phys(obj, align);
13132 if (ret) {
13133 DRM_DEBUG_KMS("failed to attach phys object\n");
13134 return ret;
13135 }
13136 } else {
13137 struct i915_vma *vma;
13138
13139 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13140 if (IS_ERR(vma)) {
13141 DRM_DEBUG_KMS("failed to pin object\n");
13142 return PTR_ERR(vma);
13143 }
13144
13145 to_intel_plane_state(new_state)->vma = vma;
13146 }
13147 }
13148
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013149 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013150 return 0;
13151
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013152 if (old_obj) {
13153 struct drm_crtc_state *crtc_state =
Chris Wilsonc004a902016-10-28 13:58:45 +010013154 drm_atomic_get_existing_crtc_state(new_state->state,
13155 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013156
13157 /* Big Hammer, we also need to ensure that any pending
13158 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13159 * current scanout is retired before unpinning the old
13160 * framebuffer. Note that we rely on userspace rendering
13161 * into the buffer attached to the pipe they are waiting
13162 * on. If not, userspace generates a GPU hang with IPEHR
13163 * point to the MI_WAIT_FOR_EVENT.
13164 *
13165 * This should only fail upon a hung GPU, in which case we
13166 * can safely continue.
13167 */
Chris Wilsonc004a902016-10-28 13:58:45 +010013168 if (needs_modeset(crtc_state)) {
13169 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13170 old_obj->resv, NULL,
13171 false, 0,
13172 GFP_KERNEL);
13173 if (ret < 0)
13174 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013175 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013176 }
13177
Chris Wilsonc004a902016-10-28 13:58:45 +010013178 if (new_state->fence) { /* explicit fencing */
13179 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13180 new_state->fence,
13181 I915_FENCE_TIMEOUT,
13182 GFP_KERNEL);
13183 if (ret < 0)
13184 return ret;
13185 }
13186
Chris Wilsonc37efb92016-06-17 08:28:47 +010013187 if (!obj)
13188 return 0;
13189
Chris Wilsonc004a902016-10-28 13:58:45 +010013190 if (!new_state->fence) { /* implicit fencing */
13191 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13192 obj->resv, NULL,
13193 false, I915_FENCE_TIMEOUT,
13194 GFP_KERNEL);
13195 if (ret < 0)
13196 return ret;
Chris Wilson6b5e90f2016-11-14 20:41:05 +000013197
13198 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Chris Wilsonc004a902016-10-28 13:58:45 +010013199 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020013200
Chris Wilsond07f0e52016-10-28 13:58:44 +010013201 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080013202}
13203
Matt Roper38f3ce32014-12-02 07:45:25 -080013204/**
13205 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13206 * @plane: drm plane to clean up for
13207 * @fb: old framebuffer that was on plane
13208 *
13209 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013210 *
13211 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013212 */
13213void
13214intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013215 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013216{
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013217 struct i915_vma *vma;
Matt Roper38f3ce32014-12-02 07:45:25 -080013218
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013219 /* Should only be called after a successful intel_prepare_plane_fb()! */
13220 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13221 if (vma)
13222 intel_unpin_fb_vma(vma);
Matt Roper465c1202014-05-29 08:06:54 -070013223}
13224
Chandra Konduru6156a452015-04-27 13:48:39 -070013225int
13226skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13227{
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013228 struct drm_i915_private *dev_priv;
Chandra Konduru6156a452015-04-27 13:48:39 -070013229 int max_scale;
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013230 int crtc_clock, max_dotclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013231
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013232 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013233 return DRM_PLANE_HELPER_NO_SCALING;
13234
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013235 dev_priv = to_i915(intel_crtc->base.dev);
Chandra Konduru6156a452015-04-27 13:48:39 -070013236
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013237 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13238 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13239
13240 if (IS_GEMINILAKE(dev_priv))
13241 max_dotclk *= 2;
13242
13243 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013244 return DRM_PLANE_HELPER_NO_SCALING;
13245
13246 /*
13247 * skl max scale is lower of:
13248 * close to 3 but not 3, -1 is for that purpose
13249 * or
13250 * cdclk/crtc_clock
13251 */
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013252 max_scale = min((1 << 16) * 3 - 1,
13253 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
Chandra Konduru6156a452015-04-27 13:48:39 -070013254
13255 return max_scale;
13256}
13257
Matt Roper465c1202014-05-29 08:06:54 -070013258static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013259intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013260 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013261 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013262{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013263 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080013264 struct drm_crtc *crtc = state->base.crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013265 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013266 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13267 bool can_position = false;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013268 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013269
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013270 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013271 /* use scaler when colorkey is not required */
13272 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13273 min_scale = 1;
13274 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13275 }
Sonika Jindald8106362015-04-10 14:37:28 +053013276 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013277 }
Sonika Jindald8106362015-04-10 14:37:28 +053013278
Daniel Vettercc926382016-08-15 10:41:47 +020013279 ret = drm_plane_helper_check_state(&state->base,
13280 &state->clip,
13281 min_scale, max_scale,
13282 can_position, true);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013283 if (ret)
13284 return ret;
13285
Daniel Vettercc926382016-08-15 10:41:47 +020013286 if (!state->base.fb)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013287 return 0;
13288
13289 if (INTEL_GEN(dev_priv) >= 9) {
13290 ret = skl_check_plane_surface(state);
13291 if (ret)
13292 return ret;
13293 }
13294
13295 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013296}
13297
Daniel Vetter5a21b662016-05-24 17:13:53 +020013298static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13299 struct drm_crtc_state *old_crtc_state)
13300{
13301 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040013302 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Lyudeb707aa52016-09-15 10:56:06 -040013304 struct intel_crtc_state *intel_cstate =
13305 to_intel_crtc_state(crtc->state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013306 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020013307 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013308 struct intel_atomic_state *old_intel_state =
13309 to_intel_atomic_state(old_crtc_state->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013310 bool modeset = needs_modeset(crtc->state);
13311
13312 /* Perform vblank evasion around commit operation */
13313 intel_pipe_update_start(intel_crtc);
13314
13315 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013316 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013317
13318 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13319 intel_color_set_csc(crtc->state);
13320 intel_color_load_luts(crtc->state);
13321 }
13322
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013323 if (intel_cstate->update_pipe)
13324 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13325 else if (INTEL_GEN(dev_priv) >= 9)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013326 skl_detach_scalers(intel_crtc);
Lyude62e0fb82016-08-22 12:50:08 -040013327
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013328out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013329 if (dev_priv->display.atomic_update_watermarks)
13330 dev_priv->display.atomic_update_watermarks(old_intel_state,
13331 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013332}
13333
13334static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13335 struct drm_crtc_state *old_crtc_state)
13336{
13337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13338
13339 intel_pipe_update_end(intel_crtc, NULL);
13340}
13341
Matt Ropercf4c7c12014-12-04 10:27:42 -080013342/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013343 * intel_plane_destroy - destroy a plane
13344 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013345 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013346 * Common destruction function for all types of planes (primary, cursor,
13347 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013348 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013349void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013350{
Matt Roper465c1202014-05-29 08:06:54 -070013351 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030013352 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070013353}
13354
Matt Roper65a3fea2015-01-21 16:35:42 -080013355const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013356 .update_plane = drm_atomic_helper_update_plane,
13357 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013358 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013359 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013360 .atomic_get_property = intel_plane_atomic_get_property,
13361 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013362 .atomic_duplicate_state = intel_plane_duplicate_state,
13363 .atomic_destroy_state = intel_plane_destroy_state,
Matt Roper465c1202014-05-29 08:06:54 -070013364};
13365
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013366static int
13367intel_legacy_cursor_update(struct drm_plane *plane,
13368 struct drm_crtc *crtc,
13369 struct drm_framebuffer *fb,
13370 int crtc_x, int crtc_y,
13371 unsigned int crtc_w, unsigned int crtc_h,
13372 uint32_t src_x, uint32_t src_y,
13373 uint32_t src_w, uint32_t src_h)
13374{
13375 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13376 int ret;
13377 struct drm_plane_state *old_plane_state, *new_plane_state;
13378 struct intel_plane *intel_plane = to_intel_plane(plane);
13379 struct drm_framebuffer *old_fb;
13380 struct drm_crtc_state *crtc_state = crtc->state;
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013381 struct i915_vma *old_vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013382
13383 /*
13384 * When crtc is inactive or there is a modeset pending,
13385 * wait for it to complete in the slowpath
13386 */
13387 if (!crtc_state->active || needs_modeset(crtc_state) ||
13388 to_intel_crtc_state(crtc_state)->update_pipe)
13389 goto slow;
13390
13391 old_plane_state = plane->state;
13392
13393 /*
13394 * If any parameters change that may affect watermarks,
13395 * take the slowpath. Only changing fb or position should be
13396 * in the fastpath.
13397 */
13398 if (old_plane_state->crtc != crtc ||
13399 old_plane_state->src_w != src_w ||
13400 old_plane_state->src_h != src_h ||
13401 old_plane_state->crtc_w != crtc_w ||
13402 old_plane_state->crtc_h != crtc_h ||
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013403 !old_plane_state->fb != !fb)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013404 goto slow;
13405
13406 new_plane_state = intel_plane_duplicate_state(plane);
13407 if (!new_plane_state)
13408 return -ENOMEM;
13409
13410 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13411
13412 new_plane_state->src_x = src_x;
13413 new_plane_state->src_y = src_y;
13414 new_plane_state->src_w = src_w;
13415 new_plane_state->src_h = src_h;
13416 new_plane_state->crtc_x = crtc_x;
13417 new_plane_state->crtc_y = crtc_y;
13418 new_plane_state->crtc_w = crtc_w;
13419 new_plane_state->crtc_h = crtc_h;
13420
13421 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13422 to_intel_plane_state(new_plane_state));
13423 if (ret)
13424 goto out_free;
13425
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013426 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13427 if (ret)
13428 goto out_free;
13429
13430 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
13431 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13432
13433 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13434 if (ret) {
13435 DRM_DEBUG_KMS("failed to attach phys object\n");
13436 goto out_unlock;
13437 }
13438 } else {
13439 struct i915_vma *vma;
13440
13441 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13442 if (IS_ERR(vma)) {
13443 DRM_DEBUG_KMS("failed to pin object\n");
13444
13445 ret = PTR_ERR(vma);
13446 goto out_unlock;
13447 }
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013448
13449 to_intel_plane_state(new_plane_state)->vma = vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013450 }
13451
13452 old_fb = old_plane_state->fb;
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013453 old_vma = to_intel_plane_state(old_plane_state)->vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013454
13455 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13456 intel_plane->frontbuffer_bit);
13457
13458 /* Swap plane state */
13459 new_plane_state->fence = old_plane_state->fence;
13460 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13461 new_plane_state->fence = NULL;
13462 new_plane_state->fb = old_fb;
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013463 to_intel_plane_state(new_plane_state)->vma = old_vma;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013464
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013465 if (plane->state->visible)
13466 intel_plane->update_plane(plane,
13467 to_intel_crtc_state(crtc->state),
13468 to_intel_plane_state(plane->state));
13469 else
13470 intel_plane->disable_plane(plane, crtc);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013471
13472 intel_cleanup_plane_fb(plane, new_plane_state);
13473
13474out_unlock:
13475 mutex_unlock(&dev_priv->drm.struct_mutex);
13476out_free:
13477 intel_plane_destroy_state(plane, new_plane_state);
13478 return ret;
13479
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013480slow:
13481 return drm_atomic_helper_update_plane(plane, crtc, fb,
13482 crtc_x, crtc_y, crtc_w, crtc_h,
13483 src_x, src_y, src_w, src_h);
13484}
13485
13486static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13487 .update_plane = intel_legacy_cursor_update,
13488 .disable_plane = drm_atomic_helper_disable_plane,
13489 .destroy = intel_plane_destroy,
13490 .set_property = drm_atomic_helper_plane_set_property,
13491 .atomic_get_property = intel_plane_atomic_get_property,
13492 .atomic_set_property = intel_plane_atomic_set_property,
13493 .atomic_duplicate_state = intel_plane_duplicate_state,
13494 .atomic_destroy_state = intel_plane_destroy_state,
13495};
13496
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013497static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020013498intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070013499{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013500 struct intel_plane *primary = NULL;
13501 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013502 const uint32_t *intel_primary_formats;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013503 unsigned int supported_rotations;
Thierry Reding45e37432015-08-12 16:54:28 +020013504 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013505 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013506
13507 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013508 if (!primary) {
13509 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013510 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013511 }
Matt Roper465c1202014-05-29 08:06:54 -070013512
Matt Roper8e7d6882015-01-21 16:35:41 -080013513 state = intel_create_plane_state(&primary->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013514 if (!state) {
13515 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013516 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013517 }
13518
Matt Roper8e7d6882015-01-21 16:35:41 -080013519 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013520
Matt Roper465c1202014-05-29 08:06:54 -070013521 primary->can_scale = false;
13522 primary->max_downscale = 1;
Ville Syrjälä580503c2016-10-31 22:37:00 +020013523 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru6156a452015-04-27 13:48:39 -070013524 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013525 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013526 }
Matt Roper465c1202014-05-29 08:06:54 -070013527 primary->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013528 /*
13529 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13530 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13531 */
13532 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13533 primary->plane = (enum plane) !pipe;
13534 else
13535 primary->plane = (enum plane) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013536 primary->id = PLANE_PRIMARY;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013537 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013538 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013539
Ville Syrjälä580503c2016-10-31 22:37:00 +020013540 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013541 intel_primary_formats = skl_primary_formats;
13542 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013543
13544 primary->update_plane = skylake_update_primary_plane;
13545 primary->disable_plane = skylake_disable_primary_plane;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010013546 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013547 intel_primary_formats = i965_primary_formats;
13548 num_formats = ARRAY_SIZE(i965_primary_formats);
13549
13550 primary->update_plane = ironlake_update_primary_plane;
13551 primary->disable_plane = i9xx_disable_primary_plane;
Ville Syrjälä580503c2016-10-31 22:37:00 +020013552 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013553 intel_primary_formats = i965_primary_formats;
13554 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013555
13556 primary->update_plane = i9xx_update_primary_plane;
13557 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013558 } else {
13559 intel_primary_formats = i8xx_primary_formats;
13560 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013561
13562 primary->update_plane = i9xx_update_primary_plane;
13563 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013564 }
13565
Ville Syrjälä580503c2016-10-31 22:37:00 +020013566 if (INTEL_GEN(dev_priv) >= 9)
13567 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13568 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013569 intel_primary_formats, num_formats,
13570 DRM_PLANE_TYPE_PRIMARY,
13571 "plane 1%c", pipe_name(pipe));
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010013572 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä580503c2016-10-31 22:37:00 +020013573 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13574 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013575 intel_primary_formats, num_formats,
13576 DRM_PLANE_TYPE_PRIMARY,
13577 "primary %c", pipe_name(pipe));
13578 else
Ville Syrjälä580503c2016-10-31 22:37:00 +020013579 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13580 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013581 intel_primary_formats, num_formats,
13582 DRM_PLANE_TYPE_PRIMARY,
13583 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013584 if (ret)
13585 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053013586
Dave Airlie5481e272016-10-25 16:36:13 +100013587 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013588 supported_rotations =
13589 DRM_ROTATE_0 | DRM_ROTATE_90 |
13590 DRM_ROTATE_180 | DRM_ROTATE_270;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020013591 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13592 supported_rotations =
13593 DRM_ROTATE_0 | DRM_ROTATE_180 |
13594 DRM_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100013595 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013596 supported_rotations =
13597 DRM_ROTATE_0 | DRM_ROTATE_180;
13598 } else {
13599 supported_rotations = DRM_ROTATE_0;
13600 }
13601
Dave Airlie5481e272016-10-25 16:36:13 +100013602 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013603 drm_plane_create_rotation_property(&primary->base,
13604 DRM_ROTATE_0,
13605 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053013606
Matt Roperea2c67b2014-12-23 10:41:52 -080013607 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13608
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013609 return primary;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013610
13611fail:
13612 kfree(state);
13613 kfree(primary);
13614
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013615 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070013616}
13617
Matt Roper3d7d6512014-06-10 08:28:13 -070013618static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013619intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013620 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013621 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013622{
Matt Roper2b875c22014-12-01 15:40:13 -080013623 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013624 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013625 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013626 unsigned stride;
13627 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013628
Ville Syrjäläf8856a42016-07-26 19:07:00 +030013629 ret = drm_plane_helper_check_state(&state->base,
13630 &state->clip,
13631 DRM_PLANE_HELPER_NO_SCALING,
13632 DRM_PLANE_HELPER_NO_SCALING,
13633 true, true);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013634 if (ret)
13635 return ret;
13636
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013637 /* if we want to turn off the cursor ignore width and height */
13638 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013639 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013640
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013641 /* Check for which cursor types we support */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010013642 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
13643 state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013644 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13645 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013646 return -EINVAL;
13647 }
13648
Matt Roperea2c67b2014-12-23 10:41:52 -080013649 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13650 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013651 DRM_DEBUG_KMS("buffer is too small\n");
13652 return -ENOMEM;
13653 }
13654
Ville Syrjäläbae781b2016-11-16 13:33:16 +020013655 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013656 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013657 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013658 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013659
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013660 /*
13661 * There's something wrong with the cursor on CHV pipe C.
13662 * If it straddles the left edge of the screen then
13663 * moving it away from the edge or disabling it often
13664 * results in a pipe underrun, and often that can lead to
13665 * dead pipe (constant underrun reported, and it scans
13666 * out just a solid color). To recover from that, the
13667 * display power well must be turned off and on again.
13668 * Refuse the put the cursor into that compromised position.
13669 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013670 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
Ville Syrjälä936e71e2016-07-26 19:06:59 +030013671 state->base.visible && state->base.crtc_x < 0) {
Ville Syrjäläb29ec922015-12-18 19:24:39 +020013672 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13673 return -EINVAL;
13674 }
13675
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013676 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013677}
13678
Matt Roperf4a2cf22014-12-01 15:40:12 -080013679static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013680intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013681 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013682{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010013683 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13684
13685 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013686 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013687}
13688
13689static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013690intel_update_cursor_plane(struct drm_plane *plane,
13691 const struct intel_crtc_state *crtc_state,
13692 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013693{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013694 struct drm_crtc *crtc = crtc_state->base.crtc;
13695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000013696 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080013697 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013698 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013699
Matt Roperf4a2cf22014-12-01 15:40:12 -080013700 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013701 addr = 0;
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000013702 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013703 addr = intel_plane_ggtt_offset(state);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013704 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013705 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013706
Gustavo Padovana912f122014-12-01 15:40:10 -080013707 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013708 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013709}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013710
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013711static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020013712intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070013713{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013714 struct intel_plane *cursor = NULL;
13715 struct intel_plane_state *state = NULL;
13716 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070013717
13718 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013719 if (!cursor) {
13720 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013721 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013722 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013723
Matt Roper8e7d6882015-01-21 16:35:41 -080013724 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013725 if (!state) {
13726 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013727 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013728 }
13729
Matt Roper8e7d6882015-01-21 16:35:41 -080013730 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013731
Matt Roper3d7d6512014-06-10 08:28:13 -070013732 cursor->can_scale = false;
13733 cursor->max_downscale = 1;
13734 cursor->pipe = pipe;
13735 cursor->plane = pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013736 cursor->id = PLANE_CURSOR;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013737 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013738 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010013739 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013740 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013741
Ville Syrjälä580503c2016-10-31 22:37:00 +020013742 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013743 0, &intel_cursor_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013744 intel_cursor_formats,
13745 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013746 DRM_PLANE_TYPE_CURSOR,
13747 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013748 if (ret)
13749 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013750
Dave Airlie5481e272016-10-25 16:36:13 +100013751 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013752 drm_plane_create_rotation_property(&cursor->base,
13753 DRM_ROTATE_0,
13754 DRM_ROTATE_0 |
13755 DRM_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013756
Ville Syrjälä580503c2016-10-31 22:37:00 +020013757 if (INTEL_GEN(dev_priv) >= 9)
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013758 state->scaler_id = -1;
13759
Matt Roperea2c67b2014-12-23 10:41:52 -080013760 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13761
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013762 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013763
13764fail:
13765 kfree(state);
13766 kfree(cursor);
13767
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013768 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070013769}
13770
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013771static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13772 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013773{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013774 struct intel_crtc_scaler_state *scaler_state =
13775 &crtc_state->scaler_state;
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013776 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013777 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013778
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013779 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13780 if (!crtc->num_scalers)
13781 return;
13782
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013783 for (i = 0; i < crtc->num_scalers; i++) {
13784 struct intel_scaler *scaler = &scaler_state->scalers[i];
13785
13786 scaler->in_use = 0;
13787 scaler->mode = PS_SCALER_MODE_DYN;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013788 }
13789
13790 scaler_state->scaler_id = -1;
13791}
13792
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013793static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013794{
13795 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013796 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013797 struct intel_plane *primary = NULL;
13798 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013799 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013800
Daniel Vetter955382f2013-09-19 14:05:45 +020013801 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013802 if (!intel_crtc)
13803 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080013804
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013805 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013806 if (!crtc_state) {
13807 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013808 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013809 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013810 intel_crtc->config = crtc_state;
13811 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013812 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013813
Ville Syrjälä580503c2016-10-31 22:37:00 +020013814 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013815 if (IS_ERR(primary)) {
13816 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070013817 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013818 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013819 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013820
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013821 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013822 struct intel_plane *plane;
13823
Ville Syrjälä580503c2016-10-31 22:37:00 +020013824 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013825 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013826 ret = PTR_ERR(plane);
13827 goto fail;
13828 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013829 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013830 }
13831
Ville Syrjälä580503c2016-10-31 22:37:00 +020013832 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013833 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013834 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070013835 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013836 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013837 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013838
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013839 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013840 &primary->base, &cursor->base,
13841 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030013842 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070013843 if (ret)
13844 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013845
Jesse Barnes80824002009-09-10 15:28:06 -070013846 intel_crtc->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013847 intel_crtc->plane = primary->plane;
Jesse Barnes80824002009-09-10 15:28:06 -070013848
Chris Wilson4b0e3332014-05-30 16:35:26 +030013849 intel_crtc->cursor_base = ~0;
13850 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013851 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013852
Ville Syrjälä852eb002015-06-24 22:00:07 +030013853 intel_crtc->wm.cxsr_allowed = true;
13854
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013855 /* initialize shared scalers */
13856 intel_crtc_init_scalers(intel_crtc, crtc_state);
13857
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013858 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13859 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020013860 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13861 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013862
Jesse Barnes79e53942008-11-07 14:24:08 -080013863 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013864
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013865 intel_color_init(&intel_crtc->base);
13866
Daniel Vetter87b6b102014-05-15 15:33:46 +020013867 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013868
13869 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013870
13871fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013872 /*
13873 * drm_mode_config_cleanup() will free up any
13874 * crtcs/planes already initialized.
13875 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013876 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013877 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013878
13879 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013880}
13881
Jesse Barnes752aa882013-10-31 18:55:49 +020013882enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13883{
13884 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013885 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013886
Rob Clark51fd3712013-11-19 12:10:12 -050013887 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013888
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013889 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013890 return INVALID_PIPE;
13891
13892 return to_intel_crtc(encoder->crtc)->pipe;
13893}
13894
Carl Worth08d7b3d2009-04-29 14:43:54 -070013895int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013896 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013897{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013898 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013899 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013900 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013901
Rob Clark7707e652014-07-17 23:30:04 -040013902 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010013903 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013904 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013905
Rob Clark7707e652014-07-17 23:30:04 -040013906 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013907 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013908
Daniel Vetterc05422d2009-08-11 16:05:30 +020013909 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013910}
13911
Daniel Vetter66a92782012-07-12 20:08:18 +020013912static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013913{
Daniel Vetter66a92782012-07-12 20:08:18 +020013914 struct drm_device *dev = encoder->base.dev;
13915 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013916 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013917 int entry = 0;
13918
Damien Lespiaub2784e12014-08-05 11:29:37 +010013919 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013920 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013921 index_mask |= (1 << entry);
13922
Jesse Barnes79e53942008-11-07 14:24:08 -080013923 entry++;
13924 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013925
Jesse Barnes79e53942008-11-07 14:24:08 -080013926 return index_mask;
13927}
13928
Ville Syrjälä646d5772016-10-31 22:37:14 +020013929static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000013930{
Ville Syrjälä646d5772016-10-31 22:37:14 +020013931 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000013932 return false;
13933
13934 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13935 return false;
13936
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010013937 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013938 return false;
13939
13940 return true;
13941}
13942
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013943static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070013944{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013945 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000013946 return false;
13947
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010013948 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013949 return false;
13950
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013951 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013952 return false;
13953
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013954 if (HAS_PCH_LPT_H(dev_priv) &&
13955 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020013956 return false;
13957
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020013958 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013959 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 +020013960 return false;
13961
Ville Syrjäläe4abb732015-12-01 23:31:33 +020013962 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070013963 return false;
13964
13965 return true;
13966}
13967
Imre Deak8090ba82016-08-10 14:07:33 +030013968void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
13969{
13970 int pps_num;
13971 int pps_idx;
13972
13973 if (HAS_DDI(dev_priv))
13974 return;
13975 /*
13976 * This w/a is needed at least on CPT/PPT, but to be sure apply it
13977 * everywhere where registers can be write protected.
13978 */
13979 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13980 pps_num = 2;
13981 else
13982 pps_num = 1;
13983
13984 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
13985 u32 val = I915_READ(PP_CONTROL(pps_idx));
13986
13987 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
13988 I915_WRITE(PP_CONTROL(pps_idx), val);
13989 }
13990}
13991
Imre Deak44cb7342016-08-10 14:07:29 +030013992static void intel_pps_init(struct drm_i915_private *dev_priv)
13993{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020013994 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
Imre Deak44cb7342016-08-10 14:07:29 +030013995 dev_priv->pps_mmio_base = PCH_PPS_BASE;
13996 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13997 dev_priv->pps_mmio_base = VLV_PPS_BASE;
13998 else
13999 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030014000
14001 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030014002}
14003
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014004static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080014005{
Chris Wilson4ef69c72010-09-09 15:14:28 +010014006 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014007 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014008
Imre Deak44cb7342016-08-10 14:07:29 +030014009 intel_pps_init(dev_priv);
14010
Imre Deak97a824e12016-06-21 11:51:47 +030014011 /*
14012 * intel_edp_init_connector() depends on this completing first, to
14013 * prevent the registeration of both eDP and LVDS and the incorrect
14014 * sharing of the PPS.
14015 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014016 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014017
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014018 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014019 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014020
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020014021 if (IS_GEN9_LP(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053014022 /*
14023 * FIXME: Broxton doesn't support port detection via the
14024 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14025 * detect the ports.
14026 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014027 intel_ddi_init(dev_priv, PORT_A);
14028 intel_ddi_init(dev_priv, PORT_B);
14029 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014030
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014031 intel_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014032 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014033 int found;
14034
Jesse Barnesde31fac2015-03-06 15:53:32 -080014035 /*
14036 * Haswell uses DDI functions to detect digital outputs.
14037 * On SKL pre-D0 the strap isn't connected, so we assume
14038 * it's there.
14039 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014040 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014041 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014042 if (found || IS_GEN9_BC(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014043 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014044
14045 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14046 * register */
14047 found = I915_READ(SFUSE_STRAP);
14048
14049 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014050 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014051 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014052 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014053 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014054 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014055 /*
14056 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14057 */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014058 if (IS_GEN9_BC(dev_priv) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014059 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14060 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14061 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014062 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014063
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014064 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014065 int found;
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000014066 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014067
Ville Syrjälä646d5772016-10-31 22:37:14 +020014068 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014069 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014070
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014071 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014072 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014073 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014074 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014075 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014076 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014077 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014078 }
14079
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014080 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014081 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014082
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014083 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014084 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014085
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014086 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014087 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014088
Daniel Vetter270b3042012-10-27 15:52:05 +020014089 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014090 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014091 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014092 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014093
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014094 /*
14095 * The DP_DETECTED bit is the latched state of the DDC
14096 * SDA pin at boot. However since eDP doesn't require DDC
14097 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14098 * eDP ports may have been muxed to an alternate function.
14099 * Thus we can't rely on the DP_DETECTED bit alone to detect
14100 * eDP ports. Consult the VBT as well as DP_DETECTED to
14101 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014102 *
14103 * Sadly the straps seem to be missing sometimes even for HDMI
14104 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14105 * and VBT for the presence of the port. Additionally we can't
14106 * trust the port type the VBT declares as we've seen at least
14107 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014108 */
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000014109 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014110 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14111 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014112 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014113 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014114 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014115
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000014116 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014117 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14118 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014119 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014120 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014121 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014122
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014123 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014124 /*
14125 * eDP not supported on port D,
14126 * so no need to worry about it
14127 */
14128 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14129 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014130 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014131 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014132 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014133 }
14134
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014135 intel_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014136 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014137 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014138
Paulo Zanonie2debe92013-02-18 19:00:27 -030014139 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014140 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014141 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014142 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014143 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014144 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014145 }
Ma Ling27185ae2009-08-24 13:50:23 +080014146
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014147 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014148 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014149 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014150
14151 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014152
Paulo Zanonie2debe92013-02-18 19:00:27 -030014153 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014154 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014155 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014156 }
Ma Ling27185ae2009-08-24 13:50:23 +080014157
Paulo Zanonie2debe92013-02-18 19:00:27 -030014158 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014159
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014160 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014161 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014162 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014163 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014164 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014165 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014166 }
Ma Ling27185ae2009-08-24 13:50:23 +080014167
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014168 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014169 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014170 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014171 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014172
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000014173 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014174 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014175
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014176 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014177
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014178 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014179 encoder->base.possible_crtcs = encoder->crtc_mask;
14180 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014181 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014182 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014183
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014184 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020014185
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014186 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080014187}
14188
14189static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14190{
14191 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014192
Daniel Vetteref2d6332014-02-10 18:00:38 +010014193 drm_framebuffer_cleanup(fb);
Chris Wilson70001cd2017-02-16 09:46:21 +000014194
Chris Wilsondd689282017-03-01 15:41:28 +000014195 i915_gem_object_lock(intel_fb->obj);
14196 WARN_ON(!intel_fb->obj->framebuffer_references--);
14197 i915_gem_object_unlock(intel_fb->obj);
14198
Chris Wilsonf8c417c2016-07-20 13:31:53 +010014199 i915_gem_object_put(intel_fb->obj);
Chris Wilson70001cd2017-02-16 09:46:21 +000014200
Jesse Barnes79e53942008-11-07 14:24:08 -080014201 kfree(intel_fb);
14202}
14203
14204static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014205 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014206 unsigned int *handle)
14207{
14208 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014209 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014210
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014211 if (obj->userptr.mm) {
14212 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14213 return -EINVAL;
14214 }
14215
Chris Wilson05394f32010-11-08 19:18:58 +000014216 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014217}
14218
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014219static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14220 struct drm_file *file,
14221 unsigned flags, unsigned color,
14222 struct drm_clip_rect *clips,
14223 unsigned num_clips)
14224{
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014225 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014226
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014227 i915_gem_object_flush_if_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +000014228 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014229
14230 return 0;
14231}
14232
Jesse Barnes79e53942008-11-07 14:24:08 -080014233static const struct drm_framebuffer_funcs intel_fb_funcs = {
14234 .destroy = intel_user_framebuffer_destroy,
14235 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014236 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014237};
14238
Damien Lespiaub3218032015-02-27 11:15:18 +000014239static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014240u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14241 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000014242{
Chris Wilson24dbf512017-02-15 10:59:18 +000014243 u32 gen = INTEL_GEN(dev_priv);
Damien Lespiaub3218032015-02-27 11:15:18 +000014244
14245 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014246 int cpp = drm_format_plane_cpp(pixel_format, 0);
14247
Damien Lespiaub3218032015-02-27 11:15:18 +000014248 /* "The stride in bytes must not exceed the of the size of 8K
14249 * pixels and 32K bytes."
14250 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014251 return min(8192 * cpp, 32768);
Ville Syrjälä6401c372017-02-08 19:53:28 +020014252 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014253 return 32*1024;
14254 } else if (gen >= 4) {
14255 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14256 return 16*1024;
14257 else
14258 return 32*1024;
14259 } else if (gen >= 3) {
14260 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14261 return 8*1024;
14262 else
14263 return 16*1024;
14264 } else {
14265 /* XXX DSPC is limited to 4k tiled */
14266 return 8*1024;
14267 }
14268}
14269
Chris Wilson24dbf512017-02-15 10:59:18 +000014270static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14271 struct drm_i915_gem_object *obj,
14272 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014273{
Chris Wilson24dbf512017-02-15 10:59:18 +000014274 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014275 struct drm_format_name_buf format_name;
Chris Wilsondd689282017-03-01 15:41:28 +000014276 u32 pitch_limit, stride_alignment;
14277 unsigned int tiling, stride;
Chris Wilson24dbf512017-02-15 10:59:18 +000014278 int ret = -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -080014279
Chris Wilsondd689282017-03-01 15:41:28 +000014280 i915_gem_object_lock(obj);
14281 obj->framebuffer_references++;
14282 tiling = i915_gem_object_get_tiling(obj);
14283 stride = i915_gem_object_get_stride(obj);
14284 i915_gem_object_unlock(obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014285
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014286 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014287 /*
14288 * If there's a fence, enforce that
14289 * the fb modifier and tiling mode match.
14290 */
14291 if (tiling != I915_TILING_NONE &&
14292 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014293 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014294 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014295 }
14296 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014297 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014298 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014299 } else if (tiling == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014300 DRM_DEBUG("No Y tiling for legacy addfb\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014301 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014302 }
14303 }
14304
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014305 /* Passed in modifier sanity checking. */
14306 switch (mode_cmd->modifier[0]) {
14307 case I915_FORMAT_MOD_Y_TILED:
14308 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014309 if (INTEL_GEN(dev_priv) < 9) {
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014310 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14311 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014312 goto err;
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014313 }
14314 case DRM_FORMAT_MOD_NONE:
14315 case I915_FORMAT_MOD_X_TILED:
14316 break;
14317 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014318 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14319 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014320 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014321 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014322
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014323 /*
14324 * gen2/3 display engine uses the fence if present,
14325 * so the tiling mode must match the fb modifier exactly.
14326 */
14327 if (INTEL_INFO(dev_priv)->gen < 4 &&
14328 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
14329 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014330 goto err;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014331 }
14332
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014333 stride_alignment = intel_fb_stride_alignment(dev_priv,
14334 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014335 mode_cmd->pixel_format);
14336 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14337 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14338 mode_cmd->pitches[0], stride_alignment);
Chris Wilson24dbf512017-02-15 10:59:18 +000014339 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014340 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014341
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014342 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014343 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014344 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014345 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14346 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014347 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014348 mode_cmd->pitches[0], pitch_limit);
Chris Wilson24dbf512017-02-15 10:59:18 +000014349 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014350 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014351
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014352 /*
14353 * If there's a fence, enforce that
14354 * the fb pitch and fence stride match.
14355 */
Chris Wilsondd689282017-03-01 15:41:28 +000014356 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014357 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilsondd689282017-03-01 15:41:28 +000014358 mode_cmd->pitches[0], stride);
Chris Wilson24dbf512017-02-15 10:59:18 +000014359 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014360 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014361
Ville Syrjälä57779d02012-10-31 17:50:14 +020014362 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014363 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014364 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014365 case DRM_FORMAT_RGB565:
14366 case DRM_FORMAT_XRGB8888:
14367 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014368 break;
14369 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014370 if (INTEL_GEN(dev_priv) > 3) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014371 DRM_DEBUG("unsupported pixel format: %s\n",
14372 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014373 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014374 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014375 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014376 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014377 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014378 INTEL_GEN(dev_priv) < 9) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014379 DRM_DEBUG("unsupported pixel format: %s\n",
14380 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014381 goto err;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014382 }
14383 break;
14384 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014385 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014386 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014387 if (INTEL_GEN(dev_priv) < 4) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014388 DRM_DEBUG("unsupported pixel format: %s\n",
14389 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014390 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014391 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014392 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014393 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014394 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014395 DRM_DEBUG("unsupported pixel format: %s\n",
14396 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014397 goto err;
Damien Lespiau75312082015-05-15 19:06:01 +010014398 }
14399 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014400 case DRM_FORMAT_YUYV:
14401 case DRM_FORMAT_UYVY:
14402 case DRM_FORMAT_YVYU:
14403 case DRM_FORMAT_VYUY:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014404 if (INTEL_GEN(dev_priv) < 5) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014405 DRM_DEBUG("unsupported pixel format: %s\n",
14406 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014407 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014408 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014409 break;
14410 default:
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014411 DRM_DEBUG("unsupported pixel format: %s\n",
14412 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014413 goto err;
Chris Wilson57cd6502010-08-08 12:34:44 +010014414 }
14415
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014416 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14417 if (mode_cmd->offsets[0] != 0)
Chris Wilson24dbf512017-02-15 10:59:18 +000014418 goto err;
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014419
Chris Wilson24dbf512017-02-15 10:59:18 +000014420 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14421 &intel_fb->base, mode_cmd);
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014422 intel_fb->obj = obj;
14423
Ville Syrjälä6687c902015-09-15 13:16:41 +030014424 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14425 if (ret)
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014426 goto err;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014427
Chris Wilson24dbf512017-02-15 10:59:18 +000014428 ret = drm_framebuffer_init(obj->base.dev,
14429 &intel_fb->base,
14430 &intel_fb_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080014431 if (ret) {
14432 DRM_ERROR("framebuffer init failed %d\n", ret);
Chris Wilson24dbf512017-02-15 10:59:18 +000014433 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -080014434 }
14435
Jesse Barnes79e53942008-11-07 14:24:08 -080014436 return 0;
Chris Wilson24dbf512017-02-15 10:59:18 +000014437
14438err:
Chris Wilsondd689282017-03-01 15:41:28 +000014439 i915_gem_object_lock(obj);
14440 obj->framebuffer_references--;
14441 i915_gem_object_unlock(obj);
Chris Wilson24dbf512017-02-15 10:59:18 +000014442 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014443}
14444
Jesse Barnes79e53942008-11-07 14:24:08 -080014445static struct drm_framebuffer *
14446intel_user_framebuffer_create(struct drm_device *dev,
14447 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014448 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014449{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014450 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014451 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014452 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014453
Chris Wilson03ac0642016-07-20 13:31:51 +010014454 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14455 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014456 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014457
Chris Wilson24dbf512017-02-15 10:59:18 +000014458 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014459 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010014460 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014461
14462 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014463}
14464
Chris Wilson778e23a2016-12-05 14:29:39 +000014465static void intel_atomic_state_free(struct drm_atomic_state *state)
14466{
14467 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14468
14469 drm_atomic_state_default_release(state);
14470
14471 i915_sw_fence_fini(&intel_state->commit_ready);
14472
14473 kfree(state);
14474}
14475
Jesse Barnes79e53942008-11-07 14:24:08 -080014476static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014477 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014478 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014479 .atomic_check = intel_atomic_check,
14480 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014481 .atomic_state_alloc = intel_atomic_state_alloc,
14482 .atomic_state_clear = intel_atomic_state_clear,
Chris Wilson778e23a2016-12-05 14:29:39 +000014483 .atomic_state_free = intel_atomic_state_free,
Jesse Barnes79e53942008-11-07 14:24:08 -080014484};
14485
Imre Deak88212942016-03-16 13:38:53 +020014486/**
14487 * intel_init_display_hooks - initialize the display modesetting hooks
14488 * @dev_priv: device private
14489 */
14490void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014491{
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +020014492 intel_init_cdclk_hooks(dev_priv);
14493
Imre Deak88212942016-03-16 13:38:53 +020014494 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014495 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014496 dev_priv->display.get_initial_plane_config =
14497 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014498 dev_priv->display.crtc_compute_clock =
14499 haswell_crtc_compute_clock;
14500 dev_priv->display.crtc_enable = haswell_crtc_enable;
14501 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014502 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014503 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014504 dev_priv->display.get_initial_plane_config =
14505 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014506 dev_priv->display.crtc_compute_clock =
14507 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014508 dev_priv->display.crtc_enable = haswell_crtc_enable;
14509 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014510 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014511 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014512 dev_priv->display.get_initial_plane_config =
14513 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014514 dev_priv->display.crtc_compute_clock =
14515 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014516 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14517 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014518 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014519 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014520 dev_priv->display.get_initial_plane_config =
14521 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014522 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14523 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14524 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14525 } else if (IS_VALLEYVIEW(dev_priv)) {
14526 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14527 dev_priv->display.get_initial_plane_config =
14528 i9xx_get_initial_plane_config;
14529 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014530 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14531 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014532 } else if (IS_G4X(dev_priv)) {
14533 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14534 dev_priv->display.get_initial_plane_config =
14535 i9xx_get_initial_plane_config;
14536 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14537 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14538 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014539 } else if (IS_PINEVIEW(dev_priv)) {
14540 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14541 dev_priv->display.get_initial_plane_config =
14542 i9xx_get_initial_plane_config;
14543 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14544 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14545 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014546 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014547 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014548 dev_priv->display.get_initial_plane_config =
14549 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014550 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014551 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14552 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014553 } else {
14554 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14555 dev_priv->display.get_initial_plane_config =
14556 i9xx_get_initial_plane_config;
14557 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14558 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14559 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014560 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014561
Imre Deak88212942016-03-16 13:38:53 +020014562 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014563 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014564 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014565 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014566 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014567 /* FIXME: detect B0+ stepping and use auto training */
14568 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014569 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014570 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014571 }
14572
Lyude27082492016-08-24 07:48:10 +020014573 if (dev_priv->info.gen >= 9)
14574 dev_priv->display.update_crtcs = skl_update_crtcs;
14575 else
14576 dev_priv->display.update_crtcs = intel_update_crtcs;
14577
Daniel Vetter5a21b662016-05-24 17:13:53 +020014578 switch (INTEL_INFO(dev_priv)->gen) {
14579 case 2:
14580 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14581 break;
14582
14583 case 3:
14584 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14585 break;
14586
14587 case 4:
14588 case 5:
14589 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14590 break;
14591
14592 case 6:
14593 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14594 break;
14595 case 7:
14596 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14597 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14598 break;
14599 case 9:
14600 /* Drop through - unsupported since execlist only. */
14601 default:
14602 /* Default just returns -ENODEV to indicate unsupported */
14603 dev_priv->display.queue_flip = intel_default_queue_flip;
14604 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014605}
14606
Jesse Barnesb690e962010-07-19 13:53:12 -070014607/*
14608 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14609 * resume, or other times. This quirk makes sure that's the case for
14610 * affected systems.
14611 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014612static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014613{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014614 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070014615
14616 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014617 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014618}
14619
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014620static void quirk_pipeb_force(struct drm_device *dev)
14621{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014622 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014623
14624 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14625 DRM_INFO("applying pipe b force quirk\n");
14626}
14627
Keith Packard435793d2011-07-12 14:56:22 -070014628/*
14629 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14630 */
14631static void quirk_ssc_force_disable(struct drm_device *dev)
14632{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014633 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070014634 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014635 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014636}
14637
Carsten Emde4dca20e2012-03-15 15:56:26 +010014638/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014639 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14640 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014641 */
14642static void quirk_invert_brightness(struct drm_device *dev)
14643{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014644 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010014645 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014646 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014647}
14648
Scot Doyle9c72cc62014-07-03 23:27:50 +000014649/* Some VBT's incorrectly indicate no backlight is present */
14650static void quirk_backlight_present(struct drm_device *dev)
14651{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014652 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000014653 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14654 DRM_INFO("applying backlight present quirk\n");
14655}
14656
Jesse Barnesb690e962010-07-19 13:53:12 -070014657struct intel_quirk {
14658 int device;
14659 int subsystem_vendor;
14660 int subsystem_device;
14661 void (*hook)(struct drm_device *dev);
14662};
14663
Egbert Eich5f85f172012-10-14 15:46:38 +020014664/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14665struct intel_dmi_quirk {
14666 void (*hook)(struct drm_device *dev);
14667 const struct dmi_system_id (*dmi_id_list)[];
14668};
14669
14670static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14671{
14672 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14673 return 1;
14674}
14675
14676static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14677 {
14678 .dmi_id_list = &(const struct dmi_system_id[]) {
14679 {
14680 .callback = intel_dmi_reverse_brightness,
14681 .ident = "NCR Corporation",
14682 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14683 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14684 },
14685 },
14686 { } /* terminating entry */
14687 },
14688 .hook = quirk_invert_brightness,
14689 },
14690};
14691
Ben Widawskyc43b5632012-04-16 14:07:40 -070014692static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014693 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14694 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14695
Jesse Barnesb690e962010-07-19 13:53:12 -070014696 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14697 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14698
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014699 /* 830 needs to leave pipe A & dpll A up */
14700 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14701
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014702 /* 830 needs to leave pipe B & dpll B up */
14703 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14704
Keith Packard435793d2011-07-12 14:56:22 -070014705 /* Lenovo U160 cannot use SSC on LVDS */
14706 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014707
14708 /* Sony Vaio Y cannot use SSC on LVDS */
14709 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014710
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014711 /* Acer Aspire 5734Z must invert backlight brightness */
14712 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14713
14714 /* Acer/eMachines G725 */
14715 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14716
14717 /* Acer/eMachines e725 */
14718 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14719
14720 /* Acer/Packard Bell NCL20 */
14721 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14722
14723 /* Acer Aspire 4736Z */
14724 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014725
14726 /* Acer Aspire 5336 */
14727 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014728
14729 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14730 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014731
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014732 /* Acer C720 Chromebook (Core i3 4005U) */
14733 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14734
jens steinb2a96012014-10-28 20:25:53 +010014735 /* Apple Macbook 2,1 (Core 2 T7400) */
14736 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14737
Jani Nikula1b9448b02015-11-05 11:49:59 +020014738 /* Apple Macbook 4,1 */
14739 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14740
Scot Doyled4967d82014-07-03 23:27:52 +000014741 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14742 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014743
14744 /* HP Chromebook 14 (Celeron 2955U) */
14745 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014746
14747 /* Dell Chromebook 11 */
14748 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020014749
14750 /* Dell Chromebook 11 (2015 version) */
14751 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014752};
14753
14754static void intel_init_quirks(struct drm_device *dev)
14755{
14756 struct pci_dev *d = dev->pdev;
14757 int i;
14758
14759 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14760 struct intel_quirk *q = &intel_quirks[i];
14761
14762 if (d->device == q->device &&
14763 (d->subsystem_vendor == q->subsystem_vendor ||
14764 q->subsystem_vendor == PCI_ANY_ID) &&
14765 (d->subsystem_device == q->subsystem_device ||
14766 q->subsystem_device == PCI_ANY_ID))
14767 q->hook(dev);
14768 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014769 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14770 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14771 intel_dmi_quirks[i].hook(dev);
14772 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014773}
14774
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014775/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014776static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014777{
David Weinehall52a05c32016-08-22 13:32:44 +030014778 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014779 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014780 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014781
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014782 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030014783 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014784 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014785 sr1 = inb(VGA_SR_DATA);
14786 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030014787 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014788 udelay(300);
14789
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014790 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014791 POSTING_READ(vga_reg);
14792}
14793
Daniel Vetterf8175862012-04-10 15:50:11 +020014794void intel_modeset_init_hw(struct drm_device *dev)
14795{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014796 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014797
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014798 intel_update_cdclk(dev_priv);
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020014799 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014800
Ville Syrjälä46f16e62016-10-31 22:37:22 +020014801 intel_init_clock_gating(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020014802}
14803
Matt Roperd93c0372015-12-03 11:37:41 -080014804/*
14805 * Calculate what we think the watermarks should be for the state we've read
14806 * out of the hardware and then immediately program those watermarks so that
14807 * we ensure the hardware settings match our internal state.
14808 *
14809 * We can calculate what we think WM's should be by creating a duplicate of the
14810 * current state (which was constructed during hardware readout) and running it
14811 * through the atomic check code to calculate new watermark values in the
14812 * state object.
14813 */
14814static void sanitize_watermarks(struct drm_device *dev)
14815{
14816 struct drm_i915_private *dev_priv = to_i915(dev);
14817 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014818 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014819 struct drm_crtc *crtc;
14820 struct drm_crtc_state *cstate;
14821 struct drm_modeset_acquire_ctx ctx;
14822 int ret;
14823 int i;
14824
14825 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080014826 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080014827 return;
14828
14829 /*
14830 * We need to hold connection_mutex before calling duplicate_state so
14831 * that the connector loop is protected.
14832 */
14833 drm_modeset_acquire_init(&ctx, 0);
14834retry:
Matt Roper0cd12622016-01-12 07:13:37 -080014835 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080014836 if (ret == -EDEADLK) {
14837 drm_modeset_backoff(&ctx);
14838 goto retry;
14839 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080014840 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014841 }
14842
14843 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14844 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080014845 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014846
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014847 intel_state = to_intel_atomic_state(state);
14848
Matt Ropered4a6a72016-02-23 17:20:13 -080014849 /*
14850 * Hardware readout is the only time we don't want to calculate
14851 * intermediate watermarks (since we don't trust the current
14852 * watermarks).
14853 */
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014854 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080014855
Matt Roperd93c0372015-12-03 11:37:41 -080014856 ret = intel_atomic_check(dev, state);
14857 if (ret) {
14858 /*
14859 * If we fail here, it means that the hardware appears to be
14860 * programmed in a way that shouldn't be possible, given our
14861 * understanding of watermark requirements. This might mean a
14862 * mistake in the hardware readout code or a mistake in the
14863 * watermark calculations for a given platform. Raise a WARN
14864 * so that this is noticeable.
14865 *
14866 * If this actually happens, we'll have to just leave the
14867 * BIOS-programmed watermarks untouched and hope for the best.
14868 */
14869 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014870 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014871 }
14872
14873 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080014874 for_each_crtc_in_state(state, crtc, cstate, i) {
14875 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14876
Matt Ropered4a6a72016-02-23 17:20:13 -080014877 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014878 dev_priv->display.optimize_watermarks(intel_state, cs);
Matt Roperd93c0372015-12-03 11:37:41 -080014879 }
14880
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014881put_state:
Chris Wilson08536952016-10-14 13:18:18 +010014882 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080014883fail:
Matt Roperd93c0372015-12-03 11:37:41 -080014884 drm_modeset_drop_locks(&ctx);
14885 drm_modeset_acquire_fini(&ctx);
14886}
14887
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014888int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080014889{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030014890 struct drm_i915_private *dev_priv = to_i915(dev);
14891 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014892 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014893 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014894
14895 drm_mode_config_init(dev);
14896
14897 dev->mode_config.min_width = 0;
14898 dev->mode_config.min_height = 0;
14899
Dave Airlie019d96c2011-09-29 16:20:42 +010014900 dev->mode_config.preferred_depth = 24;
14901 dev->mode_config.prefer_shadow = 1;
14902
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014903 dev->mode_config.allow_fb_modifiers = true;
14904
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014905 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014906
Chris Wilsoneb955ee2017-01-23 21:29:39 +000014907 INIT_WORK(&dev_priv->atomic_helper.free_work,
Chris Wilsonba318c62017-02-02 20:47:41 +000014908 intel_atomic_helper_free_state_worker);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000014909
Jesse Barnesb690e962010-07-19 13:53:12 -070014910 intel_init_quirks(dev);
14911
Ville Syrjälä62d75df2016-10-31 22:37:25 +020014912 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014913
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014914 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014915 return 0;
Ben Widawskye3c74752013-04-05 13:12:39 -070014916
Lukas Wunner69f92f62015-07-15 13:57:35 +020014917 /*
14918 * There may be no VBT; and if the BIOS enabled SSC we can
14919 * just keep using it to avoid unnecessary flicker. Whereas if the
14920 * BIOS isn't using it, don't assume it will work even if the VBT
14921 * indicates as much.
14922 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014923 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020014924 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14925 DREF_SSC1_ENABLE);
14926
14927 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14928 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14929 bios_lvds_use_ssc ? "en" : "dis",
14930 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14931 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14932 }
14933 }
14934
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014935 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014936 dev->mode_config.max_width = 2048;
14937 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014938 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014939 dev->mode_config.max_width = 4096;
14940 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014941 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014942 dev->mode_config.max_width = 8192;
14943 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014944 }
Damien Lespiau068be562014-03-28 14:17:49 +000014945
Jani Nikula2a307c22016-11-30 17:43:04 +020014946 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
14947 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014948 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014949 } else if (IS_GEN2(dev_priv)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014950 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14951 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14952 } else {
14953 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14954 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14955 }
14956
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030014957 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014958
Zhao Yakui28c97732009-10-09 11:39:41 +080014959 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014960 INTEL_INFO(dev_priv)->num_pipes,
14961 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014962
Damien Lespiau055e3932014-08-18 13:49:10 +010014963 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014964 int ret;
14965
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020014966 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014967 if (ret) {
14968 drm_mode_config_cleanup(dev);
14969 return ret;
14970 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014971 }
14972
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030014973 intel_update_czclk(dev_priv);
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014974 intel_update_cdclk(dev_priv);
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020014975 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030014976
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014977 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014978
Ville Syrjäläb2045352016-05-13 23:41:27 +030014979 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014980 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030014981
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014982 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014983 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014984 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000014985
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014986 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020014987 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014988 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014989
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014990 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014991 struct intel_initial_plane_config plane_config = {};
14992
Jesse Barnes46f297f2014-03-07 08:57:48 -080014993 if (!crtc->active)
14994 continue;
14995
Jesse Barnes46f297f2014-03-07 08:57:48 -080014996 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014997 * Note that reserving the BIOS fb up front prevents us
14998 * from stuffing other stolen allocations like the ring
14999 * on top. This prevents some ugliness at boot time, and
15000 * can even allow for smooth boot transitions if the BIOS
15001 * fb is large enough for the active pipe configuration.
15002 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015003 dev_priv->display.get_initial_plane_config(crtc,
15004 &plane_config);
15005
15006 /*
15007 * If the fb is shared between multiple heads, we'll
15008 * just get the first one.
15009 */
15010 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015011 }
Matt Roperd93c0372015-12-03 11:37:41 -080015012
15013 /*
15014 * Make sure hardware watermarks really match the state we read out.
15015 * Note that we need to do this after reconstructing the BIOS fb's
15016 * since the watermark calculation done here will use pstate->fb.
15017 */
15018 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015019
15020 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010015021}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015022
Daniel Vetter7fad7982012-07-04 17:51:47 +020015023static void intel_enable_pipe_a(struct drm_device *dev)
15024{
15025 struct intel_connector *connector;
15026 struct drm_connector *crt = NULL;
15027 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015028 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015029
15030 /* We can't just switch on the pipe A, we need to set things up with a
15031 * proper mode and output configuration. As a gross hack, enable pipe A
15032 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015033 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015034 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15035 crt = &connector->base;
15036 break;
15037 }
15038 }
15039
15040 if (!crt)
15041 return;
15042
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015043 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015044 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015045}
15046
Daniel Vetterfa555832012-10-10 23:14:00 +020015047static bool
15048intel_check_plane_mapping(struct intel_crtc *crtc)
15049{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015050 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015051 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015052
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015053 if (INTEL_INFO(dev_priv)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015054 return true;
15055
Ville Syrjälä649636e2015-09-22 19:50:01 +030015056 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015057
15058 if ((val & DISPLAY_PLANE_ENABLE) &&
15059 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15060 return false;
15061
15062 return true;
15063}
15064
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015065static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15066{
15067 struct drm_device *dev = crtc->base.dev;
15068 struct intel_encoder *encoder;
15069
15070 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15071 return true;
15072
15073 return false;
15074}
15075
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015076static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15077{
15078 struct drm_device *dev = encoder->base.dev;
15079 struct intel_connector *connector;
15080
15081 for_each_connector_on_encoder(dev, &encoder->base, connector)
15082 return connector;
15083
15084 return NULL;
15085}
15086
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015087static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15088 enum transcoder pch_transcoder)
15089{
15090 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15091 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15092}
15093
Daniel Vetter24929352012-07-02 20:28:59 +020015094static void intel_sanitize_crtc(struct intel_crtc *crtc)
15095{
15096 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015097 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015098 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015099
Daniel Vetter24929352012-07-02 20:28:59 +020015100 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015101 if (!transcoder_is_dsi(cpu_transcoder)) {
15102 i915_reg_t reg = PIPECONF(cpu_transcoder);
15103
15104 I915_WRITE(reg,
15105 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15106 }
Daniel Vetter24929352012-07-02 20:28:59 +020015107
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015108 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015109 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015110 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015111 struct intel_plane *plane;
15112
Daniel Vetter96256042015-02-13 21:03:42 +010015113 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015114
15115 /* Disable everything but the primary plane */
15116 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15117 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15118 continue;
15119
15120 plane->disable_plane(&plane->base, &crtc->base);
15121 }
Daniel Vetter96256042015-02-13 21:03:42 +010015122 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015123
Daniel Vetter24929352012-07-02 20:28:59 +020015124 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015125 * disable the crtc (and hence change the state) if it is wrong. Note
15126 * that gen4+ has a fixed plane -> pipe mapping. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015127 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015128 bool plane;
15129
Ville Syrjälä78108b72016-05-27 20:59:19 +030015130 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15131 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015132
15133 /* Pipe has the wrong plane attached and the plane is active.
15134 * Temporarily change the plane mapping and disable everything
15135 * ... */
15136 plane = crtc->plane;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010015137 crtc->base.primary->state->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015138 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015139 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015140 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015141 }
Daniel Vetter24929352012-07-02 20:28:59 +020015142
Daniel Vetter7fad7982012-07-04 17:51:47 +020015143 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15144 crtc->pipe == PIPE_A && !crtc->active) {
15145 /* BIOS forgot to enable pipe A, this mostly happens after
15146 * resume. Force-enable the pipe to fix this, the update_dpms
15147 * call below we restore the pipe to the right state, but leave
15148 * the required bits on. */
15149 intel_enable_pipe_a(dev);
15150 }
15151
Daniel Vetter24929352012-07-02 20:28:59 +020015152 /* Adjust the state of the output pipe according to whether we
15153 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015154 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015155 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015156
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010015157 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015158 /*
15159 * We start out with underrun reporting disabled to avoid races.
15160 * For correct bookkeeping mark this on active crtcs.
15161 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015162 * Also on gmch platforms we dont have any hardware bits to
15163 * disable the underrun reporting. Which means we need to start
15164 * out with underrun reporting disabled also on inactive pipes,
15165 * since otherwise we'll complain about the garbage we read when
15166 * e.g. coming up after runtime pm.
15167 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015168 * No protection against concurrent access is required - at
15169 * worst a fifo underrun happens which also sets this to false.
15170 */
15171 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015172 /*
15173 * We track the PCH trancoder underrun reporting state
15174 * within the crtc. With crtc for pipe A housing the underrun
15175 * reporting state for PCH transcoder A, crtc for pipe B housing
15176 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15177 * and marking underrun reporting as disabled for the non-existing
15178 * PCH transcoders B and C would prevent enabling the south
15179 * error interrupt (see cpt_can_enable_serr_int()).
15180 */
15181 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15182 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010015183 }
Daniel Vetter24929352012-07-02 20:28:59 +020015184}
15185
15186static void intel_sanitize_encoder(struct intel_encoder *encoder)
15187{
15188 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020015189
15190 /* We need to check both for a crtc link (meaning that the
15191 * encoder is active and trying to read from a pipe) and the
15192 * pipe itself being active. */
15193 bool has_active_crtc = encoder->base.crtc &&
15194 to_intel_crtc(encoder->base.crtc)->active;
15195
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015196 connector = intel_encoder_find_connector(encoder);
15197 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015198 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15199 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015200 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015201
15202 /* Connector is active, but has no active pipe. This is
15203 * fallout from our resume register restoring. Disable
15204 * the encoder manually again. */
15205 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015206 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15207
Daniel Vetter24929352012-07-02 20:28:59 +020015208 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15209 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015210 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015211 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015212 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015213 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020015214 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015215 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015216
15217 /* Inconsistent output/port/pipe state happens presumably due to
15218 * a bug in one of the get_hw_state functions. Or someplace else
15219 * in our code, like the register restore mess on resume. Clamp
15220 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015221
15222 connector->base.dpms = DRM_MODE_DPMS_OFF;
15223 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015224 }
15225 /* Enabled encoders without active connectors will be fixed in
15226 * the crtc fixup. */
15227}
15228
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015229void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015230{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015231 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015232
Imre Deak04098752014-02-18 00:02:16 +020015233 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15234 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015235 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020015236 }
15237}
15238
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015239void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020015240{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015241 /* This function can be called both from intel_modeset_setup_hw_state or
15242 * at a very early point in our resume sequence, where the power well
15243 * structures are not yet restored. Since this function is at a very
15244 * paranoid "someone might have enabled VGA while we were not looking"
15245 * level, just check if the power well is enabled instead of trying to
15246 * follow the "don't touch the power well if we don't need it" policy
15247 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015248 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015249 return;
15250
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015251 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020015252
15253 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015254}
15255
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015256static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015257{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015258 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015259
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015260 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015261}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015262
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015263/* FIXME read out full plane state for all planes */
15264static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015265{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015266 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015267 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015268 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015269
Ville Syrjälä936e71e2016-07-26 19:06:59 +030015270 plane_state->base.visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015271 primary_get_hw_state(to_intel_plane(primary));
15272
Ville Syrjälä936e71e2016-07-26 19:06:59 +030015273 if (plane_state->base.visible)
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015274 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015275}
15276
Daniel Vetter30e984d2013-06-05 13:34:17 +020015277static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015278{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015279 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015280 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015281 struct intel_crtc *crtc;
15282 struct intel_encoder *encoder;
15283 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015284 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015285
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015286 dev_priv->active_crtcs = 0;
15287
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015288 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015289 struct intel_crtc_state *crtc_state =
15290 to_intel_crtc_state(crtc->base.state);
Daniel Vetter3b117c82013-04-17 20:15:07 +020015291
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015292 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015293 memset(crtc_state, 0, sizeof(*crtc_state));
15294 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015295
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015296 crtc_state->base.active = crtc_state->base.enable =
15297 dev_priv->display.get_pipe_config(crtc, crtc_state);
15298
15299 crtc->base.enabled = crtc_state->base.enable;
15300 crtc->active = crtc_state->base.active;
15301
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015302 if (crtc_state->base.active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015303 dev_priv->active_crtcs |= 1 << crtc->pipe;
15304
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015305 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015306
Ville Syrjälä78108b72016-05-27 20:59:19 +030015307 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15308 crtc->base.base.id, crtc->base.name,
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015309 enableddisabled(crtc_state->base.active));
Daniel Vetter24929352012-07-02 20:28:59 +020015310 }
15311
Daniel Vetter53589012013-06-05 13:34:16 +020015312 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15313 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15314
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015315 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015316 &pll->state.hw_state);
15317 pll->state.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015318 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015319 struct intel_crtc_state *crtc_state =
15320 to_intel_crtc_state(crtc->base.state);
15321
15322 if (crtc_state->base.active &&
15323 crtc_state->shared_dpll == pll)
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015324 pll->state.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015325 }
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015326 pll->active_mask = pll->state.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015327
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015328 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015329 pll->name, pll->state.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015330 }
15331
Damien Lespiaub2784e12014-08-05 11:29:37 +010015332 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015333 pipe = 0;
15334
15335 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015336 struct intel_crtc_state *crtc_state;
15337
Ville Syrjälä98187832016-10-31 22:37:10 +020015338 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015339 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015340
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015341 encoder->base.crtc = &crtc->base;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015342 crtc_state->output_types |= 1 << encoder->type;
15343 encoder->get_config(encoder, crtc_state);
Daniel Vetter24929352012-07-02 20:28:59 +020015344 } else {
15345 encoder->base.crtc = NULL;
15346 }
15347
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015348 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015349 encoder->base.base.id, encoder->base.name,
15350 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015351 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015352 }
15353
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015354 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015355 if (connector->get_hw_state(connector)) {
15356 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015357
15358 encoder = connector->encoder;
15359 connector->base.encoder = &encoder->base;
15360
15361 if (encoder->base.crtc &&
15362 encoder->base.crtc->state->active) {
15363 /*
15364 * This has to be done during hardware readout
15365 * because anything calling .crtc_disable may
15366 * rely on the connector_mask being accurate.
15367 */
15368 encoder->base.crtc->state->connector_mask |=
15369 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015370 encoder->base.crtc->state->encoder_mask |=
15371 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015372 }
15373
Daniel Vetter24929352012-07-02 20:28:59 +020015374 } else {
15375 connector->base.dpms = DRM_MODE_DPMS_OFF;
15376 connector->base.encoder = NULL;
15377 }
15378 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015379 connector->base.base.id, connector->base.name,
15380 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020015381 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015382
15383 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015384 struct intel_crtc_state *crtc_state =
15385 to_intel_crtc_state(crtc->base.state);
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015386 int pixclk = 0;
15387
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015388 crtc->base.hwmode = crtc_state->base.adjusted_mode;
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015389
15390 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015391 if (crtc_state->base.active) {
15392 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15393 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015394 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15395
15396 /*
15397 * The initial mode needs to be set in order to keep
15398 * the atomic core happy. It wants a valid mode if the
15399 * crtc's enabled, so we do the above call.
15400 *
Daniel Vetter7800fb62016-12-19 09:24:23 +010015401 * But we don't set all the derived state fully, hence
15402 * set a flag to indicate that a full recalculation is
15403 * needed on the next commit.
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015404 */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015405 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015406
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020015407 intel_crtc_compute_pixel_rate(crtc_state);
15408
15409 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15410 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15411 pixclk = crtc_state->pixel_rate;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015412 else
15413 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15414
15415 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015416 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015417 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15418
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015419 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15420 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015421 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015422
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015423 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15424
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015425 intel_pipe_config_sanity_check(dev_priv, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015426 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015427}
15428
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015429static void
15430get_encoder_power_domains(struct drm_i915_private *dev_priv)
15431{
15432 struct intel_encoder *encoder;
15433
15434 for_each_intel_encoder(&dev_priv->drm, encoder) {
15435 u64 get_domains;
15436 enum intel_display_power_domain domain;
15437
15438 if (!encoder->get_power_domains)
15439 continue;
15440
15441 get_domains = encoder->get_power_domains(encoder);
15442 for_each_power_domain(domain, get_domains)
15443 intel_display_power_get(dev_priv, domain);
15444 }
15445}
15446
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015447/* Scan out the current hw modeset state,
15448 * and sanitizes it to the current state
15449 */
15450static void
15451intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015452{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015453 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015454 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015455 struct intel_crtc *crtc;
15456 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015457 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015458
15459 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015460
15461 /* HW state is read out, now we need to sanitize this mess. */
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015462 get_encoder_power_domains(dev_priv);
15463
Damien Lespiaub2784e12014-08-05 11:29:37 +010015464 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015465 intel_sanitize_encoder(encoder);
15466 }
15467
Damien Lespiau055e3932014-08-18 13:49:10 +010015468 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020015469 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015470
Daniel Vetter24929352012-07-02 20:28:59 +020015471 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015472 intel_dump_pipe_config(crtc, crtc->config,
15473 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015474 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015475
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015476 intel_modeset_update_connector_atomic_state(dev);
15477
Daniel Vetter35c95372013-07-17 06:55:04 +020015478 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15479 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15480
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015481 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015482 continue;
15483
15484 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15485
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015486 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015487 pll->on = false;
15488 }
15489
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015490 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015491 vlv_wm_get_hw_state(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015492 else if (IS_GEN9(dev_priv))
Pradeep Bhat30789992014-11-04 17:06:45 +000015493 skl_wm_get_hw_state(dev);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015494 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015495 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015496
15497 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020015498 u64 put_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015499
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015500 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015501 if (WARN_ON(put_domains))
15502 modeset_put_power_domains(dev_priv, put_domains);
15503 }
15504 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015505
Imre Deak8d8c3862017-02-17 17:39:46 +020015506 intel_power_domains_verify_state(dev_priv);
15507
Paulo Zanoni010cf732016-01-19 11:35:48 -020015508 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015509}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015510
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015511void intel_display_resume(struct drm_device *dev)
15512{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015513 struct drm_i915_private *dev_priv = to_i915(dev);
15514 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15515 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015516 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015517
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015518 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030015519 if (state)
15520 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015521
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015522 /*
15523 * This is a cludge because with real atomic modeset mode_config.mutex
15524 * won't be taken. Unfortunately some probed state like
15525 * audio_codec_enable is still protected by mode_config.mutex, so lock
15526 * it here for now.
15527 */
15528 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015529 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015530
Maarten Lankhorst73974892016-08-05 23:28:27 +030015531 while (1) {
15532 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15533 if (ret != -EDEADLK)
15534 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015535
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015536 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015537 }
15538
Maarten Lankhorst73974892016-08-05 23:28:27 +030015539 if (!ret)
15540 ret = __intel_display_resume(dev, state);
15541
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015542 drm_modeset_drop_locks(&ctx);
15543 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015544 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015545
Chris Wilson08536952016-10-14 13:18:18 +010015546 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015547 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson3c5e37f2017-01-15 12:58:25 +000015548 if (state)
15549 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015550}
15551
15552void intel_modeset_gem_init(struct drm_device *dev)
15553{
Chris Wilsondc979972016-05-10 14:10:04 +010015554 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015555
Chris Wilsondc979972016-05-10 14:10:04 +010015556 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015557
Chris Wilson1833b132012-05-09 11:56:28 +010015558 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015559
Chris Wilson1ee8da62016-05-12 12:43:23 +010015560 intel_setup_overlay(dev_priv);
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010015561}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015562
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010015563int intel_connector_register(struct drm_connector *connector)
15564{
15565 struct intel_connector *intel_connector = to_intel_connector(connector);
15566 int ret;
15567
15568 ret = intel_backlight_device_register(intel_connector);
15569 if (ret)
15570 goto err;
15571
15572 return 0;
15573
15574err:
15575 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015576}
15577
Chris Wilsonc191eca2016-06-17 11:40:33 +010015578void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020015579{
Chris Wilsone63d87c2016-06-17 11:40:34 +010015580 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015581
Chris Wilsone63d87c2016-06-17 11:40:34 +010015582 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015583 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015584}
15585
Jesse Barnes79e53942008-11-07 14:24:08 -080015586void intel_modeset_cleanup(struct drm_device *dev)
15587{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015588 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070015589
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015590 flush_work(&dev_priv->atomic_helper.free_work);
15591 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15592
Chris Wilsondc979972016-05-10 14:10:04 +010015593 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020015594
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015595 /*
15596 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015597 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015598 * experience fancy races otherwise.
15599 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015600 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015601
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015602 /*
15603 * Due to the hpd irq storm handling the hotplug work can re-arm the
15604 * poll handlers. Hence disable polling after hpd handling is shut down.
15605 */
Keith Packardf87ea762010-10-03 19:36:26 -070015606 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015607
Jesse Barnes723bfd72010-10-07 16:01:13 -070015608 intel_unregister_dsm_handler();
15609
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015610 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015611
Chris Wilson1630fe72011-07-08 12:22:42 +010015612 /* flush any delayed tasks or pending work */
15613 flush_scheduled_work();
15614
Jesse Barnes79e53942008-11-07 14:24:08 -080015615 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015616
Chris Wilson1ee8da62016-05-12 12:43:23 +010015617 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015618
Chris Wilsondc979972016-05-10 14:10:04 +010015619 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010015620
Tvrtko Ursulin40196442016-12-01 14:16:42 +000015621 intel_teardown_gmbus(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015622}
15623
Chris Wilsondf0e9242010-09-09 16:20:55 +010015624void intel_connector_attach_encoder(struct intel_connector *connector,
15625 struct intel_encoder *encoder)
15626{
15627 connector->encoder = encoder;
15628 drm_mode_connector_attach_encoder(&connector->base,
15629 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015630}
Dave Airlie28d52042009-09-21 14:33:58 +100015631
15632/*
15633 * set vga decode state - true == enable VGA decode
15634 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015635int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100015636{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015637 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015638 u16 gmch_ctrl;
15639
Chris Wilson75fa0412014-02-07 18:37:02 -020015640 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15641 DRM_ERROR("failed to read control word\n");
15642 return -EIO;
15643 }
15644
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015645 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15646 return 0;
15647
Dave Airlie28d52042009-09-21 14:33:58 +100015648 if (state)
15649 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15650 else
15651 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015652
15653 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15654 DRM_ERROR("failed to write control word\n");
15655 return -EIO;
15656 }
15657
Dave Airlie28d52042009-09-21 14:33:58 +100015658 return 0;
15659}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015660
Chris Wilson98a2f412016-10-12 10:05:18 +010015661#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15662
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015663struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015664
15665 u32 power_well_driver;
15666
Chris Wilson63b66e52013-08-08 15:12:06 +020015667 int num_transcoders;
15668
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015669 struct intel_cursor_error_state {
15670 u32 control;
15671 u32 position;
15672 u32 base;
15673 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015674 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015675
15676 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015677 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015678 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015679 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015680 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015681
15682 struct intel_plane_error_state {
15683 u32 control;
15684 u32 stride;
15685 u32 size;
15686 u32 pos;
15687 u32 addr;
15688 u32 surface;
15689 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015690 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015691
15692 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015693 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015694 enum transcoder cpu_transcoder;
15695
15696 u32 conf;
15697
15698 u32 htotal;
15699 u32 hblank;
15700 u32 hsync;
15701 u32 vtotal;
15702 u32 vblank;
15703 u32 vsync;
15704 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015705};
15706
15707struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010015708intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015709{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015710 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015711 int transcoders[] = {
15712 TRANSCODER_A,
15713 TRANSCODER_B,
15714 TRANSCODER_C,
15715 TRANSCODER_EDP,
15716 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015717 int i;
15718
Chris Wilsonc0336662016-05-06 15:40:21 +010015719 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020015720 return NULL;
15721
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015722 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015723 if (error == NULL)
15724 return NULL;
15725
Chris Wilsonc0336662016-05-06 15:40:21 +010015726 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015727 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15728
Damien Lespiau055e3932014-08-18 13:49:10 +010015729 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015730 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015731 __intel_display_power_is_enabled(dev_priv,
15732 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015733 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015734 continue;
15735
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015736 error->cursor[i].control = I915_READ(CURCNTR(i));
15737 error->cursor[i].position = I915_READ(CURPOS(i));
15738 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015739
15740 error->plane[i].control = I915_READ(DSPCNTR(i));
15741 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015742 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015743 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015744 error->plane[i].pos = I915_READ(DSPPOS(i));
15745 }
Chris Wilsonc0336662016-05-06 15:40:21 +010015746 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030015747 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015748 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015749 error->plane[i].surface = I915_READ(DSPSURF(i));
15750 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15751 }
15752
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015753 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015754
Chris Wilsonc0336662016-05-06 15:40:21 +010015755 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030015756 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015757 }
15758
Jani Nikula4d1de972016-03-18 17:05:42 +020015759 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010015760 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030015761 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020015762 error->num_transcoders++; /* Account for eDP. */
15763
15764 for (i = 0; i < error->num_transcoders; i++) {
15765 enum transcoder cpu_transcoder = transcoders[i];
15766
Imre Deakddf9c532013-11-27 22:02:02 +020015767 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015768 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015769 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015770 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015771 continue;
15772
Chris Wilson63b66e52013-08-08 15:12:06 +020015773 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15774
15775 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15776 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15777 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15778 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15779 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15780 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15781 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015782 }
15783
15784 return error;
15785}
15786
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015787#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15788
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015789void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015790intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015791 struct intel_display_error_state *error)
15792{
Chris Wilson5a4c6f12017-02-14 16:46:11 +000015793 struct drm_i915_private *dev_priv = m->i915;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015794 int i;
15795
Chris Wilson63b66e52013-08-08 15:12:06 +020015796 if (!error)
15797 return;
15798
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015799 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010015800 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015801 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015802 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015803 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015804 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015805 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020015806 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015807 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015808 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015809
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015810 err_printf(m, "Plane [%d]:\n", i);
15811 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15812 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000015813 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015814 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15815 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015816 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010015817 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015818 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000015819 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015820 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15821 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015822 }
15823
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015824 err_printf(m, "Cursor [%d]:\n", i);
15825 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15826 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15827 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015828 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015829
15830 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020015831 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015832 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015833 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020015834 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020015835 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15836 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15837 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15838 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15839 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15840 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15841 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15842 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015843}
Chris Wilson98a2f412016-10-12 10:05:18 +010015844
15845#endif