blob: c9ec88acad9c16ed0fb823511ad195b934d03a2c [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
Matt Roper465c1202014-05-29 08:06:54 -070052/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010053static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010054 DRM_FORMAT_C8,
55 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070056 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010057 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070058};
59
60/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010061static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010062 DRM_FORMAT_C8,
63 DRM_FORMAT_RGB565,
64 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070065 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010066 DRM_FORMAT_XRGB2101010,
67 DRM_FORMAT_XBGR2101010,
68};
69
Ben Widawsky714244e2017-08-01 09:58:16 -070070static const uint64_t i9xx_format_modifiers[] = {
71 I915_FORMAT_MOD_X_TILED,
72 DRM_FORMAT_MOD_LINEAR,
73 DRM_FORMAT_MOD_INVALID
74};
75
Damien Lespiau6c0fd452015-05-19 12:29:16 +010076static const uint32_t skl_primary_formats[] = {
77 DRM_FORMAT_C8,
78 DRM_FORMAT_RGB565,
79 DRM_FORMAT_XRGB8888,
80 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010081 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_ABGR8888,
83 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070084 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053085 DRM_FORMAT_YUYV,
86 DRM_FORMAT_YVYU,
87 DRM_FORMAT_UYVY,
88 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070089};
90
Chandra Konduruc0b56ab2018-05-12 03:03:16 +053091static const uint32_t skl_pri_planar_formats[] = {
92 DRM_FORMAT_C8,
93 DRM_FORMAT_RGB565,
94 DRM_FORMAT_XRGB8888,
95 DRM_FORMAT_XBGR8888,
96 DRM_FORMAT_ARGB8888,
97 DRM_FORMAT_ABGR8888,
98 DRM_FORMAT_XRGB2101010,
99 DRM_FORMAT_XBGR2101010,
100 DRM_FORMAT_YUYV,
101 DRM_FORMAT_YVYU,
102 DRM_FORMAT_UYVY,
103 DRM_FORMAT_VYUY,
104 DRM_FORMAT_NV12,
105};
106
Ben Widawsky714244e2017-08-01 09:58:16 -0700107static const uint64_t skl_format_modifiers_noccs[] = {
108 I915_FORMAT_MOD_Yf_TILED,
109 I915_FORMAT_MOD_Y_TILED,
110 I915_FORMAT_MOD_X_TILED,
111 DRM_FORMAT_MOD_LINEAR,
112 DRM_FORMAT_MOD_INVALID
113};
114
115static const uint64_t skl_format_modifiers_ccs[] = {
116 I915_FORMAT_MOD_Yf_TILED_CCS,
117 I915_FORMAT_MOD_Y_TILED_CCS,
118 I915_FORMAT_MOD_Yf_TILED,
119 I915_FORMAT_MOD_Y_TILED,
120 I915_FORMAT_MOD_X_TILED,
121 DRM_FORMAT_MOD_LINEAR,
122 DRM_FORMAT_MOD_INVALID
123};
124
Matt Roper3d7d6512014-06-10 08:28:13 -0700125/* Cursor formats */
126static const uint32_t intel_cursor_formats[] = {
127 DRM_FORMAT_ARGB8888,
128};
129
Ben Widawsky714244e2017-08-01 09:58:16 -0700130static const uint64_t cursor_format_modifiers[] = {
131 DRM_FORMAT_MOD_LINEAR,
132 DRM_FORMAT_MOD_INVALID
133};
134
Jesse Barnesf1f644d2013-06-27 00:39:25 +0300135static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200136 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +0300137static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200138 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +0300139
Chris Wilson24dbf512017-02-15 10:59:18 +0000140static int intel_framebuffer_init(struct intel_framebuffer *ifb,
141 struct drm_i915_gem_object *obj,
142 struct drm_mode_fb_cmd2 *mode_cmd);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200143static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
144static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200145static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200146static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700147 struct intel_link_m_n *m_n,
148 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200149static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200150static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200151static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200152static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200153 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200154static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200155 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200156static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
157static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Nabendu Maiti1c74eea2016-11-29 11:23:14 +0530158static void intel_crtc_init_scalers(struct intel_crtc *crtc,
159 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200160static void skylake_pfit_enable(struct intel_crtc *crtc);
161static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
162static void ironlake_pfit_enable(struct intel_crtc *crtc);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +0300163static void intel_modeset_setup_hw_state(struct drm_device *dev,
164 struct drm_modeset_acquire_ctx *ctx);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200165static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100166
Ma Lingd4906092009-03-18 20:13:27 +0800167struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300168 struct {
169 int min, max;
170 } dot, vco, n, m, m1, m2, p, p1;
171
172 struct {
173 int dot_limit;
174 int p2_slow, p2_fast;
175 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800176};
Jesse Barnes79e53942008-11-07 14:24:08 -0800177
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300178/* returns HPLL frequency in kHz */
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200179int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180{
181 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
182
183 /* Obtain SKU information */
184 mutex_lock(&dev_priv->sb_lock);
185 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
186 CCK_FUSE_HPLL_FREQ_MASK;
187 mutex_unlock(&dev_priv->sb_lock);
188
189 return vco_freq[hpll_freq] * 1000;
190}
191
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200192int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
193 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300194{
195 u32 val;
196 int divider;
197
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300198 mutex_lock(&dev_priv->sb_lock);
199 val = vlv_cck_read(dev_priv, reg);
200 mutex_unlock(&dev_priv->sb_lock);
201
202 divider = val & CCK_FREQUENCY_VALUES;
203
204 WARN((val & CCK_FREQUENCY_STATUS) !=
205 (divider << CCK_FREQUENCY_STATUS_SHIFT),
206 "%s change in progress\n", name);
207
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200208 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
209}
210
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +0200211int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
212 const char *name, u32 reg)
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200213{
214 if (dev_priv->hpll_freq == 0)
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200215 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200216
217 return vlv_get_cck_clock(dev_priv, name, reg,
218 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300219}
220
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300221static void intel_update_czclk(struct drm_i915_private *dev_priv)
222{
Wayne Boyer666a4532015-12-09 12:29:35 -0800223 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300224 return;
225
226 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
227 CCK_CZ_CLOCK_CONTROL);
228
229 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
230}
231
Chris Wilson021357a2010-09-07 20:54:59 +0100232static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200233intel_fdi_link_freq(struct drm_i915_private *dev_priv,
234 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100235{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200236 if (HAS_DDI(dev_priv))
237 return pipe_config->port_clock; /* SPLL */
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200238 else
Chris Wilson58ecd9d2017-11-05 13:49:05 +0000239 return dev_priv->fdi_pll_freq;
Chris Wilson021357a2010-09-07 20:54:59 +0100240}
241
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300242static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400243 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200244 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200245 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400246 .m = { .min = 96, .max = 140 },
247 .m1 = { .min = 18, .max = 26 },
248 .m2 = { .min = 6, .max = 16 },
249 .p = { .min = 4, .max = 128 },
250 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .p2 = { .dot_limit = 165000,
252 .p2_slow = 4, .p2_fast = 2 },
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_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200256 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200257 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200258 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200259 .m = { .min = 96, .max = 140 },
260 .m1 = { .min = 18, .max = 26 },
261 .m2 = { .min = 6, .max = 16 },
262 .p = { .min = 4, .max = 128 },
263 .p1 = { .min = 2, .max = 33 },
264 .p2 = { .dot_limit = 165000,
265 .p2_slow = 4, .p2_fast = 4 },
266};
267
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300268static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400269 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200270 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200271 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400272 .m = { .min = 96, .max = 140 },
273 .m1 = { .min = 18, .max = 26 },
274 .m2 = { .min = 6, .max = 16 },
275 .p = { .min = 4, .max = 128 },
276 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700277 .p2 = { .dot_limit = 165000,
278 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700279};
Eric Anholt273e27c2011-03-30 13:01:10 -0700280
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300281static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .dot = { .min = 20000, .max = 400000 },
283 .vco = { .min = 1400000, .max = 2800000 },
284 .n = { .min = 1, .max = 6 },
285 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100286 .m1 = { .min = 8, .max = 18 },
287 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .p = { .min = 5, .max = 80 },
289 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700290 .p2 = { .dot_limit = 200000,
291 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700292};
293
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300294static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400295 .dot = { .min = 20000, .max = 400000 },
296 .vco = { .min = 1400000, .max = 2800000 },
297 .n = { .min = 1, .max = 6 },
298 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100299 .m1 = { .min = 8, .max = 18 },
300 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400301 .p = { .min = 7, .max = 98 },
302 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700303 .p2 = { .dot_limit = 112000,
304 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700305};
306
Eric Anholt273e27c2011-03-30 13:01:10 -0700307
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300308static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700309 .dot = { .min = 25000, .max = 270000 },
310 .vco = { .min = 1750000, .max = 3500000},
311 .n = { .min = 1, .max = 4 },
312 .m = { .min = 104, .max = 138 },
313 .m1 = { .min = 17, .max = 23 },
314 .m2 = { .min = 5, .max = 11 },
315 .p = { .min = 10, .max = 30 },
316 .p1 = { .min = 1, .max = 3},
317 .p2 = { .dot_limit = 270000,
318 .p2_slow = 10,
319 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800320 },
Keith Packarde4b36692009-06-05 19:22:17 -0700321};
322
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300323static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700324 .dot = { .min = 22000, .max = 400000 },
325 .vco = { .min = 1750000, .max = 3500000},
326 .n = { .min = 1, .max = 4 },
327 .m = { .min = 104, .max = 138 },
328 .m1 = { .min = 16, .max = 23 },
329 .m2 = { .min = 5, .max = 11 },
330 .p = { .min = 5, .max = 80 },
331 .p1 = { .min = 1, .max = 8},
332 .p2 = { .dot_limit = 165000,
333 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700334};
335
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300336static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700337 .dot = { .min = 20000, .max = 115000 },
338 .vco = { .min = 1750000, .max = 3500000 },
339 .n = { .min = 1, .max = 3 },
340 .m = { .min = 104, .max = 138 },
341 .m1 = { .min = 17, .max = 23 },
342 .m2 = { .min = 5, .max = 11 },
343 .p = { .min = 28, .max = 112 },
344 .p1 = { .min = 2, .max = 8 },
345 .p2 = { .dot_limit = 0,
346 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800347 },
Keith Packarde4b36692009-06-05 19:22:17 -0700348};
349
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300350static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700351 .dot = { .min = 80000, .max = 224000 },
352 .vco = { .min = 1750000, .max = 3500000 },
353 .n = { .min = 1, .max = 3 },
354 .m = { .min = 104, .max = 138 },
355 .m1 = { .min = 17, .max = 23 },
356 .m2 = { .min = 5, .max = 11 },
357 .p = { .min = 14, .max = 42 },
358 .p1 = { .min = 2, .max = 6 },
359 .p2 = { .dot_limit = 0,
360 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800361 },
Keith Packarde4b36692009-06-05 19:22:17 -0700362};
363
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300364static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400365 .dot = { .min = 20000, .max = 400000},
366 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700367 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400368 .n = { .min = 3, .max = 6 },
369 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700370 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400371 .m1 = { .min = 0, .max = 0 },
372 .m2 = { .min = 0, .max = 254 },
373 .p = { .min = 5, .max = 80 },
374 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700375 .p2 = { .dot_limit = 200000,
376 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700377};
378
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300379static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400380 .dot = { .min = 20000, .max = 400000 },
381 .vco = { .min = 1700000, .max = 3500000 },
382 .n = { .min = 3, .max = 6 },
383 .m = { .min = 2, .max = 256 },
384 .m1 = { .min = 0, .max = 0 },
385 .m2 = { .min = 0, .max = 254 },
386 .p = { .min = 7, .max = 112 },
387 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700388 .p2 = { .dot_limit = 112000,
389 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700390};
391
Eric Anholt273e27c2011-03-30 13:01:10 -0700392/* Ironlake / Sandybridge
393 *
394 * We calculate clock using (register_value + 2) for N/M1/M2, so here
395 * the range value for them is (actual_value - 2).
396 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300397static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700398 .dot = { .min = 25000, .max = 350000 },
399 .vco = { .min = 1760000, .max = 3510000 },
400 .n = { .min = 1, .max = 5 },
401 .m = { .min = 79, .max = 127 },
402 .m1 = { .min = 12, .max = 22 },
403 .m2 = { .min = 5, .max = 9 },
404 .p = { .min = 5, .max = 80 },
405 .p1 = { .min = 1, .max = 8 },
406 .p2 = { .dot_limit = 225000,
407 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700408};
409
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300410static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700411 .dot = { .min = 25000, .max = 350000 },
412 .vco = { .min = 1760000, .max = 3510000 },
413 .n = { .min = 1, .max = 3 },
414 .m = { .min = 79, .max = 118 },
415 .m1 = { .min = 12, .max = 22 },
416 .m2 = { .min = 5, .max = 9 },
417 .p = { .min = 28, .max = 112 },
418 .p1 = { .min = 2, .max = 8 },
419 .p2 = { .dot_limit = 225000,
420 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800421};
422
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300423static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700424 .dot = { .min = 25000, .max = 350000 },
425 .vco = { .min = 1760000, .max = 3510000 },
426 .n = { .min = 1, .max = 3 },
427 .m = { .min = 79, .max = 127 },
428 .m1 = { .min = 12, .max = 22 },
429 .m2 = { .min = 5, .max = 9 },
430 .p = { .min = 14, .max = 56 },
431 .p1 = { .min = 2, .max = 8 },
432 .p2 = { .dot_limit = 225000,
433 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800434};
435
Eric Anholt273e27c2011-03-30 13:01:10 -0700436/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300437static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700438 .dot = { .min = 25000, .max = 350000 },
439 .vco = { .min = 1760000, .max = 3510000 },
440 .n = { .min = 1, .max = 2 },
441 .m = { .min = 79, .max = 126 },
442 .m1 = { .min = 12, .max = 22 },
443 .m2 = { .min = 5, .max = 9 },
444 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400445 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700446 .p2 = { .dot_limit = 225000,
447 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800448};
449
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300450static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700451 .dot = { .min = 25000, .max = 350000 },
452 .vco = { .min = 1760000, .max = 3510000 },
453 .n = { .min = 1, .max = 3 },
454 .m = { .min = 79, .max = 126 },
455 .m1 = { .min = 12, .max = 22 },
456 .m2 = { .min = 5, .max = 9 },
457 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400458 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700459 .p2 = { .dot_limit = 225000,
460 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800461};
462
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300463static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300464 /*
465 * These are the data rate limits (measured in fast clocks)
466 * since those are the strictest limits we have. The fast
467 * clock and actual rate limits are more relaxed, so checking
468 * them would make no difference.
469 */
470 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200471 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700472 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700473 .m1 = { .min = 2, .max = 3 },
474 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300475 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300476 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700477};
478
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300479static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300480 /*
481 * These are the data rate limits (measured in fast clocks)
482 * since those are the strictest limits we have. The fast
483 * clock and actual rate limits are more relaxed, so checking
484 * them would make no difference.
485 */
486 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200487 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300488 .n = { .min = 1, .max = 1 },
489 .m1 = { .min = 2, .max = 2 },
490 .m2 = { .min = 24 << 22, .max = 175 << 22 },
491 .p1 = { .min = 2, .max = 4 },
492 .p2 = { .p2_slow = 1, .p2_fast = 14 },
493};
494
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300495static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200496 /* FIXME: find real dot limits */
497 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530498 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200499 .n = { .min = 1, .max = 1 },
500 .m1 = { .min = 2, .max = 2 },
501 /* FIXME: find real m2 limits */
502 .m2 = { .min = 2 << 22, .max = 255 << 22 },
503 .p1 = { .min = 2, .max = 4 },
504 .p2 = { .p2_slow = 1, .p2_fast = 20 },
505};
506
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530507static void
Vidya Srinivas6deef9b2018-05-12 03:03:13 +0530508skl_wa_528(struct drm_i915_private *dev_priv, int pipe, bool enable)
509{
510 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
511 return;
512
513 if (enable)
514 I915_WRITE(CHICKEN_PIPESL_1(pipe), HSW_FBCQ_DIS);
515 else
516 I915_WRITE(CHICKEN_PIPESL_1(pipe), 0);
517}
518
519static void
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530520skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
521{
Vidya Srinivas6deef9b2018-05-12 03:03:13 +0530522 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530523 return;
524
525 if (enable)
526 I915_WRITE(CLKGATE_DIS_PSL(pipe),
527 DUPS1_GATING_DIS | DUPS2_GATING_DIS);
528 else
529 I915_WRITE(CLKGATE_DIS_PSL(pipe),
530 I915_READ(CLKGATE_DIS_PSL(pipe)) &
531 ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
532}
533
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200534static bool
Maarten Lankhorst24f28452017-11-22 19:39:01 +0100535needs_modeset(const struct drm_crtc_state *state)
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200536{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200537 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200538}
539
Imre Deakdccbea32015-06-22 23:35:51 +0300540/*
541 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
542 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
543 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
544 * The helpers' return value is the rate of the clock that is fed to the
545 * display engine's pipe which can be the above fast dot clock rate or a
546 * divided-down version of it.
547 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500548/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300549static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800550{
Shaohua Li21778322009-02-23 15:19:16 +0800551 clock->m = clock->m2 + 2;
552 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200553 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300554 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300555 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
556 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300557
558 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800559}
560
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200561static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
562{
563 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
564}
565
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300566static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800567{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200568 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200570 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300571 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300572 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
573 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300574
575 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800576}
577
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300578static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300579{
580 clock->m = clock->m1 * clock->m2;
581 clock->p = clock->p1 * clock->p2;
582 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300583 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300584 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
585 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300586
587 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300588}
589
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300590int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300591{
592 clock->m = clock->m1 * clock->m2;
593 clock->p = clock->p1 * clock->p2;
594 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300595 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300596 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
597 clock->n << 22);
598 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300599
600 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300601}
602
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800603#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Chris Wilsonc38c1452018-02-14 13:49:22 +0000604
605/*
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 * Returns whether the given set of divisors are valid for a given refclk with
607 * the given connectors.
608 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100609static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300610 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300611 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800612{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300613 if (clock->n < limit->n.min || limit->n.max < clock->n)
614 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400616 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800617 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400618 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400620 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100622 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200623 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300624 if (clock->m1 <= clock->m2)
625 INTELPllInvalid("m1 <= m2\n");
626
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100627 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200628 !IS_GEN9_LP(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300629 if (clock->p < limit->p.min || limit->p.max < clock->p)
630 INTELPllInvalid("p out of range\n");
631 if (clock->m < limit->m.min || limit->m.max < clock->m)
632 INTELPllInvalid("m out of range\n");
633 }
634
Jesse Barnes79e53942008-11-07 14:24:08 -0800635 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800637 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
638 * connector, etc., rather than just a single range.
639 */
640 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400641 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800642
643 return true;
644}
645
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300646static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300647i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300648 const struct intel_crtc_state *crtc_state,
649 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800650{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300651 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800652
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300653 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100655 * For LVDS just rely on its current settings for dual-channel.
656 * We haven't figured out how to reliably set up different
657 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100659 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300662 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 } else {
664 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300665 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800666 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300667 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800668 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300669}
670
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200671/*
672 * Returns a set of divisors for the desired target clock with the given
673 * refclk, or FALSE. The returned values represent the clock equation:
674 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
675 *
676 * Target and reference clocks are specified in kHz.
677 *
678 * If match_clock is provided, then best_clock P divider must match the P
679 * divider from @match_clock used for LVDS downclocking.
680 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300682i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300683 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300684 int target, int refclk, struct dpll *match_clock,
685 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686{
687 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300688 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
Akshay Joshi0206e352011-08-16 15:34:10 -0400691 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800692
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300693 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
694
Zhao Yakui42158662009-11-20 11:24:18 +0800695 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
696 clock.m1++) {
697 for (clock.m2 = limit->m2.min;
698 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200699 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800700 break;
701 for (clock.n = limit->n.min;
702 clock.n <= limit->n.max; clock.n++) {
703 for (clock.p1 = limit->p1.min;
704 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800705 int this_err;
706
Imre Deakdccbea32015-06-22 23:35:51 +0300707 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100708 if (!intel_PLL_is_valid(to_i915(dev),
709 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000710 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800711 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800712 if (match_clock &&
713 clock.p != match_clock->p)
714 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800715
716 this_err = abs(clock.dot - target);
717 if (this_err < err) {
718 *best_clock = clock;
719 err = this_err;
720 }
721 }
722 }
723 }
724 }
725
726 return (err != target);
727}
728
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200729/*
730 * Returns a set of divisors for the desired target clock with the given
731 * refclk, or FALSE. The returned values represent the clock equation:
732 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
733 *
734 * Target and reference clocks are specified in kHz.
735 *
736 * If match_clock is provided, then best_clock P divider must match the P
737 * divider from @match_clock used for LVDS downclocking.
738 */
Ma Lingd4906092009-03-18 20:13:27 +0800739static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300740pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200741 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300742 int target, int refclk, struct dpll *match_clock,
743 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200744{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300745 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300746 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200747 int err = target;
748
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200749 memset(best_clock, 0, sizeof(*best_clock));
750
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300751 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
752
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200753 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
754 clock.m1++) {
755 for (clock.m2 = limit->m2.min;
756 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200757 for (clock.n = limit->n.min;
758 clock.n <= limit->n.max; clock.n++) {
759 for (clock.p1 = limit->p1.min;
760 clock.p1 <= limit->p1.max; clock.p1++) {
761 int this_err;
762
Imre Deakdccbea32015-06-22 23:35:51 +0300763 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100764 if (!intel_PLL_is_valid(to_i915(dev),
765 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800766 &clock))
767 continue;
768 if (match_clock &&
769 clock.p != match_clock->p)
770 continue;
771
772 this_err = abs(clock.dot - target);
773 if (this_err < err) {
774 *best_clock = clock;
775 err = this_err;
776 }
777 }
778 }
779 }
780 }
781
782 return (err != target);
783}
784
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200785/*
786 * Returns a set of divisors for the desired target clock with the given
787 * refclk, or FALSE. The returned values represent the clock equation:
788 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200789 *
790 * Target and reference clocks are specified in kHz.
791 *
792 * If match_clock is provided, then best_clock P divider must match the P
793 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200794 */
Ma Lingd4906092009-03-18 20:13:27 +0800795static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300796g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200797 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300798 int target, int refclk, struct dpll *match_clock,
799 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800800{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300801 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300802 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800803 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300804 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400805 /* approximately equals target * 0.00585 */
806 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800807
808 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300809
810 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
811
Ma Lingd4906092009-03-18 20:13:27 +0800812 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200813 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800814 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200815 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800816 for (clock.m1 = limit->m1.max;
817 clock.m1 >= limit->m1.min; clock.m1--) {
818 for (clock.m2 = limit->m2.max;
819 clock.m2 >= limit->m2.min; clock.m2--) {
820 for (clock.p1 = limit->p1.max;
821 clock.p1 >= limit->p1.min; clock.p1--) {
822 int this_err;
823
Imre Deakdccbea32015-06-22 23:35:51 +0300824 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100825 if (!intel_PLL_is_valid(to_i915(dev),
826 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000827 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800828 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000829
830 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800831 if (this_err < err_most) {
832 *best_clock = clock;
833 err_most = this_err;
834 max_n = clock.n;
835 found = true;
836 }
837 }
838 }
839 }
840 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800841 return found;
842}
Ma Lingd4906092009-03-18 20:13:27 +0800843
Imre Deakd5dd62b2015-03-17 11:40:03 +0200844/*
845 * Check if the calculated PLL configuration is more optimal compared to the
846 * best configuration and error found so far. Return the calculated error.
847 */
848static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300849 const struct dpll *calculated_clock,
850 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200851 unsigned int best_error_ppm,
852 unsigned int *error_ppm)
853{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200854 /*
855 * For CHV ignore the error and consider only the P value.
856 * Prefer a bigger P value based on HW requirements.
857 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100858 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200859 *error_ppm = 0;
860
861 return calculated_clock->p > best_clock->p;
862 }
863
Imre Deak24be4e42015-03-17 11:40:04 +0200864 if (WARN_ON_ONCE(!target_freq))
865 return false;
866
Imre Deakd5dd62b2015-03-17 11:40:03 +0200867 *error_ppm = div_u64(1000000ULL *
868 abs(target_freq - calculated_clock->dot),
869 target_freq);
870 /*
871 * Prefer a better P value over a better (smaller) error if the error
872 * is small. Ensure this preference for future configurations too by
873 * setting the error to 0.
874 */
875 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
876 *error_ppm = 0;
877
878 return true;
879 }
880
881 return *error_ppm + 10 < best_error_ppm;
882}
883
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200884/*
885 * Returns a set of divisors for the desired target clock with the given
886 * refclk, or FALSE. The returned values represent the clock equation:
887 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
888 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800889static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300890vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200891 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300892 int target, int refclk, struct dpll *match_clock,
893 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700894{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200895 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300896 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300897 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300898 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300899 /* min update 19.2 MHz */
900 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300901 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700902
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300903 target *= 5; /* fast clock */
904
905 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700906
907 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300908 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300909 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300910 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300911 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300912 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700913 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300914 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200915 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300916
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300917 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
918 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300919
Imre Deakdccbea32015-06-22 23:35:51 +0300920 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300921
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100922 if (!intel_PLL_is_valid(to_i915(dev),
923 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300924 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300925 continue;
926
Imre Deakd5dd62b2015-03-17 11:40:03 +0200927 if (!vlv_PLL_is_optimal(dev, target,
928 &clock,
929 best_clock,
930 bestppm, &ppm))
931 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300932
Imre Deakd5dd62b2015-03-17 11:40:03 +0200933 *best_clock = clock;
934 bestppm = ppm;
935 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700936 }
937 }
938 }
939 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700940
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300941 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700942}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700943
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200944/*
945 * Returns a set of divisors for the desired target clock with the given
946 * refclk, or FALSE. The returned values represent the clock equation:
947 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
948 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300949static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300950chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200951 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300952 int target, int refclk, struct dpll *match_clock,
953 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300954{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200955 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300956 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200957 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300958 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300959 uint64_t m2;
960 int found = false;
961
962 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200963 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300964
965 /*
966 * Based on hardware doc, the n always set to 1, and m1 always
967 * set to 2. If requires to support 200Mhz refclk, we need to
968 * revisit this because n may not 1 anymore.
969 */
970 clock.n = 1, clock.m1 = 2;
971 target *= 5; /* fast clock */
972
973 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
974 for (clock.p2 = limit->p2.p2_fast;
975 clock.p2 >= limit->p2.p2_slow;
976 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200977 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300978
979 clock.p = clock.p1 * clock.p2;
980
981 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
982 clock.n) << 22, refclk * clock.m1);
983
984 if (m2 > INT_MAX/clock.m1)
985 continue;
986
987 clock.m2 = m2;
988
Imre Deakdccbea32015-06-22 23:35:51 +0300989 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300990
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100991 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300992 continue;
993
Imre Deak9ca3ba02015-03-17 11:40:05 +0200994 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
995 best_error_ppm, &error_ppm))
996 continue;
997
998 *best_clock = clock;
999 best_error_ppm = error_ppm;
1000 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001001 }
1002 }
1003
1004 return found;
1005}
1006
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001007bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001008 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001009{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001010 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001011 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001012
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001013 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001014 target_clock, refclk, NULL, best_clock);
1015}
1016
Ville Syrjälä525b9312016-10-31 22:37:02 +02001017bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001018{
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001019 /* Be paranoid as we can arrive here with only partial
1020 * state retrieved from the hardware during setup.
1021 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001022 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001023 * as Haswell has gained clock readout/fastboot support.
1024 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001025 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001026 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001027 *
1028 * FIXME: The intel_crtc->active here should be switched to
1029 * crtc->state->active once we have proper CRTC states wired up
1030 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001031 */
Ville Syrjälä525b9312016-10-31 22:37:02 +02001032 return crtc->active && crtc->base.primary->state->fb &&
1033 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001034}
1035
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001036enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1037 enum pipe pipe)
1038{
Ville Syrjälä98187832016-10-31 22:37:10 +02001039 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001040
Ville Syrjäläe2af48c2016-10-31 22:37:05 +02001041 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001042}
1043
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001044static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1045 enum pipe pipe)
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001046{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001047 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001048 u32 line1, line2;
1049 u32 line_mask;
1050
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001051 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001052 line_mask = DSL_LINEMASK_GEN2;
1053 else
1054 line_mask = DSL_LINEMASK_GEN3;
1055
1056 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001057 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001058 line2 = I915_READ(reg) & line_mask;
1059
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001060 return line1 != line2;
1061}
1062
1063static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1064{
1065 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1066 enum pipe pipe = crtc->pipe;
1067
1068 /* Wait for the display line to settle/start moving */
1069 if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1070 DRM_ERROR("pipe %c scanline %s wait timed out\n",
1071 pipe_name(pipe), onoff(state));
1072}
1073
1074static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1075{
1076 wait_for_pipe_scanline_moving(crtc, false);
1077}
1078
1079static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1080{
1081 wait_for_pipe_scanline_moving(crtc, true);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001082}
1083
Ville Syrjälä4972f702017-11-29 17:37:32 +02001084static void
1085intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001086{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001087 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001088 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001089
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001090 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001091 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001092 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001093
Keith Packardab7ad7f2010-10-03 00:33:06 -07001094 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001095 if (intel_wait_for_register(dev_priv,
1096 reg, I965_PIPECONF_ACTIVE, 0,
1097 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001098 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001099 } else {
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001100 intel_wait_for_pipe_scanline_stopped(crtc);
Keith Packardab7ad7f2010-10-03 00:33:06 -07001101 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001102}
1103
Jesse Barnesb24e7172011-01-04 15:09:30 -08001104/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001105void assert_pll(struct drm_i915_private *dev_priv,
1106 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001107{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001108 u32 val;
1109 bool cur_state;
1110
Ville Syrjälä649636e2015-09-22 19:50:01 +03001111 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001112 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001113 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001114 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001115 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001116}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001117
Jani Nikula23538ef2013-08-27 15:12:22 +03001118/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001119void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001120{
1121 u32 val;
1122 bool cur_state;
1123
Ville Syrjäläa5805162015-05-26 20:42:30 +03001124 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001125 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001126 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001127
1128 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001129 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001130 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001131 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001132}
Jani Nikula23538ef2013-08-27 15:12:22 +03001133
Jesse Barnes040484a2011-01-03 12:14:26 -08001134static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1135 enum pipe pipe, bool state)
1136{
Jesse Barnes040484a2011-01-03 12:14:26 -08001137 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001138 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1139 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001140
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001141 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001142 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001143 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001144 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001145 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001146 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001147 cur_state = !!(val & FDI_TX_ENABLE);
1148 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001149 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001150 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001151 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001152}
1153#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1154#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1155
1156static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1157 enum pipe pipe, bool state)
1158{
Jesse Barnes040484a2011-01-03 12:14:26 -08001159 u32 val;
1160 bool cur_state;
1161
Ville Syrjälä649636e2015-09-22 19:50:01 +03001162 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001163 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001164 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001165 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001166 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001167}
1168#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1169#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1170
1171static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1172 enum pipe pipe)
1173{
Jesse Barnes040484a2011-01-03 12:14:26 -08001174 u32 val;
1175
1176 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001177 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001178 return;
1179
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001180 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001181 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001182 return;
1183
Ville Syrjälä649636e2015-09-22 19:50:01 +03001184 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001185 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 -08001186}
1187
Daniel Vetter55607e82013-06-16 21:42:39 +02001188void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1189 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001190{
Jesse Barnes040484a2011-01-03 12:14:26 -08001191 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001192 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001193
Ville Syrjälä649636e2015-09-22 19:50:01 +03001194 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001195 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001196 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001197 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001198 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001199}
1200
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001201void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001202{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001203 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001204 u32 val;
1205 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001206 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001207
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001208 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001209 return;
1210
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001211 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001212 u32 port_sel;
1213
Imre Deak44cb7342016-08-10 14:07:29 +03001214 pp_reg = PP_CONTROL(0);
1215 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001216
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001217 if (port_sel == PANEL_PORT_SELECT_LVDS)
1218 intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001219 /* XXX: else fix for eDP */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001220 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001221 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001222 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001223 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001224 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001225 pp_reg = PP_CONTROL(0);
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001226 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
Jesse Barnesea0760c2011-01-04 15:09:32 -08001227 }
1228
1229 val = I915_READ(pp_reg);
1230 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001231 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001232 locked = false;
1233
Rob Clarke2c719b2014-12-15 13:56:32 -05001234 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001235 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001236 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001237}
1238
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001239void assert_pipe(struct drm_i915_private *dev_priv,
1240 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001241{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001242 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001243 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1244 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001245 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001246
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001247 /* we keep both pipes enabled on 830 */
1248 if (IS_I830(dev_priv))
Daniel Vetter8e636782012-01-22 01:36:48 +01001249 state = true;
1250
Imre Deak4feed0e2016-02-12 18:55:14 +02001251 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1252 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001253 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001254 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001255
1256 intel_display_power_put(dev_priv, power_domain);
1257 } else {
1258 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001259 }
1260
Rob Clarke2c719b2014-12-15 13:56:32 -05001261 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001262 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001263 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001264}
1265
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001266static void assert_plane(struct intel_plane *plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001267{
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001268 bool cur_state = plane->get_hw_state(plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001269
Rob Clarke2c719b2014-12-15 13:56:32 -05001270 I915_STATE_WARN(cur_state != state,
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001271 "%s assertion failure (expected %s, current %s)\n",
1272 plane->base.name, onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001273}
1274
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001275#define assert_plane_enabled(p) assert_plane(p, true)
1276#define assert_plane_disabled(p) assert_plane(p, false)
Chris Wilson931872f2012-01-16 23:01:13 +00001277
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001278static void assert_planes_disabled(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001279{
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001280 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1281 struct intel_plane *plane;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001283 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1284 assert_plane_disabled(plane);
Jesse Barnes19332d72013-03-28 09:55:38 -07001285}
1286
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001287static void assert_vblank_disabled(struct drm_crtc *crtc)
1288{
Rob Clarke2c719b2014-12-15 13:56:32 -05001289 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001290 drm_crtc_vblank_put(crtc);
1291}
1292
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001293void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1294 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001295{
Jesse Barnes92f25842011-01-04 15:09:34 -08001296 u32 val;
1297 bool enabled;
1298
Ville Syrjälä649636e2015-09-22 19:50:01 +03001299 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001300 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001301 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001302 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1303 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001304}
1305
Keith Packard4e634382011-08-06 10:39:45 -07001306static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001308{
1309 if ((val & DP_PORT_EN) == 0)
1310 return false;
1311
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001312 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001313 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001314 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1315 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001316 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001317 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1318 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001319 } else {
1320 if ((val & DP_PIPE_MASK) != (pipe << 30))
1321 return false;
1322 }
1323 return true;
1324}
1325
Jesse Barnes291906f2011-02-02 12:28:03 -08001326static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001327 enum pipe pipe, i915_reg_t reg,
1328 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001329{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001330 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001331 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001332 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001333 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001334
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001335 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001336 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001337 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001338}
1339
1340static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjälä76203462018-05-14 20:24:21 +03001341 enum pipe pipe, enum port port,
1342 i915_reg_t hdmi_reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001343{
Ville Syrjälä76203462018-05-14 20:24:21 +03001344 enum pipe port_pipe;
1345 bool state;
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001346
Ville Syrjälä76203462018-05-14 20:24:21 +03001347 state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1348
1349 I915_STATE_WARN(state && port_pipe == pipe,
1350 "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1351 port_name(port), pipe_name(pipe));
1352
1353 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1354 "IBX PCH HDMI %c still using transcoder B\n",
1355 port_name(port));
Jesse Barnes291906f2011-02-02 12:28:03 -08001356}
1357
1358static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
1360{
Ville Syrjälä6102a8e2018-05-14 20:24:19 +03001361 enum pipe port_pipe;
Jesse Barnes291906f2011-02-02 12:28:03 -08001362
Keith Packardf0575e92011-07-25 22:12:43 -07001363 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1364 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1365 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001366
Ville Syrjälä6102a8e2018-05-14 20:24:19 +03001367 I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1368 port_pipe == pipe,
1369 "PCH VGA enabled on transcoder %c, should be disabled\n",
1370 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001371
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001372 I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1373 port_pipe == pipe,
1374 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1375 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001376
Ville Syrjälä76203462018-05-14 20:24:21 +03001377 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1378 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1379 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001380}
1381
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001382static void _vlv_enable_pll(struct intel_crtc *crtc,
1383 const struct intel_crtc_state *pipe_config)
1384{
1385 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1386 enum pipe pipe = crtc->pipe;
1387
1388 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1389 POSTING_READ(DPLL(pipe));
1390 udelay(150);
1391
Chris Wilson2c30b432016-06-30 15:32:54 +01001392 if (intel_wait_for_register(dev_priv,
1393 DPLL(pipe),
1394 DPLL_LOCK_VLV,
1395 DPLL_LOCK_VLV,
1396 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001397 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1398}
1399
Ville Syrjäläd288f652014-10-28 13:20:22 +02001400static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001401 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001402{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001403 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001404 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001405
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001406 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001407
Daniel Vetter87442f72013-06-06 00:52:17 +02001408 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001409 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001410
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001411 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1412 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001413
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001414 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1415 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001416}
1417
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001418
1419static void _chv_enable_pll(struct intel_crtc *crtc,
1420 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001421{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001422 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001423 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001424 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001425 u32 tmp;
1426
Ville Syrjäläa5805162015-05-26 20:42:30 +03001427 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001428
1429 /* Enable back the 10bit clock to display controller */
1430 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1431 tmp |= DPIO_DCLKP_EN;
1432 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1433
Ville Syrjälä54433e92015-05-26 20:42:31 +03001434 mutex_unlock(&dev_priv->sb_lock);
1435
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001436 /*
1437 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1438 */
1439 udelay(1);
1440
1441 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001442 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001443
1444 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001445 if (intel_wait_for_register(dev_priv,
1446 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1447 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001448 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001449}
1450
1451static void chv_enable_pll(struct intel_crtc *crtc,
1452 const struct intel_crtc_state *pipe_config)
1453{
1454 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1455 enum pipe pipe = crtc->pipe;
1456
1457 assert_pipe_disabled(dev_priv, pipe);
1458
1459 /* PLL is protected by panel, make sure we can write it */
1460 assert_panel_unlocked(dev_priv, pipe);
1461
1462 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1463 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001464
Ville Syrjäläc2317752016-03-15 16:39:56 +02001465 if (pipe != PIPE_A) {
1466 /*
1467 * WaPixelRepeatModeFixForC0:chv
1468 *
1469 * DPLLCMD is AWOL. Use chicken bits to propagate
1470 * the value from DPLLBMD to either pipe B or C.
1471 */
Ville Syrjälädfa311f2017-09-13 17:08:54 +03001472 I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
Ville Syrjäläc2317752016-03-15 16:39:56 +02001473 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1474 I915_WRITE(CBR4_VLV, 0);
1475 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1476
1477 /*
1478 * DPLLB VGA mode also seems to cause problems.
1479 * We should always have it disabled.
1480 */
1481 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1482 } else {
1483 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1484 POSTING_READ(DPLL_MD(pipe));
1485 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001486}
1487
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001488static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001489{
1490 struct intel_crtc *crtc;
1491 int count = 0;
1492
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001493 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001494 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001495 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1496 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001497
1498 return count;
1499}
1500
Ville Syrjälä939994d2017-09-13 17:08:56 +03001501static void i9xx_enable_pll(struct intel_crtc *crtc,
1502 const struct intel_crtc_state *crtc_state)
Daniel Vetter87442f72013-06-06 00:52:17 +02001503{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001504 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001505 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjälä939994d2017-09-13 17:08:56 +03001506 u32 dpll = crtc_state->dpll_hw_state.dpll;
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001507 int i;
Daniel Vetter87442f72013-06-06 00:52:17 +02001508
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001509 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001510
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001511 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001512 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001513 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001514
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001515 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001516 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001517 /*
1518 * It appears to be important that we don't enable this
1519 * for the current pipe before otherwise configuring the
1520 * PLL. No idea how this should be handled if multiple
1521 * DVO outputs are enabled simultaneosly.
1522 */
1523 dpll |= DPLL_DVO_2X_MODE;
1524 I915_WRITE(DPLL(!crtc->pipe),
1525 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1526 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001527
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001528 /*
1529 * Apparently we need to have VGA mode enabled prior to changing
1530 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1531 * dividers, even though the register value does change.
1532 */
1533 I915_WRITE(reg, 0);
1534
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001535 I915_WRITE(reg, dpll);
1536
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001537 /* Wait for the clocks to stabilize. */
1538 POSTING_READ(reg);
1539 udelay(150);
1540
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001541 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001542 I915_WRITE(DPLL_MD(crtc->pipe),
Ville Syrjälä939994d2017-09-13 17:08:56 +03001543 crtc_state->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001544 } else {
1545 /* The pixel multiplier can only be updated once the
1546 * DPLL is enabled and the clocks are stable.
1547 *
1548 * So write it again.
1549 */
1550 I915_WRITE(reg, dpll);
1551 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001552
1553 /* We do this three times for luck */
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001554 for (i = 0; i < 3; i++) {
1555 I915_WRITE(reg, dpll);
1556 POSTING_READ(reg);
1557 udelay(150); /* wait for warmup */
1558 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001559}
1560
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001561static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001562{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001563 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001564 enum pipe pipe = crtc->pipe;
1565
1566 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001567 if (IS_I830(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001568 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001569 !intel_num_dvo_pipes(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001570 I915_WRITE(DPLL(PIPE_B),
1571 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1572 I915_WRITE(DPLL(PIPE_A),
1573 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1574 }
1575
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001576 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001577 if (IS_I830(dev_priv))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001578 return;
1579
1580 /* Make sure the pipe isn't still relying on us */
1581 assert_pipe_disabled(dev_priv, pipe);
1582
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001583 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001584 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001585}
1586
Jesse Barnesf6071162013-10-01 10:41:38 -07001587static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1588{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001589 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001590
1591 /* Make sure the pipe isn't still relying on us */
1592 assert_pipe_disabled(dev_priv, pipe);
1593
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001594 val = DPLL_INTEGRATED_REF_CLK_VLV |
1595 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1596 if (pipe != PIPE_A)
1597 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1598
Jesse Barnesf6071162013-10-01 10:41:38 -07001599 I915_WRITE(DPLL(pipe), val);
1600 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001601}
1602
1603static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1604{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001605 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001606 u32 val;
1607
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001608 /* Make sure the pipe isn't still relying on us */
1609 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001610
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001611 val = DPLL_SSC_REF_CLK_CHV |
1612 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001613 if (pipe != PIPE_A)
1614 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001615
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001616 I915_WRITE(DPLL(pipe), val);
1617 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001618
Ville Syrjäläa5805162015-05-26 20:42:30 +03001619 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001620
1621 /* Disable 10bit clock to display controller */
1622 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1623 val &= ~DPIO_DCLKP_EN;
1624 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1625
Ville Syrjäläa5805162015-05-26 20:42:30 +03001626 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001627}
1628
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001629void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001630 struct intel_digital_port *dport,
1631 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001632{
1633 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001634 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001635
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02001636 switch (dport->base.port) {
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001637 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001638 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001639 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001640 break;
1641 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001642 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001643 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001644 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001645 break;
1646 case PORT_D:
1647 port_mask = DPLL_PORTD_READY_MASK;
1648 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001649 break;
1650 default:
1651 BUG();
1652 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001653
Chris Wilson370004d2016-06-30 15:32:56 +01001654 if (intel_wait_for_register(dev_priv,
1655 dpll_reg, port_mask, expected_mask,
1656 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001657 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02001658 port_name(dport->base.port),
1659 I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001660}
1661
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001662static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1663 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001664{
Ville Syrjälä98187832016-10-31 22:37:10 +02001665 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1666 pipe);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001667 i915_reg_t reg;
1668 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001669
Jesse Barnes040484a2011-01-03 12:14:26 -08001670 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001671 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001672
1673 /* FDI must be feeding us bits for PCH ports */
1674 assert_fdi_tx_enabled(dev_priv, pipe);
1675 assert_fdi_rx_enabled(dev_priv, pipe);
1676
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001677 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001678 /* Workaround: Set the timing override bit before enabling the
1679 * pch transcoder. */
1680 reg = TRANS_CHICKEN2(pipe);
1681 val = I915_READ(reg);
1682 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1683 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001684 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001685
Daniel Vetterab9412b2013-05-03 11:49:46 +02001686 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001687 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001688 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001689
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001690 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001691 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001692 * Make the BPC in transcoder be consistent with
1693 * that in pipeconf reg. For HDMI we must use 8bpc
1694 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001695 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001696 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001697 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001698 val |= PIPECONF_8BPC;
1699 else
1700 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001701 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001702
1703 val &= ~TRANS_INTERLACE_MASK;
1704 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001705 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001706 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001707 val |= TRANS_LEGACY_INTERLACED_ILK;
1708 else
1709 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001710 else
1711 val |= TRANS_PROGRESSIVE;
1712
Jesse Barnes040484a2011-01-03 12:14:26 -08001713 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001714 if (intel_wait_for_register(dev_priv,
1715 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1716 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001717 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001718}
1719
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001720static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001721 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001722{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001723 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001724
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001725 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001726 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001727 assert_fdi_rx_enabled(dev_priv, PIPE_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001728
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001729 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001730 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001731 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001732 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001733
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001734 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001735 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001736
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001737 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1738 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001739 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001740 else
1741 val |= TRANS_PROGRESSIVE;
1742
Daniel Vetterab9412b2013-05-03 11:49:46 +02001743 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001744 if (intel_wait_for_register(dev_priv,
1745 LPT_TRANSCONF,
1746 TRANS_STATE_ENABLE,
1747 TRANS_STATE_ENABLE,
1748 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001749 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001750}
1751
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001752static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1753 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001754{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001755 i915_reg_t reg;
1756 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001757
1758 /* FDI relies on the transcoder */
1759 assert_fdi_tx_disabled(dev_priv, pipe);
1760 assert_fdi_rx_disabled(dev_priv, pipe);
1761
Jesse Barnes291906f2011-02-02 12:28:03 -08001762 /* Ports must be off as well */
1763 assert_pch_ports_disabled(dev_priv, pipe);
1764
Daniel Vetterab9412b2013-05-03 11:49:46 +02001765 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001766 val = I915_READ(reg);
1767 val &= ~TRANS_ENABLE;
1768 I915_WRITE(reg, val);
1769 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001770 if (intel_wait_for_register(dev_priv,
1771 reg, TRANS_STATE_ENABLE, 0,
1772 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001773 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001774
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001775 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001776 /* Workaround: Clear the timing override chicken bit again. */
1777 reg = TRANS_CHICKEN2(pipe);
1778 val = I915_READ(reg);
1779 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1780 I915_WRITE(reg, val);
1781 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001782}
1783
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001784void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001785{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001786 u32 val;
1787
Daniel Vetterab9412b2013-05-03 11:49:46 +02001788 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001789 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001790 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001791 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001792 if (intel_wait_for_register(dev_priv,
1793 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1794 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001795 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001796
1797 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001798 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001799 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001800 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001801}
1802
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001803enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
Ville Syrjälä65f21302016-10-14 20:02:53 +03001804{
1805 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1806
Ville Syrjälä65f21302016-10-14 20:02:53 +03001807 if (HAS_PCH_LPT(dev_priv))
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001808 return PIPE_A;
Ville Syrjälä65f21302016-10-14 20:02:53 +03001809 else
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001810 return crtc->pipe;
Ville Syrjälä65f21302016-10-14 20:02:53 +03001811}
1812
Ville Syrjälä4972f702017-11-29 17:37:32 +02001813static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001814{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001815 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
1816 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1817 enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
Paulo Zanoni03722642014-01-17 13:51:09 -02001818 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001819 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001820 u32 val;
1821
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001822 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1823
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001824 assert_planes_disabled(crtc);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001825
Jesse Barnesb24e7172011-01-04 15:09:30 -08001826 /*
1827 * A pipe without a PLL won't actually be able to drive bits from
1828 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1829 * need the check.
1830 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001831 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001832 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001833 assert_dsi_pll_enabled(dev_priv);
1834 else
1835 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001836 } else {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001837 if (new_crtc_state->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001838 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001839 assert_fdi_rx_pll_enabled(dev_priv,
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001840 intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001841 assert_fdi_tx_pll_enabled(dev_priv,
1842 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001843 }
1844 /* FIXME: assert CPU port conditions for SNB+ */
1845 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001846
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001847 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001848 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001849 if (val & PIPECONF_ENABLE) {
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001850 /* we keep both pipes enabled on 830 */
1851 WARN_ON(!IS_I830(dev_priv));
Chris Wilson00d70b12011-03-17 07:18:29 +00001852 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001853 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001854
1855 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001856 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001857
1858 /*
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001859 * Until the pipe starts PIPEDSL reads will return a stale value,
1860 * which causes an apparent vblank timestamp jump when PIPEDSL
1861 * resets to its proper value. That also messes up the frame count
1862 * when it's derived from the timestamps. So let's wait for the
1863 * pipe to start properly before we call drm_crtc_vblank_on()
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001864 */
Ville Syrjälä4972f702017-11-29 17:37:32 +02001865 if (dev_priv->drm.max_vblank_count == 0)
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001866 intel_wait_for_pipe_scanline_moving(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001867}
1868
Ville Syrjälä4972f702017-11-29 17:37:32 +02001869static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001870{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001871 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Chris Wilsonfac5e232016-07-04 11:34:36 +01001872 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä4972f702017-11-29 17:37:32 +02001873 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001874 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001875 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001876 u32 val;
1877
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001878 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1879
Jesse Barnesb24e7172011-01-04 15:09:30 -08001880 /*
1881 * Make sure planes won't keep trying to pump pixels to us,
1882 * or we might hang the display.
1883 */
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001884 assert_planes_disabled(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001885
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001886 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001887 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001888 if ((val & PIPECONF_ENABLE) == 0)
1889 return;
1890
Ville Syrjälä67adc642014-08-15 01:21:57 +03001891 /*
1892 * Double wide has implications for planes
1893 * so best keep it disabled when not needed.
1894 */
Ville Syrjälä4972f702017-11-29 17:37:32 +02001895 if (old_crtc_state->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03001896 val &= ~PIPECONF_DOUBLE_WIDE;
1897
1898 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001899 if (!IS_I830(dev_priv))
Ville Syrjälä67adc642014-08-15 01:21:57 +03001900 val &= ~PIPECONF_ENABLE;
1901
1902 I915_WRITE(reg, val);
1903 if ((val & PIPECONF_ENABLE) == 0)
Ville Syrjälä4972f702017-11-29 17:37:32 +02001904 intel_wait_for_pipe_off(old_crtc_state);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001905}
1906
Ville Syrjälä832be822016-01-12 21:08:33 +02001907static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1908{
1909 return IS_GEN2(dev_priv) ? 2048 : 4096;
1910}
1911
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001912static unsigned int
1913intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001914{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001915 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1916 unsigned int cpp = fb->format->cpp[plane];
1917
1918 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07001919 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001920 return cpp;
1921 case I915_FORMAT_MOD_X_TILED:
1922 if (IS_GEN2(dev_priv))
1923 return 128;
1924 else
1925 return 512;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001926 case I915_FORMAT_MOD_Y_TILED_CCS:
1927 if (plane == 1)
1928 return 128;
1929 /* fall through */
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001930 case I915_FORMAT_MOD_Y_TILED:
1931 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
1932 return 128;
1933 else
1934 return 512;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001935 case I915_FORMAT_MOD_Yf_TILED_CCS:
1936 if (plane == 1)
1937 return 128;
1938 /* fall through */
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001939 case I915_FORMAT_MOD_Yf_TILED:
1940 switch (cpp) {
1941 case 1:
1942 return 64;
1943 case 2:
1944 case 4:
1945 return 128;
1946 case 8:
1947 case 16:
1948 return 256;
1949 default:
1950 MISSING_CASE(cpp);
1951 return cpp;
1952 }
1953 break;
1954 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001955 MISSING_CASE(fb->modifier);
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001956 return cpp;
1957 }
1958}
1959
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001960static unsigned int
1961intel_tile_height(const struct drm_framebuffer *fb, int plane)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08001962{
Ben Widawsky2f075562017-03-24 14:29:48 -07001963 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä832be822016-01-12 21:08:33 +02001964 return 1;
1965 else
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001966 return intel_tile_size(to_i915(fb->dev)) /
1967 intel_tile_width_bytes(fb, plane);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001968}
1969
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001970/* Return the tile dimensions in pixel units */
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001971static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001972 unsigned int *tile_width,
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001973 unsigned int *tile_height)
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001974{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001975 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
1976 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001977
1978 *tile_width = tile_width_bytes / cpp;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001979 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001980}
1981
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001982unsigned int
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001983intel_fb_align_height(const struct drm_framebuffer *fb,
1984 int plane, unsigned int height)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001985{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001986 unsigned int tile_height = intel_tile_height(fb, plane);
Ville Syrjälä832be822016-01-12 21:08:33 +02001987
1988 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08001989}
1990
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02001991unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
1992{
1993 unsigned int size = 0;
1994 int i;
1995
1996 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
1997 size += rot_info->plane[i].width * rot_info->plane[i].height;
1998
1999 return size;
2000}
2001
Daniel Vetter75c82a52015-10-14 16:51:04 +02002002static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002003intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2004 const struct drm_framebuffer *fb,
2005 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002006{
Chris Wilson7b92c042017-01-14 00:28:26 +00002007 view->type = I915_GGTT_VIEW_NORMAL;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002008 if (drm_rotation_90_or_270(rotation)) {
Chris Wilson7b92c042017-01-14 00:28:26 +00002009 view->type = I915_GGTT_VIEW_ROTATED;
Chris Wilson8bab11932017-01-14 00:28:25 +00002010 view->rotated = to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002011 }
2012}
2013
Ville Syrjäläfabac482017-03-27 21:55:43 +03002014static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2015{
2016 if (IS_I830(dev_priv))
2017 return 16 * 1024;
2018 else if (IS_I85X(dev_priv))
2019 return 256;
Ville Syrjäläd9e15512017-03-27 21:55:45 +03002020 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2021 return 32;
Ville Syrjäläfabac482017-03-27 21:55:43 +03002022 else
2023 return 4 * 1024;
2024}
2025
Ville Syrjälä603525d2016-01-12 21:08:37 +02002026static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002027{
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00002028 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002029 return 256 * 1024;
Jani Nikulac0f86832016-12-07 12:13:04 +02002030 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002031 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002032 return 128 * 1024;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00002033 else if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002034 return 4 * 1024;
2035 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002036 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002037}
2038
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002039static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2040 int plane)
Ville Syrjälä603525d2016-01-12 21:08:37 +02002041{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002042 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2043
Ville Syrjäläb90c1ee2017-03-07 21:42:07 +02002044 /* AUX_DIST needs only 4K alignment */
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002045 if (plane == 1)
Ville Syrjäläb90c1ee2017-03-07 21:42:07 +02002046 return 4096;
2047
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002048 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07002049 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä603525d2016-01-12 21:08:37 +02002050 return intel_linear_alignment(dev_priv);
2051 case I915_FORMAT_MOD_X_TILED:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002052 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä603525d2016-01-12 21:08:37 +02002053 return 256 * 1024;
2054 return 0;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002055 case I915_FORMAT_MOD_Y_TILED_CCS:
2056 case I915_FORMAT_MOD_Yf_TILED_CCS:
Ville Syrjälä603525d2016-01-12 21:08:37 +02002057 case I915_FORMAT_MOD_Y_TILED:
2058 case I915_FORMAT_MOD_Yf_TILED:
2059 return 1 * 1024 * 1024;
2060 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002061 MISSING_CASE(fb->modifier);
Ville Syrjälä603525d2016-01-12 21:08:37 +02002062 return 0;
2063 }
2064}
2065
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002066static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2067{
2068 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2069 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2070
Ville Syrjälä32febd92018-02-21 18:02:33 +02002071 return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002072}
2073
Chris Wilson058d88c2016-08-15 10:49:06 +01002074struct i915_vma *
Chris Wilson59354852018-02-20 13:42:06 +00002075intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2076 unsigned int rotation,
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002077 bool uses_fence,
Chris Wilson59354852018-02-20 13:42:06 +00002078 unsigned long *out_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002079{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002080 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002081 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002082 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002083 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002084 struct i915_vma *vma;
Chris Wilson59354852018-02-20 13:42:06 +00002085 unsigned int pinctl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002086 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002087
Matt Roperebcdd392014-07-09 16:22:11 -07002088 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2089
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002090 alignment = intel_surf_alignment(fb, 0);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002091
Ville Syrjälä3465c582016-02-15 22:54:43 +02002092 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002093
Chris Wilson693db182013-03-05 14:52:39 +00002094 /* Note that the w/a also requires 64 PTE of padding following the
2095 * bo. We currently fill all unused PTE with the shadow page and so
2096 * we should always have valid PTE following the scanout preventing
2097 * the VT-d warning.
2098 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002099 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002100 alignment = 256 * 1024;
2101
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002102 /*
2103 * Global gtt pte registers are special registers which actually forward
2104 * writes to a chunk of system memory. Which means that there is no risk
2105 * that the register values disappear as soon as we call
2106 * intel_runtime_pm_put(), so it is correct to wrap only the
2107 * pin/unpin/fence and not more.
2108 */
2109 intel_runtime_pm_get(dev_priv);
2110
Daniel Vetter9db529a2017-08-08 10:08:28 +02002111 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2112
Chris Wilson59354852018-02-20 13:42:06 +00002113 pinctl = 0;
2114
2115 /* Valleyview is definitely limited to scanning out the first
2116 * 512MiB. Lets presume this behaviour was inherited from the
2117 * g4x display engine and that all earlier gen are similarly
2118 * limited. Testing suggests that it is a little more
2119 * complicated than this. For example, Cherryview appears quite
2120 * happy to scanout from anywhere within its global aperture.
2121 */
2122 if (HAS_GMCH_DISPLAY(dev_priv))
2123 pinctl |= PIN_MAPPABLE;
2124
2125 vma = i915_gem_object_pin_to_display_plane(obj,
2126 alignment, &view, pinctl);
Chris Wilson49ef5292016-08-18 17:17:00 +01002127 if (IS_ERR(vma))
2128 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002129
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002130 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002131 int ret;
2132
Chris Wilson49ef5292016-08-18 17:17:00 +01002133 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2134 * fence, whereas 965+ only requires a fence if using
2135 * framebuffer compression. For simplicity, we always, when
2136 * possible, install a fence as the cost is not that onerous.
2137 *
2138 * If we fail to fence the tiled scanout, then either the
2139 * modeset will reject the change (which is highly unlikely as
2140 * the affected systems, all but one, do not have unmappable
2141 * space) or we will not be able to enable full powersaving
2142 * techniques (also likely not to apply due to various limits
2143 * FBC and the like impose on the size of the buffer, which
2144 * presumably we violated anyway with this unmappable buffer).
2145 * Anyway, it is presumably better to stumble onwards with
2146 * something and try to run the system in a "less than optimal"
2147 * mode that matches the user configuration.
2148 */
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002149 ret = i915_vma_pin_fence(vma);
2150 if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
Chris Wilson75097022018-03-05 10:33:12 +00002151 i915_gem_object_unpin_from_display_plane(vma);
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002152 vma = ERR_PTR(ret);
2153 goto err;
2154 }
2155
2156 if (ret == 0 && vma->fence)
Chris Wilson59354852018-02-20 13:42:06 +00002157 *out_flags |= PLANE_HAS_FENCE;
Vivek Kasireddy98072162015-10-29 18:54:38 -07002158 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002159
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002160 i915_vma_get(vma);
Chris Wilson49ef5292016-08-18 17:17:00 +01002161err:
Daniel Vetter9db529a2017-08-08 10:08:28 +02002162 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2163
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002164 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002165 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002166}
2167
Chris Wilson59354852018-02-20 13:42:06 +00002168void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002169{
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002170 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002171
Chris Wilson59354852018-02-20 13:42:06 +00002172 if (flags & PLANE_HAS_FENCE)
2173 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002174 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002175 i915_vma_put(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002176}
2177
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002178static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2179 unsigned int rotation)
2180{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002181 if (drm_rotation_90_or_270(rotation))
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002182 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2183 else
2184 return fb->pitches[plane];
2185}
2186
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002187/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002188 * Convert the x/y offsets into a linear offset.
2189 * Only valid with 0/180 degree rotation, which is fine since linear
2190 * offset is only used with linear buffers on pre-hsw and tiled buffers
2191 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2192 */
2193u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002194 const struct intel_plane_state *state,
2195 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002196{
Ville Syrjälä29490562016-01-20 18:02:50 +02002197 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002198 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002199 unsigned int pitch = fb->pitches[plane];
2200
2201 return y * pitch + x * cpp;
2202}
2203
2204/*
2205 * Add the x/y offsets derived from fb->offsets[] to the user
2206 * specified plane src x/y offsets. The resulting x/y offsets
2207 * specify the start of scanout from the beginning of the gtt mapping.
2208 */
2209void intel_add_fb_offsets(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
2213{
Ville Syrjälä29490562016-01-20 18:02:50 +02002214 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2215 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002216
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002217 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002218 *x += intel_fb->rotated[plane].x;
2219 *y += intel_fb->rotated[plane].y;
2220 } else {
2221 *x += intel_fb->normal[plane].x;
2222 *y += intel_fb->normal[plane].y;
2223 }
2224}
2225
Ville Syrjälä303ba692017-08-24 22:10:49 +03002226static u32 __intel_adjust_tile_offset(int *x, int *y,
2227 unsigned int tile_width,
2228 unsigned int tile_height,
2229 unsigned int tile_size,
2230 unsigned int pitch_tiles,
2231 u32 old_offset,
2232 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002233{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002234 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002235 unsigned int tiles;
2236
2237 WARN_ON(old_offset & (tile_size - 1));
2238 WARN_ON(new_offset & (tile_size - 1));
2239 WARN_ON(new_offset > old_offset);
2240
2241 tiles = (old_offset - new_offset) / tile_size;
2242
2243 *y += tiles / pitch_tiles * tile_height;
2244 *x += tiles % pitch_tiles * tile_width;
2245
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002246 /* minimize x in case it got needlessly big */
2247 *y += *x / pitch_pixels * tile_height;
2248 *x %= pitch_pixels;
2249
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002250 return new_offset;
2251}
2252
Ville Syrjälä303ba692017-08-24 22:10:49 +03002253static u32 _intel_adjust_tile_offset(int *x, int *y,
2254 const struct drm_framebuffer *fb, int plane,
2255 unsigned int rotation,
2256 u32 old_offset, u32 new_offset)
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002257{
Ville Syrjälä303ba692017-08-24 22:10:49 +03002258 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä353c8592016-12-14 23:30:57 +02002259 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002260 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2261
2262 WARN_ON(new_offset > old_offset);
2263
Ben Widawsky2f075562017-03-24 14:29:48 -07002264 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002265 unsigned int tile_size, tile_width, tile_height;
2266 unsigned int pitch_tiles;
2267
2268 tile_size = intel_tile_size(dev_priv);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002269 intel_tile_dims(fb, plane, &tile_width, &tile_height);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002270
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002271 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002272 pitch_tiles = pitch / tile_height;
2273 swap(tile_width, tile_height);
2274 } else {
2275 pitch_tiles = pitch / (tile_width * cpp);
2276 }
2277
Ville Syrjälä303ba692017-08-24 22:10:49 +03002278 __intel_adjust_tile_offset(x, y, tile_width, tile_height,
2279 tile_size, pitch_tiles,
2280 old_offset, new_offset);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002281 } else {
2282 old_offset += *y * pitch + *x * cpp;
2283
2284 *y = (old_offset - new_offset) / pitch;
2285 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2286 }
2287
2288 return new_offset;
2289}
2290
2291/*
Ville Syrjälä303ba692017-08-24 22:10:49 +03002292 * Adjust the tile offset by moving the difference into
2293 * the x/y offsets.
2294 */
2295static u32 intel_adjust_tile_offset(int *x, int *y,
2296 const struct intel_plane_state *state, int plane,
2297 u32 old_offset, u32 new_offset)
2298{
2299 return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
2300 state->base.rotation,
2301 old_offset, new_offset);
2302}
2303
2304/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002305 * Computes the linear offset to the base tile and adjusts
2306 * x, y. bytes per pixel is assumed to be a power-of-two.
2307 *
2308 * In the 90/270 rotated case, x and y are assumed
2309 * to be already rotated to match the rotated GTT view, and
2310 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002311 *
2312 * This function is used when computing the derived information
2313 * under intel_framebuffer, so using any of that information
2314 * here is not allowed. Anything under drm_framebuffer can be
2315 * used. This is why the user has to pass in the pitch since it
2316 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002317 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002318static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2319 int *x, int *y,
2320 const struct drm_framebuffer *fb, int plane,
2321 unsigned int pitch,
2322 unsigned int rotation,
2323 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002324{
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002325 uint64_t fb_modifier = fb->modifier;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002326 unsigned int cpp = fb->format->cpp[plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002327 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002328
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002329 if (alignment)
2330 alignment--;
2331
Ben Widawsky2f075562017-03-24 14:29:48 -07002332 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002333 unsigned int tile_size, tile_width, tile_height;
2334 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002335
Ville Syrjäläd8433102016-01-12 21:08:35 +02002336 tile_size = intel_tile_size(dev_priv);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002337 intel_tile_dims(fb, plane, &tile_width, &tile_height);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002338
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002339 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002340 pitch_tiles = pitch / tile_height;
2341 swap(tile_width, tile_height);
2342 } else {
2343 pitch_tiles = pitch / (tile_width * cpp);
2344 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002345
Ville Syrjäläd8433102016-01-12 21:08:35 +02002346 tile_rows = *y / tile_height;
2347 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002348
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002349 tiles = *x / tile_width;
2350 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002351
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002352 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2353 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002354
Ville Syrjälä303ba692017-08-24 22:10:49 +03002355 __intel_adjust_tile_offset(x, y, tile_width, tile_height,
2356 tile_size, pitch_tiles,
2357 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002358 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002359 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002360 offset_aligned = offset & ~alignment;
2361
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002362 *y = (offset & alignment) / pitch;
2363 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002364 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002365
2366 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002367}
2368
Ville Syrjälä6687c902015-09-15 13:16:41 +03002369u32 intel_compute_tile_offset(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002370 const struct intel_plane_state *state,
2371 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002372{
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002373 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2374 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
Ville Syrjälä29490562016-01-20 18:02:50 +02002375 const struct drm_framebuffer *fb = state->base.fb;
2376 unsigned int rotation = state->base.rotation;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002377 int pitch = intel_fb_pitch(fb, plane, rotation);
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002378 u32 alignment;
2379
2380 if (intel_plane->id == PLANE_CURSOR)
2381 alignment = intel_cursor_alignment(dev_priv);
2382 else
2383 alignment = intel_surf_alignment(fb, plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002384
2385 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2386 rotation, alignment);
2387}
2388
Ville Syrjälä303ba692017-08-24 22:10:49 +03002389/* Convert the fb->offset[] into x/y offsets */
2390static int intel_fb_offset_to_xy(int *x, int *y,
2391 const struct drm_framebuffer *fb, int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002392{
Ville Syrjälä303ba692017-08-24 22:10:49 +03002393 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002394
Ville Syrjälä303ba692017-08-24 22:10:49 +03002395 if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
2396 fb->offsets[plane] % intel_tile_size(dev_priv))
2397 return -EINVAL;
2398
2399 *x = 0;
2400 *y = 0;
2401
2402 _intel_adjust_tile_offset(x, y,
2403 fb, plane, DRM_MODE_ROTATE_0,
2404 fb->offsets[plane], 0);
2405
2406 return 0;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002407}
2408
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002409static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2410{
2411 switch (fb_modifier) {
2412 case I915_FORMAT_MOD_X_TILED:
2413 return I915_TILING_X;
2414 case I915_FORMAT_MOD_Y_TILED:
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002415 case I915_FORMAT_MOD_Y_TILED_CCS:
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002416 return I915_TILING_Y;
2417 default:
2418 return I915_TILING_NONE;
2419 }
2420}
2421
Ville Syrjälä16af25f2018-01-19 16:41:52 +02002422/*
2423 * From the Sky Lake PRM:
2424 * "The Color Control Surface (CCS) contains the compression status of
2425 * the cache-line pairs. The compression state of the cache-line pair
2426 * is specified by 2 bits in the CCS. Each CCS cache-line represents
2427 * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2428 * cache-line-pairs. CCS is always Y tiled."
2429 *
2430 * Since cache line pairs refers to horizontally adjacent cache lines,
2431 * each cache line in the CCS corresponds to an area of 32x16 cache
2432 * lines on the main surface. Since each pixel is 4 bytes, this gives
2433 * us a ratio of one byte in the CCS for each 8x16 pixels in the
2434 * main surface.
2435 */
Ville Syrjäläbbfb6ce2017-08-01 09:58:12 -07002436static const struct drm_format_info ccs_formats[] = {
2437 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2438 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2439 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2440 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2441};
2442
2443static const struct drm_format_info *
2444lookup_format_info(const struct drm_format_info formats[],
2445 int num_formats, u32 format)
2446{
2447 int i;
2448
2449 for (i = 0; i < num_formats; i++) {
2450 if (formats[i].format == format)
2451 return &formats[i];
2452 }
2453
2454 return NULL;
2455}
2456
2457static const struct drm_format_info *
2458intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2459{
2460 switch (cmd->modifier[0]) {
2461 case I915_FORMAT_MOD_Y_TILED_CCS:
2462 case I915_FORMAT_MOD_Yf_TILED_CCS:
2463 return lookup_format_info(ccs_formats,
2464 ARRAY_SIZE(ccs_formats),
2465 cmd->pixel_format);
2466 default:
2467 return NULL;
2468 }
2469}
2470
Ville Syrjälä6687c902015-09-15 13:16:41 +03002471static int
2472intel_fill_fb_info(struct drm_i915_private *dev_priv,
2473 struct drm_framebuffer *fb)
2474{
2475 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2476 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2477 u32 gtt_offset_rotated = 0;
2478 unsigned int max_size = 0;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +02002479 int i, num_planes = fb->format->num_planes;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002480 unsigned int tile_size = intel_tile_size(dev_priv);
2481
2482 for (i = 0; i < num_planes; i++) {
2483 unsigned int width, height;
2484 unsigned int cpp, size;
2485 u32 offset;
2486 int x, y;
Ville Syrjälä303ba692017-08-24 22:10:49 +03002487 int ret;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002488
Ville Syrjälä353c8592016-12-14 23:30:57 +02002489 cpp = fb->format->cpp[i];
Ville Syrjälä145fcb12016-11-18 21:53:06 +02002490 width = drm_framebuffer_plane_width(fb->width, fb, i);
2491 height = drm_framebuffer_plane_height(fb->height, fb, i);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002492
Ville Syrjälä303ba692017-08-24 22:10:49 +03002493 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
2494 if (ret) {
2495 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2496 i, fb->offsets[i]);
2497 return ret;
2498 }
Ville Syrjälä6687c902015-09-15 13:16:41 +03002499
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002500 if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2501 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
2502 int hsub = fb->format->hsub;
2503 int vsub = fb->format->vsub;
2504 int tile_width, tile_height;
2505 int main_x, main_y;
2506 int ccs_x, ccs_y;
2507
2508 intel_tile_dims(fb, i, &tile_width, &tile_height);
Ville Syrjälä303ba692017-08-24 22:10:49 +03002509 tile_width *= hsub;
2510 tile_height *= vsub;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002511
Ville Syrjälä303ba692017-08-24 22:10:49 +03002512 ccs_x = (x * hsub) % tile_width;
2513 ccs_y = (y * vsub) % tile_height;
2514 main_x = intel_fb->normal[0].x % tile_width;
2515 main_y = intel_fb->normal[0].y % tile_height;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002516
2517 /*
2518 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2519 * x/y offsets must match between CCS and the main surface.
2520 */
2521 if (main_x != ccs_x || main_y != ccs_y) {
2522 DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2523 main_x, main_y,
2524 ccs_x, ccs_y,
2525 intel_fb->normal[0].x,
2526 intel_fb->normal[0].y,
2527 x, y);
2528 return -EINVAL;
2529 }
2530 }
2531
Ville Syrjälä6687c902015-09-15 13:16:41 +03002532 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002533 * The fence (if used) is aligned to the start of the object
2534 * so having the framebuffer wrap around across the edge of the
2535 * fenced region doesn't really work. We have no API to configure
2536 * the fence start offset within the object (nor could we probably
2537 * on gen2/3). So it's just easier if we just require that the
2538 * fb layout agrees with the fence layout. We already check that the
2539 * fb stride matches the fence stride elsewhere.
2540 */
Ville Syrjälä2ec4cf42017-08-24 22:10:50 +03002541 if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002542 (x + width) * cpp > fb->pitches[i]) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +02002543 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2544 i, fb->offsets[i]);
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002545 return -EINVAL;
2546 }
2547
2548 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002549 * First pixel of the framebuffer from
2550 * the start of the normal gtt mapping.
2551 */
2552 intel_fb->normal[i].x = x;
2553 intel_fb->normal[i].y = y;
2554
2555 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
Ville Syrjälä3ca46c02017-03-07 21:42:09 +02002556 fb, i, fb->pitches[i],
Robert Fossc2c446a2017-05-19 16:50:17 -04002557 DRM_MODE_ROTATE_0, tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002558 offset /= tile_size;
2559
Ben Widawsky2f075562017-03-24 14:29:48 -07002560 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002561 unsigned int tile_width, tile_height;
2562 unsigned int pitch_tiles;
2563 struct drm_rect r;
2564
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002565 intel_tile_dims(fb, i, &tile_width, &tile_height);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002566
2567 rot_info->plane[i].offset = offset;
2568 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2569 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2570 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2571
2572 intel_fb->rotated[i].pitch =
2573 rot_info->plane[i].height * tile_height;
2574
2575 /* how many tiles does this plane need */
2576 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2577 /*
2578 * If the plane isn't horizontally tile aligned,
2579 * we need one more tile.
2580 */
2581 if (x != 0)
2582 size++;
2583
2584 /* rotate the x/y offsets to match the GTT view */
2585 r.x1 = x;
2586 r.y1 = y;
2587 r.x2 = x + width;
2588 r.y2 = y + height;
2589 drm_rect_rotate(&r,
2590 rot_info->plane[i].width * tile_width,
2591 rot_info->plane[i].height * tile_height,
Robert Fossc2c446a2017-05-19 16:50:17 -04002592 DRM_MODE_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002593 x = r.x1;
2594 y = r.y1;
2595
2596 /* rotate the tile dimensions to match the GTT view */
2597 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2598 swap(tile_width, tile_height);
2599
2600 /*
2601 * We only keep the x/y offsets, so push all of the
2602 * gtt offset into the x/y offsets.
2603 */
Ville Syrjälä303ba692017-08-24 22:10:49 +03002604 __intel_adjust_tile_offset(&x, &y,
2605 tile_width, tile_height,
2606 tile_size, pitch_tiles,
2607 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002608
2609 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2610
2611 /*
2612 * First pixel of the framebuffer from
2613 * the start of the rotated gtt mapping.
2614 */
2615 intel_fb->rotated[i].x = x;
2616 intel_fb->rotated[i].y = y;
2617 } else {
2618 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2619 x * cpp, tile_size);
2620 }
2621
2622 /* how many tiles in total needed in the bo */
2623 max_size = max(max_size, offset + size);
2624 }
2625
Ville Syrjälä144cc1432017-03-07 21:42:10 +02002626 if (max_size * tile_size > intel_fb->obj->base.size) {
2627 DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
2628 max_size * tile_size, intel_fb->obj->base.size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002629 return -EINVAL;
2630 }
2631
2632 return 0;
2633}
2634
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002635static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002636{
2637 switch (format) {
2638 case DISPPLANE_8BPP:
2639 return DRM_FORMAT_C8;
2640 case DISPPLANE_BGRX555:
2641 return DRM_FORMAT_XRGB1555;
2642 case DISPPLANE_BGRX565:
2643 return DRM_FORMAT_RGB565;
2644 default:
2645 case DISPPLANE_BGRX888:
2646 return DRM_FORMAT_XRGB8888;
2647 case DISPPLANE_RGBX888:
2648 return DRM_FORMAT_XBGR8888;
2649 case DISPPLANE_BGRX101010:
2650 return DRM_FORMAT_XRGB2101010;
2651 case DISPPLANE_RGBX101010:
2652 return DRM_FORMAT_XBGR2101010;
2653 }
2654}
2655
Mahesh Kumarddf34312018-04-09 09:11:03 +05302656int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002657{
2658 switch (format) {
2659 case PLANE_CTL_FORMAT_RGB_565:
2660 return DRM_FORMAT_RGB565;
Mahesh Kumarf34a2912018-04-09 09:11:02 +05302661 case PLANE_CTL_FORMAT_NV12:
2662 return DRM_FORMAT_NV12;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002663 default:
2664 case PLANE_CTL_FORMAT_XRGB_8888:
2665 if (rgb_order) {
2666 if (alpha)
2667 return DRM_FORMAT_ABGR8888;
2668 else
2669 return DRM_FORMAT_XBGR8888;
2670 } else {
2671 if (alpha)
2672 return DRM_FORMAT_ARGB8888;
2673 else
2674 return DRM_FORMAT_XRGB8888;
2675 }
2676 case PLANE_CTL_FORMAT_XRGB_2101010:
2677 if (rgb_order)
2678 return DRM_FORMAT_XBGR2101010;
2679 else
2680 return DRM_FORMAT_XRGB2101010;
2681 }
2682}
2683
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002684static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002685intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2686 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002687{
2688 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002689 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002690 struct drm_i915_gem_object *obj = NULL;
2691 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002692 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002693 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2694 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2695 PAGE_SIZE);
2696
2697 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002698
Chris Wilsonff2652e2014-03-10 08:07:02 +00002699 if (plane_config->size == 0)
2700 return false;
2701
Paulo Zanoni3badb492015-09-23 12:52:23 -03002702 /* If the FB is too big, just don't use it since fbdev is not very
2703 * important and we should probably use that space with FBC or other
2704 * features. */
Matthew Auldb1ace602017-12-11 15:18:21 +00002705 if (size_aligned * 2 > dev_priv->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002706 return false;
2707
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002708 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00002709 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002710 base_aligned,
2711 base_aligned,
2712 size_aligned);
Chris Wilson24dbf512017-02-15 10:59:18 +00002713 mutex_unlock(&dev->struct_mutex);
2714 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002715 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002716
Chris Wilson3e510a82016-08-05 10:14:23 +01002717 if (plane_config->tiling == I915_TILING_X)
2718 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002719
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002720 mode_cmd.pixel_format = fb->format->format;
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002721 mode_cmd.width = fb->width;
2722 mode_cmd.height = fb->height;
2723 mode_cmd.pitches[0] = fb->pitches[0];
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002724 mode_cmd.modifier[0] = fb->modifier;
Daniel Vetter18c52472015-02-10 17:16:09 +00002725 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002726
Chris Wilson24dbf512017-02-15 10:59:18 +00002727 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002728 DRM_DEBUG_KMS("intel fb init failed\n");
2729 goto out_unref_obj;
2730 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002731
Jesse Barnes484b41d2014-03-07 08:57:55 -08002732
Daniel Vetterf6936e22015-03-26 12:17:05 +01002733 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002734 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002735
2736out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002737 i915_gem_object_put(obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002738 return false;
2739}
2740
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002741static void
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002742intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2743 struct intel_plane_state *plane_state,
2744 bool visible)
2745{
2746 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2747
2748 plane_state->base.visible = visible;
2749
2750 /* FIXME pre-g4x don't work like this */
2751 if (visible) {
2752 crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
2753 crtc_state->active_planes |= BIT(plane->id);
2754 } else {
2755 crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
2756 crtc_state->active_planes &= ~BIT(plane->id);
2757 }
2758
2759 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2760 crtc_state->base.crtc->name,
2761 crtc_state->active_planes);
2762}
2763
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002764static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
2765 struct intel_plane *plane)
2766{
2767 struct intel_crtc_state *crtc_state =
2768 to_intel_crtc_state(crtc->base.state);
2769 struct intel_plane_state *plane_state =
2770 to_intel_plane_state(plane->base.state);
2771
2772 intel_set_plane_visible(crtc_state, plane_state, false);
2773
2774 if (plane->id == PLANE_PRIMARY)
2775 intel_pre_disable_primary_noatomic(&crtc->base);
2776
2777 trace_intel_disable_plane(&plane->base, crtc);
2778 plane->disable_plane(plane, crtc);
2779}
2780
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002781static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002782intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2783 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002784{
2785 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002786 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002787 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002788 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002789 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002790 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002791 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2792 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002793 struct intel_plane_state *intel_state =
2794 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002795 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002796
Damien Lespiau2d140302015-02-05 17:22:18 +00002797 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002798 return;
2799
Daniel Vetterf6936e22015-03-26 12:17:05 +01002800 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002801 fb = &plane_config->fb->base;
2802 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002803 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002804
Damien Lespiau2d140302015-02-05 17:22:18 +00002805 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002806
2807 /*
2808 * Failed to alloc the obj, check to see if we should share
2809 * an fb with another CRTC instead
2810 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002811 for_each_crtc(dev, c) {
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002812 struct intel_plane_state *state;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002813
2814 if (c == &intel_crtc->base)
2815 continue;
2816
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002817 if (!to_intel_crtc(c)->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002818 continue;
2819
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002820 state = to_intel_plane_state(c->primary->state);
2821 if (!state->vma)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002822 continue;
2823
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002824 if (intel_plane_ggtt_offset(state) == plane_config->base) {
Ville Syrjälä8bc20f62018-03-22 17:22:59 +02002825 fb = state->base.fb;
Harsha Sharmac3ed1102017-10-09 17:36:43 +05302826 drm_framebuffer_get(fb);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002827 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002828 }
2829 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002830
Matt Roper200757f2015-12-03 11:37:36 -08002831 /*
2832 * We've failed to reconstruct the BIOS FB. Current display state
2833 * indicates that the primary plane is visible, but has a NULL FB,
2834 * which will lead to problems later if we don't fix it up. The
2835 * simplest solution is to just disable the primary plane now and
2836 * pretend the BIOS never had it enabled.
2837 */
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002838 intel_plane_disable_noatomic(intel_crtc, intel_plane);
Matt Roper200757f2015-12-03 11:37:36 -08002839
Daniel Vetter88595ac2015-03-26 12:42:24 +01002840 return;
2841
2842valid_fb:
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002843 mutex_lock(&dev->struct_mutex);
2844 intel_state->vma =
Chris Wilson59354852018-02-20 13:42:06 +00002845 intel_pin_and_fence_fb_obj(fb,
2846 primary->state->rotation,
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002847 intel_plane_uses_fence(intel_state),
Chris Wilson59354852018-02-20 13:42:06 +00002848 &intel_state->flags);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002849 mutex_unlock(&dev->struct_mutex);
2850 if (IS_ERR(intel_state->vma)) {
2851 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2852 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2853
2854 intel_state->vma = NULL;
Harsha Sharmac3ed1102017-10-09 17:36:43 +05302855 drm_framebuffer_put(fb);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002856 return;
2857 }
2858
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08002859 obj = intel_fb_obj(fb);
2860 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
2861
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002862 plane_state->src_x = 0;
2863 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002864 plane_state->src_w = fb->width << 16;
2865 plane_state->src_h = fb->height << 16;
2866
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002867 plane_state->crtc_x = 0;
2868 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002869 plane_state->crtc_w = fb->width;
2870 plane_state->crtc_h = fb->height;
2871
Rob Clark1638d302016-11-05 11:08:08 -04002872 intel_state->base.src = drm_plane_state_src(plane_state);
2873 intel_state->base.dst = drm_plane_state_dest(plane_state);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002874
Chris Wilson3e510a82016-08-05 10:14:23 +01002875 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002876 dev_priv->preserve_bios_swizzle = true;
2877
Harsha Sharmac3ed1102017-10-09 17:36:43 +05302878 drm_framebuffer_get(fb);
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002879 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002880 primary->crtc = primary->state->crtc = &intel_crtc->base;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002881
2882 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2883 to_intel_plane_state(plane_state),
2884 true);
2885
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002886 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2887 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002888}
2889
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002890static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2891 unsigned int rotation)
2892{
Ville Syrjälä353c8592016-12-14 23:30:57 +02002893 int cpp = fb->format->cpp[plane];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002894
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002895 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07002896 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002897 case I915_FORMAT_MOD_X_TILED:
2898 switch (cpp) {
2899 case 8:
2900 return 4096;
2901 case 4:
2902 case 2:
2903 case 1:
2904 return 8192;
2905 default:
2906 MISSING_CASE(cpp);
2907 break;
2908 }
2909 break;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002910 case I915_FORMAT_MOD_Y_TILED_CCS:
2911 case I915_FORMAT_MOD_Yf_TILED_CCS:
2912 /* FIXME AUX plane? */
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002913 case I915_FORMAT_MOD_Y_TILED:
2914 case I915_FORMAT_MOD_Yf_TILED:
2915 switch (cpp) {
2916 case 8:
2917 return 2048;
2918 case 4:
2919 return 4096;
2920 case 2:
2921 case 1:
2922 return 8192;
2923 default:
2924 MISSING_CASE(cpp);
2925 break;
2926 }
2927 break;
2928 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002929 MISSING_CASE(fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002930 }
2931
2932 return 2048;
2933}
2934
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002935static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
2936 int main_x, int main_y, u32 main_offset)
2937{
2938 const struct drm_framebuffer *fb = plane_state->base.fb;
2939 int hsub = fb->format->hsub;
2940 int vsub = fb->format->vsub;
2941 int aux_x = plane_state->aux.x;
2942 int aux_y = plane_state->aux.y;
2943 u32 aux_offset = plane_state->aux.offset;
2944 u32 alignment = intel_surf_alignment(fb, 1);
2945
2946 while (aux_offset >= main_offset && aux_y <= main_y) {
2947 int x, y;
2948
2949 if (aux_x == main_x && aux_y == main_y)
2950 break;
2951
2952 if (aux_offset == 0)
2953 break;
2954
2955 x = aux_x / hsub;
2956 y = aux_y / vsub;
2957 aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
2958 aux_offset, aux_offset - alignment);
2959 aux_x = x * hsub + aux_x % hsub;
2960 aux_y = y * vsub + aux_y % vsub;
2961 }
2962
2963 if (aux_x != main_x || aux_y != main_y)
2964 return false;
2965
2966 plane_state->aux.offset = aux_offset;
2967 plane_state->aux.x = aux_x;
2968 plane_state->aux.y = aux_y;
2969
2970 return true;
2971}
2972
Imre Deakc322c642018-01-16 13:24:14 +02002973static int skl_check_main_surface(const struct intel_crtc_state *crtc_state,
2974 struct intel_plane_state *plane_state)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002975{
Imre Deakc322c642018-01-16 13:24:14 +02002976 struct drm_i915_private *dev_priv =
2977 to_i915(plane_state->base.plane->dev);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002978 const struct drm_framebuffer *fb = plane_state->base.fb;
2979 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002980 int x = plane_state->base.src.x1 >> 16;
2981 int y = plane_state->base.src.y1 >> 16;
2982 int w = drm_rect_width(&plane_state->base.src) >> 16;
2983 int h = drm_rect_height(&plane_state->base.src) >> 16;
Imre Deakc322c642018-01-16 13:24:14 +02002984 int dst_x = plane_state->base.dst.x1;
2985 int pipe_src_w = crtc_state->pipe_src_w;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002986 int max_width = skl_max_plane_width(fb, 0, rotation);
2987 int max_height = 4096;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002988 u32 alignment, offset, aux_offset = plane_state->aux.offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002989
2990 if (w > max_width || h > max_height) {
2991 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2992 w, h, max_width, max_height);
2993 return -EINVAL;
2994 }
2995
Imre Deakc322c642018-01-16 13:24:14 +02002996 /*
2997 * Display WA #1175: cnl,glk
2998 * Planes other than the cursor may cause FIFO underflow and display
2999 * corruption if starting less than 4 pixels from the right edge of
3000 * the screen.
Imre Deak394676f2018-01-16 13:24:15 +02003001 * Besides the above WA fix the similar problem, where planes other
3002 * than the cursor ending less than 4 pixels from the left edge of the
3003 * screen may cause FIFO underflow and display corruption.
Imre Deakc322c642018-01-16 13:24:14 +02003004 */
3005 if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
Imre Deak394676f2018-01-16 13:24:15 +02003006 (dst_x + w < 4 || dst_x > pipe_src_w - 4)) {
3007 DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
3008 dst_x + w < 4 ? "end" : "start",
3009 dst_x + w < 4 ? dst_x + w : dst_x,
3010 4, pipe_src_w - 4);
Imre Deakc322c642018-01-16 13:24:14 +02003011 return -ERANGE;
3012 }
3013
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003014 intel_add_fb_offsets(&x, &y, plane_state, 0);
3015 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003016 alignment = intel_surf_alignment(fb, 0);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003017
3018 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02003019 * AUX surface offset is specified as the distance from the
3020 * main surface offset, and it must be non-negative. Make
3021 * sure that is what we will get.
3022 */
3023 if (offset > aux_offset)
3024 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3025 offset, aux_offset & ~(alignment - 1));
3026
3027 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003028 * When using an X-tiled surface, the plane blows up
3029 * if the x offset + width exceed the stride.
3030 *
3031 * TODO: linear and Y-tiled seem fine, Yf untested,
3032 */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003033 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
Ville Syrjälä353c8592016-12-14 23:30:57 +02003034 int cpp = fb->format->cpp[0];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003035
3036 while ((x + w) * cpp > fb->pitches[0]) {
3037 if (offset == 0) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003038 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003039 return -EINVAL;
3040 }
3041
3042 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3043 offset, offset - alignment);
3044 }
3045 }
3046
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003047 /*
3048 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3049 * they match with the main surface x/y offsets.
3050 */
3051 if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
3052 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
3053 while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
3054 if (offset == 0)
3055 break;
3056
3057 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3058 offset, offset - alignment);
3059 }
3060
3061 if (x != plane_state->aux.x || y != plane_state->aux.y) {
3062 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
3063 return -EINVAL;
3064 }
3065 }
3066
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003067 plane_state->main.offset = offset;
3068 plane_state->main.x = x;
3069 plane_state->main.y = y;
3070
3071 return 0;
3072}
3073
Maarten Lankhorst5d794282018-05-12 03:03:14 +05303074static int
3075skl_check_nv12_surface(const struct intel_crtc_state *crtc_state,
3076 struct intel_plane_state *plane_state)
3077{
3078 /* Display WA #1106 */
3079 if (plane_state->base.rotation !=
3080 (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90) &&
3081 plane_state->base.rotation != DRM_MODE_ROTATE_270)
3082 return 0;
3083
3084 /*
3085 * src coordinates are rotated here.
3086 * We check height but report it as width
3087 */
3088 if (((drm_rect_height(&plane_state->base.src) >> 16) % 4) != 0) {
3089 DRM_DEBUG_KMS("src width must be multiple "
3090 "of 4 for rotated NV12\n");
3091 return -EINVAL;
3092 }
3093
3094 return 0;
3095}
3096
Ville Syrjälä8d970652016-01-28 16:30:28 +02003097static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3098{
3099 const struct drm_framebuffer *fb = plane_state->base.fb;
3100 unsigned int rotation = plane_state->base.rotation;
3101 int max_width = skl_max_plane_width(fb, 1, rotation);
3102 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02003103 int x = plane_state->base.src.x1 >> 17;
3104 int y = plane_state->base.src.y1 >> 17;
3105 int w = drm_rect_width(&plane_state->base.src) >> 17;
3106 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003107 u32 offset;
3108
3109 intel_add_fb_offsets(&x, &y, plane_state, 1);
3110 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
3111
3112 /* FIXME not quite sure how/if these apply to the chroma plane */
3113 if (w > max_width || h > max_height) {
3114 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
3115 w, h, max_width, max_height);
3116 return -EINVAL;
3117 }
3118
3119 plane_state->aux.offset = offset;
3120 plane_state->aux.x = x;
3121 plane_state->aux.y = y;
3122
3123 return 0;
3124}
3125
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003126static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3127{
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003128 const struct drm_framebuffer *fb = plane_state->base.fb;
3129 int src_x = plane_state->base.src.x1 >> 16;
3130 int src_y = plane_state->base.src.y1 >> 16;
3131 int hsub = fb->format->hsub;
3132 int vsub = fb->format->vsub;
3133 int x = src_x / hsub;
3134 int y = src_y / vsub;
3135 u32 offset;
3136
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003137 if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
3138 DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
3139 plane_state->base.rotation);
3140 return -EINVAL;
3141 }
3142
3143 intel_add_fb_offsets(&x, &y, plane_state, 1);
3144 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
3145
3146 plane_state->aux.offset = offset;
3147 plane_state->aux.x = x * hsub + src_x % hsub;
3148 plane_state->aux.y = y * vsub + src_y % vsub;
3149
3150 return 0;
3151}
3152
Imre Deakc322c642018-01-16 13:24:14 +02003153int skl_check_plane_surface(const struct intel_crtc_state *crtc_state,
3154 struct intel_plane_state *plane_state)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003155{
3156 const struct drm_framebuffer *fb = plane_state->base.fb;
3157 unsigned int rotation = plane_state->base.rotation;
3158 int ret;
3159
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003160 if (rotation & DRM_MODE_REFLECT_X &&
3161 fb->modifier == DRM_FORMAT_MOD_LINEAR) {
3162 DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
3163 return -EINVAL;
3164 }
3165
Ville Syrjäläa5e4c7d2016-11-07 22:20:54 +02003166 if (!plane_state->base.visible)
3167 return 0;
3168
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003169 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003170 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02003171 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03003172 fb->width << 16, fb->height << 16,
Robert Fossc2c446a2017-05-19 16:50:17 -04003173 DRM_MODE_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003174
Ville Syrjälä8d970652016-01-28 16:30:28 +02003175 /*
3176 * Handle the AUX surface first since
3177 * the main surface setup depends on it.
3178 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003179 if (fb->format->format == DRM_FORMAT_NV12) {
Maarten Lankhorst5d794282018-05-12 03:03:14 +05303180 ret = skl_check_nv12_surface(crtc_state, plane_state);
3181 if (ret)
3182 return ret;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003183 ret = skl_check_nv12_aux_surface(plane_state);
3184 if (ret)
3185 return ret;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003186 } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
3187 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
3188 ret = skl_check_ccs_aux_surface(plane_state);
3189 if (ret)
3190 return ret;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003191 } else {
3192 plane_state->aux.offset = ~0xfff;
3193 plane_state->aux.x = 0;
3194 plane_state->aux.y = 0;
3195 }
3196
Imre Deakc322c642018-01-16 13:24:14 +02003197 ret = skl_check_main_surface(crtc_state, plane_state);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003198 if (ret)
3199 return ret;
3200
3201 return 0;
3202}
3203
Ville Syrjälä7145f602017-03-23 21:27:07 +02003204static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
3205 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07003206{
Ville Syrjälä7145f602017-03-23 21:27:07 +02003207 struct drm_i915_private *dev_priv =
3208 to_i915(plane_state->base.plane->dev);
3209 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3210 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003211 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003212 u32 dspcntr;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003213
Ville Syrjälä7145f602017-03-23 21:27:07 +02003214 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003215
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02003216 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
3217 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Ville Syrjälä7145f602017-03-23 21:27:07 +02003218 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003219
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02003220 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3221 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003222
Ville Syrjäläc154d1e2018-01-30 22:38:02 +02003223 if (INTEL_GEN(dev_priv) < 5)
Ville Syrjäläd509e282017-03-27 21:55:32 +03003224 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003225
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003226 switch (fb->format->format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02003227 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003228 dspcntr |= DISPPLANE_8BPP;
3229 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003230 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003231 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003232 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003233 case DRM_FORMAT_RGB565:
3234 dspcntr |= DISPPLANE_BGRX565;
3235 break;
3236 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003237 dspcntr |= DISPPLANE_BGRX888;
3238 break;
3239 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003240 dspcntr |= DISPPLANE_RGBX888;
3241 break;
3242 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003243 dspcntr |= DISPPLANE_BGRX101010;
3244 break;
3245 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003246 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003247 break;
3248 default:
Ville Syrjälä7145f602017-03-23 21:27:07 +02003249 MISSING_CASE(fb->format->format);
3250 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003251 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003252
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003253 if (INTEL_GEN(dev_priv) >= 4 &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003254 fb->modifier == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003255 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003256
Robert Fossc2c446a2017-05-19 16:50:17 -04003257 if (rotation & DRM_MODE_ROTATE_180)
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003258 dspcntr |= DISPPLANE_ROTATE_180;
3259
Robert Fossc2c446a2017-05-19 16:50:17 -04003260 if (rotation & DRM_MODE_REFLECT_X)
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003261 dspcntr |= DISPPLANE_MIRROR;
3262
Ville Syrjälä7145f602017-03-23 21:27:07 +02003263 return dspcntr;
3264}
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003265
Ville Syrjäläf9407ae2017-03-23 21:27:12 +02003266int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003267{
3268 struct drm_i915_private *dev_priv =
3269 to_i915(plane_state->base.plane->dev);
3270 int src_x = plane_state->base.src.x1 >> 16;
3271 int src_y = plane_state->base.src.y1 >> 16;
3272 u32 offset;
3273
3274 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003275
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003276 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003277 offset = intel_compute_tile_offset(&src_x, &src_y,
3278 plane_state, 0);
3279 else
3280 offset = 0;
Daniel Vettere506a0c2012-07-05 12:17:29 +02003281
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003282 /* HSW/BDW do this automagically in hardware */
3283 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
3284 unsigned int rotation = plane_state->base.rotation;
3285 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3286 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3287
Robert Fossc2c446a2017-05-19 16:50:17 -04003288 if (rotation & DRM_MODE_ROTATE_180) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003289 src_x += src_w - 1;
3290 src_y += src_h - 1;
Robert Fossc2c446a2017-05-19 16:50:17 -04003291 } else if (rotation & DRM_MODE_REFLECT_X) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003292 src_x += src_w - 1;
3293 }
Sonika Jindal48404c12014-08-22 14:06:04 +05303294 }
3295
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003296 plane_state->main.offset = offset;
3297 plane_state->main.x = src_x;
3298 plane_state->main.y = src_y;
3299
3300 return 0;
3301}
3302
Ville Syrjäläed150302017-11-17 21:19:10 +02003303static void i9xx_update_plane(struct intel_plane *plane,
3304 const struct intel_crtc_state *crtc_state,
3305 const struct intel_plane_state *plane_state)
Ville Syrjälä7145f602017-03-23 21:27:07 +02003306{
Ville Syrjäläed150302017-11-17 21:19:10 +02003307 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä282dbf92017-03-27 21:55:33 +03003308 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjäläed150302017-11-17 21:19:10 +02003309 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003310 u32 linear_offset;
Ville Syrjäläa0864d52017-03-23 21:27:09 +02003311 u32 dspcntr = plane_state->ctl;
Ville Syrjäläed150302017-11-17 21:19:10 +02003312 i915_reg_t reg = DSPCNTR(i9xx_plane);
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003313 int x = plane_state->main.x;
3314 int y = plane_state->main.y;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003315 unsigned long irqflags;
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003316 u32 dspaddr_offset;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003317
Ville Syrjälä29490562016-01-20 18:02:50 +02003318 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003319
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003320 if (INTEL_GEN(dev_priv) >= 4)
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003321 dspaddr_offset = plane_state->main.offset;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003322 else
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003323 dspaddr_offset = linear_offset;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003324
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003325 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3326
Ville Syrjälä78587de2017-03-09 17:44:32 +02003327 if (INTEL_GEN(dev_priv) < 4) {
3328 /* pipesrc and dspsize control the size that is scaled from,
3329 * which should always be the user's requested size.
3330 */
Ville Syrjäläed150302017-11-17 21:19:10 +02003331 I915_WRITE_FW(DSPSIZE(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003332 ((crtc_state->pipe_src_h - 1) << 16) |
3333 (crtc_state->pipe_src_w - 1));
Ville Syrjäläed150302017-11-17 21:19:10 +02003334 I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
3335 } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
3336 I915_WRITE_FW(PRIMSIZE(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003337 ((crtc_state->pipe_src_h - 1) << 16) |
3338 (crtc_state->pipe_src_w - 1));
Ville Syrjäläed150302017-11-17 21:19:10 +02003339 I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
3340 I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
Ville Syrjälä78587de2017-03-09 17:44:32 +02003341 }
3342
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003343 I915_WRITE_FW(reg, dspcntr);
Sonika Jindal48404c12014-08-22 14:06:04 +05303344
Ville Syrjäläed150302017-11-17 21:19:10 +02003345 I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003346 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläed150302017-11-17 21:19:10 +02003347 I915_WRITE_FW(DSPSURF(i9xx_plane),
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003348 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003349 dspaddr_offset);
Ville Syrjäläed150302017-11-17 21:19:10 +02003350 I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003351 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläed150302017-11-17 21:19:10 +02003352 I915_WRITE_FW(DSPSURF(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003353 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003354 dspaddr_offset);
Ville Syrjäläed150302017-11-17 21:19:10 +02003355 I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
3356 I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003357 } else {
Ville Syrjäläed150302017-11-17 21:19:10 +02003358 I915_WRITE_FW(DSPADDR(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003359 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003360 dspaddr_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003361 }
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003362 POSTING_READ_FW(reg);
3363
3364 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003365}
3366
Ville Syrjäläed150302017-11-17 21:19:10 +02003367static void i9xx_disable_plane(struct intel_plane *plane,
3368 struct intel_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003369{
Ville Syrjäläed150302017-11-17 21:19:10 +02003370 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3371 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003372 unsigned long irqflags;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003373
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003374 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3375
Ville Syrjäläed150302017-11-17 21:19:10 +02003376 I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
3377 if (INTEL_GEN(dev_priv) >= 4)
3378 I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003379 else
Ville Syrjäläed150302017-11-17 21:19:10 +02003380 I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
3381 POSTING_READ_FW(DSPCNTR(i9xx_plane));
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003382
3383 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003384}
3385
Ville Syrjäläed150302017-11-17 21:19:10 +02003386static bool i9xx_plane_get_hw_state(struct intel_plane *plane)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003387{
Ville Syrjäläed150302017-11-17 21:19:10 +02003388 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003389 enum intel_display_power_domain power_domain;
Ville Syrjäläed150302017-11-17 21:19:10 +02003390 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
3391 enum pipe pipe = plane->pipe;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003392 bool ret;
3393
3394 /*
3395 * Not 100% correct for planes that can move between pipes,
3396 * but that's only the case for gen2-4 which don't have any
3397 * display power wells.
3398 */
3399 power_domain = POWER_DOMAIN_PIPE(pipe);
3400 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
3401 return false;
3402
Ville Syrjäläed150302017-11-17 21:19:10 +02003403 ret = I915_READ(DSPCNTR(i9xx_plane)) & DISPLAY_PLANE_ENABLE;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003404
3405 intel_display_power_put(dev_priv, power_domain);
3406
3407 return ret;
3408}
3409
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003410static u32
3411intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
Damien Lespiaub3218032015-02-27 11:15:18 +00003412{
Ben Widawsky2f075562017-03-24 14:29:48 -07003413 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003414 return 64;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003415 else
3416 return intel_tile_width_bytes(fb, plane);
Damien Lespiaub3218032015-02-27 11:15:18 +00003417}
3418
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003419static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3420{
3421 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003422 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003423
3424 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3425 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3426 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003427}
3428
Chandra Kondurua1b22782015-04-07 15:28:45 -07003429/*
3430 * This function detaches (aka. unbinds) unused scalers in hardware
3431 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003432static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003433{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003434 struct intel_crtc_scaler_state *scaler_state;
3435 int i;
3436
Chandra Kondurua1b22782015-04-07 15:28:45 -07003437 scaler_state = &intel_crtc->config->scaler_state;
3438
3439 /* loop through and disable scalers that aren't in use */
3440 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003441 if (!scaler_state->scalers[i].in_use)
3442 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003443 }
3444}
3445
Ville Syrjäläd2196772016-01-28 18:33:11 +02003446u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3447 unsigned int rotation)
3448{
Ville Syrjälä1b500532017-03-07 21:42:08 +02003449 u32 stride;
3450
3451 if (plane >= fb->format->num_planes)
3452 return 0;
3453
3454 stride = intel_fb_pitch(fb, plane, rotation);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003455
3456 /*
3457 * The stride is either expressed as a multiple of 64 bytes chunks for
3458 * linear buffers or in number of tiles for tiled buffers.
3459 */
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003460 if (drm_rotation_90_or_270(rotation))
3461 stride /= intel_tile_height(fb, plane);
3462 else
3463 stride /= intel_fb_stride_alignment(fb, plane);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003464
3465 return stride;
3466}
3467
Ville Syrjälä2e881262017-03-17 23:17:56 +02003468static u32 skl_plane_ctl_format(uint32_t pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -07003469{
Chandra Konduru6156a452015-04-27 13:48:39 -07003470 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003471 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003472 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003473 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003474 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003475 case DRM_FORMAT_XBGR8888:
James Ausmus4036c782017-11-13 10:11:28 -08003476 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003477 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003478 case DRM_FORMAT_XRGB8888:
Chandra Konduru6156a452015-04-27 13:48:39 -07003479 case DRM_FORMAT_ARGB8888:
James Ausmus4036c782017-11-13 10:11:28 -08003480 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003481 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003482 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003483 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003484 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003485 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003486 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003487 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003488 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003489 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003490 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003491 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003492 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru77224cd2018-04-09 09:11:13 +05303493 case DRM_FORMAT_NV12:
3494 return PLANE_CTL_FORMAT_NV12;
Chandra Konduru6156a452015-04-27 13:48:39 -07003495 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003496 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003497 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003498
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003499 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003500}
3501
James Ausmus4036c782017-11-13 10:11:28 -08003502/*
3503 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3504 * to be already pre-multiplied. We need to add a knob (or a different
3505 * DRM_FORMAT) for user-space to configure that.
3506 */
3507static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
3508{
3509 switch (pixel_format) {
3510 case DRM_FORMAT_ABGR8888:
3511 case DRM_FORMAT_ARGB8888:
3512 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3513 default:
3514 return PLANE_CTL_ALPHA_DISABLE;
3515 }
3516}
3517
3518static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
3519{
3520 switch (pixel_format) {
3521 case DRM_FORMAT_ABGR8888:
3522 case DRM_FORMAT_ARGB8888:
3523 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
3524 default:
3525 return PLANE_COLOR_ALPHA_DISABLE;
3526 }
3527}
3528
Ville Syrjälä2e881262017-03-17 23:17:56 +02003529static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
Chandra Konduru6156a452015-04-27 13:48:39 -07003530{
Chandra Konduru6156a452015-04-27 13:48:39 -07003531 switch (fb_modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07003532 case DRM_FORMAT_MOD_LINEAR:
Chandra Konduru6156a452015-04-27 13:48:39 -07003533 break;
3534 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003535 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003536 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003537 return PLANE_CTL_TILED_Y;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003538 case I915_FORMAT_MOD_Y_TILED_CCS:
3539 return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003540 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003541 return PLANE_CTL_TILED_YF;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003542 case I915_FORMAT_MOD_Yf_TILED_CCS:
3543 return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003544 default:
3545 MISSING_CASE(fb_modifier);
3546 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003547
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003548 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003549}
3550
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003551static u32 skl_plane_ctl_rotate(unsigned int rotate)
Chandra Konduru6156a452015-04-27 13:48:39 -07003552{
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003553 switch (rotate) {
Robert Fossc2c446a2017-05-19 16:50:17 -04003554 case DRM_MODE_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003555 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303556 /*
Robert Fossc2c446a2017-05-19 16:50:17 -04003557 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
Sonika Jindal1e8df162015-05-20 13:40:48 +05303558 * while i915 HW rotation is clockwise, thats why this swapping.
3559 */
Robert Fossc2c446a2017-05-19 16:50:17 -04003560 case DRM_MODE_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303561 return PLANE_CTL_ROTATE_270;
Robert Fossc2c446a2017-05-19 16:50:17 -04003562 case DRM_MODE_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003563 return PLANE_CTL_ROTATE_180;
Robert Fossc2c446a2017-05-19 16:50:17 -04003564 case DRM_MODE_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303565 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003566 default:
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003567 MISSING_CASE(rotate);
3568 }
3569
3570 return 0;
3571}
3572
3573static u32 cnl_plane_ctl_flip(unsigned int reflect)
3574{
3575 switch (reflect) {
3576 case 0:
3577 break;
3578 case DRM_MODE_REFLECT_X:
3579 return PLANE_CTL_FLIP_HORIZONTAL;
3580 case DRM_MODE_REFLECT_Y:
3581 default:
3582 MISSING_CASE(reflect);
Chandra Konduru6156a452015-04-27 13:48:39 -07003583 }
3584
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003585 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003586}
3587
Ville Syrjälä2e881262017-03-17 23:17:56 +02003588u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3589 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003590{
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003591 struct drm_i915_private *dev_priv =
3592 to_i915(plane_state->base.plane->dev);
3593 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003594 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä2e881262017-03-17 23:17:56 +02003595 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003596 u32 plane_ctl;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003597
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003598 plane_ctl = PLANE_CTL_ENABLE;
3599
James Ausmus4036c782017-11-13 10:11:28 -08003600 if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
3601 plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003602 plane_ctl |=
3603 PLANE_CTL_PIPE_GAMMA_ENABLE |
3604 PLANE_CTL_PIPE_CSC_ENABLE |
3605 PLANE_CTL_PLANE_GAMMA_DISABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003606
3607 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3608 plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003609
3610 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3611 plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003612 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003613
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003614 plane_ctl |= skl_plane_ctl_format(fb->format->format);
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003615 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003616 plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
3617
3618 if (INTEL_GEN(dev_priv) >= 10)
3619 plane_ctl |= cnl_plane_ctl_flip(rotation &
3620 DRM_MODE_REFLECT_MASK);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003621
Ville Syrjälä2e881262017-03-17 23:17:56 +02003622 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3623 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3624 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3625 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3626
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003627 return plane_ctl;
3628}
3629
James Ausmus4036c782017-11-13 10:11:28 -08003630u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
3631 const struct intel_plane_state *plane_state)
3632{
James Ausmus077ef1f2018-03-28 14:57:56 -07003633 struct drm_i915_private *dev_priv =
3634 to_i915(plane_state->base.plane->dev);
James Ausmus4036c782017-11-13 10:11:28 -08003635 const struct drm_framebuffer *fb = plane_state->base.fb;
3636 u32 plane_color_ctl = 0;
3637
James Ausmus077ef1f2018-03-28 14:57:56 -07003638 if (INTEL_GEN(dev_priv) < 11) {
3639 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
3640 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
3641 }
James Ausmus4036c782017-11-13 10:11:28 -08003642 plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
3643 plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
3644
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003645 if (intel_format_is_yuv(fb->format->format)) {
Vidya Srinivas8ed30ab2018-04-09 09:11:10 +05303646 if (fb->format->format == DRM_FORMAT_NV12) {
3647 plane_color_ctl |=
3648 PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
3649 goto out;
3650 }
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003651 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3652 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
3653 else
3654 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003655
3656 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3657 plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003658 }
Vidya Srinivas8ed30ab2018-04-09 09:11:10 +05303659out:
James Ausmus4036c782017-11-13 10:11:28 -08003660 return plane_color_ctl;
3661}
3662
Maarten Lankhorst73974892016-08-05 23:28:27 +03003663static int
3664__intel_display_resume(struct drm_device *dev,
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003665 struct drm_atomic_state *state,
3666 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorst73974892016-08-05 23:28:27 +03003667{
3668 struct drm_crtc_state *crtc_state;
3669 struct drm_crtc *crtc;
3670 int i, ret;
3671
Ville Syrjäläaecd36b2017-06-01 17:36:13 +03003672 intel_modeset_setup_hw_state(dev, ctx);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003673 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003674
3675 if (!state)
3676 return 0;
3677
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01003678 /*
3679 * We've duplicated the state, pointers to the old state are invalid.
3680 *
3681 * Don't attempt to use the old state until we commit the duplicated state.
3682 */
3683 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst73974892016-08-05 23:28:27 +03003684 /*
3685 * Force recalculation even if we restore
3686 * current state. With fast modeset this may not result
3687 * in a modeset when the state is compatible.
3688 */
3689 crtc_state->mode_changed = true;
3690 }
3691
3692 /* ignore any reset values/BIOS leftovers in the WM registers */
Ville Syrjälä602ae832017-03-02 19:15:02 +02003693 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3694 to_intel_atomic_state(state)->skip_intermediate_wm = true;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003695
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003696 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003697
3698 WARN_ON(ret == -EDEADLK);
3699 return ret;
3700}
3701
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003702static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3703{
Ville Syrjäläae981042016-08-05 23:28:30 +03003704 return intel_has_gpu_reset(dev_priv) &&
3705 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003706}
3707
Chris Wilsonc0336662016-05-06 15:40:21 +01003708void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003709{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003710 struct drm_device *dev = &dev_priv->drm;
3711 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3712 struct drm_atomic_state *state;
3713 int ret;
3714
Daniel Vetterce87ea12017-07-19 14:54:55 +02003715 /* reset doesn't touch the display */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00003716 if (!i915_modparams.force_reset_modeset_test &&
Daniel Vetterce87ea12017-07-19 14:54:55 +02003717 !gpu_reset_clobbers_display(dev_priv))
3718 return;
3719
Daniel Vetter9db529a2017-08-08 10:08:28 +02003720 /* We have a modeset vs reset deadlock, defensively unbreak it. */
3721 set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
3722 wake_up_all(&dev_priv->gpu_error.wait_queue);
3723
3724 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
3725 DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
3726 i915_gem_set_wedged(dev_priv);
3727 }
Daniel Vetter97154ec2017-08-08 10:08:26 +02003728
Maarten Lankhorst73974892016-08-05 23:28:27 +03003729 /*
3730 * Need mode_config.mutex so that we don't
3731 * trample ongoing ->detect() and whatnot.
3732 */
3733 mutex_lock(&dev->mode_config.mutex);
3734 drm_modeset_acquire_init(ctx, 0);
3735 while (1) {
3736 ret = drm_modeset_lock_all_ctx(dev, ctx);
3737 if (ret != -EDEADLK)
3738 break;
3739
3740 drm_modeset_backoff(ctx);
3741 }
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003742 /*
3743 * Disabling the crtcs gracefully seems nicer. Also the
3744 * g33 docs say we should at least disable all the planes.
3745 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003746 state = drm_atomic_helper_duplicate_state(dev, ctx);
3747 if (IS_ERR(state)) {
3748 ret = PTR_ERR(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003749 DRM_ERROR("Duplicating state failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003750 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003751 }
3752
3753 ret = drm_atomic_helper_disable_all(dev, ctx);
3754 if (ret) {
3755 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003756 drm_atomic_state_put(state);
3757 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003758 }
3759
3760 dev_priv->modeset_restore_state = state;
3761 state->acquire_ctx = ctx;
Ville Syrjälä75147472014-11-24 18:28:11 +02003762}
3763
Chris Wilsonc0336662016-05-06 15:40:21 +01003764void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003765{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003766 struct drm_device *dev = &dev_priv->drm;
3767 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
Chris Wilson40da1d32018-04-05 13:37:14 +01003768 struct drm_atomic_state *state;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003769 int ret;
3770
Daniel Vetterce87ea12017-07-19 14:54:55 +02003771 /* reset doesn't touch the display */
Chris Wilson40da1d32018-04-05 13:37:14 +01003772 if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
Daniel Vetterce87ea12017-07-19 14:54:55 +02003773 return;
3774
Chris Wilson40da1d32018-04-05 13:37:14 +01003775 state = fetch_and_zero(&dev_priv->modeset_restore_state);
Daniel Vetterce87ea12017-07-19 14:54:55 +02003776 if (!state)
3777 goto unlock;
3778
Ville Syrjälä75147472014-11-24 18:28:11 +02003779 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003780 if (!gpu_reset_clobbers_display(dev_priv)) {
Daniel Vetterce87ea12017-07-19 14:54:55 +02003781 /* for testing only restore the display */
3782 ret = __intel_display_resume(dev, state, ctx);
Chris Wilson942d5d02017-08-28 11:46:04 +01003783 if (ret)
3784 DRM_ERROR("Restoring old state failed with %i\n", ret);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003785 } else {
3786 /*
3787 * The display has been reset as well,
3788 * so need a full re-initialization.
3789 */
3790 intel_runtime_pm_disable_interrupts(dev_priv);
3791 intel_runtime_pm_enable_interrupts(dev_priv);
3792
Imre Deak51f59202016-09-14 13:04:13 +03003793 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003794 intel_modeset_init_hw(dev);
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02003795 intel_init_clock_gating(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003796
3797 spin_lock_irq(&dev_priv->irq_lock);
3798 if (dev_priv->display.hpd_irq_setup)
3799 dev_priv->display.hpd_irq_setup(dev_priv);
3800 spin_unlock_irq(&dev_priv->irq_lock);
3801
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003802 ret = __intel_display_resume(dev, state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003803 if (ret)
3804 DRM_ERROR("Restoring old state failed with %i\n", ret);
3805
3806 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003807 }
3808
Daniel Vetterce87ea12017-07-19 14:54:55 +02003809 drm_atomic_state_put(state);
3810unlock:
Maarten Lankhorst73974892016-08-05 23:28:27 +03003811 drm_modeset_drop_locks(ctx);
3812 drm_modeset_acquire_fini(ctx);
3813 mutex_unlock(&dev->mode_config.mutex);
Daniel Vetter9db529a2017-08-08 10:08:28 +02003814
3815 clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
Ville Syrjälä75147472014-11-24 18:28:11 +02003816}
3817
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003818static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
3819 const struct intel_crtc_state *new_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003820{
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003821 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003822 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003823
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003824 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003825 crtc->base.mode = new_crtc_state->base.mode;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003826
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003827 /*
3828 * Update pipe size and adjust fitter if needed: the reason for this is
3829 * that in compute_mode_changes we check the native mode (not the pfit
3830 * mode) to see if we can flip rather than do a full mode set. In the
3831 * fastboot case, we'll flip, but if we don't update the pipesrc and
3832 * pfit state, we'll end up with a big fb scanned out into the wrong
3833 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003834 */
3835
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003836 I915_WRITE(PIPESRC(crtc->pipe),
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003837 ((new_crtc_state->pipe_src_w - 1) << 16) |
3838 (new_crtc_state->pipe_src_h - 1));
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003839
3840 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003841 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003842 skl_detach_scalers(crtc);
3843
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003844 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003845 skylake_pfit_enable(crtc);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003846 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003847 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003848 ironlake_pfit_enable(crtc);
3849 else if (old_crtc_state->pch_pfit.enabled)
3850 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003851 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003852}
3853
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003854static void intel_fdi_normal_train(struct intel_crtc *crtc)
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003855{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003856 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003857 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003858 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003859 i915_reg_t reg;
3860 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003861
3862 /* enable normal train */
3863 reg = FDI_TX_CTL(pipe);
3864 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003865 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003866 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3867 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003868 } else {
3869 temp &= ~FDI_LINK_TRAIN_NONE;
3870 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003871 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003872 I915_WRITE(reg, temp);
3873
3874 reg = FDI_RX_CTL(pipe);
3875 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003876 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003877 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3878 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3879 } else {
3880 temp &= ~FDI_LINK_TRAIN_NONE;
3881 temp |= FDI_LINK_TRAIN_NONE;
3882 }
3883 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3884
3885 /* wait one idle pattern time */
3886 POSTING_READ(reg);
3887 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003888
3889 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003890 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003891 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3892 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003893}
3894
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003895/* The FDI link training functions for ILK/Ibexpeak. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003896static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3897 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003898{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003899 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003900 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003901 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003902 i915_reg_t reg;
3903 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003904
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003905 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003906 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003907
Adam Jacksone1a44742010-06-25 15:32:14 -04003908 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3909 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003910 reg = FDI_RX_IMR(pipe);
3911 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003912 temp &= ~FDI_RX_SYMBOL_LOCK;
3913 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003914 I915_WRITE(reg, temp);
3915 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003916 udelay(150);
3917
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003918 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003919 reg = FDI_TX_CTL(pipe);
3920 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003921 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003922 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003923 temp &= ~FDI_LINK_TRAIN_NONE;
3924 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003925 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003926
Chris Wilson5eddb702010-09-11 13:48:45 +01003927 reg = FDI_RX_CTL(pipe);
3928 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003929 temp &= ~FDI_LINK_TRAIN_NONE;
3930 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003931 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3932
3933 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003934 udelay(150);
3935
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003936 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003937 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3938 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3939 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003940
Chris Wilson5eddb702010-09-11 13:48:45 +01003941 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003942 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003943 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003944 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3945
3946 if ((temp & FDI_RX_BIT_LOCK)) {
3947 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003948 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003949 break;
3950 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003951 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003952 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003953 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003954
3955 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003956 reg = FDI_TX_CTL(pipe);
3957 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003958 temp &= ~FDI_LINK_TRAIN_NONE;
3959 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003960 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003961
Chris Wilson5eddb702010-09-11 13:48:45 +01003962 reg = FDI_RX_CTL(pipe);
3963 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003964 temp &= ~FDI_LINK_TRAIN_NONE;
3965 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003966 I915_WRITE(reg, temp);
3967
3968 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003969 udelay(150);
3970
Chris Wilson5eddb702010-09-11 13:48:45 +01003971 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003972 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003973 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003974 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3975
3976 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003977 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003978 DRM_DEBUG_KMS("FDI train 2 done.\n");
3979 break;
3980 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003981 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003982 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003983 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003984
3985 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003986
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003987}
3988
Akshay Joshi0206e352011-08-16 15:34:10 -04003989static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003990 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3991 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3992 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3993 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3994};
3995
3996/* The FDI link training functions for SNB/Cougarpoint. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003997static void gen6_fdi_link_train(struct intel_crtc *crtc,
3998 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003999{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004000 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004001 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004002 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004003 i915_reg_t reg;
4004 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004005
Adam Jacksone1a44742010-06-25 15:32:14 -04004006 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4007 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01004008 reg = FDI_RX_IMR(pipe);
4009 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004010 temp &= ~FDI_RX_SYMBOL_LOCK;
4011 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01004012 I915_WRITE(reg, temp);
4013
4014 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004015 udelay(150);
4016
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004017 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01004018 reg = FDI_TX_CTL(pipe);
4019 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004020 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004021 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004022 temp &= ~FDI_LINK_TRAIN_NONE;
4023 temp |= FDI_LINK_TRAIN_PATTERN_1;
4024 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4025 /* SNB-B */
4026 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01004027 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004028
Daniel Vetterd74cf322012-10-26 10:58:13 +02004029 I915_WRITE(FDI_RX_MISC(pipe),
4030 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4031
Chris Wilson5eddb702010-09-11 13:48:45 +01004032 reg = FDI_RX_CTL(pipe);
4033 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004034 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004035 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4036 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4037 } else {
4038 temp &= ~FDI_LINK_TRAIN_NONE;
4039 temp |= FDI_LINK_TRAIN_PATTERN_1;
4040 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004041 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4042
4043 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004044 udelay(150);
4045
Akshay Joshi0206e352011-08-16 15:34:10 -04004046 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004047 reg = FDI_TX_CTL(pipe);
4048 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004049 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4050 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004051 I915_WRITE(reg, temp);
4052
4053 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004054 udelay(500);
4055
Sean Paulfa37d392012-03-02 12:53:39 -05004056 for (retry = 0; retry < 5; retry++) {
4057 reg = FDI_RX_IIR(pipe);
4058 temp = I915_READ(reg);
4059 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4060 if (temp & FDI_RX_BIT_LOCK) {
4061 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4062 DRM_DEBUG_KMS("FDI train 1 done.\n");
4063 break;
4064 }
4065 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004066 }
Sean Paulfa37d392012-03-02 12:53:39 -05004067 if (retry < 5)
4068 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004069 }
4070 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004071 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004072
4073 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01004074 reg = FDI_TX_CTL(pipe);
4075 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004076 temp &= ~FDI_LINK_TRAIN_NONE;
4077 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004078 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004079 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4080 /* SNB-B */
4081 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
4082 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004083 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004084
Chris Wilson5eddb702010-09-11 13:48:45 +01004085 reg = FDI_RX_CTL(pipe);
4086 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004087 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004088 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4089 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
4090 } else {
4091 temp &= ~FDI_LINK_TRAIN_NONE;
4092 temp |= FDI_LINK_TRAIN_PATTERN_2;
4093 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004094 I915_WRITE(reg, temp);
4095
4096 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004097 udelay(150);
4098
Akshay Joshi0206e352011-08-16 15:34:10 -04004099 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004100 reg = FDI_TX_CTL(pipe);
4101 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004102 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4103 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004104 I915_WRITE(reg, temp);
4105
4106 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004107 udelay(500);
4108
Sean Paulfa37d392012-03-02 12:53:39 -05004109 for (retry = 0; retry < 5; retry++) {
4110 reg = FDI_RX_IIR(pipe);
4111 temp = I915_READ(reg);
4112 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4113 if (temp & FDI_RX_SYMBOL_LOCK) {
4114 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4115 DRM_DEBUG_KMS("FDI train 2 done.\n");
4116 break;
4117 }
4118 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004119 }
Sean Paulfa37d392012-03-02 12:53:39 -05004120 if (retry < 5)
4121 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004122 }
4123 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004124 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004125
4126 DRM_DEBUG_KMS("FDI train done.\n");
4127}
4128
Jesse Barnes357555c2011-04-28 15:09:55 -07004129/* Manual link training for Ivy Bridge A0 parts */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004130static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4131 const struct intel_crtc_state *crtc_state)
Jesse Barnes357555c2011-04-28 15:09:55 -07004132{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004133 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004134 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004135 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004136 i915_reg_t reg;
4137 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004138
4139 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4140 for train result */
4141 reg = FDI_RX_IMR(pipe);
4142 temp = I915_READ(reg);
4143 temp &= ~FDI_RX_SYMBOL_LOCK;
4144 temp &= ~FDI_RX_BIT_LOCK;
4145 I915_WRITE(reg, temp);
4146
4147 POSTING_READ(reg);
4148 udelay(150);
4149
Daniel Vetter01a415f2012-10-27 15:58:40 +02004150 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4151 I915_READ(FDI_RX_IIR(pipe)));
4152
Jesse Barnes139ccd32013-08-19 11:04:55 -07004153 /* Try each vswing and preemphasis setting twice before moving on */
4154 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4155 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004156 reg = FDI_TX_CTL(pipe);
4157 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004158 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4159 temp &= ~FDI_TX_ENABLE;
4160 I915_WRITE(reg, temp);
4161
4162 reg = FDI_RX_CTL(pipe);
4163 temp = I915_READ(reg);
4164 temp &= ~FDI_LINK_TRAIN_AUTO;
4165 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4166 temp &= ~FDI_RX_ENABLE;
4167 I915_WRITE(reg, temp);
4168
4169 /* enable CPU FDI TX and PCH FDI RX */
4170 reg = FDI_TX_CTL(pipe);
4171 temp = I915_READ(reg);
4172 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004173 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004174 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004175 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004176 temp |= snb_b_fdi_train_param[j/2];
4177 temp |= FDI_COMPOSITE_SYNC;
4178 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4179
4180 I915_WRITE(FDI_RX_MISC(pipe),
4181 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4182
4183 reg = FDI_RX_CTL(pipe);
4184 temp = I915_READ(reg);
4185 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4186 temp |= FDI_COMPOSITE_SYNC;
4187 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4188
4189 POSTING_READ(reg);
4190 udelay(1); /* should be 0.5us */
4191
4192 for (i = 0; i < 4; i++) {
4193 reg = FDI_RX_IIR(pipe);
4194 temp = I915_READ(reg);
4195 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4196
4197 if (temp & FDI_RX_BIT_LOCK ||
4198 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4199 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4200 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4201 i);
4202 break;
4203 }
4204 udelay(1); /* should be 0.5us */
4205 }
4206 if (i == 4) {
4207 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4208 continue;
4209 }
4210
4211 /* Train 2 */
4212 reg = FDI_TX_CTL(pipe);
4213 temp = I915_READ(reg);
4214 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4215 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4216 I915_WRITE(reg, temp);
4217
4218 reg = FDI_RX_CTL(pipe);
4219 temp = I915_READ(reg);
4220 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4221 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004222 I915_WRITE(reg, temp);
4223
4224 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004225 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004226
Jesse Barnes139ccd32013-08-19 11:04:55 -07004227 for (i = 0; i < 4; i++) {
4228 reg = FDI_RX_IIR(pipe);
4229 temp = I915_READ(reg);
4230 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004231
Jesse Barnes139ccd32013-08-19 11:04:55 -07004232 if (temp & FDI_RX_SYMBOL_LOCK ||
4233 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4234 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4235 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4236 i);
4237 goto train_done;
4238 }
4239 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004240 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004241 if (i == 4)
4242 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004243 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004244
Jesse Barnes139ccd32013-08-19 11:04:55 -07004245train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004246 DRM_DEBUG_KMS("FDI train done.\n");
4247}
4248
Daniel Vetter88cefb62012-08-12 19:27:14 +02004249static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004250{
Daniel Vetter88cefb62012-08-12 19:27:14 +02004251 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004252 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004253 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004254 i915_reg_t reg;
4255 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004256
Jesse Barnes0e23b992010-09-10 11:10:00 -07004257 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004258 reg = FDI_RX_CTL(pipe);
4259 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004260 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004261 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004262 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004263 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4264
4265 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004266 udelay(200);
4267
4268 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004269 temp = I915_READ(reg);
4270 I915_WRITE(reg, temp | FDI_PCDCLK);
4271
4272 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004273 udelay(200);
4274
Paulo Zanoni20749732012-11-23 15:30:38 -02004275 /* Enable CPU FDI TX PLL, always on for Ironlake */
4276 reg = FDI_TX_CTL(pipe);
4277 temp = I915_READ(reg);
4278 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4279 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004280
Paulo Zanoni20749732012-11-23 15:30:38 -02004281 POSTING_READ(reg);
4282 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004283 }
4284}
4285
Daniel Vetter88cefb62012-08-12 19:27:14 +02004286static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4287{
4288 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004289 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004290 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004291 i915_reg_t reg;
4292 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004293
4294 /* Switch from PCDclk to Rawclk */
4295 reg = FDI_RX_CTL(pipe);
4296 temp = I915_READ(reg);
4297 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4298
4299 /* Disable CPU FDI TX PLL */
4300 reg = FDI_TX_CTL(pipe);
4301 temp = I915_READ(reg);
4302 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4303
4304 POSTING_READ(reg);
4305 udelay(100);
4306
4307 reg = FDI_RX_CTL(pipe);
4308 temp = I915_READ(reg);
4309 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4310
4311 /* Wait for the clocks to turn off. */
4312 POSTING_READ(reg);
4313 udelay(100);
4314}
4315
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004316static void ironlake_fdi_disable(struct drm_crtc *crtc)
4317{
4318 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004319 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4321 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004322 i915_reg_t reg;
4323 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004324
4325 /* disable CPU FDI tx and PCH FDI rx */
4326 reg = FDI_TX_CTL(pipe);
4327 temp = I915_READ(reg);
4328 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4329 POSTING_READ(reg);
4330
4331 reg = FDI_RX_CTL(pipe);
4332 temp = I915_READ(reg);
4333 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004334 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004335 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4336
4337 POSTING_READ(reg);
4338 udelay(100);
4339
4340 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004341 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004342 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004343
4344 /* still set train pattern 1 */
4345 reg = FDI_TX_CTL(pipe);
4346 temp = I915_READ(reg);
4347 temp &= ~FDI_LINK_TRAIN_NONE;
4348 temp |= FDI_LINK_TRAIN_PATTERN_1;
4349 I915_WRITE(reg, temp);
4350
4351 reg = FDI_RX_CTL(pipe);
4352 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004353 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004354 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4355 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4356 } else {
4357 temp &= ~FDI_LINK_TRAIN_NONE;
4358 temp |= FDI_LINK_TRAIN_PATTERN_1;
4359 }
4360 /* BPC in FDI rx is consistent with that in PIPECONF */
4361 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004362 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004363 I915_WRITE(reg, temp);
4364
4365 POSTING_READ(reg);
4366 udelay(100);
4367}
4368
Chris Wilson49d73912016-11-29 09:50:08 +00004369bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004370{
Daniel Vetterfa058872017-07-20 19:57:52 +02004371 struct drm_crtc *crtc;
4372 bool cleanup_done;
Chris Wilson5dce5b932014-01-20 10:17:36 +00004373
Daniel Vetterfa058872017-07-20 19:57:52 +02004374 drm_for_each_crtc(crtc, &dev_priv->drm) {
4375 struct drm_crtc_commit *commit;
4376 spin_lock(&crtc->commit_lock);
4377 commit = list_first_entry_or_null(&crtc->commit_list,
4378 struct drm_crtc_commit, commit_entry);
4379 cleanup_done = commit ?
4380 try_wait_for_completion(&commit->cleanup_done) : true;
4381 spin_unlock(&crtc->commit_lock);
4382
4383 if (cleanup_done)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004384 continue;
4385
Daniel Vetterfa058872017-07-20 19:57:52 +02004386 drm_crtc_wait_one_vblank(crtc);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004387
4388 return true;
4389 }
4390
4391 return false;
4392}
4393
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004394void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004395{
4396 u32 temp;
4397
4398 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4399
4400 mutex_lock(&dev_priv->sb_lock);
4401
4402 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4403 temp |= SBI_SSCCTL_DISABLE;
4404 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4405
4406 mutex_unlock(&dev_priv->sb_lock);
4407}
4408
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004409/* Program iCLKIP clock to the desired frequency */
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004410static void lpt_program_iclkip(struct intel_crtc *crtc)
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004411{
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004412 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4413 int clock = crtc->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004414 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4415 u32 temp;
4416
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004417 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004418
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004419 /* The iCLK virtual clock root frequency is in MHz,
4420 * but the adjusted_mode->crtc_clock in in KHz. To get the
4421 * divisors, it is necessary to divide one by another, so we
4422 * convert the virtual clock precision to KHz here for higher
4423 * precision.
4424 */
4425 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004426 u32 iclk_virtual_root_freq = 172800 * 1000;
4427 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004428 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004429
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004430 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4431 clock << auxdiv);
4432 divsel = (desired_divisor / iclk_pi_range) - 2;
4433 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004434
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004435 /*
4436 * Near 20MHz is a corner case which is
4437 * out of range for the 7-bit divisor
4438 */
4439 if (divsel <= 0x7f)
4440 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004441 }
4442
4443 /* This should not happen with any sane values */
4444 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4445 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4446 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4447 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4448
4449 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 +03004450 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004451 auxdiv,
4452 divsel,
4453 phasedir,
4454 phaseinc);
4455
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004456 mutex_lock(&dev_priv->sb_lock);
4457
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004458 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004459 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004460 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4461 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4462 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4463 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4464 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4465 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004466 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004467
4468 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004469 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004470 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4471 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004472 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004473
4474 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004475 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004476 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004477 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004478
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004479 mutex_unlock(&dev_priv->sb_lock);
4480
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004481 /* Wait for initialization time */
4482 udelay(24);
4483
4484 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4485}
4486
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004487int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4488{
4489 u32 divsel, phaseinc, auxdiv;
4490 u32 iclk_virtual_root_freq = 172800 * 1000;
4491 u32 iclk_pi_range = 64;
4492 u32 desired_divisor;
4493 u32 temp;
4494
4495 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4496 return 0;
4497
4498 mutex_lock(&dev_priv->sb_lock);
4499
4500 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4501 if (temp & SBI_SSCCTL_DISABLE) {
4502 mutex_unlock(&dev_priv->sb_lock);
4503 return 0;
4504 }
4505
4506 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4507 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4508 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4509 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4510 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4511
4512 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4513 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4514 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4515
4516 mutex_unlock(&dev_priv->sb_lock);
4517
4518 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4519
4520 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4521 desired_divisor << auxdiv);
4522}
4523
Daniel Vetter275f01b22013-05-03 11:49:47 +02004524static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4525 enum pipe pch_transcoder)
4526{
4527 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004528 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004529 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004530
4531 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4532 I915_READ(HTOTAL(cpu_transcoder)));
4533 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4534 I915_READ(HBLANK(cpu_transcoder)));
4535 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4536 I915_READ(HSYNC(cpu_transcoder)));
4537
4538 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4539 I915_READ(VTOTAL(cpu_transcoder)));
4540 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4541 I915_READ(VBLANK(cpu_transcoder)));
4542 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4543 I915_READ(VSYNC(cpu_transcoder)));
4544 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4545 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4546}
4547
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004548static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004549{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004550 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004551 uint32_t temp;
4552
4553 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004554 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004555 return;
4556
4557 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4558 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4559
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004560 temp &= ~FDI_BC_BIFURCATION_SELECT;
4561 if (enable)
4562 temp |= FDI_BC_BIFURCATION_SELECT;
4563
4564 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004565 I915_WRITE(SOUTH_CHICKEN1, temp);
4566 POSTING_READ(SOUTH_CHICKEN1);
4567}
4568
4569static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4570{
4571 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004572
4573 switch (intel_crtc->pipe) {
4574 case PIPE_A:
4575 break;
4576 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004577 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004578 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004579 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004580 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004581
4582 break;
4583 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004584 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004585
4586 break;
4587 default:
4588 BUG();
4589 }
4590}
4591
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004592/* Return which DP Port should be selected for Transcoder DP control */
4593static enum port
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004594intel_trans_dp_port_sel(struct intel_crtc *crtc)
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004595{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004596 struct drm_device *dev = crtc->base.dev;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004597 struct intel_encoder *encoder;
4598
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004599 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004600 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004601 encoder->type == INTEL_OUTPUT_EDP)
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02004602 return encoder->port;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004603 }
4604
4605 return -1;
4606}
4607
Jesse Barnesf67a5592011-01-05 10:31:48 -08004608/*
4609 * Enable PCH resources required for PCH ports:
4610 * - PCH PLLs
4611 * - FDI training & RX/TX
4612 * - update transcoder timings
4613 * - DP transcoding bits
4614 * - transcoder
4615 */
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004616static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08004617{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004618 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004619 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004620 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004621 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004622 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004623
Daniel Vetterab9412b2013-05-03 11:49:46 +02004624 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004625
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004626 if (IS_IVYBRIDGE(dev_priv))
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004627 ivybridge_update_fdi_bc_bifurcation(crtc);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004628
Daniel Vettercd986ab2012-10-26 10:58:12 +02004629 /* Write the TU size bits before fdi link training, so that error
4630 * detection works. */
4631 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4632 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4633
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004634 /* For PCH output, training FDI link */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004635 dev_priv->display.fdi_link_train(crtc, crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004636
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004637 /* We need to program the right clock selection before writing the pixel
4638 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004639 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004640 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004641
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004642 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004643 temp |= TRANS_DPLL_ENABLE(pipe);
4644 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004645 if (crtc_state->shared_dpll ==
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004646 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004647 temp |= sel;
4648 else
4649 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004650 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004651 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004652
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004653 /* XXX: pch pll's can be enabled any time before we enable the PCH
4654 * transcoder, and we actually should do this to not upset any PCH
4655 * transcoder that already use the clock when we share it.
4656 *
4657 * Note that enable_shared_dpll tries to do the right thing, but
4658 * get_shared_dpll unconditionally resets the pll - we need that to have
4659 * the right LVDS enable sequence. */
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004660 intel_enable_shared_dpll(crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004661
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004662 /* set transcoder timing, panel must allow it */
4663 assert_panel_unlocked(dev_priv, pipe);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004664 ironlake_pch_transcoder_set_timings(crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004665
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004666 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004667
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004668 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004669 if (HAS_PCH_CPT(dev_priv) &&
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004670 intel_crtc_has_dp_encoder(crtc_state)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004671 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004672 &crtc_state->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004673 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004674 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004675 temp = I915_READ(reg);
4676 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004677 TRANS_DP_SYNC_MASK |
4678 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004679 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004680 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004681
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004682 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004683 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004684 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004685 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004686
4687 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004688 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004689 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004690 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004691 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004692 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004693 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004694 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004695 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004696 break;
4697 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004698 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004699 }
4700
Chris Wilson5eddb702010-09-11 13:48:45 +01004701 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004702 }
4703
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004704 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004705}
4706
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004707static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004708{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004709 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004710 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004711 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004712
Matthias Kaehlckea2196032017-07-17 11:14:03 -07004713 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004714
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004715 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004716
Paulo Zanoni0540e482012-10-31 18:12:40 -02004717 /* Set transcoder timing. */
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004718 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004719
Paulo Zanoni937bb612012-10-31 18:12:47 -02004720 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004721}
4722
Daniel Vettera1520312013-05-03 11:49:50 +02004723static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004724{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004725 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004726 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004727 u32 temp;
4728
4729 temp = I915_READ(dslreg);
4730 udelay(500);
4731 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004732 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004733 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004734 }
4735}
4736
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004737static int
4738skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004739 unsigned int scaler_user, int *scaler_id,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304740 int src_w, int src_h, int dst_w, int dst_h,
4741 bool plane_scaler_check,
4742 uint32_t pixel_format)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004743{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004744 struct intel_crtc_scaler_state *scaler_state =
4745 &crtc_state->scaler_state;
4746 struct intel_crtc *intel_crtc =
4747 to_intel_crtc(crtc_state->base.crtc);
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304748 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4749 const struct drm_display_mode *adjusted_mode =
4750 &crtc_state->base.adjusted_mode;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004751 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004752
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004753 /*
4754 * Src coordinates are already rotated by 270 degrees for
4755 * the 90/270 degree plane rotation cases (to match the
4756 * GTT mapping), hence no need to account for rotation here.
4757 */
4758 need_scaling = src_w != dst_w || src_h != dst_h;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004759
Chandra Konduru77224cd2018-04-09 09:11:13 +05304760 if (plane_scaler_check)
4761 if (pixel_format == DRM_FORMAT_NV12)
4762 need_scaling = true;
4763
Shashank Sharmae5c05932017-07-21 20:55:05 +05304764 if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
4765 need_scaling = true;
4766
Chandra Kondurua1b22782015-04-07 15:28:45 -07004767 /*
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304768 * Scaling/fitting not supported in IF-ID mode in GEN9+
4769 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4770 * Once NV12 is enabled, handle it here while allocating scaler
4771 * for NV12.
4772 */
4773 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
4774 need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4775 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4776 return -EINVAL;
4777 }
4778
4779 /*
Chandra Kondurua1b22782015-04-07 15:28:45 -07004780 * if plane is being disabled or scaler is no more required or force detach
4781 * - free scaler binded to this plane/crtc
4782 * - in order to do this, update crtc->scaler_usage
4783 *
4784 * Here scaler state in crtc_state is set free so that
4785 * scaler can be assigned to other user. Actual register
4786 * update to free the scaler is done in plane/panel-fit programming.
4787 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4788 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004789 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004790 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004791 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004792 scaler_state->scalers[*scaler_id].in_use = 0;
4793
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004794 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4795 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4796 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004797 scaler_state->scaler_users);
4798 *scaler_id = -1;
4799 }
4800 return 0;
4801 }
4802
Chandra Konduru77224cd2018-04-09 09:11:13 +05304803 if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
Maarten Lankhorst5d794282018-05-12 03:03:14 +05304804 (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
Chandra Konduru77224cd2018-04-09 09:11:13 +05304805 DRM_DEBUG_KMS("NV12: src dimensions not met\n");
4806 return -EINVAL;
4807 }
4808
Chandra Kondurua1b22782015-04-07 15:28:45 -07004809 /* range checks */
4810 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
Nabendu Maiti323301a2018-03-23 10:24:18 -07004811 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4812 (IS_GEN11(dev_priv) &&
4813 (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
4814 dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
4815 (!IS_GEN11(dev_priv) &&
4816 (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4817 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004818 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004819 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004820 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004821 return -EINVAL;
4822 }
4823
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004824 /* mark this plane as a scaler user in crtc_state */
4825 scaler_state->scaler_users |= (1 << scaler_user);
4826 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4827 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4828 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4829 scaler_state->scaler_users);
4830
4831 return 0;
4832}
4833
4834/**
4835 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4836 *
4837 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004838 *
4839 * Return
4840 * 0 - scaler_usage updated successfully
4841 * error - requested scaling cannot be supported or other error condition
4842 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004843int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004844{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004845 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004846
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004847 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304848 &state->scaler_state.scaler_id,
4849 state->pipe_src_w, state->pipe_src_h,
4850 adjusted_mode->crtc_hdisplay,
4851 adjusted_mode->crtc_vdisplay, false, 0);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004852}
4853
4854/**
4855 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
Chris Wilsonc38c1452018-02-14 13:49:22 +00004856 * @crtc_state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004857 * @plane_state: atomic plane state to update
4858 *
4859 * Return
4860 * 0 - scaler_usage updated successfully
4861 * error - requested scaling cannot be supported or other error condition
4862 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004863static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4864 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004865{
4866
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004867 struct intel_plane *intel_plane =
4868 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004869 struct drm_framebuffer *fb = plane_state->base.fb;
4870 int ret;
4871
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004872 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004873
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004874 ret = skl_update_scaler(crtc_state, force_detach,
4875 drm_plane_index(&intel_plane->base),
4876 &plane_state->scaler_id,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004877 drm_rect_width(&plane_state->base.src) >> 16,
4878 drm_rect_height(&plane_state->base.src) >> 16,
4879 drm_rect_width(&plane_state->base.dst),
Chandra Konduru77224cd2018-04-09 09:11:13 +05304880 drm_rect_height(&plane_state->base.dst),
4881 fb ? true : false, fb ? fb->format->format : 0);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004882
4883 if (ret || plane_state->scaler_id < 0)
4884 return ret;
4885
Chandra Kondurua1b22782015-04-07 15:28:45 -07004886 /* check colorkey */
Ville Syrjälä6ec5bd32018-02-02 22:42:31 +02004887 if (plane_state->ckey.flags) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004888 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4889 intel_plane->base.base.id,
4890 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004891 return -EINVAL;
4892 }
4893
4894 /* Check src format */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004895 switch (fb->format->format) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004896 case DRM_FORMAT_RGB565:
4897 case DRM_FORMAT_XBGR8888:
4898 case DRM_FORMAT_XRGB8888:
4899 case DRM_FORMAT_ABGR8888:
4900 case DRM_FORMAT_ARGB8888:
4901 case DRM_FORMAT_XRGB2101010:
4902 case DRM_FORMAT_XBGR2101010:
4903 case DRM_FORMAT_YUYV:
4904 case DRM_FORMAT_YVYU:
4905 case DRM_FORMAT_UYVY:
4906 case DRM_FORMAT_VYUY:
Chandra Konduru77224cd2018-04-09 09:11:13 +05304907 case DRM_FORMAT_NV12:
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004908 break;
4909 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004910 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4911 intel_plane->base.base.id, intel_plane->base.name,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004912 fb->base.id, fb->format->format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004913 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004914 }
4915
Chandra Kondurua1b22782015-04-07 15:28:45 -07004916 return 0;
4917}
4918
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004919static void skylake_scaler_disable(struct intel_crtc *crtc)
4920{
4921 int i;
4922
4923 for (i = 0; i < crtc->num_scalers; i++)
4924 skl_detach_scaler(crtc, i);
4925}
4926
4927static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004928{
4929 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004930 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004931 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004932 struct intel_crtc_scaler_state *scaler_state =
4933 &crtc->config->scaler_state;
4934
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004935 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004936 int id;
4937
Ville Syrjäläc3f8ad52017-03-07 22:54:19 +02004938 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
Chandra Kondurua1b22782015-04-07 15:28:45 -07004939 return;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004940
4941 id = scaler_state->scaler_id;
4942 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4943 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4944 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4945 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004946 }
4947}
4948
Jesse Barnesb074cec2013-04-25 12:55:02 -07004949static void ironlake_pfit_enable(struct intel_crtc *crtc)
4950{
4951 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004952 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004953 int pipe = crtc->pipe;
4954
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004955 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004956 /* Force use of hard-coded filter coefficients
4957 * as some pre-programmed values are broken,
4958 * e.g. x201.
4959 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004960 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07004961 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4962 PF_PIPE_SEL_IVB(pipe));
4963 else
4964 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004965 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4966 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004967 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004968}
4969
Maarten Lankhorst199ea382017-11-10 12:35:00 +01004970void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004971{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01004972 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004973 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004974 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004975
Maarten Lankhorst24f28452017-11-22 19:39:01 +01004976 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004977 return;
4978
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004979 /*
4980 * We can only enable IPS after we enable a plane and wait for a vblank
4981 * This function is called from post_plane_update, which is run after
4982 * a vblank wait.
4983 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01004984 WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02004985
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004986 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01004987 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjälä61843f02017-09-12 18:34:11 +03004988 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
4989 IPS_ENABLE | IPS_PCODE_CONTROL));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01004990 mutex_unlock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004991 /* Quoting Art Runyan: "its not safe to expect any particular
4992 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004993 * mailbox." Moreover, the mailbox may return a bogus state,
4994 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004995 */
4996 } else {
4997 I915_WRITE(IPS_CTL, IPS_ENABLE);
4998 /* The bit only becomes 1 in the next vblank, so this wait here
4999 * is essentially intel_wait_for_vblank. If we don't have this
5000 * and don't wait for vblanks until the end of crtc_enable, then
5001 * the HW state readout code will complain that the expected
5002 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01005003 if (intel_wait_for_register(dev_priv,
5004 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
5005 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005006 DRM_ERROR("Timed out waiting for IPS enable\n");
5007 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005008}
5009
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005010void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005011{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005012 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005013 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005014 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005015
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005016 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005017 return;
5018
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005019 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005020 mutex_lock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005021 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005022 mutex_unlock(&dev_priv->pcu_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07005023 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01005024 if (intel_wait_for_register(dev_priv,
5025 IPS_CTL, IPS_ENABLE, 0,
5026 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07005027 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08005028 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005029 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08005030 POSTING_READ(IPS_CTL);
5031 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005032
5033 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005034 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005035}
5036
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005037static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005038{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005039 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005040 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005041
5042 mutex_lock(&dev->struct_mutex);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005043 (void) intel_overlay_switch_off(intel_crtc->overlay);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005044 mutex_unlock(&dev->struct_mutex);
5045 }
5046
5047 /* Let userspace switch the overlay on again. In most cases userspace
5048 * has to recompute where to put it anyway.
5049 */
5050}
5051
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005052/**
5053 * intel_post_enable_primary - Perform operations after enabling primary plane
5054 * @crtc: the CRTC whose primary plane was just enabled
Chris Wilsonc38c1452018-02-14 13:49:22 +00005055 * @new_crtc_state: the enabling state
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005056 *
5057 * Performs potentially sleeping operations that must be done after the primary
5058 * plane is enabled, such as updating FBC and IPS. Note that this may be
5059 * called due to an explicit primary plane update, or due to an implicit
5060 * re-enable that is caused when a sprite plane is updated to no longer
5061 * completely hide the primary plane.
5062 */
5063static void
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005064intel_post_enable_primary(struct drm_crtc *crtc,
5065 const struct intel_crtc_state *new_crtc_state)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005066{
5067 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005068 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5070 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005071
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005072 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005073 * Gen2 reports pipe underruns whenever all planes are disabled.
5074 * So don't enable underrun reporting before at least some planes
5075 * are enabled.
5076 * FIXME: Need to fix the logic to work when we turn off all planes
5077 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02005078 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005079 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005080 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5081
Ville Syrjäläaca7b682015-10-30 19:22:21 +02005082 /* Underruns don't always raise interrupts, so check manually. */
5083 intel_check_cpu_fifo_underruns(dev_priv);
5084 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005085}
5086
Ville Syrjälä2622a082016-03-09 19:07:26 +02005087/* FIXME get rid of this and use pre_plane_update */
5088static void
5089intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5090{
5091 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005092 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5094 int pipe = intel_crtc->pipe;
5095
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005096 /*
5097 * Gen2 reports pipe underruns whenever all planes are disabled.
5098 * So disable underrun reporting before all the planes get disabled.
5099 */
5100 if (IS_GEN2(dev_priv))
5101 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5102
5103 hsw_disable_ips(to_intel_crtc_state(crtc->state));
Ville Syrjälä2622a082016-03-09 19:07:26 +02005104
5105 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005106 * Vblank time updates from the shadow to live plane control register
5107 * are blocked if the memory self-refresh mode is active at that
5108 * moment. So to make sure the plane gets truly disabled, disable
5109 * first the self-refresh mode. The self-refresh enable bit in turn
5110 * will be checked/applied by the HW only at the next frame start
5111 * event which is after the vblank start event, so we need to have a
5112 * wait-for-vblank between disabling the plane and the pipe.
5113 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +02005114 if (HAS_GMCH_DISPLAY(dev_priv) &&
5115 intel_set_memory_cxsr(dev_priv, false))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005116 intel_wait_for_vblank(dev_priv, pipe);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005117}
5118
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005119static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
5120 const struct intel_crtc_state *new_crtc_state)
5121{
5122 if (!old_crtc_state->ips_enabled)
5123 return false;
5124
5125 if (needs_modeset(&new_crtc_state->base))
5126 return true;
5127
5128 return !new_crtc_state->ips_enabled;
5129}
5130
5131static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
5132 const struct intel_crtc_state *new_crtc_state)
5133{
5134 if (!new_crtc_state->ips_enabled)
5135 return false;
5136
5137 if (needs_modeset(&new_crtc_state->base))
5138 return true;
5139
5140 /*
5141 * We can't read out IPS on broadwell, assume the worst and
5142 * forcibly enable IPS on the first fastset.
5143 */
5144 if (new_crtc_state->update_pipe &&
5145 old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
5146 return true;
5147
5148 return !old_crtc_state->ips_enabled;
5149}
5150
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305151static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
5152 const struct intel_crtc_state *crtc_state)
5153{
5154 if (!crtc_state->nv12_planes)
5155 return false;
5156
5157 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
5158 return false;
5159
5160 if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
5161 IS_CANNONLAKE(dev_priv))
5162 return true;
5163
5164 return false;
5165}
5166
Daniel Vetter5a21b662016-05-24 17:13:53 +02005167static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5168{
5169 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +05305170 struct drm_device *dev = crtc->base.dev;
5171 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005172 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5173 struct intel_crtc_state *pipe_config =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005174 intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
5175 crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005176 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005177 struct drm_plane_state *old_primary_state =
5178 drm_atomic_get_old_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005179
Chris Wilson5748b6a2016-08-04 16:32:38 +01005180 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005181
Daniel Vetter5a21b662016-05-24 17:13:53 +02005182 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005183 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005184
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005185 if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
5186 hsw_enable_ips(pipe_config);
5187
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005188 if (old_primary_state) {
5189 struct drm_plane_state *new_primary_state =
5190 drm_atomic_get_new_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005191
5192 intel_fbc_post_update(crtc);
5193
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005194 if (new_primary_state->visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005195 (needs_modeset(&pipe_config->base) ||
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005196 !old_primary_state->visible))
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005197 intel_post_enable_primary(&crtc->base, pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005198 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305199
5200 /* Display WA 827 */
5201 if (needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305202 !needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305203 skl_wa_clkgate(dev_priv, crtc->pipe, false);
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305204 skl_wa_528(dev_priv, crtc->pipe, false);
5205 }
Daniel Vetter5a21b662016-05-24 17:13:53 +02005206}
5207
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005208static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5209 struct intel_crtc_state *pipe_config)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005210{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005211 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005212 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005213 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005214 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5215 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005216 struct drm_plane_state *old_primary_state =
5217 drm_atomic_get_old_plane_state(old_state, primary);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005218 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005219 struct intel_atomic_state *old_intel_state =
5220 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005221
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005222 if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
5223 hsw_disable_ips(old_crtc_state);
5224
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005225 if (old_primary_state) {
5226 struct intel_plane_state *new_primary_state =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005227 intel_atomic_get_new_plane_state(old_intel_state,
5228 to_intel_plane(primary));
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005229
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005230 intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005231 /*
5232 * Gen2 reports pipe underruns whenever all planes are disabled.
5233 * So disable underrun reporting before all the planes get disabled.
5234 */
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005235 if (IS_GEN2(dev_priv) && old_primary_state->visible &&
5236 (modeset || !new_primary_state->base.visible))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005237 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005238 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005239
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305240 /* Display WA 827 */
5241 if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305242 needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305243 skl_wa_clkgate(dev_priv, crtc->pipe, true);
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305244 skl_wa_528(dev_priv, crtc->pipe, true);
5245 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305246
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02005247 /*
5248 * Vblank time updates from the shadow to live plane control register
5249 * are blocked if the memory self-refresh mode is active at that
5250 * moment. So to make sure the plane gets truly disabled, disable
5251 * first the self-refresh mode. The self-refresh enable bit in turn
5252 * will be checked/applied by the HW only at the next frame start
5253 * event which is after the vblank start event, so we need to have a
5254 * wait-for-vblank between disabling the plane and the pipe.
5255 */
5256 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5257 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5258 intel_wait_for_vblank(dev_priv, crtc->pipe);
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005259
Matt Ropered4a6a72016-02-23 17:20:13 -08005260 /*
5261 * IVB workaround: must disable low power watermarks for at least
5262 * one frame before enabling scaling. LP watermarks can be re-enabled
5263 * when scaling is disabled.
5264 *
5265 * WaCxSRDisabledForSpriteScaling:ivb
5266 */
Ville Syrjäläddd2b792016-11-28 19:37:04 +02005267 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005268 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005269
5270 /*
5271 * If we're doing a modeset, we're done. No need to do any pre-vblank
5272 * watermark programming here.
5273 */
5274 if (needs_modeset(&pipe_config->base))
5275 return;
5276
5277 /*
5278 * For platforms that support atomic watermarks, program the
5279 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5280 * will be the intermediate values that are safe for both pre- and
5281 * post- vblank; when vblank happens, the 'active' values will be set
5282 * to the final 'target' values and we'll do this again to get the
5283 * optimal watermarks. For gen9+ platforms, the values we program here
5284 * will be the final target values which will get automatically latched
5285 * at vblank time; no further programming will be necessary.
5286 *
5287 * If a platform hasn't been transitioned to atomic watermarks yet,
5288 * we'll continue to update watermarks the old way, if flags tell
5289 * us to.
5290 */
5291 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005292 dev_priv->display.initial_watermarks(old_intel_state,
5293 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005294 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005295 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005296}
5297
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005298static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005299{
5300 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005301 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005302 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005303 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005304
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005305 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005306
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005307 drm_for_each_plane_mask(p, dev, plane_mask)
Ville Syrjälä282dbf92017-03-27 21:55:33 +03005308 to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005309
Daniel Vetterf99d7062014-06-19 16:01:59 +02005310 /*
5311 * FIXME: Once we grow proper nuclear flip support out of this we need
5312 * to compute the mask of flip planes precisely. For the time being
5313 * consider this a flip to a NULL plane.
5314 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01005315 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005316}
5317
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005318static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005319 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005320 struct drm_atomic_state *old_state)
5321{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005322 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005323 struct drm_connector *conn;
5324 int i;
5325
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005326 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005327 struct intel_encoder *encoder =
5328 to_intel_encoder(conn_state->best_encoder);
5329
5330 if (conn_state->crtc != crtc)
5331 continue;
5332
5333 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005334 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005335 }
5336}
5337
5338static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005339 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005340 struct drm_atomic_state *old_state)
5341{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005342 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005343 struct drm_connector *conn;
5344 int i;
5345
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005346 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005347 struct intel_encoder *encoder =
5348 to_intel_encoder(conn_state->best_encoder);
5349
5350 if (conn_state->crtc != crtc)
5351 continue;
5352
5353 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005354 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005355 }
5356}
5357
5358static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005359 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005360 struct drm_atomic_state *old_state)
5361{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005362 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005363 struct drm_connector *conn;
5364 int i;
5365
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005366 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005367 struct intel_encoder *encoder =
5368 to_intel_encoder(conn_state->best_encoder);
5369
5370 if (conn_state->crtc != crtc)
5371 continue;
5372
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005373 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005374 intel_opregion_notify_encoder(encoder, true);
5375 }
5376}
5377
5378static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005379 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005380 struct drm_atomic_state *old_state)
5381{
5382 struct drm_connector_state *old_conn_state;
5383 struct drm_connector *conn;
5384 int i;
5385
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005386 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005387 struct intel_encoder *encoder =
5388 to_intel_encoder(old_conn_state->best_encoder);
5389
5390 if (old_conn_state->crtc != crtc)
5391 continue;
5392
5393 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005394 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005395 }
5396}
5397
5398static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005399 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005400 struct drm_atomic_state *old_state)
5401{
5402 struct drm_connector_state *old_conn_state;
5403 struct drm_connector *conn;
5404 int i;
5405
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005406 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005407 struct intel_encoder *encoder =
5408 to_intel_encoder(old_conn_state->best_encoder);
5409
5410 if (old_conn_state->crtc != crtc)
5411 continue;
5412
5413 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005414 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005415 }
5416}
5417
5418static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005419 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005420 struct drm_atomic_state *old_state)
5421{
5422 struct drm_connector_state *old_conn_state;
5423 struct drm_connector *conn;
5424 int i;
5425
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005426 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005427 struct intel_encoder *encoder =
5428 to_intel_encoder(old_conn_state->best_encoder);
5429
5430 if (old_conn_state->crtc != crtc)
5431 continue;
5432
5433 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005434 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005435 }
5436}
5437
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005438static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5439 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005440{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005441 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005442 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005443 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5445 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005446 struct intel_atomic_state *old_intel_state =
5447 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005448
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005449 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005450 return;
5451
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005452 /*
5453 * Sometimes spurious CPU pipe underruns happen during FDI
5454 * training, at least with VGA+HDMI cloning. Suppress them.
5455 *
5456 * On ILK we get an occasional spurious CPU pipe underruns
5457 * between eDP port A enable and vdd enable. Also PCH port
5458 * enable seems to result in the occasional CPU pipe underrun.
5459 *
5460 * Spurious PCH underruns also occur during PCH enabling.
5461 */
5462 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5463 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005464 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005465 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5466
5467 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005468 intel_prepare_shared_dpll(intel_crtc);
5469
Ville Syrjälä37a56502016-06-22 21:57:04 +03005470 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305471 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005472
5473 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005474 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005475
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005476 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005477 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005478 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005479 }
5480
5481 ironlake_set_pipeconf(crtc);
5482
Jesse Barnesf67a5592011-01-05 10:31:48 -08005483 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005484
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005485 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005486
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005487 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005488 /* Note: FDI PLL enabling _must_ be done before we enable the
5489 * cpu pipes, hence this is separate from all the other fdi/pch
5490 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005491 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005492 } else {
5493 assert_fdi_tx_disabled(dev_priv, pipe);
5494 assert_fdi_rx_disabled(dev_priv, pipe);
5495 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005496
Jesse Barnesb074cec2013-04-25 12:55:02 -07005497 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005498
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005499 /*
5500 * On ILK+ LUT must be loaded before the pipe is running but with
5501 * clocks enabled
5502 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005503 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005504
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005505 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005506 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02005507 intel_enable_pipe(pipe_config);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005508
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005509 if (intel_crtc->config->has_pch_encoder)
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02005510 ironlake_pch_enable(pipe_config);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005511
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005512 assert_vblank_disabled(crtc);
5513 drm_crtc_vblank_on(crtc);
5514
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005515 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005516
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005517 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005518 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005519
5520 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5521 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005522 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005523 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005524 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005525}
5526
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005527/* IPS only exists on ULT machines and is tied to pipe A. */
5528static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5529{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005530 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005531}
5532
Imre Deaked69cd42017-10-02 10:55:57 +03005533static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
5534 enum pipe pipe, bool apply)
5535{
5536 u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
5537 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
5538
5539 if (apply)
5540 val |= mask;
5541 else
5542 val &= ~mask;
5543
5544 I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
5545}
5546
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005547static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
5548{
5549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5550 enum pipe pipe = crtc->pipe;
5551 uint32_t val;
5552
5553 val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
5554
5555 /* Program B credit equally to all pipes */
5556 val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
5557
5558 I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
5559}
5560
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005561static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5562 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005563{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005564 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005565 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005566 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005567 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005568 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005569 struct intel_atomic_state *old_intel_state =
5570 to_intel_atomic_state(old_state);
Imre Deaked69cd42017-10-02 10:55:57 +03005571 bool psl_clkgate_wa;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005572
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005573 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005574 return;
5575
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005576 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005577
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005578 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005579 intel_enable_shared_dpll(intel_crtc);
5580
Paulo Zanonic27e9172018-04-27 16:14:36 -07005581 if (INTEL_GEN(dev_priv) >= 11)
5582 icl_map_plls_to_ports(crtc, pipe_config, old_state);
5583
Paulo Zanonic8af5272018-05-02 14:58:51 -07005584 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5585
5586 if (!transcoder_is_dsi(cpu_transcoder))
5587 intel_ddi_enable_pipe_clock(pipe_config);
5588
Ville Syrjälä37a56502016-06-22 21:57:04 +03005589 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305590 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005591
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005592 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005593 intel_set_pipe_timings(intel_crtc);
5594
Jani Nikulabc58be62016-03-18 17:05:39 +02005595 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005596
Jani Nikula4d1de972016-03-18 17:05:42 +02005597 if (cpu_transcoder != TRANSCODER_EDP &&
5598 !transcoder_is_dsi(cpu_transcoder)) {
5599 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005600 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005601 }
5602
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005603 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005604 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005605 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005606 }
5607
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005608 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005609 haswell_set_pipeconf(crtc);
5610
Jani Nikula391bf042016-03-18 17:05:40 +02005611 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005612
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005613 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005614
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005615 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005616
Imre Deaked69cd42017-10-02 10:55:57 +03005617 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
5618 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
5619 intel_crtc->config->pch_pfit.enabled;
5620 if (psl_clkgate_wa)
5621 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
5622
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005623 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005624 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005625 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005626 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005627
5628 /*
5629 * On ILK+ LUT must be loaded before the pipe is running but with
5630 * clocks enabled
5631 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005632 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005633
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005634 intel_ddi_set_pipe_settings(pipe_config);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005635 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005636 intel_ddi_enable_transcoder_func(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005637
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005638 if (dev_priv->display.initial_watermarks != NULL)
Ville Syrjälä3125d392016-11-28 19:37:03 +02005639 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005640
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005641 if (INTEL_GEN(dev_priv) >= 11)
5642 icl_pipe_mbus_enable(intel_crtc);
5643
Jani Nikula4d1de972016-03-18 17:05:42 +02005644 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005645 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005646 intel_enable_pipe(pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005647
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005648 if (intel_crtc->config->has_pch_encoder)
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02005649 lpt_pch_enable(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005650
Ville Syrjälä00370712016-11-14 19:44:06 +02005651 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005652 intel_ddi_set_vc_payload_alloc(pipe_config, true);
Dave Airlie0e32b392014-05-02 14:02:48 +10005653
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005654 assert_vblank_disabled(crtc);
5655 drm_crtc_vblank_on(crtc);
5656
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005657 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005658
Imre Deaked69cd42017-10-02 10:55:57 +03005659 if (psl_clkgate_wa) {
5660 intel_wait_for_vblank(dev_priv, pipe);
5661 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
5662 }
5663
Paulo Zanonie4916942013-09-20 16:21:19 -03005664 /* If we change the relative order between pipe/planes enabling, we need
5665 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005666 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005667 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005668 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5669 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005670 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005671}
5672
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005673static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005674{
5675 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005676 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005677 int pipe = crtc->pipe;
5678
5679 /* To avoid upsetting the power well on haswell only disable the pfit if
5680 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005681 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005682 I915_WRITE(PF_CTL(pipe), 0);
5683 I915_WRITE(PF_WIN_POS(pipe), 0);
5684 I915_WRITE(PF_WIN_SZ(pipe), 0);
5685 }
5686}
5687
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005688static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5689 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005690{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005691 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005692 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005693 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005694 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5695 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005696
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005697 /*
5698 * Sometimes spurious CPU pipe underruns happen when the
5699 * pipe is already disabled, but FDI RX/TX is still enabled.
5700 * Happens at least with VGA+HDMI cloning. Suppress them.
5701 */
5702 if (intel_crtc->config->has_pch_encoder) {
5703 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005704 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005705 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005706
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005707 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005708
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005709 drm_crtc_vblank_off(crtc);
5710 assert_vblank_disabled(crtc);
5711
Ville Syrjälä4972f702017-11-29 17:37:32 +02005712 intel_disable_pipe(old_crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005713
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005714 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005715
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005716 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005717 ironlake_fdi_disable(crtc);
5718
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005719 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005720
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005721 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005722 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005723
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005724 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005725 i915_reg_t reg;
5726 u32 temp;
5727
Daniel Vetterd925c592013-06-05 13:34:04 +02005728 /* disable TRANS_DP_CTL */
5729 reg = TRANS_DP_CTL(pipe);
5730 temp = I915_READ(reg);
5731 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5732 TRANS_DP_PORT_SEL_MASK);
5733 temp |= TRANS_DP_PORT_SEL_NONE;
5734 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005735
Daniel Vetterd925c592013-06-05 13:34:04 +02005736 /* disable DPLL_SEL */
5737 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005738 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005739 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005740 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005741
Daniel Vetterd925c592013-06-05 13:34:04 +02005742 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005743 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005744
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005745 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005746 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005747}
5748
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005749static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5750 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005751{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005752 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005753 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005755 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005756
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005757 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005758
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005759 drm_crtc_vblank_off(crtc);
5760 assert_vblank_disabled(crtc);
5761
Jani Nikula4d1de972016-03-18 17:05:42 +02005762 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005763 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005764 intel_disable_pipe(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005765
Ville Syrjälä00370712016-11-14 19:44:06 +02005766 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005767 intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005768
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005769 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305770 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005771
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005772 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005773 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005774 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005775 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005776
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005777 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005778 intel_ddi_disable_pipe_clock(intel_crtc->config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005779
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005780 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Paulo Zanonic27e9172018-04-27 16:14:36 -07005781
5782 if (INTEL_GEN(dev_priv) >= 11)
5783 icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005784}
5785
Jesse Barnes2dd24552013-04-25 12:55:01 -07005786static void i9xx_pfit_enable(struct intel_crtc *crtc)
5787{
5788 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005789 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005790 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005791
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005792 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005793 return;
5794
Daniel Vetterc0b03412013-05-28 12:05:54 +02005795 /*
5796 * The panel fitter should only be adjusted whilst the pipe is disabled,
5797 * according to register description and PRM.
5798 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005799 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5800 assert_pipe_disabled(dev_priv, crtc->pipe);
5801
Jesse Barnesb074cec2013-04-25 12:55:02 -07005802 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5803 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005804
5805 /* Border color in case we don't scale up to the full screen. Black by
5806 * default, change to something else for debugging. */
5807 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005808}
5809
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005810enum intel_display_power_domain intel_port_to_power_domain(enum port port)
Dave Airlied05410f2014-06-05 13:22:59 +10005811{
5812 switch (port) {
5813 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005814 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005815 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005816 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005817 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005818 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005819 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005820 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005821 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005822 return POWER_DOMAIN_PORT_DDI_E_LANES;
Rodrigo Vivi9787e832018-01-29 15:22:22 -08005823 case PORT_F:
5824 return POWER_DOMAIN_PORT_DDI_F_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005825 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005826 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005827 return POWER_DOMAIN_PORT_OTHER;
5828 }
5829}
5830
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005831static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5832 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005833{
5834 struct drm_device *dev = crtc->dev;
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005835 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005836 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5838 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005839 u64 mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005840 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005841
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005842 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005843 return 0;
5844
Imre Deak17bd6e62018-01-09 14:20:40 +02005845 mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
5846 mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005847 if (crtc_state->pch_pfit.enabled ||
5848 crtc_state->pch_pfit.force_thru)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005849 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
Imre Deak77d22dc2014-03-05 16:20:52 +02005850
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005851 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5852 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5853
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005854 mask |= BIT_ULL(intel_encoder->power_domain);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005855 }
Imre Deak319be8a2014-03-04 19:22:57 +02005856
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005857 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
Imre Deak17bd6e62018-01-09 14:20:40 +02005858 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005859
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005860 if (crtc_state->shared_dpll)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005861 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005862
Imre Deak77d22dc2014-03-05 16:20:52 +02005863 return mask;
5864}
5865
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005866static u64
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005867modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5868 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005869{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005870 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5872 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005873 u64 domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005874
5875 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005876 intel_crtc->enabled_power_domains = new_domains =
5877 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005878
Daniel Vetter5a21b662016-05-24 17:13:53 +02005879 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005880
5881 for_each_power_domain(domain, domains)
5882 intel_display_power_get(dev_priv, domain);
5883
Daniel Vetter5a21b662016-05-24 17:13:53 +02005884 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005885}
5886
5887static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005888 u64 domains)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005889{
5890 enum intel_display_power_domain domain;
5891
5892 for_each_power_domain(domain, domains)
5893 intel_display_power_put(dev_priv, domain);
5894}
5895
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005896static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5897 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005898{
Ville Syrjäläff32c542017-03-02 19:14:57 +02005899 struct intel_atomic_state *old_intel_state =
5900 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005901 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005902 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005903 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005905 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005906
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005907 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005908 return;
5909
Ville Syrjälä37a56502016-06-22 21:57:04 +03005910 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305911 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005912
5913 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005914 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005915
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005916 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01005917 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005918
5919 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5920 I915_WRITE(CHV_CANVAS(pipe), 0);
5921 }
5922
Daniel Vetter5b18e572014-04-24 23:55:06 +02005923 i9xx_set_pipeconf(intel_crtc);
5924
Jesse Barnes89b667f2013-04-18 14:51:36 -07005925 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005926
Daniel Vettera72e4c92014-09-30 10:56:47 +02005927 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005928
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005929 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005930
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005931 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03005932 chv_prepare_pll(intel_crtc, intel_crtc->config);
5933 chv_enable_pll(intel_crtc, intel_crtc->config);
5934 } else {
5935 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5936 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005937 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005938
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005939 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005940
Jesse Barnes2dd24552013-04-25 12:55:01 -07005941 i9xx_pfit_enable(intel_crtc);
5942
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005943 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005944
Ville Syrjäläff32c542017-03-02 19:14:57 +02005945 dev_priv->display.initial_watermarks(old_intel_state,
5946 pipe_config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02005947 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005948
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005949 assert_vblank_disabled(crtc);
5950 drm_crtc_vblank_on(crtc);
5951
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005952 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005953}
5954
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005955static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5956{
5957 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005958 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005959
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005960 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5961 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005962}
5963
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005964static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5965 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005966{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005967 struct intel_atomic_state *old_intel_state =
5968 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005969 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005970 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005971 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03005973 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005974
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005975 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005976 return;
5977
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005978 i9xx_set_pll_dividers(intel_crtc);
5979
Ville Syrjälä37a56502016-06-22 21:57:04 +03005980 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305981 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005982
5983 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005984 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005985
Daniel Vetter5b18e572014-04-24 23:55:06 +02005986 i9xx_set_pipeconf(intel_crtc);
5987
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005988 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005989
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005990 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005991 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005992
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005993 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005994
Ville Syrjälä939994d2017-09-13 17:08:56 +03005995 i9xx_enable_pll(intel_crtc, pipe_config);
Daniel Vetterf6736a12013-06-05 13:34:30 +02005996
Jesse Barnes2dd24552013-04-25 12:55:01 -07005997 i9xx_pfit_enable(intel_crtc);
5998
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005999 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006000
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006001 if (dev_priv->display.initial_watermarks != NULL)
6002 dev_priv->display.initial_watermarks(old_intel_state,
6003 intel_crtc->config);
6004 else
6005 intel_update_watermarks(intel_crtc);
Ville Syrjälä4972f702017-11-29 17:37:32 +02006006 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006007
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006008 assert_vblank_disabled(crtc);
6009 drm_crtc_vblank_on(crtc);
6010
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006011 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006012}
6013
Daniel Vetter87476d62013-04-11 16:29:06 +02006014static void i9xx_pfit_disable(struct intel_crtc *crtc)
6015{
6016 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006017 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006018
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006019 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006020 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006021
6022 assert_pipe_disabled(dev_priv, crtc->pipe);
6023
Daniel Vetter328d8e82013-05-08 10:36:31 +02006024 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6025 I915_READ(PFIT_CONTROL));
6026 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006027}
6028
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006029static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6030 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006031{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006032 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006033 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006034 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6036 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006037
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006038 /*
6039 * On gen2 planes are double buffered but the pipe isn't, so we must
6040 * wait for planes to fully turn off before disabling the pipe.
6041 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006042 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02006043 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006044
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006045 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006046
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006047 drm_crtc_vblank_off(crtc);
6048 assert_vblank_disabled(crtc);
6049
Ville Syrjälä4972f702017-11-29 17:37:32 +02006050 intel_disable_pipe(old_crtc_state);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006051
Daniel Vetter87476d62013-04-11 16:29:06 +02006052 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006053
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006054 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006055
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006056 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006057 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006058 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006059 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006060 vlv_disable_pll(dev_priv, pipe);
6061 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006062 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006063 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006064
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006065 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006066
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006067 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006068 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006069
6070 if (!dev_priv->display.initial_watermarks)
6071 intel_update_watermarks(intel_crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +03006072
6073 /* clock the pipe down to 640x480@60 to potentially save power */
6074 if (IS_I830(dev_priv))
6075 i830_enable_pipe(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006076}
6077
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006078static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
6079 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006080{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006081 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006083 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006084 enum intel_display_power_domain domain;
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006085 struct intel_plane *plane;
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02006086 u64 domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006087 struct drm_atomic_state *state;
6088 struct intel_crtc_state *crtc_state;
6089 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006090
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006091 if (!intel_crtc->active)
6092 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006093
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006094 for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
6095 const struct intel_plane_state *plane_state =
6096 to_intel_plane_state(plane->base.state);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006097
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006098 if (plane_state->base.visible)
6099 intel_plane_disable_noatomic(intel_crtc, plane);
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006100 }
6101
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006102 state = drm_atomic_state_alloc(crtc->dev);
Ander Conselvan de Oliveira31bb2ef2017-01-20 16:28:45 +02006103 if (!state) {
6104 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
6105 crtc->base.id, crtc->name);
6106 return;
6107 }
6108
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006109 state->acquire_ctx = ctx;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006110
6111 /* Everything's already locked, -EDEADLK can't happen. */
6112 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6113 ret = drm_atomic_add_affected_connectors(state, crtc);
6114
6115 WARN_ON(IS_ERR(crtc_state) || ret);
6116
6117 dev_priv->display.crtc_disable(crtc_state, state);
6118
Chris Wilson08536952016-10-14 13:18:18 +01006119 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006120
Ville Syrjälä78108b72016-05-27 20:59:19 +03006121 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6122 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006123
6124 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6125 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006126 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006127 crtc->enabled = false;
6128 crtc->state->connector_mask = 0;
6129 crtc->state->encoder_mask = 0;
6130
6131 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6132 encoder->base.crtc = NULL;
6133
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006134 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02006135 intel_update_watermarks(intel_crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006136 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006137
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006138 domains = intel_crtc->enabled_power_domains;
6139 for_each_power_domain(domain, domains)
6140 intel_display_power_put(dev_priv, domain);
6141 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006142
6143 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
Ville Syrjäläd305e062017-08-30 21:57:03 +03006144 dev_priv->min_cdclk[intel_crtc->pipe] = 0;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +03006145 dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006146}
6147
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006148/*
6149 * turn all crtc's off, but do not adjust state
6150 * This has to be paired with a call to intel_modeset_setup_hw_state.
6151 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006152int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006153{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006154 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006155 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006156 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006157
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006158 state = drm_atomic_helper_suspend(dev);
6159 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006160 if (ret)
6161 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006162 else
6163 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006164 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006165}
6166
Chris Wilsonea5b2132010-08-04 13:50:23 +01006167void intel_encoder_destroy(struct drm_encoder *encoder)
6168{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006169 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006170
Chris Wilsonea5b2132010-08-04 13:50:23 +01006171 drm_encoder_cleanup(encoder);
6172 kfree(intel_encoder);
6173}
6174
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006175/* Cross check the actual hw state with our own modeset state tracking (and it's
6176 * internal consistency). */
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006177static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
6178 struct drm_connector_state *conn_state)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006179{
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006180 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006181
6182 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6183 connector->base.base.id,
6184 connector->base.name);
6185
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006186 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006187 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006188
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006189 I915_STATE_WARN(!crtc_state,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006190 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006191
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006192 if (!crtc_state)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006193 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006194
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006195 I915_STATE_WARN(!crtc_state->active,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006196 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006197
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006198 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006199 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006200
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006201 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006202 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006203
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006204 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006205 "attached encoder crtc differs from connector crtc\n");
6206 } else {
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006207 I915_STATE_WARN(crtc_state && crtc_state->active,
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006208 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006209 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006210 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006211 }
6212}
6213
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006214int intel_connector_init(struct intel_connector *connector)
6215{
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02006216 struct intel_digital_connector_state *conn_state;
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006217
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02006218 /*
6219 * Allocate enough memory to hold intel_digital_connector_state,
6220 * This might be a few bytes too many, but for connectors that don't
6221 * need it we'll free the state and allocate a smaller one on the first
6222 * succesful commit anyway.
6223 */
6224 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
6225 if (!conn_state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006226 return -ENOMEM;
6227
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02006228 __drm_atomic_helper_connector_reset(&connector->base,
6229 &conn_state->base);
6230
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006231 return 0;
6232}
6233
6234struct intel_connector *intel_connector_alloc(void)
6235{
6236 struct intel_connector *connector;
6237
6238 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6239 if (!connector)
6240 return NULL;
6241
6242 if (intel_connector_init(connector) < 0) {
6243 kfree(connector);
6244 return NULL;
6245 }
6246
6247 return connector;
6248}
6249
James Ausmus091a4f92017-10-13 11:01:44 -07006250/*
6251 * Free the bits allocated by intel_connector_alloc.
6252 * This should only be used after intel_connector_alloc has returned
6253 * successfully, and before drm_connector_init returns successfully.
6254 * Otherwise the destroy callbacks for the connector and the state should
6255 * take care of proper cleanup/free
6256 */
6257void intel_connector_free(struct intel_connector *connector)
6258{
6259 kfree(to_intel_digital_connector_state(connector->base.state));
6260 kfree(connector);
6261}
6262
Daniel Vetterf0947c32012-07-02 13:10:34 +02006263/* Simple connector->get_hw_state implementation for encoders that support only
6264 * one connector and no cloning and hence the encoder state determines the state
6265 * of the connector. */
6266bool intel_connector_get_hw_state(struct intel_connector *connector)
6267{
Daniel Vetter24929352012-07-02 20:28:59 +02006268 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006269 struct intel_encoder *encoder = connector->encoder;
6270
6271 return encoder->get_hw_state(encoder, &pipe);
6272}
6273
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006274static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006275{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006276 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6277 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006278
6279 return 0;
6280}
6281
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006282static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006283 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006284{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006285 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006286 struct drm_atomic_state *state = pipe_config->base.state;
6287 struct intel_crtc *other_crtc;
6288 struct intel_crtc_state *other_crtc_state;
6289
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006290 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6291 pipe_name(pipe), pipe_config->fdi_lanes);
6292 if (pipe_config->fdi_lanes > 4) {
6293 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6294 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006295 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006296 }
6297
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006298 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006299 if (pipe_config->fdi_lanes > 2) {
6300 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6301 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006302 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006303 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006304 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006305 }
6306 }
6307
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00006308 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006309 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006310
6311 /* Ivybridge 3 pipe is really complicated */
6312 switch (pipe) {
6313 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006314 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006315 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006316 if (pipe_config->fdi_lanes <= 2)
6317 return 0;
6318
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006319 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006320 other_crtc_state =
6321 intel_atomic_get_crtc_state(state, other_crtc);
6322 if (IS_ERR(other_crtc_state))
6323 return PTR_ERR(other_crtc_state);
6324
6325 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006326 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6327 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006328 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006329 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006330 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006331 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006332 if (pipe_config->fdi_lanes > 2) {
6333 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6334 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006335 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006336 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006337
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006338 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006339 other_crtc_state =
6340 intel_atomic_get_crtc_state(state, other_crtc);
6341 if (IS_ERR(other_crtc_state))
6342 return PTR_ERR(other_crtc_state);
6343
6344 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006345 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006346 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006347 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006348 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006349 default:
6350 BUG();
6351 }
6352}
6353
Daniel Vettere29c22c2013-02-21 00:00:16 +01006354#define RETRY 1
6355static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006356 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006357{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006358 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006359 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006360 int lane, link_bw, fdi_dotclock, ret;
6361 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006362
Daniel Vettere29c22c2013-02-21 00:00:16 +01006363retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006364 /* FDI is a binary signal running at ~2.7GHz, encoding
6365 * each output octet as 10 bits. The actual frequency
6366 * is stored as a divider into a 100MHz clock, and the
6367 * mode pixel clock is stored in units of 1KHz.
6368 * Hence the bw of each lane in terms of the mode signal
6369 * is:
6370 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006371 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006372
Damien Lespiau241bfc32013-09-25 16:45:37 +01006373 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006374
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006375 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006376 pipe_config->pipe_bpp);
6377
6378 pipe_config->fdi_lanes = lane;
6379
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006380 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006381 link_bw, &pipe_config->fdi_m_n, false);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006382
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006383 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006384 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006385 pipe_config->pipe_bpp -= 2*3;
6386 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6387 pipe_config->pipe_bpp);
6388 needs_recompute = true;
6389 pipe_config->bw_constrained = true;
6390
6391 goto retry;
6392 }
6393
6394 if (needs_recompute)
6395 return RETRY;
6396
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006397 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006398}
6399
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006400bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006401{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006402 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6403 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6404
6405 /* IPS only exists on ULT machines and is tied to pipe A. */
6406 if (!hsw_crtc_supports_ips(crtc))
Ville Syrjälä6e644622017-08-17 17:55:09 +03006407 return false;
6408
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006409 if (!i915_modparams.enable_ips)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006410 return false;
6411
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006412 if (crtc_state->pipe_bpp > 24)
6413 return false;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006414
6415 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006416 * We compare against max which means we must take
6417 * the increased cdclk requirement into account when
6418 * calculating the new cdclk.
6419 *
6420 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006421 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006422 if (IS_BROADWELL(dev_priv) &&
6423 crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
6424 return false;
6425
6426 return true;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006427}
6428
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006429static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006430{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006431 struct drm_i915_private *dev_priv =
6432 to_i915(crtc_state->base.crtc->dev);
6433 struct intel_atomic_state *intel_state =
6434 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006435
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006436 if (!hsw_crtc_state_ips_capable(crtc_state))
6437 return false;
6438
6439 if (crtc_state->ips_force_disable)
6440 return false;
6441
Maarten Lankhorstadbe5c52017-11-22 19:39:06 +01006442 /* IPS should be fine as long as at least one plane is enabled. */
6443 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006444 return false;
6445
6446 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
6447 if (IS_BROADWELL(dev_priv) &&
6448 crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
6449 return false;
6450
6451 return true;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006452}
6453
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006454static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6455{
6456 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6457
6458 /* GDG double wide on either pipe, otherwise pipe A only */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00006459 return INTEL_GEN(dev_priv) < 4 &&
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006460 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6461}
6462
Ville Syrjäläceb99322017-01-20 20:22:05 +02006463static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6464{
6465 uint32_t pixel_rate;
6466
6467 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6468
6469 /*
6470 * We only use IF-ID interlacing. If we ever use
6471 * PF-ID we'll need to adjust the pixel_rate here.
6472 */
6473
6474 if (pipe_config->pch_pfit.enabled) {
6475 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6476 uint32_t pfit_size = pipe_config->pch_pfit.size;
6477
6478 pipe_w = pipe_config->pipe_src_w;
6479 pipe_h = pipe_config->pipe_src_h;
6480
6481 pfit_w = (pfit_size >> 16) & 0xFFFF;
6482 pfit_h = pfit_size & 0xFFFF;
6483 if (pipe_w < pfit_w)
6484 pipe_w = pfit_w;
6485 if (pipe_h < pfit_h)
6486 pipe_h = pfit_h;
6487
6488 if (WARN_ON(!pfit_w || !pfit_h))
6489 return pixel_rate;
6490
6491 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6492 pfit_w * pfit_h);
6493 }
6494
6495 return pixel_rate;
6496}
6497
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006498static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
6499{
6500 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
6501
6502 if (HAS_GMCH_DISPLAY(dev_priv))
6503 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6504 crtc_state->pixel_rate =
6505 crtc_state->base.adjusted_mode.crtc_clock;
6506 else
6507 crtc_state->pixel_rate =
6508 ilk_pipe_pixel_rate(crtc_state);
6509}
6510
Daniel Vettera43f6e02013-06-07 23:10:32 +02006511static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006512 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006513{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006514 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006515 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006516 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006517 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006518
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006519 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006520 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006521
6522 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006523 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006524 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006525 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006526 if (intel_crtc_supports_double_wide(crtc) &&
6527 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006528 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006529 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006530 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006531 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006532
Ville Syrjäläf3261152016-05-24 21:34:18 +03006533 if (adjusted_mode->crtc_clock > clock_limit) {
6534 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6535 adjusted_mode->crtc_clock, clock_limit,
6536 yesno(pipe_config->double_wide));
6537 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006538 }
Chris Wilson89749352010-09-12 18:25:19 +01006539
Shashank Sharma25edf912017-07-21 20:55:07 +05306540 if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
6541 /*
6542 * There is only one pipe CSC unit per pipe, and we need that
6543 * for output conversion from RGB->YCBCR. So if CTM is already
6544 * applied we can't support YCBCR420 output.
6545 */
6546 DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
6547 return -EINVAL;
6548 }
6549
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006550 /*
6551 * Pipe horizontal size must be even in:
6552 * - DVO ganged mode
6553 * - LVDS dual channel mode
6554 * - Double wide pipe
6555 */
Ville Syrjälä0574bd82017-11-23 21:04:48 +02006556 if (pipe_config->pipe_src_w & 1) {
6557 if (pipe_config->double_wide) {
6558 DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
6559 return -EINVAL;
6560 }
6561
6562 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6563 intel_is_dual_link_lvds(dev)) {
6564 DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
6565 return -EINVAL;
6566 }
6567 }
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006568
Damien Lespiau8693a822013-05-03 18:48:11 +01006569 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6570 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006571 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006572 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006573 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006574 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006575
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006576 intel_crtc_compute_pixel_rate(pipe_config);
6577
Daniel Vetter877d48d2013-04-19 11:24:43 +02006578 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006579 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006580
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006581 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006582}
6583
Zhenyu Wang2c072452009-06-05 15:38:42 +08006584static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006585intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006586{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006587 while (*num > DATA_LINK_M_N_MASK ||
6588 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006589 *num >>= 1;
6590 *den >>= 1;
6591 }
6592}
6593
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006594static void compute_m_n(unsigned int m, unsigned int n,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006595 uint32_t *ret_m, uint32_t *ret_n,
6596 bool reduce_m_n)
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006597{
Jani Nikula9a86cda2017-03-27 14:33:25 +03006598 /*
6599 * Reduce M/N as much as possible without loss in precision. Several DP
6600 * dongles in particular seem to be fussy about too large *link* M/N
6601 * values. The passed in values are more likely to have the least
6602 * significant bits zero than M after rounding below, so do this first.
6603 */
Jani Nikulab31e85e2017-05-18 14:10:25 +03006604 if (reduce_m_n) {
6605 while ((m & 1) == 0 && (n & 1) == 0) {
6606 m >>= 1;
6607 n >>= 1;
6608 }
Jani Nikula9a86cda2017-03-27 14:33:25 +03006609 }
6610
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006611 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6612 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6613 intel_reduce_m_n_ratio(ret_m, ret_n);
6614}
6615
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006616void
6617intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6618 int pixel_clock, int link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006619 struct intel_link_m_n *m_n,
6620 bool reduce_m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006621{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006622 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006623
6624 compute_m_n(bits_per_pixel * pixel_clock,
6625 link_clock * nlanes * 8,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006626 &m_n->gmch_m, &m_n->gmch_n,
6627 reduce_m_n);
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006628
6629 compute_m_n(pixel_clock, link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006630 &m_n->link_m, &m_n->link_n,
6631 reduce_m_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006632}
6633
Chris Wilsona7615032011-01-12 17:04:08 +00006634static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6635{
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00006636 if (i915_modparams.panel_use_ssc >= 0)
6637 return i915_modparams.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006638 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006639 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006640}
6641
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006642static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006643{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006644 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006645}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006646
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006647static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6648{
6649 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006650}
6651
Daniel Vetterf47709a2013-03-28 10:42:02 +01006652static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006653 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006654 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08006655{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006656 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006657 u32 fp, fp2 = 0;
6658
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006659 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006660 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006661 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006662 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006663 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006664 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006665 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006666 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006667 }
6668
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006669 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006670
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006671 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006672 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006673 crtc_state->dpll_hw_state.fp1 = fp2;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006674 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006675 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006676 }
6677}
6678
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006679static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6680 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006681{
6682 u32 reg_val;
6683
6684 /*
6685 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6686 * and set it to a reasonable value instead.
6687 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006688 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006689 reg_val &= 0xffffff00;
6690 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006691 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006692
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006693 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Imre Deaked585702017-05-10 12:21:47 +03006694 reg_val &= 0x00ffffff;
6695 reg_val |= 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006696 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006697
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006698 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006699 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006700 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006701
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006702 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006703 reg_val &= 0x00ffffff;
6704 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006705 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006706}
6707
Daniel Vetterb5518422013-05-03 11:49:48 +02006708static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6709 struct intel_link_m_n *m_n)
6710{
6711 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006712 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006713 int pipe = crtc->pipe;
6714
Daniel Vettere3b95f12013-05-03 11:49:49 +02006715 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6716 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6717 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6718 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006719}
6720
6721static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006722 struct intel_link_m_n *m_n,
6723 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006724{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006725 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006726 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006727 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006728
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006729 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02006730 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6731 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6732 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6733 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006734 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6735 * for gen < 8) and if DRRS is supported (to make sure the
6736 * registers are not unnecessarily accessed).
6737 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006738 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6739 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006740 I915_WRITE(PIPE_DATA_M2(transcoder),
6741 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6742 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6743 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6744 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6745 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006746 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006747 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6748 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6749 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6750 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006751 }
6752}
6753
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306754void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006755{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306756 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6757
6758 if (m_n == M1_N1) {
6759 dp_m_n = &crtc->config->dp_m_n;
6760 dp_m2_n2 = &crtc->config->dp_m2_n2;
6761 } else if (m_n == M2_N2) {
6762
6763 /*
6764 * M2_N2 registers are not supported. Hence m2_n2 divider value
6765 * needs to be programmed into M1_N1.
6766 */
6767 dp_m_n = &crtc->config->dp_m2_n2;
6768 } else {
6769 DRM_ERROR("Unsupported divider value\n");
6770 return;
6771 }
6772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006773 if (crtc->config->has_pch_encoder)
6774 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006775 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306776 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006777}
6778
Daniel Vetter251ac862015-06-18 10:30:24 +02006779static void vlv_compute_dpll(struct intel_crtc *crtc,
6780 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006781{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006782 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006783 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006784 if (crtc->pipe != PIPE_A)
6785 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006786
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006787 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006788 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006789 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6790 DPLL_EXT_BUFFER_ENABLE_VLV;
6791
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006792 pipe_config->dpll_hw_state.dpll_md =
6793 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6794}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006795
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006796static void chv_compute_dpll(struct intel_crtc *crtc,
6797 struct intel_crtc_state *pipe_config)
6798{
6799 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006800 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006801 if (crtc->pipe != PIPE_A)
6802 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6803
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006804 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006805 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006806 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6807
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006808 pipe_config->dpll_hw_state.dpll_md =
6809 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006810}
6811
Ville Syrjäläd288f652014-10-28 13:20:22 +02006812static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006813 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006814{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006815 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006816 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006817 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006818 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006819 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006820 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006821
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006822 /* Enable Refclk */
6823 I915_WRITE(DPLL(pipe),
6824 pipe_config->dpll_hw_state.dpll &
6825 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6826
6827 /* No need to actually set up the DPLL with DSI */
6828 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6829 return;
6830
Ville Syrjäläa5805162015-05-26 20:42:30 +03006831 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01006832
Ville Syrjäläd288f652014-10-28 13:20:22 +02006833 bestn = pipe_config->dpll.n;
6834 bestm1 = pipe_config->dpll.m1;
6835 bestm2 = pipe_config->dpll.m2;
6836 bestp1 = pipe_config->dpll.p1;
6837 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006838
Jesse Barnes89b667f2013-04-18 14:51:36 -07006839 /* See eDP HDMI DPIO driver vbios notes doc */
6840
6841 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006842 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006843 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006844
6845 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006846 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006847
6848 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006849 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006850 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006851 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006852
6853 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006854 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006855
6856 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006857 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6858 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6859 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006860 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006861
6862 /*
6863 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6864 * but we don't support that).
6865 * Note: don't use the DAC post divider as it seems unstable.
6866 */
6867 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006868 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006869
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006870 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006871 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006872
Jesse Barnes89b667f2013-04-18 14:51:36 -07006873 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006874 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006875 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6876 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006877 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006878 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006879 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006880 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006881 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006882
Ville Syrjälä37a56502016-06-22 21:57:04 +03006883 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006884 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006885 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006886 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006887 0x0df40000);
6888 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006889 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006890 0x0df70000);
6891 } else { /* HDMI or VGA */
6892 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006893 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006894 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006895 0x0df70000);
6896 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006897 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006898 0x0df40000);
6899 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006900
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006901 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006902 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03006903 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006904 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006905 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006906
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006907 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006908 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006909}
6910
Ville Syrjäläd288f652014-10-28 13:20:22 +02006911static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006912 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006913{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006914 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006915 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006916 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006917 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306918 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006919 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306920 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306921 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006922
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006923 /* Enable Refclk and SSC */
6924 I915_WRITE(DPLL(pipe),
6925 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6926
6927 /* No need to actually set up the DPLL with DSI */
6928 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6929 return;
6930
Ville Syrjäläd288f652014-10-28 13:20:22 +02006931 bestn = pipe_config->dpll.n;
6932 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6933 bestm1 = pipe_config->dpll.m1;
6934 bestm2 = pipe_config->dpll.m2 >> 22;
6935 bestp1 = pipe_config->dpll.p1;
6936 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306937 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306938 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306939 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006940
Ville Syrjäläa5805162015-05-26 20:42:30 +03006941 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006942
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006943 /* p1 and p2 divider */
6944 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6945 5 << DPIO_CHV_S1_DIV_SHIFT |
6946 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6947 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6948 1 << DPIO_CHV_K_DIV_SHIFT);
6949
6950 /* Feedback post-divider - m2 */
6951 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6952
6953 /* Feedback refclk divider - n and m1 */
6954 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6955 DPIO_CHV_M1_DIV_BY_2 |
6956 1 << DPIO_CHV_N_DIV_SHIFT);
6957
6958 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03006959 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006960
6961 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306962 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6963 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6964 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6965 if (bestm2_frac)
6966 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6967 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006968
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306969 /* Program digital lock detect threshold */
6970 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6971 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6972 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6973 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6974 if (!bestm2_frac)
6975 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6976 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6977
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006978 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306979 if (vco == 5400000) {
6980 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6981 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6982 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6983 tribuf_calcntr = 0x9;
6984 } else if (vco <= 6200000) {
6985 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6986 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6987 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6988 tribuf_calcntr = 0x9;
6989 } else if (vco <= 6480000) {
6990 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6991 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6992 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6993 tribuf_calcntr = 0x8;
6994 } else {
6995 /* Not supported. Apply the same limits as in the max case */
6996 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6997 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6998 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6999 tribuf_calcntr = 0;
7000 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007001 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7002
Ville Syrjälä968040b2015-03-11 22:52:08 +02007003 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307004 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7005 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7006 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7007
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007008 /* AFC Recal */
7009 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7010 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7011 DPIO_AFC_RECAL);
7012
Ville Syrjäläa5805162015-05-26 20:42:30 +03007013 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007014}
7015
Ville Syrjäläd288f652014-10-28 13:20:22 +02007016/**
7017 * vlv_force_pll_on - forcibly enable just the PLL
7018 * @dev_priv: i915 private structure
7019 * @pipe: pipe PLL to enable
7020 * @dpll: PLL configuration
7021 *
7022 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7023 * in cases where we need the PLL enabled even when @pipe is not going to
7024 * be enabled.
7025 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007026int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007027 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007028{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007029 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007030 struct intel_crtc_state *pipe_config;
7031
7032 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7033 if (!pipe_config)
7034 return -ENOMEM;
7035
7036 pipe_config->base.crtc = &crtc->base;
7037 pipe_config->pixel_multiplier = 1;
7038 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007039
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007040 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007041 chv_compute_dpll(crtc, pipe_config);
7042 chv_prepare_pll(crtc, pipe_config);
7043 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007044 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007045 vlv_compute_dpll(crtc, pipe_config);
7046 vlv_prepare_pll(crtc, pipe_config);
7047 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007048 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007049
7050 kfree(pipe_config);
7051
7052 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007053}
7054
7055/**
7056 * vlv_force_pll_off - forcibly disable just the PLL
7057 * @dev_priv: i915 private structure
7058 * @pipe: pipe PLL to disable
7059 *
7060 * Disable the PLL for @pipe. To be used in cases where we need
7061 * the PLL enabled even when @pipe is not going to be enabled.
7062 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007063void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007064{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007065 if (IS_CHERRYVIEW(dev_priv))
7066 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007067 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007068 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007069}
7070
Daniel Vetter251ac862015-06-18 10:30:24 +02007071static void i9xx_compute_dpll(struct intel_crtc *crtc,
7072 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007073 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007074{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007076 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007077 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007078
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007079 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307080
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007081 dpll = DPLL_VGA_MODE_DIS;
7082
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007083 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007084 dpll |= DPLLB_MODE_LVDS;
7085 else
7086 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007087
Jani Nikula73f67aa2016-12-07 22:48:09 +02007088 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
7089 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007090 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007091 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007092 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007093
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007094 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7095 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007096 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007097
Ville Syrjälä37a56502016-06-22 21:57:04 +03007098 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007099 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007100
7101 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007102 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007103 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7104 else {
7105 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007106 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007107 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7108 }
7109 switch (clock->p2) {
7110 case 5:
7111 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7112 break;
7113 case 7:
7114 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7115 break;
7116 case 10:
7117 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7118 break;
7119 case 14:
7120 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7121 break;
7122 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007123 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007124 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7125
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007126 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007127 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007128 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007129 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007130 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7131 else
7132 dpll |= PLL_REF_INPUT_DREFCLK;
7133
7134 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007135 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007136
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007137 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007138 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007139 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007140 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007141 }
7142}
7143
Daniel Vetter251ac862015-06-18 10:30:24 +02007144static void i8xx_compute_dpll(struct intel_crtc *crtc,
7145 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007146 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007147{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007148 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007149 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007150 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007151 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007152
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007153 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307154
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007155 dpll = DPLL_VGA_MODE_DIS;
7156
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007157 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007158 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7159 } else {
7160 if (clock->p1 == 2)
7161 dpll |= PLL_P1_DIVIDE_BY_TWO;
7162 else
7163 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7164 if (clock->p2 == 4)
7165 dpll |= PLL_P2_DIVIDE_BY_4;
7166 }
7167
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007168 if (!IS_I830(dev_priv) &&
7169 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007170 dpll |= DPLL_DVO_2X_MODE;
7171
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007172 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007173 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007174 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7175 else
7176 dpll |= PLL_REF_INPUT_DREFCLK;
7177
7178 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007179 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007180}
7181
Daniel Vetter8a654f32013-06-01 17:16:22 +02007182static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007183{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007184 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007185 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007186 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007187 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007188 uint32_t crtc_vtotal, crtc_vblank_end;
7189 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007190
7191 /* We need to be careful not to changed the adjusted mode, for otherwise
7192 * the hw state checker will get angry at the mismatch. */
7193 crtc_vtotal = adjusted_mode->crtc_vtotal;
7194 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007195
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007196 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007197 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007198 crtc_vtotal -= 1;
7199 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007200
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007201 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007202 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7203 else
7204 vsyncshift = adjusted_mode->crtc_hsync_start -
7205 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007206 if (vsyncshift < 0)
7207 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007208 }
7209
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007210 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007211 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007212
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007213 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007214 (adjusted_mode->crtc_hdisplay - 1) |
7215 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007216 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007217 (adjusted_mode->crtc_hblank_start - 1) |
7218 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007219 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007220 (adjusted_mode->crtc_hsync_start - 1) |
7221 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7222
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007223 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007224 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007225 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007226 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007227 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007228 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007229 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007230 (adjusted_mode->crtc_vsync_start - 1) |
7231 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7232
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007233 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7234 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7235 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7236 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01007237 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007238 (pipe == PIPE_B || pipe == PIPE_C))
7239 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7240
Jani Nikulabc58be62016-03-18 17:05:39 +02007241}
7242
7243static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7244{
7245 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007246 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007247 enum pipe pipe = intel_crtc->pipe;
7248
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007249 /* pipesrc controls the size that is scaled from, which should
7250 * always be the user's requested size.
7251 */
7252 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007253 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7254 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007255}
7256
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007257static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007258 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007259{
7260 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007261 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007262 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7263 uint32_t tmp;
7264
7265 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007266 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7267 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007268 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007269 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7270 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007271 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007272 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7273 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007274
7275 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007276 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7277 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007278 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007279 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7280 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007281 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007282 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7283 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007284
7285 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007286 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7287 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7288 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007289 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007290}
7291
7292static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7293 struct intel_crtc_state *pipe_config)
7294{
7295 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007296 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007297 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007298
7299 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007300 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7301 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7302
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007303 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7304 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007305}
7306
Daniel Vetterf6a83282014-02-11 15:28:57 -08007307void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007308 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007309{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007310 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7311 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7312 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7313 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007314
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007315 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7316 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7317 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7318 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007319
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007320 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007321 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007322
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007323 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007324
7325 mode->hsync = drm_mode_hsync(mode);
7326 mode->vrefresh = drm_mode_vrefresh(mode);
7327 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007328}
7329
Daniel Vetter84b046f2013-02-19 18:48:54 +01007330static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7331{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007332 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007333 uint32_t pipeconf;
7334
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007335 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007336
Ville Syrjäläe56134b2017-06-01 17:36:19 +03007337 /* we keep both pipes enabled on 830 */
7338 if (IS_I830(dev_priv))
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007339 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007340
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007341 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007342 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007343
Daniel Vetterff9ce462013-04-24 14:57:17 +02007344 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007345 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7346 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007347 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007348 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007349 pipeconf |= PIPECONF_DITHER_EN |
7350 PIPECONF_DITHER_TYPE_SP;
7351
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007352 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007353 case 18:
7354 pipeconf |= PIPECONF_6BPC;
7355 break;
7356 case 24:
7357 pipeconf |= PIPECONF_8BPC;
7358 break;
7359 case 30:
7360 pipeconf |= PIPECONF_10BPC;
7361 break;
7362 default:
7363 /* Case prevented by intel_choose_pipe_bpp_dither. */
7364 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007365 }
7366 }
7367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007368 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007369 if (INTEL_GEN(dev_priv) < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007370 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007371 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7372 else
7373 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7374 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007375 pipeconf |= PIPECONF_PROGRESSIVE;
7376
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007377 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007378 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007379 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007380
Daniel Vetter84b046f2013-02-19 18:48:54 +01007381 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7382 POSTING_READ(PIPECONF(intel_crtc->pipe));
7383}
7384
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007385static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7386 struct intel_crtc_state *crtc_state)
7387{
7388 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007389 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007390 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007391 int refclk = 48000;
7392
7393 memset(&crtc_state->dpll_hw_state, 0,
7394 sizeof(crtc_state->dpll_hw_state));
7395
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007396 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007397 if (intel_panel_use_ssc(dev_priv)) {
7398 refclk = dev_priv->vbt.lvds_ssc_freq;
7399 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7400 }
7401
7402 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007403 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007404 limit = &intel_limits_i8xx_dvo;
7405 } else {
7406 limit = &intel_limits_i8xx_dac;
7407 }
7408
7409 if (!crtc_state->clock_set &&
7410 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7411 refclk, NULL, &crtc_state->dpll)) {
7412 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7413 return -EINVAL;
7414 }
7415
7416 i8xx_compute_dpll(crtc, crtc_state, NULL);
7417
7418 return 0;
7419}
7420
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007421static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7422 struct intel_crtc_state *crtc_state)
7423{
7424 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007425 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007426 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007427 int refclk = 96000;
7428
7429 memset(&crtc_state->dpll_hw_state, 0,
7430 sizeof(crtc_state->dpll_hw_state));
7431
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007432 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007433 if (intel_panel_use_ssc(dev_priv)) {
7434 refclk = dev_priv->vbt.lvds_ssc_freq;
7435 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7436 }
7437
7438 if (intel_is_dual_link_lvds(dev))
7439 limit = &intel_limits_g4x_dual_channel_lvds;
7440 else
7441 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007442 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7443 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007444 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007445 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007446 limit = &intel_limits_g4x_sdvo;
7447 } else {
7448 /* The option is for other outputs */
7449 limit = &intel_limits_i9xx_sdvo;
7450 }
7451
7452 if (!crtc_state->clock_set &&
7453 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7454 refclk, NULL, &crtc_state->dpll)) {
7455 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7456 return -EINVAL;
7457 }
7458
7459 i9xx_compute_dpll(crtc, crtc_state, NULL);
7460
7461 return 0;
7462}
7463
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007464static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7465 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007466{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007467 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007468 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007469 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007470 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007471
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007472 memset(&crtc_state->dpll_hw_state, 0,
7473 sizeof(crtc_state->dpll_hw_state));
7474
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007475 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007476 if (intel_panel_use_ssc(dev_priv)) {
7477 refclk = dev_priv->vbt.lvds_ssc_freq;
7478 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7479 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007480
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007481 limit = &intel_limits_pineview_lvds;
7482 } else {
7483 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007484 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007485
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007486 if (!crtc_state->clock_set &&
7487 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7488 refclk, NULL, &crtc_state->dpll)) {
7489 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7490 return -EINVAL;
7491 }
7492
7493 i9xx_compute_dpll(crtc, crtc_state, NULL);
7494
7495 return 0;
7496}
7497
7498static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7499 struct intel_crtc_state *crtc_state)
7500{
7501 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007502 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007503 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007504 int refclk = 96000;
7505
7506 memset(&crtc_state->dpll_hw_state, 0,
7507 sizeof(crtc_state->dpll_hw_state));
7508
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007509 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007510 if (intel_panel_use_ssc(dev_priv)) {
7511 refclk = dev_priv->vbt.lvds_ssc_freq;
7512 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007513 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007514
7515 limit = &intel_limits_i9xx_lvds;
7516 } else {
7517 limit = &intel_limits_i9xx_sdvo;
7518 }
7519
7520 if (!crtc_state->clock_set &&
7521 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7522 refclk, NULL, &crtc_state->dpll)) {
7523 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7524 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007525 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007526
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007527 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007528
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007529 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007530}
7531
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007532static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7533 struct intel_crtc_state *crtc_state)
7534{
7535 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007536 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007537
7538 memset(&crtc_state->dpll_hw_state, 0,
7539 sizeof(crtc_state->dpll_hw_state));
7540
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007541 if (!crtc_state->clock_set &&
7542 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7543 refclk, NULL, &crtc_state->dpll)) {
7544 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7545 return -EINVAL;
7546 }
7547
7548 chv_compute_dpll(crtc, crtc_state);
7549
7550 return 0;
7551}
7552
7553static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7554 struct intel_crtc_state *crtc_state)
7555{
7556 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007557 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007558
7559 memset(&crtc_state->dpll_hw_state, 0,
7560 sizeof(crtc_state->dpll_hw_state));
7561
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007562 if (!crtc_state->clock_set &&
7563 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7564 refclk, NULL, &crtc_state->dpll)) {
7565 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7566 return -EINVAL;
7567 }
7568
7569 vlv_compute_dpll(crtc, crtc_state);
7570
7571 return 0;
7572}
7573
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007574static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007575 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007576{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007577 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007578 uint32_t tmp;
7579
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007580 if (INTEL_GEN(dev_priv) <= 3 &&
7581 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007582 return;
7583
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007584 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007585 if (!(tmp & PFIT_ENABLE))
7586 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007587
Daniel Vetter06922822013-07-11 13:35:40 +02007588 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007589 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007590 if (crtc->pipe != PIPE_B)
7591 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007592 } else {
7593 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7594 return;
7595 }
7596
Daniel Vetter06922822013-07-11 13:35:40 +02007597 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007598 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007599}
7600
Jesse Barnesacbec812013-09-20 11:29:32 -07007601static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007602 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007603{
7604 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007605 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07007606 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007607 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07007608 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007609 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007610
Ville Syrjäläb5219732016-03-15 16:40:01 +02007611 /* In case of DSI, DPLL will not be used */
7612 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05307613 return;
7614
Ville Syrjäläa5805162015-05-26 20:42:30 +03007615 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007616 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007617 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007618
7619 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7620 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7621 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7622 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7623 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7624
Imre Deakdccbea32015-06-22 23:35:51 +03007625 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007626}
7627
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007628static void
7629i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7630 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007631{
7632 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007633 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007634 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
7635 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
7636 enum pipe pipe = crtc->pipe;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007637 u32 val, base, offset;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007638 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007639 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007640 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007641 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007642
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02007643 if (!plane->get_hw_state(plane))
Damien Lespiau42a7b082015-02-05 19:35:13 +00007644 return;
7645
Damien Lespiaud9806c92015-01-21 14:07:19 +00007646 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007647 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007648 DRM_DEBUG_KMS("failed to alloc fb\n");
7649 return;
7650 }
7651
Damien Lespiau1b842c82015-01-21 13:50:54 +00007652 fb = &intel_fb->base;
7653
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02007654 fb->dev = dev;
7655
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02007656 val = I915_READ(DSPCNTR(i9xx_plane));
7657
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007658 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00007659 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007660 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02007661 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00007662 }
7663 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007664
7665 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007666 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02007667 fb->format = drm_format_info(fourcc);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007668
Ville Syrjälä81894b22017-11-17 21:19:13 +02007669 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7670 offset = I915_READ(DSPOFFSET(i9xx_plane));
7671 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
7672 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007673 if (plane_config->tiling)
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007674 offset = I915_READ(DSPTILEOFF(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007675 else
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007676 offset = I915_READ(DSPLINOFF(i9xx_plane));
7677 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007678 } else {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007679 base = I915_READ(DSPADDR(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007680 }
7681 plane_config->base = base;
7682
7683 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007684 fb->width = ((val >> 16) & 0xfff) + 1;
7685 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007686
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007687 val = I915_READ(DSPSTRIDE(i9xx_plane));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007688 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007689
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02007690 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007691
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007692 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007693
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007694 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7695 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02007696 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00007697 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007698
Damien Lespiau2d140302015-02-05 17:22:18 +00007699 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007700}
7701
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007702static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007703 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007704{
7705 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007706 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007707 int pipe = pipe_config->cpu_transcoder;
7708 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007709 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007710 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007711 int refclk = 100000;
7712
Ville Syrjäläb5219732016-03-15 16:40:01 +02007713 /* In case of DSI, DPLL will not be used */
7714 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7715 return;
7716
Ville Syrjäläa5805162015-05-26 20:42:30 +03007717 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007718 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7719 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7720 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7721 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007722 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007723 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007724
7725 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007726 clock.m2 = (pll_dw0 & 0xff) << 22;
7727 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7728 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007729 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7730 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7731 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7732
Imre Deakdccbea32015-06-22 23:35:51 +03007733 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007734}
7735
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007736static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007737 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007738{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007739 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02007740 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007741 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02007742 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007743
Imre Deak17290502016-02-12 18:55:11 +02007744 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7745 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02007746 return false;
7747
Daniel Vettere143a212013-07-04 12:01:15 +02007748 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02007749 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02007750
Imre Deak17290502016-02-12 18:55:11 +02007751 ret = false;
7752
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007753 tmp = I915_READ(PIPECONF(crtc->pipe));
7754 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02007755 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007756
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007757 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7758 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007759 switch (tmp & PIPECONF_BPC_MASK) {
7760 case PIPECONF_6BPC:
7761 pipe_config->pipe_bpp = 18;
7762 break;
7763 case PIPECONF_8BPC:
7764 pipe_config->pipe_bpp = 24;
7765 break;
7766 case PIPECONF_10BPC:
7767 pipe_config->pipe_bpp = 30;
7768 break;
7769 default:
7770 break;
7771 }
7772 }
7773
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007774 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007775 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007776 pipe_config->limited_color_range = true;
7777
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007778 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03007779 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7780
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007781 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02007782 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007783
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007784 i9xx_get_pfit_config(crtc, pipe_config);
7785
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007786 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02007787 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007788 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02007789 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7790 else
7791 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02007792 pipe_config->pixel_multiplier =
7793 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7794 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007795 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007796 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02007797 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02007798 tmp = I915_READ(DPLL(crtc->pipe));
7799 pipe_config->pixel_multiplier =
7800 ((tmp & SDVO_MULTIPLIER_MASK)
7801 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7802 } else {
7803 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7804 * port and will be fixed up in the encoder->get_config
7805 * function. */
7806 pipe_config->pixel_multiplier = 1;
7807 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007808 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007809 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007810 /*
7811 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7812 * on 830. Filter it out here so that we don't
7813 * report errors due to that.
7814 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007815 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007816 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7817
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007818 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7819 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007820 } else {
7821 /* Mask out read-only status bits. */
7822 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7823 DPLL_PORTC_READY_MASK |
7824 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007825 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007826
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007827 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007828 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007829 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07007830 vlv_crtc_clock_get(crtc, pipe_config);
7831 else
7832 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007833
Ville Syrjälä0f646142015-08-26 19:39:18 +03007834 /*
7835 * Normally the dotclock is filled in by the encoder .get_config()
7836 * but in case the pipe is enabled w/o any ports we need a sane
7837 * default.
7838 */
7839 pipe_config->base.adjusted_mode.crtc_clock =
7840 pipe_config->port_clock / pipe_config->pixel_multiplier;
7841
Imre Deak17290502016-02-12 18:55:11 +02007842 ret = true;
7843
7844out:
7845 intel_display_power_put(dev_priv, power_domain);
7846
7847 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007848}
7849
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007850static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007851{
Jesse Barnes13d83a62011-08-03 12:59:20 -07007852 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04007853 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007854 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007855 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007856 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007857 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007858 bool has_ck505 = false;
7859 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04007860 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007861
7862 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007863 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007864 switch (encoder->type) {
7865 case INTEL_OUTPUT_LVDS:
7866 has_panel = true;
7867 has_lvds = true;
7868 break;
7869 case INTEL_OUTPUT_EDP:
7870 has_panel = true;
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02007871 if (encoder->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007872 has_cpu_edp = true;
7873 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007874 default:
7875 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007876 }
7877 }
7878
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007879 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007880 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007881 can_ssc = has_ck505;
7882 } else {
7883 has_ck505 = false;
7884 can_ssc = true;
7885 }
7886
Lyude1c1a24d2016-06-14 11:04:09 -04007887 /* Check if any DPLLs are using the SSC source */
7888 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7889 u32 temp = I915_READ(PCH_DPLL(i));
7890
7891 if (!(temp & DPLL_VCO_ENABLE))
7892 continue;
7893
7894 if ((temp & PLL_REF_INPUT_MASK) ==
7895 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7896 using_ssc_source = true;
7897 break;
7898 }
7899 }
7900
7901 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7902 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007903
7904 /* Ironlake: try to setup display ref clock before DPLL
7905 * enabling. This is only under driver's control after
7906 * PCH B stepping, previous chipset stepping should be
7907 * ignoring this setting.
7908 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007909 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007910
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007911 /* As we must carefully and slowly disable/enable each source in turn,
7912 * compute the final state we want first and check if we need to
7913 * make any changes at all.
7914 */
7915 final = val;
7916 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007917 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007918 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007919 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007920 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7921
Daniel Vetter8c07eb62016-06-09 18:39:07 +02007922 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007923 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02007924 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007925
Keith Packard199e5d72011-09-22 12:01:57 -07007926 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007927 final |= DREF_SSC_SOURCE_ENABLE;
7928
7929 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7930 final |= DREF_SSC1_ENABLE;
7931
7932 if (has_cpu_edp) {
7933 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7934 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7935 else
7936 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7937 } else
7938 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04007939 } else if (using_ssc_source) {
7940 final |= DREF_SSC_SOURCE_ENABLE;
7941 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007942 }
7943
7944 if (final == val)
7945 return;
7946
7947 /* Always enable nonspread source */
7948 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7949
7950 if (has_ck505)
7951 val |= DREF_NONSPREAD_CK505_ENABLE;
7952 else
7953 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7954
7955 if (has_panel) {
7956 val &= ~DREF_SSC_SOURCE_MASK;
7957 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007958
Keith Packard199e5d72011-09-22 12:01:57 -07007959 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007960 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007961 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007962 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007963 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007964 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007965
7966 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007967 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007968 POSTING_READ(PCH_DREF_CONTROL);
7969 udelay(200);
7970
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007971 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007972
7973 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007974 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007975 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007976 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007977 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007978 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007979 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007980 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007981 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007982
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007983 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007984 POSTING_READ(PCH_DREF_CONTROL);
7985 udelay(200);
7986 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04007987 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07007988
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007989 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007990
7991 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007992 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007993
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007994 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007995 POSTING_READ(PCH_DREF_CONTROL);
7996 udelay(200);
7997
Lyude1c1a24d2016-06-14 11:04:09 -04007998 if (!using_ssc_source) {
7999 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008000
Lyude1c1a24d2016-06-14 11:04:09 -04008001 /* Turn off the SSC source */
8002 val &= ~DREF_SSC_SOURCE_MASK;
8003 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008004
Lyude1c1a24d2016-06-14 11:04:09 -04008005 /* Turn off SSC1 */
8006 val &= ~DREF_SSC1_ENABLE;
8007
8008 I915_WRITE(PCH_DREF_CONTROL, val);
8009 POSTING_READ(PCH_DREF_CONTROL);
8010 udelay(200);
8011 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008012 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008013
8014 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008015}
8016
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008017static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008018{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008019 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008020
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008021 tmp = I915_READ(SOUTH_CHICKEN2);
8022 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8023 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008024
Imre Deakcf3598c2016-06-28 13:37:31 +03008025 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8026 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008027 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008028
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008029 tmp = I915_READ(SOUTH_CHICKEN2);
8030 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8031 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008032
Imre Deakcf3598c2016-06-28 13:37:31 +03008033 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8034 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008035 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008036}
8037
8038/* WaMPhyProgramming:hsw */
8039static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8040{
8041 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008042
8043 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8044 tmp &= ~(0xFF << 24);
8045 tmp |= (0x12 << 24);
8046 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8047
Paulo Zanonidde86e22012-12-01 12:04:25 -02008048 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8049 tmp |= (1 << 11);
8050 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8051
8052 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8053 tmp |= (1 << 11);
8054 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8055
Paulo Zanonidde86e22012-12-01 12:04:25 -02008056 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8057 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8058 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8059
8060 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8061 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8062 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8063
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008064 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8065 tmp &= ~(7 << 13);
8066 tmp |= (5 << 13);
8067 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008068
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008069 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8070 tmp &= ~(7 << 13);
8071 tmp |= (5 << 13);
8072 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008073
8074 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8075 tmp &= ~0xFF;
8076 tmp |= 0x1C;
8077 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8078
8079 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8080 tmp &= ~0xFF;
8081 tmp |= 0x1C;
8082 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8083
8084 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8085 tmp &= ~(0xFF << 16);
8086 tmp |= (0x1C << 16);
8087 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8088
8089 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8090 tmp &= ~(0xFF << 16);
8091 tmp |= (0x1C << 16);
8092 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8093
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008094 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8095 tmp |= (1 << 27);
8096 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008097
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008098 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8099 tmp |= (1 << 27);
8100 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008101
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008102 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8103 tmp &= ~(0xF << 28);
8104 tmp |= (4 << 28);
8105 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008106
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008107 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8108 tmp &= ~(0xF << 28);
8109 tmp |= (4 << 28);
8110 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008111}
8112
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008113/* Implements 3 different sequences from BSpec chapter "Display iCLK
8114 * Programming" based on the parameters passed:
8115 * - Sequence to enable CLKOUT_DP
8116 * - Sequence to enable CLKOUT_DP without spread
8117 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8118 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008119static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
8120 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008121{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008122 uint32_t reg, tmp;
8123
8124 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8125 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008126 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
8127 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008128 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008129
Ville Syrjäläa5805162015-05-26 20:42:30 +03008130 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008131
8132 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8133 tmp &= ~SBI_SSCCTL_DISABLE;
8134 tmp |= SBI_SSCCTL_PATHALT;
8135 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8136
8137 udelay(24);
8138
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008139 if (with_spread) {
8140 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8141 tmp &= ~SBI_SSCCTL_PATHALT;
8142 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008143
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008144 if (with_fdi) {
8145 lpt_reset_fdi_mphy(dev_priv);
8146 lpt_program_fdi_mphy(dev_priv);
8147 }
8148 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008149
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008150 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008151 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8152 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8153 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008154
Ville Syrjäläa5805162015-05-26 20:42:30 +03008155 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008156}
8157
Paulo Zanoni47701c32013-07-23 11:19:25 -03008158/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008159static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03008160{
Paulo Zanoni47701c32013-07-23 11:19:25 -03008161 uint32_t reg, tmp;
8162
Ville Syrjäläa5805162015-05-26 20:42:30 +03008163 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008164
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008165 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008166 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8167 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8168 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8169
8170 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8171 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8172 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8173 tmp |= SBI_SSCCTL_PATHALT;
8174 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8175 udelay(32);
8176 }
8177 tmp |= SBI_SSCCTL_DISABLE;
8178 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8179 }
8180
Ville Syrjäläa5805162015-05-26 20:42:30 +03008181 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008182}
8183
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008184#define BEND_IDX(steps) ((50 + (steps)) / 5)
8185
8186static const uint16_t sscdivintphase[] = {
8187 [BEND_IDX( 50)] = 0x3B23,
8188 [BEND_IDX( 45)] = 0x3B23,
8189 [BEND_IDX( 40)] = 0x3C23,
8190 [BEND_IDX( 35)] = 0x3C23,
8191 [BEND_IDX( 30)] = 0x3D23,
8192 [BEND_IDX( 25)] = 0x3D23,
8193 [BEND_IDX( 20)] = 0x3E23,
8194 [BEND_IDX( 15)] = 0x3E23,
8195 [BEND_IDX( 10)] = 0x3F23,
8196 [BEND_IDX( 5)] = 0x3F23,
8197 [BEND_IDX( 0)] = 0x0025,
8198 [BEND_IDX( -5)] = 0x0025,
8199 [BEND_IDX(-10)] = 0x0125,
8200 [BEND_IDX(-15)] = 0x0125,
8201 [BEND_IDX(-20)] = 0x0225,
8202 [BEND_IDX(-25)] = 0x0225,
8203 [BEND_IDX(-30)] = 0x0325,
8204 [BEND_IDX(-35)] = 0x0325,
8205 [BEND_IDX(-40)] = 0x0425,
8206 [BEND_IDX(-45)] = 0x0425,
8207 [BEND_IDX(-50)] = 0x0525,
8208};
8209
8210/*
8211 * Bend CLKOUT_DP
8212 * steps -50 to 50 inclusive, in steps of 5
8213 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8214 * change in clock period = -(steps / 10) * 5.787 ps
8215 */
8216static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8217{
8218 uint32_t tmp;
8219 int idx = BEND_IDX(steps);
8220
8221 if (WARN_ON(steps % 5 != 0))
8222 return;
8223
8224 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8225 return;
8226
8227 mutex_lock(&dev_priv->sb_lock);
8228
8229 if (steps % 10 != 0)
8230 tmp = 0xAAAAAAAB;
8231 else
8232 tmp = 0x00000000;
8233 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8234
8235 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8236 tmp &= 0xffff0000;
8237 tmp |= sscdivintphase[idx];
8238 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8239
8240 mutex_unlock(&dev_priv->sb_lock);
8241}
8242
8243#undef BEND_IDX
8244
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008245static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008246{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008247 struct intel_encoder *encoder;
8248 bool has_vga = false;
8249
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008250 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008251 switch (encoder->type) {
8252 case INTEL_OUTPUT_ANALOG:
8253 has_vga = true;
8254 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008255 default:
8256 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008257 }
8258 }
8259
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008260 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008261 lpt_bend_clkout_dp(dev_priv, 0);
8262 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008263 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008264 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008265 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008266}
8267
Paulo Zanonidde86e22012-12-01 12:04:25 -02008268/*
8269 * Initialize reference clocks when the driver loads
8270 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008271void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008272{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008273 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008274 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008275 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008276 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008277}
8278
Daniel Vetter6ff93602013-04-19 11:24:36 +02008279static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008280{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008281 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8283 int pipe = intel_crtc->pipe;
8284 uint32_t val;
8285
Daniel Vetter78114072013-06-13 00:54:57 +02008286 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008287
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008288 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008289 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008290 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008291 break;
8292 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008293 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008294 break;
8295 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008296 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008297 break;
8298 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008299 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008300 break;
8301 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008302 /* Case prevented by intel_choose_pipe_bpp_dither. */
8303 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008304 }
8305
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008306 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008307 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8308
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008309 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008310 val |= PIPECONF_INTERLACED_ILK;
8311 else
8312 val |= PIPECONF_PROGRESSIVE;
8313
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008314 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008315 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008316
Paulo Zanonic8203562012-09-12 10:06:29 -03008317 I915_WRITE(PIPECONF(pipe), val);
8318 POSTING_READ(PIPECONF(pipe));
8319}
8320
Daniel Vetter6ff93602013-04-19 11:24:36 +02008321static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008322{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008323 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008325 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008326 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008327
Jani Nikula391bf042016-03-18 17:05:40 +02008328 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008329 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8330
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008331 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008332 val |= PIPECONF_INTERLACED_ILK;
8333 else
8334 val |= PIPECONF_PROGRESSIVE;
8335
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008336 I915_WRITE(PIPECONF(cpu_transcoder), val);
8337 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008338}
8339
Jani Nikula391bf042016-03-18 17:05:40 +02008340static void haswell_set_pipemisc(struct drm_crtc *crtc)
8341{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008342 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Shashank Sharmab22ca992017-07-24 19:19:32 +05308344 struct intel_crtc_state *config = intel_crtc->config;
Jani Nikula391bf042016-03-18 17:05:40 +02008345
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00008346 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
Jani Nikula391bf042016-03-18 17:05:40 +02008347 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008348
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008349 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008350 case 18:
8351 val |= PIPEMISC_DITHER_6_BPC;
8352 break;
8353 case 24:
8354 val |= PIPEMISC_DITHER_8_BPC;
8355 break;
8356 case 30:
8357 val |= PIPEMISC_DITHER_10_BPC;
8358 break;
8359 case 36:
8360 val |= PIPEMISC_DITHER_12_BPC;
8361 break;
8362 default:
8363 /* Case prevented by pipe_config_set_bpp. */
8364 BUG();
8365 }
8366
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008367 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008368 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8369
Shashank Sharmab22ca992017-07-24 19:19:32 +05308370 if (config->ycbcr420) {
8371 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
8372 PIPEMISC_YUV420_ENABLE |
8373 PIPEMISC_YUV420_MODE_FULL_BLEND;
8374 }
8375
Jani Nikula391bf042016-03-18 17:05:40 +02008376 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008377 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008378}
8379
Paulo Zanonid4b19312012-11-29 11:29:32 -02008380int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8381{
8382 /*
8383 * Account for spread spectrum to avoid
8384 * oversubscribing the link. Max center spread
8385 * is 2.5%; use 5% for safety's sake.
8386 */
8387 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008388 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008389}
8390
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008391static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008392{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008393 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008394}
8395
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008396static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8397 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008398 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008399{
8400 struct drm_crtc *crtc = &intel_crtc->base;
8401 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008402 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008403 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008404 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008405
Chris Wilsonc1858122010-12-03 21:35:48 +00008406 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008407 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008408 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008409 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008410 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008411 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008412 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008413 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008414 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008415
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008416 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008417
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008418 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8419 fp |= FP_CB_TUNE;
8420
8421 if (reduced_clock) {
8422 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8423
8424 if (reduced_clock->m < factor * reduced_clock->n)
8425 fp2 |= FP_CB_TUNE;
8426 } else {
8427 fp2 = fp;
8428 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008429
Chris Wilson5eddb702010-09-11 13:48:45 +01008430 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008431
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008432 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008433 dpll |= DPLLB_MODE_LVDS;
8434 else
8435 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008436
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008437 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008438 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008439
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008440 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8441 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008442 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008443
Ville Syrjälä37a56502016-06-22 21:57:04 +03008444 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008445 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008446
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03008447 /*
8448 * The high speed IO clock is only really required for
8449 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8450 * possible to share the DPLL between CRT and HDMI. Enabling
8451 * the clock needlessly does no real harm, except use up a
8452 * bit of power potentially.
8453 *
8454 * We'll limit this to IVB with 3 pipes, since it has only two
8455 * DPLLs and so DPLL sharing is the only way to get three pipes
8456 * driving PCH ports at the same time. On SNB we could do this,
8457 * and potentially avoid enabling the second DPLL, but it's not
8458 * clear if it''s a win or loss power wise. No point in doing
8459 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8460 */
8461 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8462 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8463 dpll |= DPLL_SDVO_HIGH_SPEED;
8464
Eric Anholta07d6782011-03-30 13:01:08 -07008465 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008466 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008467 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008468 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008469
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008470 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008471 case 5:
8472 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8473 break;
8474 case 7:
8475 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8476 break;
8477 case 10:
8478 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8479 break;
8480 case 14:
8481 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8482 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008483 }
8484
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008485 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8486 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008487 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008488 else
8489 dpll |= PLL_REF_INPUT_DREFCLK;
8490
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008491 dpll |= DPLL_VCO_ENABLE;
8492
8493 crtc_state->dpll_hw_state.dpll = dpll;
8494 crtc_state->dpll_hw_state.fp0 = fp;
8495 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008496}
8497
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008498static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8499 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008500{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008501 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008502 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008503 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008504 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008505
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008506 memset(&crtc_state->dpll_hw_state, 0,
8507 sizeof(crtc_state->dpll_hw_state));
8508
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008509 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8510 if (!crtc_state->has_pch_encoder)
8511 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008512
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008513 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008514 if (intel_panel_use_ssc(dev_priv)) {
8515 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8516 dev_priv->vbt.lvds_ssc_freq);
8517 refclk = dev_priv->vbt.lvds_ssc_freq;
8518 }
8519
8520 if (intel_is_dual_link_lvds(dev)) {
8521 if (refclk == 100000)
8522 limit = &intel_limits_ironlake_dual_lvds_100m;
8523 else
8524 limit = &intel_limits_ironlake_dual_lvds;
8525 } else {
8526 if (refclk == 100000)
8527 limit = &intel_limits_ironlake_single_lvds_100m;
8528 else
8529 limit = &intel_limits_ironlake_single_lvds;
8530 }
8531 } else {
8532 limit = &intel_limits_ironlake_dac;
8533 }
8534
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008535 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008536 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8537 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008538 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8539 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008540 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008541
Gustavo A. R. Silvacbaa3312017-05-15 16:56:05 -05008542 ironlake_compute_dpll(crtc, crtc_state, NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008543
Gustavo A. R. Silvaefd38b62017-05-15 17:00:28 -05008544 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008545 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8546 pipe_name(crtc->pipe));
8547 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008548 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008549
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008550 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008551}
8552
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008553static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8554 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008555{
8556 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008557 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008558 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008559
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008560 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8561 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8562 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8563 & ~TU_SIZE_MASK;
8564 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8565 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8566 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8567}
8568
8569static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8570 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008571 struct intel_link_m_n *m_n,
8572 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008573{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008574 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008575 enum pipe pipe = crtc->pipe;
8576
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008577 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008578 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8579 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8580 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8581 & ~TU_SIZE_MASK;
8582 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8583 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8584 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008585 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8586 * gen < 8) and if DRRS is supported (to make sure the
8587 * registers are not unnecessarily read).
8588 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008589 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008590 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008591 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8592 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8593 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8594 & ~TU_SIZE_MASK;
8595 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8596 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8597 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8598 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008599 } else {
8600 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8601 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8602 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8603 & ~TU_SIZE_MASK;
8604 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8605 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8606 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8607 }
8608}
8609
8610void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008611 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008612{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008613 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008614 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8615 else
8616 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008617 &pipe_config->dp_m_n,
8618 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008619}
8620
Daniel Vetter72419202013-04-04 13:28:53 +02008621static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008622 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008623{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008624 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008625 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008626}
8627
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008628static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008629 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008630{
8631 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008632 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07008633 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8634 uint32_t ps_ctrl = 0;
8635 int id = -1;
8636 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008637
Chandra Kondurua1b22782015-04-07 15:28:45 -07008638 /* find scaler attached to this pipe */
8639 for (i = 0; i < crtc->num_scalers; i++) {
8640 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8641 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8642 id = i;
8643 pipe_config->pch_pfit.enabled = true;
8644 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8645 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8646 break;
8647 }
8648 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008649
Chandra Kondurua1b22782015-04-07 15:28:45 -07008650 scaler_state->scaler_id = id;
8651 if (id >= 0) {
8652 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8653 } else {
8654 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008655 }
8656}
8657
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008658static void
8659skylake_get_initial_plane_config(struct intel_crtc *crtc,
8660 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008661{
8662 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008663 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008664 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
8665 enum plane_id plane_id = plane->id;
8666 enum pipe pipe = crtc->pipe;
James Ausmus4036c782017-11-13 10:11:28 -08008667 u32 val, base, offset, stride_mult, tiling, alpha;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008668 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008669 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008670 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008671 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008672
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02008673 if (!plane->get_hw_state(plane))
8674 return;
8675
Damien Lespiaud9806c92015-01-21 14:07:19 +00008676 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008677 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008678 DRM_DEBUG_KMS("failed to alloc fb\n");
8679 return;
8680 }
8681
Damien Lespiau1b842c82015-01-21 13:50:54 +00008682 fb = &intel_fb->base;
8683
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008684 fb->dev = dev;
8685
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008686 val = I915_READ(PLANE_CTL(pipe, plane_id));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008687
James Ausmusb5972772018-01-30 11:49:16 -02008688 if (INTEL_GEN(dev_priv) >= 11)
8689 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
8690 else
8691 pixel_format = val & PLANE_CTL_FORMAT_MASK;
James Ausmus4036c782017-11-13 10:11:28 -08008692
8693 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008694 alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
James Ausmus4036c782017-11-13 10:11:28 -08008695 alpha &= PLANE_COLOR_ALPHA_MASK;
8696 } else {
8697 alpha = val & PLANE_CTL_ALPHA_MASK;
8698 }
8699
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008700 fourcc = skl_format_to_fourcc(pixel_format,
James Ausmus4036c782017-11-13 10:11:28 -08008701 val & PLANE_CTL_ORDER_RGBX, alpha);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008702 fb->format = drm_format_info(fourcc);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008703
Damien Lespiau40f46282015-02-27 11:15:21 +00008704 tiling = val & PLANE_CTL_TILED_MASK;
8705 switch (tiling) {
8706 case PLANE_CTL_TILED_LINEAR:
Ben Widawsky2f075562017-03-24 14:29:48 -07008707 fb->modifier = DRM_FORMAT_MOD_LINEAR;
Damien Lespiau40f46282015-02-27 11:15:21 +00008708 break;
8709 case PLANE_CTL_TILED_X:
8710 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008711 fb->modifier = I915_FORMAT_MOD_X_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008712 break;
8713 case PLANE_CTL_TILED_Y:
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008714 if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
8715 fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
8716 else
8717 fb->modifier = I915_FORMAT_MOD_Y_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008718 break;
8719 case PLANE_CTL_TILED_YF:
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008720 if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
8721 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
8722 else
8723 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008724 break;
8725 default:
8726 MISSING_CASE(tiling);
8727 goto error;
8728 }
8729
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008730 base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008731 plane_config->base = base;
8732
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008733 offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008734
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008735 val = I915_READ(PLANE_SIZE(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008736 fb->height = ((val >> 16) & 0xfff) + 1;
8737 fb->width = ((val >> 0) & 0x1fff) + 1;
8738
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008739 val = I915_READ(PLANE_STRIDE(pipe, plane_id));
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008740 stride_mult = intel_fb_stride_alignment(fb, 0);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008741 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8742
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008743 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008744
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008745 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008746
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008747 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8748 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008749 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008750 plane_config->size);
8751
Damien Lespiau2d140302015-02-05 17:22:18 +00008752 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008753 return;
8754
8755error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01008756 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008757}
8758
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008759static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008760 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008761{
8762 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008763 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008764 uint32_t tmp;
8765
8766 tmp = I915_READ(PF_CTL(crtc->pipe));
8767
8768 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008769 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008770 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8771 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008772
8773 /* We currently do not free assignements of panel fitters on
8774 * ivb/hsw (since we don't use the higher upscaling modes which
8775 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008776 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008777 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8778 PF_PIPE_SEL_IVB(crtc->pipe));
8779 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008780 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008781}
8782
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008783static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008784 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008785{
8786 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008787 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008788 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008789 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008790 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008791
Imre Deak17290502016-02-12 18:55:11 +02008792 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8793 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008794 return false;
8795
Daniel Vettere143a212013-07-04 12:01:15 +02008796 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008797 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008798
Imre Deak17290502016-02-12 18:55:11 +02008799 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008800 tmp = I915_READ(PIPECONF(crtc->pipe));
8801 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008802 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008803
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008804 switch (tmp & PIPECONF_BPC_MASK) {
8805 case PIPECONF_6BPC:
8806 pipe_config->pipe_bpp = 18;
8807 break;
8808 case PIPECONF_8BPC:
8809 pipe_config->pipe_bpp = 24;
8810 break;
8811 case PIPECONF_10BPC:
8812 pipe_config->pipe_bpp = 30;
8813 break;
8814 case PIPECONF_12BPC:
8815 pipe_config->pipe_bpp = 36;
8816 break;
8817 default:
8818 break;
8819 }
8820
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008821 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8822 pipe_config->limited_color_range = true;
8823
Daniel Vetterab9412b2013-05-03 11:49:46 +02008824 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008825 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008826 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008827
Daniel Vetter88adfff2013-03-28 10:42:01 +01008828 pipe_config->has_pch_encoder = true;
8829
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008830 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8831 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8832 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008833
8834 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008835
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008836 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03008837 /*
8838 * The pipe->pch transcoder and pch transcoder->pll
8839 * mapping is fixed.
8840 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008841 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008842 } else {
8843 tmp = I915_READ(PCH_DPLL_SEL);
8844 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008845 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008846 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008847 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008848 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008849
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008850 pipe_config->shared_dpll =
8851 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8852 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008853
Lucas De Marchiee1398b2018-03-20 15:06:33 -07008854 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
8855 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008856
8857 tmp = pipe_config->dpll_hw_state.dpll;
8858 pipe_config->pixel_multiplier =
8859 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8860 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008861
8862 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008863 } else {
8864 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008865 }
8866
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008867 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008868 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008869
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008870 ironlake_get_pfit_config(crtc, pipe_config);
8871
Imre Deak17290502016-02-12 18:55:11 +02008872 ret = true;
8873
8874out:
8875 intel_display_power_put(dev_priv, power_domain);
8876
8877 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008878}
8879
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008880static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8881{
Chris Wilson91c8a322016-07-05 10:40:23 +01008882 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008883 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008884
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008885 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008886 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008887 pipe_name(crtc->pipe));
8888
Imre Deak9c3a16c2017-08-14 18:15:30 +03008889 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
8890 "Display power well on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008891 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03008892 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8893 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03008894 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008895 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008896 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008897 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -05008898 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008899 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008900 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008901 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008902 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008903 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008904 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008905
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008906 /*
8907 * In theory we can still leave IRQs enabled, as long as only the HPD
8908 * interrupts remain enabled. We used to check for that, but since it's
8909 * gen-specific and since we only disable LCPLL after we fully disable
8910 * the interrupts, the check below should be enough.
8911 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008912 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008913}
8914
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008915static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8916{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008917 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008918 return I915_READ(D_COMP_HSW);
8919 else
8920 return I915_READ(D_COMP_BDW);
8921}
8922
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008923static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8924{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008925 if (IS_HASWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008926 mutex_lock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008927 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8928 val))
Chris Wilson79cf2192016-08-24 11:16:07 +01008929 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008930 mutex_unlock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008931 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008932 I915_WRITE(D_COMP_BDW, val);
8933 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008934 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008935}
8936
8937/*
8938 * This function implements pieces of two sequences from BSpec:
8939 * - Sequence for display software to disable LCPLL
8940 * - Sequence for display software to allow package C8+
8941 * The steps implemented here are just the steps that actually touch the LCPLL
8942 * register. Callers should take care of disabling all the display engine
8943 * functions, doing the mode unset, fixing interrupts, etc.
8944 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008945static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8946 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008947{
8948 uint32_t val;
8949
8950 assert_can_disable_lcpll(dev_priv);
8951
8952 val = I915_READ(LCPLL_CTL);
8953
8954 if (switch_to_fclk) {
8955 val |= LCPLL_CD_SOURCE_FCLK;
8956 I915_WRITE(LCPLL_CTL, val);
8957
Imre Deakf53dd632016-06-28 13:37:32 +03008958 if (wait_for_us(I915_READ(LCPLL_CTL) &
8959 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008960 DRM_ERROR("Switching to FCLK failed\n");
8961
8962 val = I915_READ(LCPLL_CTL);
8963 }
8964
8965 val |= LCPLL_PLL_DISABLE;
8966 I915_WRITE(LCPLL_CTL, val);
8967 POSTING_READ(LCPLL_CTL);
8968
Chris Wilson24d84412016-06-30 15:33:07 +01008969 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008970 DRM_ERROR("LCPLL still locked\n");
8971
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008972 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008973 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008974 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008975 ndelay(100);
8976
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008977 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8978 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008979 DRM_ERROR("D_COMP RCOMP still in progress\n");
8980
8981 if (allow_power_down) {
8982 val = I915_READ(LCPLL_CTL);
8983 val |= LCPLL_POWER_DOWN_ALLOW;
8984 I915_WRITE(LCPLL_CTL, val);
8985 POSTING_READ(LCPLL_CTL);
8986 }
8987}
8988
8989/*
8990 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8991 * source.
8992 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008993static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008994{
8995 uint32_t val;
8996
8997 val = I915_READ(LCPLL_CTL);
8998
8999 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9000 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9001 return;
9002
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009003 /*
9004 * Make sure we're not on PC8 state before disabling PC8, otherwise
9005 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009006 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009007 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009008
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009009 if (val & LCPLL_POWER_DOWN_ALLOW) {
9010 val &= ~LCPLL_POWER_DOWN_ALLOW;
9011 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009012 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009013 }
9014
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009015 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009016 val |= D_COMP_COMP_FORCE;
9017 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009018 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009019
9020 val = I915_READ(LCPLL_CTL);
9021 val &= ~LCPLL_PLL_DISABLE;
9022 I915_WRITE(LCPLL_CTL, val);
9023
Chris Wilson93220c02016-06-30 15:33:08 +01009024 if (intel_wait_for_register(dev_priv,
9025 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9026 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009027 DRM_ERROR("LCPLL not locked yet\n");
9028
9029 if (val & LCPLL_CD_SOURCE_FCLK) {
9030 val = I915_READ(LCPLL_CTL);
9031 val &= ~LCPLL_CD_SOURCE_FCLK;
9032 I915_WRITE(LCPLL_CTL, val);
9033
Imre Deakf53dd632016-06-28 13:37:32 +03009034 if (wait_for_us((I915_READ(LCPLL_CTL) &
9035 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009036 DRM_ERROR("Switching back to LCPLL failed\n");
9037 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009038
Mika Kuoppala59bad942015-01-16 11:34:40 +02009039 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009040
Ville Syrjälä4c75b942016-10-31 22:37:12 +02009041 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009042 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009043}
9044
Paulo Zanoni765dab672014-03-07 20:08:18 -03009045/*
9046 * Package states C8 and deeper are really deep PC states that can only be
9047 * reached when all the devices on the system allow it, so even if the graphics
9048 * device allows PC8+, it doesn't mean the system will actually get to these
9049 * states. Our driver only allows PC8+ when going into runtime PM.
9050 *
9051 * The requirements for PC8+ are that all the outputs are disabled, the power
9052 * well is disabled and most interrupts are disabled, and these are also
9053 * requirements for runtime PM. When these conditions are met, we manually do
9054 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9055 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9056 * hang the machine.
9057 *
9058 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9059 * the state of some registers, so when we come back from PC8+ we need to
9060 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9061 * need to take care of the registers kept by RC6. Notice that this happens even
9062 * if we don't put the device in PCI D3 state (which is what currently happens
9063 * because of the runtime PM support).
9064 *
9065 * For more, read "Display Sequences for Package C8" on the hardware
9066 * documentation.
9067 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009068void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009069{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009070 uint32_t val;
9071
Paulo Zanonic67a4702013-08-19 13:18:09 -03009072 DRM_DEBUG_KMS("Enabling package C8+\n");
9073
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009074 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009075 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9076 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9077 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9078 }
9079
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009080 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009081 hsw_disable_lcpll(dev_priv, true, true);
9082}
9083
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009084void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009085{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009086 uint32_t val;
9087
Paulo Zanonic67a4702013-08-19 13:18:09 -03009088 DRM_DEBUG_KMS("Disabling package C8+\n");
9089
9090 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009091 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009092
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009093 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009094 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9095 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9096 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9097 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009098}
9099
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009100static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9101 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009102{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009103 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009104 struct intel_encoder *encoder =
9105 intel_ddi_get_crtc_new_encoder(crtc_state);
9106
9107 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
9108 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9109 pipe_name(crtc->pipe));
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009110 return -EINVAL;
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009111 }
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009112 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009113
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009114 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009115}
9116
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009117static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
9118 enum port port,
9119 struct intel_crtc_state *pipe_config)
9120{
9121 enum intel_dpll_id id;
9122 u32 temp;
9123
9124 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
Paulo Zanonidfbd4502017-08-25 16:40:04 -03009125 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009126
9127 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
9128 return;
9129
9130 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9131}
9132
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309133static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9134 enum port port,
9135 struct intel_crtc_state *pipe_config)
9136{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009137 enum intel_dpll_id id;
9138
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309139 switch (port) {
9140 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +02009141 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309142 break;
9143 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +02009144 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309145 break;
9146 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +02009147 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309148 break;
9149 default:
9150 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009151 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309152 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009153
9154 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309155}
9156
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009157static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9158 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009159 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009160{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009161 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009162 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009163
9164 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009165 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009166
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009167 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009168 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009169
9170 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009171}
9172
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009173static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9174 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009175 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009176{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009177 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009178 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009179
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009180 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009181 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009182 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009183 break;
9184 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009185 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009186 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009187 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009188 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009189 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009190 case PORT_CLK_SEL_LCPLL_810:
9191 id = DPLL_ID_LCPLL_810;
9192 break;
9193 case PORT_CLK_SEL_LCPLL_1350:
9194 id = DPLL_ID_LCPLL_1350;
9195 break;
9196 case PORT_CLK_SEL_LCPLL_2700:
9197 id = DPLL_ID_LCPLL_2700;
9198 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009199 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009200 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009201 /* fall through */
9202 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009203 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009204 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009205
9206 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009207}
9208
Jani Nikulacf304292016-03-18 17:05:41 +02009209static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9210 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009211 u64 *power_domain_mask)
Jani Nikulacf304292016-03-18 17:05:41 +02009212{
9213 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009214 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02009215 enum intel_display_power_domain power_domain;
9216 u32 tmp;
9217
Imre Deakd9a7bc62016-05-12 16:18:50 +03009218 /*
9219 * The pipe->transcoder mapping is fixed with the exception of the eDP
9220 * transcoder handled below.
9221 */
Jani Nikulacf304292016-03-18 17:05:41 +02009222 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9223
9224 /*
9225 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9226 * consistency and less surprising code; it's in always on power).
9227 */
9228 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9229 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9230 enum pipe trans_edp_pipe;
9231 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9232 default:
9233 WARN(1, "unknown pipe linked to edp transcoder\n");
9234 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9235 case TRANS_DDI_EDP_INPUT_A_ON:
9236 trans_edp_pipe = PIPE_A;
9237 break;
9238 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9239 trans_edp_pipe = PIPE_B;
9240 break;
9241 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9242 trans_edp_pipe = PIPE_C;
9243 break;
9244 }
9245
9246 if (trans_edp_pipe == crtc->pipe)
9247 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9248 }
9249
9250 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9251 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9252 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009253 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikulacf304292016-03-18 17:05:41 +02009254
9255 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9256
9257 return tmp & PIPECONF_ENABLE;
9258}
9259
Jani Nikula4d1de972016-03-18 17:05:42 +02009260static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9261 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009262 u64 *power_domain_mask)
Jani Nikula4d1de972016-03-18 17:05:42 +02009263{
9264 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009265 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +02009266 enum intel_display_power_domain power_domain;
9267 enum port port;
9268 enum transcoder cpu_transcoder;
9269 u32 tmp;
9270
Jani Nikula4d1de972016-03-18 17:05:42 +02009271 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9272 if (port == PORT_A)
9273 cpu_transcoder = TRANSCODER_DSI_A;
9274 else
9275 cpu_transcoder = TRANSCODER_DSI_C;
9276
9277 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9278 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9279 continue;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009280 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikula4d1de972016-03-18 17:05:42 +02009281
Imre Deakdb18b6a2016-03-24 12:41:40 +02009282 /*
9283 * The PLL needs to be enabled with a valid divider
9284 * configuration, otherwise accessing DSI registers will hang
9285 * the machine. See BSpec North Display Engine
9286 * registers/MIPI[BXT]. We can break out here early, since we
9287 * need the same DSI PLL to be enabled for both DSI ports.
9288 */
9289 if (!intel_dsi_pll_is_enabled(dev_priv))
9290 break;
9291
Jani Nikula4d1de972016-03-18 17:05:42 +02009292 /* XXX: this works for video mode only */
9293 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9294 if (!(tmp & DPI_ENABLE))
9295 continue;
9296
9297 tmp = I915_READ(MIPI_CTRL(port));
9298 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9299 continue;
9300
9301 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +02009302 break;
9303 }
9304
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009305 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +02009306}
9307
Daniel Vetter26804af2014-06-25 22:01:55 +03009308static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009309 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009310{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009311 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009312 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009313 enum port port;
9314 uint32_t tmp;
9315
9316 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9317
9318 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9319
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009320 if (IS_CANNONLAKE(dev_priv))
9321 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9322 else if (IS_GEN9_BC(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009323 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009324 else if (IS_GEN9_LP(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309325 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009326 else
9327 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009328
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009329 pll = pipe_config->shared_dpll;
9330 if (pll) {
Lucas De Marchiee1398b2018-03-20 15:06:33 -07009331 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
9332 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009333 }
9334
Daniel Vetter26804af2014-06-25 22:01:55 +03009335 /*
9336 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9337 * DDI E. So just check whether this pipe is wired to DDI E and whether
9338 * the PCH transcoder is on.
9339 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009340 if (INTEL_GEN(dev_priv) < 9 &&
Damien Lespiauca370452013-12-03 13:56:24 +00009341 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009342 pipe_config->has_pch_encoder = true;
9343
9344 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9345 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9346 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9347
9348 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9349 }
9350}
9351
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009352static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009353 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009354{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009355 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02009356 enum intel_display_power_domain power_domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009357 u64 power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009358 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009359
Imre Deake79dfb52017-07-20 01:50:57 +03009360 intel_crtc_init_scalers(crtc, pipe_config);
Imre Deak5fb9dad2017-07-20 14:28:20 +03009361
Imre Deak17290502016-02-12 18:55:11 +02009362 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9363 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009364 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009365 power_domain_mask = BIT_ULL(power_domain);
Imre Deak17290502016-02-12 18:55:11 +02009366
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009367 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009368
Jani Nikulacf304292016-03-18 17:05:41 +02009369 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009370
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009371 if (IS_GEN9_LP(dev_priv) &&
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009372 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9373 WARN_ON(active);
9374 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +02009375 }
9376
Jani Nikulacf304292016-03-18 17:05:41 +02009377 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009378 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009379
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009380 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +02009381 haswell_get_ddi_port_state(crtc, pipe_config);
9382 intel_get_pipe_timings(crtc, pipe_config);
9383 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009384
Jani Nikulabc58be62016-03-18 17:05:39 +02009385 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009386
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009387 pipe_config->gamma_mode =
9388 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9389
Rodrigo Vivibd30ca22017-09-26 14:13:46 -07009390 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
Shashank Sharmab22ca992017-07-24 19:19:32 +05309391 u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
9392 bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
9393
Rodrigo Vivibd30ca22017-09-26 14:13:46 -07009394 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Shashank Sharmab22ca992017-07-24 19:19:32 +05309395 bool blend_mode_420 = tmp &
9396 PIPEMISC_YUV420_MODE_FULL_BLEND;
9397
9398 pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
9399 if (pipe_config->ycbcr420 != clrspace_yuv ||
9400 pipe_config->ycbcr420 != blend_mode_420)
9401 DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
9402 } else if (clrspace_yuv) {
9403 DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
9404 }
9405 }
9406
Imre Deak17290502016-02-12 18:55:11 +02009407 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9408 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009409 power_domain_mask |= BIT_ULL(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009410 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009411 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009412 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009413 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009414 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009415
Maarten Lankhorst24f28452017-11-22 19:39:01 +01009416 if (hsw_crtc_supports_ips(crtc)) {
9417 if (IS_HASWELL(dev_priv))
9418 pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
9419 else {
9420 /*
9421 * We cannot readout IPS state on broadwell, set to
9422 * true so we can set it to a defined state on first
9423 * commit.
9424 */
9425 pipe_config->ips_enabled = true;
9426 }
9427 }
9428
Jani Nikula4d1de972016-03-18 17:05:42 +02009429 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9430 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009431 pipe_config->pixel_multiplier =
9432 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9433 } else {
9434 pipe_config->pixel_multiplier = 1;
9435 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009436
Imre Deak17290502016-02-12 18:55:11 +02009437out:
9438 for_each_power_domain(power_domain, power_domain_mask)
9439 intel_display_power_put(dev_priv, power_domain);
9440
Jani Nikulacf304292016-03-18 17:05:41 +02009441 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009442}
9443
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009444static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009445{
9446 struct drm_i915_private *dev_priv =
9447 to_i915(plane_state->base.plane->dev);
9448 const struct drm_framebuffer *fb = plane_state->base.fb;
9449 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9450 u32 base;
9451
9452 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9453 base = obj->phys_handle->busaddr;
9454 else
9455 base = intel_plane_ggtt_offset(plane_state);
9456
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009457 base += plane_state->main.offset;
9458
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009459 /* ILK+ do this automagically */
9460 if (HAS_GMCH_DISPLAY(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009461 plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009462 base += (plane_state->base.crtc_h *
9463 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9464
9465 return base;
9466}
9467
Ville Syrjäläed270222017-03-27 21:55:36 +03009468static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9469{
9470 int x = plane_state->base.crtc_x;
9471 int y = plane_state->base.crtc_y;
9472 u32 pos = 0;
9473
9474 if (x < 0) {
9475 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9476 x = -x;
9477 }
9478 pos |= x << CURSOR_X_SHIFT;
9479
9480 if (y < 0) {
9481 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9482 y = -y;
9483 }
9484 pos |= y << CURSOR_Y_SHIFT;
9485
9486 return pos;
9487}
9488
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009489static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9490{
9491 const struct drm_mode_config *config =
9492 &plane_state->base.plane->dev->mode_config;
9493 int width = plane_state->base.crtc_w;
9494 int height = plane_state->base.crtc_h;
9495
9496 return width > 0 && width <= config->cursor_width &&
9497 height > 0 && height <= config->cursor_height;
9498}
9499
Ville Syrjälä659056f2017-03-27 21:55:39 +03009500static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9501 struct intel_plane_state *plane_state)
9502{
9503 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009504 int src_x, src_y;
9505 u32 offset;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009506 int ret;
9507
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009508 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
9509 &crtc_state->base,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009510 DRM_PLANE_HELPER_NO_SCALING,
9511 DRM_PLANE_HELPER_NO_SCALING,
9512 true, true);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009513 if (ret)
9514 return ret;
9515
9516 if (!fb)
9517 return 0;
9518
9519 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9520 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9521 return -EINVAL;
9522 }
9523
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009524 src_x = plane_state->base.src_x >> 16;
9525 src_y = plane_state->base.src_y >> 16;
9526
9527 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9528 offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
9529
9530 if (src_x != 0 || src_y != 0) {
9531 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9532 return -EINVAL;
9533 }
9534
9535 plane_state->main.offset = offset;
9536
Ville Syrjälä659056f2017-03-27 21:55:39 +03009537 return 0;
9538}
9539
Ville Syrjälä292889e2017-03-17 23:18:01 +02009540static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9541 const struct intel_plane_state *plane_state)
9542{
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009543 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009544
Ville Syrjälä292889e2017-03-17 23:18:01 +02009545 return CURSOR_ENABLE |
9546 CURSOR_GAMMA_ENABLE |
9547 CURSOR_FORMAT_ARGB |
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009548 CURSOR_STRIDE(fb->pitches[0]);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009549}
9550
Ville Syrjälä659056f2017-03-27 21:55:39 +03009551static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9552{
Ville Syrjälä659056f2017-03-27 21:55:39 +03009553 int width = plane_state->base.crtc_w;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009554
9555 /*
9556 * 845g/865g are only limited by the width of their cursors,
9557 * the height is arbitrary up to the precision of the register.
9558 */
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009559 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009560}
9561
9562static int i845_check_cursor(struct intel_plane *plane,
9563 struct intel_crtc_state *crtc_state,
9564 struct intel_plane_state *plane_state)
9565{
9566 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009567 int ret;
9568
9569 ret = intel_check_cursor(crtc_state, plane_state);
9570 if (ret)
9571 return ret;
9572
9573 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009574 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009575 return 0;
9576
9577 /* Check for which cursor types we support */
9578 if (!i845_cursor_size_ok(plane_state)) {
9579 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9580 plane_state->base.crtc_w,
9581 plane_state->base.crtc_h);
9582 return -EINVAL;
9583 }
9584
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009585 switch (fb->pitches[0]) {
Chris Wilson560b85b2010-08-07 11:01:38 +01009586 case 256:
9587 case 512:
9588 case 1024:
9589 case 2048:
Ville Syrjälädc41c152014-08-13 11:57:05 +03009590 break;
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009591 default:
9592 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9593 fb->pitches[0]);
9594 return -EINVAL;
Chris Wilson560b85b2010-08-07 11:01:38 +01009595 }
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009596
Ville Syrjälä659056f2017-03-27 21:55:39 +03009597 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9598
9599 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009600}
9601
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009602static void i845_update_cursor(struct intel_plane *plane,
9603 const struct intel_crtc_state *crtc_state,
Chris Wilson560b85b2010-08-07 11:01:38 +01009604 const struct intel_plane_state *plane_state)
9605{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009606 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009607 u32 cntl = 0, base = 0, pos = 0, size = 0;
9608 unsigned long irqflags;
Chris Wilson560b85b2010-08-07 11:01:38 +01009609
Ville Syrjälä936e71e2016-07-26 19:06:59 +03009610 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009611 unsigned int width = plane_state->base.crtc_w;
9612 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009613
Ville Syrjäläa0864d52017-03-23 21:27:09 +02009614 cntl = plane_state->ctl;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009615 size = (height << 12) | width;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009616
9617 base = intel_cursor_base(plane_state);
9618 pos = intel_cursor_position(plane_state);
Chris Wilson4b0e3332014-05-30 16:35:26 +03009619 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009620
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009621 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9622
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009623 /* On these chipsets we can only modify the base/size/stride
9624 * whilst the cursor is disabled.
9625 */
9626 if (plane->cursor.base != base ||
9627 plane->cursor.size != size ||
9628 plane->cursor.cntl != cntl) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009629 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009630 I915_WRITE_FW(CURBASE(PIPE_A), base);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009631 I915_WRITE_FW(CURSIZE, size);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009632 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009633 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
Ville Syrjälä75343a42017-03-27 21:55:38 +03009634
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009635 plane->cursor.base = base;
9636 plane->cursor.size = size;
9637 plane->cursor.cntl = cntl;
9638 } else {
9639 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009640 }
9641
Ville Syrjälä75343a42017-03-27 21:55:38 +03009642 POSTING_READ_FW(CURCNTR(PIPE_A));
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009643
9644 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9645}
9646
9647static void i845_disable_cursor(struct intel_plane *plane,
9648 struct intel_crtc *crtc)
9649{
9650 i845_update_cursor(plane, NULL, NULL);
Chris Wilson560b85b2010-08-07 11:01:38 +01009651}
9652
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009653static bool i845_cursor_get_hw_state(struct intel_plane *plane)
9654{
9655 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9656 enum intel_display_power_domain power_domain;
9657 bool ret;
9658
9659 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
9660 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9661 return false;
9662
9663 ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
9664
9665 intel_display_power_put(dev_priv, power_domain);
9666
9667 return ret;
9668}
9669
Ville Syrjälä292889e2017-03-17 23:18:01 +02009670static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9671 const struct intel_plane_state *plane_state)
9672{
9673 struct drm_i915_private *dev_priv =
9674 to_i915(plane_state->base.plane->dev);
9675 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009676 u32 cntl;
9677
9678 cntl = MCURSOR_GAMMA_ENABLE;
9679
9680 if (HAS_DDI(dev_priv))
9681 cntl |= CURSOR_PIPE_CSC_ENABLE;
9682
Ville Syrjälä32ea06b2018-01-30 22:38:01 +02009683 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
9684 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009685
9686 switch (plane_state->base.crtc_w) {
9687 case 64:
9688 cntl |= CURSOR_MODE_64_ARGB_AX;
9689 break;
9690 case 128:
9691 cntl |= CURSOR_MODE_128_ARGB_AX;
9692 break;
9693 case 256:
9694 cntl |= CURSOR_MODE_256_ARGB_AX;
9695 break;
9696 default:
9697 MISSING_CASE(plane_state->base.crtc_w);
9698 return 0;
9699 }
9700
Robert Fossc2c446a2017-05-19 16:50:17 -04009701 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjälä292889e2017-03-17 23:18:01 +02009702 cntl |= CURSOR_ROTATE_180;
9703
9704 return cntl;
9705}
9706
Ville Syrjälä659056f2017-03-27 21:55:39 +03009707static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009708{
Ville Syrjälä024faac2017-03-27 21:55:42 +03009709 struct drm_i915_private *dev_priv =
9710 to_i915(plane_state->base.plane->dev);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009711 int width = plane_state->base.crtc_w;
9712 int height = plane_state->base.crtc_h;
Chris Wilson560b85b2010-08-07 11:01:38 +01009713
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009714 if (!intel_cursor_size_ok(plane_state))
Ville Syrjälädc41c152014-08-13 11:57:05 +03009715 return false;
9716
Ville Syrjälä024faac2017-03-27 21:55:42 +03009717 /* Cursor width is limited to a few power-of-two sizes */
9718 switch (width) {
Ville Syrjälä659056f2017-03-27 21:55:39 +03009719 case 256:
9720 case 128:
Ville Syrjälä659056f2017-03-27 21:55:39 +03009721 case 64:
9722 break;
9723 default:
9724 return false;
9725 }
9726
Ville Syrjälädc41c152014-08-13 11:57:05 +03009727 /*
Ville Syrjälä024faac2017-03-27 21:55:42 +03009728 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9729 * height from 8 lines up to the cursor width, when the
9730 * cursor is not rotated. Everything else requires square
9731 * cursors.
Ville Syrjälädc41c152014-08-13 11:57:05 +03009732 */
Ville Syrjälä024faac2017-03-27 21:55:42 +03009733 if (HAS_CUR_FBC(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009734 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009735 if (height < 8 || height > width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009736 return false;
9737 } else {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009738 if (height != width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009739 return false;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009740 }
9741
9742 return true;
9743}
9744
Ville Syrjälä659056f2017-03-27 21:55:39 +03009745static int i9xx_check_cursor(struct intel_plane *plane,
9746 struct intel_crtc_state *crtc_state,
9747 struct intel_plane_state *plane_state)
9748{
9749 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9750 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009751 enum pipe pipe = plane->pipe;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009752 int ret;
9753
9754 ret = intel_check_cursor(crtc_state, plane_state);
9755 if (ret)
9756 return ret;
9757
9758 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009759 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009760 return 0;
9761
9762 /* Check for which cursor types we support */
9763 if (!i9xx_cursor_size_ok(plane_state)) {
9764 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9765 plane_state->base.crtc_w,
9766 plane_state->base.crtc_h);
9767 return -EINVAL;
9768 }
9769
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009770 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9771 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9772 fb->pitches[0], plane_state->base.crtc_w);
9773 return -EINVAL;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009774 }
9775
9776 /*
9777 * There's something wrong with the cursor on CHV pipe C.
9778 * If it straddles the left edge of the screen then
9779 * moving it away from the edge or disabling it often
9780 * results in a pipe underrun, and often that can lead to
9781 * dead pipe (constant underrun reported, and it scans
9782 * out just a solid color). To recover from that, the
9783 * display power well must be turned off and on again.
9784 * Refuse the put the cursor into that compromised position.
9785 */
9786 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9787 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9788 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9789 return -EINVAL;
9790 }
9791
9792 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
9793
9794 return 0;
9795}
9796
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009797static void i9xx_update_cursor(struct intel_plane *plane,
9798 const struct intel_crtc_state *crtc_state,
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309799 const struct intel_plane_state *plane_state)
9800{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009801 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9802 enum pipe pipe = plane->pipe;
Ville Syrjälä024faac2017-03-27 21:55:42 +03009803 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009804 unsigned long irqflags;
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309805
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009806 if (plane_state && plane_state->base.visible) {
Ville Syrjäläa0864d52017-03-23 21:27:09 +02009807 cntl = plane_state->ctl;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009808
Ville Syrjälä024faac2017-03-27 21:55:42 +03009809 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
9810 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
9811
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009812 base = intel_cursor_base(plane_state);
9813 pos = intel_cursor_position(plane_state);
9814 }
9815
9816 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9817
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009818 /*
9819 * On some platforms writing CURCNTR first will also
9820 * cause CURPOS to be armed by the CURBASE write.
9821 * Without the CURCNTR write the CURPOS write would
Ville Syrjälä8753d2b2017-07-14 18:52:27 +03009822 * arm itself. Thus we always start the full update
9823 * with a CURCNTR write.
9824 *
9825 * On other platforms CURPOS always requires the
9826 * CURBASE write to arm the update. Additonally
9827 * a write to any of the cursor register will cancel
9828 * an already armed cursor update. Thus leaving out
9829 * the CURBASE write after CURPOS could lead to a
9830 * cursor that doesn't appear to move, or even change
9831 * shape. Thus we always write CURBASE.
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009832 *
9833 * CURCNTR and CUR_FBC_CTL are always
9834 * armed by the CURBASE write only.
9835 */
9836 if (plane->cursor.base != base ||
Ville Syrjälä024faac2017-03-27 21:55:42 +03009837 plane->cursor.size != fbc_ctl ||
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009838 plane->cursor.cntl != cntl) {
9839 I915_WRITE_FW(CURCNTR(pipe), cntl);
9840 if (HAS_CUR_FBC(dev_priv))
9841 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
9842 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä75343a42017-03-27 21:55:38 +03009843 I915_WRITE_FW(CURBASE(pipe), base);
9844
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009845 plane->cursor.base = base;
9846 plane->cursor.size = fbc_ctl;
9847 plane->cursor.cntl = cntl;
9848 } else {
9849 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä8753d2b2017-07-14 18:52:27 +03009850 I915_WRITE_FW(CURBASE(pipe), base);
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009851 }
9852
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309853 POSTING_READ_FW(CURBASE(pipe));
9854
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009855 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009856}
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009857
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009858static void i9xx_disable_cursor(struct intel_plane *plane,
9859 struct intel_crtc *crtc)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009860{
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009861 i9xx_update_cursor(plane, NULL, NULL);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009862}
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009863
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009864static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
9865{
9866 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9867 enum intel_display_power_domain power_domain;
9868 enum pipe pipe = plane->pipe;
9869 bool ret;
9870
9871 /*
9872 * Not 100% correct for planes that can move between pipes,
9873 * but that's only the case for gen2-3 which don't have any
9874 * display power wells.
9875 */
9876 power_domain = POWER_DOMAIN_PIPE(pipe);
9877 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9878 return false;
9879
9880 ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
9881
9882 intel_display_power_put(dev_priv, power_domain);
9883
9884 return ret;
9885}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009886
Jesse Barnes79e53942008-11-07 14:24:08 -08009887/* VESA 640x480x72Hz mode to set on the pipe */
Ville Syrjäläbacdcd52017-05-18 22:38:37 +03009888static const struct drm_display_mode load_detect_mode = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009889 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9890 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9891};
9892
Daniel Vettera8bb6812014-02-10 18:00:39 +01009893struct drm_framebuffer *
Chris Wilson24dbf512017-02-15 10:59:18 +00009894intel_framebuffer_create(struct drm_i915_gem_object *obj,
9895 struct drm_mode_fb_cmd2 *mode_cmd)
Chris Wilsond2dff872011-04-19 08:36:26 +01009896{
9897 struct intel_framebuffer *intel_fb;
9898 int ret;
9899
9900 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009901 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009902 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +01009903
Chris Wilson24dbf512017-02-15 10:59:18 +00009904 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009905 if (ret)
9906 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009907
9908 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009909
Lukas Wunnerdcb13942015-07-04 11:50:58 +02009910err:
9911 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009912 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009913}
9914
Ville Syrjälä20bdc112017-12-20 10:35:45 +01009915static int intel_modeset_disable_planes(struct drm_atomic_state *state,
9916 struct drm_crtc *crtc)
Chris Wilsond2dff872011-04-19 08:36:26 +01009917{
Ville Syrjälä20bdc112017-12-20 10:35:45 +01009918 struct drm_plane *plane;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009919 struct drm_plane_state *plane_state;
Ville Syrjälä20bdc112017-12-20 10:35:45 +01009920 int ret, i;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009921
Ville Syrjälä20bdc112017-12-20 10:35:45 +01009922 ret = drm_atomic_add_affected_planes(state, crtc);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009923 if (ret)
9924 return ret;
Ville Syrjälä20bdc112017-12-20 10:35:45 +01009925
9926 for_each_new_plane_in_state(state, plane, plane_state, i) {
9927 if (plane_state->crtc != crtc)
9928 continue;
9929
9930 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
9931 if (ret)
9932 return ret;
9933
9934 drm_atomic_set_fb_for_plane(plane_state, NULL);
9935 }
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009936
9937 return 0;
9938}
9939
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +02009940int intel_get_load_detect_pipe(struct drm_connector *connector,
Ville Syrjäläbacdcd52017-05-18 22:38:37 +03009941 const struct drm_display_mode *mode,
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +02009942 struct intel_load_detect_pipe *old,
9943 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009944{
9945 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009946 struct intel_encoder *intel_encoder =
9947 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009948 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009949 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009950 struct drm_crtc *crtc = NULL;
9951 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02009952 struct drm_i915_private *dev_priv = to_i915(dev);
Rob Clark51fd3712013-11-19 12:10:12 -05009953 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009954 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009955 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009956 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009957 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009958
Chris Wilsond2dff872011-04-19 08:36:26 +01009959 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009960 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009961 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009962
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009963 old->restore_state = NULL;
9964
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +02009965 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009966
Jesse Barnes79e53942008-11-07 14:24:08 -08009967 /*
9968 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009969 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009970 * - if the connector already has an assigned crtc, use it (but make
9971 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009972 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009973 * - try to find the first unused crtc that can drive this connector,
9974 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009975 */
9976
9977 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009978 if (connector->state->crtc) {
9979 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009980
Rob Clark51fd3712013-11-19 12:10:12 -05009981 ret = drm_modeset_lock(&crtc->mutex, ctx);
9982 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +02009983 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +01009984
9985 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009986 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -08009987 }
9988
9989 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009990 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009991 i++;
9992 if (!(encoder->possible_crtcs & (1 << i)))
9993 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +01009994
9995 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9996 if (ret)
9997 goto fail;
9998
9999 if (possible_crtc->state->enable) {
10000 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010001 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010002 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010003
10004 crtc = possible_crtc;
10005 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010006 }
10007
10008 /*
10009 * If we didn't find an unused CRTC, don't use any.
10010 */
10011 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010012 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Dan Carpenterf4bf77b2017-04-14 22:54:25 +030010013 ret = -ENODEV;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010014 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010015 }
10016
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010017found:
10018 intel_crtc = to_intel_crtc(crtc);
10019
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010020 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010021 restore_state = drm_atomic_state_alloc(dev);
10022 if (!state || !restore_state) {
10023 ret = -ENOMEM;
10024 goto fail;
10025 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010026
10027 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010028 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010029
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010030 connector_state = drm_atomic_get_connector_state(state, connector);
10031 if (IS_ERR(connector_state)) {
10032 ret = PTR_ERR(connector_state);
10033 goto fail;
10034 }
10035
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010036 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10037 if (ret)
10038 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010039
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010040 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10041 if (IS_ERR(crtc_state)) {
10042 ret = PTR_ERR(crtc_state);
10043 goto fail;
10044 }
10045
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010046 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010047
Chris Wilson64927112011-04-20 07:25:26 +010010048 if (!mode)
10049 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010050
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010051 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010052 if (ret)
10053 goto fail;
10054
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010055 ret = intel_modeset_disable_planes(state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010056 if (ret)
10057 goto fail;
10058
10059 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10060 if (!ret)
10061 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
Ville Syrjäläbe90cc32018-03-22 17:23:12 +020010062 if (!ret)
10063 ret = drm_atomic_add_affected_planes(restore_state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010064 if (ret) {
10065 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10066 goto fail;
10067 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010068
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010069 ret = drm_atomic_commit(state);
10070 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010071 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010072 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010073 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010074
10075 old->restore_state = restore_state;
Chris Wilson7abbd112017-01-19 11:37:49 +000010076 drm_atomic_state_put(state);
Chris Wilson71731882011-04-19 23:10:58 +010010077
Jesse Barnes79e53942008-11-07 14:24:08 -080010078 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020010079 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010080 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010081
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010082fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010010083 if (state) {
10084 drm_atomic_state_put(state);
10085 state = NULL;
10086 }
10087 if (restore_state) {
10088 drm_atomic_state_put(restore_state);
10089 restore_state = NULL;
10090 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010091
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010092 if (ret == -EDEADLK)
10093 return ret;
Rob Clark51fd3712013-11-19 12:10:12 -050010094
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010095 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010096}
10097
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010098void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010099 struct intel_load_detect_pipe *old,
10100 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010101{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010102 struct intel_encoder *intel_encoder =
10103 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010104 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010105 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010106 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010107
Chris Wilsond2dff872011-04-19 08:36:26 +010010108 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010109 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010110 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010111
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010112 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010113 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010114
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010010115 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Chris Wilson08536952016-10-14 13:18:18 +010010116 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010117 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010010118 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010119}
10120
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010121static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010122 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010123{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010124 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010125 u32 dpll = pipe_config->dpll_hw_state.dpll;
10126
10127 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010128 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010010129 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010130 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010131 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010132 return 96000;
10133 else
10134 return 48000;
10135}
10136
Jesse Barnes79e53942008-11-07 14:24:08 -080010137/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010138static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010139 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010140{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010141 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010142 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010143 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010144 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010145 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010146 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010147 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010148 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010149
10150 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010151 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010152 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010153 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010154
10155 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010156 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010157 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10158 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010159 } else {
10160 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10161 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10162 }
10163
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010164 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010165 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010166 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10167 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010168 else
10169 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010170 DPLL_FPA01_P1_POST_DIV_SHIFT);
10171
10172 switch (dpll & DPLL_MODE_MASK) {
10173 case DPLLB_MODE_DAC_SERIAL:
10174 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10175 5 : 10;
10176 break;
10177 case DPLLB_MODE_LVDS:
10178 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10179 7 : 14;
10180 break;
10181 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010182 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010183 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010184 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010185 }
10186
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010187 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +030010188 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010189 else
Imre Deakdccbea32015-06-22 23:35:51 +030010190 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010191 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010192 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010193 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010194
10195 if (is_lvds) {
10196 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10197 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010198
10199 if (lvds & LVDS_CLKB_POWER_UP)
10200 clock.p2 = 7;
10201 else
10202 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010203 } else {
10204 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10205 clock.p1 = 2;
10206 else {
10207 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10208 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10209 }
10210 if (dpll & PLL_P2_DIVIDE_BY_4)
10211 clock.p2 = 4;
10212 else
10213 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010214 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010215
Imre Deakdccbea32015-06-22 23:35:51 +030010216 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010217 }
10218
Ville Syrjälä18442d02013-09-13 16:00:08 +030010219 /*
10220 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010221 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010222 * encoder's get_config() function.
10223 */
Imre Deakdccbea32015-06-22 23:35:51 +030010224 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010225}
10226
Ville Syrjälä6878da02013-09-13 15:59:11 +030010227int intel_dotclock_calculate(int link_freq,
10228 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010229{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010230 /*
10231 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010232 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010233 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010234 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010235 *
10236 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010237 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010238 */
10239
Ville Syrjälä6878da02013-09-13 15:59:11 +030010240 if (!m_n->link_n)
10241 return 0;
10242
Chris Wilson31236982017-09-13 11:51:53 +010010243 return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010244}
10245
Ville Syrjälä18442d02013-09-13 16:00:08 +030010246static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010247 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010248{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010249 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010250
10251 /* read out port_clock from the DPLL */
10252 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010253
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010254 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010255 * In case there is an active pipe without active ports,
10256 * we may need some idea for the dotclock anyway.
10257 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010258 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010259 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010260 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010261 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010262}
10263
Ville Syrjäläde330812017-10-09 19:19:50 +030010264/* Returns the currently programmed mode of the given encoder. */
10265struct drm_display_mode *
10266intel_encoder_current_mode(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010267{
Ville Syrjäläde330812017-10-09 19:19:50 +030010268 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
10269 struct intel_crtc_state *crtc_state;
Jesse Barnes79e53942008-11-07 14:24:08 -080010270 struct drm_display_mode *mode;
Ville Syrjäläde330812017-10-09 19:19:50 +030010271 struct intel_crtc *crtc;
10272 enum pipe pipe;
10273
10274 if (!encoder->get_hw_state(encoder, &pipe))
10275 return NULL;
10276
10277 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -080010278
10279 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10280 if (!mode)
10281 return NULL;
10282
Ville Syrjäläde330812017-10-09 19:19:50 +030010283 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
10284 if (!crtc_state) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010285 kfree(mode);
10286 return NULL;
10287 }
10288
Ville Syrjäläde330812017-10-09 19:19:50 +030010289 crtc_state->base.crtc = &crtc->base;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010290
Ville Syrjäläde330812017-10-09 19:19:50 +030010291 if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
10292 kfree(crtc_state);
10293 kfree(mode);
10294 return NULL;
10295 }
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010296
Ville Syrjäläde330812017-10-09 19:19:50 +030010297 encoder->get_config(encoder, crtc_state);
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010298
Ville Syrjäläde330812017-10-09 19:19:50 +030010299 intel_mode_from_pipe_config(mode, crtc_state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010300
Ville Syrjäläde330812017-10-09 19:19:50 +030010301 kfree(crtc_state);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010302
Jesse Barnes79e53942008-11-07 14:24:08 -080010303 return mode;
10304}
10305
10306static void intel_crtc_destroy(struct drm_crtc *crtc)
10307{
10308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10309
10310 drm_crtc_cleanup(crtc);
10311 kfree(intel_crtc);
10312}
10313
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010314/**
10315 * intel_wm_need_update - Check whether watermarks need updating
10316 * @plane: drm plane
10317 * @state: new plane state
10318 *
10319 * Check current plane state versus the new one to determine whether
10320 * watermarks need to be recalculated.
10321 *
10322 * Returns true or false.
10323 */
10324static bool intel_wm_need_update(struct drm_plane *plane,
10325 struct drm_plane_state *state)
10326{
Matt Roperd21fbe82015-09-24 15:53:12 -070010327 struct intel_plane_state *new = to_intel_plane_state(state);
10328 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10329
10330 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010331 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010332 return true;
10333
10334 if (!cur->base.fb || !new->base.fb)
10335 return false;
10336
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010337 if (cur->base.fb->modifier != new->base.fb->modifier ||
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010338 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010339 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10340 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10341 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10342 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010343 return true;
10344
10345 return false;
10346}
10347
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010348static bool needs_scaling(const struct intel_plane_state *state)
Matt Roperd21fbe82015-09-24 15:53:12 -070010349{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010350 int src_w = drm_rect_width(&state->base.src) >> 16;
10351 int src_h = drm_rect_height(&state->base.src) >> 16;
10352 int dst_w = drm_rect_width(&state->base.dst);
10353 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070010354
10355 return (src_w != dst_w || src_h != dst_h);
10356}
10357
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010358int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
10359 struct drm_crtc_state *crtc_state,
10360 const struct intel_plane_state *old_plane_state,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010361 struct drm_plane_state *plane_state)
10362{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010363 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010364 struct drm_crtc *crtc = crtc_state->crtc;
10365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010366 struct intel_plane *plane = to_intel_plane(plane_state->plane);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010367 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080010368 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010369 bool mode_changed = needs_modeset(crtc_state);
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010370 bool was_crtc_enabled = old_crtc_state->base.active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010371 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010372 bool turn_off, turn_on, visible, was_visible;
10373 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030010374 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010375
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010376 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010377 ret = skl_update_scaler_plane(
10378 to_intel_crtc_state(crtc_state),
10379 to_intel_plane_state(plane_state));
10380 if (ret)
10381 return ret;
10382 }
10383
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010384 was_visible = old_plane_state->base.visible;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010385 visible = plane_state->visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010386
10387 if (!was_crtc_enabled && WARN_ON(was_visible))
10388 was_visible = false;
10389
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010390 /*
10391 * Visibility is calculated as if the crtc was on, but
10392 * after scaler setup everything depends on it being off
10393 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030010394 *
10395 * FIXME this is wrong for watermarks. Watermarks should also
10396 * be computed as if the pipe would be active. Perhaps move
10397 * per-plane wm computation to the .check_plane() hook, and
10398 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010399 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010400 if (!is_crtc_enabled) {
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010401 plane_state->visible = visible = false;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010402 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10403 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010404
10405 if (!was_visible && !visible)
10406 return 0;
10407
Maarten Lankhorste8861672016-02-24 11:24:26 +010010408 if (fb != old_plane_state->base.fb)
10409 pipe_config->fb_changed = true;
10410
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010411 turn_off = was_visible && (!visible || mode_changed);
10412 turn_on = visible && (!was_visible || mode_changed);
10413
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010414 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010415 intel_crtc->base.base.id, intel_crtc->base.name,
10416 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010417 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010418
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010419 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010420 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010421 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010422 turn_off, turn_on, mode_changed);
10423
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010424 if (turn_on) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010425 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010426 pipe_config->update_wm_pre = true;
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010427
10428 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010429 if (plane->id != PLANE_CURSOR)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010430 pipe_config->disable_cxsr = true;
10431 } else if (turn_off) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010432 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010433 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010434
Ville Syrjälä852eb002015-06-24 22:00:07 +030010435 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010436 if (plane->id != PLANE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010437 pipe_config->disable_cxsr = true;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010438 } else if (intel_wm_need_update(&plane->base, plane_state)) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010439 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010440 /* FIXME bollocks */
10441 pipe_config->update_wm_pre = true;
10442 pipe_config->update_wm_post = true;
10443 }
Ville Syrjälä852eb002015-06-24 22:00:07 +030010444 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010445
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070010446 if (visible || was_visible)
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010447 pipe_config->fb_bits |= plane->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010448
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010449 /*
10450 * WaCxSRDisabledForSpriteScaling:ivb
10451 *
10452 * cstate->update_wm was already set above, so this flag will
10453 * take effect when we commit and program watermarks.
10454 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010455 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010456 needs_scaling(to_intel_plane_state(plane_state)) &&
10457 !needs_scaling(old_plane_state))
10458 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010459
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010460 return 0;
10461}
10462
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010463static bool encoders_cloneable(const struct intel_encoder *a,
10464 const struct intel_encoder *b)
10465{
10466 /* masks could be asymmetric, so check both ways */
10467 return a == b || (a->cloneable & (1 << b->type) &&
10468 b->cloneable & (1 << a->type));
10469}
10470
10471static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10472 struct intel_crtc *crtc,
10473 struct intel_encoder *encoder)
10474{
10475 struct intel_encoder *source_encoder;
10476 struct drm_connector *connector;
10477 struct drm_connector_state *connector_state;
10478 int i;
10479
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010480 for_each_new_connector_in_state(state, connector, connector_state, i) {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010481 if (connector_state->crtc != &crtc->base)
10482 continue;
10483
10484 source_encoder =
10485 to_intel_encoder(connector_state->best_encoder);
10486 if (!encoders_cloneable(encoder, source_encoder))
10487 return false;
10488 }
10489
10490 return true;
10491}
10492
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010493static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10494 struct drm_crtc_state *crtc_state)
10495{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010496 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010497 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010498 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010499 struct intel_crtc_state *pipe_config =
10500 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010501 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020010502 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010503 bool mode_changed = needs_modeset(crtc_state);
10504
Ville Syrjälä852eb002015-06-24 22:00:07 +030010505 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010506 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020010507
Maarten Lankhorstad421372015-06-15 12:33:42 +020010508 if (mode_changed && crtc_state->enable &&
10509 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010510 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020010511 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10512 pipe_config);
10513 if (ret)
10514 return ret;
10515 }
10516
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010517 if (crtc_state->color_mgmt_changed) {
10518 ret = intel_color_check(crtc, crtc_state);
10519 if (ret)
10520 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010010521
10522 /*
10523 * Changing color management on Intel hardware is
10524 * handled as part of planes update.
10525 */
10526 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010527 }
10528
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010529 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010530 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010010531 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080010532 if (ret) {
10533 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070010534 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080010535 }
10536 }
10537
10538 if (dev_priv->display.compute_intermediate_wm &&
10539 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10540 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10541 return 0;
10542
10543 /*
10544 * Calculate 'intermediate' watermarks that satisfy both the
10545 * old state and the new state. We can program these
10546 * immediately.
10547 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010548 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080010549 intel_crtc,
10550 pipe_config);
10551 if (ret) {
10552 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
10553 return ret;
10554 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070010555 } else if (dev_priv->display.compute_intermediate_wm) {
10556 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10557 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010558 }
10559
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010560 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010561 if (mode_changed)
10562 ret = skl_update_scaler_crtc(pipe_config);
10563
10564 if (!ret)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +053010565 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
10566 pipe_config);
10567 if (!ret)
Ander Conselvan de Oliveira6ebc6922017-02-23 09:15:59 +020010568 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010569 pipe_config);
10570 }
10571
Maarten Lankhorst24f28452017-11-22 19:39:01 +010010572 if (HAS_IPS(dev_priv))
10573 pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
10574
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010575 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010576}
10577
Jani Nikula65b38e02015-04-13 11:26:56 +030010578static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Daniel Vetter5a21b662016-05-24 17:13:53 +020010579 .atomic_begin = intel_begin_crtc_commit,
10580 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010581 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010582};
10583
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010584static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10585{
10586 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010587 struct drm_connector_list_iter conn_iter;
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010588
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010589 drm_connector_list_iter_begin(dev, &conn_iter);
10590 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020010591 if (connector->base.state->crtc)
10592 drm_connector_unreference(&connector->base);
10593
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010594 if (connector->base.encoder) {
10595 connector->base.state->best_encoder =
10596 connector->base.encoder;
10597 connector->base.state->crtc =
10598 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020010599
10600 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010601 } else {
10602 connector->base.state->best_encoder = NULL;
10603 connector->base.state->crtc = NULL;
10604 }
10605 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010606 drm_connector_list_iter_end(&conn_iter);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010607}
10608
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010609static void
Robin Schroereba905b2014-05-18 02:24:50 +020010610connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010611 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010612{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010613 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010614 int bpp = pipe_config->pipe_bpp;
10615
10616 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010617 connector->base.base.id,
10618 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010619
10620 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010621 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010622 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010623 bpp, info->bpc * 3);
10624 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010625 }
10626
Mario Kleiner196f9542016-07-06 12:05:45 +020010627 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010628 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020010629 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10630 bpp);
10631 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010632 }
10633}
10634
10635static int
10636compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010637 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010638{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010639 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010640 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010641 struct drm_connector *connector;
10642 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010643 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010644
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010645 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
10646 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010647 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010648 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020010649 bpp = 12*3;
10650 else
10651 bpp = 8*3;
10652
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010653
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010654 pipe_config->pipe_bpp = bpp;
10655
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010656 state = pipe_config->base.state;
10657
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010658 /* Clamp display bpp to EDID value */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010659 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010660 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010661 continue;
10662
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010663 connected_sink_compute_bpp(to_intel_connector(connector),
10664 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010665 }
10666
10667 return bpp;
10668}
10669
Daniel Vetter644db712013-09-19 14:53:58 +020010670static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10671{
10672 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10673 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010674 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010675 mode->crtc_hdisplay, mode->crtc_hsync_start,
10676 mode->crtc_hsync_end, mode->crtc_htotal,
10677 mode->crtc_vdisplay, mode->crtc_vsync_start,
10678 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10679}
10680
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010681static inline void
10682intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000010683 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010684{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000010685 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10686 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010687 m_n->gmch_m, m_n->gmch_n,
10688 m_n->link_m, m_n->link_n, m_n->tu);
10689}
10690
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010691#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
10692
10693static const char * const output_type_str[] = {
10694 OUTPUT_TYPE(UNUSED),
10695 OUTPUT_TYPE(ANALOG),
10696 OUTPUT_TYPE(DVO),
10697 OUTPUT_TYPE(SDVO),
10698 OUTPUT_TYPE(LVDS),
10699 OUTPUT_TYPE(TVOUT),
10700 OUTPUT_TYPE(HDMI),
10701 OUTPUT_TYPE(DP),
10702 OUTPUT_TYPE(EDP),
10703 OUTPUT_TYPE(DSI),
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030010704 OUTPUT_TYPE(DDI),
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010705 OUTPUT_TYPE(DP_MST),
10706};
10707
10708#undef OUTPUT_TYPE
10709
10710static void snprintf_output_types(char *buf, size_t len,
10711 unsigned int output_types)
10712{
10713 char *str = buf;
10714 int i;
10715
10716 str[0] = '\0';
10717
10718 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
10719 int r;
10720
10721 if ((output_types & BIT(i)) == 0)
10722 continue;
10723
10724 r = snprintf(str, len, "%s%s",
10725 str != buf ? "," : "", output_type_str[i]);
10726 if (r >= len)
10727 break;
10728 str += r;
10729 len -= r;
10730
10731 output_types &= ~BIT(i);
10732 }
10733
10734 WARN_ON_ONCE(output_types != 0);
10735}
10736
Daniel Vetterc0b03412013-05-28 12:05:54 +020010737static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010738 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010739 const char *context)
10740{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010741 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010742 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010743 struct drm_plane *plane;
10744 struct intel_plane *intel_plane;
10745 struct intel_plane_state *state;
10746 struct drm_framebuffer *fb;
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010747 char buf[64];
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010748
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000010749 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
10750 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010751
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010752 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
10753 DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
10754 buf, pipe_config->output_types);
10755
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000010756 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
10757 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020010758 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000010759
10760 if (pipe_config->has_pch_encoder)
10761 intel_dump_m_n_config(pipe_config, "fdi",
10762 pipe_config->fdi_lanes,
10763 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010764
Shashank Sharmab22ca992017-07-24 19:19:32 +053010765 if (pipe_config->ycbcr420)
10766 DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
10767
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010768 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000010769 intel_dump_m_n_config(pipe_config, "dp m_n",
10770 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000010771 if (pipe_config->has_drrs)
10772 intel_dump_m_n_config(pipe_config, "dp m2_n2",
10773 pipe_config->lane_count,
10774 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010775 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010776
Daniel Vetter55072d12014-11-20 16:10:28 +010010777 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000010778 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010010779
Daniel Vetterc0b03412013-05-28 12:05:54 +020010780 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010781 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010782 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010783 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10784 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020010785 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000010786 pipe_config->port_clock,
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020010787 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
10788 pipe_config->pixel_rate);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000010789
10790 if (INTEL_GEN(dev_priv) >= 9)
10791 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
10792 crtc->num_scalers,
10793 pipe_config->scaler_state.scaler_users,
10794 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000010795
10796 if (HAS_GMCH_DISPLAY(dev_priv))
10797 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10798 pipe_config->gmch_pfit.control,
10799 pipe_config->gmch_pfit.pgm_ratios,
10800 pipe_config->gmch_pfit.lvds_border_bits);
10801 else
10802 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
10803 pipe_config->pch_pfit.pos,
10804 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000010805 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000010806
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000010807 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
10808 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010809
Ander Conselvan de Oliveiraf50b79f2016-12-29 17:22:12 +020010810 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010010811
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010812 DRM_DEBUG_KMS("planes on this crtc\n");
10813 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000010814 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010815 intel_plane = to_intel_plane(plane);
10816 if (intel_plane->pipe != crtc->pipe)
10817 continue;
10818
10819 state = to_intel_plane_state(plane->state);
10820 fb = state->base.fb;
10821 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030010822 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
10823 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010824 continue;
10825 }
10826
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000010827 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
10828 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000010829 fb->base.id, fb->width, fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +020010830 drm_get_format_name(fb->format->format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000010831 if (INTEL_GEN(dev_priv) >= 9)
10832 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
10833 state->scaler_id,
10834 state->base.src.x1 >> 16,
10835 state->base.src.y1 >> 16,
10836 drm_rect_width(&state->base.src) >> 16,
10837 drm_rect_height(&state->base.src) >> 16,
10838 state->base.dst.x1, state->base.dst.y1,
10839 drm_rect_width(&state->base.dst),
10840 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010841 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010842}
10843
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010844static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010845{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010846 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010847 struct drm_connector *connector;
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030010848 struct drm_connector_list_iter conn_iter;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010849 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030010850 unsigned int used_mst_ports = 0;
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010010851 bool ret = true;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010852
10853 /*
10854 * Walk the connector list instead of the encoder
10855 * list to detect the problem on ddi platforms
10856 * where there's just one encoder per digital port.
10857 */
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030010858 drm_connector_list_iter_begin(dev, &conn_iter);
10859 drm_for_each_connector_iter(connector, &conn_iter) {
Ville Syrjälä0bff4852015-12-10 18:22:31 +020010860 struct drm_connector_state *connector_state;
10861 struct intel_encoder *encoder;
10862
Maarten Lankhorst8b694492018-04-09 14:46:55 +020010863 connector_state = drm_atomic_get_new_connector_state(state, connector);
Ville Syrjälä0bff4852015-12-10 18:22:31 +020010864 if (!connector_state)
10865 connector_state = connector->state;
10866
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030010867 if (!connector_state->best_encoder)
10868 continue;
10869
10870 encoder = to_intel_encoder(connector_state->best_encoder);
10871
10872 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010873
10874 switch (encoder->type) {
10875 unsigned int port_mask;
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030010876 case INTEL_OUTPUT_DDI:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010877 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010878 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030010879 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010880 case INTEL_OUTPUT_HDMI:
10881 case INTEL_OUTPUT_EDP:
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020010882 port_mask = 1 << encoder->port;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010883
10884 /* the same port mustn't appear more than once */
10885 if (used_ports & port_mask)
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010010886 ret = false;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010887
10888 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030010889 break;
10890 case INTEL_OUTPUT_DP_MST:
10891 used_mst_ports |=
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020010892 1 << encoder->port;
Ville Syrjälä477321e2016-07-28 17:50:40 +030010893 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010894 default:
10895 break;
10896 }
10897 }
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030010898 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010899
Ville Syrjälä477321e2016-07-28 17:50:40 +030010900 /* can't mix MST and SST/HDMI on the same port */
10901 if (used_ports & used_mst_ports)
10902 return false;
10903
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010010904 return ret;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020010905}
10906
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010907static void
10908clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
10909{
Ville Syrjäläff32c542017-03-02 19:14:57 +020010910 struct drm_i915_private *dev_priv =
10911 to_i915(crtc_state->base.crtc->dev);
Chandra Konduru663a3642015-04-07 15:28:41 -070010912 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030010913 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010914 struct intel_shared_dpll *shared_dpll;
Ville Syrjäläff32c542017-03-02 19:14:57 +020010915 struct intel_crtc_wm_state wm_state;
Ville Syrjälä6e644622017-08-17 17:55:09 +030010916 bool force_thru, ips_force_disable;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010917
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030010918 /* FIXME: before the switch to atomic started, a new pipe_config was
10919 * kzalloc'd. Code that depends on any field being zero should be
10920 * fixed, so that the crtc_state can be safely duplicated. For now,
10921 * only fields that are know to not cause problems are preserved. */
10922
Chandra Konduru663a3642015-04-07 15:28:41 -070010923 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030010924 shared_dpll = crtc_state->shared_dpll;
10925 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020010926 force_thru = crtc_state->pch_pfit.force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030010927 ips_force_disable = crtc_state->ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010928 if (IS_G4X(dev_priv) ||
10929 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020010930 wm_state = crtc_state->wm;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030010931
Chris Wilsond2fa80a2017-03-03 15:46:44 +000010932 /* Keep base drm_crtc_state intact, only clear our extended struct */
10933 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
10934 memset(&crtc_state->base + 1, 0,
10935 sizeof(*crtc_state) - sizeof(crtc_state->base));
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030010936
Chandra Konduru663a3642015-04-07 15:28:41 -070010937 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030010938 crtc_state->shared_dpll = shared_dpll;
10939 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020010940 crtc_state->pch_pfit.force_thru = force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030010941 crtc_state->ips_force_disable = ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010942 if (IS_G4X(dev_priv) ||
10943 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020010944 crtc_state->wm = wm_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010945}
10946
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030010947static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010948intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020010949 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020010950{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020010951 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020010952 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010953 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010954 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020010955 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020010956 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010957 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010958
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010959 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020010960
Daniel Vettere143a212013-07-04 12:01:15 +020010961 pipe_config->cpu_transcoder =
10962 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010963
Imre Deak2960bc92013-07-30 13:36:32 +030010964 /*
10965 * Sanitize sync polarity flags based on requested ones. If neither
10966 * positive or negative polarity is requested, treat this as meaning
10967 * negative polarity.
10968 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010969 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010970 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010971 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010972
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010973 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030010974 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010975 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030010976
Daniel Vetterd328c9d2015-04-10 16:22:37 +020010977 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10978 pipe_config);
10979 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010980 goto fail;
10981
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010982 /*
10983 * Determine the real pipe dimensions. Note that stereo modes can
10984 * increase the actual pipe size due to the frame doubling and
10985 * insertion of additional space for blanks between the frame. This
10986 * is stored in the crtc timings. We use the requested mode to do this
10987 * computation to clearly distinguish it from the adjusted mode, which
10988 * can be changed by the connectors in the below retry loop.
10989 */
Daniel Vetter196cd5d2017-01-25 07:26:56 +010010990 drm_mode_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080010991 &pipe_config->pipe_src_w,
10992 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010993
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010994 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ville Syrjälä253c84c2016-06-22 21:57:01 +030010995 if (connector_state->crtc != crtc)
10996 continue;
10997
10998 encoder = to_intel_encoder(connector_state->best_encoder);
10999
Ville Syrjäläe25148d2016-06-22 21:57:09 +030011000 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11001 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11002 goto fail;
11003 }
11004
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011005 /*
11006 * Determine output_types before calling the .compute_config()
11007 * hooks so that the hooks can use this information safely.
11008 */
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011009 if (encoder->compute_output_type)
11010 pipe_config->output_types |=
11011 BIT(encoder->compute_output_type(encoder, pipe_config,
11012 connector_state));
11013 else
11014 pipe_config->output_types |= BIT(encoder->type);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011015 }
11016
Daniel Vettere29c22c2013-02-21 00:00:16 +010011017encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011018 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011019 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011020 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011021
Daniel Vetter135c81b2013-07-21 21:37:09 +020011022 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011023 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11024 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011025
Daniel Vetter7758a112012-07-08 19:40:39 +020011026 /* Pass our mode to the connectors and the CRTC to give them a chance to
11027 * adjust it according to limitations or connector properties, and also
11028 * a chance to reject the mode entirely.
11029 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011030 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011031 if (connector_state->crtc != crtc)
11032 continue;
11033
11034 encoder = to_intel_encoder(connector_state->best_encoder);
11035
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020011036 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020011037 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011038 goto fail;
11039 }
11040 }
11041
Daniel Vetterff9a6752013-06-01 17:16:21 +020011042 /* Set default port clock if not overwritten by the encoder. Needs to be
11043 * done afterwards in case the encoder adjusts the mode. */
11044 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011045 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011046 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011047
Daniel Vettera43f6e02013-06-07 23:10:32 +020011048 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011049 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011050 DRM_DEBUG_KMS("CRTC fixup failed\n");
11051 goto fail;
11052 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011053
11054 if (ret == RETRY) {
11055 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11056 ret = -EINVAL;
11057 goto fail;
11058 }
11059
11060 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11061 retry = false;
11062 goto encoder_retry;
11063 }
11064
Daniel Vettere8fa4272015-08-12 11:43:34 +020011065 /* Dithering seems to not pass-through bits correctly when it should, so
Manasi Navare611032b2017-01-24 08:21:49 -080011066 * only enable it on 6bpc panels and when its not a compliance
11067 * test requesting 6bpc video pattern.
11068 */
11069 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11070 !pipe_config->dither_force_disable;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020011071 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011072 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011073
Daniel Vetter7758a112012-07-08 19:40:39 +020011074fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011075 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011076}
11077
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011078static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011079{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011080 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011081
11082 if (clock1 == clock2)
11083 return true;
11084
11085 if (!clock1 || !clock2)
11086 return false;
11087
11088 diff = abs(clock1 - clock2);
11089
11090 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11091 return true;
11092
11093 return false;
11094}
11095
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011096static bool
11097intel_compare_m_n(unsigned int m, unsigned int n,
11098 unsigned int m2, unsigned int n2,
11099 bool exact)
11100{
11101 if (m == m2 && n == n2)
11102 return true;
11103
11104 if (exact || !m || !n || !m2 || !n2)
11105 return false;
11106
11107 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11108
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011109 if (n > n2) {
11110 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011111 m2 <<= 1;
11112 n2 <<= 1;
11113 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011114 } else if (n < n2) {
11115 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011116 m <<= 1;
11117 n <<= 1;
11118 }
11119 }
11120
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011121 if (n != n2)
11122 return false;
11123
11124 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011125}
11126
11127static bool
11128intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11129 struct intel_link_m_n *m2_n2,
11130 bool adjust)
11131{
11132 if (m_n->tu == m2_n2->tu &&
11133 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11134 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11135 intel_compare_m_n(m_n->link_m, m_n->link_n,
11136 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11137 if (adjust)
11138 *m2_n2 = *m_n;
11139
11140 return true;
11141 }
11142
11143 return false;
11144}
11145
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011146static void __printf(3, 4)
11147pipe_config_err(bool adjust, const char *name, const char *format, ...)
11148{
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011149 struct va_format vaf;
11150 va_list args;
11151
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011152 va_start(args, format);
11153 vaf.fmt = format;
11154 vaf.va = &args;
11155
Joe Perches99a95482018-03-13 15:02:15 -070011156 if (adjust)
11157 drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
11158 else
11159 drm_err("mismatch in %s %pV", name, &vaf);
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011160
11161 va_end(args);
11162}
11163
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011164static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011165intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011166 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011167 struct intel_crtc_state *pipe_config,
11168 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011169{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011170 bool ret = true;
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011171 bool fixup_inherited = adjust &&
11172 (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
11173 !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011174
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011175#define PIPE_CONF_CHECK_X(name) do { \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011176 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011177 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011178 "(expected 0x%08x, found 0x%08x)\n", \
11179 current_config->name, \
11180 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011181 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011182 } \
11183} while (0)
Daniel Vetter66e985c2013-06-05 13:34:20 +020011184
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011185#define PIPE_CONF_CHECK_I(name) do { \
Daniel Vetter08a24032013-04-19 11:25:34 +020011186 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011187 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter08a24032013-04-19 11:25:34 +020011188 "(expected %i, found %i)\n", \
11189 current_config->name, \
11190 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011191 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011192 } \
11193} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011194
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011195#define PIPE_CONF_CHECK_BOOL(name) do { \
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011196 if (current_config->name != pipe_config->name) { \
11197 pipe_config_err(adjust, __stringify(name), \
11198 "(expected %s, found %s)\n", \
11199 yesno(current_config->name), \
11200 yesno(pipe_config->name)); \
11201 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011202 } \
11203} while (0)
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011204
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011205/*
11206 * Checks state where we only read out the enabling, but not the entire
11207 * state itself (like full infoframes or ELD for audio). These states
11208 * require a full modeset on bootup to fix up.
11209 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011210#define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011211 if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
11212 PIPE_CONF_CHECK_BOOL(name); \
11213 } else { \
11214 pipe_config_err(adjust, __stringify(name), \
11215 "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
11216 yesno(current_config->name), \
11217 yesno(pipe_config->name)); \
11218 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011219 } \
11220} while (0)
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011221
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011222#define PIPE_CONF_CHECK_P(name) do { \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011223 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011224 pipe_config_err(adjust, __stringify(name), \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011225 "(expected %p, found %p)\n", \
11226 current_config->name, \
11227 pipe_config->name); \
11228 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011229 } \
11230} while (0)
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011231
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011232#define PIPE_CONF_CHECK_M_N(name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011233 if (!intel_compare_link_m_n(&current_config->name, \
11234 &pipe_config->name,\
11235 adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011236 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011237 "(expected tu %i gmch %i/%i link %i/%i, " \
11238 "found tu %i, gmch %i/%i link %i/%i)\n", \
11239 current_config->name.tu, \
11240 current_config->name.gmch_m, \
11241 current_config->name.gmch_n, \
11242 current_config->name.link_m, \
11243 current_config->name.link_n, \
11244 pipe_config->name.tu, \
11245 pipe_config->name.gmch_m, \
11246 pipe_config->name.gmch_n, \
11247 pipe_config->name.link_m, \
11248 pipe_config->name.link_n); \
11249 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011250 } \
11251} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011252
Daniel Vetter55c561a2016-03-30 11:34:36 +020011253/* This is required for BDW+ where there is only one set of registers for
11254 * switching between high and low RR.
11255 * This macro can be used whenever a comparison has to be made between one
11256 * hw state and multiple sw state variables.
11257 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011258#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011259 if (!intel_compare_link_m_n(&current_config->name, \
11260 &pipe_config->name, adjust) && \
11261 !intel_compare_link_m_n(&current_config->alt_name, \
11262 &pipe_config->name, adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011263 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011264 "(expected tu %i gmch %i/%i link %i/%i, " \
11265 "or tu %i gmch %i/%i link %i/%i, " \
11266 "found tu %i, gmch %i/%i link %i/%i)\n", \
11267 current_config->name.tu, \
11268 current_config->name.gmch_m, \
11269 current_config->name.gmch_n, \
11270 current_config->name.link_m, \
11271 current_config->name.link_n, \
11272 current_config->alt_name.tu, \
11273 current_config->alt_name.gmch_m, \
11274 current_config->alt_name.gmch_n, \
11275 current_config->alt_name.link_m, \
11276 current_config->alt_name.link_n, \
11277 pipe_config->name.tu, \
11278 pipe_config->name.gmch_m, \
11279 pipe_config->name.gmch_n, \
11280 pipe_config->name.link_m, \
11281 pipe_config->name.link_n); \
11282 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011283 } \
11284} while (0)
Daniel Vetter88adfff2013-03-28 10:42:01 +010011285
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011286#define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011287 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011288 pipe_config_err(adjust, __stringify(name), \
11289 "(%x) (expected %i, found %i)\n", \
11290 (mask), \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011291 current_config->name & (mask), \
11292 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011293 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011294 } \
11295} while (0)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011296
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011297#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011298 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011299 pipe_config_err(adjust, __stringify(name), \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011300 "(expected %i, found %i)\n", \
11301 current_config->name, \
11302 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011303 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011304 } \
11305} while (0)
Ville Syrjälä5e550652013-09-06 23:29:07 +030011306
Daniel Vetterbb760062013-06-06 14:55:52 +020011307#define PIPE_CONF_QUIRK(quirk) \
11308 ((current_config->quirks | pipe_config->quirks) & (quirk))
11309
Daniel Vettereccb1402013-05-22 00:50:22 +020011310 PIPE_CONF_CHECK_I(cpu_transcoder);
11311
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011312 PIPE_CONF_CHECK_BOOL(has_pch_encoder);
Daniel Vetter08a24032013-04-19 11:25:34 +020011313 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011314 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020011315
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011316 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030011317 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011318
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011319 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011320 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011321
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011322 if (current_config->has_drrs)
11323 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11324 } else
11325 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011326
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011327 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020011328
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011329 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11330 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11331 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11332 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11333 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11334 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011335
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011336 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11337 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11338 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11339 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11340 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11341 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011342
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011343 PIPE_CONF_CHECK_I(pixel_multiplier);
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011344 PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011345 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010011346 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011347 PIPE_CONF_CHECK_BOOL(limited_color_range);
Shashank Sharma15953632017-03-13 16:54:03 +053011348
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011349 PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
11350 PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011351 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011352 PIPE_CONF_CHECK_BOOL(ycbcr420);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011353
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011354 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011355
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011356 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011357 DRM_MODE_FLAG_INTERLACE);
11358
Daniel Vetterbb760062013-06-06 14:55:52 +020011359 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011360 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011361 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011362 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011363 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011364 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011365 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011366 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011367 DRM_MODE_FLAG_NVSYNC);
11368 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011369
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011370 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020011371 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011372 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020011373 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011374 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020011375
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011376 if (!adjust) {
11377 PIPE_CONF_CHECK_I(pipe_src_w);
11378 PIPE_CONF_CHECK_I(pipe_src_h);
11379
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011380 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011381 if (current_config->pch_pfit.enabled) {
11382 PIPE_CONF_CHECK_X(pch_pfit.pos);
11383 PIPE_CONF_CHECK_X(pch_pfit.size);
11384 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011385
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011386 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011387 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011388 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070011389
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011390 PIPE_CONF_CHECK_BOOL(double_wide);
Ville Syrjälä282740f2013-09-04 18:30:03 +030011391
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011392 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011393 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011394 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011395 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11396 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011397 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010011398 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011399 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11400 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11401 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Paulo Zanoni2de38132017-09-22 17:53:42 -030011402 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
11403 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
11404 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
11405 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
11406 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
11407 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
11408 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
11409 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
11410 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
11411 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
11412 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
11413 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
Paulo Zanonic27e9172018-04-27 16:14:36 -070011414 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
11415 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
11416 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
11417 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
11418 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
11419 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
11420 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
11421 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
11422 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
11423 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011424
Ville Syrjälä47eacba2016-04-12 22:14:35 +030011425 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11426 PIPE_CONF_CHECK_X(dsi_pll.div);
11427
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011428 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011429 PIPE_CONF_CHECK_I(pipe_bpp);
11430
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011431 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011432 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011433
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030011434 PIPE_CONF_CHECK_I(min_voltage_level);
11435
Daniel Vetter66e985c2013-06-05 13:34:20 +020011436#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011437#undef PIPE_CONF_CHECK_I
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011438#undef PIPE_CONF_CHECK_BOOL
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011439#undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011440#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011441#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011442#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011443#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011444
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011445 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011446}
11447
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011448static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11449 const struct intel_crtc_state *pipe_config)
11450{
11451 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011452 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011453 &pipe_config->fdi_m_n);
11454 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11455
11456 /*
11457 * FDI already provided one idea for the dotclock.
11458 * Yell if the encoder disagrees.
11459 */
11460 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11461 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11462 fdi_dotclock, dotclock);
11463 }
11464}
11465
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011466static void verify_wm_state(struct drm_crtc *crtc,
11467 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000011468{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011469 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000011470 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011471 struct skl_pipe_wm hw_wm, *sw_wm;
11472 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11473 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11475 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011476 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000011477
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011478 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000011479 return;
11480
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011481 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020011482 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011483
Damien Lespiau08db6652014-11-04 17:06:52 +000011484 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11485 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11486
Mahesh Kumar74bd8002018-04-26 19:55:15 +053011487 if (INTEL_GEN(dev_priv) >= 11)
11488 if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
11489 DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
11490 sw_ddb->enabled_slices,
11491 hw_ddb.enabled_slices);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011492 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070011493 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011494 hw_plane_wm = &hw_wm.planes[plane];
11495 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000011496
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011497 /* Watermarks */
11498 for (level = 0; level <= max_level; level++) {
11499 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11500 &sw_plane_wm->wm[level]))
11501 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000011502
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011503 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",
11504 pipe_name(pipe), plane + 1, level,
11505 sw_plane_wm->wm[level].plane_en,
11506 sw_plane_wm->wm[level].plane_res_b,
11507 sw_plane_wm->wm[level].plane_res_l,
11508 hw_plane_wm->wm[level].plane_en,
11509 hw_plane_wm->wm[level].plane_res_b,
11510 hw_plane_wm->wm[level].plane_res_l);
11511 }
11512
11513 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11514 &sw_plane_wm->trans_wm)) {
11515 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",
11516 pipe_name(pipe), plane + 1,
11517 sw_plane_wm->trans_wm.plane_en,
11518 sw_plane_wm->trans_wm.plane_res_b,
11519 sw_plane_wm->trans_wm.plane_res_l,
11520 hw_plane_wm->trans_wm.plane_en,
11521 hw_plane_wm->trans_wm.plane_res_b,
11522 hw_plane_wm->trans_wm.plane_res_l);
11523 }
11524
11525 /* DDB */
11526 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11527 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11528
11529 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011530 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 -040011531 pipe_name(pipe), plane + 1,
11532 sw_ddb_entry->start, sw_ddb_entry->end,
11533 hw_ddb_entry->start, hw_ddb_entry->end);
11534 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011535 }
11536
Lyude27082492016-08-24 07:48:10 +020011537 /*
11538 * cursor
11539 * If the cursor plane isn't active, we may not have updated it's ddb
11540 * allocation. In that case since the ddb allocation will be updated
11541 * once the plane becomes visible, we can skip this check
11542 */
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030011543 if (1) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011544 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11545 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011546
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011547 /* Watermarks */
11548 for (level = 0; level <= max_level; level++) {
11549 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11550 &sw_plane_wm->wm[level]))
11551 continue;
11552
11553 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",
11554 pipe_name(pipe), level,
11555 sw_plane_wm->wm[level].plane_en,
11556 sw_plane_wm->wm[level].plane_res_b,
11557 sw_plane_wm->wm[level].plane_res_l,
11558 hw_plane_wm->wm[level].plane_en,
11559 hw_plane_wm->wm[level].plane_res_b,
11560 hw_plane_wm->wm[level].plane_res_l);
11561 }
11562
11563 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11564 &sw_plane_wm->trans_wm)) {
11565 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11566 pipe_name(pipe),
11567 sw_plane_wm->trans_wm.plane_en,
11568 sw_plane_wm->trans_wm.plane_res_b,
11569 sw_plane_wm->trans_wm.plane_res_l,
11570 hw_plane_wm->trans_wm.plane_en,
11571 hw_plane_wm->trans_wm.plane_res_b,
11572 hw_plane_wm->trans_wm.plane_res_l);
11573 }
11574
11575 /* DDB */
11576 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11577 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
11578
11579 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011580 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020011581 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011582 sw_ddb_entry->start, sw_ddb_entry->end,
11583 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020011584 }
Damien Lespiau08db6652014-11-04 17:06:52 +000011585 }
11586}
11587
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011588static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011589verify_connector_state(struct drm_device *dev,
11590 struct drm_atomic_state *state,
11591 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011592{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011593 struct drm_connector *connector;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011594 struct drm_connector_state *new_conn_state;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011595 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011596
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011597 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011598 struct drm_encoder *encoder = connector->encoder;
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011599 struct drm_crtc_state *crtc_state = NULL;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011600
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011601 if (new_conn_state->crtc != crtc)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011602 continue;
11603
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011604 if (crtc)
11605 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
11606
11607 intel_connector_verify_state(crtc_state, new_conn_state);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011608
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011609 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011610 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011611 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011612}
11613
11614static void
Daniel Vetter86b04262017-03-01 10:52:26 +010011615verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011616{
11617 struct intel_encoder *encoder;
Daniel Vetter86b04262017-03-01 10:52:26 +010011618 struct drm_connector *connector;
11619 struct drm_connector_state *old_conn_state, *new_conn_state;
11620 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011621
Damien Lespiaub2784e12014-08-05 11:29:37 +010011622 for_each_intel_encoder(dev, encoder) {
Daniel Vetter86b04262017-03-01 10:52:26 +010011623 bool enabled = false, found = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011624 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011625
11626 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11627 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011628 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011629
Daniel Vetter86b04262017-03-01 10:52:26 +010011630 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
11631 new_conn_state, i) {
11632 if (old_conn_state->best_encoder == &encoder->base)
11633 found = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011634
Daniel Vetter86b04262017-03-01 10:52:26 +010011635 if (new_conn_state->best_encoder != &encoder->base)
11636 continue;
11637 found = enabled = true;
11638
11639 I915_STATE_WARN(new_conn_state->crtc !=
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011640 encoder->base.crtc,
11641 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011642 }
Daniel Vetter86b04262017-03-01 10:52:26 +010011643
11644 if (!found)
11645 continue;
Dave Airlie0e32b392014-05-02 14:02:48 +100011646
Rob Clarke2c719b2014-12-15 13:56:32 -050011647 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011648 "encoder's enabled state mismatch "
11649 "(expected %i, found %i)\n",
11650 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011651
11652 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011653 bool active;
11654
11655 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011656 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011657 "encoder detached but still enabled on pipe %c.\n",
11658 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011659 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011660 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011661}
11662
11663static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011664verify_crtc_state(struct drm_crtc *crtc,
11665 struct drm_crtc_state *old_crtc_state,
11666 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011667{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011668 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011669 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011670 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11672 struct intel_crtc_state *pipe_config, *sw_config;
11673 struct drm_atomic_state *old_state;
11674 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011675
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011676 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020011677 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011678 pipe_config = to_intel_crtc_state(old_crtc_state);
11679 memset(pipe_config, 0, sizeof(*pipe_config));
11680 pipe_config->base.crtc = crtc;
11681 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011682
Ville Syrjälä78108b72016-05-27 20:59:19 +030011683 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011684
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011685 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011686
Ville Syrjäläe56134b2017-06-01 17:36:19 +030011687 /* we keep both pipes enabled on 830 */
11688 if (IS_I830(dev_priv))
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011689 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011690
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011691 I915_STATE_WARN(new_crtc_state->active != active,
11692 "crtc active state doesn't match with hw state "
11693 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011694
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011695 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
11696 "transitional active state does not match atomic hw state "
11697 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011698
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011699 for_each_encoder_on_crtc(dev, crtc, encoder) {
11700 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011701
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011702 active = encoder->get_hw_state(encoder, &pipe);
11703 I915_STATE_WARN(active != new_crtc_state->active,
11704 "[ENCODER:%i] active %i with crtc active %i\n",
11705 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011706
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011707 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
11708 "Encoder connected to wrong pipe %c\n",
11709 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011710
Ville Syrjäläe1214b92017-10-27 22:31:23 +030011711 if (active)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011712 encoder->get_config(encoder, pipe_config);
11713 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011714
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011715 intel_crtc_compute_pixel_rate(pipe_config);
11716
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011717 if (!new_crtc_state->active)
11718 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011719
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011720 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011721
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011722 sw_config = to_intel_crtc_state(new_crtc_state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011723 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011724 pipe_config, false)) {
11725 I915_STATE_WARN(1, "pipe state doesn't match!\n");
11726 intel_dump_pipe_config(intel_crtc, pipe_config,
11727 "[hw state]");
11728 intel_dump_pipe_config(intel_crtc, sw_config,
11729 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011730 }
11731}
11732
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011733static void
Ville Syrjäläcff109f2017-11-17 21:19:17 +020011734intel_verify_planes(struct intel_atomic_state *state)
11735{
11736 struct intel_plane *plane;
11737 const struct intel_plane_state *plane_state;
11738 int i;
11739
11740 for_each_new_intel_plane_in_state(state, plane,
11741 plane_state, i)
11742 assert_plane(plane, plane_state->base.visible);
11743}
11744
11745static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011746verify_single_dpll_state(struct drm_i915_private *dev_priv,
11747 struct intel_shared_dpll *pll,
11748 struct drm_crtc *crtc,
11749 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011750{
11751 struct intel_dpll_hw_state dpll_hw_state;
11752 unsigned crtc_mask;
11753 bool active;
11754
11755 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11756
Lucas De Marchi72f775f2018-03-20 15:06:34 -070011757 DRM_DEBUG_KMS("%s\n", pll->info->name);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011758
Lucas De Marchiee1398b2018-03-20 15:06:33 -070011759 active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011760
Lucas De Marchi5cd281f2018-03-20 15:06:36 -070011761 if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011762 I915_STATE_WARN(!pll->on && pll->active_mask,
11763 "pll in active use but not on in sw tracking\n");
11764 I915_STATE_WARN(pll->on && !pll->active_mask,
11765 "pll is on but not used by any active crtc\n");
11766 I915_STATE_WARN(pll->on != active,
11767 "pll on state mismatch (expected %i, found %i)\n",
11768 pll->on, active);
11769 }
11770
11771 if (!crtc) {
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020011772 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011773 "more active pll users than references: %x vs %x\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020011774 pll->active_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011775
11776 return;
11777 }
11778
11779 crtc_mask = 1 << drm_crtc_index(crtc);
11780
11781 if (new_state->active)
11782 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
11783 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
11784 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
11785 else
11786 I915_STATE_WARN(pll->active_mask & crtc_mask,
11787 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
11788 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
11789
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020011790 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011791 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020011792 crtc_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011793
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020011794 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011795 &dpll_hw_state,
11796 sizeof(dpll_hw_state)),
11797 "pll hw state mismatch\n");
11798}
11799
11800static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011801verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
11802 struct drm_crtc_state *old_crtc_state,
11803 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011804{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011805 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011806 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
11807 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
11808
11809 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011810 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011811
11812 if (old_state->shared_dpll &&
11813 old_state->shared_dpll != new_state->shared_dpll) {
11814 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
11815 struct intel_shared_dpll *pll = old_state->shared_dpll;
11816
11817 I915_STATE_WARN(pll->active_mask & crtc_mask,
11818 "pll active mismatch (didn't expect pipe %c in active mask)\n",
11819 pipe_name(drm_crtc_index(crtc)));
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020011820 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011821 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
11822 pipe_name(drm_crtc_index(crtc)));
11823 }
11824}
11825
11826static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011827intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011828 struct drm_atomic_state *state,
11829 struct drm_crtc_state *old_state,
11830 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011831{
Daniel Vetter5a21b662016-05-24 17:13:53 +020011832 if (!needs_modeset(new_state) &&
11833 !to_intel_crtc_state(new_state)->update_pipe)
11834 return;
11835
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011836 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011837 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011838 verify_crtc_state(crtc, old_state, new_state);
11839 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011840}
11841
11842static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011843verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011844{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011845 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011846 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020011847
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011848 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011849 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011850}
Daniel Vetter53589012013-06-05 13:34:16 +020011851
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011852static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011853intel_modeset_verify_disabled(struct drm_device *dev,
11854 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011855{
Daniel Vetter86b04262017-03-01 10:52:26 +010011856 verify_encoder_state(dev, state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011857 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011858 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020011859}
11860
Ville Syrjälä80715b22014-05-15 20:23:23 +030011861static void update_scanline_offset(struct intel_crtc *crtc)
11862{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011863 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011864
11865 /*
11866 * The scanline counter increments at the leading edge of hsync.
11867 *
11868 * On most platforms it starts counting from vtotal-1 on the
11869 * first active line. That means the scanline counter value is
11870 * always one less than what we would expect. Ie. just after
11871 * start of vblank, which also occurs at start of hsync (on the
11872 * last active line), the scanline counter will read vblank_start-1.
11873 *
11874 * On gen2 the scanline counter starts counting from 1 instead
11875 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11876 * to keep the value positive), instead of adding one.
11877 *
11878 * On HSW+ the behaviour of the scanline counter depends on the output
11879 * type. For DP ports it behaves like most other platforms, but on HDMI
11880 * there's an extra 1 line difference. So we need to add two instead of
11881 * one to the value.
Ville Syrjäläec1b4ee2016-12-15 19:47:34 +020011882 *
11883 * On VLV/CHV DSI the scanline counter would appear to increment
11884 * approx. 1/3 of a scanline before start of vblank. Unfortunately
11885 * that means we can't tell whether we're in vblank or not while
11886 * we're on that particular line. We must still set scanline_offset
11887 * to 1 so that the vblank timestamps come out correct when we query
11888 * the scanline counter from within the vblank interrupt handler.
11889 * However if queried just before the start of vblank we'll get an
11890 * answer that's slightly in the future.
Ville Syrjälä80715b22014-05-15 20:23:23 +030011891 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011892 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030011893 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011894 int vtotal;
11895
Ville Syrjälä124abe02015-09-08 13:40:45 +030011896 vtotal = adjusted_mode->crtc_vtotal;
11897 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030011898 vtotal /= 2;
11899
11900 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011901 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030011902 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030011903 crtc->scanline_offset = 2;
11904 } else
11905 crtc->scanline_offset = 1;
11906}
11907
Maarten Lankhorstad421372015-06-15 12:33:42 +020011908static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011909{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011910 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011911 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011912 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011913 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011914 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011915
11916 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020011917 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011918
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011919 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010011920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011921 struct intel_shared_dpll *old_dpll =
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011922 to_intel_crtc_state(old_crtc_state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011923
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011924 if (!needs_modeset(new_crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030011925 continue;
11926
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011927 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010011928
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011929 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010011930 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011931
Ander Conselvan de Oliveiraa1c414e2016-12-29 17:22:07 +020011932 intel_release_shared_dpll(old_dpll, intel_crtc, state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011933 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020011934}
11935
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020011936/*
11937 * This implements the workaround described in the "notes" section of the mode
11938 * set sequence documentation. When going from no pipes or single pipe to
11939 * multiple pipes, and planes are enabled after the pipe, we need to wait at
11940 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
11941 */
11942static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
11943{
11944 struct drm_crtc_state *crtc_state;
11945 struct intel_crtc *intel_crtc;
11946 struct drm_crtc *crtc;
11947 struct intel_crtc_state *first_crtc_state = NULL;
11948 struct intel_crtc_state *other_crtc_state = NULL;
11949 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
11950 int i;
11951
11952 /* look at all crtc's that are going to be enabled in during modeset */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011953 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020011954 intel_crtc = to_intel_crtc(crtc);
11955
11956 if (!crtc_state->active || !needs_modeset(crtc_state))
11957 continue;
11958
11959 if (first_crtc_state) {
11960 other_crtc_state = to_intel_crtc_state(crtc_state);
11961 break;
11962 } else {
11963 first_crtc_state = to_intel_crtc_state(crtc_state);
11964 first_pipe = intel_crtc->pipe;
11965 }
11966 }
11967
11968 /* No workaround needed? */
11969 if (!first_crtc_state)
11970 return 0;
11971
11972 /* w/a possibly needed, check how many crtc's are already enabled. */
11973 for_each_intel_crtc(state->dev, intel_crtc) {
11974 struct intel_crtc_state *pipe_config;
11975
11976 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11977 if (IS_ERR(pipe_config))
11978 return PTR_ERR(pipe_config);
11979
11980 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
11981
11982 if (!pipe_config->base.active ||
11983 needs_modeset(&pipe_config->base))
11984 continue;
11985
11986 /* 2 or more enabled crtcs means no need for w/a */
11987 if (enabled_pipe != INVALID_PIPE)
11988 return 0;
11989
11990 enabled_pipe = intel_crtc->pipe;
11991 }
11992
11993 if (enabled_pipe != INVALID_PIPE)
11994 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
11995 else if (other_crtc_state)
11996 other_crtc_state->hsw_workaround_pipe = first_pipe;
11997
11998 return 0;
11999}
12000
Ville Syrjälä8d965612016-11-14 18:35:10 +020012001static int intel_lock_all_pipes(struct drm_atomic_state *state)
12002{
12003 struct drm_crtc *crtc;
12004
12005 /* Add all pipes to the state */
12006 for_each_crtc(state->dev, crtc) {
12007 struct drm_crtc_state *crtc_state;
12008
12009 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12010 if (IS_ERR(crtc_state))
12011 return PTR_ERR(crtc_state);
12012 }
12013
12014 return 0;
12015}
12016
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012017static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12018{
12019 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012020
Ville Syrjälä8d965612016-11-14 18:35:10 +020012021 /*
12022 * Add all pipes to the state, and force
12023 * a modeset on all the active ones.
12024 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012025 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012026 struct drm_crtc_state *crtc_state;
12027 int ret;
12028
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012029 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12030 if (IS_ERR(crtc_state))
12031 return PTR_ERR(crtc_state);
12032
12033 if (!crtc_state->active || needs_modeset(crtc_state))
12034 continue;
12035
12036 crtc_state->mode_changed = true;
12037
12038 ret = drm_atomic_add_affected_connectors(state, crtc);
12039 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012040 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012041
12042 ret = drm_atomic_add_affected_planes(state, crtc);
12043 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012044 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012045 }
12046
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012047 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012048}
12049
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012050static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012051{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012052 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012053 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012054 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012055 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012056 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012057
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012058 if (!check_digital_port_conflicts(state)) {
12059 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12060 return -EINVAL;
12061 }
12062
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012063 intel_state->modeset = true;
12064 intel_state->active_crtcs = dev_priv->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012065 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12066 intel_state->cdclk.actual = dev_priv->cdclk.actual;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012067
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012068 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12069 if (new_crtc_state->active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012070 intel_state->active_crtcs |= 1 << i;
12071 else
12072 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070012073
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012074 if (old_crtc_state->active != new_crtc_state->active)
Matt Roper8b4a7d02016-05-12 07:06:00 -070012075 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012076 }
12077
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012078 /*
12079 * See if the config requires any additional preparation, e.g.
12080 * to adjust global state with pipes off. We need to do this
12081 * here so we can get the modeset_pipe updated config for the new
12082 * mode set on this crtc. For other crtcs we need to use the
12083 * adjusted_mode bits in the crtc directly.
12084 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012085 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030012086 ret = dev_priv->display.modeset_calc_cdclk(state);
12087 if (ret < 0)
12088 return ret;
12089
Ville Syrjälä8d965612016-11-14 18:35:10 +020012090 /*
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012091 * Writes to dev_priv->cdclk.logical must protected by
Ville Syrjälä8d965612016-11-14 18:35:10 +020012092 * holding all the crtc locks, even if we don't end up
12093 * touching the hardware
12094 */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012095 if (intel_cdclk_changed(&dev_priv->cdclk.logical,
12096 &intel_state->cdclk.logical)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012097 ret = intel_lock_all_pipes(state);
12098 if (ret < 0)
12099 return ret;
12100 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012101
Ville Syrjälä8d965612016-11-14 18:35:10 +020012102 /* All pipes must be switched off while we change the cdclk. */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012103 if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
12104 &intel_state->cdclk.actual)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012105 ret = intel_modeset_all_pipes(state);
12106 if (ret < 0)
12107 return ret;
12108 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010012109
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012110 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12111 intel_state->cdclk.logical.cdclk,
12112 intel_state->cdclk.actual.cdclk);
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030012113 DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
12114 intel_state->cdclk.logical.voltage_level,
12115 intel_state->cdclk.actual.voltage_level);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012116 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012117 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012118 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012119
Maarten Lankhorstad421372015-06-15 12:33:42 +020012120 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012121
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012122 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012123 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012124
Maarten Lankhorstad421372015-06-15 12:33:42 +020012125 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012126}
12127
Matt Roperaa363132015-09-24 15:53:18 -070012128/*
12129 * Handle calculation of various watermark data at the end of the atomic check
12130 * phase. The code here should be run after the per-crtc and per-plane 'check'
12131 * handlers to ensure that all derived state has been updated.
12132 */
Matt Roper55994c22016-05-12 07:06:08 -070012133static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070012134{
12135 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070012136 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070012137
12138 /* Is there platform-specific watermark information to calculate? */
12139 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070012140 return dev_priv->display.compute_global_watermarks(state);
12141
12142 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070012143}
12144
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012145/**
12146 * intel_atomic_check - validate state object
12147 * @dev: drm device
12148 * @state: state to validate
12149 */
12150static int intel_atomic_check(struct drm_device *dev,
12151 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012152{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012153 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070012154 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012155 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012156 struct drm_crtc_state *old_crtc_state, *crtc_state;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012157 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012158 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012159
Maarten Lankhorst8c58f732018-02-21 10:28:08 +010012160 /* Catch I915_MODE_FLAG_INHERITED */
12161 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
12162 crtc_state, i) {
12163 if (crtc_state->mode.private_flags !=
12164 old_crtc_state->mode.private_flags)
12165 crtc_state->mode_changed = true;
12166 }
12167
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012168 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012169 if (ret)
12170 return ret;
12171
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012172 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012173 struct intel_crtc_state *pipe_config =
12174 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012175
Daniel Vetter26495482015-07-15 14:15:52 +020012176 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012177 continue;
12178
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012179 if (!crtc_state->enable) {
12180 any_ms = true;
12181 continue;
12182 }
12183
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012184 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012185 if (ret) {
12186 intel_dump_pipe_config(to_intel_crtc(crtc),
12187 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012188 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012189 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012190
Michal Wajdeczko4f044a82017-09-19 19:38:44 +000012191 if (i915_modparams.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012192 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012193 to_intel_crtc_state(old_crtc_state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012194 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012195 crtc_state->mode_changed = false;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012196 pipe_config->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020012197 }
12198
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012199 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020012200 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012201
Daniel Vetter26495482015-07-15 14:15:52 +020012202 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12203 needs_modeset(crtc_state) ?
12204 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012205 }
12206
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012207 if (any_ms) {
12208 ret = intel_modeset_checks(state);
12209
12210 if (ret)
12211 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012212 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012213 intel_state->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012214 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012215
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012216 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070012217 if (ret)
12218 return ret;
12219
Ville Syrjälädd576022017-11-17 21:19:14 +020012220 intel_fbc_choose_crtc(dev_priv, intel_state);
Matt Roper55994c22016-05-12 07:06:08 -070012221 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012222}
12223
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012224static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010012225 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012226{
Chris Wilsonfd700752017-07-26 17:00:36 +010012227 return drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012228}
12229
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012230u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12231{
12232 struct drm_device *dev = crtc->base.dev;
12233
12234 if (!dev->max_vblank_count)
Dhinakaran Pandiyan734cbbf2018-02-02 21:12:54 -080012235 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012236
12237 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12238}
12239
Lyude896e5bb2016-08-24 07:48:09 +020012240static void intel_update_crtc(struct drm_crtc *crtc,
12241 struct drm_atomic_state *state,
12242 struct drm_crtc_state *old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012243 struct drm_crtc_state *new_crtc_state)
Lyude896e5bb2016-08-24 07:48:09 +020012244{
12245 struct drm_device *dev = crtc->dev;
12246 struct drm_i915_private *dev_priv = to_i915(dev);
12247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012248 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12249 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012250 struct intel_plane_state *new_plane_state =
12251 intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
12252 to_intel_plane(crtc->primary));
Lyude896e5bb2016-08-24 07:48:09 +020012253
12254 if (modeset) {
12255 update_scanline_offset(intel_crtc);
12256 dev_priv->display.crtc_enable(pipe_config, state);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012257
12258 /* vblanks work again, re-enable pipe CRC. */
12259 intel_crtc_enable_pipe_crc(intel_crtc);
Lyude896e5bb2016-08-24 07:48:09 +020012260 } else {
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012261 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12262 pipe_config);
Lyude896e5bb2016-08-24 07:48:09 +020012263 }
12264
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012265 if (new_plane_state)
12266 intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
Lyude896e5bb2016-08-24 07:48:09 +020012267
12268 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012269}
12270
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012271static void intel_update_crtcs(struct drm_atomic_state *state)
Lyude896e5bb2016-08-24 07:48:09 +020012272{
12273 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012274 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyude896e5bb2016-08-24 07:48:09 +020012275 int i;
12276
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012277 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12278 if (!new_crtc_state->active)
Lyude896e5bb2016-08-24 07:48:09 +020012279 continue;
12280
12281 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012282 new_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012283 }
12284}
12285
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012286static void skl_update_crtcs(struct drm_atomic_state *state)
Lyude27082492016-08-24 07:48:10 +020012287{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012288 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020012289 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12290 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040012291 struct intel_crtc *intel_crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012292 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040012293 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020012294 unsigned int updated = 0;
12295 bool progress;
12296 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012297 int i;
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012298 u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
12299 u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012300
12301 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12302
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012303 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012304 /* ignore allocations for crtc's that have been turned off. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012305 if (new_crtc_state->active)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012306 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012307
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012308 /* If 2nd DBuf slice required, enable it here */
12309 if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
12310 icl_dbuf_slices_update(dev_priv, required_slices);
12311
Lyude27082492016-08-24 07:48:10 +020012312 /*
12313 * Whenever the number of active pipes changes, we need to make sure we
12314 * update the pipes in the right order so that their ddb allocations
12315 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12316 * cause pipe underruns and other bad stuff.
12317 */
12318 do {
Lyude27082492016-08-24 07:48:10 +020012319 progress = false;
12320
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012321 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Lyude27082492016-08-24 07:48:10 +020012322 bool vbl_wait = false;
12323 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040012324
12325 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä21794812017-08-23 18:22:26 +030012326 cstate = to_intel_crtc_state(new_crtc_state);
Lyudece0ba282016-09-15 10:46:35 -040012327 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020012328
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012329 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020012330 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012331
Mika Kahola2b685042017-10-10 13:17:03 +030012332 if (skl_ddb_allocation_overlaps(dev_priv,
12333 entries,
12334 &cstate->wm.skl.ddb,
12335 i))
Lyude27082492016-08-24 07:48:10 +020012336 continue;
12337
12338 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012339 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012340
12341 /*
12342 * If this is an already active pipe, it's DDB changed,
12343 * and this isn't the last pipe that needs updating
12344 * then we need to wait for a vblank to pass for the
12345 * new ddb allocation to take effect.
12346 */
Lyudece0ba282016-09-15 10:46:35 -040012347 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010012348 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012349 !new_crtc_state->active_changed &&
Lyude27082492016-08-24 07:48:10 +020012350 intel_state->wm_results.dirty_pipes != updated)
12351 vbl_wait = true;
12352
12353 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012354 new_crtc_state);
Lyude27082492016-08-24 07:48:10 +020012355
12356 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012357 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020012358
12359 progress = true;
12360 }
12361 } while (progress);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012362
12363 /* If 2nd DBuf slice is no more required disable it */
12364 if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
12365 icl_dbuf_slices_update(dev_priv, required_slices);
Lyude27082492016-08-24 07:48:10 +020012366}
12367
Chris Wilsonba318c62017-02-02 20:47:41 +000012368static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12369{
12370 struct intel_atomic_state *state, *next;
12371 struct llist_node *freed;
12372
12373 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12374 llist_for_each_entry_safe(state, next, freed, freed)
12375 drm_atomic_state_put(&state->base);
12376}
12377
12378static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12379{
12380 struct drm_i915_private *dev_priv =
12381 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12382
12383 intel_atomic_helper_free_state(dev_priv);
12384}
12385
Daniel Vetter9db529a2017-08-08 10:08:28 +020012386static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
12387{
12388 struct wait_queue_entry wait_fence, wait_reset;
12389 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
12390
12391 init_wait_entry(&wait_fence, 0);
12392 init_wait_entry(&wait_reset, 0);
12393 for (;;) {
12394 prepare_to_wait(&intel_state->commit_ready.wait,
12395 &wait_fence, TASK_UNINTERRUPTIBLE);
12396 prepare_to_wait(&dev_priv->gpu_error.wait_queue,
12397 &wait_reset, TASK_UNINTERRUPTIBLE);
12398
12399
12400 if (i915_sw_fence_done(&intel_state->commit_ready)
12401 || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
12402 break;
12403
12404 schedule();
12405 }
12406 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
12407 finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
12408}
12409
Daniel Vetter94f05022016-06-14 18:01:00 +020012410static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012411{
Daniel Vetter94f05022016-06-14 18:01:00 +020012412 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012413 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012414 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012415 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012416 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012417 struct intel_crtc_state *intel_cstate;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020012418 u64 put_domains[I915_MAX_PIPES] = {};
Chris Wilsone95433c2016-10-28 13:58:27 +010012419 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012420
Daniel Vetter9db529a2017-08-08 10:08:28 +020012421 intel_atomic_commit_fence_wait(intel_state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020012422
Daniel Vetterea0000f2016-06-13 16:13:46 +020012423 drm_atomic_helper_wait_for_dependencies(state);
12424
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012425 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012426 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012427
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012428 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12430
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012431 if (needs_modeset(new_crtc_state) ||
12432 to_intel_crtc_state(new_crtc_state)->update_pipe) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012433
12434 put_domains[to_intel_crtc(crtc)->pipe] =
12435 modeset_get_crtc_power_domains(crtc,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012436 to_intel_crtc_state(new_crtc_state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020012437 }
12438
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012439 if (!needs_modeset(new_crtc_state))
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012440 continue;
12441
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012442 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12443 to_intel_crtc_state(new_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010012444
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012445 if (old_crtc_state->active) {
12446 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012447
12448 /*
12449 * We need to disable pipe CRC before disabling the pipe,
12450 * or we race against vblank off.
12451 */
12452 intel_crtc_disable_pipe_crc(intel_crtc);
12453
Maarten Lankhorst4a806552016-08-09 17:04:01 +020012454 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012455 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020012456 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012457 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020012458
12459 /*
12460 * Underruns don't always raise
12461 * interrupts, so check manually.
12462 */
12463 intel_check_cpu_fifo_underruns(dev_priv);
12464 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010012465
Ville Syrjälä21794812017-08-23 18:22:26 +030012466 if (!new_crtc_state->active) {
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012467 /*
12468 * Make sure we don't call initial_watermarks
12469 * for ILK-style watermark updates.
Ville Syrjäläff32c542017-03-02 19:14:57 +020012470 *
12471 * No clue what this is supposed to achieve.
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012472 */
Ville Syrjäläff32c542017-03-02 19:14:57 +020012473 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012474 dev_priv->display.initial_watermarks(intel_state,
Ville Syrjälä21794812017-08-23 18:22:26 +030012475 to_intel_crtc_state(new_crtc_state));
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012476 }
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012477 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012478 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012479
Daniel Vetter7a1530d72017-12-07 15:32:02 +010012480 /* FIXME: Eventually get rid of our intel_crtc->config pointer */
12481 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
12482 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012483
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012484 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012485 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010012486
Ville Syrjäläb0587e42017-01-26 21:52:01 +020012487 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010012488
Lyude656d1b82016-08-17 15:55:54 -040012489 /*
12490 * SKL workaround: bspec recommends we disable the SAGV when we
12491 * have more then one pipe enabled
12492 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030012493 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012494 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012495
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012496 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012497 }
Daniel Vetter47fab732012-10-26 10:58:18 +020012498
Lyude896e5bb2016-08-24 07:48:09 +020012499 /* Complete the events for pipes that have now been disabled */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012500 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12501 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012502
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012503 /* Complete events for now disable pipes here. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012504 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012505 spin_lock_irq(&dev->event_lock);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012506 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012507 spin_unlock_irq(&dev->event_lock);
12508
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012509 new_crtc_state->event = NULL;
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012510 }
Matt Ropered4a6a72016-02-23 17:20:13 -080012511 }
12512
Lyude896e5bb2016-08-24 07:48:09 +020012513 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012514 dev_priv->display.update_crtcs(state);
Lyude896e5bb2016-08-24 07:48:09 +020012515
Daniel Vetter94f05022016-06-14 18:01:00 +020012516 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12517 * already, but still need the state for the delayed optimization. To
12518 * fix this:
12519 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12520 * - schedule that vblank worker _before_ calling hw_done
12521 * - at the start of commit_tail, cancel it _synchrously
12522 * - switch over to the vblank wait helper in the core after that since
12523 * we don't need out special handling any more.
12524 */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012525 drm_atomic_helper_wait_for_flip_done(dev, state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012526
12527 /*
12528 * Now that the vblank has passed, we can go ahead and program the
12529 * optimal watermarks on platforms that need two-step watermark
12530 * programming.
12531 *
12532 * TODO: Move this (and other cleanup) to an async worker eventually.
12533 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012534 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12535 intel_cstate = to_intel_crtc_state(new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012536
12537 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010012538 dev_priv->display.optimize_watermarks(intel_state,
12539 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012540 }
12541
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012542 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012543 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12544
12545 if (put_domains[i])
12546 modeset_put_power_domains(dev_priv, put_domains[i]);
12547
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012548 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012549 }
12550
Ville Syrjäläcff109f2017-11-17 21:19:17 +020012551 if (intel_state->modeset)
12552 intel_verify_planes(intel_state);
12553
Paulo Zanoni56feca92016-09-22 18:00:28 -030012554 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012555 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012556
Daniel Vetter94f05022016-06-14 18:01:00 +020012557 drm_atomic_helper_commit_hw_done(state);
12558
Chris Wilsond5553c02017-05-04 12:55:08 +010012559 if (intel_state->modeset) {
12560 /* As one of the primary mmio accessors, KMS has a high
12561 * likelihood of triggering bugs in unclaimed access. After we
12562 * finish modesetting, see if an error has been flagged, and if
12563 * so enable debugging for the next modeset - and hope we catch
12564 * the culprit.
12565 */
12566 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012567 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
Chris Wilsond5553c02017-05-04 12:55:08 +010012568 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020012569
Daniel Vetter5a21b662016-05-24 17:13:53 +020012570 drm_atomic_helper_cleanup_planes(dev, state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012571
Daniel Vetterea0000f2016-06-13 16:13:46 +020012572 drm_atomic_helper_commit_cleanup_done(state);
12573
Chris Wilson08536952016-10-14 13:18:18 +010012574 drm_atomic_state_put(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012575
Chris Wilsonba318c62017-02-02 20:47:41 +000012576 intel_atomic_helper_free_state(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020012577}
12578
12579static void intel_atomic_commit_work(struct work_struct *work)
12580{
Chris Wilsonc004a902016-10-28 13:58:45 +010012581 struct drm_atomic_state *state =
12582 container_of(work, struct drm_atomic_state, commit_work);
12583
Daniel Vetter94f05022016-06-14 18:01:00 +020012584 intel_atomic_commit_tail(state);
12585}
12586
Chris Wilsonc004a902016-10-28 13:58:45 +010012587static int __i915_sw_fence_call
12588intel_atomic_commit_ready(struct i915_sw_fence *fence,
12589 enum i915_sw_fence_notify notify)
12590{
12591 struct intel_atomic_state *state =
12592 container_of(fence, struct intel_atomic_state, commit_ready);
12593
12594 switch (notify) {
12595 case FENCE_COMPLETE:
Daniel Vetter42b062b2017-08-08 10:08:27 +020012596 /* we do blocking waits in the worker, nothing to do here */
Chris Wilsonc004a902016-10-28 13:58:45 +010012597 break;
Chris Wilsonc004a902016-10-28 13:58:45 +010012598 case FENCE_FREE:
Chris Wilsoneb955ee2017-01-23 21:29:39 +000012599 {
12600 struct intel_atomic_helper *helper =
12601 &to_i915(state->base.dev)->atomic_helper;
12602
12603 if (llist_add(&state->freed, &helper->free_list))
12604 schedule_work(&helper->free_work);
12605 break;
12606 }
Chris Wilsonc004a902016-10-28 13:58:45 +010012607 }
12608
12609 return NOTIFY_DONE;
12610}
12611
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012612static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12613{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012614 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012615 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012616 int i;
12617
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012618 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012619 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012620 intel_fb_obj(new_plane_state->fb),
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012621 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012622}
12623
Daniel Vetter94f05022016-06-14 18:01:00 +020012624/**
12625 * intel_atomic_commit - commit validated state object
12626 * @dev: DRM device
12627 * @state: the top-level driver state object
12628 * @nonblock: nonblocking commit
12629 *
12630 * This function commits a top-level state object that has been validated
12631 * with drm_atomic_helper_check().
12632 *
Daniel Vetter94f05022016-06-14 18:01:00 +020012633 * RETURNS
12634 * Zero for success or -errno.
12635 */
12636static int intel_atomic_commit(struct drm_device *dev,
12637 struct drm_atomic_state *state,
12638 bool nonblock)
12639{
12640 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012641 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020012642 int ret = 0;
12643
Chris Wilsonc004a902016-10-28 13:58:45 +010012644 drm_atomic_state_get(state);
12645 i915_sw_fence_init(&intel_state->commit_ready,
12646 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020012647
Ville Syrjälä440df932017-03-29 17:21:23 +030012648 /*
12649 * The intel_legacy_cursor_update() fast path takes care
12650 * of avoiding the vblank waits for simple cursor
12651 * movement and flips. For cursor on/off and size changes,
12652 * we want to perform the vblank waits so that watermark
12653 * updates happen during the correct frames. Gen9+ have
12654 * double buffered watermarks and so shouldn't need this.
12655 *
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020012656 * Unset state->legacy_cursor_update before the call to
12657 * drm_atomic_helper_setup_commit() because otherwise
12658 * drm_atomic_helper_wait_for_flip_done() is a noop and
12659 * we get FIFO underruns because we didn't wait
12660 * for vblank.
Ville Syrjälä440df932017-03-29 17:21:23 +030012661 *
12662 * FIXME doing watermarks and fb cleanup from a vblank worker
12663 * (assuming we had any) would solve these problems.
12664 */
Maarten Lankhorst213f1bd2017-09-19 14:14:19 +020012665 if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
12666 struct intel_crtc_state *new_crtc_state;
12667 struct intel_crtc *crtc;
12668 int i;
12669
12670 for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
12671 if (new_crtc_state->wm.need_postvbl_update ||
12672 new_crtc_state->update_wm_post)
12673 state->legacy_cursor_update = false;
12674 }
Ville Syrjälä440df932017-03-29 17:21:23 +030012675
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020012676 ret = intel_atomic_prepare_commit(dev, state);
12677 if (ret) {
12678 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
12679 i915_sw_fence_commit(&intel_state->commit_ready);
12680 return ret;
12681 }
12682
12683 ret = drm_atomic_helper_setup_commit(state, nonblock);
12684 if (!ret)
12685 ret = drm_atomic_helper_swap_state(state, true);
12686
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020012687 if (ret) {
12688 i915_sw_fence_commit(&intel_state->commit_ready);
12689
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020012690 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020012691 return ret;
12692 }
Daniel Vetter94f05022016-06-14 18:01:00 +020012693 dev_priv->wm.distrust_bios_wm = false;
Ander Conselvan de Oliveira3c0fb582016-12-29 17:22:08 +020012694 intel_shared_dpll_swap_state(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012695 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020012696
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012697 if (intel_state->modeset) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030012698 memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
12699 sizeof(intel_state->min_cdclk));
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030012700 memcpy(dev_priv->min_voltage_level,
12701 intel_state->min_voltage_level,
12702 sizeof(intel_state->min_voltage_level));
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012703 dev_priv->active_crtcs = intel_state->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012704 dev_priv->cdclk.logical = intel_state->cdclk.logical;
12705 dev_priv->cdclk.actual = intel_state->cdclk.actual;
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012706 }
12707
Chris Wilson08536952016-10-14 13:18:18 +010012708 drm_atomic_state_get(state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020012709 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
Chris Wilsonc004a902016-10-28 13:58:45 +010012710
12711 i915_sw_fence_commit(&intel_state->commit_ready);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020012712 if (nonblock && intel_state->modeset) {
12713 queue_work(dev_priv->modeset_wq, &state->commit_work);
12714 } else if (nonblock) {
Daniel Vetter42b062b2017-08-08 10:08:27 +020012715 queue_work(system_unbound_wq, &state->commit_work);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020012716 } else {
12717 if (intel_state->modeset)
12718 flush_workqueue(dev_priv->modeset_wq);
Daniel Vetter94f05022016-06-14 18:01:00 +020012719 intel_atomic_commit_tail(state);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020012720 }
Mika Kuoppala75714942015-12-16 09:26:48 +020012721
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012722 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012723}
12724
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012725static const struct drm_crtc_funcs intel_crtc_funcs = {
Daniel Vetter3fab2f02017-04-03 10:32:57 +020012726 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012727 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012728 .destroy = intel_crtc_destroy,
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010012729 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012730 .atomic_duplicate_state = intel_crtc_duplicate_state,
12731 .atomic_destroy_state = intel_crtc_destroy_state,
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +010012732 .set_crc_source = intel_crtc_set_crc_source,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012733};
12734
Chris Wilson74d290f2017-08-17 13:37:06 +010012735struct wait_rps_boost {
12736 struct wait_queue_entry wait;
12737
12738 struct drm_crtc *crtc;
Chris Wilsone61e0f52018-02-21 09:56:36 +000012739 struct i915_request *request;
Chris Wilson74d290f2017-08-17 13:37:06 +010012740};
12741
12742static int do_rps_boost(struct wait_queue_entry *_wait,
12743 unsigned mode, int sync, void *key)
12744{
12745 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
Chris Wilsone61e0f52018-02-21 09:56:36 +000012746 struct i915_request *rq = wait->request;
Chris Wilson74d290f2017-08-17 13:37:06 +010012747
Chris Wilsone9af4ea2018-01-18 13:16:09 +000012748 /*
12749 * If we missed the vblank, but the request is already running it
12750 * is reasonable to assume that it will complete before the next
12751 * vblank without our intervention, so leave RPS alone.
12752 */
Chris Wilsone61e0f52018-02-21 09:56:36 +000012753 if (!i915_request_started(rq))
Chris Wilsone9af4ea2018-01-18 13:16:09 +000012754 gen6_rps_boost(rq, NULL);
Chris Wilsone61e0f52018-02-21 09:56:36 +000012755 i915_request_put(rq);
Chris Wilson74d290f2017-08-17 13:37:06 +010012756
12757 drm_crtc_vblank_put(wait->crtc);
12758
12759 list_del(&wait->wait.entry);
12760 kfree(wait);
12761 return 1;
12762}
12763
12764static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
12765 struct dma_fence *fence)
12766{
12767 struct wait_rps_boost *wait;
12768
12769 if (!dma_fence_is_i915(fence))
12770 return;
12771
12772 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
12773 return;
12774
12775 if (drm_crtc_vblank_get(crtc))
12776 return;
12777
12778 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
12779 if (!wait) {
12780 drm_crtc_vblank_put(crtc);
12781 return;
12782 }
12783
12784 wait->request = to_request(dma_fence_get(fence));
12785 wait->crtc = crtc;
12786
12787 wait->wait.func = do_rps_boost;
12788 wait->wait.flags = 0;
12789
12790 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
12791}
12792
Ville Syrjäläef1a1912018-02-21 18:02:34 +020012793static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
12794{
12795 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
12796 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
12797 struct drm_framebuffer *fb = plane_state->base.fb;
12798 struct i915_vma *vma;
12799
12800 if (plane->id == PLANE_CURSOR &&
12801 INTEL_INFO(dev_priv)->cursor_needs_physical) {
12802 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12803 const int align = intel_cursor_alignment(dev_priv);
12804
12805 return i915_gem_object_attach_phys(obj, align);
12806 }
12807
12808 vma = intel_pin_and_fence_fb_obj(fb,
12809 plane_state->base.rotation,
12810 intel_plane_uses_fence(plane_state),
12811 &plane_state->flags);
12812 if (IS_ERR(vma))
12813 return PTR_ERR(vma);
12814
12815 plane_state->vma = vma;
12816
12817 return 0;
12818}
12819
12820static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
12821{
12822 struct i915_vma *vma;
12823
12824 vma = fetch_and_zero(&old_plane_state->vma);
12825 if (vma)
12826 intel_unpin_fb_vma(vma, old_plane_state->flags);
12827}
12828
Chris Wilsonb7268c52018-04-18 19:40:52 +010012829static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
12830{
12831 struct i915_sched_attr attr = {
12832 .priority = I915_PRIORITY_DISPLAY,
12833 };
12834
12835 i915_gem_object_wait_priority(obj, 0, &attr);
12836}
12837
Matt Roper6beb8c232014-12-01 15:40:14 -080012838/**
12839 * intel_prepare_plane_fb - Prepare fb for usage on plane
12840 * @plane: drm plane to prepare for
Chris Wilsonc38c1452018-02-14 13:49:22 +000012841 * @new_state: the plane state being prepared
Matt Roper6beb8c232014-12-01 15:40:14 -080012842 *
12843 * Prepares a framebuffer for usage on a display plane. Generally this
12844 * involves pinning the underlying object and updating the frontbuffer tracking
12845 * bits. Some older platforms need special physical address handling for
12846 * cursor planes.
12847 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020012848 * Must be called with struct_mutex held.
12849 *
Matt Roper6beb8c232014-12-01 15:40:14 -080012850 * Returns 0 on success, negative error code on failure.
12851 */
12852int
12853intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010012854 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012855{
Chris Wilsonc004a902016-10-28 13:58:45 +010012856 struct intel_atomic_state *intel_state =
12857 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000012858 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020012859 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080012860 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020012861 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010012862 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070012863
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012864 if (old_obj) {
12865 struct drm_crtc_state *crtc_state =
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012866 drm_atomic_get_new_crtc_state(new_state->state,
12867 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012868
12869 /* Big Hammer, we also need to ensure that any pending
12870 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
12871 * current scanout is retired before unpinning the old
12872 * framebuffer. Note that we rely on userspace rendering
12873 * into the buffer attached to the pipe they are waiting
12874 * on. If not, userspace generates a GPU hang with IPEHR
12875 * point to the MI_WAIT_FOR_EVENT.
12876 *
12877 * This should only fail upon a hung GPU, in which case we
12878 * can safely continue.
12879 */
Chris Wilsonc004a902016-10-28 13:58:45 +010012880 if (needs_modeset(crtc_state)) {
12881 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
12882 old_obj->resv, NULL,
12883 false, 0,
12884 GFP_KERNEL);
12885 if (ret < 0)
12886 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010012887 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012888 }
12889
Chris Wilsonc004a902016-10-28 13:58:45 +010012890 if (new_state->fence) { /* explicit fencing */
12891 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
12892 new_state->fence,
12893 I915_FENCE_TIMEOUT,
12894 GFP_KERNEL);
12895 if (ret < 0)
12896 return ret;
12897 }
12898
Chris Wilsonc37efb92016-06-17 08:28:47 +010012899 if (!obj)
12900 return 0;
12901
Chris Wilson4d3088c2017-07-26 17:00:38 +010012902 ret = i915_gem_object_pin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010012903 if (ret)
12904 return ret;
12905
Chris Wilson4d3088c2017-07-26 17:00:38 +010012906 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
12907 if (ret) {
12908 i915_gem_object_unpin_pages(obj);
12909 return ret;
12910 }
12911
Ville Syrjäläef1a1912018-02-21 18:02:34 +020012912 ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
Chris Wilsonfd700752017-07-26 17:00:36 +010012913
Chris Wilsonb7268c52018-04-18 19:40:52 +010012914 fb_obj_bump_render_priority(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010012915
12916 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson4d3088c2017-07-26 17:00:38 +010012917 i915_gem_object_unpin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010012918 if (ret)
12919 return ret;
12920
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080012921 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
12922
Chris Wilsonc004a902016-10-28 13:58:45 +010012923 if (!new_state->fence) { /* implicit fencing */
Chris Wilson74d290f2017-08-17 13:37:06 +010012924 struct dma_fence *fence;
12925
Chris Wilsonc004a902016-10-28 13:58:45 +010012926 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
12927 obj->resv, NULL,
12928 false, I915_FENCE_TIMEOUT,
12929 GFP_KERNEL);
12930 if (ret < 0)
12931 return ret;
Chris Wilson74d290f2017-08-17 13:37:06 +010012932
12933 fence = reservation_object_get_excl_rcu(obj->resv);
12934 if (fence) {
12935 add_rps_boost_after_vblank(new_state->crtc, fence);
12936 dma_fence_put(fence);
12937 }
12938 } else {
12939 add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
Chris Wilsonc004a902016-10-28 13:58:45 +010012940 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020012941
Chris Wilsond07f0e52016-10-28 13:58:44 +010012942 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080012943}
12944
Matt Roper38f3ce32014-12-02 07:45:25 -080012945/**
12946 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12947 * @plane: drm plane to clean up for
Chris Wilsonc38c1452018-02-14 13:49:22 +000012948 * @old_state: the state from the previous modeset
Matt Roper38f3ce32014-12-02 07:45:25 -080012949 *
12950 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020012951 *
12952 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080012953 */
12954void
12955intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010012956 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012957{
Ville Syrjäläef1a1912018-02-21 18:02:34 +020012958 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper38f3ce32014-12-02 07:45:25 -080012959
Chris Wilsonbe1e3412017-01-16 15:21:27 +000012960 /* Should only be called after a successful intel_prepare_plane_fb()! */
Ville Syrjäläef1a1912018-02-21 18:02:34 +020012961 mutex_lock(&dev_priv->drm.struct_mutex);
12962 intel_plane_unpin_fb(to_intel_plane_state(old_state));
12963 mutex_unlock(&dev_priv->drm.struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012964}
12965
Chandra Konduru6156a452015-04-27 13:48:39 -070012966int
Chandra Konduru77224cd2018-04-09 09:11:13 +053012967skl_max_scale(struct intel_crtc *intel_crtc,
12968 struct intel_crtc_state *crtc_state,
12969 uint32_t pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -070012970{
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020012971 struct drm_i915_private *dev_priv;
Chandra Konduru77224cd2018-04-09 09:11:13 +053012972 int max_scale, mult;
12973 int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
Chandra Konduru6156a452015-04-27 13:48:39 -070012974
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010012975 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070012976 return DRM_PLANE_HELPER_NO_SCALING;
12977
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020012978 dev_priv = to_i915(intel_crtc->base.dev);
Chandra Konduru6156a452015-04-27 13:48:39 -070012979
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020012980 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
12981 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
12982
Rodrigo Vivi43037c82017-10-03 15:31:42 -070012983 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020012984 max_dotclk *= 2;
12985
12986 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070012987 return DRM_PLANE_HELPER_NO_SCALING;
12988
12989 /*
12990 * skl max scale is lower of:
12991 * close to 3 but not 3, -1 is for that purpose
12992 * or
12993 * cdclk/crtc_clock
12994 */
Chandra Konduru77224cd2018-04-09 09:11:13 +053012995 mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
12996 tmpclk1 = (1 << 16) * mult - 1;
12997 tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
12998 max_scale = min(tmpclk1, tmpclk2);
Chandra Konduru6156a452015-04-27 13:48:39 -070012999
13000 return max_scale;
13001}
13002
Matt Roper465c1202014-05-29 08:06:54 -070013003static int
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013004intel_check_primary_plane(struct intel_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013005 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013006 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013007{
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013008 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Matt Roper2b875c22014-12-01 15:40:13 -080013009 struct drm_crtc *crtc = state->base.crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013010 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013011 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13012 bool can_position = false;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013013 int ret;
Chandra Konduru77224cd2018-04-09 09:11:13 +053013014 uint32_t pixel_format = 0;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013015
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013016 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013017 /* use scaler when colorkey is not required */
Ville Syrjälä6ec5bd32018-02-02 22:42:31 +020013018 if (!state->ckey.flags) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013019 min_scale = 1;
Chandra Konduru77224cd2018-04-09 09:11:13 +053013020 if (state->base.fb)
13021 pixel_format = state->base.fb->format->format;
13022 max_scale = skl_max_scale(to_intel_crtc(crtc),
13023 crtc_state, pixel_format);
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013024 }
Sonika Jindald8106362015-04-10 14:37:28 +053013025 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013026 }
Sonika Jindald8106362015-04-10 14:37:28 +053013027
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +020013028 ret = drm_atomic_helper_check_plane_state(&state->base,
13029 &crtc_state->base,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +020013030 min_scale, max_scale,
13031 can_position, true);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013032 if (ret)
13033 return ret;
13034
Daniel Vettercc926382016-08-15 10:41:47 +020013035 if (!state->base.fb)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013036 return 0;
13037
13038 if (INTEL_GEN(dev_priv) >= 9) {
Imre Deakc322c642018-01-16 13:24:14 +020013039 ret = skl_check_plane_surface(crtc_state, state);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013040 if (ret)
13041 return ret;
Ville Syrjäläa0864d52017-03-23 21:27:09 +020013042
13043 state->ctl = skl_plane_ctl(crtc_state, state);
13044 } else {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +020013045 ret = i9xx_check_plane_surface(state);
13046 if (ret)
13047 return ret;
13048
Ville Syrjäläa0864d52017-03-23 21:27:09 +020013049 state->ctl = i9xx_plane_ctl(crtc_state, state);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013050 }
13051
James Ausmus4036c782017-11-13 10:11:28 -080013052 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
13053 state->color_ctl = glk_plane_color_ctl(crtc_state, state);
13054
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020013055 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013056}
13057
Daniel Vetter5a21b662016-05-24 17:13:53 +020013058static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13059 struct drm_crtc_state *old_crtc_state)
13060{
13061 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040013062 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013064 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020013065 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013066 struct intel_atomic_state *old_intel_state =
13067 to_intel_atomic_state(old_crtc_state->state);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013068 struct intel_crtc_state *intel_cstate =
13069 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
13070 bool modeset = needs_modeset(&intel_cstate->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013071
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013072 if (!modeset &&
13073 (intel_cstate->base.color_mgmt_changed ||
13074 intel_cstate->update_pipe)) {
Ville Syrjälä5c857e62017-08-23 18:22:20 +030013075 intel_color_set_csc(&intel_cstate->base);
13076 intel_color_load_luts(&intel_cstate->base);
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013077 }
13078
Daniel Vetter5a21b662016-05-24 17:13:53 +020013079 /* Perform vblank evasion around commit operation */
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013080 intel_pipe_update_start(intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013081
13082 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013083 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013084
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013085 if (intel_cstate->update_pipe)
Ville Syrjälä1a15b772017-08-23 18:22:25 +030013086 intel_update_pipe_config(old_intel_cstate, intel_cstate);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013087 else if (INTEL_GEN(dev_priv) >= 9)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013088 skl_detach_scalers(intel_crtc);
Lyude62e0fb82016-08-22 12:50:08 -040013089
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013090out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013091 if (dev_priv->display.atomic_update_watermarks)
13092 dev_priv->display.atomic_update_watermarks(old_intel_state,
13093 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013094}
13095
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013096void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
13097 struct intel_crtc_state *crtc_state)
13098{
13099 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13100
13101 if (!IS_GEN2(dev_priv))
13102 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
13103
13104 if (crtc_state->has_pch_encoder) {
13105 enum pipe pch_transcoder =
13106 intel_crtc_pch_transcoder(crtc);
13107
13108 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
13109 }
13110}
13111
Daniel Vetter5a21b662016-05-24 17:13:53 +020013112static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13113 struct drm_crtc_state *old_crtc_state)
13114{
13115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013116 struct intel_atomic_state *old_intel_state =
13117 to_intel_atomic_state(old_crtc_state->state);
13118 struct intel_crtc_state *new_crtc_state =
13119 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013120
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013121 intel_pipe_update_end(new_crtc_state);
Maarten Lankhorst33a49862017-11-13 15:40:43 +010013122
13123 if (new_crtc_state->update_pipe &&
13124 !needs_modeset(&new_crtc_state->base) &&
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013125 old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
13126 intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013127}
13128
Matt Ropercf4c7c12014-12-04 10:27:42 -080013129/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013130 * intel_plane_destroy - destroy a plane
13131 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013132 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013133 * Common destruction function for all types of planes (primary, cursor,
13134 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013135 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013136void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013137{
Matt Roper465c1202014-05-29 08:06:54 -070013138 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030013139 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070013140}
13141
Ben Widawsky714244e2017-08-01 09:58:16 -070013142static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
13143{
13144 switch (format) {
13145 case DRM_FORMAT_C8:
13146 case DRM_FORMAT_RGB565:
13147 case DRM_FORMAT_XRGB1555:
13148 case DRM_FORMAT_XRGB8888:
13149 return modifier == DRM_FORMAT_MOD_LINEAR ||
13150 modifier == I915_FORMAT_MOD_X_TILED;
13151 default:
13152 return false;
13153 }
13154}
13155
13156static bool i965_mod_supported(uint32_t format, uint64_t modifier)
13157{
13158 switch (format) {
13159 case DRM_FORMAT_C8:
13160 case DRM_FORMAT_RGB565:
13161 case DRM_FORMAT_XRGB8888:
13162 case DRM_FORMAT_XBGR8888:
13163 case DRM_FORMAT_XRGB2101010:
13164 case DRM_FORMAT_XBGR2101010:
13165 return modifier == DRM_FORMAT_MOD_LINEAR ||
13166 modifier == I915_FORMAT_MOD_X_TILED;
13167 default:
13168 return false;
13169 }
13170}
13171
13172static bool skl_mod_supported(uint32_t format, uint64_t modifier)
13173{
13174 switch (format) {
13175 case DRM_FORMAT_XRGB8888:
13176 case DRM_FORMAT_XBGR8888:
13177 case DRM_FORMAT_ARGB8888:
13178 case DRM_FORMAT_ABGR8888:
13179 if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
13180 modifier == I915_FORMAT_MOD_Y_TILED_CCS)
13181 return true;
13182 /* fall through */
13183 case DRM_FORMAT_RGB565:
13184 case DRM_FORMAT_XRGB2101010:
13185 case DRM_FORMAT_XBGR2101010:
13186 case DRM_FORMAT_YUYV:
13187 case DRM_FORMAT_YVYU:
13188 case DRM_FORMAT_UYVY:
13189 case DRM_FORMAT_VYUY:
Chandra Konduruc0b56ab2018-05-12 03:03:16 +053013190 case DRM_FORMAT_NV12:
Ben Widawsky714244e2017-08-01 09:58:16 -070013191 if (modifier == I915_FORMAT_MOD_Yf_TILED)
13192 return true;
13193 /* fall through */
13194 case DRM_FORMAT_C8:
13195 if (modifier == DRM_FORMAT_MOD_LINEAR ||
13196 modifier == I915_FORMAT_MOD_X_TILED ||
13197 modifier == I915_FORMAT_MOD_Y_TILED)
13198 return true;
13199 /* fall through */
13200 default:
13201 return false;
13202 }
13203}
13204
13205static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
13206 uint32_t format,
13207 uint64_t modifier)
13208{
13209 struct drm_i915_private *dev_priv = to_i915(plane->dev);
13210
13211 if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
13212 return false;
13213
13214 if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
13215 modifier != DRM_FORMAT_MOD_LINEAR)
13216 return false;
13217
13218 if (INTEL_GEN(dev_priv) >= 9)
13219 return skl_mod_supported(format, modifier);
13220 else if (INTEL_GEN(dev_priv) >= 4)
13221 return i965_mod_supported(format, modifier);
13222 else
13223 return i8xx_mod_supported(format, modifier);
Ben Widawsky714244e2017-08-01 09:58:16 -070013224}
13225
13226static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
13227 uint32_t format,
13228 uint64_t modifier)
13229{
13230 if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
13231 return false;
13232
13233 return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
13234}
13235
13236static struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013237 .update_plane = drm_atomic_helper_update_plane,
13238 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013239 .destroy = intel_plane_destroy,
Matt Ropera98b3432015-01-21 16:35:43 -080013240 .atomic_get_property = intel_plane_atomic_get_property,
13241 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013242 .atomic_duplicate_state = intel_plane_duplicate_state,
13243 .atomic_destroy_state = intel_plane_destroy_state,
Ben Widawsky714244e2017-08-01 09:58:16 -070013244 .format_mod_supported = intel_primary_plane_format_mod_supported,
Matt Roper465c1202014-05-29 08:06:54 -070013245};
13246
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013247static int
13248intel_legacy_cursor_update(struct drm_plane *plane,
13249 struct drm_crtc *crtc,
13250 struct drm_framebuffer *fb,
13251 int crtc_x, int crtc_y,
13252 unsigned int crtc_w, unsigned int crtc_h,
13253 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013254 uint32_t src_w, uint32_t src_h,
13255 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013256{
13257 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13258 int ret;
13259 struct drm_plane_state *old_plane_state, *new_plane_state;
13260 struct intel_plane *intel_plane = to_intel_plane(plane);
13261 struct drm_framebuffer *old_fb;
13262 struct drm_crtc_state *crtc_state = crtc->state;
13263
13264 /*
13265 * When crtc is inactive or there is a modeset pending,
13266 * wait for it to complete in the slowpath
13267 */
13268 if (!crtc_state->active || needs_modeset(crtc_state) ||
13269 to_intel_crtc_state(crtc_state)->update_pipe)
13270 goto slow;
13271
13272 old_plane_state = plane->state;
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013273 /*
13274 * Don't do an async update if there is an outstanding commit modifying
13275 * the plane. This prevents our async update's changes from getting
13276 * overridden by a previous synchronous update's state.
13277 */
13278 if (old_plane_state->commit &&
13279 !try_wait_for_completion(&old_plane_state->commit->hw_done))
13280 goto slow;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013281
13282 /*
13283 * If any parameters change that may affect watermarks,
13284 * take the slowpath. Only changing fb or position should be
13285 * in the fastpath.
13286 */
13287 if (old_plane_state->crtc != crtc ||
13288 old_plane_state->src_w != src_w ||
13289 old_plane_state->src_h != src_h ||
13290 old_plane_state->crtc_w != crtc_w ||
13291 old_plane_state->crtc_h != crtc_h ||
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013292 !old_plane_state->fb != !fb)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013293 goto slow;
13294
13295 new_plane_state = intel_plane_duplicate_state(plane);
13296 if (!new_plane_state)
13297 return -ENOMEM;
13298
13299 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13300
13301 new_plane_state->src_x = src_x;
13302 new_plane_state->src_y = src_y;
13303 new_plane_state->src_w = src_w;
13304 new_plane_state->src_h = src_h;
13305 new_plane_state->crtc_x = crtc_x;
13306 new_plane_state->crtc_y = crtc_y;
13307 new_plane_state->crtc_w = crtc_w;
13308 new_plane_state->crtc_h = crtc_h;
13309
13310 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
Ville Syrjäläb2b55502017-08-23 18:22:23 +030013311 to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
13312 to_intel_plane_state(plane->state),
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013313 to_intel_plane_state(new_plane_state));
13314 if (ret)
13315 goto out_free;
13316
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013317 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13318 if (ret)
13319 goto out_free;
13320
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013321 ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
13322 if (ret)
13323 goto out_unlock;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013324
Dhinakaran Pandiyana694e222018-03-06 19:34:19 -080013325 intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013326
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080013327 old_fb = old_plane_state->fb;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013328 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13329 intel_plane->frontbuffer_bit);
13330
13331 /* Swap plane state */
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013332 plane->state = new_plane_state;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013333
Ville Syrjälä72259532017-03-02 19:15:05 +020013334 if (plane->state->visible) {
13335 trace_intel_update_plane(plane, to_intel_crtc(crtc));
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013336 intel_plane->update_plane(intel_plane,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013337 to_intel_crtc_state(crtc->state),
13338 to_intel_plane_state(plane->state));
Ville Syrjälä72259532017-03-02 19:15:05 +020013339 } else {
13340 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013341 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
Ville Syrjälä72259532017-03-02 19:15:05 +020013342 }
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013343
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013344 intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013345
13346out_unlock:
13347 mutex_unlock(&dev_priv->drm.struct_mutex);
13348out_free:
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013349 if (ret)
13350 intel_plane_destroy_state(plane, new_plane_state);
13351 else
13352 intel_plane_destroy_state(plane, old_plane_state);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013353 return ret;
13354
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013355slow:
13356 return drm_atomic_helper_update_plane(plane, crtc, fb,
13357 crtc_x, crtc_y, crtc_w, crtc_h,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013358 src_x, src_y, src_w, src_h, ctx);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013359}
13360
13361static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13362 .update_plane = intel_legacy_cursor_update,
13363 .disable_plane = drm_atomic_helper_disable_plane,
13364 .destroy = intel_plane_destroy,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013365 .atomic_get_property = intel_plane_atomic_get_property,
13366 .atomic_set_property = intel_plane_atomic_set_property,
13367 .atomic_duplicate_state = intel_plane_duplicate_state,
13368 .atomic_destroy_state = intel_plane_destroy_state,
Ben Widawsky714244e2017-08-01 09:58:16 -070013369 .format_mod_supported = intel_cursor_plane_format_mod_supported,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013370};
13371
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013372static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
13373 enum i9xx_plane_id i9xx_plane)
13374{
13375 if (!HAS_FBC(dev_priv))
13376 return false;
13377
13378 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
13379 return i9xx_plane == PLANE_A; /* tied to pipe A */
13380 else if (IS_IVYBRIDGE(dev_priv))
13381 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
13382 i9xx_plane == PLANE_C;
13383 else if (INTEL_GEN(dev_priv) >= 4)
13384 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
13385 else
13386 return i9xx_plane == PLANE_A;
13387}
13388
13389static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
13390 enum pipe pipe, enum plane_id plane_id)
13391{
13392 if (!HAS_FBC(dev_priv))
13393 return false;
13394
13395 return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
13396}
13397
Chandra Konduruc0b56ab2018-05-12 03:03:16 +053013398bool skl_plane_has_planar(struct drm_i915_private *dev_priv,
13399 enum pipe pipe, enum plane_id plane_id)
13400{
13401 if (plane_id == PLANE_PRIMARY) {
13402 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
13403 return false;
13404 else if ((INTEL_GEN(dev_priv) == 9 && pipe == PIPE_C) &&
13405 !IS_GEMINILAKE(dev_priv))
13406 return false;
13407 } else if (plane_id >= PLANE_SPRITE0) {
13408 if (plane_id == PLANE_CURSOR)
13409 return false;
13410 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) == 10) {
13411 if (plane_id != PLANE_SPRITE0)
13412 return false;
13413 } else {
13414 if (plane_id != PLANE_SPRITE0 || pipe == PIPE_C ||
13415 IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
13416 return false;
13417 }
13418 }
13419 return true;
13420}
13421
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013422static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020013423intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070013424{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013425 struct intel_plane *primary = NULL;
13426 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013427 const uint32_t *intel_primary_formats;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013428 unsigned int supported_rotations;
Thierry Reding45e37432015-08-12 16:54:28 +020013429 unsigned int num_formats;
Ben Widawsky714244e2017-08-01 09:58:16 -070013430 const uint64_t *modifiers;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013431 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013432
13433 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013434 if (!primary) {
13435 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013436 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013437 }
Matt Roper465c1202014-05-29 08:06:54 -070013438
Matt Roper8e7d6882015-01-21 16:35:41 -080013439 state = intel_create_plane_state(&primary->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013440 if (!state) {
13441 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013442 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013443 }
13444
Matt Roper8e7d6882015-01-21 16:35:41 -080013445 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013446
Matt Roper465c1202014-05-29 08:06:54 -070013447 primary->can_scale = false;
13448 primary->max_downscale = 1;
Ville Syrjälä580503c2016-10-31 22:37:00 +020013449 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru6156a452015-04-27 13:48:39 -070013450 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013451 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013452 }
Matt Roper465c1202014-05-29 08:06:54 -070013453 primary->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013454 /*
13455 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13456 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13457 */
13458 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
Ville Syrjäläed150302017-11-17 21:19:10 +020013459 primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013460 else
Ville Syrjäläed150302017-11-17 21:19:10 +020013461 primary->i9xx_plane = (enum i9xx_plane_id) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013462 primary->id = PLANE_PRIMARY;
Ville Syrjäläc19e1122018-01-23 20:33:43 +020013463 primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013464
13465 if (INTEL_GEN(dev_priv) >= 9)
13466 primary->has_fbc = skl_plane_has_fbc(dev_priv,
13467 primary->pipe,
13468 primary->id);
13469 else
13470 primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
13471 primary->i9xx_plane);
13472
13473 if (primary->has_fbc) {
13474 struct intel_fbc *fbc = &dev_priv->fbc;
13475
13476 fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
13477 }
13478
Matt Roperc59cb172014-12-01 15:40:16 -080013479 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013480
Ville Syrjälä77064e22017-12-22 21:22:28 +020013481 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduruc0b56ab2018-05-12 03:03:16 +053013482 if (skl_plane_has_planar(dev_priv, pipe, PLANE_PRIMARY)) {
13483 intel_primary_formats = skl_pri_planar_formats;
13484 num_formats = ARRAY_SIZE(skl_pri_planar_formats);
13485 } else {
13486 intel_primary_formats = skl_primary_formats;
13487 num_formats = ARRAY_SIZE(skl_primary_formats);
13488 }
Ben Widawsky714244e2017-08-01 09:58:16 -070013489
Ville Syrjälä77064e22017-12-22 21:22:28 +020013490 if (skl_plane_has_ccs(dev_priv, pipe, PLANE_PRIMARY))
Ben Widawsky714244e2017-08-01 09:58:16 -070013491 modifiers = skl_format_modifiers_ccs;
13492 else
13493 modifiers = skl_format_modifiers_noccs;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013494
Juha-Pekka Heikkila9a8cc572017-10-17 23:08:09 +030013495 primary->update_plane = skl_update_plane;
Juha-Pekka Heikkila779d4d82017-10-17 23:08:10 +030013496 primary->disable_plane = skl_disable_plane;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013497 primary->get_hw_state = skl_plane_get_hw_state;
Ville Syrjälä580503c2016-10-31 22:37:00 +020013498 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013499 intel_primary_formats = i965_primary_formats;
13500 num_formats = ARRAY_SIZE(i965_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013501 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013502
Ville Syrjäläed150302017-11-17 21:19:10 +020013503 primary->update_plane = i9xx_update_plane;
13504 primary->disable_plane = i9xx_disable_plane;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013505 primary->get_hw_state = i9xx_plane_get_hw_state;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013506 } else {
13507 intel_primary_formats = i8xx_primary_formats;
13508 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013509 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013510
Ville Syrjäläed150302017-11-17 21:19:10 +020013511 primary->update_plane = i9xx_update_plane;
13512 primary->disable_plane = i9xx_disable_plane;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013513 primary->get_hw_state = i9xx_plane_get_hw_state;
Matt Roper465c1202014-05-29 08:06:54 -070013514 }
13515
Ville Syrjälä580503c2016-10-31 22:37:00 +020013516 if (INTEL_GEN(dev_priv) >= 9)
13517 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13518 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013519 intel_primary_formats, num_formats,
Ben Widawsky714244e2017-08-01 09:58:16 -070013520 modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013521 DRM_PLANE_TYPE_PRIMARY,
13522 "plane 1%c", pipe_name(pipe));
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010013523 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä580503c2016-10-31 22:37:00 +020013524 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13525 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013526 intel_primary_formats, num_formats,
Ben Widawsky714244e2017-08-01 09:58:16 -070013527 modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013528 DRM_PLANE_TYPE_PRIMARY,
13529 "primary %c", pipe_name(pipe));
13530 else
Ville Syrjälä580503c2016-10-31 22:37:00 +020013531 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13532 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013533 intel_primary_formats, num_formats,
Ben Widawsky714244e2017-08-01 09:58:16 -070013534 modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013535 DRM_PLANE_TYPE_PRIMARY,
Ville Syrjäläed150302017-11-17 21:19:10 +020013536 "plane %c",
13537 plane_name(primary->i9xx_plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013538 if (ret)
13539 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053013540
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -080013541 if (INTEL_GEN(dev_priv) >= 10) {
13542 supported_rotations =
13543 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13544 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
13545 DRM_MODE_REFLECT_X;
13546 } else if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013547 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013548 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13549 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020013550 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13551 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013552 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13553 DRM_MODE_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100013554 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013555 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013556 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013557 } else {
Robert Fossc2c446a2017-05-19 16:50:17 -040013558 supported_rotations = DRM_MODE_ROTATE_0;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013559 }
13560
Dave Airlie5481e272016-10-25 16:36:13 +100013561 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013562 drm_plane_create_rotation_property(&primary->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013563 DRM_MODE_ROTATE_0,
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013564 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053013565
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +020013566 if (INTEL_GEN(dev_priv) >= 9)
13567 drm_plane_create_color_properties(&primary->base,
13568 BIT(DRM_COLOR_YCBCR_BT601) |
13569 BIT(DRM_COLOR_YCBCR_BT709),
Ville Syrjäläc8624ed2018-02-14 21:23:27 +020013570 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
13571 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
Ville Syrjälä23b28082018-02-14 21:23:26 +020013572 DRM_COLOR_YCBCR_BT709,
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +020013573 DRM_COLOR_YCBCR_LIMITED_RANGE);
13574
Matt Roperea2c67b2014-12-23 10:41:52 -080013575 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13576
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013577 return primary;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013578
13579fail:
13580 kfree(state);
13581 kfree(primary);
13582
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013583 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070013584}
13585
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013586static struct intel_plane *
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013587intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13588 enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070013589{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013590 struct intel_plane *cursor = NULL;
13591 struct intel_plane_state *state = NULL;
13592 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070013593
13594 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013595 if (!cursor) {
13596 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013597 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013598 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013599
Matt Roper8e7d6882015-01-21 16:35:41 -080013600 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013601 if (!state) {
13602 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013603 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013604 }
13605
Matt Roper8e7d6882015-01-21 16:35:41 -080013606 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013607
Matt Roper3d7d6512014-06-10 08:28:13 -070013608 cursor->can_scale = false;
13609 cursor->max_downscale = 1;
13610 cursor->pipe = pipe;
Ville Syrjäläed150302017-11-17 21:19:10 +020013611 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013612 cursor->id = PLANE_CURSOR;
Ville Syrjäläc19e1122018-01-23 20:33:43 +020013613 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013614
13615 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
13616 cursor->update_plane = i845_update_cursor;
13617 cursor->disable_plane = i845_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013618 cursor->get_hw_state = i845_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013619 cursor->check_plane = i845_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013620 } else {
13621 cursor->update_plane = i9xx_update_cursor;
13622 cursor->disable_plane = i9xx_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013623 cursor->get_hw_state = i9xx_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013624 cursor->check_plane = i9xx_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013625 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013626
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030013627 cursor->cursor.base = ~0;
13628 cursor->cursor.cntl = ~0;
Ville Syrjälä024faac2017-03-27 21:55:42 +030013629
13630 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13631 cursor->cursor.size = ~0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013632
Ville Syrjälä580503c2016-10-31 22:37:00 +020013633 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013634 0, &intel_cursor_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013635 intel_cursor_formats,
13636 ARRAY_SIZE(intel_cursor_formats),
Ben Widawsky714244e2017-08-01 09:58:16 -070013637 cursor_format_modifiers,
13638 DRM_PLANE_TYPE_CURSOR,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013639 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013640 if (ret)
13641 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013642
Dave Airlie5481e272016-10-25 16:36:13 +100013643 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013644 drm_plane_create_rotation_property(&cursor->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013645 DRM_MODE_ROTATE_0,
13646 DRM_MODE_ROTATE_0 |
13647 DRM_MODE_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013648
Ville Syrjälä580503c2016-10-31 22:37:00 +020013649 if (INTEL_GEN(dev_priv) >= 9)
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013650 state->scaler_id = -1;
13651
Matt Roperea2c67b2014-12-23 10:41:52 -080013652 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13653
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013654 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013655
13656fail:
13657 kfree(state);
13658 kfree(cursor);
13659
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013660 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070013661}
13662
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013663static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13664 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013665{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013666 struct intel_crtc_scaler_state *scaler_state =
13667 &crtc_state->scaler_state;
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013668 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013669 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013670
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013671 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13672 if (!crtc->num_scalers)
13673 return;
13674
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013675 for (i = 0; i < crtc->num_scalers; i++) {
13676 struct intel_scaler *scaler = &scaler_state->scalers[i];
13677
13678 scaler->in_use = 0;
13679 scaler->mode = PS_SCALER_MODE_DYN;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013680 }
13681
13682 scaler_state->scaler_id = -1;
13683}
13684
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013685static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013686{
13687 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013688 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013689 struct intel_plane *primary = NULL;
13690 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013691 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013692
Daniel Vetter955382f2013-09-19 14:05:45 +020013693 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013694 if (!intel_crtc)
13695 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080013696
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013697 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013698 if (!crtc_state) {
13699 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013700 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013701 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013702 intel_crtc->config = crtc_state;
13703 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013704 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013705
Ville Syrjälä580503c2016-10-31 22:37:00 +020013706 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013707 if (IS_ERR(primary)) {
13708 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070013709 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013710 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013711 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013712
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013713 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013714 struct intel_plane *plane;
13715
Ville Syrjälä580503c2016-10-31 22:37:00 +020013716 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013717 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013718 ret = PTR_ERR(plane);
13719 goto fail;
13720 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013721 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013722 }
13723
Ville Syrjälä580503c2016-10-31 22:37:00 +020013724 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013725 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013726 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070013727 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013728 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013729 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013730
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013731 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013732 &primary->base, &cursor->base,
13733 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030013734 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070013735 if (ret)
13736 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013737
Jesse Barnes80824002009-09-10 15:28:06 -070013738 intel_crtc->pipe = pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013739
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013740 /* initialize shared scalers */
13741 intel_crtc_init_scalers(intel_crtc, crtc_state);
13742
Ville Syrjälä1947fd12018-03-05 19:41:22 +020013743 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
13744 dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
13745 dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
13746
13747 if (INTEL_GEN(dev_priv) < 9) {
13748 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
13749
13750 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13751 dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
13752 dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
13753 }
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013754
Jesse Barnes79e53942008-11-07 14:24:08 -080013755 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013756
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013757 intel_color_init(&intel_crtc->base);
13758
Daniel Vetter87b6b102014-05-15 15:33:46 +020013759 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013760
13761 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013762
13763fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013764 /*
13765 * drm_mode_config_cleanup() will free up any
13766 * crtcs/planes already initialized.
13767 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013768 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013769 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013770
13771 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013772}
13773
Jesse Barnes752aa882013-10-31 18:55:49 +020013774enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13775{
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013776 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013777
Rob Clark51fd3712013-11-19 12:10:12 -050013778 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013779
Daniel Vetter51ec53d2017-03-01 10:52:24 +010013780 if (!connector->base.state->crtc)
Jesse Barnes752aa882013-10-31 18:55:49 +020013781 return INVALID_PIPE;
13782
Daniel Vetter51ec53d2017-03-01 10:52:24 +010013783 return to_intel_crtc(connector->base.state->crtc)->pipe;
Jesse Barnes752aa882013-10-31 18:55:49 +020013784}
13785
Ville Syrjälä6a20fe72018-02-07 18:48:41 +020013786int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
13787 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013788{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013789 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013790 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013791 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013792
Keith Packard418da172017-03-14 23:25:07 -070013793 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010013794 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013795 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013796
Rob Clark7707e652014-07-17 23:30:04 -040013797 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013798 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013799
Daniel Vetterc05422d2009-08-11 16:05:30 +020013800 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013801}
13802
Daniel Vetter66a92782012-07-12 20:08:18 +020013803static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013804{
Daniel Vetter66a92782012-07-12 20:08:18 +020013805 struct drm_device *dev = encoder->base.dev;
13806 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013807 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013808 int entry = 0;
13809
Damien Lespiaub2784e12014-08-05 11:29:37 +010013810 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013811 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013812 index_mask |= (1 << entry);
13813
Jesse Barnes79e53942008-11-07 14:24:08 -080013814 entry++;
13815 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013816
Jesse Barnes79e53942008-11-07 14:24:08 -080013817 return index_mask;
13818}
13819
Ville Syrjälä646d5772016-10-31 22:37:14 +020013820static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000013821{
Ville Syrjälä646d5772016-10-31 22:37:14 +020013822 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000013823 return false;
13824
13825 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13826 return false;
13827
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010013828 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013829 return false;
13830
13831 return true;
13832}
13833
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013834static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070013835{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013836 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000013837 return false;
13838
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010013839 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013840 return false;
13841
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013842 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013843 return false;
13844
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013845 if (HAS_PCH_LPT_H(dev_priv) &&
13846 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020013847 return false;
13848
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020013849 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013850 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 +020013851 return false;
13852
Ville Syrjäläe4abb732015-12-01 23:31:33 +020013853 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070013854 return false;
13855
13856 return true;
13857}
13858
Imre Deak8090ba82016-08-10 14:07:33 +030013859void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
13860{
13861 int pps_num;
13862 int pps_idx;
13863
13864 if (HAS_DDI(dev_priv))
13865 return;
13866 /*
13867 * This w/a is needed at least on CPT/PPT, but to be sure apply it
13868 * everywhere where registers can be write protected.
13869 */
13870 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13871 pps_num = 2;
13872 else
13873 pps_num = 1;
13874
13875 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
13876 u32 val = I915_READ(PP_CONTROL(pps_idx));
13877
13878 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
13879 I915_WRITE(PP_CONTROL(pps_idx), val);
13880 }
13881}
13882
Imre Deak44cb7342016-08-10 14:07:29 +030013883static void intel_pps_init(struct drm_i915_private *dev_priv)
13884{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020013885 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
Imre Deak44cb7342016-08-10 14:07:29 +030013886 dev_priv->pps_mmio_base = PCH_PPS_BASE;
13887 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13888 dev_priv->pps_mmio_base = VLV_PPS_BASE;
13889 else
13890 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030013891
13892 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030013893}
13894
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013895static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080013896{
Chris Wilson4ef69c72010-09-09 15:14:28 +010013897 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013898 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013899
Imre Deak44cb7342016-08-10 14:07:29 +030013900 intel_pps_init(dev_priv);
13901
Imre Deak97a824e12016-06-21 11:51:47 +030013902 /*
13903 * intel_edp_init_connector() depends on this completing first, to
13904 * prevent the registeration of both eDP and LVDS and the incorrect
13905 * sharing of the PPS.
13906 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013907 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080013908
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013909 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013910 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013911
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020013912 if (IS_GEN9_LP(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053013913 /*
13914 * FIXME: Broxton doesn't support port detection via the
13915 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13916 * detect the ports.
13917 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013918 intel_ddi_init(dev_priv, PORT_A);
13919 intel_ddi_init(dev_priv, PORT_B);
13920 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020013921
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013922 intel_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013923 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013924 int found;
13925
Jesse Barnesde31fac2015-03-06 15:53:32 -080013926 /*
13927 * Haswell uses DDI functions to detect digital outputs.
13928 * On SKL pre-D0 the strap isn't connected, so we assume
13929 * it's there.
13930 */
Ville Syrjälä77179402015-09-18 20:03:35 +030013931 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013932 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080013933 if (found || IS_GEN9_BC(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013934 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013935
Rodrigo Vivi9787e832018-01-29 15:22:22 -080013936 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013937 * register */
13938 found = I915_READ(SFUSE_STRAP);
13939
13940 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013941 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013942 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013943 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013944 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013945 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi9787e832018-01-29 15:22:22 -080013946 if (found & SFUSE_STRAP_DDIF_DETECTED)
13947 intel_ddi_init(dev_priv, PORT_F);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070013948 /*
13949 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
13950 */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080013951 if (IS_GEN9_BC(dev_priv) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070013952 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
13953 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
13954 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013955 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070013956
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010013957 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013958 int found;
Jani Nikula7b91bf72017-08-18 12:30:19 +030013959 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013960
Ville Syrjälä646d5772016-10-31 22:37:14 +020013961 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013962 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013963
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013964 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013965 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013966 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013967 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013968 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013969 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013970 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013971 }
13972
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013973 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013974 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013975
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013976 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013977 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013978
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013979 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013980 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013981
Daniel Vetter270b3042012-10-27 15:52:05 +020013982 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020013983 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013984 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030013985 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010013986
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013987 /*
13988 * The DP_DETECTED bit is the latched state of the DDC
13989 * SDA pin at boot. However since eDP doesn't require DDC
13990 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13991 * eDP ports may have been muxed to an alternate function.
13992 * Thus we can't rely on the DP_DETECTED bit alone to detect
13993 * eDP ports. Consult the VBT as well as DP_DETECTED to
13994 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030013995 *
13996 * Sadly the straps seem to be missing sometimes even for HDMI
13997 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
13998 * and VBT for the presence of the port. Additionally we can't
13999 * trust the port type the VBT declares as we've seen at least
14000 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014001 */
Jani Nikula7b91bf72017-08-18 12:30:19 +030014002 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014003 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14004 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014005 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014006 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014007 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014008
Jani Nikula7b91bf72017-08-18 12:30:19 +030014009 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014010 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14011 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014012 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014013 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014014 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014015
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014016 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014017 /*
14018 * eDP not supported on port D,
14019 * so no need to worry about it
14020 */
14021 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14022 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014023 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014024 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014025 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014026 }
14027
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014028 intel_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014029 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014030 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014031
Paulo Zanonie2debe92013-02-18 19:00:27 -030014032 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014033 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014034 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014035 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014036 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014037 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014038 }
Ma Ling27185ae2009-08-24 13:50:23 +080014039
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014040 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014041 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014042 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014043
14044 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014045
Paulo Zanonie2debe92013-02-18 19:00:27 -030014046 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014047 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014048 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014049 }
Ma Ling27185ae2009-08-24 13:50:23 +080014050
Paulo Zanonie2debe92013-02-18 19:00:27 -030014051 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014052
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014053 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014054 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014055 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014056 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014057 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014058 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014059 }
Ma Ling27185ae2009-08-24 13:50:23 +080014060
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014061 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014062 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014063 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014064 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014065
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000014066 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014067 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014068
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014069 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014070
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014071 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014072 encoder->base.possible_crtcs = encoder->crtc_mask;
14073 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014074 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014075 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014076
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014077 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020014078
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014079 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080014080}
14081
14082static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14083{
14084 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014085
Daniel Vetteref2d6332014-02-10 18:00:38 +010014086 drm_framebuffer_cleanup(fb);
Chris Wilson70001cd2017-02-16 09:46:21 +000014087
Chris Wilsondd689282017-03-01 15:41:28 +000014088 i915_gem_object_lock(intel_fb->obj);
14089 WARN_ON(!intel_fb->obj->framebuffer_references--);
14090 i915_gem_object_unlock(intel_fb->obj);
14091
Chris Wilsonf8c417c2016-07-20 13:31:53 +010014092 i915_gem_object_put(intel_fb->obj);
Chris Wilson70001cd2017-02-16 09:46:21 +000014093
Jesse Barnes79e53942008-11-07 14:24:08 -080014094 kfree(intel_fb);
14095}
14096
14097static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014098 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014099 unsigned int *handle)
14100{
14101 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014102 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014103
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014104 if (obj->userptr.mm) {
14105 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14106 return -EINVAL;
14107 }
14108
Chris Wilson05394f32010-11-08 19:18:58 +000014109 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014110}
14111
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014112static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14113 struct drm_file *file,
14114 unsigned flags, unsigned color,
14115 struct drm_clip_rect *clips,
14116 unsigned num_clips)
14117{
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014118 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014119
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014120 i915_gem_object_flush_if_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +000014121 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014122
14123 return 0;
14124}
14125
Jesse Barnes79e53942008-11-07 14:24:08 -080014126static const struct drm_framebuffer_funcs intel_fb_funcs = {
14127 .destroy = intel_user_framebuffer_destroy,
14128 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014129 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014130};
14131
Damien Lespiaub3218032015-02-27 11:15:18 +000014132static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014133u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14134 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000014135{
Chris Wilson24dbf512017-02-15 10:59:18 +000014136 u32 gen = INTEL_GEN(dev_priv);
Damien Lespiaub3218032015-02-27 11:15:18 +000014137
14138 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014139 int cpp = drm_format_plane_cpp(pixel_format, 0);
14140
Damien Lespiaub3218032015-02-27 11:15:18 +000014141 /* "The stride in bytes must not exceed the of the size of 8K
14142 * pixels and 32K bytes."
14143 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014144 return min(8192 * cpp, 32768);
Ville Syrjälä6401c372017-02-08 19:53:28 +020014145 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014146 return 32*1024;
14147 } else if (gen >= 4) {
14148 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14149 return 16*1024;
14150 else
14151 return 32*1024;
14152 } else if (gen >= 3) {
14153 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14154 return 8*1024;
14155 else
14156 return 16*1024;
14157 } else {
14158 /* XXX DSPC is limited to 4k tiled */
14159 return 8*1024;
14160 }
14161}
14162
Chris Wilson24dbf512017-02-15 10:59:18 +000014163static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14164 struct drm_i915_gem_object *obj,
14165 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014166{
Chris Wilson24dbf512017-02-15 10:59:18 +000014167 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014168 struct drm_framebuffer *fb = &intel_fb->base;
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014169 struct drm_format_name_buf format_name;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014170 u32 pitch_limit;
Chris Wilsondd689282017-03-01 15:41:28 +000014171 unsigned int tiling, stride;
Chris Wilson24dbf512017-02-15 10:59:18 +000014172 int ret = -EINVAL;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014173 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -080014174
Chris Wilsondd689282017-03-01 15:41:28 +000014175 i915_gem_object_lock(obj);
14176 obj->framebuffer_references++;
14177 tiling = i915_gem_object_get_tiling(obj);
14178 stride = i915_gem_object_get_stride(obj);
14179 i915_gem_object_unlock(obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014180
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014181 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014182 /*
14183 * If there's a fence, enforce that
14184 * the fb modifier and tiling mode match.
14185 */
14186 if (tiling != I915_TILING_NONE &&
14187 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014188 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014189 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014190 }
14191 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014192 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014193 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014194 } else if (tiling == I915_TILING_Y) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014195 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014196 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014197 }
14198 }
14199
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014200 /* Passed in modifier sanity checking. */
14201 switch (mode_cmd->modifier[0]) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014202 case I915_FORMAT_MOD_Y_TILED_CCS:
14203 case I915_FORMAT_MOD_Yf_TILED_CCS:
14204 switch (mode_cmd->pixel_format) {
14205 case DRM_FORMAT_XBGR8888:
14206 case DRM_FORMAT_ABGR8888:
14207 case DRM_FORMAT_XRGB8888:
14208 case DRM_FORMAT_ARGB8888:
14209 break;
14210 default:
14211 DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
14212 goto err;
14213 }
14214 /* fall through */
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014215 case I915_FORMAT_MOD_Y_TILED:
14216 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014217 if (INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014218 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14219 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014220 goto err;
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014221 }
Ben Widawsky2f075562017-03-24 14:29:48 -070014222 case DRM_FORMAT_MOD_LINEAR:
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014223 case I915_FORMAT_MOD_X_TILED:
14224 break;
14225 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014226 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14227 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014228 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014229 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014230
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014231 /*
14232 * gen2/3 display engine uses the fence if present,
14233 * so the tiling mode must match the fb modifier exactly.
14234 */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014235 if (INTEL_GEN(dev_priv) < 4 &&
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014236 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014237 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014238 goto err;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014239 }
14240
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014241 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014242 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014243 if (mode_cmd->pitches[0] > pitch_limit) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014244 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
Ben Widawsky2f075562017-03-24 14:29:48 -070014245 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014246 "tiled" : "linear",
14247 mode_cmd->pitches[0], pitch_limit);
Chris Wilson24dbf512017-02-15 10:59:18 +000014248 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014249 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014250
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014251 /*
14252 * If there's a fence, enforce that
14253 * the fb pitch and fence stride match.
14254 */
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014255 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14256 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14257 mode_cmd->pitches[0], stride);
Chris Wilson24dbf512017-02-15 10:59:18 +000014258 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014259 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014260
Ville Syrjälä57779d02012-10-31 17:50:14 +020014261 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014262 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014263 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014264 case DRM_FORMAT_RGB565:
14265 case DRM_FORMAT_XRGB8888:
14266 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014267 break;
14268 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014269 if (INTEL_GEN(dev_priv) > 3) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014270 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14271 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014272 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014273 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014274 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014275 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014276 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014277 INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014278 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14279 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014280 goto err;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014281 }
14282 break;
14283 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014284 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014285 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014286 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014287 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14288 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014289 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014290 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014291 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014292 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014293 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014294 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14295 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014296 goto err;
Damien Lespiau75312082015-05-15 19:06:01 +010014297 }
14298 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014299 case DRM_FORMAT_YUYV:
14300 case DRM_FORMAT_UYVY:
14301 case DRM_FORMAT_YVYU:
14302 case DRM_FORMAT_VYUY:
Ville Syrjäläab330812017-04-21 21:14:32 +030014303 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014304 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14305 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014306 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014307 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014308 break;
Chandra Kondurue44134f2018-05-12 03:03:15 +053014309 case DRM_FORMAT_NV12:
14310 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_Y_TILED_CCS ||
14311 mode_cmd->modifier[0] == I915_FORMAT_MOD_Yf_TILED_CCS) {
14312 DRM_DEBUG_KMS("RC not to be enabled with NV12\n");
14313 goto err;
14314 }
14315 if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
14316 IS_BROXTON(dev_priv)) {
14317 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14318 drm_get_format_name(mode_cmd->pixel_format,
14319 &format_name));
14320 goto err;
14321 }
14322 break;
Chris Wilson57cd6502010-08-08 12:34:44 +010014323 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014324 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14325 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014326 goto err;
Chris Wilson57cd6502010-08-08 12:34:44 +010014327 }
14328
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014329 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14330 if (mode_cmd->offsets[0] != 0)
Chris Wilson24dbf512017-02-15 10:59:18 +000014331 goto err;
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014332
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014333 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014334
Chandra Kondurue44134f2018-05-12 03:03:15 +053014335 if (fb->format->format == DRM_FORMAT_NV12 &&
14336 (fb->width < SKL_MIN_YUV_420_SRC_W ||
14337 fb->height < SKL_MIN_YUV_420_SRC_H ||
14338 (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
14339 DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
14340 return -EINVAL;
14341 }
14342
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014343 for (i = 0; i < fb->format->num_planes; i++) {
14344 u32 stride_alignment;
14345
14346 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
14347 DRM_DEBUG_KMS("bad plane %d handle\n", i);
Christophe JAILLET37875d62017-09-10 10:56:42 +020014348 goto err;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014349 }
14350
14351 stride_alignment = intel_fb_stride_alignment(fb, i);
14352
14353 /*
14354 * Display WA #0531: skl,bxt,kbl,glk
14355 *
14356 * Render decompression and plane width > 3840
14357 * combined with horizontal panning requires the
14358 * plane stride to be a multiple of 4. We'll just
14359 * require the entire fb to accommodate that to avoid
14360 * potential runtime errors at plane configuration time.
14361 */
14362 if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
14363 (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
14364 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
14365 stride_alignment *= 4;
14366
14367 if (fb->pitches[i] & (stride_alignment - 1)) {
14368 DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
14369 i, fb->pitches[i], stride_alignment);
14370 goto err;
14371 }
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014372 }
14373
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014374 intel_fb->obj = obj;
14375
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014376 ret = intel_fill_fb_info(dev_priv, fb);
Ville Syrjälä6687c902015-09-15 13:16:41 +030014377 if (ret)
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014378 goto err;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014379
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014380 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080014381 if (ret) {
14382 DRM_ERROR("framebuffer init failed %d\n", ret);
Chris Wilson24dbf512017-02-15 10:59:18 +000014383 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -080014384 }
14385
Jesse Barnes79e53942008-11-07 14:24:08 -080014386 return 0;
Chris Wilson24dbf512017-02-15 10:59:18 +000014387
14388err:
Chris Wilsondd689282017-03-01 15:41:28 +000014389 i915_gem_object_lock(obj);
14390 obj->framebuffer_references--;
14391 i915_gem_object_unlock(obj);
Chris Wilson24dbf512017-02-15 10:59:18 +000014392 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014393}
14394
Jesse Barnes79e53942008-11-07 14:24:08 -080014395static struct drm_framebuffer *
14396intel_user_framebuffer_create(struct drm_device *dev,
14397 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014398 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014399{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014400 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014401 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014402 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014403
Chris Wilson03ac0642016-07-20 13:31:51 +010014404 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14405 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014406 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014407
Chris Wilson24dbf512017-02-15 10:59:18 +000014408 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014409 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010014410 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014411
14412 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014413}
14414
Chris Wilson778e23a2016-12-05 14:29:39 +000014415static void intel_atomic_state_free(struct drm_atomic_state *state)
14416{
14417 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14418
14419 drm_atomic_state_default_release(state);
14420
14421 i915_sw_fence_fini(&intel_state->commit_ready);
14422
14423 kfree(state);
14424}
14425
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014426static enum drm_mode_status
14427intel_mode_valid(struct drm_device *dev,
14428 const struct drm_display_mode *mode)
14429{
14430 if (mode->vscan > 1)
14431 return MODE_NO_VSCAN;
14432
14433 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
14434 return MODE_NO_DBLESCAN;
14435
14436 if (mode->flags & DRM_MODE_FLAG_HSKEW)
14437 return MODE_H_ILLEGAL;
14438
14439 if (mode->flags & (DRM_MODE_FLAG_CSYNC |
14440 DRM_MODE_FLAG_NCSYNC |
14441 DRM_MODE_FLAG_PCSYNC))
14442 return MODE_HSYNC;
14443
14444 if (mode->flags & (DRM_MODE_FLAG_BCAST |
14445 DRM_MODE_FLAG_PIXMUX |
14446 DRM_MODE_FLAG_CLKDIV2))
14447 return MODE_BAD;
14448
14449 return MODE_OK;
14450}
14451
Jesse Barnes79e53942008-11-07 14:24:08 -080014452static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014453 .fb_create = intel_user_framebuffer_create,
Ville Syrjäläbbfb6ce2017-08-01 09:58:12 -070014454 .get_format_info = intel_get_format_info,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014455 .output_poll_changed = intel_fbdev_output_poll_changed,
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014456 .mode_valid = intel_mode_valid,
Matt Roper5ee67f12015-01-21 16:35:44 -080014457 .atomic_check = intel_atomic_check,
14458 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014459 .atomic_state_alloc = intel_atomic_state_alloc,
14460 .atomic_state_clear = intel_atomic_state_clear,
Chris Wilson778e23a2016-12-05 14:29:39 +000014461 .atomic_state_free = intel_atomic_state_free,
Jesse Barnes79e53942008-11-07 14:24:08 -080014462};
14463
Imre Deak88212942016-03-16 13:38:53 +020014464/**
14465 * intel_init_display_hooks - initialize the display modesetting hooks
14466 * @dev_priv: device private
14467 */
14468void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014469{
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +020014470 intel_init_cdclk_hooks(dev_priv);
14471
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014472 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014473 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014474 dev_priv->display.get_initial_plane_config =
14475 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014476 dev_priv->display.crtc_compute_clock =
14477 haswell_crtc_compute_clock;
14478 dev_priv->display.crtc_enable = haswell_crtc_enable;
14479 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014480 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014481 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014482 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014483 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014484 dev_priv->display.crtc_compute_clock =
14485 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014486 dev_priv->display.crtc_enable = haswell_crtc_enable;
14487 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014488 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014489 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014490 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014491 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014492 dev_priv->display.crtc_compute_clock =
14493 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014494 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14495 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014496 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014497 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014498 dev_priv->display.get_initial_plane_config =
14499 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014500 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14501 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14502 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14503 } else if (IS_VALLEYVIEW(dev_priv)) {
14504 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14505 dev_priv->display.get_initial_plane_config =
14506 i9xx_get_initial_plane_config;
14507 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014508 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14509 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014510 } else if (IS_G4X(dev_priv)) {
14511 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14512 dev_priv->display.get_initial_plane_config =
14513 i9xx_get_initial_plane_config;
14514 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14515 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14516 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014517 } else if (IS_PINEVIEW(dev_priv)) {
14518 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14519 dev_priv->display.get_initial_plane_config =
14520 i9xx_get_initial_plane_config;
14521 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14522 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14523 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014524 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014525 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014526 dev_priv->display.get_initial_plane_config =
14527 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014528 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014529 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14530 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014531 } else {
14532 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14533 dev_priv->display.get_initial_plane_config =
14534 i9xx_get_initial_plane_config;
14535 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14536 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14537 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014538 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014539
Imre Deak88212942016-03-16 13:38:53 +020014540 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014541 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014542 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014543 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014544 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014545 /* FIXME: detect B0+ stepping and use auto training */
14546 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014547 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014548 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014549 }
14550
Rodrigo Vivibd30ca22017-09-26 14:13:46 -070014551 if (INTEL_GEN(dev_priv) >= 9)
Lyude27082492016-08-24 07:48:10 +020014552 dev_priv->display.update_crtcs = skl_update_crtcs;
14553 else
14554 dev_priv->display.update_crtcs = intel_update_crtcs;
Jesse Barnese70236a2009-09-21 10:42:27 -070014555}
14556
Jesse Barnesb690e962010-07-19 13:53:12 -070014557/*
Keith Packard435793d2011-07-12 14:56:22 -070014558 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14559 */
14560static void quirk_ssc_force_disable(struct drm_device *dev)
14561{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014562 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070014563 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014564 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014565}
14566
Carsten Emde4dca20e2012-03-15 15:56:26 +010014567/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014568 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14569 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014570 */
14571static void quirk_invert_brightness(struct drm_device *dev)
14572{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014573 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010014574 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014575 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014576}
14577
Scot Doyle9c72cc62014-07-03 23:27:50 +000014578/* Some VBT's incorrectly indicate no backlight is present */
14579static void quirk_backlight_present(struct drm_device *dev)
14580{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014581 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000014582 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14583 DRM_INFO("applying backlight present quirk\n");
14584}
14585
Manasi Navarec99a2592017-06-30 09:33:48 -070014586/* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
14587 * which is 300 ms greater than eDP spec T12 min.
14588 */
14589static void quirk_increase_t12_delay(struct drm_device *dev)
14590{
14591 struct drm_i915_private *dev_priv = to_i915(dev);
14592
14593 dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
14594 DRM_INFO("Applying T12 delay quirk\n");
14595}
14596
Jesse Barnesb690e962010-07-19 13:53:12 -070014597struct intel_quirk {
14598 int device;
14599 int subsystem_vendor;
14600 int subsystem_device;
14601 void (*hook)(struct drm_device *dev);
14602};
14603
Egbert Eich5f85f172012-10-14 15:46:38 +020014604/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14605struct intel_dmi_quirk {
14606 void (*hook)(struct drm_device *dev);
14607 const struct dmi_system_id (*dmi_id_list)[];
14608};
14609
14610static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14611{
14612 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14613 return 1;
14614}
14615
14616static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14617 {
14618 .dmi_id_list = &(const struct dmi_system_id[]) {
14619 {
14620 .callback = intel_dmi_reverse_brightness,
14621 .ident = "NCR Corporation",
14622 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14623 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14624 },
14625 },
14626 { } /* terminating entry */
14627 },
14628 .hook = quirk_invert_brightness,
14629 },
14630};
14631
Ben Widawskyc43b5632012-04-16 14:07:40 -070014632static struct intel_quirk intel_quirks[] = {
Keith Packard435793d2011-07-12 14:56:22 -070014633 /* Lenovo U160 cannot use SSC on LVDS */
14634 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014635
14636 /* Sony Vaio Y cannot use SSC on LVDS */
14637 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014638
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014639 /* Acer Aspire 5734Z must invert backlight brightness */
14640 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14641
14642 /* Acer/eMachines G725 */
14643 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14644
14645 /* Acer/eMachines e725 */
14646 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14647
14648 /* Acer/Packard Bell NCL20 */
14649 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14650
14651 /* Acer Aspire 4736Z */
14652 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014653
14654 /* Acer Aspire 5336 */
14655 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014656
14657 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14658 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014659
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014660 /* Acer C720 Chromebook (Core i3 4005U) */
14661 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14662
jens steinb2a96012014-10-28 20:25:53 +010014663 /* Apple Macbook 2,1 (Core 2 T7400) */
14664 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14665
Jani Nikula1b9448b2015-11-05 11:49:59 +020014666 /* Apple Macbook 4,1 */
14667 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14668
Scot Doyled4967d82014-07-03 23:27:52 +000014669 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14670 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014671
14672 /* HP Chromebook 14 (Celeron 2955U) */
14673 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014674
14675 /* Dell Chromebook 11 */
14676 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020014677
14678 /* Dell Chromebook 11 (2015 version) */
14679 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Manasi Navarec99a2592017-06-30 09:33:48 -070014680
14681 /* Toshiba Satellite P50-C-18C */
14682 { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
Jesse Barnesb690e962010-07-19 13:53:12 -070014683};
14684
14685static void intel_init_quirks(struct drm_device *dev)
14686{
14687 struct pci_dev *d = dev->pdev;
14688 int i;
14689
14690 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14691 struct intel_quirk *q = &intel_quirks[i];
14692
14693 if (d->device == q->device &&
14694 (d->subsystem_vendor == q->subsystem_vendor ||
14695 q->subsystem_vendor == PCI_ANY_ID) &&
14696 (d->subsystem_device == q->subsystem_device ||
14697 q->subsystem_device == PCI_ANY_ID))
14698 q->hook(dev);
14699 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014700 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14701 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14702 intel_dmi_quirks[i].hook(dev);
14703 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014704}
14705
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014706/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014707static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014708{
David Weinehall52a05c32016-08-22 13:32:44 +030014709 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014710 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014711 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014712
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014713 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030014714 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014715 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014716 sr1 = inb(VGA_SR_DATA);
14717 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030014718 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014719 udelay(300);
14720
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014721 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014722 POSTING_READ(vga_reg);
14723}
14724
Daniel Vetterf8175862012-04-10 15:50:11 +020014725void intel_modeset_init_hw(struct drm_device *dev)
14726{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014727 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014728
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014729 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +030014730 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020014731 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
Daniel Vetterf8175862012-04-10 15:50:11 +020014732}
14733
Matt Roperd93c0372015-12-03 11:37:41 -080014734/*
14735 * Calculate what we think the watermarks should be for the state we've read
14736 * out of the hardware and then immediately program those watermarks so that
14737 * we ensure the hardware settings match our internal state.
14738 *
14739 * We can calculate what we think WM's should be by creating a duplicate of the
14740 * current state (which was constructed during hardware readout) and running it
14741 * through the atomic check code to calculate new watermark values in the
14742 * state object.
14743 */
14744static void sanitize_watermarks(struct drm_device *dev)
14745{
14746 struct drm_i915_private *dev_priv = to_i915(dev);
14747 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014748 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014749 struct drm_crtc *crtc;
14750 struct drm_crtc_state *cstate;
14751 struct drm_modeset_acquire_ctx ctx;
14752 int ret;
14753 int i;
14754
14755 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080014756 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080014757 return;
14758
14759 /*
14760 * We need to hold connection_mutex before calling duplicate_state so
14761 * that the connector loop is protected.
14762 */
14763 drm_modeset_acquire_init(&ctx, 0);
14764retry:
Matt Roper0cd12622016-01-12 07:13:37 -080014765 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080014766 if (ret == -EDEADLK) {
14767 drm_modeset_backoff(&ctx);
14768 goto retry;
14769 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080014770 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014771 }
14772
14773 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14774 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080014775 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014776
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014777 intel_state = to_intel_atomic_state(state);
14778
Matt Ropered4a6a72016-02-23 17:20:13 -080014779 /*
14780 * Hardware readout is the only time we don't want to calculate
14781 * intermediate watermarks (since we don't trust the current
14782 * watermarks).
14783 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020014784 if (!HAS_GMCH_DISPLAY(dev_priv))
14785 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080014786
Matt Roperd93c0372015-12-03 11:37:41 -080014787 ret = intel_atomic_check(dev, state);
14788 if (ret) {
14789 /*
14790 * If we fail here, it means that the hardware appears to be
14791 * programmed in a way that shouldn't be possible, given our
14792 * understanding of watermark requirements. This might mean a
14793 * mistake in the hardware readout code or a mistake in the
14794 * watermark calculations for a given platform. Raise a WARN
14795 * so that this is noticeable.
14796 *
14797 * If this actually happens, we'll have to just leave the
14798 * BIOS-programmed watermarks untouched and hope for the best.
14799 */
14800 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014801 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014802 }
14803
14804 /* Write calculated watermark values back */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010014805 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roperd93c0372015-12-03 11:37:41 -080014806 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14807
Matt Ropered4a6a72016-02-23 17:20:13 -080014808 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014809 dev_priv->display.optimize_watermarks(intel_state, cs);
Maarten Lankhorst556fe362017-11-10 12:34:53 +010014810
14811 to_intel_crtc_state(crtc->state)->wm = cs->wm;
Matt Roperd93c0372015-12-03 11:37:41 -080014812 }
14813
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014814put_state:
Chris Wilson08536952016-10-14 13:18:18 +010014815 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080014816fail:
Matt Roperd93c0372015-12-03 11:37:41 -080014817 drm_modeset_drop_locks(&ctx);
14818 drm_modeset_acquire_fini(&ctx);
14819}
14820
Chris Wilson58ecd9d2017-11-05 13:49:05 +000014821static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
14822{
14823 if (IS_GEN5(dev_priv)) {
14824 u32 fdi_pll_clk =
14825 I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
14826
14827 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
14828 } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
14829 dev_priv->fdi_pll_freq = 270000;
14830 } else {
14831 return;
14832 }
14833
14834 DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
14835}
14836
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014837int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080014838{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030014839 struct drm_i915_private *dev_priv = to_i915(dev);
14840 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014841 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014842 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014843
Ville Syrjälä757fffc2017-11-13 15:36:22 +020014844 dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
14845
Jesse Barnes79e53942008-11-07 14:24:08 -080014846 drm_mode_config_init(dev);
14847
14848 dev->mode_config.min_width = 0;
14849 dev->mode_config.min_height = 0;
14850
Dave Airlie019d96c2011-09-29 16:20:42 +010014851 dev->mode_config.preferred_depth = 24;
14852 dev->mode_config.prefer_shadow = 1;
14853
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014854 dev->mode_config.allow_fb_modifiers = true;
14855
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014856 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014857
Andrea Arcangeli400c19d2017-04-07 01:23:45 +020014858 init_llist_head(&dev_priv->atomic_helper.free_list);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000014859 INIT_WORK(&dev_priv->atomic_helper.free_work,
Chris Wilsonba318c62017-02-02 20:47:41 +000014860 intel_atomic_helper_free_state_worker);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000014861
Jesse Barnesb690e962010-07-19 13:53:12 -070014862 intel_init_quirks(dev);
14863
Ville Syrjälä62d75df2016-10-31 22:37:25 +020014864 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014865
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014866 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014867 return 0;
Ben Widawskye3c74752013-04-05 13:12:39 -070014868
Lukas Wunner69f92f62015-07-15 13:57:35 +020014869 /*
14870 * There may be no VBT; and if the BIOS enabled SSC we can
14871 * just keep using it to avoid unnecessary flicker. Whereas if the
14872 * BIOS isn't using it, don't assume it will work even if the VBT
14873 * indicates as much.
14874 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014875 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020014876 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14877 DREF_SSC1_ENABLE);
14878
14879 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14880 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14881 bios_lvds_use_ssc ? "en" : "dis",
14882 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14883 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14884 }
14885 }
14886
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014887 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014888 dev->mode_config.max_width = 2048;
14889 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014890 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014891 dev->mode_config.max_width = 4096;
14892 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014893 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014894 dev->mode_config.max_width = 8192;
14895 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014896 }
Damien Lespiau068be562014-03-28 14:17:49 +000014897
Jani Nikula2a307c22016-11-30 17:43:04 +020014898 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
14899 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014900 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014901 } else if (IS_GEN2(dev_priv)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014902 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14903 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14904 } else {
14905 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14906 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14907 }
14908
Matthew Auld73ebd502017-12-11 15:18:20 +000014909 dev->mode_config.fb_base = ggtt->gmadr.start;
Jesse Barnes79e53942008-11-07 14:24:08 -080014910
Zhao Yakui28c97732009-10-09 11:39:41 +080014911 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014912 INTEL_INFO(dev_priv)->num_pipes,
14913 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014914
Damien Lespiau055e3932014-08-18 13:49:10 +010014915 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014916 int ret;
14917
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020014918 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014919 if (ret) {
14920 drm_mode_config_cleanup(dev);
14921 return ret;
14922 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014923 }
14924
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014925 intel_shared_dpll_init(dev);
Chris Wilson58ecd9d2017-11-05 13:49:05 +000014926 intel_update_fdi_pll_freq(dev_priv);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014927
Ville Syrjälä5be6e332017-02-20 16:04:43 +020014928 intel_update_czclk(dev_priv);
14929 intel_modeset_init_hw(dev);
14930
Ville Syrjäläb2045352016-05-13 23:41:27 +030014931 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014932 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030014933
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014934 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014935 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014936 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000014937
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014938 drm_modeset_lock_all(dev);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030014939 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014940 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014941
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014942 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014943 struct intel_initial_plane_config plane_config = {};
14944
Jesse Barnes46f297f2014-03-07 08:57:48 -080014945 if (!crtc->active)
14946 continue;
14947
Jesse Barnes46f297f2014-03-07 08:57:48 -080014948 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014949 * Note that reserving the BIOS fb up front prevents us
14950 * from stuffing other stolen allocations like the ring
14951 * on top. This prevents some ugliness at boot time, and
14952 * can even allow for smooth boot transitions if the BIOS
14953 * fb is large enough for the active pipe configuration.
14954 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014955 dev_priv->display.get_initial_plane_config(crtc,
14956 &plane_config);
14957
14958 /*
14959 * If the fb is shared between multiple heads, we'll
14960 * just get the first one.
14961 */
14962 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014963 }
Matt Roperd93c0372015-12-03 11:37:41 -080014964
14965 /*
14966 * Make sure hardware watermarks really match the state we read out.
14967 * Note that we need to do this after reconstructing the BIOS fb's
14968 * since the watermark calculation done here will use pstate->fb.
14969 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020014970 if (!HAS_GMCH_DISPLAY(dev_priv))
14971 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014972
14973 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010014974}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014975
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030014976void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
14977{
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020014978 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030014979 /* 640x480@60Hz, ~25175 kHz */
14980 struct dpll clock = {
14981 .m1 = 18,
14982 .m2 = 7,
14983 .p1 = 13,
14984 .p2 = 4,
14985 .n = 2,
14986 };
14987 u32 dpll, fp;
14988 int i;
14989
14990 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
14991
14992 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
14993 pipe_name(pipe), clock.vco, clock.dot);
14994
14995 fp = i9xx_dpll_compute_fp(&clock);
14996 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
14997 DPLL_VGA_MODE_DIS |
14998 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
14999 PLL_P2_DIVIDE_BY_4 |
15000 PLL_REF_INPUT_DREFCLK |
15001 DPLL_VCO_ENABLE;
15002
15003 I915_WRITE(FP0(pipe), fp);
15004 I915_WRITE(FP1(pipe), fp);
15005
15006 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
15007 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
15008 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
15009 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
15010 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
15011 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
15012 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
15013
15014 /*
15015 * Apparently we need to have VGA mode enabled prior to changing
15016 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
15017 * dividers, even though the register value does change.
15018 */
15019 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
15020 I915_WRITE(DPLL(pipe), dpll);
15021
15022 /* Wait for the clocks to stabilize. */
15023 POSTING_READ(DPLL(pipe));
15024 udelay(150);
15025
15026 /* The pixel multiplier can only be updated once the
15027 * DPLL is enabled and the clocks are stable.
15028 *
15029 * So write it again.
15030 */
15031 I915_WRITE(DPLL(pipe), dpll);
15032
15033 /* We do this three times for luck */
15034 for (i = 0; i < 3 ; i++) {
15035 I915_WRITE(DPLL(pipe), dpll);
15036 POSTING_READ(DPLL(pipe));
15037 udelay(150); /* wait for warmup */
15038 }
15039
15040 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
15041 POSTING_READ(PIPECONF(pipe));
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020015042
15043 intel_wait_for_pipe_scanline_moving(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015044}
15045
15046void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15047{
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015048 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15049
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015050 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
15051 pipe_name(pipe));
15052
Ville Syrjälä5816d9c2017-11-29 14:54:11 +020015053 WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
15054 WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
15055 WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
15056 WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
15057 WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015058
15059 I915_WRITE(PIPECONF(pipe), 0);
15060 POSTING_READ(PIPECONF(pipe));
15061
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015062 intel_wait_for_pipe_scanline_stopped(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015063
15064 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
15065 POSTING_READ(DPLL(pipe));
15066}
15067
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015068static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
Ville Syrjäläed150302017-11-17 21:19:10 +020015069 struct intel_plane *plane)
Daniel Vetterfa555832012-10-10 23:14:00 +020015070{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015071 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläed150302017-11-17 21:19:10 +020015072 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
15073 u32 val = I915_READ(DSPCNTR(i9xx_plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015074
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015075 return (val & DISPLAY_PLANE_ENABLE) == 0 ||
15076 (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
15077}
Daniel Vetterfa555832012-10-10 23:14:00 +020015078
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015079static void
15080intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
15081{
15082 struct intel_crtc *crtc;
Daniel Vetterfa555832012-10-10 23:14:00 +020015083
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015084 if (INTEL_GEN(dev_priv) >= 4)
15085 return;
Daniel Vetterfa555832012-10-10 23:14:00 +020015086
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015087 for_each_intel_crtc(&dev_priv->drm, crtc) {
15088 struct intel_plane *plane =
15089 to_intel_plane(crtc->base.primary);
15090
15091 if (intel_plane_mapping_ok(crtc, plane))
15092 continue;
15093
15094 DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
15095 plane->base.name);
15096 intel_plane_disable_noatomic(crtc, plane);
15097 }
Daniel Vetterfa555832012-10-10 23:14:00 +020015098}
15099
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015100static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15101{
15102 struct drm_device *dev = crtc->base.dev;
15103 struct intel_encoder *encoder;
15104
15105 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15106 return true;
15107
15108 return false;
15109}
15110
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015111static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15112{
15113 struct drm_device *dev = encoder->base.dev;
15114 struct intel_connector *connector;
15115
15116 for_each_connector_on_encoder(dev, &encoder->base, connector)
15117 return connector;
15118
15119 return NULL;
15120}
15121
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015122static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015123 enum pipe pch_transcoder)
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015124{
15125 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015126 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015127}
15128
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015129static void intel_sanitize_crtc(struct intel_crtc *crtc,
15130 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter24929352012-07-02 20:28:59 +020015131{
15132 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015133 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015134 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015135
Daniel Vetter24929352012-07-02 20:28:59 +020015136 /* Clear any frame start delays used for debugging left by the BIOS */
Ville Syrjälä738a8142017-11-15 22:04:42 +020015137 if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020015138 i915_reg_t reg = PIPECONF(cpu_transcoder);
15139
15140 I915_WRITE(reg,
15141 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15142 }
Daniel Vetter24929352012-07-02 20:28:59 +020015143
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015144 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015145 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015146 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015147 struct intel_plane *plane;
15148
Daniel Vetter96256042015-02-13 21:03:42 +010015149 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015150
15151 /* Disable everything but the primary plane */
15152 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015153 const struct intel_plane_state *plane_state =
15154 to_intel_plane_state(plane->base.state);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015155
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015156 if (plane_state->base.visible &&
15157 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
15158 intel_plane_disable_noatomic(crtc, plane);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015159 }
Daniel Vetter96256042015-02-13 21:03:42 +010015160 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015161
Daniel Vetter24929352012-07-02 20:28:59 +020015162 /* Adjust the state of the output pipe according to whether we
15163 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015164 if (crtc->active && !intel_crtc_has_encoders(crtc))
Ville Syrjäläda1d0e22017-06-01 17:36:14 +030015165 intel_crtc_disable_noatomic(&crtc->base, ctx);
Daniel Vetter24929352012-07-02 20:28:59 +020015166
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010015167 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015168 /*
15169 * We start out with underrun reporting disabled to avoid races.
15170 * For correct bookkeeping mark this on active crtcs.
15171 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015172 * Also on gmch platforms we dont have any hardware bits to
15173 * disable the underrun reporting. Which means we need to start
15174 * out with underrun reporting disabled also on inactive pipes,
15175 * since otherwise we'll complain about the garbage we read when
15176 * e.g. coming up after runtime pm.
15177 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015178 * No protection against concurrent access is required - at
15179 * worst a fifo underrun happens which also sets this to false.
15180 */
15181 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015182 /*
15183 * We track the PCH trancoder underrun reporting state
15184 * within the crtc. With crtc for pipe A housing the underrun
15185 * reporting state for PCH transcoder A, crtc for pipe B housing
15186 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15187 * and marking underrun reporting as disabled for the non-existing
15188 * PCH transcoders B and C would prevent enabling the south
15189 * error interrupt (see cpt_can_enable_serr_int()).
15190 */
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015191 if (has_pch_trancoder(dev_priv, crtc->pipe))
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015192 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010015193 }
Daniel Vetter24929352012-07-02 20:28:59 +020015194}
15195
15196static void intel_sanitize_encoder(struct intel_encoder *encoder)
15197{
15198 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020015199
15200 /* We need to check both for a crtc link (meaning that the
15201 * encoder is active and trying to read from a pipe) and the
15202 * pipe itself being active. */
15203 bool has_active_crtc = encoder->base.crtc &&
15204 to_intel_crtc(encoder->base.crtc)->active;
15205
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015206 connector = intel_encoder_find_connector(encoder);
15207 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015208 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15209 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015210 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015211
15212 /* Connector is active, but has no active pipe. This is
15213 * fallout from our resume register restoring. Disable
15214 * the encoder manually again. */
15215 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015216 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15217
Daniel Vetter24929352012-07-02 20:28:59 +020015218 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15219 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015220 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015221 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015222 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015223 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020015224 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015225 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015226
15227 /* Inconsistent output/port/pipe state happens presumably due to
15228 * a bug in one of the get_hw_state functions. Or someplace else
15229 * in our code, like the register restore mess on resume. Clamp
15230 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015231
15232 connector->base.dpms = DRM_MODE_DPMS_OFF;
15233 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015234 }
Daniel Vetter24929352012-07-02 20:28:59 +020015235}
15236
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015237void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015238{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015239 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015240
Imre Deak04098752014-02-18 00:02:16 +020015241 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15242 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015243 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020015244 }
15245}
15246
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015247void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020015248{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015249 /* This function can be called both from intel_modeset_setup_hw_state or
15250 * at a very early point in our resume sequence, where the power well
15251 * structures are not yet restored. Since this function is at a very
15252 * paranoid "someone might have enabled VGA while we were not looking"
15253 * level, just check if the power well is enabled instead of trying to
15254 * follow the "don't touch the power well if we don't need it" policy
15255 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015256 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015257 return;
15258
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015259 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020015260
15261 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015262}
15263
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015264/* FIXME read out full plane state for all planes */
15265static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015266{
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015267 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15268 struct intel_crtc_state *crtc_state =
15269 to_intel_crtc_state(crtc->base.state);
15270 struct intel_plane *plane;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015271
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015272 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
15273 struct intel_plane_state *plane_state =
15274 to_intel_plane_state(plane->base.state);
15275 bool visible = plane->get_hw_state(plane);
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015276
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015277 intel_set_plane_visible(crtc_state, plane_state, visible);
15278 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015279}
15280
Daniel Vetter30e984d2013-06-05 13:34:17 +020015281static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015282{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015283 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015284 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015285 struct intel_crtc *crtc;
15286 struct intel_encoder *encoder;
15287 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015288 struct drm_connector_list_iter conn_iter;
Daniel Vetter53589012013-06-05 13:34:16 +020015289 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015290
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015291 dev_priv->active_crtcs = 0;
15292
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015293 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015294 struct intel_crtc_state *crtc_state =
15295 to_intel_crtc_state(crtc->base.state);
Daniel Vetter3b117c82013-04-17 20:15:07 +020015296
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015297 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015298 memset(crtc_state, 0, sizeof(*crtc_state));
15299 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015300
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015301 crtc_state->base.active = crtc_state->base.enable =
15302 dev_priv->display.get_pipe_config(crtc, crtc_state);
15303
15304 crtc->base.enabled = crtc_state->base.enable;
15305 crtc->active = crtc_state->base.active;
15306
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015307 if (crtc_state->base.active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015308 dev_priv->active_crtcs |= 1 << crtc->pipe;
15309
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015310 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015311
Ville Syrjälä78108b72016-05-27 20:59:19 +030015312 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15313 crtc->base.base.id, crtc->base.name,
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015314 enableddisabled(crtc_state->base.active));
Daniel Vetter24929352012-07-02 20:28:59 +020015315 }
15316
Daniel Vetter53589012013-06-05 13:34:16 +020015317 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15318 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15319
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015320 pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
15321 &pll->state.hw_state);
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015322 pll->state.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015323 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015324 struct intel_crtc_state *crtc_state =
15325 to_intel_crtc_state(crtc->base.state);
15326
15327 if (crtc_state->base.active &&
15328 crtc_state->shared_dpll == pll)
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015329 pll->state.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015330 }
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015331 pll->active_mask = pll->state.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015332
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015333 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015334 pll->info->name, pll->state.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015335 }
15336
Damien Lespiaub2784e12014-08-05 11:29:37 +010015337 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015338 pipe = 0;
15339
15340 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015341 struct intel_crtc_state *crtc_state;
15342
Ville Syrjälä98187832016-10-31 22:37:10 +020015343 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015344 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015345
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015346 encoder->base.crtc = &crtc->base;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015347 encoder->get_config(encoder, crtc_state);
Daniel Vetter24929352012-07-02 20:28:59 +020015348 } else {
15349 encoder->base.crtc = NULL;
15350 }
15351
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015352 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015353 encoder->base.base.id, encoder->base.name,
15354 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015355 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015356 }
15357
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015358 drm_connector_list_iter_begin(dev, &conn_iter);
15359 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter24929352012-07-02 20:28:59 +020015360 if (connector->get_hw_state(connector)) {
15361 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015362
15363 encoder = connector->encoder;
15364 connector->base.encoder = &encoder->base;
15365
15366 if (encoder->base.crtc &&
15367 encoder->base.crtc->state->active) {
15368 /*
15369 * This has to be done during hardware readout
15370 * because anything calling .crtc_disable may
15371 * rely on the connector_mask being accurate.
15372 */
15373 encoder->base.crtc->state->connector_mask |=
15374 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015375 encoder->base.crtc->state->encoder_mask |=
15376 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015377 }
15378
Daniel Vetter24929352012-07-02 20:28:59 +020015379 } else {
15380 connector->base.dpms = DRM_MODE_DPMS_OFF;
15381 connector->base.encoder = NULL;
15382 }
15383 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015384 connector->base.base.id, connector->base.name,
15385 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020015386 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015387 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015388
15389 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015390 struct intel_crtc_state *crtc_state =
15391 to_intel_crtc_state(crtc->base.state);
Ville Syrjäläd305e062017-08-30 21:57:03 +030015392 int min_cdclk = 0;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015393
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015394 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015395 if (crtc_state->base.active) {
15396 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
Ville Syrjäläbd4cd032018-04-26 19:30:15 +030015397 crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
15398 crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015399 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015400 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15401
15402 /*
15403 * The initial mode needs to be set in order to keep
15404 * the atomic core happy. It wants a valid mode if the
15405 * crtc's enabled, so we do the above call.
15406 *
Daniel Vetter7800fb62016-12-19 09:24:23 +010015407 * But we don't set all the derived state fully, hence
15408 * set a flag to indicate that a full recalculation is
15409 * needed on the next commit.
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015410 */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015411 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015412
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020015413 intel_crtc_compute_pixel_rate(crtc_state);
15414
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015415 if (dev_priv->display.modeset_calc_cdclk) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030015416 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015417 if (WARN_ON(min_cdclk < 0))
15418 min_cdclk = 0;
15419 }
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015420
Daniel Vetter5caa0fe2017-05-09 16:03:29 +020015421 drm_calc_timestamping_constants(&crtc->base,
15422 &crtc_state->base.adjusted_mode);
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015423 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015424 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015425
Ville Syrjäläd305e062017-08-30 21:57:03 +030015426 dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030015427 dev_priv->min_voltage_level[crtc->pipe] =
15428 crtc_state->min_voltage_level;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015429
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015430 intel_pipe_config_sanity_check(dev_priv, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015431 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015432}
15433
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015434static void
15435get_encoder_power_domains(struct drm_i915_private *dev_priv)
15436{
15437 struct intel_encoder *encoder;
15438
15439 for_each_intel_encoder(&dev_priv->drm, encoder) {
15440 u64 get_domains;
15441 enum intel_display_power_domain domain;
15442
15443 if (!encoder->get_power_domains)
15444 continue;
15445
15446 get_domains = encoder->get_power_domains(encoder);
15447 for_each_power_domain(domain, get_domains)
15448 intel_display_power_get(dev_priv, domain);
15449 }
15450}
15451
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015452static void intel_early_display_was(struct drm_i915_private *dev_priv)
15453{
15454 /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
15455 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
15456 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
15457 DARBF_GATING_DIS);
15458
15459 if (IS_HASWELL(dev_priv)) {
15460 /*
15461 * WaRsPkgCStateDisplayPMReq:hsw
15462 * System hang if this isn't done before disabling all planes!
15463 */
15464 I915_WRITE(CHICKEN_PAR1_1,
15465 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
15466 }
15467}
15468
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015469/* Scan out the current hw modeset state,
15470 * and sanitizes it to the current state
15471 */
15472static void
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015473intel_modeset_setup_hw_state(struct drm_device *dev,
15474 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015475{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015476 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015477 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015478 struct intel_crtc *crtc;
15479 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015480 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015481
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015482 intel_early_display_was(dev_priv);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015483 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015484
15485 /* HW state is read out, now we need to sanitize this mess. */
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015486 get_encoder_power_domains(dev_priv);
15487
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015488 intel_sanitize_plane_mapping(dev_priv);
15489
Damien Lespiaub2784e12014-08-05 11:29:37 +010015490 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015491 intel_sanitize_encoder(encoder);
15492 }
15493
Damien Lespiau055e3932014-08-18 13:49:10 +010015494 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020015495 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015496
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015497 intel_sanitize_crtc(crtc, ctx);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015498 intel_dump_pipe_config(crtc, crtc->config,
15499 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015500 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015501
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015502 intel_modeset_update_connector_atomic_state(dev);
15503
Daniel Vetter35c95372013-07-17 06:55:04 +020015504 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15505 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15506
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015507 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015508 continue;
15509
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015510 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
15511 pll->info->name);
Daniel Vetter35c95372013-07-17 06:55:04 +020015512
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015513 pll->info->funcs->disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015514 pll->on = false;
15515 }
15516
Ville Syrjälä04548cb2017-04-21 21:14:29 +030015517 if (IS_G4X(dev_priv)) {
15518 g4x_wm_get_hw_state(dev);
15519 g4x_wm_sanitize(dev_priv);
15520 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015521 vlv_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015522 vlv_wm_sanitize(dev_priv);
Rodrigo Vivia029fa42017-08-09 13:52:48 -070015523 } else if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat30789992014-11-04 17:06:45 +000015524 skl_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015525 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015526 ilk_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015527 }
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015528
15529 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020015530 u64 put_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015531
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015532 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015533 if (WARN_ON(put_domains))
15534 modeset_put_power_domains(dev_priv, put_domains);
15535 }
15536 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015537
Imre Deak8d8c3862017-02-17 17:39:46 +020015538 intel_power_domains_verify_state(dev_priv);
15539
Paulo Zanoni010cf732016-01-19 11:35:48 -020015540 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015541}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015542
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015543void intel_display_resume(struct drm_device *dev)
15544{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015545 struct drm_i915_private *dev_priv = to_i915(dev);
15546 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15547 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015548 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015549
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015550 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030015551 if (state)
15552 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015553
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015554 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015555
Maarten Lankhorst73974892016-08-05 23:28:27 +030015556 while (1) {
15557 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15558 if (ret != -EDEADLK)
15559 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015560
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015561 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015562 }
15563
Maarten Lankhorst73974892016-08-05 23:28:27 +030015564 if (!ret)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010015565 ret = __intel_display_resume(dev, state, &ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +030015566
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +053015567 intel_enable_ipc(dev_priv);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015568 drm_modeset_drop_locks(&ctx);
15569 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015570
Chris Wilson08536952016-10-14 13:18:18 +010015571 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015572 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson3c5e37f2017-01-15 12:58:25 +000015573 if (state)
15574 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015575}
15576
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010015577int intel_connector_register(struct drm_connector *connector)
15578{
15579 struct intel_connector *intel_connector = to_intel_connector(connector);
15580 int ret;
15581
15582 ret = intel_backlight_device_register(intel_connector);
15583 if (ret)
15584 goto err;
15585
15586 return 0;
15587
15588err:
15589 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015590}
15591
Chris Wilsonc191eca2016-06-17 11:40:33 +010015592void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020015593{
Chris Wilsone63d87c2016-06-17 11:40:34 +010015594 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015595
Chris Wilsone63d87c2016-06-17 11:40:34 +010015596 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015597 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015598}
15599
Manasi Navare886c6b82017-10-26 14:52:00 -070015600static void intel_hpd_poll_fini(struct drm_device *dev)
15601{
15602 struct intel_connector *connector;
15603 struct drm_connector_list_iter conn_iter;
15604
Chris Wilson448aa912017-11-28 11:01:47 +000015605 /* Kill all the work that may have been queued by hpd. */
Manasi Navare886c6b82017-10-26 14:52:00 -070015606 drm_connector_list_iter_begin(dev, &conn_iter);
15607 for_each_intel_connector_iter(connector, &conn_iter) {
15608 if (connector->modeset_retry_work.func)
15609 cancel_work_sync(&connector->modeset_retry_work);
Sean Paulee5e5e72018-01-08 14:55:39 -050015610 if (connector->hdcp_shim) {
15611 cancel_delayed_work_sync(&connector->hdcp_check_work);
15612 cancel_work_sync(&connector->hdcp_prop_work);
15613 }
Manasi Navare886c6b82017-10-26 14:52:00 -070015614 }
15615 drm_connector_list_iter_end(&conn_iter);
15616}
15617
Jesse Barnes79e53942008-11-07 14:24:08 -080015618void intel_modeset_cleanup(struct drm_device *dev)
15619{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015620 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070015621
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015622 flush_work(&dev_priv->atomic_helper.free_work);
15623 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15624
Chris Wilsondc979972016-05-10 14:10:04 +010015625 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020015626
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015627 /*
15628 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015629 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015630 * experience fancy races otherwise.
15631 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015632 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015633
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015634 /*
15635 * Due to the hpd irq storm handling the hotplug work can re-arm the
15636 * poll handlers. Hence disable polling after hpd handling is shut down.
15637 */
Manasi Navare886c6b82017-10-26 14:52:00 -070015638 intel_hpd_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015639
Daniel Vetter4f256d82017-07-15 00:46:55 +020015640 /* poll work can call into fbdev, hence clean that up afterwards */
15641 intel_fbdev_fini(dev_priv);
15642
Jesse Barnes723bfd72010-10-07 16:01:13 -070015643 intel_unregister_dsm_handler();
15644
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015645 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015646
Chris Wilson1630fe72011-07-08 12:22:42 +010015647 /* flush any delayed tasks or pending work */
15648 flush_scheduled_work();
15649
Jesse Barnes79e53942008-11-07 14:24:08 -080015650 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015651
Chris Wilson1ee8da62016-05-12 12:43:23 +010015652 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015653
Chris Wilsondc979972016-05-10 14:10:04 +010015654 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010015655
Tvrtko Ursulin40196442016-12-01 14:16:42 +000015656 intel_teardown_gmbus(dev_priv);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020015657
15658 destroy_workqueue(dev_priv->modeset_wq);
Jesse Barnes79e53942008-11-07 14:24:08 -080015659}
15660
Chris Wilsondf0e9242010-09-09 16:20:55 +010015661void intel_connector_attach_encoder(struct intel_connector *connector,
15662 struct intel_encoder *encoder)
15663{
15664 connector->encoder = encoder;
15665 drm_mode_connector_attach_encoder(&connector->base,
15666 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015667}
Dave Airlie28d52042009-09-21 14:33:58 +100015668
15669/*
15670 * set vga decode state - true == enable VGA decode
15671 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015672int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100015673{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015674 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015675 u16 gmch_ctrl;
15676
Chris Wilson75fa0412014-02-07 18:37:02 -020015677 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15678 DRM_ERROR("failed to read control word\n");
15679 return -EIO;
15680 }
15681
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015682 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15683 return 0;
15684
Dave Airlie28d52042009-09-21 14:33:58 +100015685 if (state)
15686 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15687 else
15688 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015689
15690 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15691 DRM_ERROR("failed to write control word\n");
15692 return -EIO;
15693 }
15694
Dave Airlie28d52042009-09-21 14:33:58 +100015695 return 0;
15696}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015697
Chris Wilson98a2f412016-10-12 10:05:18 +010015698#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15699
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015700struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015701
15702 u32 power_well_driver;
15703
Chris Wilson63b66e52013-08-08 15:12:06 +020015704 int num_transcoders;
15705
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015706 struct intel_cursor_error_state {
15707 u32 control;
15708 u32 position;
15709 u32 base;
15710 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015711 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015712
15713 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015714 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015715 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015716 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015717 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015718
15719 struct intel_plane_error_state {
15720 u32 control;
15721 u32 stride;
15722 u32 size;
15723 u32 pos;
15724 u32 addr;
15725 u32 surface;
15726 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015727 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015728
15729 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015730 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015731 enum transcoder cpu_transcoder;
15732
15733 u32 conf;
15734
15735 u32 htotal;
15736 u32 hblank;
15737 u32 hsync;
15738 u32 vtotal;
15739 u32 vblank;
15740 u32 vsync;
15741 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015742};
15743
15744struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010015745intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015746{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015747 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015748 int transcoders[] = {
15749 TRANSCODER_A,
15750 TRANSCODER_B,
15751 TRANSCODER_C,
15752 TRANSCODER_EDP,
15753 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015754 int i;
15755
Chris Wilsonc0336662016-05-06 15:40:21 +010015756 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020015757 return NULL;
15758
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015759 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015760 if (error == NULL)
15761 return NULL;
15762
Chris Wilsonc0336662016-05-06 15:40:21 +010015763 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Imre Deak9c3a16c2017-08-14 18:15:30 +030015764 error->power_well_driver =
15765 I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015766
Damien Lespiau055e3932014-08-18 13:49:10 +010015767 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015768 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015769 __intel_display_power_is_enabled(dev_priv,
15770 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015771 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015772 continue;
15773
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015774 error->cursor[i].control = I915_READ(CURCNTR(i));
15775 error->cursor[i].position = I915_READ(CURPOS(i));
15776 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015777
15778 error->plane[i].control = I915_READ(DSPCNTR(i));
15779 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015780 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015781 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015782 error->plane[i].pos = I915_READ(DSPPOS(i));
15783 }
Chris Wilsonc0336662016-05-06 15:40:21 +010015784 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030015785 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015786 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015787 error->plane[i].surface = I915_READ(DSPSURF(i));
15788 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15789 }
15790
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015791 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015792
Chris Wilsonc0336662016-05-06 15:40:21 +010015793 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030015794 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015795 }
15796
Jani Nikula4d1de972016-03-18 17:05:42 +020015797 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010015798 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030015799 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020015800 error->num_transcoders++; /* Account for eDP. */
15801
15802 for (i = 0; i < error->num_transcoders; i++) {
15803 enum transcoder cpu_transcoder = transcoders[i];
15804
Imre Deakddf9c532013-11-27 22:02:02 +020015805 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015806 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015807 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015808 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015809 continue;
15810
Chris Wilson63b66e52013-08-08 15:12:06 +020015811 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15812
15813 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15814 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15815 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15816 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15817 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15818 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15819 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015820 }
15821
15822 return error;
15823}
15824
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015825#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15826
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015827void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015828intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015829 struct intel_display_error_state *error)
15830{
Chris Wilson5a4c6f12017-02-14 16:46:11 +000015831 struct drm_i915_private *dev_priv = m->i915;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015832 int i;
15833
Chris Wilson63b66e52013-08-08 15:12:06 +020015834 if (!error)
15835 return;
15836
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015837 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010015838 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015839 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015840 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015841 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015842 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015843 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020015844 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015845 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015846 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015847
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015848 err_printf(m, "Plane [%d]:\n", i);
15849 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15850 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000015851 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015852 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15853 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015854 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010015855 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015856 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000015857 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015858 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15859 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015860 }
15861
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015862 err_printf(m, "Cursor [%d]:\n", i);
15863 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15864 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15865 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015866 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015867
15868 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020015869 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015870 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015871 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020015872 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020015873 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15874 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15875 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15876 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15877 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15878 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15879 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15880 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015881}
Chris Wilson98a2f412016-10-12 10:05:18 +010015882
15883#endif