blob: bbf8ca21a7a29e918850fdee1de4bce5161b00ec [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
Jesse Barnesc1c7af62009-09-10 15:28:03 -070027#include <linux/module.h>
28#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080029#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080030#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070032#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080033#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000039#include "i915_gem_clflush.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Daniel Vetter72fdb402018-09-05 15:57:11 +020048#include <drm/drm_atomic_uapi.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
Matt Roper3d7d6512014-06-10 08:28:13 -070076/* Cursor formats */
77static const uint32_t intel_cursor_formats[] = {
78 DRM_FORMAT_ARGB8888,
79};
80
Ben Widawsky714244e2017-08-01 09:58:16 -070081static const uint64_t cursor_format_modifiers[] = {
82 DRM_FORMAT_MOD_LINEAR,
83 DRM_FORMAT_MOD_INVALID
84};
85
Jesse Barnesf1f644d2013-06-27 00:39:25 +030086static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030088static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020089 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030090
Chris Wilson24dbf512017-02-15 10:59:18 +000091static int intel_framebuffer_init(struct intel_framebuffer *ifb,
92 struct drm_i915_gem_object *obj,
93 struct drm_mode_fb_cmd2 *mode_cmd);
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +020094static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state);
95static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
Maarten Lankhorst4c354752018-10-11 12:04:49 +020096static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
97 const struct intel_link_m_n *m_n,
98 const struct intel_link_m_n *m2_n2);
Maarten Lankhorstfdf73512018-10-04 11:45:52 +020099static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
100static void ironlake_set_pipeconf(const struct intel_crtc_state *crtc_state);
101static void haswell_set_pipeconf(const struct intel_crtc_state *crtc_state);
102static void haswell_set_pipemisc(const struct intel_crtc_state *crtc_state);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200103static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200104 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200105static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200106 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200107static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
108static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Nabendu Maiti1c74eea2016-11-29 11:23:14 +0530109static void intel_crtc_init_scalers(struct intel_crtc *crtc,
110 struct intel_crtc_state *crtc_state);
Maarten Lankhorstb2562712018-10-04 11:45:53 +0200111static void skylake_pfit_enable(const struct intel_crtc_state *crtc_state);
112static void ironlake_pfit_disable(const struct intel_crtc_state *old_crtc_state);
113static void ironlake_pfit_enable(const struct intel_crtc_state *crtc_state);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +0300114static void intel_modeset_setup_hw_state(struct drm_device *dev,
115 struct drm_modeset_acquire_ctx *ctx);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200116static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100117
Ma Lingd4906092009-03-18 20:13:27 +0800118struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300119 struct {
120 int min, max;
121 } dot, vco, n, m, m1, m2, p, p1;
122
123 struct {
124 int dot_limit;
125 int p2_slow, p2_fast;
126 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800127};
Jesse Barnes79e53942008-11-07 14:24:08 -0800128
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300129/* returns HPLL frequency in kHz */
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200130int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300131{
132 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
133
134 /* Obtain SKU information */
135 mutex_lock(&dev_priv->sb_lock);
136 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
137 CCK_FUSE_HPLL_FREQ_MASK;
138 mutex_unlock(&dev_priv->sb_lock);
139
140 return vco_freq[hpll_freq] * 1000;
141}
142
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200143int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
144 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300145{
146 u32 val;
147 int divider;
148
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300149 mutex_lock(&dev_priv->sb_lock);
150 val = vlv_cck_read(dev_priv, reg);
151 mutex_unlock(&dev_priv->sb_lock);
152
153 divider = val & CCK_FREQUENCY_VALUES;
154
155 WARN((val & CCK_FREQUENCY_STATUS) !=
156 (divider << CCK_FREQUENCY_STATUS_SHIFT),
157 "%s change in progress\n", name);
158
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200159 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
160}
161
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +0200162int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
163 const char *name, u32 reg)
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200164{
165 if (dev_priv->hpll_freq == 0)
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200166 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200167
168 return vlv_get_cck_clock(dev_priv, name, reg,
169 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300170}
171
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300172static void intel_update_czclk(struct drm_i915_private *dev_priv)
173{
Wayne Boyer666a4532015-12-09 12:29:35 -0800174 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300175 return;
176
177 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
178 CCK_CZ_CLOCK_CONTROL);
179
180 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
181}
182
Chris Wilson021357a2010-09-07 20:54:59 +0100183static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200184intel_fdi_link_freq(struct drm_i915_private *dev_priv,
185 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100186{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200187 if (HAS_DDI(dev_priv))
188 return pipe_config->port_clock; /* SPLL */
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200189 else
Chris Wilson58ecd9d2017-11-05 13:49:05 +0000190 return dev_priv->fdi_pll_freq;
Chris Wilson021357a2010-09-07 20:54:59 +0100191}
192
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300193static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400194 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200195 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200196 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400197 .m = { .min = 96, .max = 140 },
198 .m1 = { .min = 18, .max = 26 },
199 .m2 = { .min = 6, .max = 16 },
200 .p = { .min = 4, .max = 128 },
201 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700202 .p2 = { .dot_limit = 165000,
203 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700204};
205
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300206static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200207 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200208 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200209 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200210 .m = { .min = 96, .max = 140 },
211 .m1 = { .min = 18, .max = 26 },
212 .m2 = { .min = 6, .max = 16 },
213 .p = { .min = 4, .max = 128 },
214 .p1 = { .min = 2, .max = 33 },
215 .p2 = { .dot_limit = 165000,
216 .p2_slow = 4, .p2_fast = 4 },
217};
218
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300219static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400220 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200221 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200222 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400223 .m = { .min = 96, .max = 140 },
224 .m1 = { .min = 18, .max = 26 },
225 .m2 = { .min = 6, .max = 16 },
226 .p = { .min = 4, .max = 128 },
227 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700228 .p2 = { .dot_limit = 165000,
229 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700230};
Eric Anholt273e27c2011-03-30 13:01:10 -0700231
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300232static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400233 .dot = { .min = 20000, .max = 400000 },
234 .vco = { .min = 1400000, .max = 2800000 },
235 .n = { .min = 1, .max = 6 },
236 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100237 .m1 = { .min = 8, .max = 18 },
238 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400239 .p = { .min = 5, .max = 80 },
240 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700241 .p2 = { .dot_limit = 200000,
242 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700243};
244
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300245static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400246 .dot = { .min = 20000, .max = 400000 },
247 .vco = { .min = 1400000, .max = 2800000 },
248 .n = { .min = 1, .max = 6 },
249 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100250 .m1 = { .min = 8, .max = 18 },
251 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400252 .p = { .min = 7, .max = 98 },
253 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700254 .p2 = { .dot_limit = 112000,
255 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700256};
257
Eric Anholt273e27c2011-03-30 13:01:10 -0700258
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300259static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .dot = { .min = 25000, .max = 270000 },
261 .vco = { .min = 1750000, .max = 3500000},
262 .n = { .min = 1, .max = 4 },
263 .m = { .min = 104, .max = 138 },
264 .m1 = { .min = 17, .max = 23 },
265 .m2 = { .min = 5, .max = 11 },
266 .p = { .min = 10, .max = 30 },
267 .p1 = { .min = 1, .max = 3},
268 .p2 = { .dot_limit = 270000,
269 .p2_slow = 10,
270 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800271 },
Keith Packarde4b36692009-06-05 19:22:17 -0700272};
273
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300274static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700275 .dot = { .min = 22000, .max = 400000 },
276 .vco = { .min = 1750000, .max = 3500000},
277 .n = { .min = 1, .max = 4 },
278 .m = { .min = 104, .max = 138 },
279 .m1 = { .min = 16, .max = 23 },
280 .m2 = { .min = 5, .max = 11 },
281 .p = { .min = 5, .max = 80 },
282 .p1 = { .min = 1, .max = 8},
283 .p2 = { .dot_limit = 165000,
284 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700285};
286
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300287static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700288 .dot = { .min = 20000, .max = 115000 },
289 .vco = { .min = 1750000, .max = 3500000 },
290 .n = { .min = 1, .max = 3 },
291 .m = { .min = 104, .max = 138 },
292 .m1 = { .min = 17, .max = 23 },
293 .m2 = { .min = 5, .max = 11 },
294 .p = { .min = 28, .max = 112 },
295 .p1 = { .min = 2, .max = 8 },
296 .p2 = { .dot_limit = 0,
297 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800298 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
300
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300301static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .dot = { .min = 80000, .max = 224000 },
303 .vco = { .min = 1750000, .max = 3500000 },
304 .n = { .min = 1, .max = 3 },
305 .m = { .min = 104, .max = 138 },
306 .m1 = { .min = 17, .max = 23 },
307 .m2 = { .min = 5, .max = 11 },
308 .p = { .min = 14, .max = 42 },
309 .p1 = { .min = 2, .max = 6 },
310 .p2 = { .dot_limit = 0,
311 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800312 },
Keith Packarde4b36692009-06-05 19:22:17 -0700313};
314
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300315static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400316 .dot = { .min = 20000, .max = 400000},
317 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400319 .n = { .min = 3, .max = 6 },
320 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700321 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400322 .m1 = { .min = 0, .max = 0 },
323 .m2 = { .min = 0, .max = 254 },
324 .p = { .min = 5, .max = 80 },
325 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700326 .p2 = { .dot_limit = 200000,
327 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700328};
329
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300330static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400331 .dot = { .min = 20000, .max = 400000 },
332 .vco = { .min = 1700000, .max = 3500000 },
333 .n = { .min = 3, .max = 6 },
334 .m = { .min = 2, .max = 256 },
335 .m1 = { .min = 0, .max = 0 },
336 .m2 = { .min = 0, .max = 254 },
337 .p = { .min = 7, .max = 112 },
338 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700339 .p2 = { .dot_limit = 112000,
340 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
Eric Anholt273e27c2011-03-30 13:01:10 -0700343/* Ironlake / Sandybridge
344 *
345 * We calculate clock using (register_value + 2) for N/M1/M2, so here
346 * the range value for them is (actual_value - 2).
347 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300348static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700349 .dot = { .min = 25000, .max = 350000 },
350 .vco = { .min = 1760000, .max = 3510000 },
351 .n = { .min = 1, .max = 5 },
352 .m = { .min = 79, .max = 127 },
353 .m1 = { .min = 12, .max = 22 },
354 .m2 = { .min = 5, .max = 9 },
355 .p = { .min = 5, .max = 80 },
356 .p1 = { .min = 1, .max = 8 },
357 .p2 = { .dot_limit = 225000,
358 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700359};
360
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300361static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700362 .dot = { .min = 25000, .max = 350000 },
363 .vco = { .min = 1760000, .max = 3510000 },
364 .n = { .min = 1, .max = 3 },
365 .m = { .min = 79, .max = 118 },
366 .m1 = { .min = 12, .max = 22 },
367 .m2 = { .min = 5, .max = 9 },
368 .p = { .min = 28, .max = 112 },
369 .p1 = { .min = 2, .max = 8 },
370 .p2 = { .dot_limit = 225000,
371 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800372};
373
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300374static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700375 .dot = { .min = 25000, .max = 350000 },
376 .vco = { .min = 1760000, .max = 3510000 },
377 .n = { .min = 1, .max = 3 },
378 .m = { .min = 79, .max = 127 },
379 .m1 = { .min = 12, .max = 22 },
380 .m2 = { .min = 5, .max = 9 },
381 .p = { .min = 14, .max = 56 },
382 .p1 = { .min = 2, .max = 8 },
383 .p2 = { .dot_limit = 225000,
384 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800385};
386
Eric Anholt273e27c2011-03-30 13:01:10 -0700387/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300388static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700389 .dot = { .min = 25000, .max = 350000 },
390 .vco = { .min = 1760000, .max = 3510000 },
391 .n = { .min = 1, .max = 2 },
392 .m = { .min = 79, .max = 126 },
393 .m1 = { .min = 12, .max = 22 },
394 .m2 = { .min = 5, .max = 9 },
395 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400396 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700397 .p2 = { .dot_limit = 225000,
398 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800399};
400
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300401static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700402 .dot = { .min = 25000, .max = 350000 },
403 .vco = { .min = 1760000, .max = 3510000 },
404 .n = { .min = 1, .max = 3 },
405 .m = { .min = 79, .max = 126 },
406 .m1 = { .min = 12, .max = 22 },
407 .m2 = { .min = 5, .max = 9 },
408 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400409 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700410 .p2 = { .dot_limit = 225000,
411 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800412};
413
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300414static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300415 /*
416 * These are the data rate limits (measured in fast clocks)
417 * since those are the strictest limits we have. The fast
418 * clock and actual rate limits are more relaxed, so checking
419 * them would make no difference.
420 */
421 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200422 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700423 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700424 .m1 = { .min = 2, .max = 3 },
425 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300426 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300427 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700428};
429
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300430static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300431 /*
432 * These are the data rate limits (measured in fast clocks)
433 * since those are the strictest limits we have. The fast
434 * clock and actual rate limits are more relaxed, so checking
435 * them would make no difference.
436 */
437 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200438 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300439 .n = { .min = 1, .max = 1 },
440 .m1 = { .min = 2, .max = 2 },
441 .m2 = { .min = 24 << 22, .max = 175 << 22 },
442 .p1 = { .min = 2, .max = 4 },
443 .p2 = { .p2_slow = 1, .p2_fast = 14 },
444};
445
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300446static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200447 /* FIXME: find real dot limits */
448 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530449 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200450 .n = { .min = 1, .max = 1 },
451 .m1 = { .min = 2, .max = 2 },
452 /* FIXME: find real m2 limits */
453 .m2 = { .min = 2 << 22, .max = 255 << 22 },
454 .p1 = { .min = 2, .max = 4 },
455 .p2 = { .p2_slow = 1, .p2_fast = 20 },
456};
457
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530458static void
459skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
460{
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530461 if (enable)
462 I915_WRITE(CLKGATE_DIS_PSL(pipe),
463 DUPS1_GATING_DIS | DUPS2_GATING_DIS);
464 else
465 I915_WRITE(CLKGATE_DIS_PSL(pipe),
466 I915_READ(CLKGATE_DIS_PSL(pipe)) &
467 ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
468}
469
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200470static bool
Maarten Lankhorst24f28452017-11-22 19:39:01 +0100471needs_modeset(const struct drm_crtc_state *state)
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200472{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200473 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200474}
475
Imre Deakdccbea32015-06-22 23:35:51 +0300476/*
477 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
478 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
479 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
480 * The helpers' return value is the rate of the clock that is fed to the
481 * display engine's pipe which can be the above fast dot clock rate or a
482 * divided-down version of it.
483 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500484/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300485static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800486{
Shaohua Li21778322009-02-23 15:19:16 +0800487 clock->m = clock->m2 + 2;
488 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200489 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300490 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300491 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
492 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300493
494 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800495}
496
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200497static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
498{
499 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
500}
501
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300502static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800503{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200504 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800505 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200506 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300507 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300508 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
509 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300510
511 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800512}
513
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300514static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300515{
516 clock->m = clock->m1 * clock->m2;
517 clock->p = clock->p1 * clock->p2;
518 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300519 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300520 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
521 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300522
523 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300524}
525
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300526int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300527{
528 clock->m = clock->m1 * clock->m2;
529 clock->p = clock->p1 * clock->p2;
530 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300531 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300532 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
533 clock->n << 22);
534 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300535
536 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300537}
538
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800539#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Chris Wilsonc38c1452018-02-14 13:49:22 +0000540
541/*
Jesse Barnes79e53942008-11-07 14:24:08 -0800542 * Returns whether the given set of divisors are valid for a given refclk with
543 * the given connectors.
544 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100545static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300546 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300547 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800548{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300549 if (clock->n < limit->n.min || limit->n.max < clock->n)
550 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800551 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400552 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800553 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400554 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400556 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300557
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100558 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200559 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300560 if (clock->m1 <= clock->m2)
561 INTELPllInvalid("m1 <= m2\n");
562
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100563 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200564 !IS_GEN9_LP(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300565 if (clock->p < limit->p.min || limit->p.max < clock->p)
566 INTELPllInvalid("p out of range\n");
567 if (clock->m < limit->m.min || limit->m.max < clock->m)
568 INTELPllInvalid("m out of range\n");
569 }
570
Jesse Barnes79e53942008-11-07 14:24:08 -0800571 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400572 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800573 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
574 * connector, etc., rather than just a single range.
575 */
576 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400577 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800578
579 return true;
580}
581
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300582static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300583i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300584 const struct intel_crtc_state *crtc_state,
585 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800586{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300587 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800588
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300589 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800590 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100591 * For LVDS just rely on its current settings for dual-channel.
592 * We haven't figured out how to reliably set up different
593 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100595 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300596 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300598 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800599 } else {
600 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300601 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300603 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800604 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300605}
606
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200607/*
608 * Returns a set of divisors for the desired target clock with the given
609 * refclk, or FALSE. The returned values represent the clock equation:
610 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
611 *
612 * Target and reference clocks are specified in kHz.
613 *
614 * If match_clock is provided, then best_clock P divider must match the P
615 * divider from @match_clock used for LVDS downclocking.
616 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300617static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300618i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300619 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300620 int target, int refclk, struct dpll *match_clock,
621 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300622{
623 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300624 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300625 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800626
Akshay Joshi0206e352011-08-16 15:34:10 -0400627 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800628
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300629 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
630
Zhao Yakui42158662009-11-20 11:24:18 +0800631 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
632 clock.m1++) {
633 for (clock.m2 = limit->m2.min;
634 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200635 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800636 break;
637 for (clock.n = limit->n.min;
638 clock.n <= limit->n.max; clock.n++) {
639 for (clock.p1 = limit->p1.min;
640 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800641 int this_err;
642
Imre Deakdccbea32015-06-22 23:35:51 +0300643 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100644 if (!intel_PLL_is_valid(to_i915(dev),
645 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000646 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800648 if (match_clock &&
649 clock.p != match_clock->p)
650 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800651
652 this_err = abs(clock.dot - target);
653 if (this_err < err) {
654 *best_clock = clock;
655 err = this_err;
656 }
657 }
658 }
659 }
660 }
661
662 return (err != target);
663}
664
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200665/*
666 * Returns a set of divisors for the desired target clock with the given
667 * refclk, or FALSE. The returned values represent the clock equation:
668 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
669 *
670 * Target and reference clocks are specified in kHz.
671 *
672 * If match_clock is provided, then best_clock P divider must match the P
673 * divider from @match_clock used for LVDS downclocking.
674 */
Ma Lingd4906092009-03-18 20:13:27 +0800675static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300676pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200677 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300678 int target, int refclk, struct dpll *match_clock,
679 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200680{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300682 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200683 int err = target;
684
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200685 memset(best_clock, 0, sizeof(*best_clock));
686
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300687 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
688
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200689 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
690 clock.m1++) {
691 for (clock.m2 = limit->m2.min;
692 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200693 for (clock.n = limit->n.min;
694 clock.n <= limit->n.max; clock.n++) {
695 for (clock.p1 = limit->p1.min;
696 clock.p1 <= limit->p1.max; clock.p1++) {
697 int this_err;
698
Imre Deakdccbea32015-06-22 23:35:51 +0300699 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100700 if (!intel_PLL_is_valid(to_i915(dev),
701 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 &clock))
703 continue;
704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200721/*
722 * Returns a set of divisors for the desired target clock with the given
723 * refclk, or FALSE. The returned values represent the clock equation:
724 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200725 *
726 * Target and reference clocks are specified in kHz.
727 *
728 * If match_clock is provided, then best_clock P divider must match the P
729 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200730 */
Ma Lingd4906092009-03-18 20:13:27 +0800731static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300732g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200733 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300734 int target, int refclk, struct dpll *match_clock,
735 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800736{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300737 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300738 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800739 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300740 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400741 /* approximately equals target * 0.00585 */
742 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800743
744 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300745
746 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
747
Ma Lingd4906092009-03-18 20:13:27 +0800748 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200749 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800750 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200751 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800752 for (clock.m1 = limit->m1.max;
753 clock.m1 >= limit->m1.min; clock.m1--) {
754 for (clock.m2 = limit->m2.max;
755 clock.m2 >= limit->m2.min; clock.m2--) {
756 for (clock.p1 = limit->p1.max;
757 clock.p1 >= limit->p1.min; clock.p1--) {
758 int this_err;
759
Imre Deakdccbea32015-06-22 23:35:51 +0300760 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100761 if (!intel_PLL_is_valid(to_i915(dev),
762 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000763 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800764 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000765
766 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800767 if (this_err < err_most) {
768 *best_clock = clock;
769 err_most = this_err;
770 max_n = clock.n;
771 found = true;
772 }
773 }
774 }
775 }
776 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800777 return found;
778}
Ma Lingd4906092009-03-18 20:13:27 +0800779
Imre Deakd5dd62b2015-03-17 11:40:03 +0200780/*
781 * Check if the calculated PLL configuration is more optimal compared to the
782 * best configuration and error found so far. Return the calculated error.
783 */
784static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300785 const struct dpll *calculated_clock,
786 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200787 unsigned int best_error_ppm,
788 unsigned int *error_ppm)
789{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200790 /*
791 * For CHV ignore the error and consider only the P value.
792 * Prefer a bigger P value based on HW requirements.
793 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100794 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200795 *error_ppm = 0;
796
797 return calculated_clock->p > best_clock->p;
798 }
799
Imre Deak24be4e42015-03-17 11:40:04 +0200800 if (WARN_ON_ONCE(!target_freq))
801 return false;
802
Imre Deakd5dd62b2015-03-17 11:40:03 +0200803 *error_ppm = div_u64(1000000ULL *
804 abs(target_freq - calculated_clock->dot),
805 target_freq);
806 /*
807 * Prefer a better P value over a better (smaller) error if the error
808 * is small. Ensure this preference for future configurations too by
809 * setting the error to 0.
810 */
811 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
812 *error_ppm = 0;
813
814 return true;
815 }
816
817 return *error_ppm + 10 < best_error_ppm;
818}
819
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200820/*
821 * Returns a set of divisors for the desired target clock with the given
822 * refclk, or FALSE. The returned values represent the clock equation:
823 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
824 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800825static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300826vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200827 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300828 int target, int refclk, struct dpll *match_clock,
829 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700830{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200831 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300832 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300833 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300834 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300835 /* min update 19.2 MHz */
836 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300837 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700838
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300839 target *= 5; /* fast clock */
840
841 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700842
843 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300844 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300845 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300846 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300847 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300848 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700849 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300850 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200851 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300852
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300853 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
854 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300855
Imre Deakdccbea32015-06-22 23:35:51 +0300856 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300857
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100858 if (!intel_PLL_is_valid(to_i915(dev),
859 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300860 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300861 continue;
862
Imre Deakd5dd62b2015-03-17 11:40:03 +0200863 if (!vlv_PLL_is_optimal(dev, target,
864 &clock,
865 best_clock,
866 bestppm, &ppm))
867 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300868
Imre Deakd5dd62b2015-03-17 11:40:03 +0200869 *best_clock = clock;
870 bestppm = ppm;
871 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700872 }
873 }
874 }
875 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700876
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300877 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700878}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700879
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200880/*
881 * Returns a set of divisors for the desired target clock with the given
882 * refclk, or FALSE. The returned values represent the clock equation:
883 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
884 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300885static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300886chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200887 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300888 int target, int refclk, struct dpll *match_clock,
889 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300890{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200891 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300892 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200893 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300894 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300895 uint64_t m2;
896 int found = false;
897
898 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200899 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300900
901 /*
902 * Based on hardware doc, the n always set to 1, and m1 always
903 * set to 2. If requires to support 200Mhz refclk, we need to
904 * revisit this because n may not 1 anymore.
905 */
906 clock.n = 1, clock.m1 = 2;
907 target *= 5; /* fast clock */
908
909 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
910 for (clock.p2 = limit->p2.p2_fast;
911 clock.p2 >= limit->p2.p2_slow;
912 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200913 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300914
915 clock.p = clock.p1 * clock.p2;
916
917 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
918 clock.n) << 22, refclk * clock.m1);
919
920 if (m2 > INT_MAX/clock.m1)
921 continue;
922
923 clock.m2 = m2;
924
Imre Deakdccbea32015-06-22 23:35:51 +0300925 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300926
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100927 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300928 continue;
929
Imre Deak9ca3ba02015-03-17 11:40:05 +0200930 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
931 best_error_ppm, &error_ppm))
932 continue;
933
934 *best_clock = clock;
935 best_error_ppm = error_ppm;
936 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300937 }
938 }
939
940 return found;
941}
942
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200943bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300944 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200945{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200946 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300947 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200948
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200949 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200950 target_clock, refclk, NULL, best_clock);
951}
952
Ville Syrjälä525b9312016-10-31 22:37:02 +0200953bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300954{
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300955 /* Be paranoid as we can arrive here with only partial
956 * state retrieved from the hardware during setup.
957 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100958 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300959 * as Haswell has gained clock readout/fastboot support.
960 *
Ville Syrjäläcd30fbc2018-05-25 21:50:40 +0300961 * We can ditch the crtc->primary->state->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300962 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700963 *
964 * FIXME: The intel_crtc->active here should be switched to
965 * crtc->state->active once we have proper CRTC states wired up
966 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300967 */
Ville Syrjälä525b9312016-10-31 22:37:02 +0200968 return crtc->active && crtc->base.primary->state->fb &&
969 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300970}
971
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200972enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
973 enum pipe pipe)
974{
Ville Syrjälä98187832016-10-31 22:37:10 +0200975 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200976
Ville Syrjäläe2af48c2016-10-31 22:37:05 +0200977 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200978}
979
Ville Syrjälä8fedd642017-11-29 17:37:30 +0200980static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
981 enum pipe pipe)
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300982{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200983 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300984 u32 line1, line2;
985 u32 line_mask;
986
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100987 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300988 line_mask = DSL_LINEMASK_GEN2;
989 else
990 line_mask = DSL_LINEMASK_GEN3;
991
992 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +0200993 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300994 line2 = I915_READ(reg) & line_mask;
995
Ville Syrjälä8fedd642017-11-29 17:37:30 +0200996 return line1 != line2;
997}
998
999static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1000{
1001 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1002 enum pipe pipe = crtc->pipe;
1003
1004 /* Wait for the display line to settle/start moving */
1005 if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1006 DRM_ERROR("pipe %c scanline %s wait timed out\n",
1007 pipe_name(pipe), onoff(state));
1008}
1009
1010static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1011{
1012 wait_for_pipe_scanline_moving(crtc, false);
1013}
1014
1015static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1016{
1017 wait_for_pipe_scanline_moving(crtc, true);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001018}
1019
Ville Syrjälä4972f702017-11-29 17:37:32 +02001020static void
1021intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001022{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001023 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001024 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001025
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001026 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001027 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001028 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001029
Keith Packardab7ad7f2010-10-03 00:33:06 -07001030 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001031 if (intel_wait_for_register(dev_priv,
1032 reg, I965_PIPECONF_ACTIVE, 0,
1033 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001034 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001035 } else {
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001036 intel_wait_for_pipe_scanline_stopped(crtc);
Keith Packardab7ad7f2010-10-03 00:33:06 -07001037 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001038}
1039
Jesse Barnesb24e7172011-01-04 15:09:30 -08001040/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001041void assert_pll(struct drm_i915_private *dev_priv,
1042 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001043{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001044 u32 val;
1045 bool cur_state;
1046
Ville Syrjälä649636e2015-09-22 19:50:01 +03001047 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001048 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001049 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001050 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001051 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001052}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001053
Jani Nikula23538ef2013-08-27 15:12:22 +03001054/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001055void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001056{
1057 u32 val;
1058 bool cur_state;
1059
Ville Syrjäläa5805162015-05-26 20:42:30 +03001060 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001061 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001062 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001063
1064 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001065 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001066 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001067 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001068}
Jani Nikula23538ef2013-08-27 15:12:22 +03001069
Jesse Barnes040484a2011-01-03 12:14:26 -08001070static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1071 enum pipe pipe, bool state)
1072{
Jesse Barnes040484a2011-01-03 12:14:26 -08001073 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001074 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1075 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001076
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001077 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001078 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001079 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001080 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001081 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001082 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001083 cur_state = !!(val & FDI_TX_ENABLE);
1084 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001085 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001086 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001087 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001088}
1089#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1090#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1091
1092static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1093 enum pipe pipe, bool state)
1094{
Jesse Barnes040484a2011-01-03 12:14:26 -08001095 u32 val;
1096 bool cur_state;
1097
Ville Syrjälä649636e2015-09-22 19:50:01 +03001098 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001099 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001100 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001101 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001102 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001103}
1104#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1105#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1106
1107static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1108 enum pipe pipe)
1109{
Jesse Barnes040484a2011-01-03 12:14:26 -08001110 u32 val;
1111
1112 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001113 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001114 return;
1115
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001116 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001117 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001118 return;
1119
Ville Syrjälä649636e2015-09-22 19:50:01 +03001120 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001121 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 -08001122}
1123
Daniel Vetter55607e82013-06-16 21:42:39 +02001124void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1125 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001126{
Jesse Barnes040484a2011-01-03 12:14:26 -08001127 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001128 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001129
Ville Syrjälä649636e2015-09-22 19:50:01 +03001130 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001131 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001132 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001133 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001134 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001135}
1136
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001137void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001138{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001139 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001140 u32 val;
Ville Syrjälä10ed55e2018-05-23 17:57:18 +03001141 enum pipe panel_pipe = INVALID_PIPE;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001142 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001143
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001144 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001145 return;
1146
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001147 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001148 u32 port_sel;
1149
Imre Deak44cb7342016-08-10 14:07:29 +03001150 pp_reg = PP_CONTROL(0);
1151 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001152
Ville Syrjälä4c23dea2018-05-18 18:29:30 +03001153 switch (port_sel) {
1154 case PANEL_PORT_SELECT_LVDS:
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001155 intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
Ville Syrjälä4c23dea2018-05-18 18:29:30 +03001156 break;
1157 case PANEL_PORT_SELECT_DPA:
1158 intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1159 break;
1160 case PANEL_PORT_SELECT_DPC:
1161 intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1162 break;
1163 case PANEL_PORT_SELECT_DPD:
1164 intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1165 break;
1166 default:
1167 MISSING_CASE(port_sel);
1168 break;
1169 }
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001170 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001171 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001172 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001173 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001174 } else {
Ville Syrjäläf0d2b752018-05-18 18:29:31 +03001175 u32 port_sel;
1176
Imre Deak44cb7342016-08-10 14:07:29 +03001177 pp_reg = PP_CONTROL(0);
Ville Syrjäläf0d2b752018-05-18 18:29:31 +03001178 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1179
1180 WARN_ON(port_sel != PANEL_PORT_SELECT_LVDS);
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001181 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
Jesse Barnesea0760c2011-01-04 15:09:32 -08001182 }
1183
1184 val = I915_READ(pp_reg);
1185 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001186 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001187 locked = false;
1188
Rob Clarke2c719b2014-12-15 13:56:32 -05001189 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001190 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001191 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001192}
1193
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001194void assert_pipe(struct drm_i915_private *dev_priv,
1195 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001196{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001197 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001198 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1199 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001200 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001201
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001202 /* we keep both pipes enabled on 830 */
1203 if (IS_I830(dev_priv))
Daniel Vetter8e636782012-01-22 01:36:48 +01001204 state = true;
1205
Imre Deak4feed0e2016-02-12 18:55:14 +02001206 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1207 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001208 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001209 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001210
1211 intel_display_power_put(dev_priv, power_domain);
1212 } else {
1213 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001214 }
1215
Rob Clarke2c719b2014-12-15 13:56:32 -05001216 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001217 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001218 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001219}
1220
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001221static void assert_plane(struct intel_plane *plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001222{
Ville Syrjäläeade6c82018-01-30 22:38:03 +02001223 enum pipe pipe;
1224 bool cur_state;
1225
1226 cur_state = plane->get_hw_state(plane, &pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001227
Rob Clarke2c719b2014-12-15 13:56:32 -05001228 I915_STATE_WARN(cur_state != state,
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001229 "%s assertion failure (expected %s, current %s)\n",
1230 plane->base.name, onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001231}
1232
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001233#define assert_plane_enabled(p) assert_plane(p, true)
1234#define assert_plane_disabled(p) assert_plane(p, false)
Chris Wilson931872f2012-01-16 23:01:13 +00001235
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001236static void assert_planes_disabled(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001237{
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001238 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1239 struct intel_plane *plane;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001240
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001241 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1242 assert_plane_disabled(plane);
Jesse Barnes19332d72013-03-28 09:55:38 -07001243}
1244
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001245static void assert_vblank_disabled(struct drm_crtc *crtc)
1246{
Rob Clarke2c719b2014-12-15 13:56:32 -05001247 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001248 drm_crtc_vblank_put(crtc);
1249}
1250
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001251void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1252 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001253{
Jesse Barnes92f25842011-01-04 15:09:34 -08001254 u32 val;
1255 bool enabled;
1256
Ville Syrjälä649636e2015-09-22 19:50:01 +03001257 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001258 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001259 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001260 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1261 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001262}
1263
Jesse Barnes291906f2011-02-02 12:28:03 -08001264static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001265 enum pipe pipe, enum port port,
1266 i915_reg_t dp_reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001267{
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001268 enum pipe port_pipe;
1269 bool state;
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001270
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001271 state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1272
1273 I915_STATE_WARN(state && port_pipe == pipe,
1274 "PCH DP %c enabled on transcoder %c, should be disabled\n",
1275 port_name(port), pipe_name(pipe));
1276
1277 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1278 "IBX PCH DP %c still using transcoder B\n",
1279 port_name(port));
Jesse Barnes291906f2011-02-02 12:28:03 -08001280}
1281
1282static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjälä76203462018-05-14 20:24:21 +03001283 enum pipe pipe, enum port port,
1284 i915_reg_t hdmi_reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001285{
Ville Syrjälä76203462018-05-14 20:24:21 +03001286 enum pipe port_pipe;
1287 bool state;
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001288
Ville Syrjälä76203462018-05-14 20:24:21 +03001289 state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1290
1291 I915_STATE_WARN(state && port_pipe == pipe,
1292 "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1293 port_name(port), pipe_name(pipe));
1294
1295 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1296 "IBX PCH HDMI %c still using transcoder B\n",
1297 port_name(port));
Jesse Barnes291906f2011-02-02 12:28:03 -08001298}
1299
1300static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1301 enum pipe pipe)
1302{
Ville Syrjälä6102a8e2018-05-14 20:24:19 +03001303 enum pipe port_pipe;
Jesse Barnes291906f2011-02-02 12:28:03 -08001304
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001305 assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1306 assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1307 assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001308
Ville Syrjälä6102a8e2018-05-14 20:24:19 +03001309 I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1310 port_pipe == pipe,
1311 "PCH VGA enabled on transcoder %c, should be disabled\n",
1312 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001313
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001314 I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1315 port_pipe == pipe,
1316 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1317 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001318
Ville Syrjälä76203462018-05-14 20:24:21 +03001319 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1320 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1321 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001322}
1323
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001324static void _vlv_enable_pll(struct intel_crtc *crtc,
1325 const struct intel_crtc_state *pipe_config)
1326{
1327 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1328 enum pipe pipe = crtc->pipe;
1329
1330 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1331 POSTING_READ(DPLL(pipe));
1332 udelay(150);
1333
Chris Wilson2c30b432016-06-30 15:32:54 +01001334 if (intel_wait_for_register(dev_priv,
1335 DPLL(pipe),
1336 DPLL_LOCK_VLV,
1337 DPLL_LOCK_VLV,
1338 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001339 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1340}
1341
Ville Syrjäläd288f652014-10-28 13:20:22 +02001342static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001343 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001344{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001345 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001346 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001347
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001348 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001349
Daniel Vetter87442f72013-06-06 00:52:17 +02001350 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001351 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001352
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001353 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1354 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001355
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001356 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1357 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001358}
1359
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001360
1361static void _chv_enable_pll(struct intel_crtc *crtc,
1362 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001363{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001364 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001365 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001366 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001367 u32 tmp;
1368
Ville Syrjäläa5805162015-05-26 20:42:30 +03001369 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001370
1371 /* Enable back the 10bit clock to display controller */
1372 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1373 tmp |= DPIO_DCLKP_EN;
1374 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1375
Ville Syrjälä54433e92015-05-26 20:42:31 +03001376 mutex_unlock(&dev_priv->sb_lock);
1377
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001378 /*
1379 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1380 */
1381 udelay(1);
1382
1383 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001384 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001385
1386 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001387 if (intel_wait_for_register(dev_priv,
1388 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1389 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001390 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001391}
1392
1393static void chv_enable_pll(struct intel_crtc *crtc,
1394 const struct intel_crtc_state *pipe_config)
1395{
1396 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1397 enum pipe pipe = crtc->pipe;
1398
1399 assert_pipe_disabled(dev_priv, pipe);
1400
1401 /* PLL is protected by panel, make sure we can write it */
1402 assert_panel_unlocked(dev_priv, pipe);
1403
1404 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1405 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001406
Ville Syrjäläc2317752016-03-15 16:39:56 +02001407 if (pipe != PIPE_A) {
1408 /*
1409 * WaPixelRepeatModeFixForC0:chv
1410 *
1411 * DPLLCMD is AWOL. Use chicken bits to propagate
1412 * the value from DPLLBMD to either pipe B or C.
1413 */
Ville Syrjälädfa311f2017-09-13 17:08:54 +03001414 I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
Ville Syrjäläc2317752016-03-15 16:39:56 +02001415 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1416 I915_WRITE(CBR4_VLV, 0);
1417 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1418
1419 /*
1420 * DPLLB VGA mode also seems to cause problems.
1421 * We should always have it disabled.
1422 */
1423 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1424 } else {
1425 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1426 POSTING_READ(DPLL_MD(pipe));
1427 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001428}
1429
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001430static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001431{
1432 struct intel_crtc *crtc;
1433 int count = 0;
1434
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001435 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001436 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001437 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1438 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001439
1440 return count;
1441}
1442
Ville Syrjälä939994d2017-09-13 17:08:56 +03001443static void i9xx_enable_pll(struct intel_crtc *crtc,
1444 const struct intel_crtc_state *crtc_state)
Daniel Vetter87442f72013-06-06 00:52:17 +02001445{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001446 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001447 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjälä939994d2017-09-13 17:08:56 +03001448 u32 dpll = crtc_state->dpll_hw_state.dpll;
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001449 int i;
Daniel Vetter87442f72013-06-06 00:52:17 +02001450
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001451 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001452
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001453 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001454 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001455 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001456
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001457 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001458 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001459 /*
1460 * It appears to be important that we don't enable this
1461 * for the current pipe before otherwise configuring the
1462 * PLL. No idea how this should be handled if multiple
1463 * DVO outputs are enabled simultaneosly.
1464 */
1465 dpll |= DPLL_DVO_2X_MODE;
1466 I915_WRITE(DPLL(!crtc->pipe),
1467 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1468 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001469
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001470 /*
1471 * Apparently we need to have VGA mode enabled prior to changing
1472 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1473 * dividers, even though the register value does change.
1474 */
1475 I915_WRITE(reg, 0);
1476
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001477 I915_WRITE(reg, dpll);
1478
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001479 /* Wait for the clocks to stabilize. */
1480 POSTING_READ(reg);
1481 udelay(150);
1482
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001483 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001484 I915_WRITE(DPLL_MD(crtc->pipe),
Ville Syrjälä939994d2017-09-13 17:08:56 +03001485 crtc_state->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001486 } else {
1487 /* The pixel multiplier can only be updated once the
1488 * DPLL is enabled and the clocks are stable.
1489 *
1490 * So write it again.
1491 */
1492 I915_WRITE(reg, dpll);
1493 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001494
1495 /* We do this three times for luck */
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001496 for (i = 0; i < 3; i++) {
1497 I915_WRITE(reg, dpll);
1498 POSTING_READ(reg);
1499 udelay(150); /* wait for warmup */
1500 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001501}
1502
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02001503static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001504{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02001505 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001506 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001507 enum pipe pipe = crtc->pipe;
1508
1509 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001510 if (IS_I830(dev_priv) &&
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02001511 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001512 !intel_num_dvo_pipes(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001513 I915_WRITE(DPLL(PIPE_B),
1514 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1515 I915_WRITE(DPLL(PIPE_A),
1516 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1517 }
1518
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001519 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001520 if (IS_I830(dev_priv))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001521 return;
1522
1523 /* Make sure the pipe isn't still relying on us */
1524 assert_pipe_disabled(dev_priv, pipe);
1525
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001526 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001527 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001528}
1529
Jesse Barnesf6071162013-10-01 10:41:38 -07001530static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1531{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001532 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001533
1534 /* Make sure the pipe isn't still relying on us */
1535 assert_pipe_disabled(dev_priv, pipe);
1536
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001537 val = DPLL_INTEGRATED_REF_CLK_VLV |
1538 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1539 if (pipe != PIPE_A)
1540 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1541
Jesse Barnesf6071162013-10-01 10:41:38 -07001542 I915_WRITE(DPLL(pipe), val);
1543 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001544}
1545
1546static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1547{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001548 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001549 u32 val;
1550
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001551 /* Make sure the pipe isn't still relying on us */
1552 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001553
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001554 val = DPLL_SSC_REF_CLK_CHV |
1555 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001556 if (pipe != PIPE_A)
1557 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001558
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001559 I915_WRITE(DPLL(pipe), val);
1560 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001561
Ville Syrjäläa5805162015-05-26 20:42:30 +03001562 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001563
1564 /* Disable 10bit clock to display controller */
1565 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1566 val &= ~DPIO_DCLKP_EN;
1567 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1568
Ville Syrjäläa5805162015-05-26 20:42:30 +03001569 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001570}
1571
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001572void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001573 struct intel_digital_port *dport,
1574 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001575{
1576 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001577 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001578
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02001579 switch (dport->base.port) {
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001580 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001581 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001582 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001583 break;
1584 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001585 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001586 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001587 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001588 break;
1589 case PORT_D:
1590 port_mask = DPLL_PORTD_READY_MASK;
1591 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001592 break;
1593 default:
1594 BUG();
1595 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001596
Chris Wilson370004d2016-06-30 15:32:56 +01001597 if (intel_wait_for_register(dev_priv,
1598 dpll_reg, port_mask, expected_mask,
1599 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001600 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 +02001601 port_name(dport->base.port),
1602 I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001603}
1604
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001605static void ironlake_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001606{
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001607 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1608 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1609 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001610 i915_reg_t reg;
1611 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001612
Jesse Barnes040484a2011-01-03 12:14:26 -08001613 /* Make sure PCH DPLL is enabled */
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001614 assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001615
1616 /* FDI must be feeding us bits for PCH ports */
1617 assert_fdi_tx_enabled(dev_priv, pipe);
1618 assert_fdi_rx_enabled(dev_priv, pipe);
1619
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001620 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001621 /* Workaround: Set the timing override bit before enabling the
1622 * pch transcoder. */
1623 reg = TRANS_CHICKEN2(pipe);
1624 val = I915_READ(reg);
1625 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1626 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001627 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001628
Daniel Vetterab9412b2013-05-03 11:49:46 +02001629 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001630 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001631 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001632
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001633 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001634 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001635 * Make the BPC in transcoder be consistent with
1636 * that in pipeconf reg. For HDMI we must use 8bpc
1637 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001638 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001639 val &= ~PIPECONF_BPC_MASK;
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001640 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001641 val |= PIPECONF_8BPC;
1642 else
1643 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001644 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001645
1646 val &= ~TRANS_INTERLACE_MASK;
1647 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001648 if (HAS_PCH_IBX(dev_priv) &&
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001649 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001650 val |= TRANS_LEGACY_INTERLACED_ILK;
1651 else
1652 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001653 else
1654 val |= TRANS_PROGRESSIVE;
1655
Jesse Barnes040484a2011-01-03 12:14:26 -08001656 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001657 if (intel_wait_for_register(dev_priv,
1658 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1659 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001660 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001661}
1662
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001663static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001664 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001665{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001666 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001667
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001668 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001669 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001670 assert_fdi_rx_enabled(dev_priv, PIPE_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001671
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001672 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001673 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001674 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001675 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001676
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001677 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001678 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001679
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001680 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1681 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001682 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001683 else
1684 val |= TRANS_PROGRESSIVE;
1685
Daniel Vetterab9412b2013-05-03 11:49:46 +02001686 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001687 if (intel_wait_for_register(dev_priv,
1688 LPT_TRANSCONF,
1689 TRANS_STATE_ENABLE,
1690 TRANS_STATE_ENABLE,
1691 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001692 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001693}
1694
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001695static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1696 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001697{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001698 i915_reg_t reg;
1699 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001700
1701 /* FDI relies on the transcoder */
1702 assert_fdi_tx_disabled(dev_priv, pipe);
1703 assert_fdi_rx_disabled(dev_priv, pipe);
1704
Jesse Barnes291906f2011-02-02 12:28:03 -08001705 /* Ports must be off as well */
1706 assert_pch_ports_disabled(dev_priv, pipe);
1707
Daniel Vetterab9412b2013-05-03 11:49:46 +02001708 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001709 val = I915_READ(reg);
1710 val &= ~TRANS_ENABLE;
1711 I915_WRITE(reg, val);
1712 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001713 if (intel_wait_for_register(dev_priv,
1714 reg, TRANS_STATE_ENABLE, 0,
1715 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001716 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001717
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001718 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001719 /* Workaround: Clear the timing override chicken bit again. */
1720 reg = TRANS_CHICKEN2(pipe);
1721 val = I915_READ(reg);
1722 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1723 I915_WRITE(reg, val);
1724 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001725}
1726
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001727void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001728{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001729 u32 val;
1730
Daniel Vetterab9412b2013-05-03 11:49:46 +02001731 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001732 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001733 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001734 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001735 if (intel_wait_for_register(dev_priv,
1736 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1737 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001738 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001739
1740 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001741 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001742 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001743 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001744}
1745
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001746enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
Ville Syrjälä65f21302016-10-14 20:02:53 +03001747{
1748 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1749
Ville Syrjälä65f21302016-10-14 20:02:53 +03001750 if (HAS_PCH_LPT(dev_priv))
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001751 return PIPE_A;
Ville Syrjälä65f21302016-10-14 20:02:53 +03001752 else
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001753 return crtc->pipe;
Ville Syrjälä65f21302016-10-14 20:02:53 +03001754}
1755
Ville Syrjälä4972f702017-11-29 17:37:32 +02001756static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001757{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001758 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
1759 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1760 enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
Paulo Zanoni03722642014-01-17 13:51:09 -02001761 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001762 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001763 u32 val;
1764
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001765 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1766
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001767 assert_planes_disabled(crtc);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001768
Jesse Barnesb24e7172011-01-04 15:09:30 -08001769 /*
1770 * A pipe without a PLL won't actually be able to drive bits from
1771 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1772 * need the check.
1773 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001774 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001775 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001776 assert_dsi_pll_enabled(dev_priv);
1777 else
1778 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001779 } else {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001780 if (new_crtc_state->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001781 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001782 assert_fdi_rx_pll_enabled(dev_priv,
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001783 intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001784 assert_fdi_tx_pll_enabled(dev_priv,
1785 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001786 }
1787 /* FIXME: assert CPU port conditions for SNB+ */
1788 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001789
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001790 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001791 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001792 if (val & PIPECONF_ENABLE) {
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001793 /* we keep both pipes enabled on 830 */
1794 WARN_ON(!IS_I830(dev_priv));
Chris Wilson00d70b12011-03-17 07:18:29 +00001795 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001796 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001797
1798 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001799 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001800
1801 /*
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001802 * Until the pipe starts PIPEDSL reads will return a stale value,
1803 * which causes an apparent vblank timestamp jump when PIPEDSL
1804 * resets to its proper value. That also messes up the frame count
1805 * when it's derived from the timestamps. So let's wait for the
1806 * pipe to start properly before we call drm_crtc_vblank_on()
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001807 */
Ville Syrjälä4972f702017-11-29 17:37:32 +02001808 if (dev_priv->drm.max_vblank_count == 0)
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001809 intel_wait_for_pipe_scanline_moving(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001810}
1811
Ville Syrjälä4972f702017-11-29 17:37:32 +02001812static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001813{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001814 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Chris Wilsonfac5e232016-07-04 11:34:36 +01001815 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä4972f702017-11-29 17:37:32 +02001816 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001817 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001818 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001819 u32 val;
1820
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001821 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1822
Jesse Barnesb24e7172011-01-04 15:09:30 -08001823 /*
1824 * Make sure planes won't keep trying to pump pixels to us,
1825 * or we might hang the display.
1826 */
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001827 assert_planes_disabled(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001828
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001829 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001830 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001831 if ((val & PIPECONF_ENABLE) == 0)
1832 return;
1833
Ville Syrjälä67adc642014-08-15 01:21:57 +03001834 /*
1835 * Double wide has implications for planes
1836 * so best keep it disabled when not needed.
1837 */
Ville Syrjälä4972f702017-11-29 17:37:32 +02001838 if (old_crtc_state->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03001839 val &= ~PIPECONF_DOUBLE_WIDE;
1840
1841 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001842 if (!IS_I830(dev_priv))
Ville Syrjälä67adc642014-08-15 01:21:57 +03001843 val &= ~PIPECONF_ENABLE;
1844
1845 I915_WRITE(reg, val);
1846 if ((val & PIPECONF_ENABLE) == 0)
Ville Syrjälä4972f702017-11-29 17:37:32 +02001847 intel_wait_for_pipe_off(old_crtc_state);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001848}
1849
Ville Syrjälä832be822016-01-12 21:08:33 +02001850static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1851{
1852 return IS_GEN2(dev_priv) ? 2048 : 4096;
1853}
1854
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001855static unsigned int
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001856intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001857{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001858 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001859 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001860
1861 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07001862 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001863 return cpp;
1864 case I915_FORMAT_MOD_X_TILED:
1865 if (IS_GEN2(dev_priv))
1866 return 128;
1867 else
1868 return 512;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001869 case I915_FORMAT_MOD_Y_TILED_CCS:
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001870 if (color_plane == 1)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001871 return 128;
1872 /* fall through */
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001873 case I915_FORMAT_MOD_Y_TILED:
1874 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
1875 return 128;
1876 else
1877 return 512;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001878 case I915_FORMAT_MOD_Yf_TILED_CCS:
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001879 if (color_plane == 1)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001880 return 128;
1881 /* fall through */
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001882 case I915_FORMAT_MOD_Yf_TILED:
1883 switch (cpp) {
1884 case 1:
1885 return 64;
1886 case 2:
1887 case 4:
1888 return 128;
1889 case 8:
1890 case 16:
1891 return 256;
1892 default:
1893 MISSING_CASE(cpp);
1894 return cpp;
1895 }
1896 break;
1897 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001898 MISSING_CASE(fb->modifier);
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001899 return cpp;
1900 }
1901}
1902
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001903static unsigned int
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001904intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08001905{
Ben Widawsky2f075562017-03-24 14:29:48 -07001906 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä832be822016-01-12 21:08:33 +02001907 return 1;
1908 else
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001909 return intel_tile_size(to_i915(fb->dev)) /
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001910 intel_tile_width_bytes(fb, color_plane);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001911}
1912
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001913/* Return the tile dimensions in pixel units */
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001914static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001915 unsigned int *tile_width,
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001916 unsigned int *tile_height)
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001917{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001918 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
1919 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001920
1921 *tile_width = tile_width_bytes / cpp;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001922 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001923}
1924
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001925unsigned int
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001926intel_fb_align_height(const struct drm_framebuffer *fb,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001927 int color_plane, unsigned int height)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001928{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001929 unsigned int tile_height = intel_tile_height(fb, color_plane);
Ville Syrjälä832be822016-01-12 21:08:33 +02001930
1931 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08001932}
1933
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02001934unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
1935{
1936 unsigned int size = 0;
1937 int i;
1938
1939 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
1940 size += rot_info->plane[i].width * rot_info->plane[i].height;
1941
1942 return size;
1943}
1944
Daniel Vetter75c82a52015-10-14 16:51:04 +02001945static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02001946intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
1947 const struct drm_framebuffer *fb,
1948 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00001949{
Chris Wilson7b92c042017-01-14 00:28:26 +00001950 view->type = I915_GGTT_VIEW_NORMAL;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03001951 if (drm_rotation_90_or_270(rotation)) {
Chris Wilson7b92c042017-01-14 00:28:26 +00001952 view->type = I915_GGTT_VIEW_ROTATED;
Chris Wilson8bab11932017-01-14 00:28:25 +00001953 view->rotated = to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02001954 }
1955}
1956
Ville Syrjäläfabac482017-03-27 21:55:43 +03001957static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
1958{
1959 if (IS_I830(dev_priv))
1960 return 16 * 1024;
1961 else if (IS_I85X(dev_priv))
1962 return 256;
Ville Syrjäläd9e15512017-03-27 21:55:45 +03001963 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
1964 return 32;
Ville Syrjäläfabac482017-03-27 21:55:43 +03001965 else
1966 return 4 * 1024;
1967}
1968
Ville Syrjälä603525d2016-01-12 21:08:37 +02001969static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001970{
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00001971 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001972 return 256 * 1024;
Jani Nikulac0f86832016-12-07 12:13:04 +02001973 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08001974 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001975 return 128 * 1024;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00001976 else if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001977 return 4 * 1024;
1978 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03001979 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001980}
1981
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001982static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001983 int color_plane)
Ville Syrjälä603525d2016-01-12 21:08:37 +02001984{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001985 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1986
Ville Syrjäläb90c1ee2017-03-07 21:42:07 +02001987 /* AUX_DIST needs only 4K alignment */
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001988 if (color_plane == 1)
Ville Syrjäläb90c1ee2017-03-07 21:42:07 +02001989 return 4096;
1990
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001991 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07001992 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä603525d2016-01-12 21:08:37 +02001993 return intel_linear_alignment(dev_priv);
1994 case I915_FORMAT_MOD_X_TILED:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001995 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä603525d2016-01-12 21:08:37 +02001996 return 256 * 1024;
1997 return 0;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001998 case I915_FORMAT_MOD_Y_TILED_CCS:
1999 case I915_FORMAT_MOD_Yf_TILED_CCS:
Ville Syrjälä603525d2016-01-12 21:08:37 +02002000 case I915_FORMAT_MOD_Y_TILED:
2001 case I915_FORMAT_MOD_Yf_TILED:
2002 return 1 * 1024 * 1024;
2003 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002004 MISSING_CASE(fb->modifier);
Ville Syrjälä603525d2016-01-12 21:08:37 +02002005 return 0;
2006 }
2007}
2008
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002009static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2010{
2011 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2012 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2013
Ville Syrjälä32febd92018-02-21 18:02:33 +02002014 return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002015}
2016
Chris Wilson058d88c2016-08-15 10:49:06 +01002017struct i915_vma *
Chris Wilson59354852018-02-20 13:42:06 +00002018intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002019 const struct i915_ggtt_view *view,
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002020 bool uses_fence,
Chris Wilson59354852018-02-20 13:42:06 +00002021 unsigned long *out_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002022{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002023 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002024 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002025 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Chris Wilson058d88c2016-08-15 10:49:06 +01002026 struct i915_vma *vma;
Chris Wilson59354852018-02-20 13:42:06 +00002027 unsigned int pinctl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002028 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002029
Matt Roperebcdd392014-07-09 16:22:11 -07002030 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2031
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002032 alignment = intel_surf_alignment(fb, 0);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002033
Chris Wilson693db182013-03-05 14:52:39 +00002034 /* Note that the w/a also requires 64 PTE of padding following the
2035 * bo. We currently fill all unused PTE with the shadow page and so
2036 * we should always have valid PTE following the scanout preventing
2037 * the VT-d warning.
2038 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002039 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002040 alignment = 256 * 1024;
2041
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002042 /*
2043 * Global gtt pte registers are special registers which actually forward
2044 * writes to a chunk of system memory. Which means that there is no risk
2045 * that the register values disappear as soon as we call
2046 * intel_runtime_pm_put(), so it is correct to wrap only the
2047 * pin/unpin/fence and not more.
2048 */
2049 intel_runtime_pm_get(dev_priv);
2050
Daniel Vetter9db529a2017-08-08 10:08:28 +02002051 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2052
Chris Wilson59354852018-02-20 13:42:06 +00002053 pinctl = 0;
2054
2055 /* Valleyview is definitely limited to scanning out the first
2056 * 512MiB. Lets presume this behaviour was inherited from the
2057 * g4x display engine and that all earlier gen are similarly
2058 * limited. Testing suggests that it is a little more
2059 * complicated than this. For example, Cherryview appears quite
2060 * happy to scanout from anywhere within its global aperture.
2061 */
2062 if (HAS_GMCH_DISPLAY(dev_priv))
2063 pinctl |= PIN_MAPPABLE;
2064
2065 vma = i915_gem_object_pin_to_display_plane(obj,
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002066 alignment, view, pinctl);
Chris Wilson49ef5292016-08-18 17:17:00 +01002067 if (IS_ERR(vma))
2068 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002069
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002070 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002071 int ret;
2072
Chris Wilson49ef5292016-08-18 17:17:00 +01002073 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2074 * fence, whereas 965+ only requires a fence if using
2075 * framebuffer compression. For simplicity, we always, when
2076 * possible, install a fence as the cost is not that onerous.
2077 *
2078 * If we fail to fence the tiled scanout, then either the
2079 * modeset will reject the change (which is highly unlikely as
2080 * the affected systems, all but one, do not have unmappable
2081 * space) or we will not be able to enable full powersaving
2082 * techniques (also likely not to apply due to various limits
2083 * FBC and the like impose on the size of the buffer, which
2084 * presumably we violated anyway with this unmappable buffer).
2085 * Anyway, it is presumably better to stumble onwards with
2086 * something and try to run the system in a "less than optimal"
2087 * mode that matches the user configuration.
2088 */
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002089 ret = i915_vma_pin_fence(vma);
2090 if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
Chris Wilson75097022018-03-05 10:33:12 +00002091 i915_gem_object_unpin_from_display_plane(vma);
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002092 vma = ERR_PTR(ret);
2093 goto err;
2094 }
2095
2096 if (ret == 0 && vma->fence)
Chris Wilson59354852018-02-20 13:42:06 +00002097 *out_flags |= PLANE_HAS_FENCE;
Vivek Kasireddy98072162015-10-29 18:54:38 -07002098 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002099
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002100 i915_vma_get(vma);
Chris Wilson49ef5292016-08-18 17:17:00 +01002101err:
Daniel Vetter9db529a2017-08-08 10:08:28 +02002102 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2103
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002104 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002105 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002106}
2107
Chris Wilson59354852018-02-20 13:42:06 +00002108void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002109{
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002110 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002111
Chris Wilson59354852018-02-20 13:42:06 +00002112 if (flags & PLANE_HAS_FENCE)
2113 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002114 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002115 i915_vma_put(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002116}
2117
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002118static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002119 unsigned int rotation)
2120{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002121 if (drm_rotation_90_or_270(rotation))
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002122 return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002123 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002124 return fb->pitches[color_plane];
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002125}
2126
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002127/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002128 * Convert the x/y offsets into a linear offset.
2129 * Only valid with 0/180 degree rotation, which is fine since linear
2130 * offset is only used with linear buffers on pre-hsw and tiled buffers
2131 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2132 */
2133u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002134 const struct intel_plane_state *state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002135 int color_plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002136{
Ville Syrjälä29490562016-01-20 18:02:50 +02002137 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002138 unsigned int cpp = fb->format->cpp[color_plane];
2139 unsigned int pitch = state->color_plane[color_plane].stride;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002140
2141 return y * pitch + x * cpp;
2142}
2143
2144/*
2145 * Add the x/y offsets derived from fb->offsets[] to the user
2146 * specified plane src x/y offsets. The resulting x/y offsets
2147 * specify the start of scanout from the beginning of the gtt mapping.
2148 */
2149void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002150 const struct intel_plane_state *state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002151 int color_plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002152
2153{
Ville Syrjälä29490562016-01-20 18:02:50 +02002154 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2155 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002156
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002157 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002158 *x += intel_fb->rotated[color_plane].x;
2159 *y += intel_fb->rotated[color_plane].y;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002160 } else {
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002161 *x += intel_fb->normal[color_plane].x;
2162 *y += intel_fb->normal[color_plane].y;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002163 }
2164}
2165
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002166static u32 intel_adjust_tile_offset(int *x, int *y,
2167 unsigned int tile_width,
2168 unsigned int tile_height,
2169 unsigned int tile_size,
2170 unsigned int pitch_tiles,
2171 u32 old_offset,
2172 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002173{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002174 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002175 unsigned int tiles;
2176
2177 WARN_ON(old_offset & (tile_size - 1));
2178 WARN_ON(new_offset & (tile_size - 1));
2179 WARN_ON(new_offset > old_offset);
2180
2181 tiles = (old_offset - new_offset) / tile_size;
2182
2183 *y += tiles / pitch_tiles * tile_height;
2184 *x += tiles % pitch_tiles * tile_width;
2185
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002186 /* minimize x in case it got needlessly big */
2187 *y += *x / pitch_pixels * tile_height;
2188 *x %= pitch_pixels;
2189
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002190 return new_offset;
2191}
2192
Dhinakaran Pandiyan2a11b1b2018-10-26 12:38:04 -07002193static bool is_surface_linear(u64 modifier, int color_plane)
2194{
2195 return modifier == DRM_FORMAT_MOD_LINEAR;
2196}
2197
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002198static u32 intel_adjust_aligned_offset(int *x, int *y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002199 const struct drm_framebuffer *fb,
2200 int color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002201 unsigned int rotation,
Ville Syrjälädf79cf42018-09-11 18:01:39 +03002202 unsigned int pitch,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002203 u32 old_offset, u32 new_offset)
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002204{
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002205 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002206 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002207
2208 WARN_ON(new_offset > old_offset);
2209
Dhinakaran Pandiyan2a11b1b2018-10-26 12:38:04 -07002210 if (!is_surface_linear(fb->modifier, color_plane)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002211 unsigned int tile_size, tile_width, tile_height;
2212 unsigned int pitch_tiles;
2213
2214 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002215 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002216
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002217 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002218 pitch_tiles = pitch / tile_height;
2219 swap(tile_width, tile_height);
2220 } else {
2221 pitch_tiles = pitch / (tile_width * cpp);
2222 }
2223
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002224 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2225 tile_size, pitch_tiles,
2226 old_offset, new_offset);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002227 } else {
2228 old_offset += *y * pitch + *x * cpp;
2229
2230 *y = (old_offset - new_offset) / pitch;
2231 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2232 }
2233
2234 return new_offset;
2235}
2236
2237/*
Ville Syrjälä303ba692017-08-24 22:10:49 +03002238 * Adjust the tile offset by moving the difference into
2239 * the x/y offsets.
2240 */
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002241static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2242 const struct intel_plane_state *state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002243 int color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002244 u32 old_offset, u32 new_offset)
Ville Syrjälä303ba692017-08-24 22:10:49 +03002245{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002246 return intel_adjust_aligned_offset(x, y, state->base.fb, color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002247 state->base.rotation,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002248 state->color_plane[color_plane].stride,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002249 old_offset, new_offset);
Ville Syrjälä303ba692017-08-24 22:10:49 +03002250}
2251
2252/*
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002253 * Computes the aligned offset to the base tile and adjusts
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002254 * x, y. bytes per pixel is assumed to be a power-of-two.
2255 *
2256 * In the 90/270 rotated case, x and y are assumed
2257 * to be already rotated to match the rotated GTT view, and
2258 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002259 *
2260 * This function is used when computing the derived information
2261 * under intel_framebuffer, so using any of that information
2262 * here is not allowed. Anything under drm_framebuffer can be
2263 * used. This is why the user has to pass in the pitch since it
2264 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002265 */
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002266static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2267 int *x, int *y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002268 const struct drm_framebuffer *fb,
2269 int color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002270 unsigned int pitch,
2271 unsigned int rotation,
2272 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002273{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002274 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002275 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002276
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002277 if (alignment)
2278 alignment--;
2279
Dhinakaran Pandiyan2a11b1b2018-10-26 12:38:04 -07002280 if (!is_surface_linear(fb->modifier, color_plane)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002281 unsigned int tile_size, tile_width, tile_height;
2282 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002283
Ville Syrjäläd8433102016-01-12 21:08:35 +02002284 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002285 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002286
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002287 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002288 pitch_tiles = pitch / tile_height;
2289 swap(tile_width, tile_height);
2290 } else {
2291 pitch_tiles = pitch / (tile_width * cpp);
2292 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002293
Ville Syrjäläd8433102016-01-12 21:08:35 +02002294 tile_rows = *y / tile_height;
2295 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002296
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002297 tiles = *x / tile_width;
2298 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002299
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002300 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2301 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002302
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002303 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2304 tile_size, pitch_tiles,
2305 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002306 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002307 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002308 offset_aligned = offset & ~alignment;
2309
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002310 *y = (offset & alignment) / pitch;
2311 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002312 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002313
2314 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002315}
2316
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002317static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2318 const struct intel_plane_state *state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002319 int color_plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002320{
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002321 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2322 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
Ville Syrjälä29490562016-01-20 18:02:50 +02002323 const struct drm_framebuffer *fb = state->base.fb;
2324 unsigned int rotation = state->base.rotation;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002325 int pitch = state->color_plane[color_plane].stride;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002326 u32 alignment;
2327
2328 if (intel_plane->id == PLANE_CURSOR)
2329 alignment = intel_cursor_alignment(dev_priv);
2330 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002331 alignment = intel_surf_alignment(fb, color_plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002332
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002333 return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002334 pitch, rotation, alignment);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002335}
2336
Ville Syrjälä303ba692017-08-24 22:10:49 +03002337/* Convert the fb->offset[] into x/y offsets */
2338static int intel_fb_offset_to_xy(int *x, int *y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002339 const struct drm_framebuffer *fb,
2340 int color_plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002341{
Ville Syrjälä303ba692017-08-24 22:10:49 +03002342 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002343
Ville Syrjälä303ba692017-08-24 22:10:49 +03002344 if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002345 fb->offsets[color_plane] % intel_tile_size(dev_priv))
Ville Syrjälä303ba692017-08-24 22:10:49 +03002346 return -EINVAL;
2347
2348 *x = 0;
2349 *y = 0;
2350
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002351 intel_adjust_aligned_offset(x, y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002352 fb, color_plane, DRM_MODE_ROTATE_0,
2353 fb->pitches[color_plane],
2354 fb->offsets[color_plane], 0);
Ville Syrjälä303ba692017-08-24 22:10:49 +03002355
2356 return 0;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002357}
2358
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002359static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2360{
2361 switch (fb_modifier) {
2362 case I915_FORMAT_MOD_X_TILED:
2363 return I915_TILING_X;
2364 case I915_FORMAT_MOD_Y_TILED:
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002365 case I915_FORMAT_MOD_Y_TILED_CCS:
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002366 return I915_TILING_Y;
2367 default:
2368 return I915_TILING_NONE;
2369 }
2370}
2371
Ville Syrjälä16af25f2018-01-19 16:41:52 +02002372/*
2373 * From the Sky Lake PRM:
2374 * "The Color Control Surface (CCS) contains the compression status of
2375 * the cache-line pairs. The compression state of the cache-line pair
2376 * is specified by 2 bits in the CCS. Each CCS cache-line represents
2377 * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2378 * cache-line-pairs. CCS is always Y tiled."
2379 *
2380 * Since cache line pairs refers to horizontally adjacent cache lines,
2381 * each cache line in the CCS corresponds to an area of 32x16 cache
2382 * lines on the main surface. Since each pixel is 4 bytes, this gives
2383 * us a ratio of one byte in the CCS for each 8x16 pixels in the
2384 * main surface.
2385 */
Ville Syrjäläbbfb6ce2017-08-01 09:58:12 -07002386static const struct drm_format_info ccs_formats[] = {
2387 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2388 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2389 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2390 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2391};
2392
2393static const struct drm_format_info *
2394lookup_format_info(const struct drm_format_info formats[],
2395 int num_formats, u32 format)
2396{
2397 int i;
2398
2399 for (i = 0; i < num_formats; i++) {
2400 if (formats[i].format == format)
2401 return &formats[i];
2402 }
2403
2404 return NULL;
2405}
2406
2407static const struct drm_format_info *
2408intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2409{
2410 switch (cmd->modifier[0]) {
2411 case I915_FORMAT_MOD_Y_TILED_CCS:
2412 case I915_FORMAT_MOD_Yf_TILED_CCS:
2413 return lookup_format_info(ccs_formats,
2414 ARRAY_SIZE(ccs_formats),
2415 cmd->pixel_format);
2416 default:
2417 return NULL;
2418 }
2419}
2420
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07002421bool is_ccs_modifier(u64 modifier)
2422{
2423 return modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2424 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2425}
2426
Ville Syrjälä6687c902015-09-15 13:16:41 +03002427static int
2428intel_fill_fb_info(struct drm_i915_private *dev_priv,
2429 struct drm_framebuffer *fb)
2430{
2431 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2432 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
Daniel Stonea5ff7a42018-05-18 15:30:07 +01002433 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002434 u32 gtt_offset_rotated = 0;
2435 unsigned int max_size = 0;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +02002436 int i, num_planes = fb->format->num_planes;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002437 unsigned int tile_size = intel_tile_size(dev_priv);
2438
2439 for (i = 0; i < num_planes; i++) {
2440 unsigned int width, height;
2441 unsigned int cpp, size;
2442 u32 offset;
2443 int x, y;
Ville Syrjälä303ba692017-08-24 22:10:49 +03002444 int ret;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002445
Ville Syrjälä353c8592016-12-14 23:30:57 +02002446 cpp = fb->format->cpp[i];
Ville Syrjälä145fcb12016-11-18 21:53:06 +02002447 width = drm_framebuffer_plane_width(fb->width, fb, i);
2448 height = drm_framebuffer_plane_height(fb->height, fb, i);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002449
Ville Syrjälä303ba692017-08-24 22:10:49 +03002450 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
2451 if (ret) {
2452 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2453 i, fb->offsets[i]);
2454 return ret;
2455 }
Ville Syrjälä6687c902015-09-15 13:16:41 +03002456
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07002457 if (is_ccs_modifier(fb->modifier) && i == 1) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002458 int hsub = fb->format->hsub;
2459 int vsub = fb->format->vsub;
2460 int tile_width, tile_height;
2461 int main_x, main_y;
2462 int ccs_x, ccs_y;
2463
2464 intel_tile_dims(fb, i, &tile_width, &tile_height);
Ville Syrjälä303ba692017-08-24 22:10:49 +03002465 tile_width *= hsub;
2466 tile_height *= vsub;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002467
Ville Syrjälä303ba692017-08-24 22:10:49 +03002468 ccs_x = (x * hsub) % tile_width;
2469 ccs_y = (y * vsub) % tile_height;
2470 main_x = intel_fb->normal[0].x % tile_width;
2471 main_y = intel_fb->normal[0].y % tile_height;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002472
2473 /*
2474 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2475 * x/y offsets must match between CCS and the main surface.
2476 */
2477 if (main_x != ccs_x || main_y != ccs_y) {
2478 DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2479 main_x, main_y,
2480 ccs_x, ccs_y,
2481 intel_fb->normal[0].x,
2482 intel_fb->normal[0].y,
2483 x, y);
2484 return -EINVAL;
2485 }
2486 }
2487
Ville Syrjälä6687c902015-09-15 13:16:41 +03002488 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002489 * The fence (if used) is aligned to the start of the object
2490 * so having the framebuffer wrap around across the edge of the
2491 * fenced region doesn't really work. We have no API to configure
2492 * the fence start offset within the object (nor could we probably
2493 * on gen2/3). So it's just easier if we just require that the
2494 * fb layout agrees with the fence layout. We already check that the
2495 * fb stride matches the fence stride elsewhere.
2496 */
Daniel Stonea5ff7a42018-05-18 15:30:07 +01002497 if (i == 0 && i915_gem_object_is_tiled(obj) &&
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002498 (x + width) * cpp > fb->pitches[i]) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +02002499 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2500 i, fb->offsets[i]);
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002501 return -EINVAL;
2502 }
2503
2504 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002505 * First pixel of the framebuffer from
2506 * the start of the normal gtt mapping.
2507 */
2508 intel_fb->normal[i].x = x;
2509 intel_fb->normal[i].y = y;
2510
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002511 offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
2512 fb->pitches[i],
2513 DRM_MODE_ROTATE_0,
2514 tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002515 offset /= tile_size;
2516
Dhinakaran Pandiyan2a11b1b2018-10-26 12:38:04 -07002517 if (!is_surface_linear(fb->modifier, i)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002518 unsigned int tile_width, tile_height;
2519 unsigned int pitch_tiles;
2520 struct drm_rect r;
2521
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002522 intel_tile_dims(fb, i, &tile_width, &tile_height);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002523
2524 rot_info->plane[i].offset = offset;
2525 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2526 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2527 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2528
2529 intel_fb->rotated[i].pitch =
2530 rot_info->plane[i].height * tile_height;
2531
2532 /* how many tiles does this plane need */
2533 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2534 /*
2535 * If the plane isn't horizontally tile aligned,
2536 * we need one more tile.
2537 */
2538 if (x != 0)
2539 size++;
2540
2541 /* rotate the x/y offsets to match the GTT view */
2542 r.x1 = x;
2543 r.y1 = y;
2544 r.x2 = x + width;
2545 r.y2 = y + height;
2546 drm_rect_rotate(&r,
2547 rot_info->plane[i].width * tile_width,
2548 rot_info->plane[i].height * tile_height,
Robert Fossc2c446a2017-05-19 16:50:17 -04002549 DRM_MODE_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002550 x = r.x1;
2551 y = r.y1;
2552
2553 /* rotate the tile dimensions to match the GTT view */
2554 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2555 swap(tile_width, tile_height);
2556
2557 /*
2558 * We only keep the x/y offsets, so push all of the
2559 * gtt offset into the x/y offsets.
2560 */
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002561 intel_adjust_tile_offset(&x, &y,
2562 tile_width, tile_height,
2563 tile_size, pitch_tiles,
2564 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002565
2566 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2567
2568 /*
2569 * First pixel of the framebuffer from
2570 * the start of the rotated gtt mapping.
2571 */
2572 intel_fb->rotated[i].x = x;
2573 intel_fb->rotated[i].y = y;
2574 } else {
2575 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2576 x * cpp, tile_size);
2577 }
2578
2579 /* how many tiles in total needed in the bo */
2580 max_size = max(max_size, offset + size);
2581 }
2582
Ville Syrjälä4e050472018-09-12 21:04:43 +03002583 if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
2584 DRM_DEBUG_KMS("fb too big for bo (need %llu bytes, have %zu bytes)\n",
2585 mul_u32_u32(max_size, tile_size), obj->base.size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002586 return -EINVAL;
2587 }
2588
2589 return 0;
2590}
2591
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002592static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002593{
2594 switch (format) {
2595 case DISPPLANE_8BPP:
2596 return DRM_FORMAT_C8;
2597 case DISPPLANE_BGRX555:
2598 return DRM_FORMAT_XRGB1555;
2599 case DISPPLANE_BGRX565:
2600 return DRM_FORMAT_RGB565;
2601 default:
2602 case DISPPLANE_BGRX888:
2603 return DRM_FORMAT_XRGB8888;
2604 case DISPPLANE_RGBX888:
2605 return DRM_FORMAT_XBGR8888;
2606 case DISPPLANE_BGRX101010:
2607 return DRM_FORMAT_XRGB2101010;
2608 case DISPPLANE_RGBX101010:
2609 return DRM_FORMAT_XBGR2101010;
2610 }
2611}
2612
Mahesh Kumarddf34312018-04-09 09:11:03 +05302613int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002614{
2615 switch (format) {
2616 case PLANE_CTL_FORMAT_RGB_565:
2617 return DRM_FORMAT_RGB565;
Mahesh Kumarf34a2912018-04-09 09:11:02 +05302618 case PLANE_CTL_FORMAT_NV12:
2619 return DRM_FORMAT_NV12;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002620 default:
2621 case PLANE_CTL_FORMAT_XRGB_8888:
2622 if (rgb_order) {
2623 if (alpha)
2624 return DRM_FORMAT_ABGR8888;
2625 else
2626 return DRM_FORMAT_XBGR8888;
2627 } else {
2628 if (alpha)
2629 return DRM_FORMAT_ARGB8888;
2630 else
2631 return DRM_FORMAT_XRGB8888;
2632 }
2633 case PLANE_CTL_FORMAT_XRGB_2101010:
2634 if (rgb_order)
2635 return DRM_FORMAT_XBGR2101010;
2636 else
2637 return DRM_FORMAT_XRGB2101010;
2638 }
2639}
2640
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002641static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002642intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2643 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002644{
2645 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002646 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002647 struct drm_i915_gem_object *obj = NULL;
2648 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002649 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002650 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2651 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2652 PAGE_SIZE);
2653
2654 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002655
Chris Wilsonff2652e2014-03-10 08:07:02 +00002656 if (plane_config->size == 0)
2657 return false;
2658
Paulo Zanoni3badb492015-09-23 12:52:23 -03002659 /* If the FB is too big, just don't use it since fbdev is not very
2660 * important and we should probably use that space with FBC or other
2661 * features. */
Matthew Auldb1ace602017-12-11 15:18:21 +00002662 if (size_aligned * 2 > dev_priv->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002663 return false;
2664
Imre Deak914a4fd2018-10-16 19:00:11 +03002665 switch (fb->modifier) {
2666 case DRM_FORMAT_MOD_LINEAR:
2667 case I915_FORMAT_MOD_X_TILED:
2668 case I915_FORMAT_MOD_Y_TILED:
2669 break;
2670 default:
2671 DRM_DEBUG_DRIVER("Unsupported modifier for initial FB: 0x%llx\n",
2672 fb->modifier);
2673 return false;
2674 }
2675
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002676 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00002677 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002678 base_aligned,
2679 base_aligned,
2680 size_aligned);
Chris Wilson24dbf512017-02-15 10:59:18 +00002681 mutex_unlock(&dev->struct_mutex);
2682 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002683 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002684
Imre Deak914a4fd2018-10-16 19:00:11 +03002685 switch (plane_config->tiling) {
2686 case I915_TILING_NONE:
2687 break;
2688 case I915_TILING_X:
2689 case I915_TILING_Y:
2690 obj->tiling_and_stride = fb->pitches[0] | plane_config->tiling;
2691 break;
2692 default:
2693 MISSING_CASE(plane_config->tiling);
2694 return false;
2695 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002696
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002697 mode_cmd.pixel_format = fb->format->format;
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002698 mode_cmd.width = fb->width;
2699 mode_cmd.height = fb->height;
2700 mode_cmd.pitches[0] = fb->pitches[0];
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002701 mode_cmd.modifier[0] = fb->modifier;
Daniel Vetter18c52472015-02-10 17:16:09 +00002702 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002703
Chris Wilson24dbf512017-02-15 10:59:18 +00002704 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002705 DRM_DEBUG_KMS("intel fb init failed\n");
2706 goto out_unref_obj;
2707 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002708
Jesse Barnes484b41d2014-03-07 08:57:55 -08002709
Daniel Vetterf6936e22015-03-26 12:17:05 +01002710 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002711 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002712
2713out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002714 i915_gem_object_put(obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002715 return false;
2716}
2717
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002718static void
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002719intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2720 struct intel_plane_state *plane_state,
2721 bool visible)
2722{
2723 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2724
2725 plane_state->base.visible = visible;
2726
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002727 if (visible)
Ville Syrjälä40560e22018-06-26 22:47:11 +03002728 crtc_state->base.plane_mask |= drm_plane_mask(&plane->base);
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002729 else
Ville Syrjälä40560e22018-06-26 22:47:11 +03002730 crtc_state->base.plane_mask &= ~drm_plane_mask(&plane->base);
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002731}
2732
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002733static void fixup_active_planes(struct intel_crtc_state *crtc_state)
2734{
2735 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2736 struct drm_plane *plane;
2737
2738 /*
2739 * Active_planes aliases if multiple "primary" or cursor planes
2740 * have been used on the same (or wrong) pipe. plane_mask uses
2741 * unique ids, hence we can use that to reconstruct active_planes.
2742 */
2743 crtc_state->active_planes = 0;
2744
2745 drm_for_each_plane_mask(plane, &dev_priv->drm,
2746 crtc_state->base.plane_mask)
2747 crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
2748}
2749
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002750static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
2751 struct intel_plane *plane)
2752{
2753 struct intel_crtc_state *crtc_state =
2754 to_intel_crtc_state(crtc->base.state);
2755 struct intel_plane_state *plane_state =
2756 to_intel_plane_state(plane->base.state);
2757
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +03002758 DRM_DEBUG_KMS("Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
2759 plane->base.base.id, plane->base.name,
2760 crtc->base.base.id, crtc->base.name);
2761
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002762 intel_set_plane_visible(crtc_state, plane_state, false);
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002763 fixup_active_planes(crtc_state);
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002764
2765 if (plane->id == PLANE_PRIMARY)
2766 intel_pre_disable_primary_noatomic(&crtc->base);
2767
2768 trace_intel_disable_plane(&plane->base, crtc);
2769 plane->disable_plane(plane, crtc);
2770}
2771
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002772static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002773intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2774 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002775{
2776 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002777 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002778 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002779 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002780 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002781 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002782 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002783 struct intel_plane_state *intel_state =
2784 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002785 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002786
Damien Lespiau2d140302015-02-05 17:22:18 +00002787 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002788 return;
2789
Daniel Vetterf6936e22015-03-26 12:17:05 +01002790 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002791 fb = &plane_config->fb->base;
2792 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002793 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002794
Damien Lespiau2d140302015-02-05 17:22:18 +00002795 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002796
2797 /*
2798 * Failed to alloc the obj, check to see if we should share
2799 * an fb with another CRTC instead
2800 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002801 for_each_crtc(dev, c) {
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002802 struct intel_plane_state *state;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002803
2804 if (c == &intel_crtc->base)
2805 continue;
2806
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002807 if (!to_intel_crtc(c)->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002808 continue;
2809
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002810 state = to_intel_plane_state(c->primary->state);
2811 if (!state->vma)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002812 continue;
2813
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002814 if (intel_plane_ggtt_offset(state) == plane_config->base) {
Ville Syrjälä8bc20f62018-03-22 17:22:59 +02002815 fb = state->base.fb;
Harsha Sharmac3ed1102017-10-09 17:36:43 +05302816 drm_framebuffer_get(fb);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002817 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002818 }
2819 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002820
Matt Roper200757f2015-12-03 11:37:36 -08002821 /*
2822 * We've failed to reconstruct the BIOS FB. Current display state
2823 * indicates that the primary plane is visible, but has a NULL FB,
2824 * which will lead to problems later if we don't fix it up. The
2825 * simplest solution is to just disable the primary plane now and
2826 * pretend the BIOS never had it enabled.
2827 */
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002828 intel_plane_disable_noatomic(intel_crtc, intel_plane);
Matt Roper200757f2015-12-03 11:37:36 -08002829
Daniel Vetter88595ac2015-03-26 12:42:24 +01002830 return;
2831
2832valid_fb:
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002833 intel_fill_fb_ggtt_view(&intel_state->view, fb,
2834 intel_state->base.rotation);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03002835 intel_state->color_plane[0].stride =
2836 intel_fb_pitch(fb, 0, intel_state->base.rotation);
2837
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002838 mutex_lock(&dev->struct_mutex);
2839 intel_state->vma =
Chris Wilson59354852018-02-20 13:42:06 +00002840 intel_pin_and_fence_fb_obj(fb,
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002841 &intel_state->view,
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002842 intel_plane_uses_fence(intel_state),
Chris Wilson59354852018-02-20 13:42:06 +00002843 &intel_state->flags);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002844 mutex_unlock(&dev->struct_mutex);
2845 if (IS_ERR(intel_state->vma)) {
2846 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2847 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2848
2849 intel_state->vma = NULL;
Harsha Sharmac3ed1102017-10-09 17:36:43 +05302850 drm_framebuffer_put(fb);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002851 return;
2852 }
2853
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08002854 obj = intel_fb_obj(fb);
2855 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
2856
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002857 plane_state->src_x = 0;
2858 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002859 plane_state->src_w = fb->width << 16;
2860 plane_state->src_h = fb->height << 16;
2861
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002862 plane_state->crtc_x = 0;
2863 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002864 plane_state->crtc_w = fb->width;
2865 plane_state->crtc_h = fb->height;
2866
Rob Clark1638d302016-11-05 11:08:08 -04002867 intel_state->base.src = drm_plane_state_src(plane_state);
2868 intel_state->base.dst = drm_plane_state_dest(plane_state);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002869
Chris Wilson3e510a82016-08-05 10:14:23 +01002870 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002871 dev_priv->preserve_bios_swizzle = true;
2872
Ville Syrjäläcd30fbc2018-05-25 21:50:40 +03002873 plane_state->fb = fb;
2874 plane_state->crtc = &intel_crtc->base;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002875
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002876 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2877 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002878}
2879
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002880static int skl_max_plane_width(const struct drm_framebuffer *fb,
2881 int color_plane,
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002882 unsigned int rotation)
2883{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002884 int cpp = fb->format->cpp[color_plane];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002885
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002886 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07002887 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002888 case I915_FORMAT_MOD_X_TILED:
2889 switch (cpp) {
2890 case 8:
2891 return 4096;
2892 case 4:
2893 case 2:
2894 case 1:
2895 return 8192;
2896 default:
2897 MISSING_CASE(cpp);
2898 break;
2899 }
2900 break;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002901 case I915_FORMAT_MOD_Y_TILED_CCS:
2902 case I915_FORMAT_MOD_Yf_TILED_CCS:
2903 /* FIXME AUX plane? */
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002904 case I915_FORMAT_MOD_Y_TILED:
2905 case I915_FORMAT_MOD_Yf_TILED:
2906 switch (cpp) {
2907 case 8:
2908 return 2048;
2909 case 4:
2910 return 4096;
2911 case 2:
2912 case 1:
2913 return 8192;
2914 default:
2915 MISSING_CASE(cpp);
2916 break;
2917 }
2918 break;
2919 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002920 MISSING_CASE(fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002921 }
2922
2923 return 2048;
2924}
2925
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002926static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
2927 int main_x, int main_y, u32 main_offset)
2928{
2929 const struct drm_framebuffer *fb = plane_state->base.fb;
2930 int hsub = fb->format->hsub;
2931 int vsub = fb->format->vsub;
Ville Syrjäläc11ada02018-09-07 18:24:04 +03002932 int aux_x = plane_state->color_plane[1].x;
2933 int aux_y = plane_state->color_plane[1].y;
2934 u32 aux_offset = plane_state->color_plane[1].offset;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002935 u32 alignment = intel_surf_alignment(fb, 1);
2936
2937 while (aux_offset >= main_offset && aux_y <= main_y) {
2938 int x, y;
2939
2940 if (aux_x == main_x && aux_y == main_y)
2941 break;
2942
2943 if (aux_offset == 0)
2944 break;
2945
2946 x = aux_x / hsub;
2947 y = aux_y / vsub;
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002948 aux_offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 1,
2949 aux_offset, aux_offset - alignment);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002950 aux_x = x * hsub + aux_x % hsub;
2951 aux_y = y * vsub + aux_y % vsub;
2952 }
2953
2954 if (aux_x != main_x || aux_y != main_y)
2955 return false;
2956
Ville Syrjäläc11ada02018-09-07 18:24:04 +03002957 plane_state->color_plane[1].offset = aux_offset;
2958 plane_state->color_plane[1].x = aux_x;
2959 plane_state->color_plane[1].y = aux_y;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002960
2961 return true;
2962}
2963
Ville Syrjälä73266592018-09-07 18:24:11 +03002964static int skl_check_main_surface(struct intel_plane_state *plane_state)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002965{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002966 const struct drm_framebuffer *fb = plane_state->base.fb;
2967 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002968 int x = plane_state->base.src.x1 >> 16;
2969 int y = plane_state->base.src.y1 >> 16;
2970 int w = drm_rect_width(&plane_state->base.src) >> 16;
2971 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002972 int max_width = skl_max_plane_width(fb, 0, rotation);
2973 int max_height = 4096;
Ville Syrjäläc11ada02018-09-07 18:24:04 +03002974 u32 alignment, offset, aux_offset = plane_state->color_plane[1].offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002975
2976 if (w > max_width || h > max_height) {
2977 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2978 w, h, max_width, max_height);
2979 return -EINVAL;
2980 }
2981
2982 intel_add_fb_offsets(&x, &y, plane_state, 0);
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002983 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002984 alignment = intel_surf_alignment(fb, 0);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002985
2986 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002987 * AUX surface offset is specified as the distance from the
2988 * main surface offset, and it must be non-negative. Make
2989 * sure that is what we will get.
2990 */
2991 if (offset > aux_offset)
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002992 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
2993 offset, aux_offset & ~(alignment - 1));
Ville Syrjälä8d970652016-01-28 16:30:28 +02002994
2995 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002996 * When using an X-tiled surface, the plane blows up
2997 * if the x offset + width exceed the stride.
2998 *
2999 * TODO: linear and Y-tiled seem fine, Yf untested,
3000 */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003001 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
Ville Syrjälä353c8592016-12-14 23:30:57 +02003002 int cpp = fb->format->cpp[0];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003003
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003004 while ((x + w) * cpp > plane_state->color_plane[0].stride) {
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003005 if (offset == 0) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003006 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003007 return -EINVAL;
3008 }
3009
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003010 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3011 offset, offset - alignment);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003012 }
3013 }
3014
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003015 /*
3016 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3017 * they match with the main surface x/y offsets.
3018 */
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07003019 if (is_ccs_modifier(fb->modifier)) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003020 while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
3021 if (offset == 0)
3022 break;
3023
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003024 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3025 offset, offset - alignment);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003026 }
3027
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003028 if (x != plane_state->color_plane[1].x || y != plane_state->color_plane[1].y) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003029 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
3030 return -EINVAL;
3031 }
3032 }
3033
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003034 plane_state->color_plane[0].offset = offset;
3035 plane_state->color_plane[0].x = x;
3036 plane_state->color_plane[0].y = y;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003037
3038 return 0;
3039}
3040
Ville Syrjälä8d970652016-01-28 16:30:28 +02003041static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3042{
3043 const struct drm_framebuffer *fb = plane_state->base.fb;
3044 unsigned int rotation = plane_state->base.rotation;
3045 int max_width = skl_max_plane_width(fb, 1, rotation);
3046 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02003047 int x = plane_state->base.src.x1 >> 17;
3048 int y = plane_state->base.src.y1 >> 17;
3049 int w = drm_rect_width(&plane_state->base.src) >> 17;
3050 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003051 u32 offset;
3052
3053 intel_add_fb_offsets(&x, &y, plane_state, 1);
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003054 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
Ville Syrjälä8d970652016-01-28 16:30:28 +02003055
3056 /* FIXME not quite sure how/if these apply to the chroma plane */
3057 if (w > max_width || h > max_height) {
3058 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
3059 w, h, max_width, max_height);
3060 return -EINVAL;
3061 }
3062
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003063 plane_state->color_plane[1].offset = offset;
3064 plane_state->color_plane[1].x = x;
3065 plane_state->color_plane[1].y = y;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003066
3067 return 0;
3068}
3069
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003070static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3071{
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003072 const struct drm_framebuffer *fb = plane_state->base.fb;
3073 int src_x = plane_state->base.src.x1 >> 16;
3074 int src_y = plane_state->base.src.y1 >> 16;
3075 int hsub = fb->format->hsub;
3076 int vsub = fb->format->vsub;
3077 int x = src_x / hsub;
3078 int y = src_y / vsub;
3079 u32 offset;
3080
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003081 intel_add_fb_offsets(&x, &y, plane_state, 1);
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003082 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003083
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003084 plane_state->color_plane[1].offset = offset;
3085 plane_state->color_plane[1].x = x * hsub + src_x % hsub;
3086 plane_state->color_plane[1].y = y * vsub + src_y % vsub;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003087
3088 return 0;
3089}
3090
Ville Syrjälä73266592018-09-07 18:24:11 +03003091int skl_check_plane_surface(struct intel_plane_state *plane_state)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003092{
3093 const struct drm_framebuffer *fb = plane_state->base.fb;
3094 unsigned int rotation = plane_state->base.rotation;
3095 int ret;
3096
Ville Syrjäläf5929c52018-09-07 18:24:06 +03003097 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003098 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
3099 plane_state->color_plane[1].stride = intel_fb_pitch(fb, 1, rotation);
3100
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03003101 ret = intel_plane_check_stride(plane_state);
3102 if (ret)
3103 return ret;
3104
Ville Syrjäläa5e4c7d2016-11-07 22:20:54 +02003105 if (!plane_state->base.visible)
3106 return 0;
3107
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003108 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003109 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02003110 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03003111 fb->width << 16, fb->height << 16,
Robert Fossc2c446a2017-05-19 16:50:17 -04003112 DRM_MODE_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003113
Ville Syrjälä8d970652016-01-28 16:30:28 +02003114 /*
3115 * Handle the AUX surface first since
3116 * the main surface setup depends on it.
3117 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003118 if (fb->format->format == DRM_FORMAT_NV12) {
Ville Syrjälä8d970652016-01-28 16:30:28 +02003119 ret = skl_check_nv12_aux_surface(plane_state);
3120 if (ret)
3121 return ret;
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07003122 } else if (is_ccs_modifier(fb->modifier)) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003123 ret = skl_check_ccs_aux_surface(plane_state);
3124 if (ret)
3125 return ret;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003126 } else {
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003127 plane_state->color_plane[1].offset = ~0xfff;
3128 plane_state->color_plane[1].x = 0;
3129 plane_state->color_plane[1].y = 0;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003130 }
3131
Ville Syrjälä73266592018-09-07 18:24:11 +03003132 ret = skl_check_main_surface(plane_state);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003133 if (ret)
3134 return ret;
3135
3136 return 0;
3137}
3138
Ville Syrjäläddd57132018-09-07 18:24:02 +03003139unsigned int
3140i9xx_plane_max_stride(struct intel_plane *plane,
3141 u32 pixel_format, u64 modifier,
3142 unsigned int rotation)
3143{
3144 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3145
3146 if (!HAS_GMCH_DISPLAY(dev_priv)) {
3147 return 32*1024;
3148 } else if (INTEL_GEN(dev_priv) >= 4) {
3149 if (modifier == I915_FORMAT_MOD_X_TILED)
3150 return 16*1024;
3151 else
3152 return 32*1024;
3153 } else if (INTEL_GEN(dev_priv) >= 3) {
3154 if (modifier == I915_FORMAT_MOD_X_TILED)
3155 return 8*1024;
3156 else
3157 return 16*1024;
3158 } else {
3159 if (plane->i9xx_plane == PLANE_C)
3160 return 4*1024;
3161 else
3162 return 8*1024;
3163 }
3164}
3165
Ville Syrjälä7145f602017-03-23 21:27:07 +02003166static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
3167 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07003168{
Ville Syrjälä7145f602017-03-23 21:27:07 +02003169 struct drm_i915_private *dev_priv =
3170 to_i915(plane_state->base.plane->dev);
3171 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3172 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003173 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003174 u32 dspcntr;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003175
Ville Syrjälä7145f602017-03-23 21:27:07 +02003176 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003177
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02003178 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
3179 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Ville Syrjälä7145f602017-03-23 21:27:07 +02003180 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003181
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02003182 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3183 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003184
Ville Syrjäläc154d1e2018-01-30 22:38:02 +02003185 if (INTEL_GEN(dev_priv) < 5)
Ville Syrjäläd509e282017-03-27 21:55:32 +03003186 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003187
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003188 switch (fb->format->format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02003189 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003190 dspcntr |= DISPPLANE_8BPP;
3191 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003192 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003193 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003194 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003195 case DRM_FORMAT_RGB565:
3196 dspcntr |= DISPPLANE_BGRX565;
3197 break;
3198 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003199 dspcntr |= DISPPLANE_BGRX888;
3200 break;
3201 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003202 dspcntr |= DISPPLANE_RGBX888;
3203 break;
3204 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003205 dspcntr |= DISPPLANE_BGRX101010;
3206 break;
3207 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003208 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003209 break;
3210 default:
Ville Syrjälä7145f602017-03-23 21:27:07 +02003211 MISSING_CASE(fb->format->format);
3212 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003213 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003214
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003215 if (INTEL_GEN(dev_priv) >= 4 &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003216 fb->modifier == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003217 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003218
Robert Fossc2c446a2017-05-19 16:50:17 -04003219 if (rotation & DRM_MODE_ROTATE_180)
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003220 dspcntr |= DISPPLANE_ROTATE_180;
3221
Robert Fossc2c446a2017-05-19 16:50:17 -04003222 if (rotation & DRM_MODE_REFLECT_X)
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003223 dspcntr |= DISPPLANE_MIRROR;
3224
Ville Syrjälä7145f602017-03-23 21:27:07 +02003225 return dspcntr;
3226}
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003227
Ville Syrjäläf9407ae2017-03-23 21:27:12 +02003228int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003229{
3230 struct drm_i915_private *dev_priv =
3231 to_i915(plane_state->base.plane->dev);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003232 const struct drm_framebuffer *fb = plane_state->base.fb;
3233 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003234 int src_x = plane_state->base.src.x1 >> 16;
3235 int src_y = plane_state->base.src.y1 >> 16;
3236 u32 offset;
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03003237 int ret;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003238
Ville Syrjäläf5929c52018-09-07 18:24:06 +03003239 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003240 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
3241
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03003242 ret = intel_plane_check_stride(plane_state);
3243 if (ret)
3244 return ret;
3245
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003246 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003247
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003248 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003249 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
3250 plane_state, 0);
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003251 else
3252 offset = 0;
Daniel Vettere506a0c2012-07-05 12:17:29 +02003253
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003254 /* HSW/BDW do this automagically in hardware */
3255 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003256 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3257 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3258
Robert Fossc2c446a2017-05-19 16:50:17 -04003259 if (rotation & DRM_MODE_ROTATE_180) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003260 src_x += src_w - 1;
3261 src_y += src_h - 1;
Robert Fossc2c446a2017-05-19 16:50:17 -04003262 } else if (rotation & DRM_MODE_REFLECT_X) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003263 src_x += src_w - 1;
3264 }
Sonika Jindal48404c12014-08-22 14:06:04 +05303265 }
3266
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003267 plane_state->color_plane[0].offset = offset;
3268 plane_state->color_plane[0].x = src_x;
3269 plane_state->color_plane[0].y = src_y;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003270
3271 return 0;
3272}
3273
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03003274static int
3275i9xx_plane_check(struct intel_crtc_state *crtc_state,
3276 struct intel_plane_state *plane_state)
3277{
3278 int ret;
3279
Ville Syrjälä25721f82018-09-07 18:24:12 +03003280 ret = chv_plane_check_rotation(plane_state);
3281 if (ret)
3282 return ret;
3283
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03003284 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
3285 &crtc_state->base,
3286 DRM_PLANE_HELPER_NO_SCALING,
3287 DRM_PLANE_HELPER_NO_SCALING,
3288 false, true);
3289 if (ret)
3290 return ret;
3291
3292 if (!plane_state->base.visible)
3293 return 0;
3294
3295 ret = intel_plane_check_src_coordinates(plane_state);
3296 if (ret)
3297 return ret;
3298
3299 ret = i9xx_check_plane_surface(plane_state);
3300 if (ret)
3301 return ret;
3302
3303 plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
3304
3305 return 0;
3306}
3307
Ville Syrjäläed150302017-11-17 21:19:10 +02003308static void i9xx_update_plane(struct intel_plane *plane,
3309 const struct intel_crtc_state *crtc_state,
3310 const struct intel_plane_state *plane_state)
Ville Syrjälä7145f602017-03-23 21:27:07 +02003311{
Ville Syrjäläed150302017-11-17 21:19:10 +02003312 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläed150302017-11-17 21:19:10 +02003313 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003314 u32 linear_offset;
Ville Syrjäläa0864d52017-03-23 21:27:09 +02003315 u32 dspcntr = plane_state->ctl;
Ville Syrjäläed150302017-11-17 21:19:10 +02003316 i915_reg_t reg = DSPCNTR(i9xx_plane);
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003317 int x = plane_state->color_plane[0].x;
3318 int y = plane_state->color_plane[0].y;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003319 unsigned long irqflags;
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003320 u32 dspaddr_offset;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003321
Ville Syrjälä29490562016-01-20 18:02:50 +02003322 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003323
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003324 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003325 dspaddr_offset = plane_state->color_plane[0].offset;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003326 else
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003327 dspaddr_offset = linear_offset;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003328
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003329 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3330
Ville Syrjälä78587de2017-03-09 17:44:32 +02003331 if (INTEL_GEN(dev_priv) < 4) {
3332 /* pipesrc and dspsize control the size that is scaled from,
3333 * which should always be the user's requested size.
3334 */
Ville Syrjäläed150302017-11-17 21:19:10 +02003335 I915_WRITE_FW(DSPSIZE(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003336 ((crtc_state->pipe_src_h - 1) << 16) |
3337 (crtc_state->pipe_src_w - 1));
Ville Syrjäläed150302017-11-17 21:19:10 +02003338 I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
3339 } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
3340 I915_WRITE_FW(PRIMSIZE(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003341 ((crtc_state->pipe_src_h - 1) << 16) |
3342 (crtc_state->pipe_src_w - 1));
Ville Syrjäläed150302017-11-17 21:19:10 +02003343 I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
3344 I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
Ville Syrjälä78587de2017-03-09 17:44:32 +02003345 }
3346
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003347 I915_WRITE_FW(reg, dspcntr);
Sonika Jindal48404c12014-08-22 14:06:04 +05303348
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003349 I915_WRITE_FW(DSPSTRIDE(i9xx_plane), plane_state->color_plane[0].stride);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003350 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläed150302017-11-17 21:19:10 +02003351 I915_WRITE_FW(DSPSURF(i9xx_plane),
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003352 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003353 dspaddr_offset);
Ville Syrjäläed150302017-11-17 21:19:10 +02003354 I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003355 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläed150302017-11-17 21:19:10 +02003356 I915_WRITE_FW(DSPSURF(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003357 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003358 dspaddr_offset);
Ville Syrjäläed150302017-11-17 21:19:10 +02003359 I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
3360 I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003361 } else {
Ville Syrjäläed150302017-11-17 21:19:10 +02003362 I915_WRITE_FW(DSPADDR(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003363 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003364 dspaddr_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003365 }
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003366 POSTING_READ_FW(reg);
3367
3368 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003369}
3370
Ville Syrjäläed150302017-11-17 21:19:10 +02003371static void i9xx_disable_plane(struct intel_plane *plane,
3372 struct intel_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003373{
Ville Syrjäläed150302017-11-17 21:19:10 +02003374 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3375 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003376 unsigned long irqflags;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003377
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003378 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3379
Ville Syrjäläed150302017-11-17 21:19:10 +02003380 I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
3381 if (INTEL_GEN(dev_priv) >= 4)
3382 I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003383 else
Ville Syrjäläed150302017-11-17 21:19:10 +02003384 I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
3385 POSTING_READ_FW(DSPCNTR(i9xx_plane));
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003386
3387 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003388}
3389
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003390static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
3391 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003392{
Ville Syrjäläed150302017-11-17 21:19:10 +02003393 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003394 enum intel_display_power_domain power_domain;
Ville Syrjäläed150302017-11-17 21:19:10 +02003395 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003396 bool ret;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003397 u32 val;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003398
3399 /*
3400 * Not 100% correct for planes that can move between pipes,
3401 * but that's only the case for gen2-4 which don't have any
3402 * display power wells.
3403 */
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003404 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003405 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
3406 return false;
3407
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003408 val = I915_READ(DSPCNTR(i9xx_plane));
3409
3410 ret = val & DISPLAY_PLANE_ENABLE;
3411
3412 if (INTEL_GEN(dev_priv) >= 5)
3413 *pipe = plane->pipe;
3414 else
3415 *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
3416 DISPPLANE_SEL_PIPE_SHIFT;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003417
3418 intel_display_power_put(dev_priv, power_domain);
3419
3420 return ret;
3421}
3422
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003423static u32
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003424intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
Damien Lespiaub3218032015-02-27 11:15:18 +00003425{
Ben Widawsky2f075562017-03-24 14:29:48 -07003426 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003427 return 64;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003428 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003429 return intel_tile_width_bytes(fb, color_plane);
Damien Lespiaub3218032015-02-27 11:15:18 +00003430}
3431
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003432static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3433{
3434 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003435 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003436
3437 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3438 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3439 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003440}
3441
Chandra Kondurua1b22782015-04-07 15:28:45 -07003442/*
3443 * This function detaches (aka. unbinds) unused scalers in hardware
3444 */
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003445static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003446{
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3448 const struct intel_crtc_scaler_state *scaler_state =
3449 &crtc_state->scaler_state;
Chandra Kondurua1b22782015-04-07 15:28:45 -07003450 int i;
3451
Chandra Kondurua1b22782015-04-07 15:28:45 -07003452 /* loop through and disable scalers that aren't in use */
3453 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003454 if (!scaler_state->scalers[i].in_use)
3455 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003456 }
3457}
3458
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003459u32 skl_plane_stride(const struct intel_plane_state *plane_state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003460 int color_plane)
Ville Syrjäläd2196772016-01-28 18:33:11 +02003461{
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003462 const struct drm_framebuffer *fb = plane_state->base.fb;
3463 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003464 u32 stride = plane_state->color_plane[color_plane].stride;
Ville Syrjälä1b500532017-03-07 21:42:08 +02003465
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003466 if (color_plane >= fb->format->num_planes)
Ville Syrjälä1b500532017-03-07 21:42:08 +02003467 return 0;
3468
Ville Syrjäläd2196772016-01-28 18:33:11 +02003469 /*
3470 * The stride is either expressed as a multiple of 64 bytes chunks for
3471 * linear buffers or in number of tiles for tiled buffers.
3472 */
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003473 if (drm_rotation_90_or_270(rotation))
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003474 stride /= intel_tile_height(fb, color_plane);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003475 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003476 stride /= intel_fb_stride_alignment(fb, color_plane);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003477
3478 return stride;
3479}
3480
Ville Syrjälä2e881262017-03-17 23:17:56 +02003481static u32 skl_plane_ctl_format(uint32_t pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -07003482{
Chandra Konduru6156a452015-04-27 13:48:39 -07003483 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003484 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003485 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003486 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003487 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003488 case DRM_FORMAT_XBGR8888:
James Ausmus4036c782017-11-13 10:11:28 -08003489 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003490 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003491 case DRM_FORMAT_XRGB8888:
Chandra Konduru6156a452015-04-27 13:48:39 -07003492 case DRM_FORMAT_ARGB8888:
James Ausmus4036c782017-11-13 10:11:28 -08003493 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003494 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003495 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003496 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003497 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003498 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003499 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003500 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003501 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003502 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003503 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003504 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003505 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru77224cd2018-04-09 09:11:13 +05303506 case DRM_FORMAT_NV12:
3507 return PLANE_CTL_FORMAT_NV12;
Chandra Konduru6156a452015-04-27 13:48:39 -07003508 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003509 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003510 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003511
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003512 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003513}
3514
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003515static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
James Ausmus4036c782017-11-13 10:11:28 -08003516{
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003517 if (!plane_state->base.fb->format->has_alpha)
3518 return PLANE_CTL_ALPHA_DISABLE;
3519
3520 switch (plane_state->base.pixel_blend_mode) {
3521 case DRM_MODE_BLEND_PIXEL_NONE:
3522 return PLANE_CTL_ALPHA_DISABLE;
3523 case DRM_MODE_BLEND_PREMULTI:
James Ausmus4036c782017-11-13 10:11:28 -08003524 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003525 case DRM_MODE_BLEND_COVERAGE:
3526 return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
James Ausmus4036c782017-11-13 10:11:28 -08003527 default:
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003528 MISSING_CASE(plane_state->base.pixel_blend_mode);
James Ausmus4036c782017-11-13 10:11:28 -08003529 return PLANE_CTL_ALPHA_DISABLE;
3530 }
3531}
3532
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003533static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
James Ausmus4036c782017-11-13 10:11:28 -08003534{
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003535 if (!plane_state->base.fb->format->has_alpha)
3536 return PLANE_COLOR_ALPHA_DISABLE;
3537
3538 switch (plane_state->base.pixel_blend_mode) {
3539 case DRM_MODE_BLEND_PIXEL_NONE:
3540 return PLANE_COLOR_ALPHA_DISABLE;
3541 case DRM_MODE_BLEND_PREMULTI:
James Ausmus4036c782017-11-13 10:11:28 -08003542 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003543 case DRM_MODE_BLEND_COVERAGE:
3544 return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
James Ausmus4036c782017-11-13 10:11:28 -08003545 default:
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003546 MISSING_CASE(plane_state->base.pixel_blend_mode);
James Ausmus4036c782017-11-13 10:11:28 -08003547 return PLANE_COLOR_ALPHA_DISABLE;
3548 }
3549}
3550
Ville Syrjälä2e881262017-03-17 23:17:56 +02003551static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
Chandra Konduru6156a452015-04-27 13:48:39 -07003552{
Chandra Konduru6156a452015-04-27 13:48:39 -07003553 switch (fb_modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07003554 case DRM_FORMAT_MOD_LINEAR:
Chandra Konduru6156a452015-04-27 13:48:39 -07003555 break;
3556 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003557 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003558 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003559 return PLANE_CTL_TILED_Y;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003560 case I915_FORMAT_MOD_Y_TILED_CCS:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07003561 return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003562 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003563 return PLANE_CTL_TILED_YF;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003564 case I915_FORMAT_MOD_Yf_TILED_CCS:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07003565 return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003566 default:
3567 MISSING_CASE(fb_modifier);
3568 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003569
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003570 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003571}
3572
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003573static u32 skl_plane_ctl_rotate(unsigned int rotate)
Chandra Konduru6156a452015-04-27 13:48:39 -07003574{
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003575 switch (rotate) {
Robert Fossc2c446a2017-05-19 16:50:17 -04003576 case DRM_MODE_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003577 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303578 /*
Robert Fossc2c446a2017-05-19 16:50:17 -04003579 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
Sonika Jindal1e8df162015-05-20 13:40:48 +05303580 * while i915 HW rotation is clockwise, thats why this swapping.
3581 */
Robert Fossc2c446a2017-05-19 16:50:17 -04003582 case DRM_MODE_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303583 return PLANE_CTL_ROTATE_270;
Robert Fossc2c446a2017-05-19 16:50:17 -04003584 case DRM_MODE_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003585 return PLANE_CTL_ROTATE_180;
Robert Fossc2c446a2017-05-19 16:50:17 -04003586 case DRM_MODE_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303587 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003588 default:
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003589 MISSING_CASE(rotate);
3590 }
3591
3592 return 0;
3593}
3594
3595static u32 cnl_plane_ctl_flip(unsigned int reflect)
3596{
3597 switch (reflect) {
3598 case 0:
3599 break;
3600 case DRM_MODE_REFLECT_X:
3601 return PLANE_CTL_FLIP_HORIZONTAL;
3602 case DRM_MODE_REFLECT_Y:
3603 default:
3604 MISSING_CASE(reflect);
Chandra Konduru6156a452015-04-27 13:48:39 -07003605 }
3606
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003607 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003608}
3609
Ville Syrjälä2e881262017-03-17 23:17:56 +02003610u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3611 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003612{
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003613 struct drm_i915_private *dev_priv =
3614 to_i915(plane_state->base.plane->dev);
3615 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003616 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä2e881262017-03-17 23:17:56 +02003617 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003618 u32 plane_ctl;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003619
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003620 plane_ctl = PLANE_CTL_ENABLE;
3621
James Ausmus4036c782017-11-13 10:11:28 -08003622 if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003623 plane_ctl |= skl_plane_ctl_alpha(plane_state);
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003624 plane_ctl |=
3625 PLANE_CTL_PIPE_GAMMA_ENABLE |
3626 PLANE_CTL_PIPE_CSC_ENABLE |
3627 PLANE_CTL_PLANE_GAMMA_DISABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003628
3629 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3630 plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003631
3632 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3633 plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003634 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003635
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003636 plane_ctl |= skl_plane_ctl_format(fb->format->format);
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003637 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003638 plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
3639
3640 if (INTEL_GEN(dev_priv) >= 10)
3641 plane_ctl |= cnl_plane_ctl_flip(rotation &
3642 DRM_MODE_REFLECT_MASK);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003643
Ville Syrjälä2e881262017-03-17 23:17:56 +02003644 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3645 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3646 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3647 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3648
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003649 return plane_ctl;
3650}
3651
James Ausmus4036c782017-11-13 10:11:28 -08003652u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
3653 const struct intel_plane_state *plane_state)
3654{
James Ausmus077ef1f2018-03-28 14:57:56 -07003655 struct drm_i915_private *dev_priv =
3656 to_i915(plane_state->base.plane->dev);
James Ausmus4036c782017-11-13 10:11:28 -08003657 const struct drm_framebuffer *fb = plane_state->base.fb;
Uma Shankarbfe60a02018-11-02 00:40:20 +05303658 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
James Ausmus4036c782017-11-13 10:11:28 -08003659 u32 plane_color_ctl = 0;
3660
James Ausmus077ef1f2018-03-28 14:57:56 -07003661 if (INTEL_GEN(dev_priv) < 11) {
3662 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
3663 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
3664 }
James Ausmus4036c782017-11-13 10:11:28 -08003665 plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003666 plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
James Ausmus4036c782017-11-13 10:11:28 -08003667
Uma Shankarbfe60a02018-11-02 00:40:20 +05303668 if (fb->format->is_yuv && !icl_is_hdr_plane(plane)) {
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003669 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3670 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
3671 else
3672 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003673
3674 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3675 plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
Uma Shankarbfe60a02018-11-02 00:40:20 +05303676 } else if (fb->format->is_yuv) {
3677 plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003678 }
Ville Syrjälä012d79e2018-05-21 21:56:12 +03003679
James Ausmus4036c782017-11-13 10:11:28 -08003680 return plane_color_ctl;
3681}
3682
Maarten Lankhorst73974892016-08-05 23:28:27 +03003683static int
3684__intel_display_resume(struct drm_device *dev,
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003685 struct drm_atomic_state *state,
3686 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorst73974892016-08-05 23:28:27 +03003687{
3688 struct drm_crtc_state *crtc_state;
3689 struct drm_crtc *crtc;
3690 int i, ret;
3691
Ville Syrjäläaecd36b2017-06-01 17:36:13 +03003692 intel_modeset_setup_hw_state(dev, ctx);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003693 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003694
3695 if (!state)
3696 return 0;
3697
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01003698 /*
3699 * We've duplicated the state, pointers to the old state are invalid.
3700 *
3701 * Don't attempt to use the old state until we commit the duplicated state.
3702 */
3703 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst73974892016-08-05 23:28:27 +03003704 /*
3705 * Force recalculation even if we restore
3706 * current state. With fast modeset this may not result
3707 * in a modeset when the state is compatible.
3708 */
3709 crtc_state->mode_changed = true;
3710 }
3711
3712 /* ignore any reset values/BIOS leftovers in the WM registers */
Ville Syrjälä602ae832017-03-02 19:15:02 +02003713 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3714 to_intel_atomic_state(state)->skip_intermediate_wm = true;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003715
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003716 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003717
3718 WARN_ON(ret == -EDEADLK);
3719 return ret;
3720}
3721
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003722static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3723{
Ville Syrjäläae981042016-08-05 23:28:30 +03003724 return intel_has_gpu_reset(dev_priv) &&
3725 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003726}
3727
Chris Wilsonc0336662016-05-06 15:40:21 +01003728void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003729{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003730 struct drm_device *dev = &dev_priv->drm;
3731 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3732 struct drm_atomic_state *state;
3733 int ret;
3734
Daniel Vetterce87ea12017-07-19 14:54:55 +02003735 /* reset doesn't touch the display */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00003736 if (!i915_modparams.force_reset_modeset_test &&
Daniel Vetterce87ea12017-07-19 14:54:55 +02003737 !gpu_reset_clobbers_display(dev_priv))
3738 return;
3739
Daniel Vetter9db529a2017-08-08 10:08:28 +02003740 /* We have a modeset vs reset deadlock, defensively unbreak it. */
3741 set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
3742 wake_up_all(&dev_priv->gpu_error.wait_queue);
3743
3744 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
3745 DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
3746 i915_gem_set_wedged(dev_priv);
3747 }
Daniel Vetter97154ec2017-08-08 10:08:26 +02003748
Maarten Lankhorst73974892016-08-05 23:28:27 +03003749 /*
3750 * Need mode_config.mutex so that we don't
3751 * trample ongoing ->detect() and whatnot.
3752 */
3753 mutex_lock(&dev->mode_config.mutex);
3754 drm_modeset_acquire_init(ctx, 0);
3755 while (1) {
3756 ret = drm_modeset_lock_all_ctx(dev, ctx);
3757 if (ret != -EDEADLK)
3758 break;
3759
3760 drm_modeset_backoff(ctx);
3761 }
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003762 /*
3763 * Disabling the crtcs gracefully seems nicer. Also the
3764 * g33 docs say we should at least disable all the planes.
3765 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003766 state = drm_atomic_helper_duplicate_state(dev, ctx);
3767 if (IS_ERR(state)) {
3768 ret = PTR_ERR(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003769 DRM_ERROR("Duplicating state failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003770 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003771 }
3772
3773 ret = drm_atomic_helper_disable_all(dev, ctx);
3774 if (ret) {
3775 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003776 drm_atomic_state_put(state);
3777 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003778 }
3779
3780 dev_priv->modeset_restore_state = state;
3781 state->acquire_ctx = ctx;
Ville Syrjälä75147472014-11-24 18:28:11 +02003782}
3783
Chris Wilsonc0336662016-05-06 15:40:21 +01003784void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003785{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003786 struct drm_device *dev = &dev_priv->drm;
3787 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
Chris Wilson40da1d32018-04-05 13:37:14 +01003788 struct drm_atomic_state *state;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003789 int ret;
3790
Daniel Vetterce87ea12017-07-19 14:54:55 +02003791 /* reset doesn't touch the display */
Chris Wilson40da1d32018-04-05 13:37:14 +01003792 if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
Daniel Vetterce87ea12017-07-19 14:54:55 +02003793 return;
3794
Chris Wilson40da1d32018-04-05 13:37:14 +01003795 state = fetch_and_zero(&dev_priv->modeset_restore_state);
Daniel Vetterce87ea12017-07-19 14:54:55 +02003796 if (!state)
3797 goto unlock;
3798
Ville Syrjälä75147472014-11-24 18:28:11 +02003799 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003800 if (!gpu_reset_clobbers_display(dev_priv)) {
Daniel Vetterce87ea12017-07-19 14:54:55 +02003801 /* for testing only restore the display */
3802 ret = __intel_display_resume(dev, state, ctx);
Chris Wilson942d5d02017-08-28 11:46:04 +01003803 if (ret)
3804 DRM_ERROR("Restoring old state failed with %i\n", ret);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003805 } else {
3806 /*
3807 * The display has been reset as well,
3808 * so need a full re-initialization.
3809 */
3810 intel_runtime_pm_disable_interrupts(dev_priv);
3811 intel_runtime_pm_enable_interrupts(dev_priv);
3812
Imre Deak51f59202016-09-14 13:04:13 +03003813 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003814 intel_modeset_init_hw(dev);
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02003815 intel_init_clock_gating(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003816
3817 spin_lock_irq(&dev_priv->irq_lock);
3818 if (dev_priv->display.hpd_irq_setup)
3819 dev_priv->display.hpd_irq_setup(dev_priv);
3820 spin_unlock_irq(&dev_priv->irq_lock);
3821
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003822 ret = __intel_display_resume(dev, state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003823 if (ret)
3824 DRM_ERROR("Restoring old state failed with %i\n", ret);
3825
3826 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003827 }
3828
Daniel Vetterce87ea12017-07-19 14:54:55 +02003829 drm_atomic_state_put(state);
3830unlock:
Maarten Lankhorst73974892016-08-05 23:28:27 +03003831 drm_modeset_drop_locks(ctx);
3832 drm_modeset_acquire_fini(ctx);
3833 mutex_unlock(&dev->mode_config.mutex);
Daniel Vetter9db529a2017-08-08 10:08:28 +02003834
3835 clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
Ville Syrjälä75147472014-11-24 18:28:11 +02003836}
3837
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003838static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
3839 const struct intel_crtc_state *new_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003840{
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003841 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003842 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003843
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003844 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003845 crtc->base.mode = new_crtc_state->base.mode;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003846
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003847 /*
3848 * Update pipe size and adjust fitter if needed: the reason for this is
3849 * that in compute_mode_changes we check the native mode (not the pfit
3850 * mode) to see if we can flip rather than do a full mode set. In the
3851 * fastboot case, we'll flip, but if we don't update the pipesrc and
3852 * pfit state, we'll end up with a big fb scanned out into the wrong
3853 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003854 */
3855
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003856 I915_WRITE(PIPESRC(crtc->pipe),
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003857 ((new_crtc_state->pipe_src_w - 1) << 16) |
3858 (new_crtc_state->pipe_src_h - 1));
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003859
3860 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003861 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003862 skl_detach_scalers(new_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003863
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003864 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003865 skylake_pfit_enable(new_crtc_state);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003866 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003867 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003868 ironlake_pfit_enable(new_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003869 else if (old_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003870 ironlake_pfit_disable(old_crtc_state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003871 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003872}
3873
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003874static void intel_fdi_normal_train(struct intel_crtc *crtc)
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003875{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003876 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003877 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003878 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003879 i915_reg_t reg;
3880 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003881
3882 /* enable normal train */
3883 reg = FDI_TX_CTL(pipe);
3884 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003885 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003886 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3887 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003888 } else {
3889 temp &= ~FDI_LINK_TRAIN_NONE;
3890 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003891 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003892 I915_WRITE(reg, temp);
3893
3894 reg = FDI_RX_CTL(pipe);
3895 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003896 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003897 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3898 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3899 } else {
3900 temp &= ~FDI_LINK_TRAIN_NONE;
3901 temp |= FDI_LINK_TRAIN_NONE;
3902 }
3903 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3904
3905 /* wait one idle pattern time */
3906 POSTING_READ(reg);
3907 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003908
3909 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003910 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003911 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3912 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003913}
3914
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003915/* The FDI link training functions for ILK/Ibexpeak. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003916static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3917 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003918{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003919 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003920 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003921 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003922 i915_reg_t reg;
3923 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003924
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003925 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003926 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003927
Adam Jacksone1a44742010-06-25 15:32:14 -04003928 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3929 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003930 reg = FDI_RX_IMR(pipe);
3931 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003932 temp &= ~FDI_RX_SYMBOL_LOCK;
3933 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003934 I915_WRITE(reg, temp);
3935 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003936 udelay(150);
3937
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003938 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003939 reg = FDI_TX_CTL(pipe);
3940 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003941 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003942 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003943 temp &= ~FDI_LINK_TRAIN_NONE;
3944 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003945 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003946
Chris Wilson5eddb702010-09-11 13:48:45 +01003947 reg = FDI_RX_CTL(pipe);
3948 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003949 temp &= ~FDI_LINK_TRAIN_NONE;
3950 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003951 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3952
3953 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003954 udelay(150);
3955
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003956 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003957 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3958 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3959 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003960
Chris Wilson5eddb702010-09-11 13:48:45 +01003961 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003962 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003963 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003964 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3965
3966 if ((temp & FDI_RX_BIT_LOCK)) {
3967 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003968 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003969 break;
3970 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003971 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003972 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003973 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003974
3975 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003976 reg = FDI_TX_CTL(pipe);
3977 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003978 temp &= ~FDI_LINK_TRAIN_NONE;
3979 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003980 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003981
Chris Wilson5eddb702010-09-11 13:48:45 +01003982 reg = FDI_RX_CTL(pipe);
3983 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003984 temp &= ~FDI_LINK_TRAIN_NONE;
3985 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003986 I915_WRITE(reg, temp);
3987
3988 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003989 udelay(150);
3990
Chris Wilson5eddb702010-09-11 13:48:45 +01003991 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003992 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003993 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003994 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3995
3996 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003997 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003998 DRM_DEBUG_KMS("FDI train 2 done.\n");
3999 break;
4000 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004001 }
Adam Jacksone1a44742010-06-25 15:32:14 -04004002 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01004003 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004004
4005 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004006
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004007}
4008
Akshay Joshi0206e352011-08-16 15:34:10 -04004009static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004010 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
4011 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
4012 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
4013 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
4014};
4015
4016/* The FDI link training functions for SNB/Cougarpoint. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004017static void gen6_fdi_link_train(struct intel_crtc *crtc,
4018 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004019{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004020 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004021 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004022 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004023 i915_reg_t reg;
4024 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004025
Adam Jacksone1a44742010-06-25 15:32:14 -04004026 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4027 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01004028 reg = FDI_RX_IMR(pipe);
4029 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004030 temp &= ~FDI_RX_SYMBOL_LOCK;
4031 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01004032 I915_WRITE(reg, temp);
4033
4034 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004035 udelay(150);
4036
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004037 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01004038 reg = FDI_TX_CTL(pipe);
4039 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004040 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004041 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004042 temp &= ~FDI_LINK_TRAIN_NONE;
4043 temp |= FDI_LINK_TRAIN_PATTERN_1;
4044 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4045 /* SNB-B */
4046 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01004047 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004048
Daniel Vetterd74cf322012-10-26 10:58:13 +02004049 I915_WRITE(FDI_RX_MISC(pipe),
4050 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4051
Chris Wilson5eddb702010-09-11 13:48:45 +01004052 reg = FDI_RX_CTL(pipe);
4053 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004054 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004055 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4056 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4057 } else {
4058 temp &= ~FDI_LINK_TRAIN_NONE;
4059 temp |= FDI_LINK_TRAIN_PATTERN_1;
4060 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004061 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4062
4063 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004064 udelay(150);
4065
Akshay Joshi0206e352011-08-16 15:34:10 -04004066 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004067 reg = FDI_TX_CTL(pipe);
4068 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004069 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4070 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004071 I915_WRITE(reg, temp);
4072
4073 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004074 udelay(500);
4075
Sean Paulfa37d392012-03-02 12:53:39 -05004076 for (retry = 0; retry < 5; retry++) {
4077 reg = FDI_RX_IIR(pipe);
4078 temp = I915_READ(reg);
4079 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4080 if (temp & FDI_RX_BIT_LOCK) {
4081 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4082 DRM_DEBUG_KMS("FDI train 1 done.\n");
4083 break;
4084 }
4085 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004086 }
Sean Paulfa37d392012-03-02 12:53:39 -05004087 if (retry < 5)
4088 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004089 }
4090 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004091 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004092
4093 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01004094 reg = FDI_TX_CTL(pipe);
4095 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004096 temp &= ~FDI_LINK_TRAIN_NONE;
4097 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004098 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004099 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4100 /* SNB-B */
4101 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
4102 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004103 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004104
Chris Wilson5eddb702010-09-11 13:48:45 +01004105 reg = FDI_RX_CTL(pipe);
4106 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004107 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004108 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4109 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
4110 } else {
4111 temp &= ~FDI_LINK_TRAIN_NONE;
4112 temp |= FDI_LINK_TRAIN_PATTERN_2;
4113 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004114 I915_WRITE(reg, temp);
4115
4116 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004117 udelay(150);
4118
Akshay Joshi0206e352011-08-16 15:34:10 -04004119 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004120 reg = FDI_TX_CTL(pipe);
4121 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004122 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4123 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004124 I915_WRITE(reg, temp);
4125
4126 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004127 udelay(500);
4128
Sean Paulfa37d392012-03-02 12:53:39 -05004129 for (retry = 0; retry < 5; retry++) {
4130 reg = FDI_RX_IIR(pipe);
4131 temp = I915_READ(reg);
4132 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4133 if (temp & FDI_RX_SYMBOL_LOCK) {
4134 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4135 DRM_DEBUG_KMS("FDI train 2 done.\n");
4136 break;
4137 }
4138 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004139 }
Sean Paulfa37d392012-03-02 12:53:39 -05004140 if (retry < 5)
4141 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004142 }
4143 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004144 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004145
4146 DRM_DEBUG_KMS("FDI train done.\n");
4147}
4148
Jesse Barnes357555c2011-04-28 15:09:55 -07004149/* Manual link training for Ivy Bridge A0 parts */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004150static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4151 const struct intel_crtc_state *crtc_state)
Jesse Barnes357555c2011-04-28 15:09:55 -07004152{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004153 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004154 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004155 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004156 i915_reg_t reg;
4157 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004158
4159 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4160 for train result */
4161 reg = FDI_RX_IMR(pipe);
4162 temp = I915_READ(reg);
4163 temp &= ~FDI_RX_SYMBOL_LOCK;
4164 temp &= ~FDI_RX_BIT_LOCK;
4165 I915_WRITE(reg, temp);
4166
4167 POSTING_READ(reg);
4168 udelay(150);
4169
Daniel Vetter01a415f2012-10-27 15:58:40 +02004170 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4171 I915_READ(FDI_RX_IIR(pipe)));
4172
Jesse Barnes139ccd32013-08-19 11:04:55 -07004173 /* Try each vswing and preemphasis setting twice before moving on */
4174 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4175 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004176 reg = FDI_TX_CTL(pipe);
4177 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004178 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4179 temp &= ~FDI_TX_ENABLE;
4180 I915_WRITE(reg, temp);
4181
4182 reg = FDI_RX_CTL(pipe);
4183 temp = I915_READ(reg);
4184 temp &= ~FDI_LINK_TRAIN_AUTO;
4185 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4186 temp &= ~FDI_RX_ENABLE;
4187 I915_WRITE(reg, temp);
4188
4189 /* enable CPU FDI TX and PCH FDI RX */
4190 reg = FDI_TX_CTL(pipe);
4191 temp = I915_READ(reg);
4192 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004193 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004194 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004195 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004196 temp |= snb_b_fdi_train_param[j/2];
4197 temp |= FDI_COMPOSITE_SYNC;
4198 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4199
4200 I915_WRITE(FDI_RX_MISC(pipe),
4201 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4202
4203 reg = FDI_RX_CTL(pipe);
4204 temp = I915_READ(reg);
4205 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4206 temp |= FDI_COMPOSITE_SYNC;
4207 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4208
4209 POSTING_READ(reg);
4210 udelay(1); /* should be 0.5us */
4211
4212 for (i = 0; i < 4; i++) {
4213 reg = FDI_RX_IIR(pipe);
4214 temp = I915_READ(reg);
4215 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4216
4217 if (temp & FDI_RX_BIT_LOCK ||
4218 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4219 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4220 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4221 i);
4222 break;
4223 }
4224 udelay(1); /* should be 0.5us */
4225 }
4226 if (i == 4) {
4227 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4228 continue;
4229 }
4230
4231 /* Train 2 */
4232 reg = FDI_TX_CTL(pipe);
4233 temp = I915_READ(reg);
4234 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4235 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4236 I915_WRITE(reg, temp);
4237
4238 reg = FDI_RX_CTL(pipe);
4239 temp = I915_READ(reg);
4240 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4241 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004242 I915_WRITE(reg, temp);
4243
4244 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004245 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004246
Jesse Barnes139ccd32013-08-19 11:04:55 -07004247 for (i = 0; i < 4; i++) {
4248 reg = FDI_RX_IIR(pipe);
4249 temp = I915_READ(reg);
4250 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004251
Jesse Barnes139ccd32013-08-19 11:04:55 -07004252 if (temp & FDI_RX_SYMBOL_LOCK ||
4253 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4254 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4255 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4256 i);
4257 goto train_done;
4258 }
4259 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004260 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004261 if (i == 4)
4262 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004263 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004264
Jesse Barnes139ccd32013-08-19 11:04:55 -07004265train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004266 DRM_DEBUG_KMS("FDI train done.\n");
4267}
4268
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004269static void ironlake_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004270{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
4272 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004273 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004274 i915_reg_t reg;
4275 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004276
Jesse Barnes0e23b992010-09-10 11:10:00 -07004277 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004278 reg = FDI_RX_CTL(pipe);
4279 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004280 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004281 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004282 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004283 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4284
4285 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004286 udelay(200);
4287
4288 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004289 temp = I915_READ(reg);
4290 I915_WRITE(reg, temp | FDI_PCDCLK);
4291
4292 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004293 udelay(200);
4294
Paulo Zanoni20749732012-11-23 15:30:38 -02004295 /* Enable CPU FDI TX PLL, always on for Ironlake */
4296 reg = FDI_TX_CTL(pipe);
4297 temp = I915_READ(reg);
4298 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4299 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004300
Paulo Zanoni20749732012-11-23 15:30:38 -02004301 POSTING_READ(reg);
4302 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004303 }
4304}
4305
Daniel Vetter88cefb62012-08-12 19:27:14 +02004306static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4307{
4308 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004309 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004310 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004311 i915_reg_t reg;
4312 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004313
4314 /* Switch from PCDclk to Rawclk */
4315 reg = FDI_RX_CTL(pipe);
4316 temp = I915_READ(reg);
4317 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4318
4319 /* Disable CPU FDI TX PLL */
4320 reg = FDI_TX_CTL(pipe);
4321 temp = I915_READ(reg);
4322 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4323
4324 POSTING_READ(reg);
4325 udelay(100);
4326
4327 reg = FDI_RX_CTL(pipe);
4328 temp = I915_READ(reg);
4329 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4330
4331 /* Wait for the clocks to turn off. */
4332 POSTING_READ(reg);
4333 udelay(100);
4334}
4335
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004336static void ironlake_fdi_disable(struct drm_crtc *crtc)
4337{
4338 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004339 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4341 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004342 i915_reg_t reg;
4343 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004344
4345 /* disable CPU FDI tx and PCH FDI rx */
4346 reg = FDI_TX_CTL(pipe);
4347 temp = I915_READ(reg);
4348 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4349 POSTING_READ(reg);
4350
4351 reg = FDI_RX_CTL(pipe);
4352 temp = I915_READ(reg);
4353 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004354 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004355 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4356
4357 POSTING_READ(reg);
4358 udelay(100);
4359
4360 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004361 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004362 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004363
4364 /* still set train pattern 1 */
4365 reg = FDI_TX_CTL(pipe);
4366 temp = I915_READ(reg);
4367 temp &= ~FDI_LINK_TRAIN_NONE;
4368 temp |= FDI_LINK_TRAIN_PATTERN_1;
4369 I915_WRITE(reg, temp);
4370
4371 reg = FDI_RX_CTL(pipe);
4372 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004373 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004374 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4375 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4376 } else {
4377 temp &= ~FDI_LINK_TRAIN_NONE;
4378 temp |= FDI_LINK_TRAIN_PATTERN_1;
4379 }
4380 /* BPC in FDI rx is consistent with that in PIPECONF */
4381 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004382 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004383 I915_WRITE(reg, temp);
4384
4385 POSTING_READ(reg);
4386 udelay(100);
4387}
4388
Chris Wilson49d73912016-11-29 09:50:08 +00004389bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004390{
Daniel Vetterfa058872017-07-20 19:57:52 +02004391 struct drm_crtc *crtc;
4392 bool cleanup_done;
Chris Wilson5dce5b932014-01-20 10:17:36 +00004393
Daniel Vetterfa058872017-07-20 19:57:52 +02004394 drm_for_each_crtc(crtc, &dev_priv->drm) {
4395 struct drm_crtc_commit *commit;
4396 spin_lock(&crtc->commit_lock);
4397 commit = list_first_entry_or_null(&crtc->commit_list,
4398 struct drm_crtc_commit, commit_entry);
4399 cleanup_done = commit ?
4400 try_wait_for_completion(&commit->cleanup_done) : true;
4401 spin_unlock(&crtc->commit_lock);
4402
4403 if (cleanup_done)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004404 continue;
4405
Daniel Vetterfa058872017-07-20 19:57:52 +02004406 drm_crtc_wait_one_vblank(crtc);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004407
4408 return true;
4409 }
4410
4411 return false;
4412}
4413
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004414void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004415{
4416 u32 temp;
4417
4418 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4419
4420 mutex_lock(&dev_priv->sb_lock);
4421
4422 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4423 temp |= SBI_SSCCTL_DISABLE;
4424 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4425
4426 mutex_unlock(&dev_priv->sb_lock);
4427}
4428
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004429/* Program iCLKIP clock to the desired frequency */
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004430static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004431{
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004432 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004433 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004434 int clock = crtc_state->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004435 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4436 u32 temp;
4437
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004438 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004439
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004440 /* The iCLK virtual clock root frequency is in MHz,
4441 * but the adjusted_mode->crtc_clock in in KHz. To get the
4442 * divisors, it is necessary to divide one by another, so we
4443 * convert the virtual clock precision to KHz here for higher
4444 * precision.
4445 */
4446 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004447 u32 iclk_virtual_root_freq = 172800 * 1000;
4448 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004449 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004450
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004451 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4452 clock << auxdiv);
4453 divsel = (desired_divisor / iclk_pi_range) - 2;
4454 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004455
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004456 /*
4457 * Near 20MHz is a corner case which is
4458 * out of range for the 7-bit divisor
4459 */
4460 if (divsel <= 0x7f)
4461 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004462 }
4463
4464 /* This should not happen with any sane values */
4465 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4466 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4467 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4468 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4469
4470 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 +03004471 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004472 auxdiv,
4473 divsel,
4474 phasedir,
4475 phaseinc);
4476
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004477 mutex_lock(&dev_priv->sb_lock);
4478
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004479 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004480 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004481 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4482 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4483 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4484 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4485 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4486 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004487 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004488
4489 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004490 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004491 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4492 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004493 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004494
4495 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004496 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004497 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004498 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004499
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004500 mutex_unlock(&dev_priv->sb_lock);
4501
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004502 /* Wait for initialization time */
4503 udelay(24);
4504
4505 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4506}
4507
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004508int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4509{
4510 u32 divsel, phaseinc, auxdiv;
4511 u32 iclk_virtual_root_freq = 172800 * 1000;
4512 u32 iclk_pi_range = 64;
4513 u32 desired_divisor;
4514 u32 temp;
4515
4516 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4517 return 0;
4518
4519 mutex_lock(&dev_priv->sb_lock);
4520
4521 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4522 if (temp & SBI_SSCCTL_DISABLE) {
4523 mutex_unlock(&dev_priv->sb_lock);
4524 return 0;
4525 }
4526
4527 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4528 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4529 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4530 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4531 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4532
4533 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4534 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4535 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4536
4537 mutex_unlock(&dev_priv->sb_lock);
4538
4539 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4540
4541 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4542 desired_divisor << auxdiv);
4543}
4544
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004545static void ironlake_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
Daniel Vetter275f01b22013-05-03 11:49:47 +02004546 enum pipe pch_transcoder)
4547{
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004548 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4550 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004551
4552 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4553 I915_READ(HTOTAL(cpu_transcoder)));
4554 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4555 I915_READ(HBLANK(cpu_transcoder)));
4556 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4557 I915_READ(HSYNC(cpu_transcoder)));
4558
4559 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4560 I915_READ(VTOTAL(cpu_transcoder)));
4561 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4562 I915_READ(VBLANK(cpu_transcoder)));
4563 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4564 I915_READ(VSYNC(cpu_transcoder)));
4565 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4566 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4567}
4568
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004569static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004570{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004571 uint32_t temp;
4572
4573 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004574 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004575 return;
4576
4577 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4578 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4579
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004580 temp &= ~FDI_BC_BIFURCATION_SELECT;
4581 if (enable)
4582 temp |= FDI_BC_BIFURCATION_SELECT;
4583
4584 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004585 I915_WRITE(SOUTH_CHICKEN1, temp);
4586 POSTING_READ(SOUTH_CHICKEN1);
4587}
4588
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004589static void ivybridge_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004590{
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004591 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4592 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004593
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004594 switch (crtc->pipe) {
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004595 case PIPE_A:
4596 break;
4597 case PIPE_B:
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004598 if (crtc_state->fdi_lanes > 2)
4599 cpt_set_fdi_bc_bifurcation(dev_priv, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004600 else
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004601 cpt_set_fdi_bc_bifurcation(dev_priv, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004602
4603 break;
4604 case PIPE_C:
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004605 cpt_set_fdi_bc_bifurcation(dev_priv, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004606
4607 break;
4608 default:
4609 BUG();
4610 }
4611}
4612
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004613/*
4614 * Finds the encoder associated with the given CRTC. This can only be
4615 * used when we know that the CRTC isn't feeding multiple encoders!
4616 */
4617static struct intel_encoder *
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004618intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
4619 const struct intel_crtc_state *crtc_state)
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004620{
4621 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004622 const struct drm_connector_state *connector_state;
4623 const struct drm_connector *connector;
4624 struct intel_encoder *encoder = NULL;
4625 int num_encoders = 0;
4626 int i;
4627
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004628 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004629 if (connector_state->crtc != &crtc->base)
4630 continue;
4631
4632 encoder = to_intel_encoder(connector_state->best_encoder);
4633 num_encoders++;
4634 }
4635
4636 WARN(num_encoders != 1, "%d encoders for pipe %c\n",
4637 num_encoders, pipe_name(crtc->pipe));
4638
4639 return encoder;
4640}
4641
Jesse Barnesf67a5592011-01-05 10:31:48 -08004642/*
4643 * Enable PCH resources required for PCH ports:
4644 * - PCH PLLs
4645 * - FDI training & RX/TX
4646 * - update transcoder timings
4647 * - DP transcoding bits
4648 * - transcoder
4649 */
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004650static void ironlake_pch_enable(const struct intel_atomic_state *state,
4651 const struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08004652{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004653 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004654 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004655 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004656 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004657 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004658
Daniel Vetterab9412b2013-05-03 11:49:46 +02004659 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004660
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004661 if (IS_IVYBRIDGE(dev_priv))
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004662 ivybridge_update_fdi_bc_bifurcation(crtc_state);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004663
Daniel Vettercd986ab2012-10-26 10:58:12 +02004664 /* Write the TU size bits before fdi link training, so that error
4665 * detection works. */
4666 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4667 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4668
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004669 /* For PCH output, training FDI link */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004670 dev_priv->display.fdi_link_train(crtc, crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004671
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004672 /* We need to program the right clock selection before writing the pixel
4673 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004674 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004675 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004676
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004677 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004678 temp |= TRANS_DPLL_ENABLE(pipe);
4679 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004680 if (crtc_state->shared_dpll ==
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004681 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004682 temp |= sel;
4683 else
4684 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004685 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004686 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004687
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004688 /* XXX: pch pll's can be enabled any time before we enable the PCH
4689 * transcoder, and we actually should do this to not upset any PCH
4690 * transcoder that already use the clock when we share it.
4691 *
4692 * Note that enable_shared_dpll tries to do the right thing, but
4693 * get_shared_dpll unconditionally resets the pll - we need that to have
4694 * the right LVDS enable sequence. */
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02004695 intel_enable_shared_dpll(crtc_state);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004696
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004697 /* set transcoder timing, panel must allow it */
4698 assert_panel_unlocked(dev_priv, pipe);
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004699 ironlake_pch_transcoder_set_timings(crtc_state, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004700
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004701 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004702
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004703 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004704 if (HAS_PCH_CPT(dev_priv) &&
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004705 intel_crtc_has_dp_encoder(crtc_state)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004706 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004707 &crtc_state->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004708 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004709 i915_reg_t reg = TRANS_DP_CTL(pipe);
Ville Syrjäläf67dc6d2018-05-18 18:29:26 +03004710 enum port port;
4711
Chris Wilson5eddb702010-09-11 13:48:45 +01004712 temp = I915_READ(reg);
4713 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004714 TRANS_DP_SYNC_MASK |
4715 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004716 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004717 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004718
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004719 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004720 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004721 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004722 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004723
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004724 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
Ville Syrjäläf67dc6d2018-05-18 18:29:26 +03004725 WARN_ON(port < PORT_B || port > PORT_D);
4726 temp |= TRANS_DP_PORT_SEL(port);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004727
Chris Wilson5eddb702010-09-11 13:48:45 +01004728 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004729 }
4730
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02004731 ironlake_enable_pch_transcoder(crtc_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004732}
4733
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004734static void lpt_pch_enable(const struct intel_atomic_state *state,
4735 const struct intel_crtc_state *crtc_state)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004736{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004737 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004738 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004739 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004740
Matthias Kaehlckea2196032017-07-17 11:14:03 -07004741 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004742
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004743 lpt_program_iclkip(crtc_state);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004744
Paulo Zanoni0540e482012-10-31 18:12:40 -02004745 /* Set transcoder timing. */
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004746 ironlake_pch_transcoder_set_timings(crtc_state, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004747
Paulo Zanoni937bb612012-10-31 18:12:47 -02004748 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004749}
4750
Daniel Vettera1520312013-05-03 11:49:50 +02004751static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004752{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004753 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004754 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004755 u32 temp;
4756
4757 temp = I915_READ(dslreg);
4758 udelay(500);
4759 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004760 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004761 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004762 }
4763}
4764
Ville Syrjälä0a599522018-05-21 21:56:13 +03004765/*
4766 * The hardware phase 0.0 refers to the center of the pixel.
4767 * We want to start from the top/left edge which is phase
4768 * -0.5. That matches how the hardware calculates the scaling
4769 * factors (from top-left of the first pixel to bottom-right
4770 * of the last pixel, as opposed to the pixel centers).
4771 *
4772 * For 4:2:0 subsampled chroma planes we obviously have to
4773 * adjust that so that the chroma sample position lands in
4774 * the right spot.
4775 *
4776 * Note that for packed YCbCr 4:2:2 formats there is no way to
4777 * control chroma siting. The hardware simply replicates the
4778 * chroma samples for both of the luma samples, and thus we don't
4779 * actually get the expected MPEG2 chroma siting convention :(
4780 * The same behaviour is observed on pre-SKL platforms as well.
4781 */
4782u16 skl_scaler_calc_phase(int sub, bool chroma_cosited)
4783{
4784 int phase = -0x8000;
4785 u16 trip = 0;
4786
4787 if (chroma_cosited)
4788 phase += (sub - 1) * 0x8000 / sub;
4789
4790 if (phase < 0)
4791 phase = 0x10000 + phase;
4792 else
4793 trip = PS_PHASE_TRIP;
4794
4795 return ((phase >> 2) & PS_PHASE_MASK) | trip;
4796}
4797
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004798static int
4799skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004800 unsigned int scaler_user, int *scaler_id,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304801 int src_w, int src_h, int dst_w, int dst_h,
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004802 const struct drm_format_info *format, bool need_scaler)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004803{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004804 struct intel_crtc_scaler_state *scaler_state =
4805 &crtc_state->scaler_state;
4806 struct intel_crtc *intel_crtc =
4807 to_intel_crtc(crtc_state->base.crtc);
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304808 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4809 const struct drm_display_mode *adjusted_mode =
4810 &crtc_state->base.adjusted_mode;
Chandra Konduru6156a452015-04-27 13:48:39 -07004811
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004812 /*
4813 * Src coordinates are already rotated by 270 degrees for
4814 * the 90/270 degree plane rotation cases (to match the
4815 * GTT mapping), hence no need to account for rotation here.
4816 */
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004817 if (src_w != dst_w || src_h != dst_h)
4818 need_scaler = true;
Shashank Sharmae5c05932017-07-21 20:55:05 +05304819
Chandra Kondurua1b22782015-04-07 15:28:45 -07004820 /*
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304821 * Scaling/fitting not supported in IF-ID mode in GEN9+
4822 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4823 * Once NV12 is enabled, handle it here while allocating scaler
4824 * for NV12.
4825 */
4826 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004827 need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304828 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4829 return -EINVAL;
4830 }
4831
4832 /*
Chandra Kondurua1b22782015-04-07 15:28:45 -07004833 * if plane is being disabled or scaler is no more required or force detach
4834 * - free scaler binded to this plane/crtc
4835 * - in order to do this, update crtc->scaler_usage
4836 *
4837 * Here scaler state in crtc_state is set free so that
4838 * scaler can be assigned to other user. Actual register
4839 * update to free the scaler is done in plane/panel-fit programming.
4840 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4841 */
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004842 if (force_detach || !need_scaler) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004843 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004844 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004845 scaler_state->scalers[*scaler_id].in_use = 0;
4846
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004847 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4848 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4849 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004850 scaler_state->scaler_users);
4851 *scaler_id = -1;
4852 }
4853 return 0;
4854 }
4855
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004856 if (format && format->format == DRM_FORMAT_NV12 &&
Maarten Lankhorst5d794282018-05-12 03:03:14 +05304857 (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
Chandra Konduru77224cd2018-04-09 09:11:13 +05304858 DRM_DEBUG_KMS("NV12: src dimensions not met\n");
4859 return -EINVAL;
4860 }
4861
Chandra Kondurua1b22782015-04-07 15:28:45 -07004862 /* range checks */
4863 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
Nabendu Maiti323301a2018-03-23 10:24:18 -07004864 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4865 (IS_GEN11(dev_priv) &&
4866 (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
4867 dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
4868 (!IS_GEN11(dev_priv) &&
4869 (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4870 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004871 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004872 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004873 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004874 return -EINVAL;
4875 }
4876
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004877 /* mark this plane as a scaler user in crtc_state */
4878 scaler_state->scaler_users |= (1 << scaler_user);
4879 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4880 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4881 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4882 scaler_state->scaler_users);
4883
4884 return 0;
4885}
4886
4887/**
4888 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4889 *
4890 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004891 *
4892 * Return
4893 * 0 - scaler_usage updated successfully
4894 * error - requested scaling cannot be supported or other error condition
4895 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004896int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004897{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004898 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004899 bool need_scaler = false;
4900
4901 if (state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
4902 need_scaler = true;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004903
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004904 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304905 &state->scaler_state.scaler_id,
4906 state->pipe_src_w, state->pipe_src_h,
4907 adjusted_mode->crtc_hdisplay,
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004908 adjusted_mode->crtc_vdisplay, NULL, need_scaler);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004909}
4910
4911/**
4912 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
Chris Wilsonc38c1452018-02-14 13:49:22 +00004913 * @crtc_state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004914 * @plane_state: atomic plane state to update
4915 *
4916 * Return
4917 * 0 - scaler_usage updated successfully
4918 * error - requested scaling cannot be supported or other error condition
4919 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004920static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4921 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004922{
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004923 struct intel_plane *intel_plane =
4924 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004925 struct drm_framebuffer *fb = plane_state->base.fb;
4926 int ret;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004927 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004928 bool need_scaler = false;
4929
4930 /* Pre-gen11 and SDR planes always need a scaler for planar formats. */
4931 if (!icl_is_hdr_plane(intel_plane) &&
4932 fb && fb->format->format == DRM_FORMAT_NV12)
4933 need_scaler = true;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004934
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004935 ret = skl_update_scaler(crtc_state, force_detach,
4936 drm_plane_index(&intel_plane->base),
4937 &plane_state->scaler_id,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004938 drm_rect_width(&plane_state->base.src) >> 16,
4939 drm_rect_height(&plane_state->base.src) >> 16,
4940 drm_rect_width(&plane_state->base.dst),
Chandra Konduru77224cd2018-04-09 09:11:13 +05304941 drm_rect_height(&plane_state->base.dst),
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004942 fb ? fb->format : NULL, need_scaler);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004943
4944 if (ret || plane_state->scaler_id < 0)
4945 return ret;
4946
Chandra Kondurua1b22782015-04-07 15:28:45 -07004947 /* check colorkey */
Ville Syrjälä6ec5bd32018-02-02 22:42:31 +02004948 if (plane_state->ckey.flags) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004949 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4950 intel_plane->base.base.id,
4951 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004952 return -EINVAL;
4953 }
4954
4955 /* Check src format */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004956 switch (fb->format->format) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004957 case DRM_FORMAT_RGB565:
4958 case DRM_FORMAT_XBGR8888:
4959 case DRM_FORMAT_XRGB8888:
4960 case DRM_FORMAT_ABGR8888:
4961 case DRM_FORMAT_ARGB8888:
4962 case DRM_FORMAT_XRGB2101010:
4963 case DRM_FORMAT_XBGR2101010:
4964 case DRM_FORMAT_YUYV:
4965 case DRM_FORMAT_YVYU:
4966 case DRM_FORMAT_UYVY:
4967 case DRM_FORMAT_VYUY:
Chandra Konduru77224cd2018-04-09 09:11:13 +05304968 case DRM_FORMAT_NV12:
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004969 break;
4970 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004971 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4972 intel_plane->base.base.id, intel_plane->base.name,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004973 fb->base.id, fb->format->format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004974 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004975 }
4976
Chandra Kondurua1b22782015-04-07 15:28:45 -07004977 return 0;
4978}
4979
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004980static void skylake_scaler_disable(struct intel_crtc *crtc)
4981{
4982 int i;
4983
4984 for (i = 0; i < crtc->num_scalers; i++)
4985 skl_detach_scaler(crtc, i);
4986}
4987
Maarten Lankhorstb2562712018-10-04 11:45:53 +02004988static void skylake_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004989{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02004990 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4991 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4992 enum pipe pipe = crtc->pipe;
4993 const struct intel_crtc_scaler_state *scaler_state =
4994 &crtc_state->scaler_state;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004995
Maarten Lankhorstb2562712018-10-04 11:45:53 +02004996 if (crtc_state->pch_pfit.enabled) {
Ville Syrjälä0a599522018-05-21 21:56:13 +03004997 u16 uv_rgb_hphase, uv_rgb_vphase;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004998 int id;
4999
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005000 if (WARN_ON(crtc_state->scaler_state.scaler_id < 0))
Chandra Kondurua1b22782015-04-07 15:28:45 -07005001 return;
Chandra Kondurua1b22782015-04-07 15:28:45 -07005002
Ville Syrjälä0a599522018-05-21 21:56:13 +03005003 uv_rgb_hphase = skl_scaler_calc_phase(1, false);
5004 uv_rgb_vphase = skl_scaler_calc_phase(1, false);
5005
Chandra Kondurua1b22782015-04-07 15:28:45 -07005006 id = scaler_state->scaler_id;
5007 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
5008 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
Ville Syrjälä0a599522018-05-21 21:56:13 +03005009 I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
5010 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
5011 I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
5012 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005013 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc_state->pch_pfit.pos);
5014 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc_state->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005015 }
5016}
5017
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005018static void ironlake_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnesb074cec2013-04-25 12:55:02 -07005019{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005020 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5021 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07005022 int pipe = crtc->pipe;
5023
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005024 if (crtc_state->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07005025 /* Force use of hard-coded filter coefficients
5026 * as some pre-programmed values are broken,
5027 * e.g. x201.
5028 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005029 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07005030 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
5031 PF_PIPE_SEL_IVB(pipe));
5032 else
5033 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005034 I915_WRITE(PF_WIN_POS(pipe), crtc_state->pch_pfit.pos);
5035 I915_WRITE(PF_WIN_SZ(pipe), crtc_state->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08005036 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005037}
5038
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005039void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005040{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005041 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03005042 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005043 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005044
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005045 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005046 return;
5047
Maarten Lankhorst307e4492016-03-23 14:33:28 +01005048 /*
5049 * We can only enable IPS after we enable a plane and wait for a vblank
5050 * This function is called from post_plane_update, which is run after
5051 * a vblank wait.
5052 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005053 WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02005054
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005055 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005056 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjälä61843f02017-09-12 18:34:11 +03005057 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
5058 IPS_ENABLE | IPS_PCODE_CONTROL));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005059 mutex_unlock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005060 /* Quoting Art Runyan: "its not safe to expect any particular
5061 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08005062 * mailbox." Moreover, the mailbox may return a bogus state,
5063 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005064 */
5065 } else {
5066 I915_WRITE(IPS_CTL, IPS_ENABLE);
5067 /* The bit only becomes 1 in the next vblank, so this wait here
5068 * is essentially intel_wait_for_vblank. If we don't have this
5069 * and don't wait for vblanks until the end of crtc_enable, then
5070 * the HW state readout code will complain that the expected
5071 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01005072 if (intel_wait_for_register(dev_priv,
5073 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
5074 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005075 DRM_ERROR("Timed out waiting for IPS enable\n");
5076 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005077}
5078
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005079void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005080{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005081 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005082 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005083 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005084
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005085 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005086 return;
5087
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005088 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005089 mutex_lock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005090 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005091 mutex_unlock(&dev_priv->pcu_lock);
Imre Deakacb3ef02018-09-05 13:00:05 +03005092 /*
5093 * Wait for PCODE to finish disabling IPS. The BSpec specified
5094 * 42ms timeout value leads to occasional timeouts so use 100ms
5095 * instead.
5096 */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01005097 if (intel_wait_for_register(dev_priv,
5098 IPS_CTL, IPS_ENABLE, 0,
Imre Deakacb3ef02018-09-05 13:00:05 +03005099 100))
Ben Widawsky23d0b132014-04-10 14:32:41 -07005100 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08005101 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005102 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08005103 POSTING_READ(IPS_CTL);
5104 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005105
5106 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005107 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005108}
5109
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005110static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005111{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005112 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005113 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005114
5115 mutex_lock(&dev->struct_mutex);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005116 (void) intel_overlay_switch_off(intel_crtc->overlay);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005117 mutex_unlock(&dev->struct_mutex);
5118 }
5119
5120 /* Let userspace switch the overlay on again. In most cases userspace
5121 * has to recompute where to put it anyway.
5122 */
5123}
5124
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005125/**
5126 * intel_post_enable_primary - Perform operations after enabling primary plane
5127 * @crtc: the CRTC whose primary plane was just enabled
Chris Wilsonc38c1452018-02-14 13:49:22 +00005128 * @new_crtc_state: the enabling state
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005129 *
5130 * Performs potentially sleeping operations that must be done after the primary
5131 * plane is enabled, such as updating FBC and IPS. Note that this may be
5132 * called due to an explicit primary plane update, or due to an implicit
5133 * re-enable that is caused when a sprite plane is updated to no longer
5134 * completely hide the primary plane.
5135 */
5136static void
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005137intel_post_enable_primary(struct drm_crtc *crtc,
5138 const struct intel_crtc_state *new_crtc_state)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005139{
5140 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005141 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5143 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005144
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005145 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005146 * Gen2 reports pipe underruns whenever all planes are disabled.
5147 * So don't enable underrun reporting before at least some planes
5148 * are enabled.
5149 * FIXME: Need to fix the logic to work when we turn off all planes
5150 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02005151 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005152 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005153 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5154
Ville Syrjäläaca7b682015-10-30 19:22:21 +02005155 /* Underruns don't always raise interrupts, so check manually. */
5156 intel_check_cpu_fifo_underruns(dev_priv);
5157 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005158}
5159
Ville Syrjälä2622a082016-03-09 19:07:26 +02005160/* FIXME get rid of this and use pre_plane_update */
5161static void
5162intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5163{
5164 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005165 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5167 int pipe = intel_crtc->pipe;
5168
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005169 /*
5170 * Gen2 reports pipe underruns whenever all planes are disabled.
5171 * So disable underrun reporting before all the planes get disabled.
5172 */
5173 if (IS_GEN2(dev_priv))
5174 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5175
5176 hsw_disable_ips(to_intel_crtc_state(crtc->state));
Ville Syrjälä2622a082016-03-09 19:07:26 +02005177
5178 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005179 * Vblank time updates from the shadow to live plane control register
5180 * are blocked if the memory self-refresh mode is active at that
5181 * moment. So to make sure the plane gets truly disabled, disable
5182 * first the self-refresh mode. The self-refresh enable bit in turn
5183 * will be checked/applied by the HW only at the next frame start
5184 * event which is after the vblank start event, so we need to have a
5185 * wait-for-vblank between disabling the plane and the pipe.
5186 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +02005187 if (HAS_GMCH_DISPLAY(dev_priv) &&
5188 intel_set_memory_cxsr(dev_priv, false))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005189 intel_wait_for_vblank(dev_priv, pipe);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005190}
5191
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005192static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
5193 const struct intel_crtc_state *new_crtc_state)
5194{
5195 if (!old_crtc_state->ips_enabled)
5196 return false;
5197
5198 if (needs_modeset(&new_crtc_state->base))
5199 return true;
5200
5201 return !new_crtc_state->ips_enabled;
5202}
5203
5204static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
5205 const struct intel_crtc_state *new_crtc_state)
5206{
5207 if (!new_crtc_state->ips_enabled)
5208 return false;
5209
5210 if (needs_modeset(&new_crtc_state->base))
5211 return true;
5212
5213 /*
5214 * We can't read out IPS on broadwell, assume the worst and
5215 * forcibly enable IPS on the first fastset.
5216 */
5217 if (new_crtc_state->update_pipe &&
5218 old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
5219 return true;
5220
5221 return !old_crtc_state->ips_enabled;
5222}
5223
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305224static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
5225 const struct intel_crtc_state *crtc_state)
5226{
5227 if (!crtc_state->nv12_planes)
5228 return false;
5229
Rodrigo Vivi1347d3c2018-10-31 09:28:45 -07005230 /* WA Display #0827: Gen9:all */
5231 if (IS_GEN9(dev_priv) && !IS_GEMINILAKE(dev_priv))
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305232 return true;
5233
5234 return false;
5235}
5236
Daniel Vetter5a21b662016-05-24 17:13:53 +02005237static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5238{
5239 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +05305240 struct drm_device *dev = crtc->base.dev;
5241 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005242 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5243 struct intel_crtc_state *pipe_config =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005244 intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
5245 crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005246 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005247 struct drm_plane_state *old_primary_state =
5248 drm_atomic_get_old_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005249
Chris Wilson5748b6a2016-08-04 16:32:38 +01005250 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005251
Daniel Vetter5a21b662016-05-24 17:13:53 +02005252 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005253 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005254
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005255 if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
5256 hsw_enable_ips(pipe_config);
5257
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005258 if (old_primary_state) {
5259 struct drm_plane_state *new_primary_state =
5260 drm_atomic_get_new_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005261
5262 intel_fbc_post_update(crtc);
5263
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005264 if (new_primary_state->visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005265 (needs_modeset(&pipe_config->base) ||
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005266 !old_primary_state->visible))
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005267 intel_post_enable_primary(&crtc->base, pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005268 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305269
5270 /* Display WA 827 */
5271 if (needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305272 !needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305273 skl_wa_clkgate(dev_priv, crtc->pipe, false);
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305274 }
Daniel Vetter5a21b662016-05-24 17:13:53 +02005275}
5276
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005277static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5278 struct intel_crtc_state *pipe_config)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005279{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005280 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005281 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005282 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005283 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5284 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005285 struct drm_plane_state *old_primary_state =
5286 drm_atomic_get_old_plane_state(old_state, primary);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005287 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005288 struct intel_atomic_state *old_intel_state =
5289 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005290
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005291 if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
5292 hsw_disable_ips(old_crtc_state);
5293
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005294 if (old_primary_state) {
5295 struct intel_plane_state *new_primary_state =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005296 intel_atomic_get_new_plane_state(old_intel_state,
5297 to_intel_plane(primary));
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005298
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005299 intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005300 /*
5301 * Gen2 reports pipe underruns whenever all planes are disabled.
5302 * So disable underrun reporting before all the planes get disabled.
5303 */
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005304 if (IS_GEN2(dev_priv) && old_primary_state->visible &&
5305 (modeset || !new_primary_state->base.visible))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005306 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005307 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005308
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305309 /* Display WA 827 */
5310 if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305311 needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305312 skl_wa_clkgate(dev_priv, crtc->pipe, true);
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305313 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305314
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02005315 /*
5316 * Vblank time updates from the shadow to live plane control register
5317 * are blocked if the memory self-refresh mode is active at that
5318 * moment. So to make sure the plane gets truly disabled, disable
5319 * first the self-refresh mode. The self-refresh enable bit in turn
5320 * will be checked/applied by the HW only at the next frame start
5321 * event which is after the vblank start event, so we need to have a
5322 * wait-for-vblank between disabling the plane and the pipe.
5323 */
5324 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5325 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5326 intel_wait_for_vblank(dev_priv, crtc->pipe);
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005327
Matt Ropered4a6a72016-02-23 17:20:13 -08005328 /*
5329 * IVB workaround: must disable low power watermarks for at least
5330 * one frame before enabling scaling. LP watermarks can be re-enabled
5331 * when scaling is disabled.
5332 *
5333 * WaCxSRDisabledForSpriteScaling:ivb
5334 */
Ville Syrjälä8e7a4422018-10-04 15:15:27 +03005335 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev) &&
5336 old_crtc_state->base.active)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005337 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005338
5339 /*
5340 * If we're doing a modeset, we're done. No need to do any pre-vblank
5341 * watermark programming here.
5342 */
5343 if (needs_modeset(&pipe_config->base))
5344 return;
5345
5346 /*
5347 * For platforms that support atomic watermarks, program the
5348 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5349 * will be the intermediate values that are safe for both pre- and
5350 * post- vblank; when vblank happens, the 'active' values will be set
5351 * to the final 'target' values and we'll do this again to get the
5352 * optimal watermarks. For gen9+ platforms, the values we program here
5353 * will be the final target values which will get automatically latched
5354 * at vblank time; no further programming will be necessary.
5355 *
5356 * If a platform hasn't been transitioned to atomic watermarks yet,
5357 * we'll continue to update watermarks the old way, if flags tell
5358 * us to.
5359 */
5360 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005361 dev_priv->display.initial_watermarks(old_intel_state,
5362 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005363 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005364 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005365}
5366
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005367static void intel_crtc_disable_planes(struct intel_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005368{
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005369 struct drm_device *dev = crtc->base.dev;
5370 struct intel_plane *plane;
5371 unsigned fb_bits = 0;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005372
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005373 intel_crtc_dpms_overlay_disable(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005374
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005375 for_each_intel_plane_on_crtc(dev, crtc, plane) {
5376 if (plane_mask & BIT(plane->id)) {
5377 plane->disable_plane(plane, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005378
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005379 fb_bits |= plane->frontbuffer_bit;
5380 }
5381 }
5382
5383 intel_frontbuffer_flip(to_i915(dev), fb_bits);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005384}
5385
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005386static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005387 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005388 struct drm_atomic_state *old_state)
5389{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005390 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005391 struct drm_connector *conn;
5392 int i;
5393
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005394 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005395 struct intel_encoder *encoder =
5396 to_intel_encoder(conn_state->best_encoder);
5397
5398 if (conn_state->crtc != crtc)
5399 continue;
5400
5401 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005402 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005403 }
5404}
5405
5406static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005407 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005408 struct drm_atomic_state *old_state)
5409{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005410 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005411 struct drm_connector *conn;
5412 int i;
5413
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005414 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005415 struct intel_encoder *encoder =
5416 to_intel_encoder(conn_state->best_encoder);
5417
5418 if (conn_state->crtc != crtc)
5419 continue;
5420
5421 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005422 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005423 }
5424}
5425
5426static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005427 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005428 struct drm_atomic_state *old_state)
5429{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005430 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005431 struct drm_connector *conn;
5432 int i;
5433
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005434 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005435 struct intel_encoder *encoder =
5436 to_intel_encoder(conn_state->best_encoder);
5437
5438 if (conn_state->crtc != crtc)
5439 continue;
5440
Jani Nikulac84c6fe2018-10-16 15:41:34 +03005441 if (encoder->enable)
5442 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005443 intel_opregion_notify_encoder(encoder, true);
5444 }
5445}
5446
5447static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005448 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005449 struct drm_atomic_state *old_state)
5450{
5451 struct drm_connector_state *old_conn_state;
5452 struct drm_connector *conn;
5453 int i;
5454
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005455 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005456 struct intel_encoder *encoder =
5457 to_intel_encoder(old_conn_state->best_encoder);
5458
5459 if (old_conn_state->crtc != crtc)
5460 continue;
5461
5462 intel_opregion_notify_encoder(encoder, false);
Jani Nikulac84c6fe2018-10-16 15:41:34 +03005463 if (encoder->disable)
5464 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005465 }
5466}
5467
5468static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005469 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005470 struct drm_atomic_state *old_state)
5471{
5472 struct drm_connector_state *old_conn_state;
5473 struct drm_connector *conn;
5474 int i;
5475
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005476 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005477 struct intel_encoder *encoder =
5478 to_intel_encoder(old_conn_state->best_encoder);
5479
5480 if (old_conn_state->crtc != crtc)
5481 continue;
5482
5483 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005484 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005485 }
5486}
5487
5488static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005489 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005490 struct drm_atomic_state *old_state)
5491{
5492 struct drm_connector_state *old_conn_state;
5493 struct drm_connector *conn;
5494 int i;
5495
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005496 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005497 struct intel_encoder *encoder =
5498 to_intel_encoder(old_conn_state->best_encoder);
5499
5500 if (old_conn_state->crtc != crtc)
5501 continue;
5502
5503 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005504 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005505 }
5506}
5507
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005508static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5509 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005510{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005511 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005512 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005513 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5515 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005516 struct intel_atomic_state *old_intel_state =
5517 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005518
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005519 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005520 return;
5521
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005522 /*
5523 * Sometimes spurious CPU pipe underruns happen during FDI
5524 * training, at least with VGA+HDMI cloning. Suppress them.
5525 *
5526 * On ILK we get an occasional spurious CPU pipe underruns
5527 * between eDP port A enable and vdd enable. Also PCH port
5528 * enable seems to result in the occasional CPU pipe underrun.
5529 *
5530 * Spurious PCH underruns also occur during PCH enabling.
5531 */
Ville Syrjälä2b5b6312018-05-24 22:04:06 +03005532 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5533 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005534
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02005535 if (pipe_config->has_pch_encoder)
5536 intel_prepare_shared_dpll(pipe_config);
Daniel Vetterb14b1052014-04-24 23:55:13 +02005537
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005538 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005539 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005540
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005541 intel_set_pipe_timings(pipe_config);
5542 intel_set_pipe_src_size(pipe_config);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005543
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005544 if (pipe_config->has_pch_encoder) {
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005545 intel_cpu_transcoder_set_m_n(pipe_config,
5546 &pipe_config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005547 }
5548
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005549 ironlake_set_pipeconf(pipe_config);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005550
Jesse Barnesf67a5592011-01-05 10:31:48 -08005551 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005552
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005553 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005554
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005555 if (pipe_config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005556 /* Note: FDI PLL enabling _must_ be done before we enable the
5557 * cpu pipes, hence this is separate from all the other fdi/pch
5558 * enabling. */
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02005559 ironlake_fdi_pll_enable(pipe_config);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005560 } else {
5561 assert_fdi_tx_disabled(dev_priv, pipe);
5562 assert_fdi_rx_disabled(dev_priv, pipe);
5563 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005564
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005565 ironlake_pfit_enable(pipe_config);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005566
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005567 /*
5568 * On ILK+ LUT must be loaded before the pipe is running but with
5569 * clocks enabled
5570 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005571 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005572
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005573 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005574 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02005575 intel_enable_pipe(pipe_config);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005576
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005577 if (pipe_config->has_pch_encoder)
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03005578 ironlake_pch_enable(old_intel_state, pipe_config);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005579
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005580 assert_vblank_disabled(crtc);
5581 drm_crtc_vblank_on(crtc);
5582
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005583 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005584
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005585 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005586 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005587
Ville Syrjäläea80a662018-05-24 22:04:05 +03005588 /*
5589 * Must wait for vblank to avoid spurious PCH FIFO underruns.
5590 * And a second vblank wait is needed at least on ILK with
5591 * some interlaced HDMI modes. Let's do the double wait always
5592 * in case there are more corner cases we don't know about.
5593 */
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005594 if (pipe_config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005595 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläea80a662018-05-24 22:04:05 +03005596 intel_wait_for_vblank(dev_priv, pipe);
5597 }
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005598 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005599 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005600}
5601
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005602/* IPS only exists on ULT machines and is tied to pipe A. */
5603static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5604{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005605 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005606}
5607
Imre Deaked69cd42017-10-02 10:55:57 +03005608static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
5609 enum pipe pipe, bool apply)
5610{
5611 u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
5612 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
5613
5614 if (apply)
5615 val |= mask;
5616 else
5617 val &= ~mask;
5618
5619 I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
5620}
5621
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005622static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
5623{
5624 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5625 enum pipe pipe = crtc->pipe;
5626 uint32_t val;
5627
Rodrigo Vivi443d5e32018-10-04 08:18:14 -07005628 val = MBUS_DBOX_A_CREDIT(2);
5629 val |= MBUS_DBOX_BW_CREDIT(1);
5630 val |= MBUS_DBOX_B_CREDIT(8);
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005631
5632 I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
5633}
5634
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005635static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5636 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005637{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005638 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005639 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005641 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005642 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005643 struct intel_atomic_state *old_intel_state =
5644 to_intel_atomic_state(old_state);
Imre Deaked69cd42017-10-02 10:55:57 +03005645 bool psl_clkgate_wa;
Vandita Kulkarnie16a3752018-06-21 20:43:56 +05305646 u32 pipe_chicken;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005647
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005648 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005649 return;
5650
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005651 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005652
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02005653 if (pipe_config->shared_dpll)
5654 intel_enable_shared_dpll(pipe_config);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005655
Paulo Zanonic27e9172018-04-27 16:14:36 -07005656 if (INTEL_GEN(dev_priv) >= 11)
5657 icl_map_plls_to_ports(crtc, pipe_config, old_state);
5658
Paulo Zanonic8af5272018-05-02 14:58:51 -07005659 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5660
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005661 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005662 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005663
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005664 if (!transcoder_is_dsi(cpu_transcoder))
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005665 intel_set_pipe_timings(pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005666
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005667 intel_set_pipe_src_size(pipe_config);
Daniel Vetter229fca92014-04-24 23:55:09 +02005668
Jani Nikula4d1de972016-03-18 17:05:42 +02005669 if (cpu_transcoder != TRANSCODER_EDP &&
5670 !transcoder_is_dsi(cpu_transcoder)) {
5671 I915_WRITE(PIPE_MULT(cpu_transcoder),
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005672 pipe_config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005673 }
5674
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005675 if (pipe_config->has_pch_encoder) {
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005676 intel_cpu_transcoder_set_m_n(pipe_config,
5677 &pipe_config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005678 }
5679
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005680 if (!transcoder_is_dsi(cpu_transcoder))
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005681 haswell_set_pipeconf(pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005682
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005683 haswell_set_pipemisc(pipe_config);
Daniel Vetter229fca92014-04-24 23:55:09 +02005684
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005685 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005686
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005687 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005688
Imre Deaked69cd42017-10-02 10:55:57 +03005689 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
5690 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005691 pipe_config->pch_pfit.enabled;
Imre Deaked69cd42017-10-02 10:55:57 +03005692 if (psl_clkgate_wa)
5693 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
5694
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005695 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005696 skylake_pfit_enable(pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005697 else
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005698 ironlake_pfit_enable(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005699
5700 /*
5701 * On ILK+ LUT must be loaded before the pipe is running but with
5702 * clocks enabled
5703 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005704 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005705
Vandita Kulkarnie16a3752018-06-21 20:43:56 +05305706 /*
5707 * Display WA #1153: enable hardware to bypass the alpha math
5708 * and rounding for per-pixel values 00 and 0xff
5709 */
5710 if (INTEL_GEN(dev_priv) >= 11) {
5711 pipe_chicken = I915_READ(PIPE_CHICKEN(pipe));
5712 if (!(pipe_chicken & PER_PIXEL_ALPHA_BYPASS_EN))
5713 I915_WRITE_FW(PIPE_CHICKEN(pipe),
5714 pipe_chicken | PER_PIXEL_ALPHA_BYPASS_EN);
5715 }
5716
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005717 intel_ddi_set_pipe_settings(pipe_config);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005718 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005719 intel_ddi_enable_transcoder_func(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005720
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005721 if (dev_priv->display.initial_watermarks != NULL)
Ville Syrjälä3125d392016-11-28 19:37:03 +02005722 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005723
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005724 if (INTEL_GEN(dev_priv) >= 11)
5725 icl_pipe_mbus_enable(intel_crtc);
5726
Jani Nikula4d1de972016-03-18 17:05:42 +02005727 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005728 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005729 intel_enable_pipe(pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005730
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005731 if (pipe_config->has_pch_encoder)
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03005732 lpt_pch_enable(old_intel_state, pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005733
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005734 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DP_MST))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005735 intel_ddi_set_vc_payload_alloc(pipe_config, true);
Dave Airlie0e32b392014-05-02 14:02:48 +10005736
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005737 assert_vblank_disabled(crtc);
5738 drm_crtc_vblank_on(crtc);
5739
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005740 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005741
Imre Deaked69cd42017-10-02 10:55:57 +03005742 if (psl_clkgate_wa) {
5743 intel_wait_for_vblank(dev_priv, pipe);
5744 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
5745 }
5746
Paulo Zanonie4916942013-09-20 16:21:19 -03005747 /* If we change the relative order between pipe/planes enabling, we need
5748 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005749 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005750 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005751 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5752 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005753 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005754}
5755
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005756static void ironlake_pfit_disable(const struct intel_crtc_state *old_crtc_state)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005757{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005758 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5759 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5760 enum pipe pipe = crtc->pipe;
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005761
5762 /* To avoid upsetting the power well on haswell only disable the pfit if
5763 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005764 if (old_crtc_state->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005765 I915_WRITE(PF_CTL(pipe), 0);
5766 I915_WRITE(PF_WIN_POS(pipe), 0);
5767 I915_WRITE(PF_WIN_SZ(pipe), 0);
5768 }
5769}
5770
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005771static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5772 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005773{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005774 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005775 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005776 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5778 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005779
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005780 /*
5781 * Sometimes spurious CPU pipe underruns happen when the
5782 * pipe is already disabled, but FDI RX/TX is still enabled.
5783 * Happens at least with VGA+HDMI cloning. Suppress them.
5784 */
Ville Syrjälä2b5b6312018-05-24 22:04:06 +03005785 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5786 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005787
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005788 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005789
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005790 drm_crtc_vblank_off(crtc);
5791 assert_vblank_disabled(crtc);
5792
Ville Syrjälä4972f702017-11-29 17:37:32 +02005793 intel_disable_pipe(old_crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005794
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005795 ironlake_pfit_disable(old_crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005796
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005797 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005798 ironlake_fdi_disable(crtc);
5799
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005800 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005801
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005802 if (old_crtc_state->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005803 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005804
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005805 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005806 i915_reg_t reg;
5807 u32 temp;
5808
Daniel Vetterd925c592013-06-05 13:34:04 +02005809 /* disable TRANS_DP_CTL */
5810 reg = TRANS_DP_CTL(pipe);
5811 temp = I915_READ(reg);
5812 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5813 TRANS_DP_PORT_SEL_MASK);
5814 temp |= TRANS_DP_PORT_SEL_NONE;
5815 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005816
Daniel Vetterd925c592013-06-05 13:34:04 +02005817 /* disable DPLL_SEL */
5818 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005819 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005820 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005821 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005822
Daniel Vetterd925c592013-06-05 13:34:04 +02005823 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005824 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005825
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005826 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005827 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005828}
5829
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005830static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5831 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005832{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005833 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005834 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Imre Deak24a28172018-06-13 20:07:06 +03005836 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005837
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005838 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005839
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005840 drm_crtc_vblank_off(crtc);
5841 assert_vblank_disabled(crtc);
5842
Jani Nikula4d1de972016-03-18 17:05:42 +02005843 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005844 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005845 intel_disable_pipe(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005846
Imre Deak24a28172018-06-13 20:07:06 +03005847 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
5848 intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005849
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005850 if (!transcoder_is_dsi(cpu_transcoder))
Clint Taylor90c3e212018-07-10 13:02:05 -07005851 intel_ddi_disable_transcoder_func(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005852
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005853 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005854 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005855 else
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005856 ironlake_pfit_disable(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005857
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005858 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Paulo Zanonic27e9172018-04-27 16:14:36 -07005859
5860 if (INTEL_GEN(dev_priv) >= 11)
5861 icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
Imre Deakbdaa29b2018-11-01 16:04:24 +02005862
5863 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005864}
5865
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005866static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005867{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005868 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5869 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005870
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005871 if (!crtc_state->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005872 return;
5873
Daniel Vetterc0b03412013-05-28 12:05:54 +02005874 /*
5875 * The panel fitter should only be adjusted whilst the pipe is disabled,
5876 * according to register description and PRM.
5877 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005878 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5879 assert_pipe_disabled(dev_priv, crtc->pipe);
5880
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005881 I915_WRITE(PFIT_PGM_RATIOS, crtc_state->gmch_pfit.pgm_ratios);
5882 I915_WRITE(PFIT_CONTROL, crtc_state->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005883
5884 /* Border color in case we don't scale up to the full screen. Black by
5885 * default, change to something else for debugging. */
5886 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005887}
5888
Mahesh Kumar176597a2018-10-04 14:20:43 +05305889bool intel_port_is_combophy(struct drm_i915_private *dev_priv, enum port port)
5890{
5891 if (port == PORT_NONE)
5892 return false;
5893
5894 if (IS_ICELAKE(dev_priv))
5895 return port <= PORT_B;
5896
5897 return false;
5898}
5899
Paulo Zanoniac213c12018-05-21 17:25:37 -07005900bool intel_port_is_tc(struct drm_i915_private *dev_priv, enum port port)
5901{
5902 if (IS_ICELAKE(dev_priv))
5903 return port >= PORT_C && port <= PORT_F;
5904
5905 return false;
5906}
5907
5908enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
5909{
5910 if (!intel_port_is_tc(dev_priv, port))
5911 return PORT_TC_NONE;
5912
5913 return port - PORT_C;
5914}
5915
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005916enum intel_display_power_domain intel_port_to_power_domain(enum port port)
Dave Airlied05410f2014-06-05 13:22:59 +10005917{
5918 switch (port) {
5919 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005920 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005921 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005922 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005923 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005924 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005925 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005926 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005927 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005928 return POWER_DOMAIN_PORT_DDI_E_LANES;
Rodrigo Vivi9787e832018-01-29 15:22:22 -08005929 case PORT_F:
5930 return POWER_DOMAIN_PORT_DDI_F_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005931 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005932 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005933 return POWER_DOMAIN_PORT_OTHER;
5934 }
5935}
5936
Imre Deak337837a2018-11-01 16:04:23 +02005937enum intel_display_power_domain
5938intel_aux_power_domain(struct intel_digital_port *dig_port)
5939{
5940 switch (dig_port->aux_ch) {
5941 case AUX_CH_A:
5942 return POWER_DOMAIN_AUX_A;
5943 case AUX_CH_B:
5944 return POWER_DOMAIN_AUX_B;
5945 case AUX_CH_C:
5946 return POWER_DOMAIN_AUX_C;
5947 case AUX_CH_D:
5948 return POWER_DOMAIN_AUX_D;
5949 case AUX_CH_E:
5950 return POWER_DOMAIN_AUX_E;
5951 case AUX_CH_F:
5952 return POWER_DOMAIN_AUX_F;
5953 default:
5954 MISSING_CASE(dig_port->aux_ch);
5955 return POWER_DOMAIN_AUX_A;
5956 }
5957}
5958
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005959static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5960 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005961{
5962 struct drm_device *dev = crtc->dev;
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005963 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005964 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5966 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005967 u64 mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005968 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005969
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005970 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005971 return 0;
5972
Imre Deak17bd6e62018-01-09 14:20:40 +02005973 mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
5974 mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005975 if (crtc_state->pch_pfit.enabled ||
5976 crtc_state->pch_pfit.force_thru)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005977 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
Imre Deak77d22dc2014-03-05 16:20:52 +02005978
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005979 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5980 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5981
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005982 mask |= BIT_ULL(intel_encoder->power_domain);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005983 }
Imre Deak319be8a2014-03-04 19:22:57 +02005984
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005985 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
Imre Deak17bd6e62018-01-09 14:20:40 +02005986 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005987
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005988 if (crtc_state->shared_dpll)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005989 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005990
Imre Deak77d22dc2014-03-05 16:20:52 +02005991 return mask;
5992}
5993
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005994static u64
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005995modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5996 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005997{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005998 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005999 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6000 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02006001 u64 domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006002
6003 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01006004 intel_crtc->enabled_power_domains = new_domains =
6005 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006006
Daniel Vetter5a21b662016-05-24 17:13:53 +02006007 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006008
6009 for_each_power_domain(domain, domains)
6010 intel_display_power_get(dev_priv, domain);
6011
Daniel Vetter5a21b662016-05-24 17:13:53 +02006012 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006013}
6014
6015static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02006016 u64 domains)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006017{
6018 enum intel_display_power_domain domain;
6019
6020 for_each_power_domain(domain, domains)
6021 intel_display_power_put(dev_priv, domain);
6022}
6023
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006024static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6025 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006026{
Ville Syrjäläff32c542017-03-02 19:14:57 +02006027 struct intel_atomic_state *old_intel_state =
6028 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006029 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006030 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006031 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006033 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006034
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006035 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006036 return;
6037
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006038 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006039 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006040
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02006041 intel_set_pipe_timings(pipe_config);
6042 intel_set_pipe_src_size(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006043
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006044 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006045 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6046 I915_WRITE(CHV_CANVAS(pipe), 0);
6047 }
6048
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02006049 i9xx_set_pipeconf(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006050
P Raviraj Sitaramc59d2da2018-09-10 19:57:14 +05306051 intel_color_set_csc(&pipe_config->base);
6052
Jesse Barnes89b667f2013-04-18 14:51:36 -07006053 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006054
Daniel Vettera72e4c92014-09-30 10:56:47 +02006055 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006056
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006057 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006058
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006059 if (IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006060 chv_prepare_pll(intel_crtc, pipe_config);
6061 chv_enable_pll(intel_crtc, pipe_config);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006062 } else {
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006063 vlv_prepare_pll(intel_crtc, pipe_config);
6064 vlv_enable_pll(intel_crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006065 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006067 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006068
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006069 i9xx_pfit_enable(pipe_config);
Jesse Barnes2dd24552013-04-25 12:55:01 -07006070
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006071 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006072
Ville Syrjäläff32c542017-03-02 19:14:57 +02006073 dev_priv->display.initial_watermarks(old_intel_state,
6074 pipe_config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02006075 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006076
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006077 assert_vblank_disabled(crtc);
6078 drm_crtc_vblank_on(crtc);
6079
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006080 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006081}
6082
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006083static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006084{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006085 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6086 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006087
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006088 I915_WRITE(FP0(crtc->pipe), crtc_state->dpll_hw_state.fp0);
6089 I915_WRITE(FP1(crtc->pipe), crtc_state->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006090}
6091
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006092static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6093 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006094{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006095 struct intel_atomic_state *old_intel_state =
6096 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006097 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006098 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006099 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006101 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006102
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006103 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006104 return;
6105
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006106 i9xx_set_pll_dividers(pipe_config);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006107
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006108 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006109 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006110
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02006111 intel_set_pipe_timings(pipe_config);
6112 intel_set_pipe_src_size(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006113
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02006114 i9xx_set_pipeconf(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006115
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006116 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006117
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006118 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006119 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006120
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006121 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006122
Ville Syrjälä939994d2017-09-13 17:08:56 +03006123 i9xx_enable_pll(intel_crtc, pipe_config);
Daniel Vetterf6736a12013-06-05 13:34:30 +02006124
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006125 i9xx_pfit_enable(pipe_config);
Jesse Barnes2dd24552013-04-25 12:55:01 -07006126
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006127 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006128
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006129 if (dev_priv->display.initial_watermarks != NULL)
6130 dev_priv->display.initial_watermarks(old_intel_state,
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006131 pipe_config);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006132 else
6133 intel_update_watermarks(intel_crtc);
Ville Syrjälä4972f702017-11-29 17:37:32 +02006134 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006135
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006136 assert_vblank_disabled(crtc);
6137 drm_crtc_vblank_on(crtc);
6138
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006139 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006140}
6141
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006142static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
Daniel Vetter87476d62013-04-11 16:29:06 +02006143{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006144 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
6145 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006146
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006147 if (!old_crtc_state->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006148 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006149
6150 assert_pipe_disabled(dev_priv, crtc->pipe);
6151
Chris Wilson43031782018-09-13 14:16:26 +01006152 DRM_DEBUG_KMS("disabling pfit, current: 0x%08x\n",
6153 I915_READ(PFIT_CONTROL));
Daniel Vetter328d8e82013-05-08 10:36:31 +02006154 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006155}
6156
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006157static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6158 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006159{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006160 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006161 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006162 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6164 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006165
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006166 /*
6167 * On gen2 planes are double buffered but the pipe isn't, so we must
6168 * wait for planes to fully turn off before disabling the pipe.
6169 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006170 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02006171 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006172
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006173 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006174
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006175 drm_crtc_vblank_off(crtc);
6176 assert_vblank_disabled(crtc);
6177
Ville Syrjälä4972f702017-11-29 17:37:32 +02006178 intel_disable_pipe(old_crtc_state);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006179
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006180 i9xx_pfit_disable(old_crtc_state);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006181
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006182 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006183
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006184 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006185 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006186 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006187 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006188 vlv_disable_pll(dev_priv, pipe);
6189 else
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006190 i9xx_disable_pll(old_crtc_state);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006191 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006192
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006193 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006194
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006195 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006196 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006197
6198 if (!dev_priv->display.initial_watermarks)
6199 intel_update_watermarks(intel_crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +03006200
6201 /* clock the pipe down to 640x480@60 to potentially save power */
6202 if (IS_I830(dev_priv))
6203 i830_enable_pipe(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006204}
6205
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006206static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
6207 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006208{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006209 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006211 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006212 enum intel_display_power_domain domain;
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006213 struct intel_plane *plane;
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02006214 u64 domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006215 struct drm_atomic_state *state;
6216 struct intel_crtc_state *crtc_state;
6217 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006218
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006219 if (!intel_crtc->active)
6220 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006221
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006222 for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
6223 const struct intel_plane_state *plane_state =
6224 to_intel_plane_state(plane->base.state);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006225
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006226 if (plane_state->base.visible)
6227 intel_plane_disable_noatomic(intel_crtc, plane);
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006228 }
6229
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006230 state = drm_atomic_state_alloc(crtc->dev);
Ander Conselvan de Oliveira31bb2ef2017-01-20 16:28:45 +02006231 if (!state) {
6232 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
6233 crtc->base.id, crtc->name);
6234 return;
6235 }
6236
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006237 state->acquire_ctx = ctx;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006238
6239 /* Everything's already locked, -EDEADLK can't happen. */
6240 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6241 ret = drm_atomic_add_affected_connectors(state, crtc);
6242
6243 WARN_ON(IS_ERR(crtc_state) || ret);
6244
6245 dev_priv->display.crtc_disable(crtc_state, state);
6246
Chris Wilson08536952016-10-14 13:18:18 +01006247 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006248
Ville Syrjälä78108b72016-05-27 20:59:19 +03006249 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6250 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006251
6252 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6253 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006254 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006255 crtc->enabled = false;
6256 crtc->state->connector_mask = 0;
6257 crtc->state->encoder_mask = 0;
6258
6259 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6260 encoder->base.crtc = NULL;
6261
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006262 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02006263 intel_update_watermarks(intel_crtc);
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02006264 intel_disable_shared_dpll(to_intel_crtc_state(crtc->state));
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006265
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006266 domains = intel_crtc->enabled_power_domains;
6267 for_each_power_domain(domain, domains)
6268 intel_display_power_put(dev_priv, domain);
6269 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006270
6271 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
Ville Syrjäläd305e062017-08-30 21:57:03 +03006272 dev_priv->min_cdclk[intel_crtc->pipe] = 0;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +03006273 dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006274}
6275
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006276/*
6277 * turn all crtc's off, but do not adjust state
6278 * This has to be paired with a call to intel_modeset_setup_hw_state.
6279 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006280int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006281{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006282 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006283 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006284 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006285
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006286 state = drm_atomic_helper_suspend(dev);
6287 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006288 if (ret)
6289 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006290 else
6291 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006292 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006293}
6294
Chris Wilsonea5b2132010-08-04 13:50:23 +01006295void intel_encoder_destroy(struct drm_encoder *encoder)
6296{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006297 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006298
Chris Wilsonea5b2132010-08-04 13:50:23 +01006299 drm_encoder_cleanup(encoder);
6300 kfree(intel_encoder);
6301}
6302
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006303/* Cross check the actual hw state with our own modeset state tracking (and it's
6304 * internal consistency). */
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006305static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
6306 struct drm_connector_state *conn_state)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006307{
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006308 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006309
6310 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6311 connector->base.base.id,
6312 connector->base.name);
6313
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006314 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006315 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006316
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006317 I915_STATE_WARN(!crtc_state,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006318 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006319
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006320 if (!crtc_state)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006321 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006322
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006323 I915_STATE_WARN(!crtc_state->active,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006324 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006325
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006326 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006327 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006328
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006329 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006330 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006331
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006332 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006333 "attached encoder crtc differs from connector crtc\n");
6334 } else {
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006335 I915_STATE_WARN(crtc_state && crtc_state->active,
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006336 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006337 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006338 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006339 }
6340}
6341
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006342static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006343{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006344 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6345 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006346
6347 return 0;
6348}
6349
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006350static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006351 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006352{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006353 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006354 struct drm_atomic_state *state = pipe_config->base.state;
6355 struct intel_crtc *other_crtc;
6356 struct intel_crtc_state *other_crtc_state;
6357
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006358 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6359 pipe_name(pipe), pipe_config->fdi_lanes);
6360 if (pipe_config->fdi_lanes > 4) {
6361 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6362 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006363 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006364 }
6365
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006366 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006367 if (pipe_config->fdi_lanes > 2) {
6368 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6369 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006370 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006371 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006372 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006373 }
6374 }
6375
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00006376 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006377 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006378
6379 /* Ivybridge 3 pipe is really complicated */
6380 switch (pipe) {
6381 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006382 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006383 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006384 if (pipe_config->fdi_lanes <= 2)
6385 return 0;
6386
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006387 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006388 other_crtc_state =
6389 intel_atomic_get_crtc_state(state, other_crtc);
6390 if (IS_ERR(other_crtc_state))
6391 return PTR_ERR(other_crtc_state);
6392
6393 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006394 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6395 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006396 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006398 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006399 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006400 if (pipe_config->fdi_lanes > 2) {
6401 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6402 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006403 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006404 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006405
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006406 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006407 other_crtc_state =
6408 intel_atomic_get_crtc_state(state, other_crtc);
6409 if (IS_ERR(other_crtc_state))
6410 return PTR_ERR(other_crtc_state);
6411
6412 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006413 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006414 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006415 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006416 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006417 default:
6418 BUG();
6419 }
6420}
6421
Daniel Vettere29c22c2013-02-21 00:00:16 +01006422#define RETRY 1
6423static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006424 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006425{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006426 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006427 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006428 int lane, link_bw, fdi_dotclock, ret;
6429 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006430
Daniel Vettere29c22c2013-02-21 00:00:16 +01006431retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006432 /* FDI is a binary signal running at ~2.7GHz, encoding
6433 * each output octet as 10 bits. The actual frequency
6434 * is stored as a divider into a 100MHz clock, and the
6435 * mode pixel clock is stored in units of 1KHz.
6436 * Hence the bw of each lane in terms of the mode signal
6437 * is:
6438 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006439 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006440
Damien Lespiau241bfc32013-09-25 16:45:37 +01006441 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006442
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006443 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006444 pipe_config->pipe_bpp);
6445
6446 pipe_config->fdi_lanes = lane;
6447
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006448 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006449 link_bw, &pipe_config->fdi_m_n, false);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006450
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006451 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006453 pipe_config->pipe_bpp -= 2*3;
6454 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6455 pipe_config->pipe_bpp);
6456 needs_recompute = true;
6457 pipe_config->bw_constrained = true;
6458
6459 goto retry;
6460 }
6461
6462 if (needs_recompute)
6463 return RETRY;
6464
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006465 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006466}
6467
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006468bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006469{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006470 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6471 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6472
6473 /* IPS only exists on ULT machines and is tied to pipe A. */
6474 if (!hsw_crtc_supports_ips(crtc))
Ville Syrjälä6e644622017-08-17 17:55:09 +03006475 return false;
6476
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006477 if (!i915_modparams.enable_ips)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006478 return false;
6479
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006480 if (crtc_state->pipe_bpp > 24)
6481 return false;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006482
6483 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006484 * We compare against max which means we must take
6485 * the increased cdclk requirement into account when
6486 * calculating the new cdclk.
6487 *
6488 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006489 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006490 if (IS_BROADWELL(dev_priv) &&
6491 crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
6492 return false;
6493
6494 return true;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006495}
6496
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006497static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006498{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006499 struct drm_i915_private *dev_priv =
6500 to_i915(crtc_state->base.crtc->dev);
6501 struct intel_atomic_state *intel_state =
6502 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006503
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006504 if (!hsw_crtc_state_ips_capable(crtc_state))
6505 return false;
6506
6507 if (crtc_state->ips_force_disable)
6508 return false;
6509
Maarten Lankhorstadbe5c52017-11-22 19:39:06 +01006510 /* IPS should be fine as long as at least one plane is enabled. */
6511 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006512 return false;
6513
6514 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
6515 if (IS_BROADWELL(dev_priv) &&
6516 crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
6517 return false;
6518
6519 return true;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006520}
6521
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006522static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6523{
6524 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6525
6526 /* GDG double wide on either pipe, otherwise pipe A only */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00006527 return INTEL_GEN(dev_priv) < 4 &&
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006528 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6529}
6530
Ville Syrjäläceb99322017-01-20 20:22:05 +02006531static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6532{
6533 uint32_t pixel_rate;
6534
6535 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6536
6537 /*
6538 * We only use IF-ID interlacing. If we ever use
6539 * PF-ID we'll need to adjust the pixel_rate here.
6540 */
6541
6542 if (pipe_config->pch_pfit.enabled) {
6543 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6544 uint32_t pfit_size = pipe_config->pch_pfit.size;
6545
6546 pipe_w = pipe_config->pipe_src_w;
6547 pipe_h = pipe_config->pipe_src_h;
6548
6549 pfit_w = (pfit_size >> 16) & 0xFFFF;
6550 pfit_h = pfit_size & 0xFFFF;
6551 if (pipe_w < pfit_w)
6552 pipe_w = pfit_w;
6553 if (pipe_h < pfit_h)
6554 pipe_h = pfit_h;
6555
6556 if (WARN_ON(!pfit_w || !pfit_h))
6557 return pixel_rate;
6558
6559 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6560 pfit_w * pfit_h);
6561 }
6562
6563 return pixel_rate;
6564}
6565
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006566static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
6567{
6568 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
6569
6570 if (HAS_GMCH_DISPLAY(dev_priv))
6571 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6572 crtc_state->pixel_rate =
6573 crtc_state->base.adjusted_mode.crtc_clock;
6574 else
6575 crtc_state->pixel_rate =
6576 ilk_pipe_pixel_rate(crtc_state);
6577}
6578
Daniel Vettera43f6e02013-06-07 23:10:32 +02006579static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006580 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006581{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006582 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006583 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006584 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006585 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006586
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006587 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006588 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006589
6590 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006591 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006592 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006593 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006594 if (intel_crtc_supports_double_wide(crtc) &&
6595 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006596 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006597 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006598 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006599 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006600
Ville Syrjäläf3261152016-05-24 21:34:18 +03006601 if (adjusted_mode->crtc_clock > clock_limit) {
6602 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6603 adjusted_mode->crtc_clock, clock_limit,
6604 yesno(pipe_config->double_wide));
6605 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006606 }
Chris Wilson89749352010-09-12 18:25:19 +01006607
Shashank Sharma8c79f842018-10-12 11:53:09 +05306608 if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
6609 pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
6610 pipe_config->base.ctm) {
Shashank Sharma25edf912017-07-21 20:55:07 +05306611 /*
6612 * There is only one pipe CSC unit per pipe, and we need that
6613 * for output conversion from RGB->YCBCR. So if CTM is already
6614 * applied we can't support YCBCR420 output.
6615 */
6616 DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
6617 return -EINVAL;
6618 }
6619
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006620 /*
6621 * Pipe horizontal size must be even in:
6622 * - DVO ganged mode
6623 * - LVDS dual channel mode
6624 * - Double wide pipe
6625 */
Ville Syrjälä0574bd82017-11-23 21:04:48 +02006626 if (pipe_config->pipe_src_w & 1) {
6627 if (pipe_config->double_wide) {
6628 DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
6629 return -EINVAL;
6630 }
6631
6632 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6633 intel_is_dual_link_lvds(dev)) {
6634 DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
6635 return -EINVAL;
6636 }
6637 }
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006638
Damien Lespiau8693a822013-05-03 18:48:11 +01006639 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6640 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006641 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006642 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006643 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006644 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006645
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006646 intel_crtc_compute_pixel_rate(pipe_config);
6647
Daniel Vetter877d48d2013-04-19 11:24:43 +02006648 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006649 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006650
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006651 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006652}
6653
Zhenyu Wang2c072452009-06-05 15:38:42 +08006654static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006655intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006656{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006657 while (*num > DATA_LINK_M_N_MASK ||
6658 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006659 *num >>= 1;
6660 *den >>= 1;
6661 }
6662}
6663
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006664static void compute_m_n(unsigned int m, unsigned int n,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006665 uint32_t *ret_m, uint32_t *ret_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006666 bool constant_n)
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006667{
Jani Nikula9a86cda2017-03-27 14:33:25 +03006668 /*
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006669 * Several DP dongles in particular seem to be fussy about
6670 * too large link M/N values. Give N value as 0x8000 that
6671 * should be acceptable by specific devices. 0x8000 is the
6672 * specified fixed N value for asynchronous clock mode,
6673 * which the devices expect also in synchronous clock mode.
Jani Nikula9a86cda2017-03-27 14:33:25 +03006674 */
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006675 if (constant_n)
6676 *ret_n = 0x8000;
6677 else
6678 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
Jani Nikula9a86cda2017-03-27 14:33:25 +03006679
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006680 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6681 intel_reduce_m_n_ratio(ret_m, ret_n);
6682}
6683
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006684void
6685intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6686 int pixel_clock, int link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006687 struct intel_link_m_n *m_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006688 bool constant_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006689{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006690 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006691
6692 compute_m_n(bits_per_pixel * pixel_clock,
6693 link_clock * nlanes * 8,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006694 &m_n->gmch_m, &m_n->gmch_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006695 constant_n);
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006696
6697 compute_m_n(pixel_clock, link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006698 &m_n->link_m, &m_n->link_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006699 constant_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006700}
6701
Chris Wilsona7615032011-01-12 17:04:08 +00006702static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6703{
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00006704 if (i915_modparams.panel_use_ssc >= 0)
6705 return i915_modparams.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006706 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006707 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006708}
6709
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006710static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006711{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006712 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006713}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006714
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006715static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6716{
6717 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006718}
6719
Daniel Vetterf47709a2013-03-28 10:42:02 +01006720static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006721 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006722 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08006723{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006724 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006725 u32 fp, fp2 = 0;
6726
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006727 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006728 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006729 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006730 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006731 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006732 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006733 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006734 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006735 }
6736
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006737 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006738
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006739 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006740 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006741 crtc_state->dpll_hw_state.fp1 = fp2;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006742 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006743 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006744 }
6745}
6746
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006747static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6748 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006749{
6750 u32 reg_val;
6751
6752 /*
6753 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6754 * and set it to a reasonable value instead.
6755 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006756 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006757 reg_val &= 0xffffff00;
6758 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006759 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006760
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006761 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Imre Deaked585702017-05-10 12:21:47 +03006762 reg_val &= 0x00ffffff;
6763 reg_val |= 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006764 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006765
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006766 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006767 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006768 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006769
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006770 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006771 reg_val &= 0x00ffffff;
6772 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006773 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006774}
6775
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006776static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
6777 const struct intel_link_m_n *m_n)
Daniel Vetterb5518422013-05-03 11:49:48 +02006778{
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006779 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6780 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6781 enum pipe pipe = crtc->pipe;
Daniel Vetterb5518422013-05-03 11:49:48 +02006782
Daniel Vettere3b95f12013-05-03 11:49:49 +02006783 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6784 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6785 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6786 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006787}
6788
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02006789static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
6790 enum transcoder transcoder)
6791{
6792 if (IS_HASWELL(dev_priv))
6793 return transcoder == TRANSCODER_EDP;
6794
6795 /*
6796 * Strictly speaking some registers are available before
6797 * gen7, but we only support DRRS on gen7+
6798 */
6799 return IS_GEN7(dev_priv) || IS_CHERRYVIEW(dev_priv);
6800}
6801
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006802static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
6803 const struct intel_link_m_n *m_n,
6804 const struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006805{
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006806 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006807 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006808 enum pipe pipe = crtc->pipe;
6809 enum transcoder transcoder = crtc_state->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006810
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006811 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02006812 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6813 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6814 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6815 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02006816 /*
6817 * M2_N2 registers are set only if DRRS is supported
6818 * (to make sure the registers are not unnecessarily accessed).
Vandana Kannanf769cd22014-08-05 07:51:22 -07006819 */
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02006820 if (m2_n2 && crtc_state->has_drrs &&
6821 transcoder_has_m2_n2(dev_priv, transcoder)) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006822 I915_WRITE(PIPE_DATA_M2(transcoder),
6823 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6824 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6825 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6826 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6827 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006828 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006829 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6830 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6831 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6832 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006833 }
6834}
6835
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006836void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006837{
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006838 const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306839
6840 if (m_n == M1_N1) {
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006841 dp_m_n = &crtc_state->dp_m_n;
6842 dp_m2_n2 = &crtc_state->dp_m2_n2;
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306843 } else if (m_n == M2_N2) {
6844
6845 /*
6846 * M2_N2 registers are not supported. Hence m2_n2 divider value
6847 * needs to be programmed into M1_N1.
6848 */
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006849 dp_m_n = &crtc_state->dp_m2_n2;
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306850 } else {
6851 DRM_ERROR("Unsupported divider value\n");
6852 return;
6853 }
6854
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006855 if (crtc_state->has_pch_encoder)
6856 intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006857 else
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006858 intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006859}
6860
Daniel Vetter251ac862015-06-18 10:30:24 +02006861static void vlv_compute_dpll(struct intel_crtc *crtc,
6862 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006863{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006864 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006865 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006866 if (crtc->pipe != PIPE_A)
6867 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006868
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006869 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006870 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006871 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6872 DPLL_EXT_BUFFER_ENABLE_VLV;
6873
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006874 pipe_config->dpll_hw_state.dpll_md =
6875 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6876}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006877
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006878static void chv_compute_dpll(struct intel_crtc *crtc,
6879 struct intel_crtc_state *pipe_config)
6880{
6881 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006882 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006883 if (crtc->pipe != PIPE_A)
6884 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6885
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006886 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006887 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006888 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6889
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006890 pipe_config->dpll_hw_state.dpll_md =
6891 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006892}
6893
Ville Syrjäläd288f652014-10-28 13:20:22 +02006894static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006895 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006896{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006897 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006898 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006899 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006900 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006901 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006902 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006903
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006904 /* Enable Refclk */
6905 I915_WRITE(DPLL(pipe),
6906 pipe_config->dpll_hw_state.dpll &
6907 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6908
6909 /* No need to actually set up the DPLL with DSI */
6910 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6911 return;
6912
Ville Syrjäläa5805162015-05-26 20:42:30 +03006913 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01006914
Ville Syrjäläd288f652014-10-28 13:20:22 +02006915 bestn = pipe_config->dpll.n;
6916 bestm1 = pipe_config->dpll.m1;
6917 bestm2 = pipe_config->dpll.m2;
6918 bestp1 = pipe_config->dpll.p1;
6919 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006920
Jesse Barnes89b667f2013-04-18 14:51:36 -07006921 /* See eDP HDMI DPIO driver vbios notes doc */
6922
6923 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006924 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006925 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006926
6927 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006928 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006929
6930 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006931 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006932 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006933 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006934
6935 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006936 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006937
6938 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006939 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6940 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6941 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006942 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006943
6944 /*
6945 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6946 * but we don't support that).
6947 * Note: don't use the DAC post divider as it seems unstable.
6948 */
6949 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006950 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006951
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006952 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006954
Jesse Barnes89b667f2013-04-18 14:51:36 -07006955 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006956 if (pipe_config->port_clock == 162000 ||
Maarten Lankhorst92d54b02018-10-11 12:04:50 +02006957 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
6958 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006959 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006960 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006961 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006963 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006964
Ville Syrjälä37a56502016-06-22 21:57:04 +03006965 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006966 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006967 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006968 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006969 0x0df40000);
6970 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006972 0x0df70000);
6973 } else { /* HDMI or VGA */
6974 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006975 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006976 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006977 0x0df70000);
6978 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006979 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006980 0x0df40000);
6981 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006982
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006983 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006984 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Maarten Lankhorst92d54b02018-10-11 12:04:50 +02006985 if (intel_crtc_has_dp_encoder(pipe_config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006986 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006988
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006989 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006990 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006991}
6992
Ville Syrjäläd288f652014-10-28 13:20:22 +02006993static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006994 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006995{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006996 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006997 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006998 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006999 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307000 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007001 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307002 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307003 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007004
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007005 /* Enable Refclk and SSC */
7006 I915_WRITE(DPLL(pipe),
7007 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7008
7009 /* No need to actually set up the DPLL with DSI */
7010 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7011 return;
7012
Ville Syrjäläd288f652014-10-28 13:20:22 +02007013 bestn = pipe_config->dpll.n;
7014 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7015 bestm1 = pipe_config->dpll.m1;
7016 bestm2 = pipe_config->dpll.m2 >> 22;
7017 bestp1 = pipe_config->dpll.p1;
7018 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307019 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307020 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307021 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007022
Ville Syrjäläa5805162015-05-26 20:42:30 +03007023 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007024
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007025 /* p1 and p2 divider */
7026 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7027 5 << DPIO_CHV_S1_DIV_SHIFT |
7028 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7029 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7030 1 << DPIO_CHV_K_DIV_SHIFT);
7031
7032 /* Feedback post-divider - m2 */
7033 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7034
7035 /* Feedback refclk divider - n and m1 */
7036 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7037 DPIO_CHV_M1_DIV_BY_2 |
7038 1 << DPIO_CHV_N_DIV_SHIFT);
7039
7040 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007041 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007042
7043 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307044 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7045 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7046 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7047 if (bestm2_frac)
7048 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7049 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007050
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307051 /* Program digital lock detect threshold */
7052 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7053 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7054 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7055 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7056 if (!bestm2_frac)
7057 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7058 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7059
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007060 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307061 if (vco == 5400000) {
7062 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7063 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7064 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7065 tribuf_calcntr = 0x9;
7066 } else if (vco <= 6200000) {
7067 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7068 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7069 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7070 tribuf_calcntr = 0x9;
7071 } else if (vco <= 6480000) {
7072 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7073 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7074 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7075 tribuf_calcntr = 0x8;
7076 } else {
7077 /* Not supported. Apply the same limits as in the max case */
7078 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7079 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7080 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7081 tribuf_calcntr = 0;
7082 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007083 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7084
Ville Syrjälä968040b2015-03-11 22:52:08 +02007085 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307086 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7087 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7088 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7089
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007090 /* AFC Recal */
7091 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7092 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7093 DPIO_AFC_RECAL);
7094
Ville Syrjäläa5805162015-05-26 20:42:30 +03007095 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007096}
7097
Ville Syrjäläd288f652014-10-28 13:20:22 +02007098/**
7099 * vlv_force_pll_on - forcibly enable just the PLL
7100 * @dev_priv: i915 private structure
7101 * @pipe: pipe PLL to enable
7102 * @dpll: PLL configuration
7103 *
7104 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7105 * in cases where we need the PLL enabled even when @pipe is not going to
7106 * be enabled.
7107 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007108int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007109 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007110{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007111 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007112 struct intel_crtc_state *pipe_config;
7113
7114 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7115 if (!pipe_config)
7116 return -ENOMEM;
7117
7118 pipe_config->base.crtc = &crtc->base;
7119 pipe_config->pixel_multiplier = 1;
7120 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007121
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007122 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007123 chv_compute_dpll(crtc, pipe_config);
7124 chv_prepare_pll(crtc, pipe_config);
7125 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007126 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007127 vlv_compute_dpll(crtc, pipe_config);
7128 vlv_prepare_pll(crtc, pipe_config);
7129 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007130 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007131
7132 kfree(pipe_config);
7133
7134 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007135}
7136
7137/**
7138 * vlv_force_pll_off - forcibly disable just the PLL
7139 * @dev_priv: i915 private structure
7140 * @pipe: pipe PLL to disable
7141 *
7142 * Disable the PLL for @pipe. To be used in cases where we need
7143 * the PLL enabled even when @pipe is not going to be enabled.
7144 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007145void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007146{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007147 if (IS_CHERRYVIEW(dev_priv))
7148 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007149 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007150 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007151}
7152
Daniel Vetter251ac862015-06-18 10:30:24 +02007153static void i9xx_compute_dpll(struct intel_crtc *crtc,
7154 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007155 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007156{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007157 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007158 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007159 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007160
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007161 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307162
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007163 dpll = DPLL_VGA_MODE_DIS;
7164
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007165 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007166 dpll |= DPLLB_MODE_LVDS;
7167 else
7168 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007169
Jani Nikula73f67aa2016-12-07 22:48:09 +02007170 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
7171 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007172 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007173 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007174 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007175
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007176 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7177 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007178 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007179
Ville Syrjälä37a56502016-06-22 21:57:04 +03007180 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007181 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007182
7183 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007184 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007185 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7186 else {
7187 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007188 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007189 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7190 }
7191 switch (clock->p2) {
7192 case 5:
7193 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7194 break;
7195 case 7:
7196 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7197 break;
7198 case 10:
7199 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7200 break;
7201 case 14:
7202 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7203 break;
7204 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007205 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007206 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7207
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007208 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007209 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007210 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007211 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007212 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7213 else
7214 dpll |= PLL_REF_INPUT_DREFCLK;
7215
7216 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007217 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007218
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007219 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007220 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007221 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007222 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007223 }
7224}
7225
Daniel Vetter251ac862015-06-18 10:30:24 +02007226static void i8xx_compute_dpll(struct intel_crtc *crtc,
7227 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007228 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007229{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007230 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007231 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007232 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007233 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007234
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007235 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307236
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007237 dpll = DPLL_VGA_MODE_DIS;
7238
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007239 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007240 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7241 } else {
7242 if (clock->p1 == 2)
7243 dpll |= PLL_P1_DIVIDE_BY_TWO;
7244 else
7245 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7246 if (clock->p2 == 4)
7247 dpll |= PLL_P2_DIVIDE_BY_4;
7248 }
7249
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007250 if (!IS_I830(dev_priv) &&
7251 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007252 dpll |= DPLL_DVO_2X_MODE;
7253
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007254 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007255 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007256 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7257 else
7258 dpll |= PLL_REF_INPUT_DREFCLK;
7259
7260 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007261 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007262}
7263
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007264static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007265{
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007266 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7267 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7268 enum pipe pipe = crtc->pipe;
7269 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
7270 const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007271 uint32_t crtc_vtotal, crtc_vblank_end;
7272 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007273
7274 /* We need to be careful not to changed the adjusted mode, for otherwise
7275 * the hw state checker will get angry at the mismatch. */
7276 crtc_vtotal = adjusted_mode->crtc_vtotal;
7277 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007278
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007279 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007280 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007281 crtc_vtotal -= 1;
7282 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007283
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007284 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007285 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7286 else
7287 vsyncshift = adjusted_mode->crtc_hsync_start -
7288 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007289 if (vsyncshift < 0)
7290 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007291 }
7292
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007293 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007294 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007295
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007296 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007297 (adjusted_mode->crtc_hdisplay - 1) |
7298 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007299 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007300 (adjusted_mode->crtc_hblank_start - 1) |
7301 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007302 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007303 (adjusted_mode->crtc_hsync_start - 1) |
7304 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7305
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007306 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007307 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007308 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007309 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007310 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007311 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007312 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007313 (adjusted_mode->crtc_vsync_start - 1) |
7314 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7315
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007316 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7317 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7318 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7319 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01007320 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007321 (pipe == PIPE_B || pipe == PIPE_C))
7322 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7323
Jani Nikulabc58be62016-03-18 17:05:39 +02007324}
7325
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007326static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
Jani Nikulabc58be62016-03-18 17:05:39 +02007327{
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007328 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7329 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7330 enum pipe pipe = crtc->pipe;
Jani Nikulabc58be62016-03-18 17:05:39 +02007331
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007332 /* pipesrc controls the size that is scaled from, which should
7333 * always be the user's requested size.
7334 */
7335 I915_WRITE(PIPESRC(pipe),
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007336 ((crtc_state->pipe_src_w - 1) << 16) |
7337 (crtc_state->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007338}
7339
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007340static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007341 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007342{
7343 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007344 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007345 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7346 uint32_t tmp;
7347
7348 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007349 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7350 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007351 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007352 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7353 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007354 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007355 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7356 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007357
7358 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007359 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7360 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007361 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007362 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7363 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007364 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007365 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7366 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007367
7368 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007369 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7370 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7371 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007372 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007373}
7374
7375static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7376 struct intel_crtc_state *pipe_config)
7377{
7378 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007379 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007380 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007381
7382 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007383 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7384 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7385
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007386 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7387 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007388}
7389
Daniel Vetterf6a83282014-02-11 15:28:57 -08007390void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007391 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007392{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007393 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7394 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7395 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7396 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007397
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007398 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7399 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7400 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7401 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007402
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007403 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007404 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007405
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007406 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007407
7408 mode->hsync = drm_mode_hsync(mode);
7409 mode->vrefresh = drm_mode_vrefresh(mode);
7410 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007411}
7412
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007413static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
Daniel Vetter84b046f2013-02-19 18:48:54 +01007414{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007415 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7416 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007417 uint32_t pipeconf;
7418
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007419 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007420
Ville Syrjäläe56134b2017-06-01 17:36:19 +03007421 /* we keep both pipes enabled on 830 */
7422 if (IS_I830(dev_priv))
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007423 pipeconf |= I915_READ(PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007424
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007425 if (crtc_state->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007426 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007427
Daniel Vetterff9ce462013-04-24 14:57:17 +02007428 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007429 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7430 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007431 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007432 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007433 pipeconf |= PIPECONF_DITHER_EN |
7434 PIPECONF_DITHER_TYPE_SP;
7435
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007436 switch (crtc_state->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007437 case 18:
7438 pipeconf |= PIPECONF_6BPC;
7439 break;
7440 case 24:
7441 pipeconf |= PIPECONF_8BPC;
7442 break;
7443 case 30:
7444 pipeconf |= PIPECONF_10BPC;
7445 break;
7446 default:
7447 /* Case prevented by intel_choose_pipe_bpp_dither. */
7448 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007449 }
7450 }
7451
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007452 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007453 if (INTEL_GEN(dev_priv) < 4 ||
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007454 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007455 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7456 else
7457 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7458 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007459 pipeconf |= PIPECONF_PROGRESSIVE;
7460
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007461 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007462 crtc_state->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007463 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007464
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007465 I915_WRITE(PIPECONF(crtc->pipe), pipeconf);
7466 POSTING_READ(PIPECONF(crtc->pipe));
Daniel Vetter84b046f2013-02-19 18:48:54 +01007467}
7468
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007469static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7470 struct intel_crtc_state *crtc_state)
7471{
7472 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007473 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007474 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007475 int refclk = 48000;
7476
7477 memset(&crtc_state->dpll_hw_state, 0,
7478 sizeof(crtc_state->dpll_hw_state));
7479
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007480 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007481 if (intel_panel_use_ssc(dev_priv)) {
7482 refclk = dev_priv->vbt.lvds_ssc_freq;
7483 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7484 }
7485
7486 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007487 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007488 limit = &intel_limits_i8xx_dvo;
7489 } else {
7490 limit = &intel_limits_i8xx_dac;
7491 }
7492
7493 if (!crtc_state->clock_set &&
7494 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7495 refclk, NULL, &crtc_state->dpll)) {
7496 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7497 return -EINVAL;
7498 }
7499
7500 i8xx_compute_dpll(crtc, crtc_state, NULL);
7501
7502 return 0;
7503}
7504
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007505static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7506 struct intel_crtc_state *crtc_state)
7507{
7508 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007509 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007510 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007511 int refclk = 96000;
7512
7513 memset(&crtc_state->dpll_hw_state, 0,
7514 sizeof(crtc_state->dpll_hw_state));
7515
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007516 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007517 if (intel_panel_use_ssc(dev_priv)) {
7518 refclk = dev_priv->vbt.lvds_ssc_freq;
7519 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7520 }
7521
7522 if (intel_is_dual_link_lvds(dev))
7523 limit = &intel_limits_g4x_dual_channel_lvds;
7524 else
7525 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007526 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7527 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007528 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007529 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007530 limit = &intel_limits_g4x_sdvo;
7531 } else {
7532 /* The option is for other outputs */
7533 limit = &intel_limits_i9xx_sdvo;
7534 }
7535
7536 if (!crtc_state->clock_set &&
7537 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7538 refclk, NULL, &crtc_state->dpll)) {
7539 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7540 return -EINVAL;
7541 }
7542
7543 i9xx_compute_dpll(crtc, crtc_state, NULL);
7544
7545 return 0;
7546}
7547
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007548static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7549 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007550{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007551 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007552 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007553 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007554 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007555
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007556 memset(&crtc_state->dpll_hw_state, 0,
7557 sizeof(crtc_state->dpll_hw_state));
7558
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007559 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007560 if (intel_panel_use_ssc(dev_priv)) {
7561 refclk = dev_priv->vbt.lvds_ssc_freq;
7562 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7563 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007564
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007565 limit = &intel_limits_pineview_lvds;
7566 } else {
7567 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007568 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007569
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007570 if (!crtc_state->clock_set &&
7571 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7572 refclk, NULL, &crtc_state->dpll)) {
7573 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7574 return -EINVAL;
7575 }
7576
7577 i9xx_compute_dpll(crtc, crtc_state, NULL);
7578
7579 return 0;
7580}
7581
7582static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7583 struct intel_crtc_state *crtc_state)
7584{
7585 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007586 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007587 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007588 int refclk = 96000;
7589
7590 memset(&crtc_state->dpll_hw_state, 0,
7591 sizeof(crtc_state->dpll_hw_state));
7592
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007593 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007594 if (intel_panel_use_ssc(dev_priv)) {
7595 refclk = dev_priv->vbt.lvds_ssc_freq;
7596 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007597 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007598
7599 limit = &intel_limits_i9xx_lvds;
7600 } else {
7601 limit = &intel_limits_i9xx_sdvo;
7602 }
7603
7604 if (!crtc_state->clock_set &&
7605 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7606 refclk, NULL, &crtc_state->dpll)) {
7607 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7608 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007609 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007610
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007611 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007612
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007613 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007614}
7615
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007616static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7617 struct intel_crtc_state *crtc_state)
7618{
7619 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007620 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007621
7622 memset(&crtc_state->dpll_hw_state, 0,
7623 sizeof(crtc_state->dpll_hw_state));
7624
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007625 if (!crtc_state->clock_set &&
7626 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7627 refclk, NULL, &crtc_state->dpll)) {
7628 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7629 return -EINVAL;
7630 }
7631
7632 chv_compute_dpll(crtc, crtc_state);
7633
7634 return 0;
7635}
7636
7637static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7638 struct intel_crtc_state *crtc_state)
7639{
7640 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007641 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007642
7643 memset(&crtc_state->dpll_hw_state, 0,
7644 sizeof(crtc_state->dpll_hw_state));
7645
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007646 if (!crtc_state->clock_set &&
7647 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7648 refclk, NULL, &crtc_state->dpll)) {
7649 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7650 return -EINVAL;
7651 }
7652
7653 vlv_compute_dpll(crtc, crtc_state);
7654
7655 return 0;
7656}
7657
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007658static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007659 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007660{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007661 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007662 uint32_t tmp;
7663
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007664 if (INTEL_GEN(dev_priv) <= 3 &&
7665 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007666 return;
7667
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007668 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007669 if (!(tmp & PFIT_ENABLE))
7670 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007671
Daniel Vetter06922822013-07-11 13:35:40 +02007672 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007673 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007674 if (crtc->pipe != PIPE_B)
7675 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007676 } else {
7677 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7678 return;
7679 }
7680
Daniel Vetter06922822013-07-11 13:35:40 +02007681 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007682 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007683}
7684
Jesse Barnesacbec812013-09-20 11:29:32 -07007685static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007686 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007687{
7688 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007689 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07007690 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007691 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07007692 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007693 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007694
Ville Syrjäläb5219732016-03-15 16:40:01 +02007695 /* In case of DSI, DPLL will not be used */
7696 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05307697 return;
7698
Ville Syrjäläa5805162015-05-26 20:42:30 +03007699 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007700 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007701 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007702
7703 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7704 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7705 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7706 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7707 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7708
Imre Deakdccbea32015-06-22 23:35:51 +03007709 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007710}
7711
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007712static void
7713i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7714 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007715{
7716 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007717 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007718 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
7719 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007720 enum pipe pipe;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007721 u32 val, base, offset;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007722 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007723 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007724 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007725 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007726
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007727 if (!plane->get_hw_state(plane, &pipe))
Damien Lespiau42a7b082015-02-05 19:35:13 +00007728 return;
7729
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007730 WARN_ON(pipe != crtc->pipe);
7731
Damien Lespiaud9806c92015-01-21 14:07:19 +00007732 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007733 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007734 DRM_DEBUG_KMS("failed to alloc fb\n");
7735 return;
7736 }
7737
Damien Lespiau1b842c82015-01-21 13:50:54 +00007738 fb = &intel_fb->base;
7739
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02007740 fb->dev = dev;
7741
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02007742 val = I915_READ(DSPCNTR(i9xx_plane));
7743
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007744 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00007745 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007746 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02007747 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00007748 }
7749 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007750
7751 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007752 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02007753 fb->format = drm_format_info(fourcc);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007754
Ville Syrjälä81894b22017-11-17 21:19:13 +02007755 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7756 offset = I915_READ(DSPOFFSET(i9xx_plane));
7757 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
7758 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007759 if (plane_config->tiling)
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007760 offset = I915_READ(DSPTILEOFF(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007761 else
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007762 offset = I915_READ(DSPLINOFF(i9xx_plane));
7763 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007764 } else {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007765 base = I915_READ(DSPADDR(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007766 }
7767 plane_config->base = base;
7768
7769 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007770 fb->width = ((val >> 16) & 0xfff) + 1;
7771 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007772
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007773 val = I915_READ(DSPSTRIDE(i9xx_plane));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007774 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007775
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02007776 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007777
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007778 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007779
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007780 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7781 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02007782 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00007783 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007784
Damien Lespiau2d140302015-02-05 17:22:18 +00007785 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007786}
7787
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007788static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007789 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007790{
7791 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007792 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007793 int pipe = pipe_config->cpu_transcoder;
7794 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007795 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007796 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007797 int refclk = 100000;
7798
Ville Syrjäläb5219732016-03-15 16:40:01 +02007799 /* In case of DSI, DPLL will not be used */
7800 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7801 return;
7802
Ville Syrjäläa5805162015-05-26 20:42:30 +03007803 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007804 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7805 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7806 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7807 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007808 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007809 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007810
7811 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007812 clock.m2 = (pll_dw0 & 0xff) << 22;
7813 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7814 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007815 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7816 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7817 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7818
Imre Deakdccbea32015-06-22 23:35:51 +03007819 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007820}
7821
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05307822static void intel_get_crtc_ycbcr_config(struct intel_crtc *crtc,
7823 struct intel_crtc_state *pipe_config)
7824{
7825 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7826 enum intel_output_format output = INTEL_OUTPUT_FORMAT_RGB;
7827
Shashank Sharma668b6c12018-10-12 11:53:14 +05307828 pipe_config->lspcon_downsampling = false;
7829
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05307830 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
7831 u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
7832
7833 if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
7834 bool ycbcr420_enabled = tmp & PIPEMISC_YUV420_ENABLE;
7835 bool blend = tmp & PIPEMISC_YUV420_MODE_FULL_BLEND;
7836
7837 if (ycbcr420_enabled) {
7838 /* We support 4:2:0 in full blend mode only */
7839 if (!blend)
7840 output = INTEL_OUTPUT_FORMAT_INVALID;
7841 else if (!(IS_GEMINILAKE(dev_priv) ||
7842 INTEL_GEN(dev_priv) >= 10))
7843 output = INTEL_OUTPUT_FORMAT_INVALID;
7844 else
7845 output = INTEL_OUTPUT_FORMAT_YCBCR420;
Shashank Sharma8c79f842018-10-12 11:53:09 +05307846 } else {
Shashank Sharma668b6c12018-10-12 11:53:14 +05307847 /*
7848 * Currently there is no interface defined to
7849 * check user preference between RGB/YCBCR444
7850 * or YCBCR420. So the only possible case for
7851 * YCBCR444 usage is driving YCBCR420 output
7852 * with LSPCON, when pipe is configured for
7853 * YCBCR444 output and LSPCON takes care of
7854 * downsampling it.
7855 */
7856 pipe_config->lspcon_downsampling = true;
Shashank Sharma8c79f842018-10-12 11:53:09 +05307857 output = INTEL_OUTPUT_FORMAT_YCBCR444;
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05307858 }
7859 }
7860 }
7861
7862 pipe_config->output_format = output;
7863}
7864
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007865static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007866 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007867{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007868 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02007869 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007870 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02007871 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007872
Imre Deak17290502016-02-12 18:55:11 +02007873 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7874 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02007875 return false;
7876
Shashank Sharmad9facae2018-10-12 11:53:07 +05307877 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
Daniel Vettere143a212013-07-04 12:01:15 +02007878 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02007879 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02007880
Imre Deak17290502016-02-12 18:55:11 +02007881 ret = false;
7882
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007883 tmp = I915_READ(PIPECONF(crtc->pipe));
7884 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02007885 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007886
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007887 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7888 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007889 switch (tmp & PIPECONF_BPC_MASK) {
7890 case PIPECONF_6BPC:
7891 pipe_config->pipe_bpp = 18;
7892 break;
7893 case PIPECONF_8BPC:
7894 pipe_config->pipe_bpp = 24;
7895 break;
7896 case PIPECONF_10BPC:
7897 pipe_config->pipe_bpp = 30;
7898 break;
7899 default:
7900 break;
7901 }
7902 }
7903
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007904 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007905 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007906 pipe_config->limited_color_range = true;
7907
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007908 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03007909 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7910
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007911 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02007912 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007913
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007914 i9xx_get_pfit_config(crtc, pipe_config);
7915
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007916 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02007917 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007918 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02007919 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7920 else
7921 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02007922 pipe_config->pixel_multiplier =
7923 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7924 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007925 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007926 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02007927 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02007928 tmp = I915_READ(DPLL(crtc->pipe));
7929 pipe_config->pixel_multiplier =
7930 ((tmp & SDVO_MULTIPLIER_MASK)
7931 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7932 } else {
7933 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7934 * port and will be fixed up in the encoder->get_config
7935 * function. */
7936 pipe_config->pixel_multiplier = 1;
7937 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007938 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007939 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007940 /*
7941 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7942 * on 830. Filter it out here so that we don't
7943 * report errors due to that.
7944 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007945 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007946 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7947
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007948 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7949 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007950 } else {
7951 /* Mask out read-only status bits. */
7952 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7953 DPLL_PORTC_READY_MASK |
7954 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007955 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007956
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007957 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007958 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007959 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07007960 vlv_crtc_clock_get(crtc, pipe_config);
7961 else
7962 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007963
Ville Syrjälä0f646142015-08-26 19:39:18 +03007964 /*
7965 * Normally the dotclock is filled in by the encoder .get_config()
7966 * but in case the pipe is enabled w/o any ports we need a sane
7967 * default.
7968 */
7969 pipe_config->base.adjusted_mode.crtc_clock =
7970 pipe_config->port_clock / pipe_config->pixel_multiplier;
7971
Imre Deak17290502016-02-12 18:55:11 +02007972 ret = true;
7973
7974out:
7975 intel_display_power_put(dev_priv, power_domain);
7976
7977 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007978}
7979
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007980static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007981{
Jesse Barnes13d83a62011-08-03 12:59:20 -07007982 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04007983 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007984 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007985 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007986 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007987 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007988 bool has_ck505 = false;
7989 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04007990 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007991
7992 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007993 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007994 switch (encoder->type) {
7995 case INTEL_OUTPUT_LVDS:
7996 has_panel = true;
7997 has_lvds = true;
7998 break;
7999 case INTEL_OUTPUT_EDP:
8000 has_panel = true;
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02008001 if (encoder->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008002 has_cpu_edp = true;
8003 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008004 default:
8005 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008006 }
8007 }
8008
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008009 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008010 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008011 can_ssc = has_ck505;
8012 } else {
8013 has_ck505 = false;
8014 can_ssc = true;
8015 }
8016
Lyude1c1a24d2016-06-14 11:04:09 -04008017 /* Check if any DPLLs are using the SSC source */
8018 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8019 u32 temp = I915_READ(PCH_DPLL(i));
8020
8021 if (!(temp & DPLL_VCO_ENABLE))
8022 continue;
8023
8024 if ((temp & PLL_REF_INPUT_MASK) ==
8025 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8026 using_ssc_source = true;
8027 break;
8028 }
8029 }
8030
8031 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8032 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008033
8034 /* Ironlake: try to setup display ref clock before DPLL
8035 * enabling. This is only under driver's control after
8036 * PCH B stepping, previous chipset stepping should be
8037 * ignoring this setting.
8038 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008039 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008040
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008041 /* As we must carefully and slowly disable/enable each source in turn,
8042 * compute the final state we want first and check if we need to
8043 * make any changes at all.
8044 */
8045 final = val;
8046 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008047 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008048 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008049 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008050 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8051
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008052 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008053 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008054 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008055
Keith Packard199e5d72011-09-22 12:01:57 -07008056 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008057 final |= DREF_SSC_SOURCE_ENABLE;
8058
8059 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8060 final |= DREF_SSC1_ENABLE;
8061
8062 if (has_cpu_edp) {
8063 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8064 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8065 else
8066 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8067 } else
8068 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008069 } else if (using_ssc_source) {
8070 final |= DREF_SSC_SOURCE_ENABLE;
8071 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008072 }
8073
8074 if (final == val)
8075 return;
8076
8077 /* Always enable nonspread source */
8078 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8079
8080 if (has_ck505)
8081 val |= DREF_NONSPREAD_CK505_ENABLE;
8082 else
8083 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8084
8085 if (has_panel) {
8086 val &= ~DREF_SSC_SOURCE_MASK;
8087 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008088
Keith Packard199e5d72011-09-22 12:01:57 -07008089 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008090 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008091 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008092 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008093 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008094 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008095
8096 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008097 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008098 POSTING_READ(PCH_DREF_CONTROL);
8099 udelay(200);
8100
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008101 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008102
8103 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008104 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008105 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008106 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008107 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008108 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008109 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008110 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008111 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008112
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008113 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008114 POSTING_READ(PCH_DREF_CONTROL);
8115 udelay(200);
8116 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008117 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008118
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008119 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008120
8121 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008122 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008123
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008124 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008125 POSTING_READ(PCH_DREF_CONTROL);
8126 udelay(200);
8127
Lyude1c1a24d2016-06-14 11:04:09 -04008128 if (!using_ssc_source) {
8129 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008130
Lyude1c1a24d2016-06-14 11:04:09 -04008131 /* Turn off the SSC source */
8132 val &= ~DREF_SSC_SOURCE_MASK;
8133 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008134
Lyude1c1a24d2016-06-14 11:04:09 -04008135 /* Turn off SSC1 */
8136 val &= ~DREF_SSC1_ENABLE;
8137
8138 I915_WRITE(PCH_DREF_CONTROL, val);
8139 POSTING_READ(PCH_DREF_CONTROL);
8140 udelay(200);
8141 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008142 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008143
8144 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008145}
8146
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008147static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008148{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008149 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008150
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008151 tmp = I915_READ(SOUTH_CHICKEN2);
8152 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8153 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008154
Imre Deakcf3598c2016-06-28 13:37:31 +03008155 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8156 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008157 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008158
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008159 tmp = I915_READ(SOUTH_CHICKEN2);
8160 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8161 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008162
Imre Deakcf3598c2016-06-28 13:37:31 +03008163 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8164 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008165 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008166}
8167
8168/* WaMPhyProgramming:hsw */
8169static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8170{
8171 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008172
8173 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8174 tmp &= ~(0xFF << 24);
8175 tmp |= (0x12 << 24);
8176 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8177
Paulo Zanonidde86e22012-12-01 12:04:25 -02008178 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8179 tmp |= (1 << 11);
8180 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8181
8182 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8183 tmp |= (1 << 11);
8184 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8185
Paulo Zanonidde86e22012-12-01 12:04:25 -02008186 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8187 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8188 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8189
8190 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8191 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8192 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8193
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008194 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8195 tmp &= ~(7 << 13);
8196 tmp |= (5 << 13);
8197 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008198
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008199 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8200 tmp &= ~(7 << 13);
8201 tmp |= (5 << 13);
8202 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008203
8204 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8205 tmp &= ~0xFF;
8206 tmp |= 0x1C;
8207 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8208
8209 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8210 tmp &= ~0xFF;
8211 tmp |= 0x1C;
8212 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8213
8214 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8215 tmp &= ~(0xFF << 16);
8216 tmp |= (0x1C << 16);
8217 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8218
8219 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8220 tmp &= ~(0xFF << 16);
8221 tmp |= (0x1C << 16);
8222 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8223
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008224 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8225 tmp |= (1 << 27);
8226 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008227
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008228 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8229 tmp |= (1 << 27);
8230 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008231
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008232 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8233 tmp &= ~(0xF << 28);
8234 tmp |= (4 << 28);
8235 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008236
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008237 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8238 tmp &= ~(0xF << 28);
8239 tmp |= (4 << 28);
8240 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008241}
8242
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008243/* Implements 3 different sequences from BSpec chapter "Display iCLK
8244 * Programming" based on the parameters passed:
8245 * - Sequence to enable CLKOUT_DP
8246 * - Sequence to enable CLKOUT_DP without spread
8247 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8248 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008249static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
8250 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008251{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008252 uint32_t reg, tmp;
8253
8254 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8255 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008256 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
8257 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008258 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008259
Ville Syrjäläa5805162015-05-26 20:42:30 +03008260 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008261
8262 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8263 tmp &= ~SBI_SSCCTL_DISABLE;
8264 tmp |= SBI_SSCCTL_PATHALT;
8265 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8266
8267 udelay(24);
8268
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008269 if (with_spread) {
8270 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8271 tmp &= ~SBI_SSCCTL_PATHALT;
8272 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008273
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008274 if (with_fdi) {
8275 lpt_reset_fdi_mphy(dev_priv);
8276 lpt_program_fdi_mphy(dev_priv);
8277 }
8278 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008279
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008280 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008281 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8282 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8283 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008284
Ville Syrjäläa5805162015-05-26 20:42:30 +03008285 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008286}
8287
Paulo Zanoni47701c32013-07-23 11:19:25 -03008288/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008289static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03008290{
Paulo Zanoni47701c32013-07-23 11:19:25 -03008291 uint32_t reg, tmp;
8292
Ville Syrjäläa5805162015-05-26 20:42:30 +03008293 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008294
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008295 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008296 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8297 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8298 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8299
8300 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8301 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8302 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8303 tmp |= SBI_SSCCTL_PATHALT;
8304 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8305 udelay(32);
8306 }
8307 tmp |= SBI_SSCCTL_DISABLE;
8308 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8309 }
8310
Ville Syrjäläa5805162015-05-26 20:42:30 +03008311 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008312}
8313
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008314#define BEND_IDX(steps) ((50 + (steps)) / 5)
8315
8316static const uint16_t sscdivintphase[] = {
8317 [BEND_IDX( 50)] = 0x3B23,
8318 [BEND_IDX( 45)] = 0x3B23,
8319 [BEND_IDX( 40)] = 0x3C23,
8320 [BEND_IDX( 35)] = 0x3C23,
8321 [BEND_IDX( 30)] = 0x3D23,
8322 [BEND_IDX( 25)] = 0x3D23,
8323 [BEND_IDX( 20)] = 0x3E23,
8324 [BEND_IDX( 15)] = 0x3E23,
8325 [BEND_IDX( 10)] = 0x3F23,
8326 [BEND_IDX( 5)] = 0x3F23,
8327 [BEND_IDX( 0)] = 0x0025,
8328 [BEND_IDX( -5)] = 0x0025,
8329 [BEND_IDX(-10)] = 0x0125,
8330 [BEND_IDX(-15)] = 0x0125,
8331 [BEND_IDX(-20)] = 0x0225,
8332 [BEND_IDX(-25)] = 0x0225,
8333 [BEND_IDX(-30)] = 0x0325,
8334 [BEND_IDX(-35)] = 0x0325,
8335 [BEND_IDX(-40)] = 0x0425,
8336 [BEND_IDX(-45)] = 0x0425,
8337 [BEND_IDX(-50)] = 0x0525,
8338};
8339
8340/*
8341 * Bend CLKOUT_DP
8342 * steps -50 to 50 inclusive, in steps of 5
8343 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8344 * change in clock period = -(steps / 10) * 5.787 ps
8345 */
8346static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8347{
8348 uint32_t tmp;
8349 int idx = BEND_IDX(steps);
8350
8351 if (WARN_ON(steps % 5 != 0))
8352 return;
8353
8354 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8355 return;
8356
8357 mutex_lock(&dev_priv->sb_lock);
8358
8359 if (steps % 10 != 0)
8360 tmp = 0xAAAAAAAB;
8361 else
8362 tmp = 0x00000000;
8363 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8364
8365 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8366 tmp &= 0xffff0000;
8367 tmp |= sscdivintphase[idx];
8368 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8369
8370 mutex_unlock(&dev_priv->sb_lock);
8371}
8372
8373#undef BEND_IDX
8374
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008375static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008376{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008377 struct intel_encoder *encoder;
8378 bool has_vga = false;
8379
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008380 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008381 switch (encoder->type) {
8382 case INTEL_OUTPUT_ANALOG:
8383 has_vga = true;
8384 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008385 default:
8386 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008387 }
8388 }
8389
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008390 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008391 lpt_bend_clkout_dp(dev_priv, 0);
8392 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008393 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008394 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008395 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008396}
8397
Paulo Zanonidde86e22012-12-01 12:04:25 -02008398/*
8399 * Initialize reference clocks when the driver loads
8400 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008401void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008402{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008403 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008404 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008405 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008406 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008407}
8408
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008409static void ironlake_set_pipeconf(const struct intel_crtc_state *crtc_state)
Paulo Zanonic8203562012-09-12 10:06:29 -03008410{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008411 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8412 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8413 enum pipe pipe = crtc->pipe;
Paulo Zanonic8203562012-09-12 10:06:29 -03008414 uint32_t val;
8415
Daniel Vetter78114072013-06-13 00:54:57 +02008416 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008417
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008418 switch (crtc_state->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008419 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008420 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008421 break;
8422 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008423 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008424 break;
8425 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008426 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008427 break;
8428 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008429 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008430 break;
8431 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008432 /* Case prevented by intel_choose_pipe_bpp_dither. */
8433 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008434 }
8435
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008436 if (crtc_state->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008437 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8438
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008439 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008440 val |= PIPECONF_INTERLACED_ILK;
8441 else
8442 val |= PIPECONF_PROGRESSIVE;
8443
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008444 if (crtc_state->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008445 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008446
Paulo Zanonic8203562012-09-12 10:06:29 -03008447 I915_WRITE(PIPECONF(pipe), val);
8448 POSTING_READ(PIPECONF(pipe));
8449}
8450
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008451static void haswell_set_pipeconf(const struct intel_crtc_state *crtc_state)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008452{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008453 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8454 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8455 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008456 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008457
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008458 if (IS_HASWELL(dev_priv) && crtc_state->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008459 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8460
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008461 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008462 val |= PIPECONF_INTERLACED_ILK;
8463 else
8464 val |= PIPECONF_PROGRESSIVE;
8465
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008466 I915_WRITE(PIPECONF(cpu_transcoder), val);
8467 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008468}
8469
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008470static void haswell_set_pipemisc(const struct intel_crtc_state *crtc_state)
Jani Nikula391bf042016-03-18 17:05:40 +02008471{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008472 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
8473 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008474
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00008475 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
Jani Nikula391bf042016-03-18 17:05:40 +02008476 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008477
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008478 switch (crtc_state->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008479 case 18:
8480 val |= PIPEMISC_DITHER_6_BPC;
8481 break;
8482 case 24:
8483 val |= PIPEMISC_DITHER_8_BPC;
8484 break;
8485 case 30:
8486 val |= PIPEMISC_DITHER_10_BPC;
8487 break;
8488 case 36:
8489 val |= PIPEMISC_DITHER_12_BPC;
8490 break;
8491 default:
8492 /* Case prevented by pipe_config_set_bpp. */
8493 BUG();
8494 }
8495
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008496 if (crtc_state->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008497 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8498
Shashank Sharma8c79f842018-10-12 11:53:09 +05308499 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8500 crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05308501 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
Shashank Sharma8c79f842018-10-12 11:53:09 +05308502
8503 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05308504 val |= PIPEMISC_YUV420_ENABLE |
Shashank Sharmab22ca992017-07-24 19:19:32 +05308505 PIPEMISC_YUV420_MODE_FULL_BLEND;
Shashank Sharmab22ca992017-07-24 19:19:32 +05308506
Jani Nikula391bf042016-03-18 17:05:40 +02008507 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008508 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008509}
8510
Paulo Zanonid4b19312012-11-29 11:29:32 -02008511int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8512{
8513 /*
8514 * Account for spread spectrum to avoid
8515 * oversubscribing the link. Max center spread
8516 * is 2.5%; use 5% for safety's sake.
8517 */
8518 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008519 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008520}
8521
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008522static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008523{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008524 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008525}
8526
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008527static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8528 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008529 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008530{
8531 struct drm_crtc *crtc = &intel_crtc->base;
8532 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008533 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008534 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008535 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008536
Chris Wilsonc1858122010-12-03 21:35:48 +00008537 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008538 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008539 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008540 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008541 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008542 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008543 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008544 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008545 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008546
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008547 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008548
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008549 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8550 fp |= FP_CB_TUNE;
8551
8552 if (reduced_clock) {
8553 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8554
8555 if (reduced_clock->m < factor * reduced_clock->n)
8556 fp2 |= FP_CB_TUNE;
8557 } else {
8558 fp2 = fp;
8559 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008560
Chris Wilson5eddb702010-09-11 13:48:45 +01008561 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008562
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008563 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008564 dpll |= DPLLB_MODE_LVDS;
8565 else
8566 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008567
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008568 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008569 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008570
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008571 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8572 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008573 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008574
Ville Syrjälä37a56502016-06-22 21:57:04 +03008575 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008576 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008577
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03008578 /*
8579 * The high speed IO clock is only really required for
8580 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8581 * possible to share the DPLL between CRT and HDMI. Enabling
8582 * the clock needlessly does no real harm, except use up a
8583 * bit of power potentially.
8584 *
8585 * We'll limit this to IVB with 3 pipes, since it has only two
8586 * DPLLs and so DPLL sharing is the only way to get three pipes
8587 * driving PCH ports at the same time. On SNB we could do this,
8588 * and potentially avoid enabling the second DPLL, but it's not
8589 * clear if it''s a win or loss power wise. No point in doing
8590 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8591 */
8592 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8593 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8594 dpll |= DPLL_SDVO_HIGH_SPEED;
8595
Eric Anholta07d6782011-03-30 13:01:08 -07008596 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008597 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008598 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008599 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008600
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008601 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008602 case 5:
8603 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8604 break;
8605 case 7:
8606 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8607 break;
8608 case 10:
8609 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8610 break;
8611 case 14:
8612 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8613 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008614 }
8615
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008616 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8617 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008618 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008619 else
8620 dpll |= PLL_REF_INPUT_DREFCLK;
8621
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008622 dpll |= DPLL_VCO_ENABLE;
8623
8624 crtc_state->dpll_hw_state.dpll = dpll;
8625 crtc_state->dpll_hw_state.fp0 = fp;
8626 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008627}
8628
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008629static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8630 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008631{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008632 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008633 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008634 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008635 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008636
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008637 memset(&crtc_state->dpll_hw_state, 0,
8638 sizeof(crtc_state->dpll_hw_state));
8639
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008640 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8641 if (!crtc_state->has_pch_encoder)
8642 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008643
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008644 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008645 if (intel_panel_use_ssc(dev_priv)) {
8646 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8647 dev_priv->vbt.lvds_ssc_freq);
8648 refclk = dev_priv->vbt.lvds_ssc_freq;
8649 }
8650
8651 if (intel_is_dual_link_lvds(dev)) {
8652 if (refclk == 100000)
8653 limit = &intel_limits_ironlake_dual_lvds_100m;
8654 else
8655 limit = &intel_limits_ironlake_dual_lvds;
8656 } else {
8657 if (refclk == 100000)
8658 limit = &intel_limits_ironlake_single_lvds_100m;
8659 else
8660 limit = &intel_limits_ironlake_single_lvds;
8661 }
8662 } else {
8663 limit = &intel_limits_ironlake_dac;
8664 }
8665
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008666 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008667 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8668 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008669 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8670 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008671 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008672
Gustavo A. R. Silvacbaa3312017-05-15 16:56:05 -05008673 ironlake_compute_dpll(crtc, crtc_state, NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008674
Gustavo A. R. Silvaefd38b62017-05-15 17:00:28 -05008675 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
Chris Wilson43031782018-09-13 14:16:26 +01008676 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
8677 pipe_name(crtc->pipe));
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008678 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008679 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008680
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008681 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008682}
8683
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008684static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8685 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008686{
8687 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008688 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008689 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008690
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008691 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8692 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8693 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8694 & ~TU_SIZE_MASK;
8695 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8696 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8697 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8698}
8699
8700static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8701 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008702 struct intel_link_m_n *m_n,
8703 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008704{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008705 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008706 enum pipe pipe = crtc->pipe;
8707
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008708 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008709 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8710 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8711 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8712 & ~TU_SIZE_MASK;
8713 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8714 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8715 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02008716
8717 if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008718 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8719 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8720 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8721 & ~TU_SIZE_MASK;
8722 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8723 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8724 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8725 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008726 } else {
8727 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8728 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8729 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8730 & ~TU_SIZE_MASK;
8731 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8732 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8733 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8734 }
8735}
8736
8737void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008738 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008739{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008740 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008741 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8742 else
8743 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008744 &pipe_config->dp_m_n,
8745 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008746}
8747
Daniel Vetter72419202013-04-04 13:28:53 +02008748static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008749 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008750{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008751 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008752 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008753}
8754
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008755static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008756 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008757{
8758 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008759 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07008760 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8761 uint32_t ps_ctrl = 0;
8762 int id = -1;
8763 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008764
Chandra Kondurua1b22782015-04-07 15:28:45 -07008765 /* find scaler attached to this pipe */
8766 for (i = 0; i < crtc->num_scalers; i++) {
8767 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8768 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8769 id = i;
8770 pipe_config->pch_pfit.enabled = true;
8771 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8772 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8773 break;
8774 }
8775 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008776
Chandra Kondurua1b22782015-04-07 15:28:45 -07008777 scaler_state->scaler_id = id;
8778 if (id >= 0) {
8779 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8780 } else {
8781 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008782 }
8783}
8784
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008785static void
8786skylake_get_initial_plane_config(struct intel_crtc *crtc,
8787 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008788{
8789 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008790 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008791 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
8792 enum plane_id plane_id = plane->id;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008793 enum pipe pipe;
James Ausmus4036c782017-11-13 10:11:28 -08008794 u32 val, base, offset, stride_mult, tiling, alpha;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008795 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008796 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008797 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008798 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008799
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008800 if (!plane->get_hw_state(plane, &pipe))
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02008801 return;
8802
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008803 WARN_ON(pipe != crtc->pipe);
8804
Damien Lespiaud9806c92015-01-21 14:07:19 +00008805 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008806 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008807 DRM_DEBUG_KMS("failed to alloc fb\n");
8808 return;
8809 }
8810
Damien Lespiau1b842c82015-01-21 13:50:54 +00008811 fb = &intel_fb->base;
8812
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008813 fb->dev = dev;
8814
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008815 val = I915_READ(PLANE_CTL(pipe, plane_id));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008816
James Ausmusb5972772018-01-30 11:49:16 -02008817 if (INTEL_GEN(dev_priv) >= 11)
8818 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
8819 else
8820 pixel_format = val & PLANE_CTL_FORMAT_MASK;
James Ausmus4036c782017-11-13 10:11:28 -08008821
8822 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008823 alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
James Ausmus4036c782017-11-13 10:11:28 -08008824 alpha &= PLANE_COLOR_ALPHA_MASK;
8825 } else {
8826 alpha = val & PLANE_CTL_ALPHA_MASK;
8827 }
8828
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008829 fourcc = skl_format_to_fourcc(pixel_format,
James Ausmus4036c782017-11-13 10:11:28 -08008830 val & PLANE_CTL_ORDER_RGBX, alpha);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008831 fb->format = drm_format_info(fourcc);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008832
Damien Lespiau40f46282015-02-27 11:15:21 +00008833 tiling = val & PLANE_CTL_TILED_MASK;
8834 switch (tiling) {
8835 case PLANE_CTL_TILED_LINEAR:
Ben Widawsky2f075562017-03-24 14:29:48 -07008836 fb->modifier = DRM_FORMAT_MOD_LINEAR;
Damien Lespiau40f46282015-02-27 11:15:21 +00008837 break;
8838 case PLANE_CTL_TILED_X:
8839 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008840 fb->modifier = I915_FORMAT_MOD_X_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008841 break;
8842 case PLANE_CTL_TILED_Y:
Imre Deak914a4fd2018-10-16 19:00:11 +03008843 plane_config->tiling = I915_TILING_Y;
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07008844 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008845 fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
8846 else
8847 fb->modifier = I915_FORMAT_MOD_Y_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008848 break;
8849 case PLANE_CTL_TILED_YF:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07008850 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008851 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
8852 else
8853 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008854 break;
8855 default:
8856 MISSING_CASE(tiling);
8857 goto error;
8858 }
8859
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008860 base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008861 plane_config->base = base;
8862
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008863 offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008864
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008865 val = I915_READ(PLANE_SIZE(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008866 fb->height = ((val >> 16) & 0xfff) + 1;
8867 fb->width = ((val >> 0) & 0x1fff) + 1;
8868
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008869 val = I915_READ(PLANE_STRIDE(pipe, plane_id));
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008870 stride_mult = intel_fb_stride_alignment(fb, 0);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008871 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8872
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008873 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008874
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008875 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008876
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008877 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8878 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008879 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008880 plane_config->size);
8881
Damien Lespiau2d140302015-02-05 17:22:18 +00008882 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008883 return;
8884
8885error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01008886 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008887}
8888
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008889static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008890 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008891{
8892 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008893 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008894 uint32_t tmp;
8895
8896 tmp = I915_READ(PF_CTL(crtc->pipe));
8897
8898 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008899 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008900 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8901 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008902
8903 /* We currently do not free assignements of panel fitters on
8904 * ivb/hsw (since we don't use the higher upscaling modes which
8905 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008906 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008907 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8908 PF_PIPE_SEL_IVB(crtc->pipe));
8909 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008910 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008911}
8912
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008913static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008914 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008915{
8916 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008917 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008918 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008919 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008920 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008921
Imre Deak17290502016-02-12 18:55:11 +02008922 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8923 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008924 return false;
8925
Shashank Sharmad9facae2018-10-12 11:53:07 +05308926 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
Daniel Vettere143a212013-07-04 12:01:15 +02008927 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008928 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008929
Imre Deak17290502016-02-12 18:55:11 +02008930 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008931 tmp = I915_READ(PIPECONF(crtc->pipe));
8932 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008933 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008934
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008935 switch (tmp & PIPECONF_BPC_MASK) {
8936 case PIPECONF_6BPC:
8937 pipe_config->pipe_bpp = 18;
8938 break;
8939 case PIPECONF_8BPC:
8940 pipe_config->pipe_bpp = 24;
8941 break;
8942 case PIPECONF_10BPC:
8943 pipe_config->pipe_bpp = 30;
8944 break;
8945 case PIPECONF_12BPC:
8946 pipe_config->pipe_bpp = 36;
8947 break;
8948 default:
8949 break;
8950 }
8951
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008952 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8953 pipe_config->limited_color_range = true;
8954
Daniel Vetterab9412b2013-05-03 11:49:46 +02008955 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008956 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008957 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008958
Daniel Vetter88adfff2013-03-28 10:42:01 +01008959 pipe_config->has_pch_encoder = true;
8960
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008961 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8962 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8963 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008964
8965 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008966
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008967 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03008968 /*
8969 * The pipe->pch transcoder and pch transcoder->pll
8970 * mapping is fixed.
8971 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008972 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008973 } else {
8974 tmp = I915_READ(PCH_DPLL_SEL);
8975 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008976 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008977 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008978 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008979 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008980
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008981 pipe_config->shared_dpll =
8982 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8983 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008984
Lucas De Marchiee1398b2018-03-20 15:06:33 -07008985 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
8986 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008987
8988 tmp = pipe_config->dpll_hw_state.dpll;
8989 pipe_config->pixel_multiplier =
8990 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8991 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008992
8993 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008994 } else {
8995 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008996 }
8997
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008998 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008999 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009000
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009001 ironlake_get_pfit_config(crtc, pipe_config);
9002
Imre Deak17290502016-02-12 18:55:11 +02009003 ret = true;
9004
9005out:
9006 intel_display_power_put(dev_priv, power_domain);
9007
9008 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009009}
9010
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009011static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9012{
Chris Wilson91c8a322016-07-05 10:40:23 +01009013 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009014 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009015
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009016 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009017 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009018 pipe_name(crtc->pipe));
9019
Imre Deak75e39682018-08-06 12:58:39 +03009020 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL2),
Imre Deak9c3a16c2017-08-14 18:15:30 +03009021 "Display power well on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009022 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009023 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9024 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03009025 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009026 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009027 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009028 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -05009029 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009030 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009031 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009032 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009033 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009034 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009035 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009036
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009037 /*
9038 * In theory we can still leave IRQs enabled, as long as only the HPD
9039 * interrupts remain enabled. We used to check for that, but since it's
9040 * gen-specific and since we only disable LCPLL after we fully disable
9041 * the interrupts, the check below should be enough.
9042 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009043 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009044}
9045
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009046static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9047{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009048 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009049 return I915_READ(D_COMP_HSW);
9050 else
9051 return I915_READ(D_COMP_BDW);
9052}
9053
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009054static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9055{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009056 if (IS_HASWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009057 mutex_lock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009058 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9059 val))
Chris Wilson79cf2192016-08-24 11:16:07 +01009060 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009061 mutex_unlock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009062 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009063 I915_WRITE(D_COMP_BDW, val);
9064 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009065 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009066}
9067
9068/*
9069 * This function implements pieces of two sequences from BSpec:
9070 * - Sequence for display software to disable LCPLL
9071 * - Sequence for display software to allow package C8+
9072 * The steps implemented here are just the steps that actually touch the LCPLL
9073 * register. Callers should take care of disabling all the display engine
9074 * functions, doing the mode unset, fixing interrupts, etc.
9075 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009076static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9077 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009078{
9079 uint32_t val;
9080
9081 assert_can_disable_lcpll(dev_priv);
9082
9083 val = I915_READ(LCPLL_CTL);
9084
9085 if (switch_to_fclk) {
9086 val |= LCPLL_CD_SOURCE_FCLK;
9087 I915_WRITE(LCPLL_CTL, val);
9088
Imre Deakf53dd632016-06-28 13:37:32 +03009089 if (wait_for_us(I915_READ(LCPLL_CTL) &
9090 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009091 DRM_ERROR("Switching to FCLK failed\n");
9092
9093 val = I915_READ(LCPLL_CTL);
9094 }
9095
9096 val |= LCPLL_PLL_DISABLE;
9097 I915_WRITE(LCPLL_CTL, val);
9098 POSTING_READ(LCPLL_CTL);
9099
Chris Wilson24d84412016-06-30 15:33:07 +01009100 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009101 DRM_ERROR("LCPLL still locked\n");
9102
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009103 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009104 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009105 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009106 ndelay(100);
9107
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009108 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9109 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009110 DRM_ERROR("D_COMP RCOMP still in progress\n");
9111
9112 if (allow_power_down) {
9113 val = I915_READ(LCPLL_CTL);
9114 val |= LCPLL_POWER_DOWN_ALLOW;
9115 I915_WRITE(LCPLL_CTL, val);
9116 POSTING_READ(LCPLL_CTL);
9117 }
9118}
9119
9120/*
9121 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9122 * source.
9123 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009124static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009125{
9126 uint32_t val;
9127
9128 val = I915_READ(LCPLL_CTL);
9129
9130 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9131 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9132 return;
9133
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009134 /*
9135 * Make sure we're not on PC8 state before disabling PC8, otherwise
9136 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009137 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009138 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009139
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009140 if (val & LCPLL_POWER_DOWN_ALLOW) {
9141 val &= ~LCPLL_POWER_DOWN_ALLOW;
9142 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009143 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009144 }
9145
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009146 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009147 val |= D_COMP_COMP_FORCE;
9148 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009149 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009150
9151 val = I915_READ(LCPLL_CTL);
9152 val &= ~LCPLL_PLL_DISABLE;
9153 I915_WRITE(LCPLL_CTL, val);
9154
Chris Wilson93220c02016-06-30 15:33:08 +01009155 if (intel_wait_for_register(dev_priv,
9156 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9157 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009158 DRM_ERROR("LCPLL not locked yet\n");
9159
9160 if (val & LCPLL_CD_SOURCE_FCLK) {
9161 val = I915_READ(LCPLL_CTL);
9162 val &= ~LCPLL_CD_SOURCE_FCLK;
9163 I915_WRITE(LCPLL_CTL, val);
9164
Imre Deakf53dd632016-06-28 13:37:32 +03009165 if (wait_for_us((I915_READ(LCPLL_CTL) &
9166 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009167 DRM_ERROR("Switching back to LCPLL failed\n");
9168 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009169
Mika Kuoppala59bad942015-01-16 11:34:40 +02009170 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009171
Ville Syrjälä4c75b942016-10-31 22:37:12 +02009172 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009173 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009174}
9175
Paulo Zanoni765dab672014-03-07 20:08:18 -03009176/*
9177 * Package states C8 and deeper are really deep PC states that can only be
9178 * reached when all the devices on the system allow it, so even if the graphics
9179 * device allows PC8+, it doesn't mean the system will actually get to these
9180 * states. Our driver only allows PC8+ when going into runtime PM.
9181 *
9182 * The requirements for PC8+ are that all the outputs are disabled, the power
9183 * well is disabled and most interrupts are disabled, and these are also
9184 * requirements for runtime PM. When these conditions are met, we manually do
9185 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9186 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9187 * hang the machine.
9188 *
9189 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9190 * the state of some registers, so when we come back from PC8+ we need to
9191 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9192 * need to take care of the registers kept by RC6. Notice that this happens even
9193 * if we don't put the device in PCI D3 state (which is what currently happens
9194 * because of the runtime PM support).
9195 *
9196 * For more, read "Display Sequences for Package C8" on the hardware
9197 * documentation.
9198 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009199void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009200{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009201 uint32_t val;
9202
Paulo Zanonic67a4702013-08-19 13:18:09 -03009203 DRM_DEBUG_KMS("Enabling package C8+\n");
9204
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009205 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009206 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9207 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9208 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9209 }
9210
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009211 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009212 hsw_disable_lcpll(dev_priv, true, true);
9213}
9214
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009215void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009216{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009217 uint32_t val;
9218
Paulo Zanonic67a4702013-08-19 13:18:09 -03009219 DRM_DEBUG_KMS("Disabling package C8+\n");
9220
9221 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009222 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009223
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009224 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009225 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9226 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9227 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9228 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009229}
9230
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009231static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9232 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009233{
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03009234 struct intel_atomic_state *state =
9235 to_intel_atomic_state(crtc_state->base.state);
9236
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009237 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009238 struct intel_encoder *encoder =
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03009239 intel_get_crtc_new_encoder(state, crtc_state);
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009240
9241 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
Chris Wilson43031782018-09-13 14:16:26 +01009242 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
9243 pipe_name(crtc->pipe));
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009244 return -EINVAL;
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009245 }
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009246 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009247
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009248 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009249}
9250
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009251static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
9252 enum port port,
9253 struct intel_crtc_state *pipe_config)
9254{
9255 enum intel_dpll_id id;
9256 u32 temp;
9257
9258 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
Paulo Zanonidfbd4502017-08-25 16:40:04 -03009259 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009260
9261 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
9262 return;
9263
9264 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9265}
9266
Paulo Zanoni970888e2018-05-21 17:25:44 -07009267static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
9268 enum port port,
9269 struct intel_crtc_state *pipe_config)
9270{
9271 enum intel_dpll_id id;
9272 u32 temp;
9273
9274 /* TODO: TBT pll not implemented. */
Vandita Kulkarni8ea59e62018-10-03 12:51:59 +05309275 if (intel_port_is_combophy(dev_priv, port)) {
Paulo Zanoni970888e2018-05-21 17:25:44 -07009276 temp = I915_READ(DPCLKA_CFGCR0_ICL) &
9277 DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
9278 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
9279
Vandita Kulkarnia54270d2018-10-03 12:52:00 +05309280 if (WARN_ON(!intel_dpll_is_combophy(id)))
Paulo Zanoni970888e2018-05-21 17:25:44 -07009281 return;
Vandita Kulkarni8ea59e62018-10-03 12:51:59 +05309282 } else if (intel_port_is_tc(dev_priv, port)) {
Vandita Kulkarnicb6caf72018-10-03 12:51:58 +05309283 id = icl_port_to_mg_pll_id(port);
Vandita Kulkarni8ea59e62018-10-03 12:51:59 +05309284 } else {
9285 WARN(1, "Invalid port %x\n", port);
Paulo Zanoni970888e2018-05-21 17:25:44 -07009286 return;
9287 }
9288
9289 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9290}
9291
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309292static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9293 enum port port,
9294 struct intel_crtc_state *pipe_config)
9295{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009296 enum intel_dpll_id id;
9297
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309298 switch (port) {
9299 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +02009300 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309301 break;
9302 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +02009303 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309304 break;
9305 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +02009306 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309307 break;
9308 default:
9309 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009310 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309311 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009312
9313 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309314}
9315
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009316static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9317 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009318 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009319{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009320 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009321 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009322
9323 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009324 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009325
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009326 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009327 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009328
9329 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009330}
9331
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009332static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9333 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009334 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009335{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009336 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009337 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009338
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009339 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009340 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009341 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009342 break;
9343 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009344 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009345 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009346 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009347 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009348 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009349 case PORT_CLK_SEL_LCPLL_810:
9350 id = DPLL_ID_LCPLL_810;
9351 break;
9352 case PORT_CLK_SEL_LCPLL_1350:
9353 id = DPLL_ID_LCPLL_1350;
9354 break;
9355 case PORT_CLK_SEL_LCPLL_2700:
9356 id = DPLL_ID_LCPLL_2700;
9357 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009358 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009359 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009360 /* fall through */
9361 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009362 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009363 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009364
9365 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009366}
9367
Jani Nikulacf304292016-03-18 17:05:41 +02009368static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9369 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009370 u64 *power_domain_mask)
Jani Nikulacf304292016-03-18 17:05:41 +02009371{
9372 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009373 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02009374 enum intel_display_power_domain power_domain;
9375 u32 tmp;
9376
Imre Deakd9a7bc62016-05-12 16:18:50 +03009377 /*
9378 * The pipe->transcoder mapping is fixed with the exception of the eDP
9379 * transcoder handled below.
9380 */
Jani Nikulacf304292016-03-18 17:05:41 +02009381 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9382
9383 /*
9384 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9385 * consistency and less surprising code; it's in always on power).
9386 */
9387 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9388 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9389 enum pipe trans_edp_pipe;
9390 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9391 default:
9392 WARN(1, "unknown pipe linked to edp transcoder\n");
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05009393 /* fall through */
Jani Nikulacf304292016-03-18 17:05:41 +02009394 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9395 case TRANS_DDI_EDP_INPUT_A_ON:
9396 trans_edp_pipe = PIPE_A;
9397 break;
9398 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9399 trans_edp_pipe = PIPE_B;
9400 break;
9401 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9402 trans_edp_pipe = PIPE_C;
9403 break;
9404 }
9405
9406 if (trans_edp_pipe == crtc->pipe)
9407 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9408 }
9409
9410 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9411 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9412 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009413 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikulacf304292016-03-18 17:05:41 +02009414
9415 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9416
9417 return tmp & PIPECONF_ENABLE;
9418}
9419
Jani Nikula4d1de972016-03-18 17:05:42 +02009420static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9421 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009422 u64 *power_domain_mask)
Jani Nikula4d1de972016-03-18 17:05:42 +02009423{
9424 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009425 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +02009426 enum intel_display_power_domain power_domain;
9427 enum port port;
9428 enum transcoder cpu_transcoder;
9429 u32 tmp;
9430
Jani Nikula4d1de972016-03-18 17:05:42 +02009431 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9432 if (port == PORT_A)
9433 cpu_transcoder = TRANSCODER_DSI_A;
9434 else
9435 cpu_transcoder = TRANSCODER_DSI_C;
9436
9437 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9438 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9439 continue;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009440 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikula4d1de972016-03-18 17:05:42 +02009441
Imre Deakdb18b6a2016-03-24 12:41:40 +02009442 /*
9443 * The PLL needs to be enabled with a valid divider
9444 * configuration, otherwise accessing DSI registers will hang
9445 * the machine. See BSpec North Display Engine
9446 * registers/MIPI[BXT]. We can break out here early, since we
9447 * need the same DSI PLL to be enabled for both DSI ports.
9448 */
Jani Nikulae5186342018-07-05 16:25:08 +03009449 if (!bxt_dsi_pll_is_enabled(dev_priv))
Imre Deakdb18b6a2016-03-24 12:41:40 +02009450 break;
9451
Jani Nikula4d1de972016-03-18 17:05:42 +02009452 /* XXX: this works for video mode only */
9453 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9454 if (!(tmp & DPI_ENABLE))
9455 continue;
9456
9457 tmp = I915_READ(MIPI_CTRL(port));
9458 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9459 continue;
9460
9461 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +02009462 break;
9463 }
9464
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009465 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +02009466}
9467
Daniel Vetter26804af2014-06-25 22:01:55 +03009468static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009469 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009470{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009471 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009472 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009473 enum port port;
9474 uint32_t tmp;
9475
9476 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9477
9478 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9479
Paulo Zanoni970888e2018-05-21 17:25:44 -07009480 if (IS_ICELAKE(dev_priv))
9481 icelake_get_ddi_pll(dev_priv, port, pipe_config);
9482 else if (IS_CANNONLAKE(dev_priv))
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009483 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9484 else if (IS_GEN9_BC(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009485 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009486 else if (IS_GEN9_LP(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309487 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009488 else
9489 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009490
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009491 pll = pipe_config->shared_dpll;
9492 if (pll) {
Lucas De Marchiee1398b2018-03-20 15:06:33 -07009493 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
9494 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009495 }
9496
Daniel Vetter26804af2014-06-25 22:01:55 +03009497 /*
9498 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9499 * DDI E. So just check whether this pipe is wired to DDI E and whether
9500 * the PCH transcoder is on.
9501 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009502 if (INTEL_GEN(dev_priv) < 9 &&
Damien Lespiauca370452013-12-03 13:56:24 +00009503 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009504 pipe_config->has_pch_encoder = true;
9505
9506 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9507 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9508 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9509
9510 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9511 }
9512}
9513
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009514static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009515 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009516{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009517 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02009518 enum intel_display_power_domain power_domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009519 u64 power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009520 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009521
Imre Deake79dfb52017-07-20 01:50:57 +03009522 intel_crtc_init_scalers(crtc, pipe_config);
Imre Deak5fb9dad2017-07-20 14:28:20 +03009523
Imre Deak17290502016-02-12 18:55:11 +02009524 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9525 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009526 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009527 power_domain_mask = BIT_ULL(power_domain);
Imre Deak17290502016-02-12 18:55:11 +02009528
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009529 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009530
Jani Nikulacf304292016-03-18 17:05:41 +02009531 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009532
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009533 if (IS_GEN9_LP(dev_priv) &&
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009534 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9535 WARN_ON(active);
9536 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +02009537 }
9538
Jani Nikulacf304292016-03-18 17:05:41 +02009539 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009540 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009541
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009542 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +02009543 haswell_get_ddi_port_state(crtc, pipe_config);
9544 intel_get_pipe_timings(crtc, pipe_config);
9545 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009546
Jani Nikulabc58be62016-03-18 17:05:39 +02009547 intel_get_pipe_src_size(crtc, pipe_config);
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05309548 intel_get_crtc_ycbcr_config(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009549
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009550 pipe_config->gamma_mode =
9551 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9552
Imre Deak17290502016-02-12 18:55:11 +02009553 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9554 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009555 power_domain_mask |= BIT_ULL(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009556 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009557 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009558 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009559 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009560 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009561
Maarten Lankhorst24f28452017-11-22 19:39:01 +01009562 if (hsw_crtc_supports_ips(crtc)) {
9563 if (IS_HASWELL(dev_priv))
9564 pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
9565 else {
9566 /*
9567 * We cannot readout IPS state on broadwell, set to
9568 * true so we can set it to a defined state on first
9569 * commit.
9570 */
9571 pipe_config->ips_enabled = true;
9572 }
9573 }
9574
Jani Nikula4d1de972016-03-18 17:05:42 +02009575 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9576 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009577 pipe_config->pixel_multiplier =
9578 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9579 } else {
9580 pipe_config->pixel_multiplier = 1;
9581 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009582
Imre Deak17290502016-02-12 18:55:11 +02009583out:
9584 for_each_power_domain(power_domain, power_domain_mask)
9585 intel_display_power_put(dev_priv, power_domain);
9586
Jani Nikulacf304292016-03-18 17:05:41 +02009587 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009588}
9589
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009590static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009591{
9592 struct drm_i915_private *dev_priv =
9593 to_i915(plane_state->base.plane->dev);
9594 const struct drm_framebuffer *fb = plane_state->base.fb;
9595 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9596 u32 base;
9597
9598 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9599 base = obj->phys_handle->busaddr;
9600 else
9601 base = intel_plane_ggtt_offset(plane_state);
9602
Ville Syrjäläc11ada02018-09-07 18:24:04 +03009603 base += plane_state->color_plane[0].offset;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009604
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009605 /* ILK+ do this automagically */
9606 if (HAS_GMCH_DISPLAY(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009607 plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009608 base += (plane_state->base.crtc_h *
9609 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9610
9611 return base;
9612}
9613
Ville Syrjäläed270222017-03-27 21:55:36 +03009614static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9615{
9616 int x = plane_state->base.crtc_x;
9617 int y = plane_state->base.crtc_y;
9618 u32 pos = 0;
9619
9620 if (x < 0) {
9621 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9622 x = -x;
9623 }
9624 pos |= x << CURSOR_X_SHIFT;
9625
9626 if (y < 0) {
9627 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9628 y = -y;
9629 }
9630 pos |= y << CURSOR_Y_SHIFT;
9631
9632 return pos;
9633}
9634
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009635static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9636{
9637 const struct drm_mode_config *config =
9638 &plane_state->base.plane->dev->mode_config;
9639 int width = plane_state->base.crtc_w;
9640 int height = plane_state->base.crtc_h;
9641
9642 return width > 0 && width <= config->cursor_width &&
9643 height > 0 && height <= config->cursor_height;
9644}
9645
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009646static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009647{
9648 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009649 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009650 int src_x, src_y;
9651 u32 offset;
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03009652 int ret;
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009653
9654 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
9655 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
9656
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03009657 ret = intel_plane_check_stride(plane_state);
9658 if (ret)
9659 return ret;
9660
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009661 src_x = plane_state->base.src_x >> 16;
9662 src_y = plane_state->base.src_y >> 16;
9663
9664 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9665 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
9666 plane_state, 0);
9667
9668 if (src_x != 0 || src_y != 0) {
9669 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9670 return -EINVAL;
9671 }
9672
9673 plane_state->color_plane[0].offset = offset;
9674
9675 return 0;
9676}
9677
9678static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9679 struct intel_plane_state *plane_state)
9680{
9681 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009682 int ret;
9683
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009684 if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9685 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9686 return -EINVAL;
9687 }
9688
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009689 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
9690 &crtc_state->base,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009691 DRM_PLANE_HELPER_NO_SCALING,
9692 DRM_PLANE_HELPER_NO_SCALING,
9693 true, true);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009694 if (ret)
9695 return ret;
9696
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009697 if (!plane_state->base.visible)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009698 return 0;
9699
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009700 ret = intel_plane_check_src_coordinates(plane_state);
9701 if (ret)
9702 return ret;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009703
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009704 ret = intel_cursor_check_surface(plane_state);
9705 if (ret)
9706 return ret;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009707
Ville Syrjälä659056f2017-03-27 21:55:39 +03009708 return 0;
9709}
9710
Ville Syrjäläddd57132018-09-07 18:24:02 +03009711static unsigned int
9712i845_cursor_max_stride(struct intel_plane *plane,
9713 u32 pixel_format, u64 modifier,
9714 unsigned int rotation)
9715{
9716 return 2048;
9717}
9718
Ville Syrjälä292889e2017-03-17 23:18:01 +02009719static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9720 const struct intel_plane_state *plane_state)
9721{
Ville Syrjälä292889e2017-03-17 23:18:01 +02009722 return CURSOR_ENABLE |
9723 CURSOR_GAMMA_ENABLE |
9724 CURSOR_FORMAT_ARGB |
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009725 CURSOR_STRIDE(plane_state->color_plane[0].stride);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009726}
9727
Ville Syrjälä659056f2017-03-27 21:55:39 +03009728static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9729{
Ville Syrjälä659056f2017-03-27 21:55:39 +03009730 int width = plane_state->base.crtc_w;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009731
9732 /*
9733 * 845g/865g are only limited by the width of their cursors,
9734 * the height is arbitrary up to the precision of the register.
9735 */
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009736 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009737}
9738
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009739static int i845_check_cursor(struct intel_crtc_state *crtc_state,
Ville Syrjälä659056f2017-03-27 21:55:39 +03009740 struct intel_plane_state *plane_state)
9741{
9742 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009743 int ret;
9744
9745 ret = intel_check_cursor(crtc_state, plane_state);
9746 if (ret)
9747 return ret;
9748
9749 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009750 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009751 return 0;
9752
9753 /* Check for which cursor types we support */
9754 if (!i845_cursor_size_ok(plane_state)) {
9755 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9756 plane_state->base.crtc_w,
9757 plane_state->base.crtc_h);
9758 return -EINVAL;
9759 }
9760
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009761 WARN_ON(plane_state->base.visible &&
9762 plane_state->color_plane[0].stride != fb->pitches[0]);
9763
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009764 switch (fb->pitches[0]) {
Chris Wilson560b85b2010-08-07 11:01:38 +01009765 case 256:
9766 case 512:
9767 case 1024:
9768 case 2048:
Ville Syrjälädc41c152014-08-13 11:57:05 +03009769 break;
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009770 default:
9771 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9772 fb->pitches[0]);
9773 return -EINVAL;
Chris Wilson560b85b2010-08-07 11:01:38 +01009774 }
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009775
Ville Syrjälä659056f2017-03-27 21:55:39 +03009776 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9777
9778 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009779}
9780
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009781static void i845_update_cursor(struct intel_plane *plane,
9782 const struct intel_crtc_state *crtc_state,
Chris Wilson560b85b2010-08-07 11:01:38 +01009783 const struct intel_plane_state *plane_state)
9784{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009785 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009786 u32 cntl = 0, base = 0, pos = 0, size = 0;
9787 unsigned long irqflags;
Chris Wilson560b85b2010-08-07 11:01:38 +01009788
Ville Syrjälä936e71e2016-07-26 19:06:59 +03009789 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009790 unsigned int width = plane_state->base.crtc_w;
9791 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009792
Ville Syrjäläa0864d52017-03-23 21:27:09 +02009793 cntl = plane_state->ctl;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009794 size = (height << 12) | width;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009795
9796 base = intel_cursor_base(plane_state);
9797 pos = intel_cursor_position(plane_state);
Chris Wilson4b0e3332014-05-30 16:35:26 +03009798 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009799
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009800 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9801
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009802 /* On these chipsets we can only modify the base/size/stride
9803 * whilst the cursor is disabled.
9804 */
9805 if (plane->cursor.base != base ||
9806 plane->cursor.size != size ||
9807 plane->cursor.cntl != cntl) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009808 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009809 I915_WRITE_FW(CURBASE(PIPE_A), base);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009810 I915_WRITE_FW(CURSIZE, size);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009811 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009812 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
Ville Syrjälä75343a42017-03-27 21:55:38 +03009813
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009814 plane->cursor.base = base;
9815 plane->cursor.size = size;
9816 plane->cursor.cntl = cntl;
9817 } else {
9818 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009819 }
9820
Ville Syrjälä75343a42017-03-27 21:55:38 +03009821 POSTING_READ_FW(CURCNTR(PIPE_A));
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009822
9823 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9824}
9825
9826static void i845_disable_cursor(struct intel_plane *plane,
9827 struct intel_crtc *crtc)
9828{
9829 i845_update_cursor(plane, NULL, NULL);
Chris Wilson560b85b2010-08-07 11:01:38 +01009830}
9831
Ville Syrjäläeade6c82018-01-30 22:38:03 +02009832static bool i845_cursor_get_hw_state(struct intel_plane *plane,
9833 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009834{
9835 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9836 enum intel_display_power_domain power_domain;
9837 bool ret;
9838
9839 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
9840 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9841 return false;
9842
9843 ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
9844
Ville Syrjäläeade6c82018-01-30 22:38:03 +02009845 *pipe = PIPE_A;
9846
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009847 intel_display_power_put(dev_priv, power_domain);
9848
9849 return ret;
9850}
9851
Ville Syrjäläddd57132018-09-07 18:24:02 +03009852static unsigned int
9853i9xx_cursor_max_stride(struct intel_plane *plane,
9854 u32 pixel_format, u64 modifier,
9855 unsigned int rotation)
9856{
9857 return plane->base.dev->mode_config.cursor_width * 4;
9858}
9859
Ville Syrjälä292889e2017-03-17 23:18:01 +02009860static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9861 const struct intel_plane_state *plane_state)
9862{
9863 struct drm_i915_private *dev_priv =
9864 to_i915(plane_state->base.plane->dev);
9865 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
José Roberto de Souzac894d632018-05-18 13:15:47 -07009866 u32 cntl = 0;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009867
Ville Syrjäläe876b782018-01-30 22:38:05 +02009868 if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
9869 cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
9870
José Roberto de Souzac894d632018-05-18 13:15:47 -07009871 if (INTEL_GEN(dev_priv) <= 10) {
9872 cntl |= MCURSOR_GAMMA_ENABLE;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009873
José Roberto de Souzac894d632018-05-18 13:15:47 -07009874 if (HAS_DDI(dev_priv))
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009875 cntl |= MCURSOR_PIPE_CSC_ENABLE;
José Roberto de Souzac894d632018-05-18 13:15:47 -07009876 }
Ville Syrjälä292889e2017-03-17 23:18:01 +02009877
Ville Syrjälä32ea06b2018-01-30 22:38:01 +02009878 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
9879 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009880
9881 switch (plane_state->base.crtc_w) {
9882 case 64:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009883 cntl |= MCURSOR_MODE_64_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009884 break;
9885 case 128:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009886 cntl |= MCURSOR_MODE_128_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009887 break;
9888 case 256:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009889 cntl |= MCURSOR_MODE_256_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009890 break;
9891 default:
9892 MISSING_CASE(plane_state->base.crtc_w);
9893 return 0;
9894 }
9895
Robert Fossc2c446a2017-05-19 16:50:17 -04009896 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009897 cntl |= MCURSOR_ROTATE_180;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009898
9899 return cntl;
9900}
9901
Ville Syrjälä659056f2017-03-27 21:55:39 +03009902static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009903{
Ville Syrjälä024faac2017-03-27 21:55:42 +03009904 struct drm_i915_private *dev_priv =
9905 to_i915(plane_state->base.plane->dev);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009906 int width = plane_state->base.crtc_w;
9907 int height = plane_state->base.crtc_h;
Chris Wilson560b85b2010-08-07 11:01:38 +01009908
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009909 if (!intel_cursor_size_ok(plane_state))
Ville Syrjälädc41c152014-08-13 11:57:05 +03009910 return false;
9911
Ville Syrjälä024faac2017-03-27 21:55:42 +03009912 /* Cursor width is limited to a few power-of-two sizes */
9913 switch (width) {
Ville Syrjälä659056f2017-03-27 21:55:39 +03009914 case 256:
9915 case 128:
Ville Syrjälä659056f2017-03-27 21:55:39 +03009916 case 64:
9917 break;
9918 default:
9919 return false;
9920 }
9921
Ville Syrjälädc41c152014-08-13 11:57:05 +03009922 /*
Ville Syrjälä024faac2017-03-27 21:55:42 +03009923 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9924 * height from 8 lines up to the cursor width, when the
9925 * cursor is not rotated. Everything else requires square
9926 * cursors.
Ville Syrjälädc41c152014-08-13 11:57:05 +03009927 */
Ville Syrjälä024faac2017-03-27 21:55:42 +03009928 if (HAS_CUR_FBC(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009929 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009930 if (height < 8 || height > width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009931 return false;
9932 } else {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009933 if (height != width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009934 return false;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009935 }
9936
9937 return true;
9938}
9939
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009940static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
Ville Syrjälä659056f2017-03-27 21:55:39 +03009941 struct intel_plane_state *plane_state)
9942{
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009943 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009944 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9945 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009946 enum pipe pipe = plane->pipe;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009947 int ret;
9948
9949 ret = intel_check_cursor(crtc_state, plane_state);
9950 if (ret)
9951 return ret;
9952
9953 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009954 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009955 return 0;
9956
9957 /* Check for which cursor types we support */
9958 if (!i9xx_cursor_size_ok(plane_state)) {
9959 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9960 plane_state->base.crtc_w,
9961 plane_state->base.crtc_h);
9962 return -EINVAL;
9963 }
9964
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009965 WARN_ON(plane_state->base.visible &&
9966 plane_state->color_plane[0].stride != fb->pitches[0]);
9967
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009968 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9969 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9970 fb->pitches[0], plane_state->base.crtc_w);
9971 return -EINVAL;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009972 }
9973
9974 /*
9975 * There's something wrong with the cursor on CHV pipe C.
9976 * If it straddles the left edge of the screen then
9977 * moving it away from the edge or disabling it often
9978 * results in a pipe underrun, and often that can lead to
9979 * dead pipe (constant underrun reported, and it scans
9980 * out just a solid color). To recover from that, the
9981 * display power well must be turned off and on again.
9982 * Refuse the put the cursor into that compromised position.
9983 */
9984 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9985 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9986 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9987 return -EINVAL;
9988 }
9989
9990 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
9991
9992 return 0;
9993}
9994
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009995static void i9xx_update_cursor(struct intel_plane *plane,
9996 const struct intel_crtc_state *crtc_state,
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309997 const struct intel_plane_state *plane_state)
9998{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009999 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10000 enum pipe pipe = plane->pipe;
Ville Syrjälä024faac2017-03-27 21:55:42 +030010001 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010002 unsigned long irqflags;
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010003
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010004 if (plane_state && plane_state->base.visible) {
Ville Syrjäläa0864d52017-03-23 21:27:09 +020010005 cntl = plane_state->ctl;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010006
Ville Syrjälä024faac2017-03-27 21:55:42 +030010007 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
10008 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
10009
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010010 base = intel_cursor_base(plane_state);
10011 pos = intel_cursor_position(plane_state);
10012 }
10013
10014 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
10015
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010016 /*
10017 * On some platforms writing CURCNTR first will also
10018 * cause CURPOS to be armed by the CURBASE write.
10019 * Without the CURCNTR write the CURPOS write would
Ville Syrjälä8753d2b2017-07-14 18:52:27 +030010020 * arm itself. Thus we always start the full update
10021 * with a CURCNTR write.
10022 *
10023 * On other platforms CURPOS always requires the
10024 * CURBASE write to arm the update. Additonally
10025 * a write to any of the cursor register will cancel
10026 * an already armed cursor update. Thus leaving out
10027 * the CURBASE write after CURPOS could lead to a
10028 * cursor that doesn't appear to move, or even change
10029 * shape. Thus we always write CURBASE.
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010030 *
10031 * CURCNTR and CUR_FBC_CTL are always
10032 * armed by the CURBASE write only.
10033 */
10034 if (plane->cursor.base != base ||
Ville Syrjälä024faac2017-03-27 21:55:42 +030010035 plane->cursor.size != fbc_ctl ||
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010036 plane->cursor.cntl != cntl) {
10037 I915_WRITE_FW(CURCNTR(pipe), cntl);
10038 if (HAS_CUR_FBC(dev_priv))
10039 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
10040 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä75343a42017-03-27 21:55:38 +030010041 I915_WRITE_FW(CURBASE(pipe), base);
10042
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010043 plane->cursor.base = base;
10044 plane->cursor.size = fbc_ctl;
10045 plane->cursor.cntl = cntl;
10046 } else {
10047 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä8753d2b2017-07-14 18:52:27 +030010048 I915_WRITE_FW(CURBASE(pipe), base);
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010049 }
10050
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010051 POSTING_READ_FW(CURBASE(pipe));
10052
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010053 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010054}
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010055
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010056static void i9xx_disable_cursor(struct intel_plane *plane,
10057 struct intel_crtc *crtc)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010058{
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010059 i9xx_update_cursor(plane, NULL, NULL);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010060}
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010061
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010062static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
10063 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010064{
10065 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10066 enum intel_display_power_domain power_domain;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010067 bool ret;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010068 u32 val;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010069
10070 /*
10071 * Not 100% correct for planes that can move between pipes,
10072 * but that's only the case for gen2-3 which don't have any
10073 * display power wells.
10074 */
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010075 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010076 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10077 return false;
10078
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010079 val = I915_READ(CURCNTR(plane->pipe));
10080
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +020010081 ret = val & MCURSOR_MODE;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010082
10083 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
10084 *pipe = plane->pipe;
10085 else
10086 *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
10087 MCURSOR_PIPE_SELECT_SHIFT;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010088
10089 intel_display_power_put(dev_priv, power_domain);
10090
10091 return ret;
10092}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010093
Jesse Barnes79e53942008-11-07 14:24:08 -080010094/* VESA 640x480x72Hz mode to set on the pipe */
Ville Syrjäläbacdcd52017-05-18 22:38:37 +030010095static const struct drm_display_mode load_detect_mode = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010096 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10097 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10098};
10099
Daniel Vettera8bb6812014-02-10 18:00:39 +010010100struct drm_framebuffer *
Chris Wilson24dbf512017-02-15 10:59:18 +000010101intel_framebuffer_create(struct drm_i915_gem_object *obj,
10102 struct drm_mode_fb_cmd2 *mode_cmd)
Chris Wilsond2dff872011-04-19 08:36:26 +010010103{
10104 struct intel_framebuffer *intel_fb;
10105 int ret;
10106
10107 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010108 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010109 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010110
Chris Wilson24dbf512017-02-15 10:59:18 +000010111 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010112 if (ret)
10113 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010114
10115 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010116
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010117err:
10118 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010119 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010120}
10121
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010122static int intel_modeset_disable_planes(struct drm_atomic_state *state,
10123 struct drm_crtc *crtc)
Chris Wilsond2dff872011-04-19 08:36:26 +010010124{
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010125 struct drm_plane *plane;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010126 struct drm_plane_state *plane_state;
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010127 int ret, i;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010128
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010129 ret = drm_atomic_add_affected_planes(state, crtc);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010130 if (ret)
10131 return ret;
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010132
10133 for_each_new_plane_in_state(state, plane, plane_state, i) {
10134 if (plane_state->crtc != crtc)
10135 continue;
10136
10137 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
10138 if (ret)
10139 return ret;
10140
10141 drm_atomic_set_fb_for_plane(plane_state, NULL);
10142 }
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010143
10144 return 0;
10145}
10146
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010147int intel_get_load_detect_pipe(struct drm_connector *connector,
Ville Syrjäläbacdcd52017-05-18 22:38:37 +030010148 const struct drm_display_mode *mode,
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010149 struct intel_load_detect_pipe *old,
10150 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010151{
10152 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010153 struct intel_encoder *intel_encoder =
10154 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010155 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010156 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010157 struct drm_crtc *crtc = NULL;
10158 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020010159 struct drm_i915_private *dev_priv = to_i915(dev);
Rob Clark51fd3712013-11-19 12:10:12 -050010160 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010161 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010162 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010163 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010164 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010165
Chris Wilsond2dff872011-04-19 08:36:26 +010010166 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010167 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010168 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010169
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010170 old->restore_state = NULL;
10171
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010172 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010173
Jesse Barnes79e53942008-11-07 14:24:08 -080010174 /*
10175 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010176 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010177 * - if the connector already has an assigned crtc, use it (but make
10178 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010179 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010180 * - try to find the first unused crtc that can drive this connector,
10181 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010182 */
10183
10184 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010185 if (connector->state->crtc) {
10186 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010187
Rob Clark51fd3712013-11-19 12:10:12 -050010188 ret = drm_modeset_lock(&crtc->mutex, ctx);
10189 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010190 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010191
10192 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010193 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010194 }
10195
10196 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010197 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010198 i++;
10199 if (!(encoder->possible_crtcs & (1 << i)))
10200 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010201
10202 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10203 if (ret)
10204 goto fail;
10205
10206 if (possible_crtc->state->enable) {
10207 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010208 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010209 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010210
10211 crtc = possible_crtc;
10212 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010213 }
10214
10215 /*
10216 * If we didn't find an unused CRTC, don't use any.
10217 */
10218 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010219 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Dan Carpenterf4bf77b2017-04-14 22:54:25 +030010220 ret = -ENODEV;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010221 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010222 }
10223
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010224found:
10225 intel_crtc = to_intel_crtc(crtc);
10226
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010227 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010228 restore_state = drm_atomic_state_alloc(dev);
10229 if (!state || !restore_state) {
10230 ret = -ENOMEM;
10231 goto fail;
10232 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010233
10234 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010235 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010236
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010237 connector_state = drm_atomic_get_connector_state(state, connector);
10238 if (IS_ERR(connector_state)) {
10239 ret = PTR_ERR(connector_state);
10240 goto fail;
10241 }
10242
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010243 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10244 if (ret)
10245 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010246
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010247 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10248 if (IS_ERR(crtc_state)) {
10249 ret = PTR_ERR(crtc_state);
10250 goto fail;
10251 }
10252
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010253 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010254
Chris Wilson64927112011-04-20 07:25:26 +010010255 if (!mode)
10256 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010257
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010258 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010259 if (ret)
10260 goto fail;
10261
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010262 ret = intel_modeset_disable_planes(state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010263 if (ret)
10264 goto fail;
10265
10266 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10267 if (!ret)
10268 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
Ville Syrjäläbe90cc32018-03-22 17:23:12 +020010269 if (!ret)
10270 ret = drm_atomic_add_affected_planes(restore_state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010271 if (ret) {
10272 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10273 goto fail;
10274 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010275
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010276 ret = drm_atomic_commit(state);
10277 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010278 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010279 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010280 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010281
10282 old->restore_state = restore_state;
Chris Wilson7abbd112017-01-19 11:37:49 +000010283 drm_atomic_state_put(state);
Chris Wilson71731882011-04-19 23:10:58 +010010284
Jesse Barnes79e53942008-11-07 14:24:08 -080010285 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020010286 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010287 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010288
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010289fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010010290 if (state) {
10291 drm_atomic_state_put(state);
10292 state = NULL;
10293 }
10294 if (restore_state) {
10295 drm_atomic_state_put(restore_state);
10296 restore_state = NULL;
10297 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010298
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010299 if (ret == -EDEADLK)
10300 return ret;
Rob Clark51fd3712013-11-19 12:10:12 -050010301
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010302 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010303}
10304
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010305void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010306 struct intel_load_detect_pipe *old,
10307 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010308{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010309 struct intel_encoder *intel_encoder =
10310 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010311 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010312 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010313 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010314
Chris Wilsond2dff872011-04-19 08:36:26 +010010315 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010316 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010317 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010318
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010319 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010320 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010321
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010010322 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Chris Wilson08536952016-10-14 13:18:18 +010010323 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010324 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010010325 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010326}
10327
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010328static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010329 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010330{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010331 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010332 u32 dpll = pipe_config->dpll_hw_state.dpll;
10333
10334 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010335 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010010336 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010337 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010338 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010339 return 96000;
10340 else
10341 return 48000;
10342}
10343
Jesse Barnes79e53942008-11-07 14:24:08 -080010344/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010345static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010346 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010347{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010348 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010349 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010350 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010351 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010352 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010353 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010354 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010355 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010356
10357 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010358 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010359 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010360 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010361
10362 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010363 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010364 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10365 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010366 } else {
10367 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10368 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10369 }
10370
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010371 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010372 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010373 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10374 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010375 else
10376 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010377 DPLL_FPA01_P1_POST_DIV_SHIFT);
10378
10379 switch (dpll & DPLL_MODE_MASK) {
10380 case DPLLB_MODE_DAC_SERIAL:
10381 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10382 5 : 10;
10383 break;
10384 case DPLLB_MODE_LVDS:
10385 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10386 7 : 14;
10387 break;
10388 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010389 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010390 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010391 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010392 }
10393
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010394 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +030010395 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010396 else
Imre Deakdccbea32015-06-22 23:35:51 +030010397 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010398 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010399 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010400 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010401
10402 if (is_lvds) {
10403 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10404 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010405
10406 if (lvds & LVDS_CLKB_POWER_UP)
10407 clock.p2 = 7;
10408 else
10409 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010410 } else {
10411 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10412 clock.p1 = 2;
10413 else {
10414 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10415 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10416 }
10417 if (dpll & PLL_P2_DIVIDE_BY_4)
10418 clock.p2 = 4;
10419 else
10420 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010421 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010422
Imre Deakdccbea32015-06-22 23:35:51 +030010423 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010424 }
10425
Ville Syrjälä18442d02013-09-13 16:00:08 +030010426 /*
10427 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010428 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010429 * encoder's get_config() function.
10430 */
Imre Deakdccbea32015-06-22 23:35:51 +030010431 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010432}
10433
Ville Syrjälä6878da02013-09-13 15:59:11 +030010434int intel_dotclock_calculate(int link_freq,
10435 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010436{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010437 /*
10438 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010439 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010440 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010441 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010442 *
10443 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010444 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010445 */
10446
Ville Syrjälä6878da02013-09-13 15:59:11 +030010447 if (!m_n->link_n)
10448 return 0;
10449
Chris Wilson31236982017-09-13 11:51:53 +010010450 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 +030010451}
10452
Ville Syrjälä18442d02013-09-13 16:00:08 +030010453static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010454 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010455{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010456 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010457
10458 /* read out port_clock from the DPLL */
10459 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010460
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010461 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010462 * In case there is an active pipe without active ports,
10463 * we may need some idea for the dotclock anyway.
10464 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010465 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010466 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010467 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010468 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010469}
10470
Ville Syrjäläde330812017-10-09 19:19:50 +030010471/* Returns the currently programmed mode of the given encoder. */
10472struct drm_display_mode *
10473intel_encoder_current_mode(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010474{
Ville Syrjäläde330812017-10-09 19:19:50 +030010475 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
10476 struct intel_crtc_state *crtc_state;
Jesse Barnes79e53942008-11-07 14:24:08 -080010477 struct drm_display_mode *mode;
Ville Syrjäläde330812017-10-09 19:19:50 +030010478 struct intel_crtc *crtc;
10479 enum pipe pipe;
10480
10481 if (!encoder->get_hw_state(encoder, &pipe))
10482 return NULL;
10483
10484 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -080010485
10486 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10487 if (!mode)
10488 return NULL;
10489
Ville Syrjäläde330812017-10-09 19:19:50 +030010490 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
10491 if (!crtc_state) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010492 kfree(mode);
10493 return NULL;
10494 }
10495
Ville Syrjäläde330812017-10-09 19:19:50 +030010496 crtc_state->base.crtc = &crtc->base;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010497
Ville Syrjäläde330812017-10-09 19:19:50 +030010498 if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
10499 kfree(crtc_state);
10500 kfree(mode);
10501 return NULL;
10502 }
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010503
Ville Syrjäläde330812017-10-09 19:19:50 +030010504 encoder->get_config(encoder, crtc_state);
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010505
Ville Syrjäläde330812017-10-09 19:19:50 +030010506 intel_mode_from_pipe_config(mode, crtc_state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010507
Ville Syrjäläde330812017-10-09 19:19:50 +030010508 kfree(crtc_state);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010509
Jesse Barnes79e53942008-11-07 14:24:08 -080010510 return mode;
10511}
10512
10513static void intel_crtc_destroy(struct drm_crtc *crtc)
10514{
10515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10516
10517 drm_crtc_cleanup(crtc);
10518 kfree(intel_crtc);
10519}
10520
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010521/**
10522 * intel_wm_need_update - Check whether watermarks need updating
10523 * @plane: drm plane
10524 * @state: new plane state
10525 *
10526 * Check current plane state versus the new one to determine whether
10527 * watermarks need to be recalculated.
10528 *
10529 * Returns true or false.
10530 */
10531static bool intel_wm_need_update(struct drm_plane *plane,
10532 struct drm_plane_state *state)
10533{
Matt Roperd21fbe82015-09-24 15:53:12 -070010534 struct intel_plane_state *new = to_intel_plane_state(state);
10535 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10536
10537 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010538 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010539 return true;
10540
10541 if (!cur->base.fb || !new->base.fb)
10542 return false;
10543
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010544 if (cur->base.fb->modifier != new->base.fb->modifier ||
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010545 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010546 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10547 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10548 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10549 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010550 return true;
10551
10552 return false;
10553}
10554
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010555static bool needs_scaling(const struct intel_plane_state *state)
Matt Roperd21fbe82015-09-24 15:53:12 -070010556{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010557 int src_w = drm_rect_width(&state->base.src) >> 16;
10558 int src_h = drm_rect_height(&state->base.src) >> 16;
10559 int dst_w = drm_rect_width(&state->base.dst);
10560 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070010561
10562 return (src_w != dst_w || src_h != dst_h);
10563}
10564
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010565int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
10566 struct drm_crtc_state *crtc_state,
10567 const struct intel_plane_state *old_plane_state,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010568 struct drm_plane_state *plane_state)
10569{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010570 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010571 struct drm_crtc *crtc = crtc_state->crtc;
10572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010573 struct intel_plane *plane = to_intel_plane(plane_state->plane);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010574 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080010575 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010576 bool mode_changed = needs_modeset(crtc_state);
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010577 bool was_crtc_enabled = old_crtc_state->base.active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010578 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010579 bool turn_off, turn_on, visible, was_visible;
10580 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030010581 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010582
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010583 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010584 ret = skl_update_scaler_plane(
10585 to_intel_crtc_state(crtc_state),
10586 to_intel_plane_state(plane_state));
10587 if (ret)
10588 return ret;
10589 }
10590
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010591 was_visible = old_plane_state->base.visible;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010592 visible = plane_state->visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010593
10594 if (!was_crtc_enabled && WARN_ON(was_visible))
10595 was_visible = false;
10596
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010597 /*
10598 * Visibility is calculated as if the crtc was on, but
10599 * after scaler setup everything depends on it being off
10600 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030010601 *
10602 * FIXME this is wrong for watermarks. Watermarks should also
10603 * be computed as if the pipe would be active. Perhaps move
10604 * per-plane wm computation to the .check_plane() hook, and
10605 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010606 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010607 if (!is_crtc_enabled) {
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010608 plane_state->visible = visible = false;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010609 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10610 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010611
10612 if (!was_visible && !visible)
10613 return 0;
10614
Maarten Lankhorste8861672016-02-24 11:24:26 +010010615 if (fb != old_plane_state->base.fb)
10616 pipe_config->fb_changed = true;
10617
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010618 turn_off = was_visible && (!visible || mode_changed);
10619 turn_on = visible && (!was_visible || mode_changed);
10620
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010621 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010622 intel_crtc->base.base.id, intel_crtc->base.name,
10623 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010624 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010625
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010626 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 +020010627 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010628 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010629 turn_off, turn_on, mode_changed);
10630
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010631 if (turn_on) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010632 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010633 pipe_config->update_wm_pre = true;
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010634
10635 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010636 if (plane->id != PLANE_CURSOR)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010637 pipe_config->disable_cxsr = true;
10638 } else if (turn_off) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010639 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010640 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010641
Ville Syrjälä852eb002015-06-24 22:00:07 +030010642 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010643 if (plane->id != PLANE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010644 pipe_config->disable_cxsr = true;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010645 } else if (intel_wm_need_update(&plane->base, plane_state)) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010646 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010647 /* FIXME bollocks */
10648 pipe_config->update_wm_pre = true;
10649 pipe_config->update_wm_post = true;
10650 }
Ville Syrjälä852eb002015-06-24 22:00:07 +030010651 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010652
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070010653 if (visible || was_visible)
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010654 pipe_config->fb_bits |= plane->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010655
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010656 /*
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010657 * ILK/SNB DVSACNTR/Sprite Enable
10658 * IVB SPR_CTL/Sprite Enable
10659 * "When in Self Refresh Big FIFO mode, a write to enable the
10660 * plane will be internally buffered and delayed while Big FIFO
10661 * mode is exiting."
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010662 *
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010663 * Which means that enabling the sprite can take an extra frame
10664 * when we start in big FIFO mode (LP1+). Thus we need to drop
10665 * down to LP0 and wait for vblank in order to make sure the
10666 * sprite gets enabled on the next vblank after the register write.
10667 * Doing otherwise would risk enabling the sprite one frame after
10668 * we've already signalled flip completion. We can resume LP1+
10669 * once the sprite has been enabled.
10670 *
10671 *
10672 * WaCxSRDisabledForSpriteScaling:ivb
10673 * IVB SPR_SCALE/Scaling Enable
10674 * "Low Power watermarks must be disabled for at least one
10675 * frame before enabling sprite scaling, and kept disabled
10676 * until sprite scaling is disabled."
10677 *
10678 * ILK/SNB DVSASCALE/Scaling Enable
10679 * "When in Self Refresh Big FIFO mode, scaling enable will be
10680 * masked off while Big FIFO mode is exiting."
10681 *
10682 * Despite the w/a only being listed for IVB we assume that
10683 * the ILK/SNB note has similar ramifications, hence we apply
10684 * the w/a on all three platforms.
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010685 */
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010686 if (plane->id == PLANE_SPRITE0 &&
10687 (IS_GEN5(dev_priv) || IS_GEN6(dev_priv) ||
10688 IS_IVYBRIDGE(dev_priv)) &&
10689 (turn_on || (!needs_scaling(old_plane_state) &&
10690 needs_scaling(to_intel_plane_state(plane_state)))))
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010691 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010692
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010693 return 0;
10694}
10695
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010696static bool encoders_cloneable(const struct intel_encoder *a,
10697 const struct intel_encoder *b)
10698{
10699 /* masks could be asymmetric, so check both ways */
10700 return a == b || (a->cloneable & (1 << b->type) &&
10701 b->cloneable & (1 << a->type));
10702}
10703
10704static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10705 struct intel_crtc *crtc,
10706 struct intel_encoder *encoder)
10707{
10708 struct intel_encoder *source_encoder;
10709 struct drm_connector *connector;
10710 struct drm_connector_state *connector_state;
10711 int i;
10712
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010713 for_each_new_connector_in_state(state, connector, connector_state, i) {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010714 if (connector_state->crtc != &crtc->base)
10715 continue;
10716
10717 source_encoder =
10718 to_intel_encoder(connector_state->best_encoder);
10719 if (!encoders_cloneable(encoder, source_encoder))
10720 return false;
10721 }
10722
10723 return true;
10724}
10725
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +020010726static int icl_add_linked_planes(struct intel_atomic_state *state)
10727{
10728 struct intel_plane *plane, *linked;
10729 struct intel_plane_state *plane_state, *linked_plane_state;
10730 int i;
10731
10732 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10733 linked = plane_state->linked_plane;
10734
10735 if (!linked)
10736 continue;
10737
10738 linked_plane_state = intel_atomic_get_plane_state(state, linked);
10739 if (IS_ERR(linked_plane_state))
10740 return PTR_ERR(linked_plane_state);
10741
10742 WARN_ON(linked_plane_state->linked_plane != plane);
10743 WARN_ON(linked_plane_state->slave == plane_state->slave);
10744 }
10745
10746 return 0;
10747}
10748
10749static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
10750{
10751 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
10752 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10753 struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->base.state);
10754 struct intel_plane *plane, *linked;
10755 struct intel_plane_state *plane_state;
10756 int i;
10757
10758 if (INTEL_GEN(dev_priv) < 11)
10759 return 0;
10760
10761 /*
10762 * Destroy all old plane links and make the slave plane invisible
10763 * in the crtc_state->active_planes mask.
10764 */
10765 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10766 if (plane->pipe != crtc->pipe || !plane_state->linked_plane)
10767 continue;
10768
10769 plane_state->linked_plane = NULL;
10770 if (plane_state->slave && !plane_state->base.visible)
10771 crtc_state->active_planes &= ~BIT(plane->id);
10772
10773 plane_state->slave = false;
10774 }
10775
10776 if (!crtc_state->nv12_planes)
10777 return 0;
10778
10779 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10780 struct intel_plane_state *linked_state = NULL;
10781
10782 if (plane->pipe != crtc->pipe ||
10783 !(crtc_state->nv12_planes & BIT(plane->id)))
10784 continue;
10785
10786 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
10787 if (!icl_is_nv12_y_plane(linked->id))
10788 continue;
10789
10790 if (crtc_state->active_planes & BIT(linked->id))
10791 continue;
10792
10793 linked_state = intel_atomic_get_plane_state(state, linked);
10794 if (IS_ERR(linked_state))
10795 return PTR_ERR(linked_state);
10796
10797 break;
10798 }
10799
10800 if (!linked_state) {
10801 DRM_DEBUG_KMS("Need %d free Y planes for NV12\n",
10802 hweight8(crtc_state->nv12_planes));
10803
10804 return -EINVAL;
10805 }
10806
10807 plane_state->linked_plane = linked;
10808
10809 linked_state->slave = true;
10810 linked_state->linked_plane = plane;
10811 crtc_state->active_planes |= BIT(linked->id);
10812 DRM_DEBUG_KMS("Using %s as Y plane for %s\n", linked->base.name, plane->base.name);
10813 }
10814
10815 return 0;
10816}
10817
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010818static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10819 struct drm_crtc_state *crtc_state)
10820{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010821 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010822 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010823 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010824 struct intel_crtc_state *pipe_config =
10825 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010826 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020010827 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010828 bool mode_changed = needs_modeset(crtc_state);
10829
Ville Syrjälä852eb002015-06-24 22:00:07 +030010830 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010831 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020010832
Maarten Lankhorstad421372015-06-15 12:33:42 +020010833 if (mode_changed && crtc_state->enable &&
10834 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010835 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020010836 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10837 pipe_config);
10838 if (ret)
10839 return ret;
10840 }
10841
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010842 if (crtc_state->color_mgmt_changed) {
10843 ret = intel_color_check(crtc, crtc_state);
10844 if (ret)
10845 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010010846
10847 /*
10848 * Changing color management on Intel hardware is
10849 * handled as part of planes update.
10850 */
10851 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010852 }
10853
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010854 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010855 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010010856 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080010857 if (ret) {
10858 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070010859 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080010860 }
10861 }
10862
10863 if (dev_priv->display.compute_intermediate_wm &&
10864 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10865 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10866 return 0;
10867
10868 /*
10869 * Calculate 'intermediate' watermarks that satisfy both the
10870 * old state and the new state. We can program these
10871 * immediately.
10872 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010873 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080010874 intel_crtc,
10875 pipe_config);
10876 if (ret) {
10877 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
10878 return ret;
10879 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070010880 } else if (dev_priv->display.compute_intermediate_wm) {
10881 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10882 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010883 }
10884
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010885 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010886 if (mode_changed)
10887 ret = skl_update_scaler_crtc(pipe_config);
10888
10889 if (!ret)
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +020010890 ret = icl_check_nv12_planes(pipe_config);
10891 if (!ret)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +053010892 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
10893 pipe_config);
10894 if (!ret)
Ander Conselvan de Oliveira6ebc6922017-02-23 09:15:59 +020010895 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010896 pipe_config);
10897 }
10898
Maarten Lankhorst24f28452017-11-22 19:39:01 +010010899 if (HAS_IPS(dev_priv))
10900 pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
10901
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010902 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010903}
10904
Jani Nikula65b38e02015-04-13 11:26:56 +030010905static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010906 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010907};
10908
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010909static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10910{
10911 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010912 struct drm_connector_list_iter conn_iter;
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010913
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010914 drm_connector_list_iter_begin(dev, &conn_iter);
10915 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020010916 if (connector->base.state->crtc)
Thomas Zimmermannef196b52018-06-18 13:01:50 +020010917 drm_connector_put(&connector->base);
Daniel Vetter8863dc72016-05-06 15:39:03 +020010918
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010919 if (connector->base.encoder) {
10920 connector->base.state->best_encoder =
10921 connector->base.encoder;
10922 connector->base.state->crtc =
10923 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020010924
Thomas Zimmermannef196b52018-06-18 13:01:50 +020010925 drm_connector_get(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010926 } else {
10927 connector->base.state->best_encoder = NULL;
10928 connector->base.state->crtc = NULL;
10929 }
10930 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010931 drm_connector_list_iter_end(&conn_iter);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010932}
10933
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010934static int
10935connected_sink_max_bpp(const struct drm_connector_state *conn_state,
10936 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010937{
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010938 int bpp;
10939 struct drm_display_info *info = &conn_state->connector->display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010940
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010941 switch (conn_state->max_bpc) {
10942 case 6 ... 7:
10943 bpp = 6 * 3;
10944 break;
10945 case 8 ... 9:
10946 bpp = 8 * 3;
10947 break;
10948 case 10 ... 11:
10949 bpp = 10 * 3;
10950 break;
10951 case 12:
10952 bpp = 12 * 3;
10953 break;
10954 default:
10955 return -EINVAL;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010956 }
10957
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010958 if (bpp < pipe_config->pipe_bpp) {
10959 DRM_DEBUG_KMS("Limiting display bpp to %d instead of Edid bpp "
10960 "%d, requested bpp %d, max platform bpp %d\n", bpp,
10961 3 * info->bpc, 3 * conn_state->max_requested_bpc,
10962 pipe_config->pipe_bpp);
10963 pipe_config->pipe_bpp = bpp;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010964 }
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010965 return 0;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010966}
10967
10968static int
10969compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010970 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010971{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010972 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010973 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010974 struct drm_connector *connector;
10975 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010976 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010977
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010978 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
10979 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010980 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010981 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020010982 bpp = 12*3;
10983 else
10984 bpp = 8*3;
10985
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010986
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010987 pipe_config->pipe_bpp = bpp;
10988
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010989 state = pipe_config->base.state;
10990
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010991 /* Clamp display bpp to EDID value */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010992 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010993 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010994 continue;
10995
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010996 if (connected_sink_max_bpp(connector_state, pipe_config) < 0)
10997 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010998 }
10999
11000 return bpp;
11001}
11002
Daniel Vetter644db712013-09-19 14:53:58 +020011003static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11004{
11005 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11006 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011007 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011008 mode->crtc_hdisplay, mode->crtc_hsync_start,
11009 mode->crtc_hsync_end, mode->crtc_htotal,
11010 mode->crtc_vdisplay, mode->crtc_vsync_start,
11011 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11012}
11013
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011014static inline void
11015intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011016 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011017{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011018 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11019 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011020 m_n->gmch_m, m_n->gmch_n,
11021 m_n->link_m, m_n->link_n, m_n->tu);
11022}
11023
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011024#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
11025
11026static const char * const output_type_str[] = {
11027 OUTPUT_TYPE(UNUSED),
11028 OUTPUT_TYPE(ANALOG),
11029 OUTPUT_TYPE(DVO),
11030 OUTPUT_TYPE(SDVO),
11031 OUTPUT_TYPE(LVDS),
11032 OUTPUT_TYPE(TVOUT),
11033 OUTPUT_TYPE(HDMI),
11034 OUTPUT_TYPE(DP),
11035 OUTPUT_TYPE(EDP),
11036 OUTPUT_TYPE(DSI),
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011037 OUTPUT_TYPE(DDI),
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011038 OUTPUT_TYPE(DP_MST),
11039};
11040
11041#undef OUTPUT_TYPE
11042
11043static void snprintf_output_types(char *buf, size_t len,
11044 unsigned int output_types)
11045{
11046 char *str = buf;
11047 int i;
11048
11049 str[0] = '\0';
11050
11051 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
11052 int r;
11053
11054 if ((output_types & BIT(i)) == 0)
11055 continue;
11056
11057 r = snprintf(str, len, "%s%s",
11058 str != buf ? "," : "", output_type_str[i]);
11059 if (r >= len)
11060 break;
11061 str += r;
11062 len -= r;
11063
11064 output_types &= ~BIT(i);
11065 }
11066
11067 WARN_ON_ONCE(output_types != 0);
11068}
11069
Shashank Sharmad9facae2018-10-12 11:53:07 +053011070static const char * const output_format_str[] = {
11071 [INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
11072 [INTEL_OUTPUT_FORMAT_RGB] = "RGB",
Shashank Sharma33b7f3e2018-10-12 11:53:08 +053011073 [INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
Shashank Sharma8c79f842018-10-12 11:53:09 +053011074 [INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
Shashank Sharmad9facae2018-10-12 11:53:07 +053011075};
11076
11077static const char *output_formats(enum intel_output_format format)
11078{
Shashank Sharma33b7f3e2018-10-12 11:53:08 +053011079 if (format >= ARRAY_SIZE(output_format_str))
Shashank Sharmad9facae2018-10-12 11:53:07 +053011080 format = INTEL_OUTPUT_FORMAT_INVALID;
11081 return output_format_str[format];
11082}
11083
Daniel Vetterc0b03412013-05-28 12:05:54 +020011084static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011085 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011086 const char *context)
11087{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011088 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011089 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011090 struct drm_plane *plane;
11091 struct intel_plane *intel_plane;
11092 struct intel_plane_state *state;
11093 struct drm_framebuffer *fb;
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011094 char buf[64];
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011095
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000011096 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11097 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011098
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011099 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
11100 DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
11101 buf, pipe_config->output_types);
11102
Shashank Sharmad9facae2018-10-12 11:53:07 +053011103 DRM_DEBUG_KMS("output format: %s\n",
11104 output_formats(pipe_config->output_format));
11105
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011106 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11107 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020011108 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011109
11110 if (pipe_config->has_pch_encoder)
11111 intel_dump_m_n_config(pipe_config, "fdi",
11112 pipe_config->fdi_lanes,
11113 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011114
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011115 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011116 intel_dump_m_n_config(pipe_config, "dp m_n",
11117 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000011118 if (pipe_config->has_drrs)
11119 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11120 pipe_config->lane_count,
11121 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011122 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011123
Daniel Vetter55072d12014-11-20 16:10:28 +010011124 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011125 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010011126
Daniel Vetterc0b03412013-05-28 12:05:54 +020011127 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011128 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011129 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011130 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11131 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011132 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011133 pipe_config->port_clock,
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011134 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11135 pipe_config->pixel_rate);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011136
11137 if (INTEL_GEN(dev_priv) >= 9)
11138 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11139 crtc->num_scalers,
11140 pipe_config->scaler_state.scaler_users,
11141 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011142
11143 if (HAS_GMCH_DISPLAY(dev_priv))
11144 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11145 pipe_config->gmch_pfit.control,
11146 pipe_config->gmch_pfit.pgm_ratios,
11147 pipe_config->gmch_pfit.lvds_border_bits);
11148 else
11149 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11150 pipe_config->pch_pfit.pos,
11151 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000011152 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011153
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011154 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11155 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011156
Ander Conselvan de Oliveiraf50b79f2016-12-29 17:22:12 +020011157 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011158
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011159 DRM_DEBUG_KMS("planes on this crtc\n");
11160 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011161 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011162 intel_plane = to_intel_plane(plane);
11163 if (intel_plane->pipe != crtc->pipe)
11164 continue;
11165
11166 state = to_intel_plane_state(plane->state);
11167 fb = state->base.fb;
11168 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030011169 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11170 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011171 continue;
11172 }
11173
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011174 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11175 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011176 fb->base.id, fb->width, fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +020011177 drm_get_format_name(fb->format->format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011178 if (INTEL_GEN(dev_priv) >= 9)
11179 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11180 state->scaler_id,
11181 state->base.src.x1 >> 16,
11182 state->base.src.y1 >> 16,
11183 drm_rect_width(&state->base.src) >> 16,
11184 drm_rect_height(&state->base.src) >> 16,
11185 state->base.dst.x1, state->base.dst.y1,
11186 drm_rect_width(&state->base.dst),
11187 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011188 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011189}
11190
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011191static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011192{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011193 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011194 struct drm_connector *connector;
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011195 struct drm_connector_list_iter conn_iter;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011196 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011197 unsigned int used_mst_ports = 0;
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011198 bool ret = true;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011199
11200 /*
11201 * Walk the connector list instead of the encoder
11202 * list to detect the problem on ddi platforms
11203 * where there's just one encoder per digital port.
11204 */
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011205 drm_connector_list_iter_begin(dev, &conn_iter);
11206 drm_for_each_connector_iter(connector, &conn_iter) {
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011207 struct drm_connector_state *connector_state;
11208 struct intel_encoder *encoder;
11209
Maarten Lankhorst8b694492018-04-09 14:46:55 +020011210 connector_state = drm_atomic_get_new_connector_state(state, connector);
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011211 if (!connector_state)
11212 connector_state = connector->state;
11213
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011214 if (!connector_state->best_encoder)
11215 continue;
11216
11217 encoder = to_intel_encoder(connector_state->best_encoder);
11218
11219 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011220
11221 switch (encoder->type) {
11222 unsigned int port_mask;
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011223 case INTEL_OUTPUT_DDI:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011224 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011225 break;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -050011226 /* else: fall through */
Ville Syrjäläcca05022016-06-22 21:57:06 +030011227 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011228 case INTEL_OUTPUT_HDMI:
11229 case INTEL_OUTPUT_EDP:
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020011230 port_mask = 1 << encoder->port;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011231
11232 /* the same port mustn't appear more than once */
11233 if (used_ports & port_mask)
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011234 ret = false;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011235
11236 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011237 break;
11238 case INTEL_OUTPUT_DP_MST:
11239 used_mst_ports |=
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020011240 1 << encoder->port;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011241 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011242 default:
11243 break;
11244 }
11245 }
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011246 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011247
Ville Syrjälä477321e2016-07-28 17:50:40 +030011248 /* can't mix MST and SST/HDMI on the same port */
11249 if (used_ports & used_mst_ports)
11250 return false;
11251
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011252 return ret;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011253}
11254
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011255static void
11256clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11257{
Ville Syrjäläff32c542017-03-02 19:14:57 +020011258 struct drm_i915_private *dev_priv =
11259 to_i915(crtc_state->base.crtc->dev);
Chandra Konduru663a3642015-04-07 15:28:41 -070011260 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011261 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011262 struct intel_shared_dpll *shared_dpll;
Ville Syrjäläff32c542017-03-02 19:14:57 +020011263 struct intel_crtc_wm_state wm_state;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011264 bool force_thru, ips_force_disable;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011265
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011266 /* FIXME: before the switch to atomic started, a new pipe_config was
11267 * kzalloc'd. Code that depends on any field being zero should be
11268 * fixed, so that the crtc_state can be safely duplicated. For now,
11269 * only fields that are know to not cause problems are preserved. */
11270
Chandra Konduru663a3642015-04-07 15:28:41 -070011271 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011272 shared_dpll = crtc_state->shared_dpll;
11273 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011274 force_thru = crtc_state->pch_pfit.force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011275 ips_force_disable = crtc_state->ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011276 if (IS_G4X(dev_priv) ||
11277 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011278 wm_state = crtc_state->wm;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011279
Chris Wilsond2fa80a2017-03-03 15:46:44 +000011280 /* Keep base drm_crtc_state intact, only clear our extended struct */
11281 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11282 memset(&crtc_state->base + 1, 0,
11283 sizeof(*crtc_state) - sizeof(crtc_state->base));
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011284
Chandra Konduru663a3642015-04-07 15:28:41 -070011285 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011286 crtc_state->shared_dpll = shared_dpll;
11287 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011288 crtc_state->pch_pfit.force_thru = force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011289 crtc_state->ips_force_disable = ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011290 if (IS_G4X(dev_priv) ||
11291 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011292 crtc_state->wm = wm_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011293}
11294
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011295static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011296intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011297 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011298{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011299 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020011300 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011301 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011302 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011303 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011304 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011305 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011306
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011307 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011308
Daniel Vettere143a212013-07-04 12:01:15 +020011309 pipe_config->cpu_transcoder =
11310 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011311
Imre Deak2960bc92013-07-30 13:36:32 +030011312 /*
11313 * Sanitize sync polarity flags based on requested ones. If neither
11314 * positive or negative polarity is requested, treat this as meaning
11315 * negative polarity.
11316 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011317 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011318 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011319 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011320
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011321 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011322 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011323 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011324
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011325 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11326 pipe_config);
11327 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011328 goto fail;
11329
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011330 /*
11331 * Determine the real pipe dimensions. Note that stereo modes can
11332 * increase the actual pipe size due to the frame doubling and
11333 * insertion of additional space for blanks between the frame. This
11334 * is stored in the crtc timings. We use the requested mode to do this
11335 * computation to clearly distinguish it from the adjusted mode, which
11336 * can be changed by the connectors in the below retry loop.
11337 */
Daniel Vetter196cd5d2017-01-25 07:26:56 +010011338 drm_mode_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011339 &pipe_config->pipe_src_w,
11340 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011341
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011342 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011343 if (connector_state->crtc != crtc)
11344 continue;
11345
11346 encoder = to_intel_encoder(connector_state->best_encoder);
11347
Ville Syrjäläe25148d2016-06-22 21:57:09 +030011348 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11349 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11350 goto fail;
11351 }
11352
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011353 /*
11354 * Determine output_types before calling the .compute_config()
11355 * hooks so that the hooks can use this information safely.
11356 */
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011357 if (encoder->compute_output_type)
11358 pipe_config->output_types |=
11359 BIT(encoder->compute_output_type(encoder, pipe_config,
11360 connector_state));
11361 else
11362 pipe_config->output_types |= BIT(encoder->type);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011363 }
11364
Daniel Vettere29c22c2013-02-21 00:00:16 +010011365encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011366 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011367 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011368 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011369
Daniel Vetter135c81b2013-07-21 21:37:09 +020011370 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011371 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11372 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011373
Daniel Vetter7758a112012-07-08 19:40:39 +020011374 /* Pass our mode to the connectors and the CRTC to give them a chance to
11375 * adjust it according to limitations or connector properties, and also
11376 * a chance to reject the mode entirely.
11377 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011378 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011379 if (connector_state->crtc != crtc)
11380 continue;
11381
11382 encoder = to_intel_encoder(connector_state->best_encoder);
11383
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020011384 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020011385 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011386 goto fail;
11387 }
11388 }
11389
Daniel Vetterff9a6752013-06-01 17:16:21 +020011390 /* Set default port clock if not overwritten by the encoder. Needs to be
11391 * done afterwards in case the encoder adjusts the mode. */
11392 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011393 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011394 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011395
Daniel Vettera43f6e02013-06-07 23:10:32 +020011396 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011397 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011398 DRM_DEBUG_KMS("CRTC fixup failed\n");
11399 goto fail;
11400 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011401
11402 if (ret == RETRY) {
11403 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11404 ret = -EINVAL;
11405 goto fail;
11406 }
11407
11408 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11409 retry = false;
11410 goto encoder_retry;
11411 }
11412
Daniel Vettere8fa4272015-08-12 11:43:34 +020011413 /* Dithering seems to not pass-through bits correctly when it should, so
Manasi Navare611032b2017-01-24 08:21:49 -080011414 * only enable it on 6bpc panels and when its not a compliance
11415 * test requesting 6bpc video pattern.
11416 */
11417 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11418 !pipe_config->dither_force_disable;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020011419 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011420 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011421
Daniel Vetter7758a112012-07-08 19:40:39 +020011422fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011423 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011424}
11425
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011426static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011427{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011428 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011429
11430 if (clock1 == clock2)
11431 return true;
11432
11433 if (!clock1 || !clock2)
11434 return false;
11435
11436 diff = abs(clock1 - clock2);
11437
11438 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11439 return true;
11440
11441 return false;
11442}
11443
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011444static bool
11445intel_compare_m_n(unsigned int m, unsigned int n,
11446 unsigned int m2, unsigned int n2,
11447 bool exact)
11448{
11449 if (m == m2 && n == n2)
11450 return true;
11451
11452 if (exact || !m || !n || !m2 || !n2)
11453 return false;
11454
11455 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11456
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011457 if (n > n2) {
11458 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011459 m2 <<= 1;
11460 n2 <<= 1;
11461 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011462 } else if (n < n2) {
11463 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011464 m <<= 1;
11465 n <<= 1;
11466 }
11467 }
11468
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011469 if (n != n2)
11470 return false;
11471
11472 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011473}
11474
11475static bool
11476intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11477 struct intel_link_m_n *m2_n2,
11478 bool adjust)
11479{
11480 if (m_n->tu == m2_n2->tu &&
11481 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11482 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11483 intel_compare_m_n(m_n->link_m, m_n->link_n,
11484 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11485 if (adjust)
11486 *m2_n2 = *m_n;
11487
11488 return true;
11489 }
11490
11491 return false;
11492}
11493
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011494static void __printf(3, 4)
11495pipe_config_err(bool adjust, const char *name, const char *format, ...)
11496{
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011497 struct va_format vaf;
11498 va_list args;
11499
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011500 va_start(args, format);
11501 vaf.fmt = format;
11502 vaf.va = &args;
11503
Joe Perches99a95482018-03-13 15:02:15 -070011504 if (adjust)
11505 drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
11506 else
11507 drm_err("mismatch in %s %pV", name, &vaf);
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011508
11509 va_end(args);
11510}
11511
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011512static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011513intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011514 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011515 struct intel_crtc_state *pipe_config,
11516 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011517{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011518 bool ret = true;
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011519 bool fixup_inherited = adjust &&
11520 (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
11521 !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011522
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011523#define PIPE_CONF_CHECK_X(name) do { \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011524 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011525 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011526 "(expected 0x%08x, found 0x%08x)\n", \
11527 current_config->name, \
11528 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011529 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011530 } \
11531} while (0)
Daniel Vetter66e985c2013-06-05 13:34:20 +020011532
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011533#define PIPE_CONF_CHECK_I(name) do { \
Daniel Vetter08a24032013-04-19 11:25:34 +020011534 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011535 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter08a24032013-04-19 11:25:34 +020011536 "(expected %i, found %i)\n", \
11537 current_config->name, \
11538 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011539 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011540 } \
11541} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011542
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011543#define PIPE_CONF_CHECK_BOOL(name) do { \
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011544 if (current_config->name != pipe_config->name) { \
11545 pipe_config_err(adjust, __stringify(name), \
11546 "(expected %s, found %s)\n", \
11547 yesno(current_config->name), \
11548 yesno(pipe_config->name)); \
11549 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011550 } \
11551} while (0)
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011552
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011553/*
11554 * Checks state where we only read out the enabling, but not the entire
11555 * state itself (like full infoframes or ELD for audio). These states
11556 * require a full modeset on bootup to fix up.
11557 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011558#define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011559 if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
11560 PIPE_CONF_CHECK_BOOL(name); \
11561 } else { \
11562 pipe_config_err(adjust, __stringify(name), \
11563 "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
11564 yesno(current_config->name), \
11565 yesno(pipe_config->name)); \
11566 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011567 } \
11568} while (0)
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011569
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011570#define PIPE_CONF_CHECK_P(name) do { \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011571 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011572 pipe_config_err(adjust, __stringify(name), \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011573 "(expected %p, found %p)\n", \
11574 current_config->name, \
11575 pipe_config->name); \
11576 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011577 } \
11578} while (0)
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011579
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011580#define PIPE_CONF_CHECK_M_N(name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011581 if (!intel_compare_link_m_n(&current_config->name, \
11582 &pipe_config->name,\
11583 adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011584 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011585 "(expected tu %i gmch %i/%i link %i/%i, " \
11586 "found tu %i, gmch %i/%i link %i/%i)\n", \
11587 current_config->name.tu, \
11588 current_config->name.gmch_m, \
11589 current_config->name.gmch_n, \
11590 current_config->name.link_m, \
11591 current_config->name.link_n, \
11592 pipe_config->name.tu, \
11593 pipe_config->name.gmch_m, \
11594 pipe_config->name.gmch_n, \
11595 pipe_config->name.link_m, \
11596 pipe_config->name.link_n); \
11597 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011598 } \
11599} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011600
Daniel Vetter55c561a2016-03-30 11:34:36 +020011601/* This is required for BDW+ where there is only one set of registers for
11602 * switching between high and low RR.
11603 * This macro can be used whenever a comparison has to be made between one
11604 * hw state and multiple sw state variables.
11605 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011606#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011607 if (!intel_compare_link_m_n(&current_config->name, \
11608 &pipe_config->name, adjust) && \
11609 !intel_compare_link_m_n(&current_config->alt_name, \
11610 &pipe_config->name, adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011611 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011612 "(expected tu %i gmch %i/%i link %i/%i, " \
11613 "or tu %i gmch %i/%i link %i/%i, " \
11614 "found tu %i, gmch %i/%i link %i/%i)\n", \
11615 current_config->name.tu, \
11616 current_config->name.gmch_m, \
11617 current_config->name.gmch_n, \
11618 current_config->name.link_m, \
11619 current_config->name.link_n, \
11620 current_config->alt_name.tu, \
11621 current_config->alt_name.gmch_m, \
11622 current_config->alt_name.gmch_n, \
11623 current_config->alt_name.link_m, \
11624 current_config->alt_name.link_n, \
11625 pipe_config->name.tu, \
11626 pipe_config->name.gmch_m, \
11627 pipe_config->name.gmch_n, \
11628 pipe_config->name.link_m, \
11629 pipe_config->name.link_n); \
11630 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011631 } \
11632} while (0)
Daniel Vetter88adfff2013-03-28 10:42:01 +010011633
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011634#define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011635 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011636 pipe_config_err(adjust, __stringify(name), \
11637 "(%x) (expected %i, found %i)\n", \
11638 (mask), \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011639 current_config->name & (mask), \
11640 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011641 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011642 } \
11643} while (0)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011644
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011645#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011646 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011647 pipe_config_err(adjust, __stringify(name), \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011648 "(expected %i, found %i)\n", \
11649 current_config->name, \
11650 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011651 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011652 } \
11653} while (0)
Ville Syrjälä5e550652013-09-06 23:29:07 +030011654
Daniel Vetterbb760062013-06-06 14:55:52 +020011655#define PIPE_CONF_QUIRK(quirk) \
11656 ((current_config->quirks | pipe_config->quirks) & (quirk))
11657
Daniel Vettereccb1402013-05-22 00:50:22 +020011658 PIPE_CONF_CHECK_I(cpu_transcoder);
11659
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011660 PIPE_CONF_CHECK_BOOL(has_pch_encoder);
Daniel Vetter08a24032013-04-19 11:25:34 +020011661 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011662 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020011663
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011664 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030011665 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011666
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011667 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011668 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011669
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011670 if (current_config->has_drrs)
11671 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11672 } else
11673 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011674
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011675 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020011676
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011677 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11678 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11679 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11680 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11681 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11682 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011683
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011684 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11685 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11686 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11687 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11688 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11689 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011690
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011691 PIPE_CONF_CHECK_I(pixel_multiplier);
Shashank Sharmad9facae2018-10-12 11:53:07 +053011692 PIPE_CONF_CHECK_I(output_format);
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011693 PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011694 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010011695 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011696 PIPE_CONF_CHECK_BOOL(limited_color_range);
Shashank Sharma15953632017-03-13 16:54:03 +053011697
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011698 PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
11699 PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011700 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011701
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011702 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011703
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011704 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011705 DRM_MODE_FLAG_INTERLACE);
11706
Daniel Vetterbb760062013-06-06 14:55:52 +020011707 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011708 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011709 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011710 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011711 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011712 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011713 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011714 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011715 DRM_MODE_FLAG_NVSYNC);
11716 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011717
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011718 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020011719 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011720 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020011721 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011722 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020011723
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011724 if (!adjust) {
11725 PIPE_CONF_CHECK_I(pipe_src_w);
11726 PIPE_CONF_CHECK_I(pipe_src_h);
11727
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011728 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011729 if (current_config->pch_pfit.enabled) {
11730 PIPE_CONF_CHECK_X(pch_pfit.pos);
11731 PIPE_CONF_CHECK_X(pch_pfit.size);
11732 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011733
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011734 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011735 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011736 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070011737
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011738 PIPE_CONF_CHECK_BOOL(double_wide);
Ville Syrjälä282740f2013-09-04 18:30:03 +030011739
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011740 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011741 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011742 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011743 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11744 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011745 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010011746 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011747 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11748 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11749 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Paulo Zanoni2de38132017-09-22 17:53:42 -030011750 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
11751 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
11752 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
11753 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
11754 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
11755 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
11756 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
11757 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
11758 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
11759 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
11760 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
11761 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
Paulo Zanonic27e9172018-04-27 16:14:36 -070011762 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
11763 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
11764 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
11765 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
11766 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
11767 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
11768 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
11769 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
11770 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
11771 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011772
Ville Syrjälä47eacba2016-04-12 22:14:35 +030011773 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11774 PIPE_CONF_CHECK_X(dsi_pll.div);
11775
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011776 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011777 PIPE_CONF_CHECK_I(pipe_bpp);
11778
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011779 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011780 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011781
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030011782 PIPE_CONF_CHECK_I(min_voltage_level);
11783
Daniel Vetter66e985c2013-06-05 13:34:20 +020011784#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011785#undef PIPE_CONF_CHECK_I
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011786#undef PIPE_CONF_CHECK_BOOL
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011787#undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011788#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011789#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011790#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011791#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011792
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011793 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011794}
11795
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011796static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11797 const struct intel_crtc_state *pipe_config)
11798{
11799 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011800 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011801 &pipe_config->fdi_m_n);
11802 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11803
11804 /*
11805 * FDI already provided one idea for the dotclock.
11806 * Yell if the encoder disagrees.
11807 */
11808 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11809 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11810 fdi_dotclock, dotclock);
11811 }
11812}
11813
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011814static void verify_wm_state(struct drm_crtc *crtc,
11815 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000011816{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011817 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000011818 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011819 struct skl_pipe_wm hw_wm, *sw_wm;
11820 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11821 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11823 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011824 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000011825
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011826 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000011827 return;
11828
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011829 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020011830 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011831
Damien Lespiau08db6652014-11-04 17:06:52 +000011832 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11833 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11834
Mahesh Kumar74bd8002018-04-26 19:55:15 +053011835 if (INTEL_GEN(dev_priv) >= 11)
11836 if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
11837 DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
11838 sw_ddb->enabled_slices,
11839 hw_ddb.enabled_slices);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011840 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070011841 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011842 hw_plane_wm = &hw_wm.planes[plane];
11843 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000011844
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011845 /* Watermarks */
11846 for (level = 0; level <= max_level; level++) {
11847 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11848 &sw_plane_wm->wm[level]))
11849 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000011850
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011851 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",
11852 pipe_name(pipe), plane + 1, level,
11853 sw_plane_wm->wm[level].plane_en,
11854 sw_plane_wm->wm[level].plane_res_b,
11855 sw_plane_wm->wm[level].plane_res_l,
11856 hw_plane_wm->wm[level].plane_en,
11857 hw_plane_wm->wm[level].plane_res_b,
11858 hw_plane_wm->wm[level].plane_res_l);
11859 }
11860
11861 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11862 &sw_plane_wm->trans_wm)) {
11863 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",
11864 pipe_name(pipe), plane + 1,
11865 sw_plane_wm->trans_wm.plane_en,
11866 sw_plane_wm->trans_wm.plane_res_b,
11867 sw_plane_wm->trans_wm.plane_res_l,
11868 hw_plane_wm->trans_wm.plane_en,
11869 hw_plane_wm->trans_wm.plane_res_b,
11870 hw_plane_wm->trans_wm.plane_res_l);
11871 }
11872
11873 /* DDB */
11874 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11875 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11876
11877 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011878 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 -040011879 pipe_name(pipe), plane + 1,
11880 sw_ddb_entry->start, sw_ddb_entry->end,
11881 hw_ddb_entry->start, hw_ddb_entry->end);
11882 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011883 }
11884
Lyude27082492016-08-24 07:48:10 +020011885 /*
11886 * cursor
11887 * If the cursor plane isn't active, we may not have updated it's ddb
11888 * allocation. In that case since the ddb allocation will be updated
11889 * once the plane becomes visible, we can skip this check
11890 */
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030011891 if (1) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011892 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11893 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011894
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011895 /* Watermarks */
11896 for (level = 0; level <= max_level; level++) {
11897 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11898 &sw_plane_wm->wm[level]))
11899 continue;
11900
11901 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",
11902 pipe_name(pipe), level,
11903 sw_plane_wm->wm[level].plane_en,
11904 sw_plane_wm->wm[level].plane_res_b,
11905 sw_plane_wm->wm[level].plane_res_l,
11906 hw_plane_wm->wm[level].plane_en,
11907 hw_plane_wm->wm[level].plane_res_b,
11908 hw_plane_wm->wm[level].plane_res_l);
11909 }
11910
11911 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11912 &sw_plane_wm->trans_wm)) {
11913 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11914 pipe_name(pipe),
11915 sw_plane_wm->trans_wm.plane_en,
11916 sw_plane_wm->trans_wm.plane_res_b,
11917 sw_plane_wm->trans_wm.plane_res_l,
11918 hw_plane_wm->trans_wm.plane_en,
11919 hw_plane_wm->trans_wm.plane_res_b,
11920 hw_plane_wm->trans_wm.plane_res_l);
11921 }
11922
11923 /* DDB */
11924 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11925 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
11926
11927 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011928 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020011929 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011930 sw_ddb_entry->start, sw_ddb_entry->end,
11931 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020011932 }
Damien Lespiau08db6652014-11-04 17:06:52 +000011933 }
11934}
11935
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011936static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011937verify_connector_state(struct drm_device *dev,
11938 struct drm_atomic_state *state,
11939 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011940{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011941 struct drm_connector *connector;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011942 struct drm_connector_state *new_conn_state;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011943 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011944
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011945 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011946 struct drm_encoder *encoder = connector->encoder;
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011947 struct drm_crtc_state *crtc_state = NULL;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011948
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011949 if (new_conn_state->crtc != crtc)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011950 continue;
11951
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011952 if (crtc)
11953 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
11954
11955 intel_connector_verify_state(crtc_state, new_conn_state);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011956
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011957 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011958 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011959 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011960}
11961
11962static void
Daniel Vetter86b04262017-03-01 10:52:26 +010011963verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011964{
11965 struct intel_encoder *encoder;
Daniel Vetter86b04262017-03-01 10:52:26 +010011966 struct drm_connector *connector;
11967 struct drm_connector_state *old_conn_state, *new_conn_state;
11968 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011969
Damien Lespiaub2784e12014-08-05 11:29:37 +010011970 for_each_intel_encoder(dev, encoder) {
Daniel Vetter86b04262017-03-01 10:52:26 +010011971 bool enabled = false, found = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011972 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011973
11974 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11975 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011976 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011977
Daniel Vetter86b04262017-03-01 10:52:26 +010011978 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
11979 new_conn_state, i) {
11980 if (old_conn_state->best_encoder == &encoder->base)
11981 found = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011982
Daniel Vetter86b04262017-03-01 10:52:26 +010011983 if (new_conn_state->best_encoder != &encoder->base)
11984 continue;
11985 found = enabled = true;
11986
11987 I915_STATE_WARN(new_conn_state->crtc !=
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011988 encoder->base.crtc,
11989 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011990 }
Daniel Vetter86b04262017-03-01 10:52:26 +010011991
11992 if (!found)
11993 continue;
Dave Airlie0e32b392014-05-02 14:02:48 +100011994
Rob Clarke2c719b2014-12-15 13:56:32 -050011995 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011996 "encoder's enabled state mismatch "
11997 "(expected %i, found %i)\n",
11998 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011999
12000 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012001 bool active;
12002
12003 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012004 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012005 "encoder detached but still enabled on pipe %c.\n",
12006 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012007 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012008 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012009}
12010
12011static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012012verify_crtc_state(struct drm_crtc *crtc,
12013 struct drm_crtc_state *old_crtc_state,
12014 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012015{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012016 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012017 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012018 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12020 struct intel_crtc_state *pipe_config, *sw_config;
12021 struct drm_atomic_state *old_state;
12022 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012023
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012024 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020012025 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012026 pipe_config = to_intel_crtc_state(old_crtc_state);
12027 memset(pipe_config, 0, sizeof(*pipe_config));
12028 pipe_config->base.crtc = crtc;
12029 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012030
Ville Syrjälä78108b72016-05-27 20:59:19 +030012031 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012032
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012033 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012034
Ville Syrjäläe56134b2017-06-01 17:36:19 +030012035 /* we keep both pipes enabled on 830 */
12036 if (IS_I830(dev_priv))
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012037 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012038
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012039 I915_STATE_WARN(new_crtc_state->active != active,
12040 "crtc active state doesn't match with hw state "
12041 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012042
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012043 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12044 "transitional active state does not match atomic hw state "
12045 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012046
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012047 for_each_encoder_on_crtc(dev, crtc, encoder) {
12048 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012049
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012050 active = encoder->get_hw_state(encoder, &pipe);
12051 I915_STATE_WARN(active != new_crtc_state->active,
12052 "[ENCODER:%i] active %i with crtc active %i\n",
12053 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012054
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012055 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12056 "Encoder connected to wrong pipe %c\n",
12057 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012058
Ville Syrjäläe1214b92017-10-27 22:31:23 +030012059 if (active)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012060 encoder->get_config(encoder, pipe_config);
12061 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012062
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020012063 intel_crtc_compute_pixel_rate(pipe_config);
12064
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012065 if (!new_crtc_state->active)
12066 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012067
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012068 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012069
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020012070 sw_config = to_intel_crtc_state(new_crtc_state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012071 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012072 pipe_config, false)) {
12073 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12074 intel_dump_pipe_config(intel_crtc, pipe_config,
12075 "[hw state]");
12076 intel_dump_pipe_config(intel_crtc, sw_config,
12077 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012078 }
12079}
12080
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012081static void
Ville Syrjäläcff109f2017-11-17 21:19:17 +020012082intel_verify_planes(struct intel_atomic_state *state)
12083{
12084 struct intel_plane *plane;
12085 const struct intel_plane_state *plane_state;
12086 int i;
12087
12088 for_each_new_intel_plane_in_state(state, plane,
12089 plane_state, i)
12090 assert_plane(plane, plane_state->base.visible);
12091}
12092
12093static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012094verify_single_dpll_state(struct drm_i915_private *dev_priv,
12095 struct intel_shared_dpll *pll,
12096 struct drm_crtc *crtc,
12097 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012098{
12099 struct intel_dpll_hw_state dpll_hw_state;
Ville Syrjälä40560e22018-06-26 22:47:11 +030012100 unsigned int crtc_mask;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012101 bool active;
12102
12103 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12104
Lucas De Marchi72f775f2018-03-20 15:06:34 -070012105 DRM_DEBUG_KMS("%s\n", pll->info->name);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012106
Lucas De Marchiee1398b2018-03-20 15:06:33 -070012107 active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012108
Lucas De Marchi5cd281f2018-03-20 15:06:36 -070012109 if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012110 I915_STATE_WARN(!pll->on && pll->active_mask,
12111 "pll in active use but not on in sw tracking\n");
12112 I915_STATE_WARN(pll->on && !pll->active_mask,
12113 "pll is on but not used by any active crtc\n");
12114 I915_STATE_WARN(pll->on != active,
12115 "pll on state mismatch (expected %i, found %i)\n",
12116 pll->on, active);
12117 }
12118
12119 if (!crtc) {
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012120 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012121 "more active pll users than references: %x vs %x\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012122 pll->active_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012123
12124 return;
12125 }
12126
Ville Syrjälä40560e22018-06-26 22:47:11 +030012127 crtc_mask = drm_crtc_mask(crtc);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012128
12129 if (new_state->active)
12130 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12131 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12132 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12133 else
12134 I915_STATE_WARN(pll->active_mask & crtc_mask,
12135 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12136 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12137
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012138 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012139 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012140 crtc_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012141
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012142 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012143 &dpll_hw_state,
12144 sizeof(dpll_hw_state)),
12145 "pll hw state mismatch\n");
12146}
12147
12148static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012149verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12150 struct drm_crtc_state *old_crtc_state,
12151 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012152{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012153 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012154 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12155 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12156
12157 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012158 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012159
12160 if (old_state->shared_dpll &&
12161 old_state->shared_dpll != new_state->shared_dpll) {
Ville Syrjälä40560e22018-06-26 22:47:11 +030012162 unsigned int crtc_mask = drm_crtc_mask(crtc);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012163 struct intel_shared_dpll *pll = old_state->shared_dpll;
12164
12165 I915_STATE_WARN(pll->active_mask & crtc_mask,
12166 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12167 pipe_name(drm_crtc_index(crtc)));
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012168 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012169 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12170 pipe_name(drm_crtc_index(crtc)));
12171 }
12172}
12173
12174static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012175intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012176 struct drm_atomic_state *state,
12177 struct drm_crtc_state *old_state,
12178 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012179{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012180 if (!needs_modeset(new_state) &&
12181 !to_intel_crtc_state(new_state)->update_pipe)
12182 return;
12183
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012184 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012185 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012186 verify_crtc_state(crtc, old_state, new_state);
12187 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012188}
12189
12190static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012191verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012192{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012193 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012194 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012195
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012196 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012197 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012198}
Daniel Vetter53589012013-06-05 13:34:16 +020012199
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012200static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012201intel_modeset_verify_disabled(struct drm_device *dev,
12202 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012203{
Daniel Vetter86b04262017-03-01 10:52:26 +010012204 verify_encoder_state(dev, state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012205 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012206 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020012207}
12208
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012209static void update_scanline_offset(const struct intel_crtc_state *crtc_state)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012210{
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012211 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012212 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012213
12214 /*
12215 * The scanline counter increments at the leading edge of hsync.
12216 *
12217 * On most platforms it starts counting from vtotal-1 on the
12218 * first active line. That means the scanline counter value is
12219 * always one less than what we would expect. Ie. just after
12220 * start of vblank, which also occurs at start of hsync (on the
12221 * last active line), the scanline counter will read vblank_start-1.
12222 *
12223 * On gen2 the scanline counter starts counting from 1 instead
12224 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12225 * to keep the value positive), instead of adding one.
12226 *
12227 * On HSW+ the behaviour of the scanline counter depends on the output
12228 * type. For DP ports it behaves like most other platforms, but on HDMI
12229 * there's an extra 1 line difference. So we need to add two instead of
12230 * one to the value.
Ville Syrjäläec1b4ee2016-12-15 19:47:34 +020012231 *
12232 * On VLV/CHV DSI the scanline counter would appear to increment
12233 * approx. 1/3 of a scanline before start of vblank. Unfortunately
12234 * that means we can't tell whether we're in vblank or not while
12235 * we're on that particular line. We must still set scanline_offset
12236 * to 1 so that the vblank timestamps come out correct when we query
12237 * the scanline counter from within the vblank interrupt handler.
12238 * However if queried just before the start of vblank we'll get an
12239 * answer that's slightly in the future.
Ville Syrjälä80715b22014-05-15 20:23:23 +030012240 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012241 if (IS_GEN2(dev_priv)) {
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012242 const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012243 int vtotal;
12244
Ville Syrjälä124abe02015-09-08 13:40:45 +030012245 vtotal = adjusted_mode->crtc_vtotal;
12246 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012247 vtotal /= 2;
12248
12249 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012250 } else if (HAS_DDI(dev_priv) &&
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012251 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012252 crtc->scanline_offset = 2;
12253 } else
12254 crtc->scanline_offset = 1;
12255}
12256
Maarten Lankhorstad421372015-06-15 12:33:42 +020012257static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012258{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012259 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012260 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012261 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012262 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012263 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012264
12265 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012266 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012267
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012268 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012270 struct intel_shared_dpll *old_dpll =
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012271 to_intel_crtc_state(old_crtc_state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012272
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012273 if (!needs_modeset(new_crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012274 continue;
12275
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012276 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012277
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012278 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012279 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012280
Ander Conselvan de Oliveiraa1c414e2016-12-29 17:22:07 +020012281 intel_release_shared_dpll(old_dpll, intel_crtc, state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012282 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012283}
12284
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012285/*
12286 * This implements the workaround described in the "notes" section of the mode
12287 * set sequence documentation. When going from no pipes or single pipe to
12288 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12289 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12290 */
12291static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12292{
12293 struct drm_crtc_state *crtc_state;
12294 struct intel_crtc *intel_crtc;
12295 struct drm_crtc *crtc;
12296 struct intel_crtc_state *first_crtc_state = NULL;
12297 struct intel_crtc_state *other_crtc_state = NULL;
12298 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12299 int i;
12300
12301 /* look at all crtc's that are going to be enabled in during modeset */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012302 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012303 intel_crtc = to_intel_crtc(crtc);
12304
12305 if (!crtc_state->active || !needs_modeset(crtc_state))
12306 continue;
12307
12308 if (first_crtc_state) {
12309 other_crtc_state = to_intel_crtc_state(crtc_state);
12310 break;
12311 } else {
12312 first_crtc_state = to_intel_crtc_state(crtc_state);
12313 first_pipe = intel_crtc->pipe;
12314 }
12315 }
12316
12317 /* No workaround needed? */
12318 if (!first_crtc_state)
12319 return 0;
12320
12321 /* w/a possibly needed, check how many crtc's are already enabled. */
12322 for_each_intel_crtc(state->dev, intel_crtc) {
12323 struct intel_crtc_state *pipe_config;
12324
12325 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12326 if (IS_ERR(pipe_config))
12327 return PTR_ERR(pipe_config);
12328
12329 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12330
12331 if (!pipe_config->base.active ||
12332 needs_modeset(&pipe_config->base))
12333 continue;
12334
12335 /* 2 or more enabled crtcs means no need for w/a */
12336 if (enabled_pipe != INVALID_PIPE)
12337 return 0;
12338
12339 enabled_pipe = intel_crtc->pipe;
12340 }
12341
12342 if (enabled_pipe != INVALID_PIPE)
12343 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12344 else if (other_crtc_state)
12345 other_crtc_state->hsw_workaround_pipe = first_pipe;
12346
12347 return 0;
12348}
12349
Ville Syrjälä8d965612016-11-14 18:35:10 +020012350static int intel_lock_all_pipes(struct drm_atomic_state *state)
12351{
12352 struct drm_crtc *crtc;
12353
12354 /* Add all pipes to the state */
12355 for_each_crtc(state->dev, crtc) {
12356 struct drm_crtc_state *crtc_state;
12357
12358 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12359 if (IS_ERR(crtc_state))
12360 return PTR_ERR(crtc_state);
12361 }
12362
12363 return 0;
12364}
12365
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012366static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12367{
12368 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012369
Ville Syrjälä8d965612016-11-14 18:35:10 +020012370 /*
12371 * Add all pipes to the state, and force
12372 * a modeset on all the active ones.
12373 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012374 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012375 struct drm_crtc_state *crtc_state;
12376 int ret;
12377
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012378 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12379 if (IS_ERR(crtc_state))
12380 return PTR_ERR(crtc_state);
12381
12382 if (!crtc_state->active || needs_modeset(crtc_state))
12383 continue;
12384
12385 crtc_state->mode_changed = true;
12386
12387 ret = drm_atomic_add_affected_connectors(state, crtc);
12388 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012389 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012390
12391 ret = drm_atomic_add_affected_planes(state, crtc);
12392 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012393 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012394 }
12395
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012396 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012397}
12398
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012399static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012400{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012401 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012402 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012403 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012404 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012405 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012406
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012407 if (!check_digital_port_conflicts(state)) {
12408 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12409 return -EINVAL;
12410 }
12411
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012412 intel_state->modeset = true;
12413 intel_state->active_crtcs = dev_priv->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012414 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12415 intel_state->cdclk.actual = dev_priv->cdclk.actual;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012416
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012417 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12418 if (new_crtc_state->active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012419 intel_state->active_crtcs |= 1 << i;
12420 else
12421 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070012422
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012423 if (old_crtc_state->active != new_crtc_state->active)
Matt Roper8b4a7d02016-05-12 07:06:00 -070012424 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012425 }
12426
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012427 /*
12428 * See if the config requires any additional preparation, e.g.
12429 * to adjust global state with pipes off. We need to do this
12430 * here so we can get the modeset_pipe updated config for the new
12431 * mode set on this crtc. For other crtcs we need to use the
12432 * adjusted_mode bits in the crtc directly.
12433 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012434 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030012435 ret = dev_priv->display.modeset_calc_cdclk(state);
12436 if (ret < 0)
12437 return ret;
12438
Ville Syrjälä8d965612016-11-14 18:35:10 +020012439 /*
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012440 * Writes to dev_priv->cdclk.logical must protected by
Ville Syrjälä8d965612016-11-14 18:35:10 +020012441 * holding all the crtc locks, even if we don't end up
12442 * touching the hardware
12443 */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012444 if (intel_cdclk_changed(&dev_priv->cdclk.logical,
12445 &intel_state->cdclk.logical)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012446 ret = intel_lock_all_pipes(state);
12447 if (ret < 0)
12448 return ret;
12449 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012450
Ville Syrjälä8d965612016-11-14 18:35:10 +020012451 /* All pipes must be switched off while we change the cdclk. */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012452 if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
12453 &intel_state->cdclk.actual)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012454 ret = intel_modeset_all_pipes(state);
12455 if (ret < 0)
12456 return ret;
12457 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010012458
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012459 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12460 intel_state->cdclk.logical.cdclk,
12461 intel_state->cdclk.actual.cdclk);
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030012462 DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
12463 intel_state->cdclk.logical.voltage_level,
12464 intel_state->cdclk.actual.voltage_level);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012465 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012466 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012467 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012468
Maarten Lankhorstad421372015-06-15 12:33:42 +020012469 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012470
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012471 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012472 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012473
Maarten Lankhorstad421372015-06-15 12:33:42 +020012474 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012475}
12476
Matt Roperaa363132015-09-24 15:53:18 -070012477/*
12478 * Handle calculation of various watermark data at the end of the atomic check
12479 * phase. The code here should be run after the per-crtc and per-plane 'check'
12480 * handlers to ensure that all derived state has been updated.
12481 */
Matt Roper55994c22016-05-12 07:06:08 -070012482static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070012483{
12484 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070012485 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070012486
12487 /* Is there platform-specific watermark information to calculate? */
12488 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070012489 return dev_priv->display.compute_global_watermarks(state);
12490
12491 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070012492}
12493
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012494/**
12495 * intel_atomic_check - validate state object
12496 * @dev: drm device
12497 * @state: state to validate
12498 */
12499static int intel_atomic_check(struct drm_device *dev,
12500 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012501{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012502 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070012503 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012504 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012505 struct drm_crtc_state *old_crtc_state, *crtc_state;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012506 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012507 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012508
Maarten Lankhorst8c58f732018-02-21 10:28:08 +010012509 /* Catch I915_MODE_FLAG_INHERITED */
12510 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
12511 crtc_state, i) {
12512 if (crtc_state->mode.private_flags !=
12513 old_crtc_state->mode.private_flags)
12514 crtc_state->mode_changed = true;
12515 }
12516
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012517 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012518 if (ret)
12519 return ret;
12520
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012521 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012522 struct intel_crtc_state *pipe_config =
12523 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012524
Daniel Vetter26495482015-07-15 14:15:52 +020012525 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012526 continue;
12527
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012528 if (!crtc_state->enable) {
12529 any_ms = true;
12530 continue;
12531 }
12532
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012533 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012534 if (ret) {
12535 intel_dump_pipe_config(to_intel_crtc(crtc),
12536 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012537 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012538 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012539
Michal Wajdeczko4f044a82017-09-19 19:38:44 +000012540 if (i915_modparams.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012541 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012542 to_intel_crtc_state(old_crtc_state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012543 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012544 crtc_state->mode_changed = false;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012545 pipe_config->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020012546 }
12547
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012548 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020012549 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012550
Daniel Vetter26495482015-07-15 14:15:52 +020012551 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12552 needs_modeset(crtc_state) ?
12553 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012554 }
12555
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012556 if (any_ms) {
12557 ret = intel_modeset_checks(state);
12558
12559 if (ret)
12560 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012561 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012562 intel_state->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012563 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012564
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +020012565 ret = icl_add_linked_planes(intel_state);
12566 if (ret)
12567 return ret;
12568
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012569 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070012570 if (ret)
12571 return ret;
12572
Ville Syrjälädd576022017-11-17 21:19:14 +020012573 intel_fbc_choose_crtc(dev_priv, intel_state);
Matt Roper55994c22016-05-12 07:06:08 -070012574 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012575}
12576
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012577static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010012578 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012579{
Chris Wilsonfd700752017-07-26 17:00:36 +010012580 return drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012581}
12582
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012583u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12584{
12585 struct drm_device *dev = crtc->base.dev;
12586
12587 if (!dev->max_vblank_count)
Dhinakaran Pandiyan734cbbf2018-02-02 21:12:54 -080012588 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012589
12590 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12591}
12592
Lyude896e5bb2016-08-24 07:48:09 +020012593static void intel_update_crtc(struct drm_crtc *crtc,
12594 struct drm_atomic_state *state,
12595 struct drm_crtc_state *old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012596 struct drm_crtc_state *new_crtc_state)
Lyude896e5bb2016-08-24 07:48:09 +020012597{
12598 struct drm_device *dev = crtc->dev;
12599 struct drm_i915_private *dev_priv = to_i915(dev);
12600 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst6c246b82018-09-20 12:27:08 +020012601 struct intel_crtc_state *old_intel_cstate = to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012602 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12603 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012604 struct intel_plane_state *new_plane_state =
12605 intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
12606 to_intel_plane(crtc->primary));
Lyude896e5bb2016-08-24 07:48:09 +020012607
12608 if (modeset) {
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012609 update_scanline_offset(pipe_config);
Lyude896e5bb2016-08-24 07:48:09 +020012610 dev_priv->display.crtc_enable(pipe_config, state);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012611
12612 /* vblanks work again, re-enable pipe CRC. */
12613 intel_crtc_enable_pipe_crc(intel_crtc);
Lyude896e5bb2016-08-24 07:48:09 +020012614 } else {
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012615 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12616 pipe_config);
Lyude896e5bb2016-08-24 07:48:09 +020012617 }
12618
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012619 if (new_plane_state)
12620 intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
Lyude896e5bb2016-08-24 07:48:09 +020012621
Maarten Lankhorst6c246b82018-09-20 12:27:08 +020012622 intel_begin_crtc_commit(crtc, old_crtc_state);
12623
12624 intel_update_planes_on_crtc(to_intel_atomic_state(state), intel_crtc,
12625 old_intel_cstate, pipe_config);
12626
12627 intel_finish_crtc_commit(crtc, old_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012628}
12629
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012630static void intel_update_crtcs(struct drm_atomic_state *state)
Lyude896e5bb2016-08-24 07:48:09 +020012631{
12632 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012633 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyude896e5bb2016-08-24 07:48:09 +020012634 int i;
12635
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012636 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12637 if (!new_crtc_state->active)
Lyude896e5bb2016-08-24 07:48:09 +020012638 continue;
12639
12640 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012641 new_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012642 }
12643}
12644
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012645static void skl_update_crtcs(struct drm_atomic_state *state)
Lyude27082492016-08-24 07:48:10 +020012646{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012647 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020012648 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12649 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040012650 struct intel_crtc *intel_crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012651 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040012652 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020012653 unsigned int updated = 0;
12654 bool progress;
12655 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012656 int i;
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012657 u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
12658 u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012659
12660 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12661
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012662 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012663 /* ignore allocations for crtc's that have been turned off. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012664 if (new_crtc_state->active)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012665 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012666
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012667 /* If 2nd DBuf slice required, enable it here */
12668 if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
12669 icl_dbuf_slices_update(dev_priv, required_slices);
12670
Lyude27082492016-08-24 07:48:10 +020012671 /*
12672 * Whenever the number of active pipes changes, we need to make sure we
12673 * update the pipes in the right order so that their ddb allocations
12674 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12675 * cause pipe underruns and other bad stuff.
12676 */
12677 do {
Lyude27082492016-08-24 07:48:10 +020012678 progress = false;
12679
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012680 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Lyude27082492016-08-24 07:48:10 +020012681 bool vbl_wait = false;
12682 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040012683
12684 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä21794812017-08-23 18:22:26 +030012685 cstate = to_intel_crtc_state(new_crtc_state);
Lyudece0ba282016-09-15 10:46:35 -040012686 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020012687
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012688 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020012689 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012690
Mika Kahola2b685042017-10-10 13:17:03 +030012691 if (skl_ddb_allocation_overlaps(dev_priv,
12692 entries,
12693 &cstate->wm.skl.ddb,
12694 i))
Lyude27082492016-08-24 07:48:10 +020012695 continue;
12696
12697 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012698 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012699
12700 /*
12701 * If this is an already active pipe, it's DDB changed,
12702 * and this isn't the last pipe that needs updating
12703 * then we need to wait for a vblank to pass for the
12704 * new ddb allocation to take effect.
12705 */
Lyudece0ba282016-09-15 10:46:35 -040012706 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010012707 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012708 !new_crtc_state->active_changed &&
Lyude27082492016-08-24 07:48:10 +020012709 intel_state->wm_results.dirty_pipes != updated)
12710 vbl_wait = true;
12711
12712 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012713 new_crtc_state);
Lyude27082492016-08-24 07:48:10 +020012714
12715 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012716 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020012717
12718 progress = true;
12719 }
12720 } while (progress);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012721
12722 /* If 2nd DBuf slice is no more required disable it */
12723 if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
12724 icl_dbuf_slices_update(dev_priv, required_slices);
Lyude27082492016-08-24 07:48:10 +020012725}
12726
Chris Wilsonba318c62017-02-02 20:47:41 +000012727static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12728{
12729 struct intel_atomic_state *state, *next;
12730 struct llist_node *freed;
12731
12732 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12733 llist_for_each_entry_safe(state, next, freed, freed)
12734 drm_atomic_state_put(&state->base);
12735}
12736
12737static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12738{
12739 struct drm_i915_private *dev_priv =
12740 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12741
12742 intel_atomic_helper_free_state(dev_priv);
12743}
12744
Daniel Vetter9db529a2017-08-08 10:08:28 +020012745static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
12746{
12747 struct wait_queue_entry wait_fence, wait_reset;
12748 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
12749
12750 init_wait_entry(&wait_fence, 0);
12751 init_wait_entry(&wait_reset, 0);
12752 for (;;) {
12753 prepare_to_wait(&intel_state->commit_ready.wait,
12754 &wait_fence, TASK_UNINTERRUPTIBLE);
12755 prepare_to_wait(&dev_priv->gpu_error.wait_queue,
12756 &wait_reset, TASK_UNINTERRUPTIBLE);
12757
12758
12759 if (i915_sw_fence_done(&intel_state->commit_ready)
12760 || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
12761 break;
12762
12763 schedule();
12764 }
12765 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
12766 finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
12767}
12768
Chris Wilson8d52e442018-06-23 11:39:51 +010012769static void intel_atomic_cleanup_work(struct work_struct *work)
12770{
12771 struct drm_atomic_state *state =
12772 container_of(work, struct drm_atomic_state, commit_work);
12773 struct drm_i915_private *i915 = to_i915(state->dev);
12774
12775 drm_atomic_helper_cleanup_planes(&i915->drm, state);
12776 drm_atomic_helper_commit_cleanup_done(state);
12777 drm_atomic_state_put(state);
12778
12779 intel_atomic_helper_free_state(i915);
12780}
12781
Daniel Vetter94f05022016-06-14 18:01:00 +020012782static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012783{
Daniel Vetter94f05022016-06-14 18:01:00 +020012784 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012785 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012786 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012787 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012788 struct intel_crtc_state *new_intel_crtc_state, *old_intel_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012789 struct drm_crtc *crtc;
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012790 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020012791 u64 put_domains[I915_MAX_PIPES] = {};
Chris Wilsone95433c2016-10-28 13:58:27 +010012792 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012793
Daniel Vetter9db529a2017-08-08 10:08:28 +020012794 intel_atomic_commit_fence_wait(intel_state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020012795
Daniel Vetterea0000f2016-06-13 16:13:46 +020012796 drm_atomic_helper_wait_for_dependencies(state);
12797
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012798 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012799 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012800
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012801 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012802 old_intel_crtc_state = to_intel_crtc_state(old_crtc_state);
12803 new_intel_crtc_state = to_intel_crtc_state(new_crtc_state);
12804 intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012805
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012806 if (needs_modeset(new_crtc_state) ||
12807 to_intel_crtc_state(new_crtc_state)->update_pipe) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012808
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012809 put_domains[intel_crtc->pipe] =
Daniel Vetter5a21b662016-05-24 17:13:53 +020012810 modeset_get_crtc_power_domains(crtc,
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012811 new_intel_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012812 }
12813
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012814 if (!needs_modeset(new_crtc_state))
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012815 continue;
12816
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012817 intel_pre_plane_update(old_intel_crtc_state, new_intel_crtc_state);
Daniel Vetter460da9162013-03-27 00:44:51 +010012818
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012819 if (old_crtc_state->active) {
Maarten Lankhorstf59e9702018-09-20 12:27:07 +020012820 intel_crtc_disable_planes(intel_crtc, old_intel_crtc_state->active_planes);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012821
12822 /*
12823 * We need to disable pipe CRC before disabling the pipe,
12824 * or we race against vblank off.
12825 */
12826 intel_crtc_disable_pipe_crc(intel_crtc);
12827
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012828 dev_priv->display.crtc_disable(old_intel_crtc_state, state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012829 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020012830 intel_fbc_disable(intel_crtc);
Maarten Lankhorst65c307f2018-10-05 11:52:44 +020012831 intel_disable_shared_dpll(old_intel_crtc_state);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020012832
12833 /*
12834 * Underruns don't always raise
12835 * interrupts, so check manually.
12836 */
12837 intel_check_cpu_fifo_underruns(dev_priv);
12838 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010012839
Ville Syrjäläa748fae2018-10-25 16:05:36 +030012840 /* FIXME unify this for all platforms */
12841 if (!new_crtc_state->active &&
12842 !HAS_GMCH_DISPLAY(dev_priv) &&
12843 dev_priv->display.initial_watermarks)
12844 dev_priv->display.initial_watermarks(intel_state,
12845 new_intel_crtc_state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012846 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012847 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012848
Daniel Vetter7a1530d72017-12-07 15:32:02 +010012849 /* FIXME: Eventually get rid of our intel_crtc->config pointer */
12850 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
12851 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012852
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012853 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012854 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010012855
Ville Syrjäläb0587e42017-01-26 21:52:01 +020012856 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010012857
Lyude656d1b82016-08-17 15:55:54 -040012858 /*
12859 * SKL workaround: bspec recommends we disable the SAGV when we
12860 * have more then one pipe enabled
12861 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030012862 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012863 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012864
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012865 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012866 }
Daniel Vetter47fab732012-10-26 10:58:18 +020012867
Lyude896e5bb2016-08-24 07:48:09 +020012868 /* Complete the events for pipes that have now been disabled */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012869 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12870 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012871
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012872 /* Complete events for now disable pipes here. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012873 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012874 spin_lock_irq(&dev->event_lock);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012875 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012876 spin_unlock_irq(&dev->event_lock);
12877
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012878 new_crtc_state->event = NULL;
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012879 }
Matt Ropered4a6a72016-02-23 17:20:13 -080012880 }
12881
Lyude896e5bb2016-08-24 07:48:09 +020012882 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012883 dev_priv->display.update_crtcs(state);
Lyude896e5bb2016-08-24 07:48:09 +020012884
Daniel Vetter94f05022016-06-14 18:01:00 +020012885 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12886 * already, but still need the state for the delayed optimization. To
12887 * fix this:
12888 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12889 * - schedule that vblank worker _before_ calling hw_done
12890 * - at the start of commit_tail, cancel it _synchrously
12891 * - switch over to the vblank wait helper in the core after that since
12892 * we don't need out special handling any more.
12893 */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012894 drm_atomic_helper_wait_for_flip_done(dev, state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012895
12896 /*
12897 * Now that the vblank has passed, we can go ahead and program the
12898 * optimal watermarks on platforms that need two-step watermark
12899 * programming.
12900 *
12901 * TODO: Move this (and other cleanup) to an async worker eventually.
12902 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012903 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012904 new_intel_crtc_state = to_intel_crtc_state(new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012905
12906 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010012907 dev_priv->display.optimize_watermarks(intel_state,
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012908 new_intel_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012909 }
12910
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012911 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012912 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12913
12914 if (put_domains[i])
12915 modeset_put_power_domains(dev_priv, put_domains[i]);
12916
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012917 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012918 }
12919
Ville Syrjäläcff109f2017-11-17 21:19:17 +020012920 if (intel_state->modeset)
12921 intel_verify_planes(intel_state);
12922
Paulo Zanoni56feca92016-09-22 18:00:28 -030012923 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012924 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012925
Daniel Vetter94f05022016-06-14 18:01:00 +020012926 drm_atomic_helper_commit_hw_done(state);
12927
Chris Wilsond5553c02017-05-04 12:55:08 +010012928 if (intel_state->modeset) {
12929 /* As one of the primary mmio accessors, KMS has a high
12930 * likelihood of triggering bugs in unclaimed access. After we
12931 * finish modesetting, see if an error has been flagged, and if
12932 * so enable debugging for the next modeset - and hope we catch
12933 * the culprit.
12934 */
12935 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012936 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
Chris Wilsond5553c02017-05-04 12:55:08 +010012937 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020012938
Chris Wilson8d52e442018-06-23 11:39:51 +010012939 /*
12940 * Defer the cleanup of the old state to a separate worker to not
12941 * impede the current task (userspace for blocking modesets) that
12942 * are executed inline. For out-of-line asynchronous modesets/flips,
12943 * deferring to a new worker seems overkill, but we would place a
12944 * schedule point (cond_resched()) here anyway to keep latencies
12945 * down.
12946 */
12947 INIT_WORK(&state->commit_work, intel_atomic_cleanup_work);
Chris Wilson41db6452018-07-12 12:57:29 +010012948 queue_work(system_highpri_wq, &state->commit_work);
Daniel Vetter94f05022016-06-14 18:01:00 +020012949}
12950
12951static void intel_atomic_commit_work(struct work_struct *work)
12952{
Chris Wilsonc004a902016-10-28 13:58:45 +010012953 struct drm_atomic_state *state =
12954 container_of(work, struct drm_atomic_state, commit_work);
12955
Daniel Vetter94f05022016-06-14 18:01:00 +020012956 intel_atomic_commit_tail(state);
12957}
12958
Chris Wilsonc004a902016-10-28 13:58:45 +010012959static int __i915_sw_fence_call
12960intel_atomic_commit_ready(struct i915_sw_fence *fence,
12961 enum i915_sw_fence_notify notify)
12962{
12963 struct intel_atomic_state *state =
12964 container_of(fence, struct intel_atomic_state, commit_ready);
12965
12966 switch (notify) {
12967 case FENCE_COMPLETE:
Daniel Vetter42b062b2017-08-08 10:08:27 +020012968 /* we do blocking waits in the worker, nothing to do here */
Chris Wilsonc004a902016-10-28 13:58:45 +010012969 break;
Chris Wilsonc004a902016-10-28 13:58:45 +010012970 case FENCE_FREE:
Chris Wilsoneb955ee2017-01-23 21:29:39 +000012971 {
12972 struct intel_atomic_helper *helper =
12973 &to_i915(state->base.dev)->atomic_helper;
12974
12975 if (llist_add(&state->freed, &helper->free_list))
12976 schedule_work(&helper->free_work);
12977 break;
12978 }
Chris Wilsonc004a902016-10-28 13:58:45 +010012979 }
12980
12981 return NOTIFY_DONE;
12982}
12983
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012984static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12985{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012986 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012987 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012988 int i;
12989
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012990 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012991 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012992 intel_fb_obj(new_plane_state->fb),
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012993 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012994}
12995
Daniel Vetter94f05022016-06-14 18:01:00 +020012996/**
12997 * intel_atomic_commit - commit validated state object
12998 * @dev: DRM device
12999 * @state: the top-level driver state object
13000 * @nonblock: nonblocking commit
13001 *
13002 * This function commits a top-level state object that has been validated
13003 * with drm_atomic_helper_check().
13004 *
Daniel Vetter94f05022016-06-14 18:01:00 +020013005 * RETURNS
13006 * Zero for success or -errno.
13007 */
13008static int intel_atomic_commit(struct drm_device *dev,
13009 struct drm_atomic_state *state,
13010 bool nonblock)
13011{
13012 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013013 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013014 int ret = 0;
13015
Chris Wilsonc004a902016-10-28 13:58:45 +010013016 drm_atomic_state_get(state);
13017 i915_sw_fence_init(&intel_state->commit_ready,
13018 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020013019
Ville Syrjälä440df932017-03-29 17:21:23 +030013020 /*
13021 * The intel_legacy_cursor_update() fast path takes care
13022 * of avoiding the vblank waits for simple cursor
13023 * movement and flips. For cursor on/off and size changes,
13024 * we want to perform the vblank waits so that watermark
13025 * updates happen during the correct frames. Gen9+ have
13026 * double buffered watermarks and so shouldn't need this.
13027 *
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020013028 * Unset state->legacy_cursor_update before the call to
13029 * drm_atomic_helper_setup_commit() because otherwise
13030 * drm_atomic_helper_wait_for_flip_done() is a noop and
13031 * we get FIFO underruns because we didn't wait
13032 * for vblank.
Ville Syrjälä440df932017-03-29 17:21:23 +030013033 *
13034 * FIXME doing watermarks and fb cleanup from a vblank worker
13035 * (assuming we had any) would solve these problems.
13036 */
Maarten Lankhorst213f1bd2017-09-19 14:14:19 +020013037 if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
13038 struct intel_crtc_state *new_crtc_state;
13039 struct intel_crtc *crtc;
13040 int i;
13041
13042 for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
13043 if (new_crtc_state->wm.need_postvbl_update ||
13044 new_crtc_state->update_wm_post)
13045 state->legacy_cursor_update = false;
13046 }
Ville Syrjälä440df932017-03-29 17:21:23 +030013047
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020013048 ret = intel_atomic_prepare_commit(dev, state);
13049 if (ret) {
13050 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13051 i915_sw_fence_commit(&intel_state->commit_ready);
13052 return ret;
13053 }
13054
13055 ret = drm_atomic_helper_setup_commit(state, nonblock);
13056 if (!ret)
13057 ret = drm_atomic_helper_swap_state(state, true);
13058
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020013059 if (ret) {
13060 i915_sw_fence_commit(&intel_state->commit_ready);
13061
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020013062 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020013063 return ret;
13064 }
Daniel Vetter94f05022016-06-14 18:01:00 +020013065 dev_priv->wm.distrust_bios_wm = false;
Ander Conselvan de Oliveira3c0fb582016-12-29 17:22:08 +020013066 intel_shared_dpll_swap_state(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013067 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013068
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013069 if (intel_state->modeset) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030013070 memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
13071 sizeof(intel_state->min_cdclk));
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030013072 memcpy(dev_priv->min_voltage_level,
13073 intel_state->min_voltage_level,
13074 sizeof(intel_state->min_voltage_level));
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013075 dev_priv->active_crtcs = intel_state->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020013076 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13077 dev_priv->cdclk.actual = intel_state->cdclk.actual;
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013078 }
13079
Chris Wilson08536952016-10-14 13:18:18 +010013080 drm_atomic_state_get(state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020013081 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
Chris Wilsonc004a902016-10-28 13:58:45 +010013082
13083 i915_sw_fence_commit(&intel_state->commit_ready);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020013084 if (nonblock && intel_state->modeset) {
13085 queue_work(dev_priv->modeset_wq, &state->commit_work);
13086 } else if (nonblock) {
Daniel Vetter42b062b2017-08-08 10:08:27 +020013087 queue_work(system_unbound_wq, &state->commit_work);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020013088 } else {
13089 if (intel_state->modeset)
13090 flush_workqueue(dev_priv->modeset_wq);
Daniel Vetter94f05022016-06-14 18:01:00 +020013091 intel_atomic_commit_tail(state);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020013092 }
Mika Kuoppala75714942015-12-16 09:26:48 +020013093
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013094 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013095}
13096
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013097static const struct drm_crtc_funcs intel_crtc_funcs = {
Daniel Vetter3fab2f02017-04-03 10:32:57 +020013098 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013099 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013100 .destroy = intel_crtc_destroy,
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010013101 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013102 .atomic_duplicate_state = intel_crtc_duplicate_state,
13103 .atomic_destroy_state = intel_crtc_destroy_state,
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +010013104 .set_crc_source = intel_crtc_set_crc_source,
Mahesh Kumara8c20832018-07-13 19:29:38 +053013105 .verify_crc_source = intel_crtc_verify_crc_source,
Mahesh Kumar260bc552018-07-13 19:29:39 +053013106 .get_crc_sources = intel_crtc_get_crc_sources,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013107};
13108
Chris Wilson74d290f2017-08-17 13:37:06 +010013109struct wait_rps_boost {
13110 struct wait_queue_entry wait;
13111
13112 struct drm_crtc *crtc;
Chris Wilsone61e0f52018-02-21 09:56:36 +000013113 struct i915_request *request;
Chris Wilson74d290f2017-08-17 13:37:06 +010013114};
13115
13116static int do_rps_boost(struct wait_queue_entry *_wait,
13117 unsigned mode, int sync, void *key)
13118{
13119 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
Chris Wilsone61e0f52018-02-21 09:56:36 +000013120 struct i915_request *rq = wait->request;
Chris Wilson74d290f2017-08-17 13:37:06 +010013121
Chris Wilsone9af4ea2018-01-18 13:16:09 +000013122 /*
13123 * If we missed the vblank, but the request is already running it
13124 * is reasonable to assume that it will complete before the next
13125 * vblank without our intervention, so leave RPS alone.
13126 */
Chris Wilsone61e0f52018-02-21 09:56:36 +000013127 if (!i915_request_started(rq))
Chris Wilsone9af4ea2018-01-18 13:16:09 +000013128 gen6_rps_boost(rq, NULL);
Chris Wilsone61e0f52018-02-21 09:56:36 +000013129 i915_request_put(rq);
Chris Wilson74d290f2017-08-17 13:37:06 +010013130
13131 drm_crtc_vblank_put(wait->crtc);
13132
13133 list_del(&wait->wait.entry);
13134 kfree(wait);
13135 return 1;
13136}
13137
13138static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
13139 struct dma_fence *fence)
13140{
13141 struct wait_rps_boost *wait;
13142
13143 if (!dma_fence_is_i915(fence))
13144 return;
13145
13146 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
13147 return;
13148
13149 if (drm_crtc_vblank_get(crtc))
13150 return;
13151
13152 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
13153 if (!wait) {
13154 drm_crtc_vblank_put(crtc);
13155 return;
13156 }
13157
13158 wait->request = to_request(dma_fence_get(fence));
13159 wait->crtc = crtc;
13160
13161 wait->wait.func = do_rps_boost;
13162 wait->wait.flags = 0;
13163
13164 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
13165}
13166
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013167static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
13168{
13169 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
13170 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
13171 struct drm_framebuffer *fb = plane_state->base.fb;
13172 struct i915_vma *vma;
13173
13174 if (plane->id == PLANE_CURSOR &&
13175 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13176 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13177 const int align = intel_cursor_alignment(dev_priv);
Chris Wilson4a477652018-08-17 09:24:05 +010013178 int err;
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013179
Chris Wilson4a477652018-08-17 09:24:05 +010013180 err = i915_gem_object_attach_phys(obj, align);
13181 if (err)
13182 return err;
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013183 }
13184
13185 vma = intel_pin_and_fence_fb_obj(fb,
Ville Syrjäläf5929c52018-09-07 18:24:06 +030013186 &plane_state->view,
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013187 intel_plane_uses_fence(plane_state),
13188 &plane_state->flags);
13189 if (IS_ERR(vma))
13190 return PTR_ERR(vma);
13191
13192 plane_state->vma = vma;
13193
13194 return 0;
13195}
13196
13197static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
13198{
13199 struct i915_vma *vma;
13200
13201 vma = fetch_and_zero(&old_plane_state->vma);
13202 if (vma)
13203 intel_unpin_fb_vma(vma, old_plane_state->flags);
13204}
13205
Chris Wilsonb7268c52018-04-18 19:40:52 +010013206static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
13207{
13208 struct i915_sched_attr attr = {
13209 .priority = I915_PRIORITY_DISPLAY,
13210 };
13211
13212 i915_gem_object_wait_priority(obj, 0, &attr);
13213}
13214
Matt Roper6beb8c232014-12-01 15:40:14 -080013215/**
13216 * intel_prepare_plane_fb - Prepare fb for usage on plane
13217 * @plane: drm plane to prepare for
Chris Wilsonc38c1452018-02-14 13:49:22 +000013218 * @new_state: the plane state being prepared
Matt Roper6beb8c232014-12-01 15:40:14 -080013219 *
13220 * Prepares a framebuffer for usage on a display plane. Generally this
13221 * involves pinning the underlying object and updating the frontbuffer tracking
13222 * bits. Some older platforms need special physical address handling for
13223 * cursor planes.
13224 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013225 * Must be called with struct_mutex held.
13226 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013227 * Returns 0 on success, negative error code on failure.
13228 */
13229int
13230intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013231 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013232{
Chris Wilsonc004a902016-10-28 13:58:45 +010013233 struct intel_atomic_state *intel_state =
13234 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000013235 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013236 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013237 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013238 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010013239 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013240
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013241 if (old_obj) {
13242 struct drm_crtc_state *crtc_state =
Maarten Lankhorst8b694492018-04-09 14:46:55 +020013243 drm_atomic_get_new_crtc_state(new_state->state,
13244 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013245
13246 /* Big Hammer, we also need to ensure that any pending
13247 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13248 * current scanout is retired before unpinning the old
13249 * framebuffer. Note that we rely on userspace rendering
13250 * into the buffer attached to the pipe they are waiting
13251 * on. If not, userspace generates a GPU hang with IPEHR
13252 * point to the MI_WAIT_FOR_EVENT.
13253 *
13254 * This should only fail upon a hung GPU, in which case we
13255 * can safely continue.
13256 */
Chris Wilsonc004a902016-10-28 13:58:45 +010013257 if (needs_modeset(crtc_state)) {
13258 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13259 old_obj->resv, NULL,
13260 false, 0,
13261 GFP_KERNEL);
13262 if (ret < 0)
13263 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013264 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013265 }
13266
Chris Wilsonc004a902016-10-28 13:58:45 +010013267 if (new_state->fence) { /* explicit fencing */
13268 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13269 new_state->fence,
13270 I915_FENCE_TIMEOUT,
13271 GFP_KERNEL);
13272 if (ret < 0)
13273 return ret;
13274 }
13275
Chris Wilsonc37efb92016-06-17 08:28:47 +010013276 if (!obj)
13277 return 0;
13278
Chris Wilson4d3088c2017-07-26 17:00:38 +010013279 ret = i915_gem_object_pin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010013280 if (ret)
13281 return ret;
13282
Chris Wilson4d3088c2017-07-26 17:00:38 +010013283 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13284 if (ret) {
13285 i915_gem_object_unpin_pages(obj);
13286 return ret;
13287 }
13288
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013289 ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
Chris Wilsonfd700752017-07-26 17:00:36 +010013290
Chris Wilsonfd700752017-07-26 17:00:36 +010013291 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson4d3088c2017-07-26 17:00:38 +010013292 i915_gem_object_unpin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010013293 if (ret)
13294 return ret;
13295
Chris Wilsone2f34962018-10-01 15:47:54 +010013296 fb_obj_bump_render_priority(obj);
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080013297 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
13298
Chris Wilsonc004a902016-10-28 13:58:45 +010013299 if (!new_state->fence) { /* implicit fencing */
Chris Wilson74d290f2017-08-17 13:37:06 +010013300 struct dma_fence *fence;
13301
Chris Wilsonc004a902016-10-28 13:58:45 +010013302 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13303 obj->resv, NULL,
13304 false, I915_FENCE_TIMEOUT,
13305 GFP_KERNEL);
13306 if (ret < 0)
13307 return ret;
Chris Wilson74d290f2017-08-17 13:37:06 +010013308
13309 fence = reservation_object_get_excl_rcu(obj->resv);
13310 if (fence) {
13311 add_rps_boost_after_vblank(new_state->crtc, fence);
13312 dma_fence_put(fence);
13313 }
13314 } else {
13315 add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
Chris Wilsonc004a902016-10-28 13:58:45 +010013316 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020013317
Chris Wilson60548c52018-07-31 14:26:29 +010013318 /*
13319 * We declare pageflips to be interactive and so merit a small bias
13320 * towards upclocking to deliver the frame on time. By only changing
13321 * the RPS thresholds to sample more regularly and aim for higher
13322 * clocks we can hopefully deliver low power workloads (like kodi)
13323 * that are not quite steady state without resorting to forcing
13324 * maximum clocks following a vblank miss (see do_rps_boost()).
13325 */
13326 if (!intel_state->rps_interactive) {
13327 intel_rps_mark_interactive(dev_priv, true);
13328 intel_state->rps_interactive = true;
13329 }
13330
Chris Wilsond07f0e52016-10-28 13:58:44 +010013331 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080013332}
13333
Matt Roper38f3ce32014-12-02 07:45:25 -080013334/**
13335 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13336 * @plane: drm plane to clean up for
Chris Wilsonc38c1452018-02-14 13:49:22 +000013337 * @old_state: the state from the previous modeset
Matt Roper38f3ce32014-12-02 07:45:25 -080013338 *
13339 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013340 *
13341 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013342 */
13343void
13344intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013345 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013346{
Chris Wilson60548c52018-07-31 14:26:29 +010013347 struct intel_atomic_state *intel_state =
13348 to_intel_atomic_state(old_state->state);
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013349 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper38f3ce32014-12-02 07:45:25 -080013350
Chris Wilson60548c52018-07-31 14:26:29 +010013351 if (intel_state->rps_interactive) {
13352 intel_rps_mark_interactive(dev_priv, false);
13353 intel_state->rps_interactive = false;
13354 }
13355
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013356 /* Should only be called after a successful intel_prepare_plane_fb()! */
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013357 mutex_lock(&dev_priv->drm.struct_mutex);
13358 intel_plane_unpin_fb(to_intel_plane_state(old_state));
13359 mutex_unlock(&dev_priv->drm.struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013360}
13361
Chandra Konduru6156a452015-04-27 13:48:39 -070013362int
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013363skl_max_scale(const struct intel_crtc_state *crtc_state,
13364 u32 pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -070013365{
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013366 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
13367 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru77224cd2018-04-09 09:11:13 +053013368 int max_scale, mult;
13369 int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
Chandra Konduru6156a452015-04-27 13:48:39 -070013370
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013371 if (!crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013372 return DRM_PLANE_HELPER_NO_SCALING;
13373
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013374 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13375 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13376
Rodrigo Vivi43037c82017-10-03 15:31:42 -070013377 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013378 max_dotclk *= 2;
13379
13380 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013381 return DRM_PLANE_HELPER_NO_SCALING;
13382
13383 /*
13384 * skl max scale is lower of:
13385 * close to 3 but not 3, -1 is for that purpose
13386 * or
13387 * cdclk/crtc_clock
13388 */
Chandra Konduru77224cd2018-04-09 09:11:13 +053013389 mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
13390 tmpclk1 = (1 << 16) * mult - 1;
13391 tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
13392 max_scale = min(tmpclk1, tmpclk2);
Chandra Konduru6156a452015-04-27 13:48:39 -070013393
13394 return max_scale;
13395}
13396
Daniel Vetter5a21b662016-05-24 17:13:53 +020013397static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13398 struct drm_crtc_state *old_crtc_state)
13399{
13400 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040013401 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013403 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020013404 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013405 struct intel_atomic_state *old_intel_state =
13406 to_intel_atomic_state(old_crtc_state->state);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013407 struct intel_crtc_state *intel_cstate =
13408 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
13409 bool modeset = needs_modeset(&intel_cstate->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013410
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013411 if (!modeset &&
13412 (intel_cstate->base.color_mgmt_changed ||
13413 intel_cstate->update_pipe)) {
Ville Syrjälä5c857e62017-08-23 18:22:20 +030013414 intel_color_set_csc(&intel_cstate->base);
13415 intel_color_load_luts(&intel_cstate->base);
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013416 }
13417
Daniel Vetter5a21b662016-05-24 17:13:53 +020013418 /* Perform vblank evasion around commit operation */
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013419 intel_pipe_update_start(intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013420
13421 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013422 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013423
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013424 if (intel_cstate->update_pipe)
Ville Syrjälä1a15b772017-08-23 18:22:25 +030013425 intel_update_pipe_config(old_intel_cstate, intel_cstate);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013426 else if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +020013427 skl_detach_scalers(intel_cstate);
Lyude62e0fb82016-08-22 12:50:08 -040013428
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013429out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013430 if (dev_priv->display.atomic_update_watermarks)
13431 dev_priv->display.atomic_update_watermarks(old_intel_state,
13432 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013433}
13434
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013435void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
13436 struct intel_crtc_state *crtc_state)
13437{
13438 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13439
13440 if (!IS_GEN2(dev_priv))
13441 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
13442
13443 if (crtc_state->has_pch_encoder) {
13444 enum pipe pch_transcoder =
13445 intel_crtc_pch_transcoder(crtc);
13446
13447 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
13448 }
13449}
13450
Daniel Vetter5a21b662016-05-24 17:13:53 +020013451static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13452 struct drm_crtc_state *old_crtc_state)
13453{
13454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013455 struct intel_atomic_state *old_intel_state =
13456 to_intel_atomic_state(old_crtc_state->state);
13457 struct intel_crtc_state *new_crtc_state =
13458 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013459
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013460 intel_pipe_update_end(new_crtc_state);
Maarten Lankhorst33a49862017-11-13 15:40:43 +010013461
13462 if (new_crtc_state->update_pipe &&
13463 !needs_modeset(&new_crtc_state->base) &&
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013464 old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
13465 intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013466}
13467
Matt Ropercf4c7c12014-12-04 10:27:42 -080013468/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013469 * intel_plane_destroy - destroy a plane
13470 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013471 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013472 * Common destruction function for all types of planes (primary, cursor,
13473 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013474 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013475void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013476{
Matt Roper465c1202014-05-29 08:06:54 -070013477 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030013478 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070013479}
13480
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013481static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
13482 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013483{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013484 switch (modifier) {
13485 case DRM_FORMAT_MOD_LINEAR:
13486 case I915_FORMAT_MOD_X_TILED:
13487 break;
13488 default:
13489 return false;
13490 }
13491
Ben Widawsky714244e2017-08-01 09:58:16 -070013492 switch (format) {
13493 case DRM_FORMAT_C8:
13494 case DRM_FORMAT_RGB565:
13495 case DRM_FORMAT_XRGB1555:
13496 case DRM_FORMAT_XRGB8888:
13497 return modifier == DRM_FORMAT_MOD_LINEAR ||
13498 modifier == I915_FORMAT_MOD_X_TILED;
13499 default:
13500 return false;
13501 }
13502}
13503
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013504static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
13505 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013506{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013507 switch (modifier) {
13508 case DRM_FORMAT_MOD_LINEAR:
13509 case I915_FORMAT_MOD_X_TILED:
13510 break;
13511 default:
13512 return false;
13513 }
13514
Ben Widawsky714244e2017-08-01 09:58:16 -070013515 switch (format) {
13516 case DRM_FORMAT_C8:
13517 case DRM_FORMAT_RGB565:
13518 case DRM_FORMAT_XRGB8888:
13519 case DRM_FORMAT_XBGR8888:
13520 case DRM_FORMAT_XRGB2101010:
13521 case DRM_FORMAT_XBGR2101010:
13522 return modifier == DRM_FORMAT_MOD_LINEAR ||
13523 modifier == I915_FORMAT_MOD_X_TILED;
13524 default:
13525 return false;
13526 }
13527}
13528
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013529static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
13530 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013531{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013532 return modifier == DRM_FORMAT_MOD_LINEAR &&
13533 format == DRM_FORMAT_ARGB8888;
Ben Widawsky714244e2017-08-01 09:58:16 -070013534}
13535
Ville Syrjälä679bfe82018-10-05 15:58:07 +030013536static const struct drm_plane_funcs i965_plane_funcs = {
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013537 .update_plane = drm_atomic_helper_update_plane,
13538 .disable_plane = drm_atomic_helper_disable_plane,
13539 .destroy = intel_plane_destroy,
13540 .atomic_get_property = intel_plane_atomic_get_property,
13541 .atomic_set_property = intel_plane_atomic_set_property,
13542 .atomic_duplicate_state = intel_plane_duplicate_state,
13543 .atomic_destroy_state = intel_plane_destroy_state,
13544 .format_mod_supported = i965_plane_format_mod_supported,
13545};
13546
Ville Syrjälä679bfe82018-10-05 15:58:07 +030013547static const struct drm_plane_funcs i8xx_plane_funcs = {
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013548 .update_plane = drm_atomic_helper_update_plane,
13549 .disable_plane = drm_atomic_helper_disable_plane,
13550 .destroy = intel_plane_destroy,
13551 .atomic_get_property = intel_plane_atomic_get_property,
13552 .atomic_set_property = intel_plane_atomic_set_property,
13553 .atomic_duplicate_state = intel_plane_duplicate_state,
13554 .atomic_destroy_state = intel_plane_destroy_state,
13555 .format_mod_supported = i8xx_plane_format_mod_supported,
Matt Roper465c1202014-05-29 08:06:54 -070013556};
13557
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013558static int
13559intel_legacy_cursor_update(struct drm_plane *plane,
13560 struct drm_crtc *crtc,
13561 struct drm_framebuffer *fb,
13562 int crtc_x, int crtc_y,
13563 unsigned int crtc_w, unsigned int crtc_h,
13564 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013565 uint32_t src_w, uint32_t src_h,
13566 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013567{
13568 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13569 int ret;
13570 struct drm_plane_state *old_plane_state, *new_plane_state;
13571 struct intel_plane *intel_plane = to_intel_plane(plane);
13572 struct drm_framebuffer *old_fb;
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013573 struct intel_crtc_state *crtc_state =
13574 to_intel_crtc_state(crtc->state);
13575 struct intel_crtc_state *new_crtc_state;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013576
13577 /*
13578 * When crtc is inactive or there is a modeset pending,
13579 * wait for it to complete in the slowpath
13580 */
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013581 if (!crtc_state->base.active || needs_modeset(&crtc_state->base) ||
13582 crtc_state->update_pipe)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013583 goto slow;
13584
13585 old_plane_state = plane->state;
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013586 /*
13587 * Don't do an async update if there is an outstanding commit modifying
13588 * the plane. This prevents our async update's changes from getting
13589 * overridden by a previous synchronous update's state.
13590 */
13591 if (old_plane_state->commit &&
13592 !try_wait_for_completion(&old_plane_state->commit->hw_done))
13593 goto slow;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013594
13595 /*
13596 * If any parameters change that may affect watermarks,
13597 * take the slowpath. Only changing fb or position should be
13598 * in the fastpath.
13599 */
13600 if (old_plane_state->crtc != crtc ||
13601 old_plane_state->src_w != src_w ||
13602 old_plane_state->src_h != src_h ||
13603 old_plane_state->crtc_w != crtc_w ||
13604 old_plane_state->crtc_h != crtc_h ||
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013605 !old_plane_state->fb != !fb)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013606 goto slow;
13607
13608 new_plane_state = intel_plane_duplicate_state(plane);
13609 if (!new_plane_state)
13610 return -ENOMEM;
13611
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013612 new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(crtc));
13613 if (!new_crtc_state) {
13614 ret = -ENOMEM;
13615 goto out_free;
13616 }
13617
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013618 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13619
13620 new_plane_state->src_x = src_x;
13621 new_plane_state->src_y = src_y;
13622 new_plane_state->src_w = src_w;
13623 new_plane_state->src_h = src_h;
13624 new_plane_state->crtc_x = crtc_x;
13625 new_plane_state->crtc_y = crtc_y;
13626 new_plane_state->crtc_w = crtc_w;
13627 new_plane_state->crtc_h = crtc_h;
13628
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013629 ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
13630 to_intel_plane_state(old_plane_state),
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013631 to_intel_plane_state(new_plane_state));
13632 if (ret)
13633 goto out_free;
13634
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013635 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13636 if (ret)
13637 goto out_free;
13638
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013639 ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
13640 if (ret)
13641 goto out_unlock;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013642
Dhinakaran Pandiyana694e222018-03-06 19:34:19 -080013643 intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013644
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080013645 old_fb = old_plane_state->fb;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013646 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13647 intel_plane->frontbuffer_bit);
13648
13649 /* Swap plane state */
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013650 plane->state = new_plane_state;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013651
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013652 /*
13653 * We cannot swap crtc_state as it may be in use by an atomic commit or
13654 * page flip that's running simultaneously. If we swap crtc_state and
13655 * destroy the old state, we will cause a use-after-free there.
13656 *
13657 * Only update active_planes, which is needed for our internal
13658 * bookkeeping. Either value will do the right thing when updating
13659 * planes atomically. If the cursor was part of the atomic update then
13660 * we would have taken the slowpath.
13661 */
13662 crtc_state->active_planes = new_crtc_state->active_planes;
13663
Ville Syrjälä72259532017-03-02 19:15:05 +020013664 if (plane->state->visible) {
13665 trace_intel_update_plane(plane, to_intel_crtc(crtc));
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013666 intel_plane->update_plane(intel_plane, crtc_state,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013667 to_intel_plane_state(plane->state));
Ville Syrjälä72259532017-03-02 19:15:05 +020013668 } else {
13669 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013670 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
Ville Syrjälä72259532017-03-02 19:15:05 +020013671 }
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013672
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013673 intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013674
13675out_unlock:
13676 mutex_unlock(&dev_priv->drm.struct_mutex);
13677out_free:
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013678 if (new_crtc_state)
13679 intel_crtc_destroy_state(crtc, &new_crtc_state->base);
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013680 if (ret)
13681 intel_plane_destroy_state(plane, new_plane_state);
13682 else
13683 intel_plane_destroy_state(plane, old_plane_state);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013684 return ret;
13685
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013686slow:
13687 return drm_atomic_helper_update_plane(plane, crtc, fb,
13688 crtc_x, crtc_y, crtc_w, crtc_h,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013689 src_x, src_y, src_w, src_h, ctx);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013690}
13691
13692static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13693 .update_plane = intel_legacy_cursor_update,
13694 .disable_plane = drm_atomic_helper_disable_plane,
13695 .destroy = intel_plane_destroy,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013696 .atomic_get_property = intel_plane_atomic_get_property,
13697 .atomic_set_property = intel_plane_atomic_set_property,
13698 .atomic_duplicate_state = intel_plane_duplicate_state,
13699 .atomic_destroy_state = intel_plane_destroy_state,
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013700 .format_mod_supported = intel_cursor_format_mod_supported,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013701};
13702
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013703static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
13704 enum i9xx_plane_id i9xx_plane)
13705{
13706 if (!HAS_FBC(dev_priv))
13707 return false;
13708
13709 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
13710 return i9xx_plane == PLANE_A; /* tied to pipe A */
13711 else if (IS_IVYBRIDGE(dev_priv))
13712 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
13713 i9xx_plane == PLANE_C;
13714 else if (INTEL_GEN(dev_priv) >= 4)
13715 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
13716 else
13717 return i9xx_plane == PLANE_A;
13718}
13719
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013720static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020013721intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070013722{
Ville Syrjälä881440a2018-10-05 15:58:17 +030013723 struct intel_plane *plane;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013724 const struct drm_plane_funcs *plane_funcs;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013725 unsigned int supported_rotations;
Ville Syrjälädeb19682018-10-05 15:58:08 +030013726 unsigned int possible_crtcs;
Ville Syrjälä881440a2018-10-05 15:58:17 +030013727 const u64 *modifiers;
13728 const u32 *formats;
13729 int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013730 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013731
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013732 if (INTEL_GEN(dev_priv) >= 9)
13733 return skl_universal_plane_create(dev_priv, pipe,
13734 PLANE_PRIMARY);
13735
Ville Syrjälä881440a2018-10-05 15:58:17 +030013736 plane = intel_plane_alloc();
13737 if (IS_ERR(plane))
13738 return plane;
Matt Roperea2c67b2014-12-23 10:41:52 -080013739
Ville Syrjälä881440a2018-10-05 15:58:17 +030013740 plane->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013741 /*
13742 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13743 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13744 */
13745 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
Ville Syrjälä881440a2018-10-05 15:58:17 +030013746 plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013747 else
Ville Syrjälä881440a2018-10-05 15:58:17 +030013748 plane->i9xx_plane = (enum i9xx_plane_id) pipe;
13749 plane->id = PLANE_PRIMARY;
13750 plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013751
Ville Syrjälä881440a2018-10-05 15:58:17 +030013752 plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
13753 if (plane->has_fbc) {
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013754 struct intel_fbc *fbc = &dev_priv->fbc;
13755
Ville Syrjälä881440a2018-10-05 15:58:17 +030013756 fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013757 }
13758
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013759 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä881440a2018-10-05 15:58:17 +030013760 formats = i965_primary_formats;
Damien Lespiau568db4f2015-05-12 16:13:18 +010013761 num_formats = ARRAY_SIZE(i965_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013762 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013763
Ville Syrjälä881440a2018-10-05 15:58:17 +030013764 plane->max_stride = i9xx_plane_max_stride;
13765 plane->update_plane = i9xx_update_plane;
13766 plane->disable_plane = i9xx_disable_plane;
13767 plane->get_hw_state = i9xx_plane_get_hw_state;
13768 plane->check_plane = i9xx_plane_check;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013769
13770 plane_funcs = &i965_plane_funcs;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013771 } else {
Ville Syrjälä881440a2018-10-05 15:58:17 +030013772 formats = i8xx_primary_formats;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013773 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013774 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013775
Ville Syrjälä881440a2018-10-05 15:58:17 +030013776 plane->max_stride = i9xx_plane_max_stride;
13777 plane->update_plane = i9xx_update_plane;
13778 plane->disable_plane = i9xx_disable_plane;
13779 plane->get_hw_state = i9xx_plane_get_hw_state;
13780 plane->check_plane = i9xx_plane_check;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013781
13782 plane_funcs = &i8xx_plane_funcs;
Matt Roper465c1202014-05-29 08:06:54 -070013783 }
13784
Ville Syrjälädeb19682018-10-05 15:58:08 +030013785 possible_crtcs = BIT(pipe);
13786
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013787 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä881440a2018-10-05 15:58:17 +030013788 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013789 possible_crtcs, plane_funcs,
Ville Syrjälä881440a2018-10-05 15:58:17 +030013790 formats, num_formats, modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013791 DRM_PLANE_TYPE_PRIMARY,
13792 "primary %c", pipe_name(pipe));
13793 else
Ville Syrjälä881440a2018-10-05 15:58:17 +030013794 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013795 possible_crtcs, plane_funcs,
Ville Syrjälä881440a2018-10-05 15:58:17 +030013796 formats, num_formats, modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013797 DRM_PLANE_TYPE_PRIMARY,
Ville Syrjäläed150302017-11-17 21:19:10 +020013798 "plane %c",
Ville Syrjälä881440a2018-10-05 15:58:17 +030013799 plane_name(plane->i9xx_plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013800 if (ret)
13801 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053013802
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013803 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020013804 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013805 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13806 DRM_MODE_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100013807 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013808 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013809 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013810 } else {
Robert Fossc2c446a2017-05-19 16:50:17 -040013811 supported_rotations = DRM_MODE_ROTATE_0;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013812 }
13813
Dave Airlie5481e272016-10-25 16:36:13 +100013814 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä881440a2018-10-05 15:58:17 +030013815 drm_plane_create_rotation_property(&plane->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013816 DRM_MODE_ROTATE_0,
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013817 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053013818
Ville Syrjälä881440a2018-10-05 15:58:17 +030013819 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
Matt Roperea2c67b2014-12-23 10:41:52 -080013820
Ville Syrjälä881440a2018-10-05 15:58:17 +030013821 return plane;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013822
13823fail:
Ville Syrjälä881440a2018-10-05 15:58:17 +030013824 intel_plane_free(plane);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013825
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013826 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070013827}
13828
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013829static struct intel_plane *
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013830intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13831 enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070013832{
Ville Syrjälädeb19682018-10-05 15:58:08 +030013833 unsigned int possible_crtcs;
Ville Syrjäläc539b572018-10-05 15:58:14 +030013834 struct intel_plane *cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013835 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070013836
Ville Syrjäläc539b572018-10-05 15:58:14 +030013837 cursor = intel_plane_alloc();
13838 if (IS_ERR(cursor))
13839 return cursor;
Matt Roperea2c67b2014-12-23 10:41:52 -080013840
Matt Roper3d7d6512014-06-10 08:28:13 -070013841 cursor->pipe = pipe;
Ville Syrjäläed150302017-11-17 21:19:10 +020013842 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013843 cursor->id = PLANE_CURSOR;
Ville Syrjäläc19e1122018-01-23 20:33:43 +020013844 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013845
13846 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjäläddd57132018-09-07 18:24:02 +030013847 cursor->max_stride = i845_cursor_max_stride;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013848 cursor->update_plane = i845_update_cursor;
13849 cursor->disable_plane = i845_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013850 cursor->get_hw_state = i845_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013851 cursor->check_plane = i845_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013852 } else {
Ville Syrjäläddd57132018-09-07 18:24:02 +030013853 cursor->max_stride = i9xx_cursor_max_stride;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013854 cursor->update_plane = i9xx_update_cursor;
13855 cursor->disable_plane = i9xx_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013856 cursor->get_hw_state = i9xx_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013857 cursor->check_plane = i9xx_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013858 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013859
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030013860 cursor->cursor.base = ~0;
13861 cursor->cursor.cntl = ~0;
Ville Syrjälä024faac2017-03-27 21:55:42 +030013862
13863 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13864 cursor->cursor.size = ~0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013865
Ville Syrjälädeb19682018-10-05 15:58:08 +030013866 possible_crtcs = BIT(pipe);
13867
Ville Syrjälä580503c2016-10-31 22:37:00 +020013868 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013869 possible_crtcs, &intel_cursor_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013870 intel_cursor_formats,
13871 ARRAY_SIZE(intel_cursor_formats),
Ben Widawsky714244e2017-08-01 09:58:16 -070013872 cursor_format_modifiers,
13873 DRM_PLANE_TYPE_CURSOR,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013874 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013875 if (ret)
13876 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013877
Dave Airlie5481e272016-10-25 16:36:13 +100013878 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013879 drm_plane_create_rotation_property(&cursor->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013880 DRM_MODE_ROTATE_0,
13881 DRM_MODE_ROTATE_0 |
13882 DRM_MODE_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013883
Matt Roperea2c67b2014-12-23 10:41:52 -080013884 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13885
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013886 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013887
13888fail:
Ville Syrjäläc539b572018-10-05 15:58:14 +030013889 intel_plane_free(cursor);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013890
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013891 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070013892}
13893
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013894static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13895 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013896{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013897 struct intel_crtc_scaler_state *scaler_state =
13898 &crtc_state->scaler_state;
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013899 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013900 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013901
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013902 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13903 if (!crtc->num_scalers)
13904 return;
13905
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013906 for (i = 0; i < crtc->num_scalers; i++) {
13907 struct intel_scaler *scaler = &scaler_state->scalers[i];
13908
13909 scaler->in_use = 0;
Maarten Lankhorst0aaf29b2018-09-21 16:44:37 +020013910 scaler->mode = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013911 }
13912
13913 scaler_state->scaler_id = -1;
13914}
13915
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013916static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013917{
13918 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013919 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013920 struct intel_plane *primary = NULL;
13921 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013922 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013923
Daniel Vetter955382f2013-09-19 14:05:45 +020013924 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013925 if (!intel_crtc)
13926 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080013927
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013928 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013929 if (!crtc_state) {
13930 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013931 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013932 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013933 intel_crtc->config = crtc_state;
13934 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013935 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013936
Ville Syrjälä580503c2016-10-31 22:37:00 +020013937 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013938 if (IS_ERR(primary)) {
13939 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070013940 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013941 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013942 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013943
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013944 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013945 struct intel_plane *plane;
13946
Ville Syrjälä580503c2016-10-31 22:37:00 +020013947 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013948 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013949 ret = PTR_ERR(plane);
13950 goto fail;
13951 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013952 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013953 }
13954
Ville Syrjälä580503c2016-10-31 22:37:00 +020013955 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013956 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013957 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070013958 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013959 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013960 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013961
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013962 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013963 &primary->base, &cursor->base,
13964 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030013965 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070013966 if (ret)
13967 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013968
Jesse Barnes80824002009-09-10 15:28:06 -070013969 intel_crtc->pipe = pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013970
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013971 /* initialize shared scalers */
13972 intel_crtc_init_scalers(intel_crtc, crtc_state);
13973
Ville Syrjälä1947fd12018-03-05 19:41:22 +020013974 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
13975 dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
13976 dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
13977
13978 if (INTEL_GEN(dev_priv) < 9) {
13979 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
13980
13981 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13982 dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
13983 dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
13984 }
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013985
Jesse Barnes79e53942008-11-07 14:24:08 -080013986 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013987
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013988 intel_color_init(&intel_crtc->base);
13989
Daniel Vetter87b6b102014-05-15 15:33:46 +020013990 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013991
13992 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013993
13994fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013995 /*
13996 * drm_mode_config_cleanup() will free up any
13997 * crtcs/planes already initialized.
13998 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013999 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014000 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014001
14002 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014003}
14004
Ville Syrjälä6a20fe72018-02-07 18:48:41 +020014005int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
14006 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014007{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014008 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014009 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014010 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014011
Keith Packard418da172017-03-14 23:25:07 -070014012 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014013 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014014 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014015
Rob Clark7707e652014-07-17 23:30:04 -040014016 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014017 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014018
Daniel Vetterc05422d2009-08-11 16:05:30 +020014019 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014020}
14021
Daniel Vetter66a92782012-07-12 20:08:18 +020014022static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014023{
Daniel Vetter66a92782012-07-12 20:08:18 +020014024 struct drm_device *dev = encoder->base.dev;
14025 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014026 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014027 int entry = 0;
14028
Damien Lespiaub2784e12014-08-05 11:29:37 +010014029 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014030 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014031 index_mask |= (1 << entry);
14032
Jesse Barnes79e53942008-11-07 14:24:08 -080014033 entry++;
14034 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014035
Jesse Barnes79e53942008-11-07 14:24:08 -080014036 return index_mask;
14037}
14038
Ville Syrjälä646d5772016-10-31 22:37:14 +020014039static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000014040{
Ville Syrjälä646d5772016-10-31 22:37:14 +020014041 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000014042 return false;
14043
14044 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14045 return false;
14046
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014047 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014048 return false;
14049
14050 return true;
14051}
14052
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014053static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014054{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014055 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000014056 return false;
14057
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014058 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014059 return false;
14060
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014061 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014062 return false;
14063
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014064 if (HAS_PCH_LPT_H(dev_priv) &&
14065 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014066 return false;
14067
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014068 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014069 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 +020014070 return false;
14071
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014072 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014073 return false;
14074
14075 return true;
14076}
14077
Imre Deak8090ba82016-08-10 14:07:33 +030014078void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14079{
14080 int pps_num;
14081 int pps_idx;
14082
14083 if (HAS_DDI(dev_priv))
14084 return;
14085 /*
14086 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14087 * everywhere where registers can be write protected.
14088 */
14089 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14090 pps_num = 2;
14091 else
14092 pps_num = 1;
14093
14094 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14095 u32 val = I915_READ(PP_CONTROL(pps_idx));
14096
14097 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14098 I915_WRITE(PP_CONTROL(pps_idx), val);
14099 }
14100}
14101
Imre Deak44cb7342016-08-10 14:07:29 +030014102static void intel_pps_init(struct drm_i915_private *dev_priv)
14103{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020014104 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
Imre Deak44cb7342016-08-10 14:07:29 +030014105 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14106 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14107 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14108 else
14109 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030014110
14111 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030014112}
14113
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014114static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080014115{
Chris Wilson4ef69c72010-09-09 15:14:28 +010014116 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014117 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014118
Imre Deak44cb7342016-08-10 14:07:29 +030014119 intel_pps_init(dev_priv);
14120
Chris Wilsonfc0c5a92018-08-15 21:12:07 +010014121 if (INTEL_INFO(dev_priv)->num_pipes == 0)
14122 return;
14123
Imre Deak97a824e12016-06-21 11:51:47 +030014124 /*
14125 * intel_edp_init_connector() depends on this completing first, to
14126 * prevent the registeration of both eDP and LVDS and the incorrect
14127 * sharing of the PPS.
14128 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014129 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014130
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014131 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014132 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014133
Paulo Zanoni00c92d92018-05-21 17:25:47 -070014134 if (IS_ICELAKE(dev_priv)) {
14135 intel_ddi_init(dev_priv, PORT_A);
14136 intel_ddi_init(dev_priv, PORT_B);
14137 intel_ddi_init(dev_priv, PORT_C);
14138 intel_ddi_init(dev_priv, PORT_D);
14139 intel_ddi_init(dev_priv, PORT_E);
14140 intel_ddi_init(dev_priv, PORT_F);
Madhav Chauhanbf4d57f2018-10-30 13:56:23 +020014141 icl_dsi_init(dev_priv);
Paulo Zanoni00c92d92018-05-21 17:25:47 -070014142 } else if (IS_GEN9_LP(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053014143 /*
14144 * FIXME: Broxton doesn't support port detection via the
14145 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14146 * detect the ports.
14147 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014148 intel_ddi_init(dev_priv, PORT_A);
14149 intel_ddi_init(dev_priv, PORT_B);
14150 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014151
Jani Nikulae5186342018-07-05 16:25:08 +030014152 vlv_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014153 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014154 int found;
14155
Jesse Barnesde31fac2015-03-06 15:53:32 -080014156 /*
14157 * Haswell uses DDI functions to detect digital outputs.
14158 * On SKL pre-D0 the strap isn't connected, so we assume
14159 * it's there.
14160 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014161 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014162 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014163 if (found || IS_GEN9_BC(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014164 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014165
Rodrigo Vivi9787e832018-01-29 15:22:22 -080014166 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014167 * register */
14168 found = I915_READ(SFUSE_STRAP);
14169
14170 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014171 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014172 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014173 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014174 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014175 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi9787e832018-01-29 15:22:22 -080014176 if (found & SFUSE_STRAP_DDIF_DETECTED)
14177 intel_ddi_init(dev_priv, PORT_F);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014178 /*
14179 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14180 */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014181 if (IS_GEN9_BC(dev_priv) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014182 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14183 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14184 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014185 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014186
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014187 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014188 int found;
Jani Nikula7b91bf72017-08-18 12:30:19 +030014189 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014190
Ville Syrjälä646d5772016-10-31 22:37:14 +020014191 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014192 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014193
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014194 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014195 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014196 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014197 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014198 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014199 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014200 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014201 }
14202
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014203 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014204 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014205
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014206 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014207 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014208
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014209 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014210 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014211
Daniel Vetter270b3042012-10-27 15:52:05 +020014212 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014213 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014214 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014215 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014216
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014217 /*
14218 * The DP_DETECTED bit is the latched state of the DDC
14219 * SDA pin at boot. However since eDP doesn't require DDC
14220 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14221 * eDP ports may have been muxed to an alternate function.
14222 * Thus we can't rely on the DP_DETECTED bit alone to detect
14223 * eDP ports. Consult the VBT as well as DP_DETECTED to
14224 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014225 *
14226 * Sadly the straps seem to be missing sometimes even for HDMI
14227 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14228 * and VBT for the presence of the port. Additionally we can't
14229 * trust the port type the VBT declares as we've seen at least
14230 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014231 */
Jani Nikula7b91bf72017-08-18 12:30:19 +030014232 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014233 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14234 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014235 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014236 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014237 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014238
Jani Nikula7b91bf72017-08-18 12:30:19 +030014239 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014240 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14241 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014242 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014243 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014244 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014245
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014246 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014247 /*
14248 * eDP not supported on port D,
14249 * so no need to worry about it
14250 */
14251 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14252 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014253 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014254 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014255 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014256 }
14257
Jani Nikulae5186342018-07-05 16:25:08 +030014258 vlv_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014259 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014260 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014261
Paulo Zanonie2debe92013-02-18 19:00:27 -030014262 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014263 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014264 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014265 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014266 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014267 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014268 }
Ma Ling27185ae2009-08-24 13:50:23 +080014269
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014270 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014271 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014272 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014273
14274 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014275
Paulo Zanonie2debe92013-02-18 19:00:27 -030014276 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014277 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014278 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014279 }
Ma Ling27185ae2009-08-24 13:50:23 +080014280
Paulo Zanonie2debe92013-02-18 19:00:27 -030014281 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014282
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014283 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014284 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014285 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014286 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014287 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014288 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014289 }
Ma Ling27185ae2009-08-24 13:50:23 +080014290
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014291 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014292 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014293 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014294 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014295
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000014296 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014297 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014298
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014299 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014300
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014301 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014302 encoder->base.possible_crtcs = encoder->crtc_mask;
14303 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014304 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014305 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014306
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014307 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020014308
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014309 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080014310}
14311
14312static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14313{
14314 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014315 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014316
Daniel Vetteref2d6332014-02-10 18:00:38 +010014317 drm_framebuffer_cleanup(fb);
Chris Wilson70001cd2017-02-16 09:46:21 +000014318
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014319 i915_gem_object_lock(obj);
14320 WARN_ON(!obj->framebuffer_references--);
14321 i915_gem_object_unlock(obj);
Chris Wilsondd689282017-03-01 15:41:28 +000014322
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014323 i915_gem_object_put(obj);
Chris Wilson70001cd2017-02-16 09:46:21 +000014324
Jesse Barnes79e53942008-11-07 14:24:08 -080014325 kfree(intel_fb);
14326}
14327
14328static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014329 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014330 unsigned int *handle)
14331{
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014332 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014333
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014334 if (obj->userptr.mm) {
14335 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14336 return -EINVAL;
14337 }
14338
Chris Wilson05394f32010-11-08 19:18:58 +000014339 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014340}
14341
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014342static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14343 struct drm_file *file,
14344 unsigned flags, unsigned color,
14345 struct drm_clip_rect *clips,
14346 unsigned num_clips)
14347{
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014348 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014349
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014350 i915_gem_object_flush_if_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +000014351 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014352
14353 return 0;
14354}
14355
Jesse Barnes79e53942008-11-07 14:24:08 -080014356static const struct drm_framebuffer_funcs intel_fb_funcs = {
14357 .destroy = intel_user_framebuffer_destroy,
14358 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014359 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014360};
14361
Damien Lespiaub3218032015-02-27 11:15:18 +000014362static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014363u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
Dhinakaran Pandiyan4c8d3512018-10-26 12:53:42 -070014364 u32 pixel_format, u64 fb_modifier)
Damien Lespiaub3218032015-02-27 11:15:18 +000014365{
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014366 struct intel_crtc *crtc;
14367 struct intel_plane *plane;
Damien Lespiaub3218032015-02-27 11:15:18 +000014368
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014369 /*
14370 * We assume the primary plane for pipe A has
14371 * the highest stride limits of them all.
14372 */
14373 crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
14374 plane = to_intel_plane(crtc->base.primary);
Ville Syrjäläac484962016-01-20 21:05:26 +020014375
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014376 return plane->max_stride(plane, pixel_format, fb_modifier,
14377 DRM_MODE_ROTATE_0);
Damien Lespiaub3218032015-02-27 11:15:18 +000014378}
14379
Chris Wilson24dbf512017-02-15 10:59:18 +000014380static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14381 struct drm_i915_gem_object *obj,
14382 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014383{
Chris Wilson24dbf512017-02-15 10:59:18 +000014384 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014385 struct drm_framebuffer *fb = &intel_fb->base;
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014386 struct drm_format_name_buf format_name;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014387 u32 pitch_limit;
Chris Wilsondd689282017-03-01 15:41:28 +000014388 unsigned int tiling, stride;
Chris Wilson24dbf512017-02-15 10:59:18 +000014389 int ret = -EINVAL;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014390 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -080014391
Chris Wilsondd689282017-03-01 15:41:28 +000014392 i915_gem_object_lock(obj);
14393 obj->framebuffer_references++;
14394 tiling = i915_gem_object_get_tiling(obj);
14395 stride = i915_gem_object_get_stride(obj);
14396 i915_gem_object_unlock(obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014397
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014398 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014399 /*
14400 * If there's a fence, enforce that
14401 * the fb modifier and tiling mode match.
14402 */
14403 if (tiling != I915_TILING_NONE &&
14404 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014405 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014406 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014407 }
14408 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014409 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014410 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014411 } else if (tiling == I915_TILING_Y) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014412 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014413 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014414 }
14415 }
14416
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014417 /* Passed in modifier sanity checking. */
14418 switch (mode_cmd->modifier[0]) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014419 case I915_FORMAT_MOD_Y_TILED_CCS:
14420 case I915_FORMAT_MOD_Yf_TILED_CCS:
14421 switch (mode_cmd->pixel_format) {
14422 case DRM_FORMAT_XBGR8888:
14423 case DRM_FORMAT_ABGR8888:
14424 case DRM_FORMAT_XRGB8888:
14425 case DRM_FORMAT_ARGB8888:
14426 break;
14427 default:
14428 DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
14429 goto err;
14430 }
14431 /* fall through */
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014432 case I915_FORMAT_MOD_Yf_TILED:
Paulo Zanonief51e0a2018-09-24 17:19:11 -070014433 if (mode_cmd->pixel_format == DRM_FORMAT_C8) {
14434 DRM_DEBUG_KMS("Indexed format does not support Yf tiling\n");
14435 goto err;
14436 }
14437 /* fall through */
14438 case I915_FORMAT_MOD_Y_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014439 if (INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014440 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14441 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014442 goto err;
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014443 }
Paulo Zanonief51e0a2018-09-24 17:19:11 -070014444 break;
Ben Widawsky2f075562017-03-24 14:29:48 -070014445 case DRM_FORMAT_MOD_LINEAR:
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014446 case I915_FORMAT_MOD_X_TILED:
14447 break;
14448 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014449 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14450 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014451 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014452 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014453
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014454 /*
14455 * gen2/3 display engine uses the fence if present,
14456 * so the tiling mode must match the fb modifier exactly.
14457 */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014458 if (INTEL_GEN(dev_priv) < 4 &&
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014459 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014460 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014461 goto err;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014462 }
14463
Dhinakaran Pandiyan4c8d3512018-10-26 12:53:42 -070014464 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->pixel_format,
14465 mode_cmd->modifier[0]);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014466 if (mode_cmd->pitches[0] > pitch_limit) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014467 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
Ben Widawsky2f075562017-03-24 14:29:48 -070014468 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014469 "tiled" : "linear",
14470 mode_cmd->pitches[0], pitch_limit);
Chris Wilson24dbf512017-02-15 10:59:18 +000014471 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014472 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014473
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014474 /*
14475 * If there's a fence, enforce that
14476 * the fb pitch and fence stride match.
14477 */
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014478 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14479 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14480 mode_cmd->pitches[0], stride);
Chris Wilson24dbf512017-02-15 10:59:18 +000014481 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014482 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014483
Ville Syrjälä57779d02012-10-31 17:50:14 +020014484 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014485 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014486 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014487 case DRM_FORMAT_RGB565:
14488 case DRM_FORMAT_XRGB8888:
14489 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014490 break;
14491 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014492 if (INTEL_GEN(dev_priv) > 3) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014493 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14494 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014495 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014496 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014497 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014498 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014499 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014500 INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014501 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14502 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014503 goto err;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014504 }
14505 break;
14506 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014507 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014508 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014509 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014510 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14511 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014512 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014513 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014514 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014515 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014516 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014517 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14518 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014519 goto err;
Damien Lespiau75312082015-05-15 19:06:01 +010014520 }
14521 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014522 case DRM_FORMAT_YUYV:
14523 case DRM_FORMAT_UYVY:
14524 case DRM_FORMAT_YVYU:
14525 case DRM_FORMAT_VYUY:
Ville Syrjäläab330812017-04-21 21:14:32 +030014526 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014527 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14528 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014529 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014530 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014531 break;
Chandra Kondurue44134f2018-05-12 03:03:15 +053014532 case DRM_FORMAT_NV12:
Chandra Kondurue44134f2018-05-12 03:03:15 +053014533 if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
Maarten Lankhorst26ee5bc2018-10-22 15:45:14 +020014534 IS_BROXTON(dev_priv)) {
Chandra Kondurue44134f2018-05-12 03:03:15 +053014535 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14536 drm_get_format_name(mode_cmd->pixel_format,
14537 &format_name));
14538 goto err;
14539 }
14540 break;
Chris Wilson57cd6502010-08-08 12:34:44 +010014541 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014542 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14543 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014544 goto err;
Chris Wilson57cd6502010-08-08 12:34:44 +010014545 }
14546
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014547 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14548 if (mode_cmd->offsets[0] != 0)
Chris Wilson24dbf512017-02-15 10:59:18 +000014549 goto err;
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014550
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014551 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014552
Chandra Kondurue44134f2018-05-12 03:03:15 +053014553 if (fb->format->format == DRM_FORMAT_NV12 &&
14554 (fb->width < SKL_MIN_YUV_420_SRC_W ||
14555 fb->height < SKL_MIN_YUV_420_SRC_H ||
14556 (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
14557 DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
Ville Syrjälä3b909462018-10-29 16:00:31 +020014558 goto err;
Chandra Kondurue44134f2018-05-12 03:03:15 +053014559 }
14560
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014561 for (i = 0; i < fb->format->num_planes; i++) {
14562 u32 stride_alignment;
14563
14564 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
14565 DRM_DEBUG_KMS("bad plane %d handle\n", i);
Christophe JAILLET37875d62017-09-10 10:56:42 +020014566 goto err;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014567 }
14568
14569 stride_alignment = intel_fb_stride_alignment(fb, i);
14570
14571 /*
14572 * Display WA #0531: skl,bxt,kbl,glk
14573 *
14574 * Render decompression and plane width > 3840
14575 * combined with horizontal panning requires the
14576 * plane stride to be a multiple of 4. We'll just
14577 * require the entire fb to accommodate that to avoid
14578 * potential runtime errors at plane configuration time.
14579 */
14580 if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -070014581 is_ccs_modifier(fb->modifier))
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014582 stride_alignment *= 4;
14583
14584 if (fb->pitches[i] & (stride_alignment - 1)) {
14585 DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
14586 i, fb->pitches[i], stride_alignment);
14587 goto err;
14588 }
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014589
Daniel Stonea268bcd2018-05-18 15:30:08 +010014590 fb->obj[i] = &obj->base;
14591 }
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014592
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014593 ret = intel_fill_fb_info(dev_priv, fb);
Ville Syrjälä6687c902015-09-15 13:16:41 +030014594 if (ret)
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014595 goto err;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014596
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014597 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080014598 if (ret) {
14599 DRM_ERROR("framebuffer init failed %d\n", ret);
Chris Wilson24dbf512017-02-15 10:59:18 +000014600 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -080014601 }
14602
Jesse Barnes79e53942008-11-07 14:24:08 -080014603 return 0;
Chris Wilson24dbf512017-02-15 10:59:18 +000014604
14605err:
Chris Wilsondd689282017-03-01 15:41:28 +000014606 i915_gem_object_lock(obj);
14607 obj->framebuffer_references--;
14608 i915_gem_object_unlock(obj);
Chris Wilson24dbf512017-02-15 10:59:18 +000014609 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014610}
14611
Jesse Barnes79e53942008-11-07 14:24:08 -080014612static struct drm_framebuffer *
14613intel_user_framebuffer_create(struct drm_device *dev,
14614 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014615 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014616{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014617 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014618 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014619 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014620
Chris Wilson03ac0642016-07-20 13:31:51 +010014621 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14622 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014623 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014624
Chris Wilson24dbf512017-02-15 10:59:18 +000014625 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014626 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010014627 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014628
14629 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014630}
14631
Chris Wilson778e23a2016-12-05 14:29:39 +000014632static void intel_atomic_state_free(struct drm_atomic_state *state)
14633{
14634 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14635
14636 drm_atomic_state_default_release(state);
14637
14638 i915_sw_fence_fini(&intel_state->commit_ready);
14639
14640 kfree(state);
14641}
14642
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014643static enum drm_mode_status
14644intel_mode_valid(struct drm_device *dev,
14645 const struct drm_display_mode *mode)
14646{
Ville Syrjäläad77c532018-06-15 20:44:05 +030014647 struct drm_i915_private *dev_priv = to_i915(dev);
14648 int hdisplay_max, htotal_max;
14649 int vdisplay_max, vtotal_max;
14650
Ville Syrjäläe4dd27a2018-05-24 15:54:03 +030014651 /*
14652 * Can't reject DBLSCAN here because Xorg ddxen can add piles
14653 * of DBLSCAN modes to the output's mode list when they detect
14654 * the scaling mode property on the connector. And they don't
14655 * ask the kernel to validate those modes in any way until
14656 * modeset time at which point the client gets a protocol error.
14657 * So in order to not upset those clients we silently ignore the
14658 * DBLSCAN flag on such connectors. For other connectors we will
14659 * reject modes with the DBLSCAN flag in encoder->compute_config().
14660 * And we always reject DBLSCAN modes in connector->mode_valid()
14661 * as we never want such modes on the connector's mode list.
14662 */
14663
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014664 if (mode->vscan > 1)
14665 return MODE_NO_VSCAN;
14666
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014667 if (mode->flags & DRM_MODE_FLAG_HSKEW)
14668 return MODE_H_ILLEGAL;
14669
14670 if (mode->flags & (DRM_MODE_FLAG_CSYNC |
14671 DRM_MODE_FLAG_NCSYNC |
14672 DRM_MODE_FLAG_PCSYNC))
14673 return MODE_HSYNC;
14674
14675 if (mode->flags & (DRM_MODE_FLAG_BCAST |
14676 DRM_MODE_FLAG_PIXMUX |
14677 DRM_MODE_FLAG_CLKDIV2))
14678 return MODE_BAD;
14679
Ville Syrjäläad77c532018-06-15 20:44:05 +030014680 if (INTEL_GEN(dev_priv) >= 9 ||
14681 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
14682 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
14683 vdisplay_max = 4096;
14684 htotal_max = 8192;
14685 vtotal_max = 8192;
14686 } else if (INTEL_GEN(dev_priv) >= 3) {
14687 hdisplay_max = 4096;
14688 vdisplay_max = 4096;
14689 htotal_max = 8192;
14690 vtotal_max = 8192;
14691 } else {
14692 hdisplay_max = 2048;
14693 vdisplay_max = 2048;
14694 htotal_max = 4096;
14695 vtotal_max = 4096;
14696 }
14697
14698 if (mode->hdisplay > hdisplay_max ||
14699 mode->hsync_start > htotal_max ||
14700 mode->hsync_end > htotal_max ||
14701 mode->htotal > htotal_max)
14702 return MODE_H_ILLEGAL;
14703
14704 if (mode->vdisplay > vdisplay_max ||
14705 mode->vsync_start > vtotal_max ||
14706 mode->vsync_end > vtotal_max ||
14707 mode->vtotal > vtotal_max)
14708 return MODE_V_ILLEGAL;
14709
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014710 return MODE_OK;
14711}
14712
Jesse Barnes79e53942008-11-07 14:24:08 -080014713static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014714 .fb_create = intel_user_framebuffer_create,
Ville Syrjäläbbfb6ce2017-08-01 09:58:12 -070014715 .get_format_info = intel_get_format_info,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014716 .output_poll_changed = intel_fbdev_output_poll_changed,
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014717 .mode_valid = intel_mode_valid,
Matt Roper5ee67f12015-01-21 16:35:44 -080014718 .atomic_check = intel_atomic_check,
14719 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014720 .atomic_state_alloc = intel_atomic_state_alloc,
14721 .atomic_state_clear = intel_atomic_state_clear,
Chris Wilson778e23a2016-12-05 14:29:39 +000014722 .atomic_state_free = intel_atomic_state_free,
Jesse Barnes79e53942008-11-07 14:24:08 -080014723};
14724
Imre Deak88212942016-03-16 13:38:53 +020014725/**
14726 * intel_init_display_hooks - initialize the display modesetting hooks
14727 * @dev_priv: device private
14728 */
14729void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014730{
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +020014731 intel_init_cdclk_hooks(dev_priv);
14732
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014733 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014734 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014735 dev_priv->display.get_initial_plane_config =
14736 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014737 dev_priv->display.crtc_compute_clock =
14738 haswell_crtc_compute_clock;
14739 dev_priv->display.crtc_enable = haswell_crtc_enable;
14740 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014741 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014742 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014743 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014744 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014745 dev_priv->display.crtc_compute_clock =
14746 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014747 dev_priv->display.crtc_enable = haswell_crtc_enable;
14748 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014749 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014750 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014751 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014752 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014753 dev_priv->display.crtc_compute_clock =
14754 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014755 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14756 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014757 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014758 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014759 dev_priv->display.get_initial_plane_config =
14760 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014761 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14762 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14763 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14764 } else if (IS_VALLEYVIEW(dev_priv)) {
14765 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14766 dev_priv->display.get_initial_plane_config =
14767 i9xx_get_initial_plane_config;
14768 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014769 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14770 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014771 } else if (IS_G4X(dev_priv)) {
14772 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14773 dev_priv->display.get_initial_plane_config =
14774 i9xx_get_initial_plane_config;
14775 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14776 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14777 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014778 } else if (IS_PINEVIEW(dev_priv)) {
14779 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14780 dev_priv->display.get_initial_plane_config =
14781 i9xx_get_initial_plane_config;
14782 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14783 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14784 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014785 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014786 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014787 dev_priv->display.get_initial_plane_config =
14788 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014789 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014790 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14791 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014792 } else {
14793 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14794 dev_priv->display.get_initial_plane_config =
14795 i9xx_get_initial_plane_config;
14796 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14797 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14798 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014799 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014800
Imre Deak88212942016-03-16 13:38:53 +020014801 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014802 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014803 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014804 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014805 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014806 /* FIXME: detect B0+ stepping and use auto training */
14807 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014808 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014809 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014810 }
14811
Rodrigo Vivibd30ca22017-09-26 14:13:46 -070014812 if (INTEL_GEN(dev_priv) >= 9)
Lyude27082492016-08-24 07:48:10 +020014813 dev_priv->display.update_crtcs = skl_update_crtcs;
14814 else
14815 dev_priv->display.update_crtcs = intel_update_crtcs;
Jesse Barnese70236a2009-09-21 10:42:27 -070014816}
14817
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014818/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014819static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014820{
David Weinehall52a05c32016-08-22 13:32:44 +030014821 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014822 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014823 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014824
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014825 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030014826 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014827 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014828 sr1 = inb(VGA_SR_DATA);
14829 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030014830 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014831 udelay(300);
14832
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014833 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014834 POSTING_READ(vga_reg);
14835}
14836
Daniel Vetterf8175862012-04-10 15:50:11 +020014837void intel_modeset_init_hw(struct drm_device *dev)
14838{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014839 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014840
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014841 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +030014842 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020014843 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
Daniel Vetterf8175862012-04-10 15:50:11 +020014844}
14845
Matt Roperd93c0372015-12-03 11:37:41 -080014846/*
14847 * Calculate what we think the watermarks should be for the state we've read
14848 * out of the hardware and then immediately program those watermarks so that
14849 * we ensure the hardware settings match our internal state.
14850 *
14851 * We can calculate what we think WM's should be by creating a duplicate of the
14852 * current state (which was constructed during hardware readout) and running it
14853 * through the atomic check code to calculate new watermark values in the
14854 * state object.
14855 */
14856static void sanitize_watermarks(struct drm_device *dev)
14857{
14858 struct drm_i915_private *dev_priv = to_i915(dev);
14859 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014860 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014861 struct drm_crtc *crtc;
14862 struct drm_crtc_state *cstate;
14863 struct drm_modeset_acquire_ctx ctx;
14864 int ret;
14865 int i;
14866
14867 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080014868 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080014869 return;
14870
14871 /*
14872 * We need to hold connection_mutex before calling duplicate_state so
14873 * that the connector loop is protected.
14874 */
14875 drm_modeset_acquire_init(&ctx, 0);
14876retry:
Matt Roper0cd12622016-01-12 07:13:37 -080014877 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080014878 if (ret == -EDEADLK) {
14879 drm_modeset_backoff(&ctx);
14880 goto retry;
14881 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080014882 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014883 }
14884
14885 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14886 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080014887 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014888
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014889 intel_state = to_intel_atomic_state(state);
14890
Matt Ropered4a6a72016-02-23 17:20:13 -080014891 /*
14892 * Hardware readout is the only time we don't want to calculate
14893 * intermediate watermarks (since we don't trust the current
14894 * watermarks).
14895 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020014896 if (!HAS_GMCH_DISPLAY(dev_priv))
14897 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080014898
Matt Roperd93c0372015-12-03 11:37:41 -080014899 ret = intel_atomic_check(dev, state);
14900 if (ret) {
14901 /*
14902 * If we fail here, it means that the hardware appears to be
14903 * programmed in a way that shouldn't be possible, given our
14904 * understanding of watermark requirements. This might mean a
14905 * mistake in the hardware readout code or a mistake in the
14906 * watermark calculations for a given platform. Raise a WARN
14907 * so that this is noticeable.
14908 *
14909 * If this actually happens, we'll have to just leave the
14910 * BIOS-programmed watermarks untouched and hope for the best.
14911 */
14912 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014913 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014914 }
14915
14916 /* Write calculated watermark values back */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010014917 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roperd93c0372015-12-03 11:37:41 -080014918 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14919
Matt Ropered4a6a72016-02-23 17:20:13 -080014920 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014921 dev_priv->display.optimize_watermarks(intel_state, cs);
Maarten Lankhorst556fe362017-11-10 12:34:53 +010014922
14923 to_intel_crtc_state(crtc->state)->wm = cs->wm;
Matt Roperd93c0372015-12-03 11:37:41 -080014924 }
14925
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014926put_state:
Chris Wilson08536952016-10-14 13:18:18 +010014927 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080014928fail:
Matt Roperd93c0372015-12-03 11:37:41 -080014929 drm_modeset_drop_locks(&ctx);
14930 drm_modeset_acquire_fini(&ctx);
14931}
14932
Chris Wilson58ecd9d2017-11-05 13:49:05 +000014933static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
14934{
14935 if (IS_GEN5(dev_priv)) {
14936 u32 fdi_pll_clk =
14937 I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
14938
14939 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
14940 } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
14941 dev_priv->fdi_pll_freq = 270000;
14942 } else {
14943 return;
14944 }
14945
14946 DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
14947}
14948
Azhar Shaikh516a49c2018-07-06 11:37:30 -070014949static int intel_initial_commit(struct drm_device *dev)
14950{
14951 struct drm_atomic_state *state = NULL;
14952 struct drm_modeset_acquire_ctx ctx;
14953 struct drm_crtc *crtc;
14954 struct drm_crtc_state *crtc_state;
14955 int ret = 0;
14956
14957 state = drm_atomic_state_alloc(dev);
14958 if (!state)
14959 return -ENOMEM;
14960
14961 drm_modeset_acquire_init(&ctx, 0);
14962
14963retry:
14964 state->acquire_ctx = &ctx;
14965
14966 drm_for_each_crtc(crtc, dev) {
14967 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14968 if (IS_ERR(crtc_state)) {
14969 ret = PTR_ERR(crtc_state);
14970 goto out;
14971 }
14972
14973 if (crtc_state->active) {
14974 ret = drm_atomic_add_affected_planes(state, crtc);
14975 if (ret)
14976 goto out;
14977 }
14978 }
14979
14980 ret = drm_atomic_commit(state);
14981
14982out:
14983 if (ret == -EDEADLK) {
14984 drm_atomic_state_clear(state);
14985 drm_modeset_backoff(&ctx);
14986 goto retry;
14987 }
14988
14989 drm_atomic_state_put(state);
14990
14991 drm_modeset_drop_locks(&ctx);
14992 drm_modeset_acquire_fini(&ctx);
14993
14994 return ret;
14995}
14996
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014997int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080014998{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030014999 struct drm_i915_private *dev_priv = to_i915(dev);
15000 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015001 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015002 struct intel_crtc *crtc;
Azhar Shaikh516a49c2018-07-06 11:37:30 -070015003 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015004
Ville Syrjälä757fffc2017-11-13 15:36:22 +020015005 dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
15006
Jesse Barnes79e53942008-11-07 14:24:08 -080015007 drm_mode_config_init(dev);
15008
15009 dev->mode_config.min_width = 0;
15010 dev->mode_config.min_height = 0;
15011
Dave Airlie019d96c2011-09-29 16:20:42 +010015012 dev->mode_config.preferred_depth = 24;
15013 dev->mode_config.prefer_shadow = 1;
15014
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015015 dev->mode_config.allow_fb_modifiers = true;
15016
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015017 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015018
Andrea Arcangeli400c19d2017-04-07 01:23:45 +020015019 init_llist_head(&dev_priv->atomic_helper.free_list);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015020 INIT_WORK(&dev_priv->atomic_helper.free_work,
Chris Wilsonba318c62017-02-02 20:47:41 +000015021 intel_atomic_helper_free_state_worker);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015022
Jani Nikula27a981b2018-10-17 12:35:39 +030015023 intel_init_quirks(dev_priv);
Jesse Barnesb690e962010-07-19 13:53:12 -070015024
Ville Syrjälä62d75df2016-10-31 22:37:25 +020015025 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015026
Lukas Wunner69f92f62015-07-15 13:57:35 +020015027 /*
15028 * There may be no VBT; and if the BIOS enabled SSC we can
15029 * just keep using it to avoid unnecessary flicker. Whereas if the
15030 * BIOS isn't using it, don't assume it will work even if the VBT
15031 * indicates as much.
15032 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015033 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020015034 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15035 DREF_SSC1_ENABLE);
15036
15037 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15038 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15039 bios_lvds_use_ssc ? "en" : "dis",
15040 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15041 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15042 }
15043 }
15044
Ville Syrjäläad77c532018-06-15 20:44:05 +030015045 /* maximum framebuffer dimensions */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015046 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015047 dev->mode_config.max_width = 2048;
15048 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015049 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015050 dev->mode_config.max_width = 4096;
15051 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015052 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015053 dev->mode_config.max_width = 8192;
15054 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015055 }
Damien Lespiau068be562014-03-28 14:17:49 +000015056
Jani Nikula2a307c22016-11-30 17:43:04 +020015057 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15058 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015059 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015060 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä98fac1d2018-06-15 20:44:04 +030015061 dev->mode_config.cursor_width = 64;
15062 dev->mode_config.cursor_height = 64;
Damien Lespiau068be562014-03-28 14:17:49 +000015063 } else {
Ville Syrjälä98fac1d2018-06-15 20:44:04 +030015064 dev->mode_config.cursor_width = 256;
15065 dev->mode_config.cursor_height = 256;
Damien Lespiau068be562014-03-28 14:17:49 +000015066 }
15067
Matthew Auld73ebd502017-12-11 15:18:20 +000015068 dev->mode_config.fb_base = ggtt->gmadr.start;
Jesse Barnes79e53942008-11-07 14:24:08 -080015069
Zhao Yakui28c97732009-10-09 11:39:41 +080015070 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015071 INTEL_INFO(dev_priv)->num_pipes,
15072 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015073
Damien Lespiau055e3932014-08-18 13:49:10 +010015074 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015075 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015076 if (ret) {
15077 drm_mode_config_cleanup(dev);
15078 return ret;
15079 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015080 }
15081
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015082 intel_shared_dpll_init(dev);
Chris Wilson58ecd9d2017-11-05 13:49:05 +000015083 intel_update_fdi_pll_freq(dev_priv);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015084
Ville Syrjälä5be6e332017-02-20 16:04:43 +020015085 intel_update_czclk(dev_priv);
15086 intel_modeset_init_hw(dev);
15087
Ville Syrjäläb2045352016-05-13 23:41:27 +030015088 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020015089 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030015090
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015091 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015092 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015093 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000015094
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015095 drm_modeset_lock_all(dev);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015096 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015097 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015098
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015099 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015100 struct intel_initial_plane_config plane_config = {};
15101
Jesse Barnes46f297f2014-03-07 08:57:48 -080015102 if (!crtc->active)
15103 continue;
15104
Jesse Barnes46f297f2014-03-07 08:57:48 -080015105 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015106 * Note that reserving the BIOS fb up front prevents us
15107 * from stuffing other stolen allocations like the ring
15108 * on top. This prevents some ugliness at boot time, and
15109 * can even allow for smooth boot transitions if the BIOS
15110 * fb is large enough for the active pipe configuration.
15111 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015112 dev_priv->display.get_initial_plane_config(crtc,
15113 &plane_config);
15114
15115 /*
15116 * If the fb is shared between multiple heads, we'll
15117 * just get the first one.
15118 */
15119 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015120 }
Matt Roperd93c0372015-12-03 11:37:41 -080015121
15122 /*
15123 * Make sure hardware watermarks really match the state we read out.
15124 * Note that we need to do this after reconstructing the BIOS fb's
15125 * since the watermark calculation done here will use pstate->fb.
15126 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020015127 if (!HAS_GMCH_DISPLAY(dev_priv))
15128 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015129
Azhar Shaikh516a49c2018-07-06 11:37:30 -070015130 /*
15131 * Force all active planes to recompute their states. So that on
15132 * mode_setcrtc after probe, all the intel_plane_state variables
15133 * are already calculated and there is no assert_plane warnings
15134 * during bootup.
15135 */
15136 ret = intel_initial_commit(dev);
15137 if (ret)
15138 DRM_DEBUG_KMS("Initial commit in probe failed.\n");
15139
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015140 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010015141}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015142
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015143void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15144{
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020015145 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015146 /* 640x480@60Hz, ~25175 kHz */
15147 struct dpll clock = {
15148 .m1 = 18,
15149 .m2 = 7,
15150 .p1 = 13,
15151 .p2 = 4,
15152 .n = 2,
15153 };
15154 u32 dpll, fp;
15155 int i;
15156
15157 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
15158
15159 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
15160 pipe_name(pipe), clock.vco, clock.dot);
15161
15162 fp = i9xx_dpll_compute_fp(&clock);
15163 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
15164 DPLL_VGA_MODE_DIS |
15165 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
15166 PLL_P2_DIVIDE_BY_4 |
15167 PLL_REF_INPUT_DREFCLK |
15168 DPLL_VCO_ENABLE;
15169
15170 I915_WRITE(FP0(pipe), fp);
15171 I915_WRITE(FP1(pipe), fp);
15172
15173 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
15174 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
15175 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
15176 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
15177 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
15178 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
15179 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
15180
15181 /*
15182 * Apparently we need to have VGA mode enabled prior to changing
15183 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
15184 * dividers, even though the register value does change.
15185 */
15186 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
15187 I915_WRITE(DPLL(pipe), dpll);
15188
15189 /* Wait for the clocks to stabilize. */
15190 POSTING_READ(DPLL(pipe));
15191 udelay(150);
15192
15193 /* The pixel multiplier can only be updated once the
15194 * DPLL is enabled and the clocks are stable.
15195 *
15196 * So write it again.
15197 */
15198 I915_WRITE(DPLL(pipe), dpll);
15199
15200 /* We do this three times for luck */
15201 for (i = 0; i < 3 ; i++) {
15202 I915_WRITE(DPLL(pipe), dpll);
15203 POSTING_READ(DPLL(pipe));
15204 udelay(150); /* wait for warmup */
15205 }
15206
15207 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
15208 POSTING_READ(PIPECONF(pipe));
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020015209
15210 intel_wait_for_pipe_scanline_moving(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015211}
15212
15213void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15214{
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015215 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15216
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015217 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
15218 pipe_name(pipe));
15219
Ville Syrjälä5816d9c2017-11-29 14:54:11 +020015220 WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
15221 WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
15222 WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +020015223 WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
15224 WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015225
15226 I915_WRITE(PIPECONF(pipe), 0);
15227 POSTING_READ(PIPECONF(pipe));
15228
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015229 intel_wait_for_pipe_scanline_stopped(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015230
15231 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
15232 POSTING_READ(DPLL(pipe));
15233}
15234
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015235static void
15236intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
15237{
15238 struct intel_crtc *crtc;
Daniel Vetterfa555832012-10-10 23:14:00 +020015239
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015240 if (INTEL_GEN(dev_priv) >= 4)
15241 return;
Daniel Vetterfa555832012-10-10 23:14:00 +020015242
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015243 for_each_intel_crtc(&dev_priv->drm, crtc) {
15244 struct intel_plane *plane =
15245 to_intel_plane(crtc->base.primary);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015246 struct intel_crtc *plane_crtc;
15247 enum pipe pipe;
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015248
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015249 if (!plane->get_hw_state(plane, &pipe))
15250 continue;
15251
15252 if (pipe == crtc->pipe)
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015253 continue;
15254
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +030015255 DRM_DEBUG_KMS("[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
15256 plane->base.base.id, plane->base.name);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015257
15258 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15259 intel_plane_disable_noatomic(plane_crtc, plane);
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015260 }
Daniel Vetterfa555832012-10-10 23:14:00 +020015261}
15262
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015263static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15264{
15265 struct drm_device *dev = crtc->base.dev;
15266 struct intel_encoder *encoder;
15267
15268 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15269 return true;
15270
15271 return false;
15272}
15273
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015274static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15275{
15276 struct drm_device *dev = encoder->base.dev;
15277 struct intel_connector *connector;
15278
15279 for_each_connector_on_encoder(dev, &encoder->base, connector)
15280 return connector;
15281
15282 return NULL;
15283}
15284
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015285static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015286 enum pipe pch_transcoder)
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015287{
15288 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015289 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015290}
15291
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015292static void intel_sanitize_crtc(struct intel_crtc *crtc,
15293 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter24929352012-07-02 20:28:59 +020015294{
15295 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015296 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015297 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
15298 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015299
Daniel Vetter24929352012-07-02 20:28:59 +020015300 /* Clear any frame start delays used for debugging left by the BIOS */
Ville Syrjälä738a8142017-11-15 22:04:42 +020015301 if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020015302 i915_reg_t reg = PIPECONF(cpu_transcoder);
15303
15304 I915_WRITE(reg,
15305 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15306 }
Daniel Vetter24929352012-07-02 20:28:59 +020015307
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015308 if (crtc_state->base.active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015309 struct intel_plane *plane;
15310
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015311 /* Disable everything but the primary plane */
15312 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015313 const struct intel_plane_state *plane_state =
15314 to_intel_plane_state(plane->base.state);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015315
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015316 if (plane_state->base.visible &&
15317 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
15318 intel_plane_disable_noatomic(crtc, plane);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015319 }
Daniel Vetter96256042015-02-13 21:03:42 +010015320 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015321
Daniel Vetter24929352012-07-02 20:28:59 +020015322 /* Adjust the state of the output pipe according to whether we
15323 * have active connectors/encoders. */
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015324 if (crtc_state->base.active && !intel_crtc_has_encoders(crtc))
Ville Syrjäläda1d0e22017-06-01 17:36:14 +030015325 intel_crtc_disable_noatomic(&crtc->base, ctx);
Daniel Vetter24929352012-07-02 20:28:59 +020015326
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015327 if (crtc_state->base.active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015328 /*
15329 * We start out with underrun reporting disabled to avoid races.
15330 * For correct bookkeeping mark this on active crtcs.
15331 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015332 * Also on gmch platforms we dont have any hardware bits to
15333 * disable the underrun reporting. Which means we need to start
15334 * out with underrun reporting disabled also on inactive pipes,
15335 * since otherwise we'll complain about the garbage we read when
15336 * e.g. coming up after runtime pm.
15337 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015338 * No protection against concurrent access is required - at
15339 * worst a fifo underrun happens which also sets this to false.
15340 */
15341 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015342 /*
15343 * We track the PCH trancoder underrun reporting state
15344 * within the crtc. With crtc for pipe A housing the underrun
15345 * reporting state for PCH transcoder A, crtc for pipe B housing
15346 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15347 * and marking underrun reporting as disabled for the non-existing
15348 * PCH transcoders B and C would prevent enabling the south
15349 * error interrupt (see cpt_can_enable_serr_int()).
15350 */
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015351 if (has_pch_trancoder(dev_priv, crtc->pipe))
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015352 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010015353 }
Daniel Vetter24929352012-07-02 20:28:59 +020015354}
15355
15356static void intel_sanitize_encoder(struct intel_encoder *encoder)
15357{
Imre Deak70332ac2018-11-01 16:04:27 +020015358 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015359 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020015360
15361 /* We need to check both for a crtc link (meaning that the
15362 * encoder is active and trying to read from a pipe) and the
15363 * pipe itself being active. */
15364 bool has_active_crtc = encoder->base.crtc &&
15365 to_intel_crtc(encoder->base.crtc)->active;
15366
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015367 connector = intel_encoder_find_connector(encoder);
15368 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015369 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15370 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015371 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015372
15373 /* Connector is active, but has no active pipe. This is
15374 * fallout from our resume register restoring. Disable
15375 * the encoder manually again. */
15376 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015377 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15378
Daniel Vetter24929352012-07-02 20:28:59 +020015379 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15380 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015381 encoder->base.name);
Jani Nikulac84c6fe2018-10-16 15:41:34 +030015382 if (encoder->disable)
15383 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015384 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015385 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020015386 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015387 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015388
15389 /* Inconsistent output/port/pipe state happens presumably due to
15390 * a bug in one of the get_hw_state functions. Or someplace else
15391 * in our code, like the register restore mess on resume. Clamp
15392 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015393
15394 connector->base.dpms = DRM_MODE_DPMS_OFF;
15395 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015396 }
Maarten Lankhorstd6cae4a2018-05-16 10:50:38 +020015397
15398 /* notify opregion of the sanitized encoder state */
15399 intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
Imre Deak70332ac2018-11-01 16:04:27 +020015400
15401 if (INTEL_GEN(dev_priv) >= 11)
15402 icl_sanitize_encoder_pll_mapping(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015403}
15404
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015405void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015406{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015407 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015408
Imre Deak04098752014-02-18 00:02:16 +020015409 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15410 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015411 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020015412 }
15413}
15414
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015415void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020015416{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015417 /* This function can be called both from intel_modeset_setup_hw_state or
15418 * at a very early point in our resume sequence, where the power well
15419 * structures are not yet restored. Since this function is at a very
15420 * paranoid "someone might have enabled VGA while we were not looking"
15421 * level, just check if the power well is enabled instead of trying to
15422 * follow the "don't touch the power well if we don't need it" policy
15423 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015424 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015425 return;
15426
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015427 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020015428
15429 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015430}
15431
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015432/* FIXME read out full plane state for all planes */
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015433static void readout_plane_state(struct drm_i915_private *dev_priv)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015434{
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015435 struct intel_plane *plane;
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015436 struct intel_crtc *crtc;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015437
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015438 for_each_intel_plane(&dev_priv->drm, plane) {
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015439 struct intel_plane_state *plane_state =
15440 to_intel_plane_state(plane->base.state);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015441 struct intel_crtc_state *crtc_state;
15442 enum pipe pipe = PIPE_A;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020015443 bool visible;
15444
15445 visible = plane->get_hw_state(plane, &pipe);
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015446
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015447 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15448 crtc_state = to_intel_crtc_state(crtc->base.state);
15449
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015450 intel_set_plane_visible(crtc_state, plane_state, visible);
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +030015451
15452 DRM_DEBUG_KMS("[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
15453 plane->base.base.id, plane->base.name,
15454 enableddisabled(visible), pipe_name(pipe));
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015455 }
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015456
15457 for_each_intel_crtc(&dev_priv->drm, crtc) {
15458 struct intel_crtc_state *crtc_state =
15459 to_intel_crtc_state(crtc->base.state);
15460
15461 fixup_active_planes(crtc_state);
15462 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015463}
15464
Daniel Vetter30e984d2013-06-05 13:34:17 +020015465static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015466{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015467 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015468 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015469 struct intel_crtc *crtc;
15470 struct intel_encoder *encoder;
15471 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015472 struct drm_connector_list_iter conn_iter;
Daniel Vetter53589012013-06-05 13:34:16 +020015473 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015474
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015475 dev_priv->active_crtcs = 0;
15476
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015477 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015478 struct intel_crtc_state *crtc_state =
15479 to_intel_crtc_state(crtc->base.state);
Daniel Vetter3b117c82013-04-17 20:15:07 +020015480
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015481 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015482 memset(crtc_state, 0, sizeof(*crtc_state));
15483 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015484
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015485 crtc_state->base.active = crtc_state->base.enable =
15486 dev_priv->display.get_pipe_config(crtc, crtc_state);
15487
15488 crtc->base.enabled = crtc_state->base.enable;
15489 crtc->active = crtc_state->base.active;
15490
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015491 if (crtc_state->base.active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015492 dev_priv->active_crtcs |= 1 << crtc->pipe;
15493
Ville Syrjälä78108b72016-05-27 20:59:19 +030015494 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15495 crtc->base.base.id, crtc->base.name,
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015496 enableddisabled(crtc_state->base.active));
Daniel Vetter24929352012-07-02 20:28:59 +020015497 }
15498
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015499 readout_plane_state(dev_priv);
15500
Daniel Vetter53589012013-06-05 13:34:16 +020015501 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15502 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15503
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015504 pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
15505 &pll->state.hw_state);
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015506 pll->state.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015507 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015508 struct intel_crtc_state *crtc_state =
15509 to_intel_crtc_state(crtc->base.state);
15510
15511 if (crtc_state->base.active &&
15512 crtc_state->shared_dpll == pll)
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015513 pll->state.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015514 }
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015515 pll->active_mask = pll->state.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015516
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015517 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015518 pll->info->name, pll->state.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015519 }
15520
Damien Lespiaub2784e12014-08-05 11:29:37 +010015521 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015522 pipe = 0;
15523
15524 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015525 struct intel_crtc_state *crtc_state;
15526
Ville Syrjälä98187832016-10-31 22:37:10 +020015527 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015528 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015529
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015530 encoder->base.crtc = &crtc->base;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015531 encoder->get_config(encoder, crtc_state);
Daniel Vetter24929352012-07-02 20:28:59 +020015532 } else {
15533 encoder->base.crtc = NULL;
15534 }
15535
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015536 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015537 encoder->base.base.id, encoder->base.name,
15538 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015539 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015540 }
15541
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015542 drm_connector_list_iter_begin(dev, &conn_iter);
15543 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter24929352012-07-02 20:28:59 +020015544 if (connector->get_hw_state(connector)) {
15545 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015546
15547 encoder = connector->encoder;
15548 connector->base.encoder = &encoder->base;
15549
15550 if (encoder->base.crtc &&
15551 encoder->base.crtc->state->active) {
15552 /*
15553 * This has to be done during hardware readout
15554 * because anything calling .crtc_disable may
15555 * rely on the connector_mask being accurate.
15556 */
15557 encoder->base.crtc->state->connector_mask |=
Ville Syrjälä40560e22018-06-26 22:47:11 +030015558 drm_connector_mask(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015559 encoder->base.crtc->state->encoder_mask |=
Ville Syrjälä40560e22018-06-26 22:47:11 +030015560 drm_encoder_mask(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015561 }
15562
Daniel Vetter24929352012-07-02 20:28:59 +020015563 } else {
15564 connector->base.dpms = DRM_MODE_DPMS_OFF;
15565 connector->base.encoder = NULL;
15566 }
15567 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015568 connector->base.base.id, connector->base.name,
15569 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020015570 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015571 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015572
15573 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015574 struct intel_crtc_state *crtc_state =
15575 to_intel_crtc_state(crtc->base.state);
Ville Syrjäläd305e062017-08-30 21:57:03 +030015576 int min_cdclk = 0;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015577
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015578 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015579 if (crtc_state->base.active) {
15580 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
Ville Syrjäläbd4cd032018-04-26 19:30:15 +030015581 crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
15582 crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015583 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015584 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15585
15586 /*
15587 * The initial mode needs to be set in order to keep
15588 * the atomic core happy. It wants a valid mode if the
15589 * crtc's enabled, so we do the above call.
15590 *
Daniel Vetter7800fb62016-12-19 09:24:23 +010015591 * But we don't set all the derived state fully, hence
15592 * set a flag to indicate that a full recalculation is
15593 * needed on the next commit.
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015594 */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015595 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015596
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020015597 intel_crtc_compute_pixel_rate(crtc_state);
15598
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015599 if (dev_priv->display.modeset_calc_cdclk) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030015600 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015601 if (WARN_ON(min_cdclk < 0))
15602 min_cdclk = 0;
15603 }
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015604
Daniel Vetter5caa0fe2017-05-09 16:03:29 +020015605 drm_calc_timestamping_constants(&crtc->base,
15606 &crtc_state->base.adjusted_mode);
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020015607 update_scanline_offset(crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015608 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015609
Ville Syrjäläd305e062017-08-30 21:57:03 +030015610 dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030015611 dev_priv->min_voltage_level[crtc->pipe] =
15612 crtc_state->min_voltage_level;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015613
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015614 intel_pipe_config_sanity_check(dev_priv, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015615 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015616}
15617
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015618static void
15619get_encoder_power_domains(struct drm_i915_private *dev_priv)
15620{
15621 struct intel_encoder *encoder;
15622
15623 for_each_intel_encoder(&dev_priv->drm, encoder) {
15624 u64 get_domains;
15625 enum intel_display_power_domain domain;
Imre Deak52528052018-06-21 21:44:49 +030015626 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015627
15628 if (!encoder->get_power_domains)
15629 continue;
15630
Imre Deak52528052018-06-21 21:44:49 +030015631 /*
Imre Deakb79ebe72018-07-05 15:26:54 +030015632 * MST-primary and inactive encoders don't have a crtc state
15633 * and neither of these require any power domain references.
Imre Deak52528052018-06-21 21:44:49 +030015634 */
Imre Deakb79ebe72018-07-05 15:26:54 +030015635 if (!encoder->base.crtc)
15636 continue;
Imre Deak52528052018-06-21 21:44:49 +030015637
Imre Deakb79ebe72018-07-05 15:26:54 +030015638 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
Imre Deak52528052018-06-21 21:44:49 +030015639 get_domains = encoder->get_power_domains(encoder, crtc_state);
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015640 for_each_power_domain(domain, get_domains)
15641 intel_display_power_get(dev_priv, domain);
15642 }
15643}
15644
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015645static void intel_early_display_was(struct drm_i915_private *dev_priv)
15646{
15647 /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
15648 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
15649 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
15650 DARBF_GATING_DIS);
15651
15652 if (IS_HASWELL(dev_priv)) {
15653 /*
15654 * WaRsPkgCStateDisplayPMReq:hsw
15655 * System hang if this isn't done before disabling all planes!
15656 */
15657 I915_WRITE(CHICKEN_PAR1_1,
15658 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
15659 }
15660}
15661
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015662/* Scan out the current hw modeset state,
15663 * and sanitizes it to the current state
15664 */
15665static void
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015666intel_modeset_setup_hw_state(struct drm_device *dev,
15667 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015668{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015669 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015670 struct intel_crtc *crtc;
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015671 struct intel_crtc_state *crtc_state;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015672 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015673 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015674
Imre Deak2cd9a682018-08-16 15:37:57 +030015675 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
15676
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015677 intel_early_display_was(dev_priv);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015678 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015679
15680 /* HW state is read out, now we need to sanitize this mess. */
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015681 get_encoder_power_domains(dev_priv);
15682
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015683 /*
15684 * intel_sanitize_plane_mapping() may need to do vblank
15685 * waits, so we need vblank interrupts restored beforehand.
15686 */
15687 for_each_intel_crtc(&dev_priv->drm, crtc) {
15688 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015689
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015690 if (crtc->base.state->active)
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015691 drm_crtc_vblank_on(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015692 }
15693
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015694 intel_sanitize_plane_mapping(dev_priv);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015695
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015696 for_each_intel_encoder(dev, encoder)
15697 intel_sanitize_encoder(encoder);
15698
15699 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015700 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015701 intel_sanitize_crtc(crtc, ctx);
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015702 intel_dump_pipe_config(crtc, crtc_state,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015703 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015704 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015705
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015706 intel_modeset_update_connector_atomic_state(dev);
15707
Daniel Vetter35c95372013-07-17 06:55:04 +020015708 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15709 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15710
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015711 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015712 continue;
15713
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015714 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
15715 pll->info->name);
Daniel Vetter35c95372013-07-17 06:55:04 +020015716
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015717 pll->info->funcs->disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015718 pll->on = false;
15719 }
15720
Ville Syrjälä04548cb2017-04-21 21:14:29 +030015721 if (IS_G4X(dev_priv)) {
15722 g4x_wm_get_hw_state(dev);
15723 g4x_wm_sanitize(dev_priv);
15724 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015725 vlv_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015726 vlv_wm_sanitize(dev_priv);
Rodrigo Vivia029fa42017-08-09 13:52:48 -070015727 } else if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat30789992014-11-04 17:06:45 +000015728 skl_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015729 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015730 ilk_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015731 }
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015732
15733 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020015734 u64 put_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015735
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015736 crtc_state = to_intel_crtc_state(crtc->base.state);
15737 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015738 if (WARN_ON(put_domains))
15739 modeset_put_power_domains(dev_priv, put_domains);
15740 }
Imre Deak2cd9a682018-08-16 15:37:57 +030015741
15742 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015743
15744 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015745}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015746
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015747void intel_display_resume(struct drm_device *dev)
15748{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015749 struct drm_i915_private *dev_priv = to_i915(dev);
15750 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15751 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015752 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015753
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015754 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030015755 if (state)
15756 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015757
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015758 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015759
Maarten Lankhorst73974892016-08-05 23:28:27 +030015760 while (1) {
15761 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15762 if (ret != -EDEADLK)
15763 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015764
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015765 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015766 }
15767
Maarten Lankhorst73974892016-08-05 23:28:27 +030015768 if (!ret)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010015769 ret = __intel_display_resume(dev, state, &ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +030015770
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +053015771 intel_enable_ipc(dev_priv);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015772 drm_modeset_drop_locks(&ctx);
15773 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015774
Chris Wilson08536952016-10-14 13:18:18 +010015775 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015776 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson3c5e37f2017-01-15 12:58:25 +000015777 if (state)
15778 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015779}
15780
Manasi Navare886c6b82017-10-26 14:52:00 -070015781static void intel_hpd_poll_fini(struct drm_device *dev)
15782{
15783 struct intel_connector *connector;
15784 struct drm_connector_list_iter conn_iter;
15785
Chris Wilson448aa912017-11-28 11:01:47 +000015786 /* Kill all the work that may have been queued by hpd. */
Manasi Navare886c6b82017-10-26 14:52:00 -070015787 drm_connector_list_iter_begin(dev, &conn_iter);
15788 for_each_intel_connector_iter(connector, &conn_iter) {
15789 if (connector->modeset_retry_work.func)
15790 cancel_work_sync(&connector->modeset_retry_work);
Ramalingam Cd3dacc72018-10-29 15:15:46 +053015791 if (connector->hdcp.shim) {
15792 cancel_delayed_work_sync(&connector->hdcp.check_work);
15793 cancel_work_sync(&connector->hdcp.prop_work);
Sean Paulee5e5e72018-01-08 14:55:39 -050015794 }
Manasi Navare886c6b82017-10-26 14:52:00 -070015795 }
15796 drm_connector_list_iter_end(&conn_iter);
15797}
15798
Jesse Barnes79e53942008-11-07 14:24:08 -080015799void intel_modeset_cleanup(struct drm_device *dev)
15800{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015801 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070015802
Chris Wilson8bcf9f72018-07-10 10:44:20 +010015803 flush_workqueue(dev_priv->modeset_wq);
15804
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015805 flush_work(&dev_priv->atomic_helper.free_work);
15806 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15807
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015808 /*
15809 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015810 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015811 * experience fancy races otherwise.
15812 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015813 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015814
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015815 /*
15816 * Due to the hpd irq storm handling the hotplug work can re-arm the
15817 * poll handlers. Hence disable polling after hpd handling is shut down.
15818 */
Manasi Navare886c6b82017-10-26 14:52:00 -070015819 intel_hpd_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015820
Daniel Vetter4f256d82017-07-15 00:46:55 +020015821 /* poll work can call into fbdev, hence clean that up afterwards */
15822 intel_fbdev_fini(dev_priv);
15823
Jesse Barnes723bfd72010-10-07 16:01:13 -070015824 intel_unregister_dsm_handler();
15825
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015826 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015827
Chris Wilson1630fe72011-07-08 12:22:42 +010015828 /* flush any delayed tasks or pending work */
15829 flush_scheduled_work();
15830
Jesse Barnes79e53942008-11-07 14:24:08 -080015831 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015832
Chris Wilson1ee8da62016-05-12 12:43:23 +010015833 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015834
Tvrtko Ursulin40196442016-12-01 14:16:42 +000015835 intel_teardown_gmbus(dev_priv);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020015836
15837 destroy_workqueue(dev_priv->modeset_wq);
Jesse Barnes79e53942008-11-07 14:24:08 -080015838}
15839
Dave Airlie28d52042009-09-21 14:33:58 +100015840/*
15841 * set vga decode state - true == enable VGA decode
15842 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015843int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100015844{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015845 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015846 u16 gmch_ctrl;
15847
Chris Wilson75fa0412014-02-07 18:37:02 -020015848 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15849 DRM_ERROR("failed to read control word\n");
15850 return -EIO;
15851 }
15852
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015853 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15854 return 0;
15855
Dave Airlie28d52042009-09-21 14:33:58 +100015856 if (state)
15857 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15858 else
15859 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015860
15861 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15862 DRM_ERROR("failed to write control word\n");
15863 return -EIO;
15864 }
15865
Dave Airlie28d52042009-09-21 14:33:58 +100015866 return 0;
15867}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015868
Chris Wilson98a2f412016-10-12 10:05:18 +010015869#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15870
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015871struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015872
15873 u32 power_well_driver;
15874
Chris Wilson63b66e52013-08-08 15:12:06 +020015875 int num_transcoders;
15876
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015877 struct intel_cursor_error_state {
15878 u32 control;
15879 u32 position;
15880 u32 base;
15881 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015882 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015883
15884 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015885 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015886 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015887 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015888 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015889
15890 struct intel_plane_error_state {
15891 u32 control;
15892 u32 stride;
15893 u32 size;
15894 u32 pos;
15895 u32 addr;
15896 u32 surface;
15897 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015898 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015899
15900 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015901 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015902 enum transcoder cpu_transcoder;
15903
15904 u32 conf;
15905
15906 u32 htotal;
15907 u32 hblank;
15908 u32 hsync;
15909 u32 vtotal;
15910 u32 vblank;
15911 u32 vsync;
15912 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015913};
15914
15915struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010015916intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015917{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015918 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015919 int transcoders[] = {
15920 TRANSCODER_A,
15921 TRANSCODER_B,
15922 TRANSCODER_C,
15923 TRANSCODER_EDP,
15924 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015925 int i;
15926
Chris Wilsonc0336662016-05-06 15:40:21 +010015927 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020015928 return NULL;
15929
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015930 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015931 if (error == NULL)
15932 return NULL;
15933
Chris Wilsonc0336662016-05-06 15:40:21 +010015934 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Imre Deak75e39682018-08-06 12:58:39 +030015935 error->power_well_driver = I915_READ(HSW_PWR_WELL_CTL2);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015936
Damien Lespiau055e3932014-08-18 13:49:10 +010015937 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015938 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015939 __intel_display_power_is_enabled(dev_priv,
15940 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015941 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015942 continue;
15943
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015944 error->cursor[i].control = I915_READ(CURCNTR(i));
15945 error->cursor[i].position = I915_READ(CURPOS(i));
15946 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015947
15948 error->plane[i].control = I915_READ(DSPCNTR(i));
15949 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015950 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015951 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015952 error->plane[i].pos = I915_READ(DSPPOS(i));
15953 }
Chris Wilsonc0336662016-05-06 15:40:21 +010015954 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030015955 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015956 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015957 error->plane[i].surface = I915_READ(DSPSURF(i));
15958 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15959 }
15960
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015961 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015962
Chris Wilsonc0336662016-05-06 15:40:21 +010015963 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030015964 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015965 }
15966
Jani Nikula4d1de972016-03-18 17:05:42 +020015967 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010015968 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030015969 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020015970 error->num_transcoders++; /* Account for eDP. */
15971
15972 for (i = 0; i < error->num_transcoders; i++) {
15973 enum transcoder cpu_transcoder = transcoders[i];
15974
Imre Deakddf9c532013-11-27 22:02:02 +020015975 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015976 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015977 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015978 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015979 continue;
15980
Chris Wilson63b66e52013-08-08 15:12:06 +020015981 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15982
15983 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15984 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15985 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15986 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15987 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15988 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15989 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015990 }
15991
15992 return error;
15993}
15994
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015995#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15996
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015997void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015998intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015999 struct intel_display_error_state *error)
16000{
Chris Wilson5a4c6f12017-02-14 16:46:11 +000016001 struct drm_i915_private *dev_priv = m->i915;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016002 int i;
16003
Chris Wilson63b66e52013-08-08 15:12:06 +020016004 if (!error)
16005 return;
16006
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016007 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010016008 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016009 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016010 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016011 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016012 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016013 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016014 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016015 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016016 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016017
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016018 err_printf(m, "Plane [%d]:\n", i);
16019 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16020 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000016021 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016022 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16023 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016024 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010016025 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016026 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000016027 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016028 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16029 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016030 }
16031
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016032 err_printf(m, "Cursor [%d]:\n", i);
16033 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16034 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16035 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016036 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016037
16038 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016039 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016040 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016041 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016042 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016043 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16044 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16045 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16046 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16047 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16048 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16049 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16050 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016051}
Chris Wilson98a2f412016-10-12 10:05:18 +010016052
16053#endif