blob: 4c176e13e9bb5021a886f3a40c0674d2a7b900b5 [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
3367 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003368}
3369
Ville Syrjäläed150302017-11-17 21:19:10 +02003370static void i9xx_disable_plane(struct intel_plane *plane,
3371 struct intel_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003372{
Ville Syrjäläed150302017-11-17 21:19:10 +02003373 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3374 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003375 unsigned long irqflags;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003376
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003377 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3378
Ville Syrjäläed150302017-11-17 21:19:10 +02003379 I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
3380 if (INTEL_GEN(dev_priv) >= 4)
3381 I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003382 else
Ville Syrjäläed150302017-11-17 21:19:10 +02003383 I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003384
3385 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003386}
3387
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003388static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
3389 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003390{
Ville Syrjäläed150302017-11-17 21:19:10 +02003391 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003392 enum intel_display_power_domain power_domain;
Ville Syrjäläed150302017-11-17 21:19:10 +02003393 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003394 bool ret;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003395 u32 val;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003396
3397 /*
3398 * Not 100% correct for planes that can move between pipes,
3399 * but that's only the case for gen2-4 which don't have any
3400 * display power wells.
3401 */
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003402 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003403 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
3404 return false;
3405
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003406 val = I915_READ(DSPCNTR(i9xx_plane));
3407
3408 ret = val & DISPLAY_PLANE_ENABLE;
3409
3410 if (INTEL_GEN(dev_priv) >= 5)
3411 *pipe = plane->pipe;
3412 else
3413 *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
3414 DISPPLANE_SEL_PIPE_SHIFT;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003415
3416 intel_display_power_put(dev_priv, power_domain);
3417
3418 return ret;
3419}
3420
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003421static u32
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003422intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
Damien Lespiaub3218032015-02-27 11:15:18 +00003423{
Ben Widawsky2f075562017-03-24 14:29:48 -07003424 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003425 return 64;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003426 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003427 return intel_tile_width_bytes(fb, color_plane);
Damien Lespiaub3218032015-02-27 11:15:18 +00003428}
3429
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003430static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3431{
3432 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003433 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003434
3435 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3436 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3437 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003438}
3439
Chandra Kondurua1b22782015-04-07 15:28:45 -07003440/*
3441 * This function detaches (aka. unbinds) unused scalers in hardware
3442 */
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003443static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003444{
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3446 const struct intel_crtc_scaler_state *scaler_state =
3447 &crtc_state->scaler_state;
Chandra Kondurua1b22782015-04-07 15:28:45 -07003448 int i;
3449
Chandra Kondurua1b22782015-04-07 15:28:45 -07003450 /* loop through and disable scalers that aren't in use */
3451 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003452 if (!scaler_state->scalers[i].in_use)
3453 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003454 }
3455}
3456
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003457u32 skl_plane_stride(const struct intel_plane_state *plane_state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003458 int color_plane)
Ville Syrjäläd2196772016-01-28 18:33:11 +02003459{
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003460 const struct drm_framebuffer *fb = plane_state->base.fb;
3461 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003462 u32 stride = plane_state->color_plane[color_plane].stride;
Ville Syrjälä1b500532017-03-07 21:42:08 +02003463
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003464 if (color_plane >= fb->format->num_planes)
Ville Syrjälä1b500532017-03-07 21:42:08 +02003465 return 0;
3466
Ville Syrjäläd2196772016-01-28 18:33:11 +02003467 /*
3468 * The stride is either expressed as a multiple of 64 bytes chunks for
3469 * linear buffers or in number of tiles for tiled buffers.
3470 */
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003471 if (drm_rotation_90_or_270(rotation))
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003472 stride /= intel_tile_height(fb, color_plane);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003473 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003474 stride /= intel_fb_stride_alignment(fb, color_plane);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003475
3476 return stride;
3477}
3478
Ville Syrjälä2e881262017-03-17 23:17:56 +02003479static u32 skl_plane_ctl_format(uint32_t pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -07003480{
Chandra Konduru6156a452015-04-27 13:48:39 -07003481 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003482 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003483 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003484 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003485 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003486 case DRM_FORMAT_XBGR8888:
James Ausmus4036c782017-11-13 10:11:28 -08003487 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003488 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003489 case DRM_FORMAT_XRGB8888:
Chandra Konduru6156a452015-04-27 13:48:39 -07003490 case DRM_FORMAT_ARGB8888:
James Ausmus4036c782017-11-13 10:11:28 -08003491 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003492 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003493 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003494 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003495 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003496 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003497 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003498 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003499 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003500 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003501 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003502 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003503 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru77224cd2018-04-09 09:11:13 +05303504 case DRM_FORMAT_NV12:
3505 return PLANE_CTL_FORMAT_NV12;
Chandra Konduru6156a452015-04-27 13:48:39 -07003506 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003507 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003508 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003509
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003510 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003511}
3512
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003513static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
James Ausmus4036c782017-11-13 10:11:28 -08003514{
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003515 if (!plane_state->base.fb->format->has_alpha)
3516 return PLANE_CTL_ALPHA_DISABLE;
3517
3518 switch (plane_state->base.pixel_blend_mode) {
3519 case DRM_MODE_BLEND_PIXEL_NONE:
3520 return PLANE_CTL_ALPHA_DISABLE;
3521 case DRM_MODE_BLEND_PREMULTI:
James Ausmus4036c782017-11-13 10:11:28 -08003522 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003523 case DRM_MODE_BLEND_COVERAGE:
3524 return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
James Ausmus4036c782017-11-13 10:11:28 -08003525 default:
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003526 MISSING_CASE(plane_state->base.pixel_blend_mode);
James Ausmus4036c782017-11-13 10:11:28 -08003527 return PLANE_CTL_ALPHA_DISABLE;
3528 }
3529}
3530
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003531static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
James Ausmus4036c782017-11-13 10:11:28 -08003532{
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003533 if (!plane_state->base.fb->format->has_alpha)
3534 return PLANE_COLOR_ALPHA_DISABLE;
3535
3536 switch (plane_state->base.pixel_blend_mode) {
3537 case DRM_MODE_BLEND_PIXEL_NONE:
3538 return PLANE_COLOR_ALPHA_DISABLE;
3539 case DRM_MODE_BLEND_PREMULTI:
James Ausmus4036c782017-11-13 10:11:28 -08003540 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003541 case DRM_MODE_BLEND_COVERAGE:
3542 return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
James Ausmus4036c782017-11-13 10:11:28 -08003543 default:
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003544 MISSING_CASE(plane_state->base.pixel_blend_mode);
James Ausmus4036c782017-11-13 10:11:28 -08003545 return PLANE_COLOR_ALPHA_DISABLE;
3546 }
3547}
3548
Ville Syrjälä2e881262017-03-17 23:17:56 +02003549static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
Chandra Konduru6156a452015-04-27 13:48:39 -07003550{
Chandra Konduru6156a452015-04-27 13:48:39 -07003551 switch (fb_modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07003552 case DRM_FORMAT_MOD_LINEAR:
Chandra Konduru6156a452015-04-27 13:48:39 -07003553 break;
3554 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003555 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003556 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003557 return PLANE_CTL_TILED_Y;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003558 case I915_FORMAT_MOD_Y_TILED_CCS:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07003559 return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003560 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003561 return PLANE_CTL_TILED_YF;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003562 case I915_FORMAT_MOD_Yf_TILED_CCS:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07003563 return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003564 default:
3565 MISSING_CASE(fb_modifier);
3566 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003567
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003568 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003569}
3570
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003571static u32 skl_plane_ctl_rotate(unsigned int rotate)
Chandra Konduru6156a452015-04-27 13:48:39 -07003572{
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003573 switch (rotate) {
Robert Fossc2c446a2017-05-19 16:50:17 -04003574 case DRM_MODE_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003575 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303576 /*
Robert Fossc2c446a2017-05-19 16:50:17 -04003577 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
Sonika Jindal1e8df162015-05-20 13:40:48 +05303578 * while i915 HW rotation is clockwise, thats why this swapping.
3579 */
Robert Fossc2c446a2017-05-19 16:50:17 -04003580 case DRM_MODE_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303581 return PLANE_CTL_ROTATE_270;
Robert Fossc2c446a2017-05-19 16:50:17 -04003582 case DRM_MODE_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003583 return PLANE_CTL_ROTATE_180;
Robert Fossc2c446a2017-05-19 16:50:17 -04003584 case DRM_MODE_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303585 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003586 default:
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003587 MISSING_CASE(rotate);
3588 }
3589
3590 return 0;
3591}
3592
3593static u32 cnl_plane_ctl_flip(unsigned int reflect)
3594{
3595 switch (reflect) {
3596 case 0:
3597 break;
3598 case DRM_MODE_REFLECT_X:
3599 return PLANE_CTL_FLIP_HORIZONTAL;
3600 case DRM_MODE_REFLECT_Y:
3601 default:
3602 MISSING_CASE(reflect);
Chandra Konduru6156a452015-04-27 13:48:39 -07003603 }
3604
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003605 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003606}
3607
Ville Syrjälä2e881262017-03-17 23:17:56 +02003608u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3609 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003610{
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003611 struct drm_i915_private *dev_priv =
3612 to_i915(plane_state->base.plane->dev);
3613 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003614 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä2e881262017-03-17 23:17:56 +02003615 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003616 u32 plane_ctl;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003617
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003618 plane_ctl = PLANE_CTL_ENABLE;
3619
James Ausmus4036c782017-11-13 10:11:28 -08003620 if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003621 plane_ctl |= skl_plane_ctl_alpha(plane_state);
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003622 plane_ctl |=
3623 PLANE_CTL_PIPE_GAMMA_ENABLE |
3624 PLANE_CTL_PIPE_CSC_ENABLE |
3625 PLANE_CTL_PLANE_GAMMA_DISABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003626
3627 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3628 plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003629
3630 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3631 plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003632 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003633
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003634 plane_ctl |= skl_plane_ctl_format(fb->format->format);
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003635 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003636 plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
3637
3638 if (INTEL_GEN(dev_priv) >= 10)
3639 plane_ctl |= cnl_plane_ctl_flip(rotation &
3640 DRM_MODE_REFLECT_MASK);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003641
Ville Syrjälä2e881262017-03-17 23:17:56 +02003642 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3643 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3644 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3645 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3646
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003647 return plane_ctl;
3648}
3649
James Ausmus4036c782017-11-13 10:11:28 -08003650u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
3651 const struct intel_plane_state *plane_state)
3652{
James Ausmus077ef1f2018-03-28 14:57:56 -07003653 struct drm_i915_private *dev_priv =
3654 to_i915(plane_state->base.plane->dev);
James Ausmus4036c782017-11-13 10:11:28 -08003655 const struct drm_framebuffer *fb = plane_state->base.fb;
Uma Shankarbfe60a02018-11-02 00:40:20 +05303656 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
James Ausmus4036c782017-11-13 10:11:28 -08003657 u32 plane_color_ctl = 0;
3658
James Ausmus077ef1f2018-03-28 14:57:56 -07003659 if (INTEL_GEN(dev_priv) < 11) {
3660 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
3661 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
3662 }
James Ausmus4036c782017-11-13 10:11:28 -08003663 plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003664 plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
James Ausmus4036c782017-11-13 10:11:28 -08003665
Uma Shankarbfe60a02018-11-02 00:40:20 +05303666 if (fb->format->is_yuv && !icl_is_hdr_plane(plane)) {
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003667 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3668 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
3669 else
3670 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003671
3672 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3673 plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
Uma Shankarbfe60a02018-11-02 00:40:20 +05303674 } else if (fb->format->is_yuv) {
3675 plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003676 }
Ville Syrjälä012d79e2018-05-21 21:56:12 +03003677
James Ausmus4036c782017-11-13 10:11:28 -08003678 return plane_color_ctl;
3679}
3680
Maarten Lankhorst73974892016-08-05 23:28:27 +03003681static int
3682__intel_display_resume(struct drm_device *dev,
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003683 struct drm_atomic_state *state,
3684 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorst73974892016-08-05 23:28:27 +03003685{
3686 struct drm_crtc_state *crtc_state;
3687 struct drm_crtc *crtc;
3688 int i, ret;
3689
Ville Syrjäläaecd36b2017-06-01 17:36:13 +03003690 intel_modeset_setup_hw_state(dev, ctx);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003691 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003692
3693 if (!state)
3694 return 0;
3695
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01003696 /*
3697 * We've duplicated the state, pointers to the old state are invalid.
3698 *
3699 * Don't attempt to use the old state until we commit the duplicated state.
3700 */
3701 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst73974892016-08-05 23:28:27 +03003702 /*
3703 * Force recalculation even if we restore
3704 * current state. With fast modeset this may not result
3705 * in a modeset when the state is compatible.
3706 */
3707 crtc_state->mode_changed = true;
3708 }
3709
3710 /* ignore any reset values/BIOS leftovers in the WM registers */
Ville Syrjälä602ae832017-03-02 19:15:02 +02003711 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3712 to_intel_atomic_state(state)->skip_intermediate_wm = true;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003713
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003714 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003715
3716 WARN_ON(ret == -EDEADLK);
3717 return ret;
3718}
3719
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003720static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3721{
Ville Syrjäläae981042016-08-05 23:28:30 +03003722 return intel_has_gpu_reset(dev_priv) &&
3723 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003724}
3725
Chris Wilsonc0336662016-05-06 15:40:21 +01003726void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003727{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003728 struct drm_device *dev = &dev_priv->drm;
3729 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3730 struct drm_atomic_state *state;
3731 int ret;
3732
Daniel Vetterce87ea12017-07-19 14:54:55 +02003733 /* reset doesn't touch the display */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00003734 if (!i915_modparams.force_reset_modeset_test &&
Daniel Vetterce87ea12017-07-19 14:54:55 +02003735 !gpu_reset_clobbers_display(dev_priv))
3736 return;
3737
Daniel Vetter9db529a2017-08-08 10:08:28 +02003738 /* We have a modeset vs reset deadlock, defensively unbreak it. */
3739 set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
3740 wake_up_all(&dev_priv->gpu_error.wait_queue);
3741
3742 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
3743 DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
3744 i915_gem_set_wedged(dev_priv);
3745 }
Daniel Vetter97154ec2017-08-08 10:08:26 +02003746
Maarten Lankhorst73974892016-08-05 23:28:27 +03003747 /*
3748 * Need mode_config.mutex so that we don't
3749 * trample ongoing ->detect() and whatnot.
3750 */
3751 mutex_lock(&dev->mode_config.mutex);
3752 drm_modeset_acquire_init(ctx, 0);
3753 while (1) {
3754 ret = drm_modeset_lock_all_ctx(dev, ctx);
3755 if (ret != -EDEADLK)
3756 break;
3757
3758 drm_modeset_backoff(ctx);
3759 }
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003760 /*
3761 * Disabling the crtcs gracefully seems nicer. Also the
3762 * g33 docs say we should at least disable all the planes.
3763 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003764 state = drm_atomic_helper_duplicate_state(dev, ctx);
3765 if (IS_ERR(state)) {
3766 ret = PTR_ERR(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003767 DRM_ERROR("Duplicating state failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003768 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003769 }
3770
3771 ret = drm_atomic_helper_disable_all(dev, ctx);
3772 if (ret) {
3773 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003774 drm_atomic_state_put(state);
3775 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003776 }
3777
3778 dev_priv->modeset_restore_state = state;
3779 state->acquire_ctx = ctx;
Ville Syrjälä75147472014-11-24 18:28:11 +02003780}
3781
Chris Wilsonc0336662016-05-06 15:40:21 +01003782void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003783{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003784 struct drm_device *dev = &dev_priv->drm;
3785 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
Chris Wilson40da1d32018-04-05 13:37:14 +01003786 struct drm_atomic_state *state;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003787 int ret;
3788
Daniel Vetterce87ea12017-07-19 14:54:55 +02003789 /* reset doesn't touch the display */
Chris Wilson40da1d32018-04-05 13:37:14 +01003790 if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
Daniel Vetterce87ea12017-07-19 14:54:55 +02003791 return;
3792
Chris Wilson40da1d32018-04-05 13:37:14 +01003793 state = fetch_and_zero(&dev_priv->modeset_restore_state);
Daniel Vetterce87ea12017-07-19 14:54:55 +02003794 if (!state)
3795 goto unlock;
3796
Ville Syrjälä75147472014-11-24 18:28:11 +02003797 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003798 if (!gpu_reset_clobbers_display(dev_priv)) {
Daniel Vetterce87ea12017-07-19 14:54:55 +02003799 /* for testing only restore the display */
3800 ret = __intel_display_resume(dev, state, ctx);
Chris Wilson942d5d02017-08-28 11:46:04 +01003801 if (ret)
3802 DRM_ERROR("Restoring old state failed with %i\n", ret);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003803 } else {
3804 /*
3805 * The display has been reset as well,
3806 * so need a full re-initialization.
3807 */
3808 intel_runtime_pm_disable_interrupts(dev_priv);
3809 intel_runtime_pm_enable_interrupts(dev_priv);
3810
Imre Deak51f59202016-09-14 13:04:13 +03003811 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003812 intel_modeset_init_hw(dev);
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02003813 intel_init_clock_gating(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003814
3815 spin_lock_irq(&dev_priv->irq_lock);
3816 if (dev_priv->display.hpd_irq_setup)
3817 dev_priv->display.hpd_irq_setup(dev_priv);
3818 spin_unlock_irq(&dev_priv->irq_lock);
3819
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003820 ret = __intel_display_resume(dev, state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003821 if (ret)
3822 DRM_ERROR("Restoring old state failed with %i\n", ret);
3823
3824 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003825 }
3826
Daniel Vetterce87ea12017-07-19 14:54:55 +02003827 drm_atomic_state_put(state);
3828unlock:
Maarten Lankhorst73974892016-08-05 23:28:27 +03003829 drm_modeset_drop_locks(ctx);
3830 drm_modeset_acquire_fini(ctx);
3831 mutex_unlock(&dev->mode_config.mutex);
Daniel Vetter9db529a2017-08-08 10:08:28 +02003832
3833 clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
Ville Syrjälä75147472014-11-24 18:28:11 +02003834}
3835
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003836static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
3837 const struct intel_crtc_state *new_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003838{
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003839 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003840 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003841
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003842 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003843 crtc->base.mode = new_crtc_state->base.mode;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003844
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003845 /*
3846 * Update pipe size and adjust fitter if needed: the reason for this is
3847 * that in compute_mode_changes we check the native mode (not the pfit
3848 * mode) to see if we can flip rather than do a full mode set. In the
3849 * fastboot case, we'll flip, but if we don't update the pipesrc and
3850 * pfit state, we'll end up with a big fb scanned out into the wrong
3851 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003852 */
3853
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003854 I915_WRITE(PIPESRC(crtc->pipe),
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003855 ((new_crtc_state->pipe_src_w - 1) << 16) |
3856 (new_crtc_state->pipe_src_h - 1));
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003857
3858 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003859 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003860 skl_detach_scalers(new_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003861
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003862 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003863 skylake_pfit_enable(new_crtc_state);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003864 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003865 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003866 ironlake_pfit_enable(new_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003867 else if (old_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003868 ironlake_pfit_disable(old_crtc_state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003869 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003870}
3871
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003872static void intel_fdi_normal_train(struct intel_crtc *crtc)
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003873{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003874 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003875 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003876 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003877 i915_reg_t reg;
3878 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003879
3880 /* enable normal train */
3881 reg = FDI_TX_CTL(pipe);
3882 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003883 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003884 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3885 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003886 } else {
3887 temp &= ~FDI_LINK_TRAIN_NONE;
3888 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003889 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003890 I915_WRITE(reg, temp);
3891
3892 reg = FDI_RX_CTL(pipe);
3893 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003894 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003895 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3896 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3897 } else {
3898 temp &= ~FDI_LINK_TRAIN_NONE;
3899 temp |= FDI_LINK_TRAIN_NONE;
3900 }
3901 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3902
3903 /* wait one idle pattern time */
3904 POSTING_READ(reg);
3905 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003906
3907 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003908 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003909 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3910 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003911}
3912
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003913/* The FDI link training functions for ILK/Ibexpeak. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003914static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3915 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003916{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003917 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003918 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003919 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003920 i915_reg_t reg;
3921 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003922
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003923 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003924 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003925
Adam Jacksone1a44742010-06-25 15:32:14 -04003926 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3927 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003928 reg = FDI_RX_IMR(pipe);
3929 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003930 temp &= ~FDI_RX_SYMBOL_LOCK;
3931 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003932 I915_WRITE(reg, temp);
3933 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003934 udelay(150);
3935
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003936 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003937 reg = FDI_TX_CTL(pipe);
3938 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003939 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003940 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003941 temp &= ~FDI_LINK_TRAIN_NONE;
3942 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003943 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003944
Chris Wilson5eddb702010-09-11 13:48:45 +01003945 reg = FDI_RX_CTL(pipe);
3946 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003947 temp &= ~FDI_LINK_TRAIN_NONE;
3948 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003949 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3950
3951 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003952 udelay(150);
3953
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003954 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003955 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3956 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3957 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003958
Chris Wilson5eddb702010-09-11 13:48:45 +01003959 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003960 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003961 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003962 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3963
3964 if ((temp & FDI_RX_BIT_LOCK)) {
3965 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003966 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003967 break;
3968 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003969 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003970 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003971 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003972
3973 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003974 reg = FDI_TX_CTL(pipe);
3975 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003976 temp &= ~FDI_LINK_TRAIN_NONE;
3977 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003978 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003979
Chris Wilson5eddb702010-09-11 13:48:45 +01003980 reg = FDI_RX_CTL(pipe);
3981 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003982 temp &= ~FDI_LINK_TRAIN_NONE;
3983 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003984 I915_WRITE(reg, temp);
3985
3986 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003987 udelay(150);
3988
Chris Wilson5eddb702010-09-11 13:48:45 +01003989 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003990 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003991 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003992 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3993
3994 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003995 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003996 DRM_DEBUG_KMS("FDI train 2 done.\n");
3997 break;
3998 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003999 }
Adam Jacksone1a44742010-06-25 15:32:14 -04004000 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01004001 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004002
4003 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004004
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004005}
4006
Akshay Joshi0206e352011-08-16 15:34:10 -04004007static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004008 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
4009 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
4010 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
4011 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
4012};
4013
4014/* The FDI link training functions for SNB/Cougarpoint. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004015static void gen6_fdi_link_train(struct intel_crtc *crtc,
4016 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004017{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004018 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004019 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004020 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004021 i915_reg_t reg;
4022 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004023
Adam Jacksone1a44742010-06-25 15:32:14 -04004024 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4025 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01004026 reg = FDI_RX_IMR(pipe);
4027 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004028 temp &= ~FDI_RX_SYMBOL_LOCK;
4029 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01004030 I915_WRITE(reg, temp);
4031
4032 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004033 udelay(150);
4034
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004035 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01004036 reg = FDI_TX_CTL(pipe);
4037 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004038 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004039 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004040 temp &= ~FDI_LINK_TRAIN_NONE;
4041 temp |= FDI_LINK_TRAIN_PATTERN_1;
4042 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4043 /* SNB-B */
4044 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01004045 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004046
Daniel Vetterd74cf322012-10-26 10:58:13 +02004047 I915_WRITE(FDI_RX_MISC(pipe),
4048 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4049
Chris Wilson5eddb702010-09-11 13:48:45 +01004050 reg = FDI_RX_CTL(pipe);
4051 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004052 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004053 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4054 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4055 } else {
4056 temp &= ~FDI_LINK_TRAIN_NONE;
4057 temp |= FDI_LINK_TRAIN_PATTERN_1;
4058 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004059 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4060
4061 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004062 udelay(150);
4063
Akshay Joshi0206e352011-08-16 15:34:10 -04004064 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004065 reg = FDI_TX_CTL(pipe);
4066 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004067 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4068 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004069 I915_WRITE(reg, temp);
4070
4071 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004072 udelay(500);
4073
Sean Paulfa37d392012-03-02 12:53:39 -05004074 for (retry = 0; retry < 5; retry++) {
4075 reg = FDI_RX_IIR(pipe);
4076 temp = I915_READ(reg);
4077 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4078 if (temp & FDI_RX_BIT_LOCK) {
4079 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4080 DRM_DEBUG_KMS("FDI train 1 done.\n");
4081 break;
4082 }
4083 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004084 }
Sean Paulfa37d392012-03-02 12:53:39 -05004085 if (retry < 5)
4086 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004087 }
4088 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004089 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004090
4091 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01004092 reg = FDI_TX_CTL(pipe);
4093 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004094 temp &= ~FDI_LINK_TRAIN_NONE;
4095 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004096 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004097 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4098 /* SNB-B */
4099 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
4100 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004101 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004102
Chris Wilson5eddb702010-09-11 13:48:45 +01004103 reg = FDI_RX_CTL(pipe);
4104 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004105 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004106 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4107 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
4108 } else {
4109 temp &= ~FDI_LINK_TRAIN_NONE;
4110 temp |= FDI_LINK_TRAIN_PATTERN_2;
4111 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004112 I915_WRITE(reg, temp);
4113
4114 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004115 udelay(150);
4116
Akshay Joshi0206e352011-08-16 15:34:10 -04004117 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004118 reg = FDI_TX_CTL(pipe);
4119 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004120 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4121 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004122 I915_WRITE(reg, temp);
4123
4124 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004125 udelay(500);
4126
Sean Paulfa37d392012-03-02 12:53:39 -05004127 for (retry = 0; retry < 5; retry++) {
4128 reg = FDI_RX_IIR(pipe);
4129 temp = I915_READ(reg);
4130 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4131 if (temp & FDI_RX_SYMBOL_LOCK) {
4132 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4133 DRM_DEBUG_KMS("FDI train 2 done.\n");
4134 break;
4135 }
4136 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004137 }
Sean Paulfa37d392012-03-02 12:53:39 -05004138 if (retry < 5)
4139 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004140 }
4141 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004143
4144 DRM_DEBUG_KMS("FDI train done.\n");
4145}
4146
Jesse Barnes357555c2011-04-28 15:09:55 -07004147/* Manual link training for Ivy Bridge A0 parts */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004148static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4149 const struct intel_crtc_state *crtc_state)
Jesse Barnes357555c2011-04-28 15:09:55 -07004150{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004151 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004152 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004153 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004154 i915_reg_t reg;
4155 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004156
4157 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4158 for train result */
4159 reg = FDI_RX_IMR(pipe);
4160 temp = I915_READ(reg);
4161 temp &= ~FDI_RX_SYMBOL_LOCK;
4162 temp &= ~FDI_RX_BIT_LOCK;
4163 I915_WRITE(reg, temp);
4164
4165 POSTING_READ(reg);
4166 udelay(150);
4167
Daniel Vetter01a415f2012-10-27 15:58:40 +02004168 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4169 I915_READ(FDI_RX_IIR(pipe)));
4170
Jesse Barnes139ccd32013-08-19 11:04:55 -07004171 /* Try each vswing and preemphasis setting twice before moving on */
4172 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4173 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004174 reg = FDI_TX_CTL(pipe);
4175 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004176 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4177 temp &= ~FDI_TX_ENABLE;
4178 I915_WRITE(reg, temp);
4179
4180 reg = FDI_RX_CTL(pipe);
4181 temp = I915_READ(reg);
4182 temp &= ~FDI_LINK_TRAIN_AUTO;
4183 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4184 temp &= ~FDI_RX_ENABLE;
4185 I915_WRITE(reg, temp);
4186
4187 /* enable CPU FDI TX and PCH FDI RX */
4188 reg = FDI_TX_CTL(pipe);
4189 temp = I915_READ(reg);
4190 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004191 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004192 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004193 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004194 temp |= snb_b_fdi_train_param[j/2];
4195 temp |= FDI_COMPOSITE_SYNC;
4196 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4197
4198 I915_WRITE(FDI_RX_MISC(pipe),
4199 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4200
4201 reg = FDI_RX_CTL(pipe);
4202 temp = I915_READ(reg);
4203 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4204 temp |= FDI_COMPOSITE_SYNC;
4205 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4206
4207 POSTING_READ(reg);
4208 udelay(1); /* should be 0.5us */
4209
4210 for (i = 0; i < 4; i++) {
4211 reg = FDI_RX_IIR(pipe);
4212 temp = I915_READ(reg);
4213 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4214
4215 if (temp & FDI_RX_BIT_LOCK ||
4216 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4217 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4218 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4219 i);
4220 break;
4221 }
4222 udelay(1); /* should be 0.5us */
4223 }
4224 if (i == 4) {
4225 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4226 continue;
4227 }
4228
4229 /* Train 2 */
4230 reg = FDI_TX_CTL(pipe);
4231 temp = I915_READ(reg);
4232 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4233 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4234 I915_WRITE(reg, temp);
4235
4236 reg = FDI_RX_CTL(pipe);
4237 temp = I915_READ(reg);
4238 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4239 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004240 I915_WRITE(reg, temp);
4241
4242 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004243 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004244
Jesse Barnes139ccd32013-08-19 11:04:55 -07004245 for (i = 0; i < 4; i++) {
4246 reg = FDI_RX_IIR(pipe);
4247 temp = I915_READ(reg);
4248 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004249
Jesse Barnes139ccd32013-08-19 11:04:55 -07004250 if (temp & FDI_RX_SYMBOL_LOCK ||
4251 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4252 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4253 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4254 i);
4255 goto train_done;
4256 }
4257 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004258 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004259 if (i == 4)
4260 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004261 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004262
Jesse Barnes139ccd32013-08-19 11:04:55 -07004263train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004264 DRM_DEBUG_KMS("FDI train done.\n");
4265}
4266
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004267static void ironlake_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004268{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
4270 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004271 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004272 i915_reg_t reg;
4273 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004274
Jesse Barnes0e23b992010-09-10 11:10:00 -07004275 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004276 reg = FDI_RX_CTL(pipe);
4277 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004278 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004279 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004280 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004281 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4282
4283 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004284 udelay(200);
4285
4286 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004287 temp = I915_READ(reg);
4288 I915_WRITE(reg, temp | FDI_PCDCLK);
4289
4290 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004291 udelay(200);
4292
Paulo Zanoni20749732012-11-23 15:30:38 -02004293 /* Enable CPU FDI TX PLL, always on for Ironlake */
4294 reg = FDI_TX_CTL(pipe);
4295 temp = I915_READ(reg);
4296 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4297 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004298
Paulo Zanoni20749732012-11-23 15:30:38 -02004299 POSTING_READ(reg);
4300 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004301 }
4302}
4303
Daniel Vetter88cefb62012-08-12 19:27:14 +02004304static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4305{
4306 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004307 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004308 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004309 i915_reg_t reg;
4310 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004311
4312 /* Switch from PCDclk to Rawclk */
4313 reg = FDI_RX_CTL(pipe);
4314 temp = I915_READ(reg);
4315 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4316
4317 /* Disable CPU FDI TX PLL */
4318 reg = FDI_TX_CTL(pipe);
4319 temp = I915_READ(reg);
4320 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4321
4322 POSTING_READ(reg);
4323 udelay(100);
4324
4325 reg = FDI_RX_CTL(pipe);
4326 temp = I915_READ(reg);
4327 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4328
4329 /* Wait for the clocks to turn off. */
4330 POSTING_READ(reg);
4331 udelay(100);
4332}
4333
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004334static void ironlake_fdi_disable(struct drm_crtc *crtc)
4335{
4336 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004337 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4339 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004340 i915_reg_t reg;
4341 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004342
4343 /* disable CPU FDI tx and PCH FDI rx */
4344 reg = FDI_TX_CTL(pipe);
4345 temp = I915_READ(reg);
4346 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4347 POSTING_READ(reg);
4348
4349 reg = FDI_RX_CTL(pipe);
4350 temp = I915_READ(reg);
4351 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004352 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004353 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4354
4355 POSTING_READ(reg);
4356 udelay(100);
4357
4358 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004359 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004360 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004361
4362 /* still set train pattern 1 */
4363 reg = FDI_TX_CTL(pipe);
4364 temp = I915_READ(reg);
4365 temp &= ~FDI_LINK_TRAIN_NONE;
4366 temp |= FDI_LINK_TRAIN_PATTERN_1;
4367 I915_WRITE(reg, temp);
4368
4369 reg = FDI_RX_CTL(pipe);
4370 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004371 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004372 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4373 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4374 } else {
4375 temp &= ~FDI_LINK_TRAIN_NONE;
4376 temp |= FDI_LINK_TRAIN_PATTERN_1;
4377 }
4378 /* BPC in FDI rx is consistent with that in PIPECONF */
4379 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004380 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004381 I915_WRITE(reg, temp);
4382
4383 POSTING_READ(reg);
4384 udelay(100);
4385}
4386
Chris Wilson49d73912016-11-29 09:50:08 +00004387bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004388{
Daniel Vetterfa058872017-07-20 19:57:52 +02004389 struct drm_crtc *crtc;
4390 bool cleanup_done;
Chris Wilson5dce5b932014-01-20 10:17:36 +00004391
Daniel Vetterfa058872017-07-20 19:57:52 +02004392 drm_for_each_crtc(crtc, &dev_priv->drm) {
4393 struct drm_crtc_commit *commit;
4394 spin_lock(&crtc->commit_lock);
4395 commit = list_first_entry_or_null(&crtc->commit_list,
4396 struct drm_crtc_commit, commit_entry);
4397 cleanup_done = commit ?
4398 try_wait_for_completion(&commit->cleanup_done) : true;
4399 spin_unlock(&crtc->commit_lock);
4400
4401 if (cleanup_done)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004402 continue;
4403
Daniel Vetterfa058872017-07-20 19:57:52 +02004404 drm_crtc_wait_one_vblank(crtc);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004405
4406 return true;
4407 }
4408
4409 return false;
4410}
4411
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004412void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004413{
4414 u32 temp;
4415
4416 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4417
4418 mutex_lock(&dev_priv->sb_lock);
4419
4420 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4421 temp |= SBI_SSCCTL_DISABLE;
4422 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4423
4424 mutex_unlock(&dev_priv->sb_lock);
4425}
4426
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004427/* Program iCLKIP clock to the desired frequency */
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004428static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004429{
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004430 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004431 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004432 int clock = crtc_state->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004433 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4434 u32 temp;
4435
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004436 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004437
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004438 /* The iCLK virtual clock root frequency is in MHz,
4439 * but the adjusted_mode->crtc_clock in in KHz. To get the
4440 * divisors, it is necessary to divide one by another, so we
4441 * convert the virtual clock precision to KHz here for higher
4442 * precision.
4443 */
4444 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004445 u32 iclk_virtual_root_freq = 172800 * 1000;
4446 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004447 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004448
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004449 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4450 clock << auxdiv);
4451 divsel = (desired_divisor / iclk_pi_range) - 2;
4452 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004453
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004454 /*
4455 * Near 20MHz is a corner case which is
4456 * out of range for the 7-bit divisor
4457 */
4458 if (divsel <= 0x7f)
4459 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004460 }
4461
4462 /* This should not happen with any sane values */
4463 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4464 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4465 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4466 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4467
4468 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 +03004469 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004470 auxdiv,
4471 divsel,
4472 phasedir,
4473 phaseinc);
4474
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004475 mutex_lock(&dev_priv->sb_lock);
4476
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004477 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004478 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004479 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4480 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4481 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4482 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4483 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4484 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004485 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004486
4487 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004488 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004489 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4490 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004491 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004492
4493 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004494 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004495 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004496 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004497
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004498 mutex_unlock(&dev_priv->sb_lock);
4499
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004500 /* Wait for initialization time */
4501 udelay(24);
4502
4503 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4504}
4505
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004506int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4507{
4508 u32 divsel, phaseinc, auxdiv;
4509 u32 iclk_virtual_root_freq = 172800 * 1000;
4510 u32 iclk_pi_range = 64;
4511 u32 desired_divisor;
4512 u32 temp;
4513
4514 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4515 return 0;
4516
4517 mutex_lock(&dev_priv->sb_lock);
4518
4519 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4520 if (temp & SBI_SSCCTL_DISABLE) {
4521 mutex_unlock(&dev_priv->sb_lock);
4522 return 0;
4523 }
4524
4525 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4526 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4527 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4528 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4529 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4530
4531 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4532 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4533 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4534
4535 mutex_unlock(&dev_priv->sb_lock);
4536
4537 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4538
4539 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4540 desired_divisor << auxdiv);
4541}
4542
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004543static void ironlake_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
Daniel Vetter275f01b22013-05-03 11:49:47 +02004544 enum pipe pch_transcoder)
4545{
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004546 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4547 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4548 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004549
4550 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4551 I915_READ(HTOTAL(cpu_transcoder)));
4552 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4553 I915_READ(HBLANK(cpu_transcoder)));
4554 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4555 I915_READ(HSYNC(cpu_transcoder)));
4556
4557 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4558 I915_READ(VTOTAL(cpu_transcoder)));
4559 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4560 I915_READ(VBLANK(cpu_transcoder)));
4561 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4562 I915_READ(VSYNC(cpu_transcoder)));
4563 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4564 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4565}
4566
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004567static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004568{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004569 uint32_t temp;
4570
4571 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004572 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004573 return;
4574
4575 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4576 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4577
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004578 temp &= ~FDI_BC_BIFURCATION_SELECT;
4579 if (enable)
4580 temp |= FDI_BC_BIFURCATION_SELECT;
4581
4582 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004583 I915_WRITE(SOUTH_CHICKEN1, temp);
4584 POSTING_READ(SOUTH_CHICKEN1);
4585}
4586
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004587static void ivybridge_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004588{
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004589 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4590 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004591
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004592 switch (crtc->pipe) {
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004593 case PIPE_A:
4594 break;
4595 case PIPE_B:
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004596 if (crtc_state->fdi_lanes > 2)
4597 cpt_set_fdi_bc_bifurcation(dev_priv, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004598 else
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004599 cpt_set_fdi_bc_bifurcation(dev_priv, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004600
4601 break;
4602 case PIPE_C:
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004603 cpt_set_fdi_bc_bifurcation(dev_priv, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004604
4605 break;
4606 default:
4607 BUG();
4608 }
4609}
4610
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004611/*
4612 * Finds the encoder associated with the given CRTC. This can only be
4613 * used when we know that the CRTC isn't feeding multiple encoders!
4614 */
4615static struct intel_encoder *
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004616intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
4617 const struct intel_crtc_state *crtc_state)
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004618{
4619 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004620 const struct drm_connector_state *connector_state;
4621 const struct drm_connector *connector;
4622 struct intel_encoder *encoder = NULL;
4623 int num_encoders = 0;
4624 int i;
4625
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004626 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004627 if (connector_state->crtc != &crtc->base)
4628 continue;
4629
4630 encoder = to_intel_encoder(connector_state->best_encoder);
4631 num_encoders++;
4632 }
4633
4634 WARN(num_encoders != 1, "%d encoders for pipe %c\n",
4635 num_encoders, pipe_name(crtc->pipe));
4636
4637 return encoder;
4638}
4639
Jesse Barnesf67a5592011-01-05 10:31:48 -08004640/*
4641 * Enable PCH resources required for PCH ports:
4642 * - PCH PLLs
4643 * - FDI training & RX/TX
4644 * - update transcoder timings
4645 * - DP transcoding bits
4646 * - transcoder
4647 */
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004648static void ironlake_pch_enable(const struct intel_atomic_state *state,
4649 const struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08004650{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004651 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004652 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004653 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004654 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004655 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004656
Daniel Vetterab9412b2013-05-03 11:49:46 +02004657 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004658
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004659 if (IS_IVYBRIDGE(dev_priv))
Maarten Lankhorstb0b62d82018-10-11 12:04:56 +02004660 ivybridge_update_fdi_bc_bifurcation(crtc_state);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004661
Daniel Vettercd986ab2012-10-26 10:58:12 +02004662 /* Write the TU size bits before fdi link training, so that error
4663 * detection works. */
4664 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4665 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4666
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004667 /* For PCH output, training FDI link */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004668 dev_priv->display.fdi_link_train(crtc, crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004669
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004670 /* We need to program the right clock selection before writing the pixel
4671 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004672 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004673 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004674
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004675 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004676 temp |= TRANS_DPLL_ENABLE(pipe);
4677 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004678 if (crtc_state->shared_dpll ==
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004679 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004680 temp |= sel;
4681 else
4682 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004683 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004684 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004685
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004686 /* XXX: pch pll's can be enabled any time before we enable the PCH
4687 * transcoder, and we actually should do this to not upset any PCH
4688 * transcoder that already use the clock when we share it.
4689 *
4690 * Note that enable_shared_dpll tries to do the right thing, but
4691 * get_shared_dpll unconditionally resets the pll - we need that to have
4692 * the right LVDS enable sequence. */
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02004693 intel_enable_shared_dpll(crtc_state);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004694
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004695 /* set transcoder timing, panel must allow it */
4696 assert_panel_unlocked(dev_priv, pipe);
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004697 ironlake_pch_transcoder_set_timings(crtc_state, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004698
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004699 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004700
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004701 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004702 if (HAS_PCH_CPT(dev_priv) &&
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004703 intel_crtc_has_dp_encoder(crtc_state)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004704 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004705 &crtc_state->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004706 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004707 i915_reg_t reg = TRANS_DP_CTL(pipe);
Ville Syrjäläf67dc6d2018-05-18 18:29:26 +03004708 enum port port;
4709
Chris Wilson5eddb702010-09-11 13:48:45 +01004710 temp = I915_READ(reg);
4711 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004712 TRANS_DP_SYNC_MASK |
4713 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004714 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004715 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004716
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004717 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004718 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004719 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004720 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004721
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004722 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
Ville Syrjäläf67dc6d2018-05-18 18:29:26 +03004723 WARN_ON(port < PORT_B || port > PORT_D);
4724 temp |= TRANS_DP_PORT_SEL(port);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004725
Chris Wilson5eddb702010-09-11 13:48:45 +01004726 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004727 }
4728
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02004729 ironlake_enable_pch_transcoder(crtc_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004730}
4731
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004732static void lpt_pch_enable(const struct intel_atomic_state *state,
4733 const struct intel_crtc_state *crtc_state)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004734{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004735 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004736 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004737 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004738
Matthias Kaehlckea2196032017-07-17 11:14:03 -07004739 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004740
Maarten Lankhorstc5b36fa2018-10-11 12:04:55 +02004741 lpt_program_iclkip(crtc_state);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004742
Paulo Zanoni0540e482012-10-31 18:12:40 -02004743 /* Set transcoder timing. */
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004744 ironlake_pch_transcoder_set_timings(crtc_state, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004745
Paulo Zanoni937bb612012-10-31 18:12:47 -02004746 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004747}
4748
Daniel Vettera1520312013-05-03 11:49:50 +02004749static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004750{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004751 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004752 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004753 u32 temp;
4754
4755 temp = I915_READ(dslreg);
4756 udelay(500);
4757 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004758 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004759 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004760 }
4761}
4762
Ville Syrjälä0a599522018-05-21 21:56:13 +03004763/*
4764 * The hardware phase 0.0 refers to the center of the pixel.
4765 * We want to start from the top/left edge which is phase
4766 * -0.5. That matches how the hardware calculates the scaling
4767 * factors (from top-left of the first pixel to bottom-right
4768 * of the last pixel, as opposed to the pixel centers).
4769 *
4770 * For 4:2:0 subsampled chroma planes we obviously have to
4771 * adjust that so that the chroma sample position lands in
4772 * the right spot.
4773 *
4774 * Note that for packed YCbCr 4:2:2 formats there is no way to
4775 * control chroma siting. The hardware simply replicates the
4776 * chroma samples for both of the luma samples, and thus we don't
4777 * actually get the expected MPEG2 chroma siting convention :(
4778 * The same behaviour is observed on pre-SKL platforms as well.
4779 */
4780u16 skl_scaler_calc_phase(int sub, bool chroma_cosited)
4781{
4782 int phase = -0x8000;
4783 u16 trip = 0;
4784
4785 if (chroma_cosited)
4786 phase += (sub - 1) * 0x8000 / sub;
4787
4788 if (phase < 0)
4789 phase = 0x10000 + phase;
4790 else
4791 trip = PS_PHASE_TRIP;
4792
4793 return ((phase >> 2) & PS_PHASE_MASK) | trip;
4794}
4795
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004796static int
4797skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004798 unsigned int scaler_user, int *scaler_id,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304799 int src_w, int src_h, int dst_w, int dst_h,
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004800 const struct drm_format_info *format, bool need_scaler)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004801{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004802 struct intel_crtc_scaler_state *scaler_state =
4803 &crtc_state->scaler_state;
4804 struct intel_crtc *intel_crtc =
4805 to_intel_crtc(crtc_state->base.crtc);
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304806 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4807 const struct drm_display_mode *adjusted_mode =
4808 &crtc_state->base.adjusted_mode;
Chandra Konduru6156a452015-04-27 13:48:39 -07004809
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004810 /*
4811 * Src coordinates are already rotated by 270 degrees for
4812 * the 90/270 degree plane rotation cases (to match the
4813 * GTT mapping), hence no need to account for rotation here.
4814 */
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004815 if (src_w != dst_w || src_h != dst_h)
4816 need_scaler = true;
Shashank Sharmae5c05932017-07-21 20:55:05 +05304817
Chandra Kondurua1b22782015-04-07 15:28:45 -07004818 /*
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304819 * Scaling/fitting not supported in IF-ID mode in GEN9+
4820 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4821 * Once NV12 is enabled, handle it here while allocating scaler
4822 * for NV12.
4823 */
4824 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004825 need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304826 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4827 return -EINVAL;
4828 }
4829
4830 /*
Chandra Kondurua1b22782015-04-07 15:28:45 -07004831 * if plane is being disabled or scaler is no more required or force detach
4832 * - free scaler binded to this plane/crtc
4833 * - in order to do this, update crtc->scaler_usage
4834 *
4835 * Here scaler state in crtc_state is set free so that
4836 * scaler can be assigned to other user. Actual register
4837 * update to free the scaler is done in plane/panel-fit programming.
4838 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4839 */
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004840 if (force_detach || !need_scaler) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004841 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004842 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004843 scaler_state->scalers[*scaler_id].in_use = 0;
4844
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004845 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4846 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4847 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004848 scaler_state->scaler_users);
4849 *scaler_id = -1;
4850 }
4851 return 0;
4852 }
4853
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004854 if (format && format->format == DRM_FORMAT_NV12 &&
Maarten Lankhorst5d794282018-05-12 03:03:14 +05304855 (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
Chandra Konduru77224cd2018-04-09 09:11:13 +05304856 DRM_DEBUG_KMS("NV12: src dimensions not met\n");
4857 return -EINVAL;
4858 }
4859
Chandra Kondurua1b22782015-04-07 15:28:45 -07004860 /* range checks */
4861 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
Nabendu Maiti323301a2018-03-23 10:24:18 -07004862 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4863 (IS_GEN11(dev_priv) &&
4864 (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
4865 dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
4866 (!IS_GEN11(dev_priv) &&
4867 (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4868 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004869 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004870 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004871 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004872 return -EINVAL;
4873 }
4874
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004875 /* mark this plane as a scaler user in crtc_state */
4876 scaler_state->scaler_users |= (1 << scaler_user);
4877 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4878 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4879 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4880 scaler_state->scaler_users);
4881
4882 return 0;
4883}
4884
4885/**
4886 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4887 *
4888 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004889 *
4890 * Return
4891 * 0 - scaler_usage updated successfully
4892 * error - requested scaling cannot be supported or other error condition
4893 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004894int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004895{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004896 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004897 bool need_scaler = false;
4898
4899 if (state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
4900 need_scaler = true;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004901
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004902 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304903 &state->scaler_state.scaler_id,
4904 state->pipe_src_w, state->pipe_src_h,
4905 adjusted_mode->crtc_hdisplay,
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004906 adjusted_mode->crtc_vdisplay, NULL, need_scaler);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004907}
4908
4909/**
4910 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
Chris Wilsonc38c1452018-02-14 13:49:22 +00004911 * @crtc_state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004912 * @plane_state: atomic plane state to update
4913 *
4914 * Return
4915 * 0 - scaler_usage updated successfully
4916 * error - requested scaling cannot be supported or other error condition
4917 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004918static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4919 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004920{
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004921 struct intel_plane *intel_plane =
4922 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004923 struct drm_framebuffer *fb = plane_state->base.fb;
4924 int ret;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004925 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004926 bool need_scaler = false;
4927
4928 /* Pre-gen11 and SDR planes always need a scaler for planar formats. */
4929 if (!icl_is_hdr_plane(intel_plane) &&
4930 fb && fb->format->format == DRM_FORMAT_NV12)
4931 need_scaler = true;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004932
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004933 ret = skl_update_scaler(crtc_state, force_detach,
4934 drm_plane_index(&intel_plane->base),
4935 &plane_state->scaler_id,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004936 drm_rect_width(&plane_state->base.src) >> 16,
4937 drm_rect_height(&plane_state->base.src) >> 16,
4938 drm_rect_width(&plane_state->base.dst),
Chandra Konduru77224cd2018-04-09 09:11:13 +05304939 drm_rect_height(&plane_state->base.dst),
Maarten Lankhorstb1554e22018-10-18 13:51:31 +02004940 fb ? fb->format : NULL, need_scaler);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004941
4942 if (ret || plane_state->scaler_id < 0)
4943 return ret;
4944
Chandra Kondurua1b22782015-04-07 15:28:45 -07004945 /* check colorkey */
Ville Syrjälä6ec5bd32018-02-02 22:42:31 +02004946 if (plane_state->ckey.flags) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004947 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4948 intel_plane->base.base.id,
4949 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004950 return -EINVAL;
4951 }
4952
4953 /* Check src format */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004954 switch (fb->format->format) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004955 case DRM_FORMAT_RGB565:
4956 case DRM_FORMAT_XBGR8888:
4957 case DRM_FORMAT_XRGB8888:
4958 case DRM_FORMAT_ABGR8888:
4959 case DRM_FORMAT_ARGB8888:
4960 case DRM_FORMAT_XRGB2101010:
4961 case DRM_FORMAT_XBGR2101010:
4962 case DRM_FORMAT_YUYV:
4963 case DRM_FORMAT_YVYU:
4964 case DRM_FORMAT_UYVY:
4965 case DRM_FORMAT_VYUY:
Chandra Konduru77224cd2018-04-09 09:11:13 +05304966 case DRM_FORMAT_NV12:
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004967 break;
4968 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004969 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4970 intel_plane->base.base.id, intel_plane->base.name,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004971 fb->base.id, fb->format->format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004972 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004973 }
4974
Chandra Kondurua1b22782015-04-07 15:28:45 -07004975 return 0;
4976}
4977
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004978static void skylake_scaler_disable(struct intel_crtc *crtc)
4979{
4980 int i;
4981
4982 for (i = 0; i < crtc->num_scalers; i++)
4983 skl_detach_scaler(crtc, i);
4984}
4985
Maarten Lankhorstb2562712018-10-04 11:45:53 +02004986static void skylake_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004987{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02004988 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4989 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4990 enum pipe pipe = crtc->pipe;
4991 const struct intel_crtc_scaler_state *scaler_state =
4992 &crtc_state->scaler_state;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004993
Maarten Lankhorstb2562712018-10-04 11:45:53 +02004994 if (crtc_state->pch_pfit.enabled) {
Ville Syrjälä0a599522018-05-21 21:56:13 +03004995 u16 uv_rgb_hphase, uv_rgb_vphase;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004996 int id;
4997
Maarten Lankhorstb2562712018-10-04 11:45:53 +02004998 if (WARN_ON(crtc_state->scaler_state.scaler_id < 0))
Chandra Kondurua1b22782015-04-07 15:28:45 -07004999 return;
Chandra Kondurua1b22782015-04-07 15:28:45 -07005000
Ville Syrjälä0a599522018-05-21 21:56:13 +03005001 uv_rgb_hphase = skl_scaler_calc_phase(1, false);
5002 uv_rgb_vphase = skl_scaler_calc_phase(1, false);
5003
Chandra Kondurua1b22782015-04-07 15:28:45 -07005004 id = scaler_state->scaler_id;
5005 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
5006 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
Ville Syrjälä0a599522018-05-21 21:56:13 +03005007 I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
5008 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
5009 I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
5010 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005011 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc_state->pch_pfit.pos);
5012 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc_state->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005013 }
5014}
5015
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005016static void ironlake_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnesb074cec2013-04-25 12:55:02 -07005017{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005018 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5019 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07005020 int pipe = crtc->pipe;
5021
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005022 if (crtc_state->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07005023 /* Force use of hard-coded filter coefficients
5024 * as some pre-programmed values are broken,
5025 * e.g. x201.
5026 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005027 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07005028 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
5029 PF_PIPE_SEL_IVB(pipe));
5030 else
5031 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005032 I915_WRITE(PF_WIN_POS(pipe), crtc_state->pch_pfit.pos);
5033 I915_WRITE(PF_WIN_SZ(pipe), crtc_state->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08005034 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005035}
5036
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005037void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005038{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005039 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03005040 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005041 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005042
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005043 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005044 return;
5045
Maarten Lankhorst307e4492016-03-23 14:33:28 +01005046 /*
5047 * We can only enable IPS after we enable a plane and wait for a vblank
5048 * This function is called from post_plane_update, which is run after
5049 * a vblank wait.
5050 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005051 WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02005052
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005053 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005054 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjälä61843f02017-09-12 18:34:11 +03005055 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
5056 IPS_ENABLE | IPS_PCODE_CONTROL));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005057 mutex_unlock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005058 /* Quoting Art Runyan: "its not safe to expect any particular
5059 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08005060 * mailbox." Moreover, the mailbox may return a bogus state,
5061 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005062 */
5063 } else {
5064 I915_WRITE(IPS_CTL, IPS_ENABLE);
5065 /* The bit only becomes 1 in the next vblank, so this wait here
5066 * is essentially intel_wait_for_vblank. If we don't have this
5067 * and don't wait for vblanks until the end of crtc_enable, then
5068 * the HW state readout code will complain that the expected
5069 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01005070 if (intel_wait_for_register(dev_priv,
5071 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
5072 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005073 DRM_ERROR("Timed out waiting for IPS enable\n");
5074 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005075}
5076
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005077void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005078{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005079 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005080 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005081 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005082
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005083 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005084 return;
5085
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005086 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005087 mutex_lock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005088 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005089 mutex_unlock(&dev_priv->pcu_lock);
Imre Deakacb3ef02018-09-05 13:00:05 +03005090 /*
5091 * Wait for PCODE to finish disabling IPS. The BSpec specified
5092 * 42ms timeout value leads to occasional timeouts so use 100ms
5093 * instead.
5094 */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01005095 if (intel_wait_for_register(dev_priv,
5096 IPS_CTL, IPS_ENABLE, 0,
Imre Deakacb3ef02018-09-05 13:00:05 +03005097 100))
Ben Widawsky23d0b132014-04-10 14:32:41 -07005098 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08005099 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005100 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08005101 POSTING_READ(IPS_CTL);
5102 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005103
5104 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005105 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005106}
5107
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005108static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005109{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005110 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005111 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005112
5113 mutex_lock(&dev->struct_mutex);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005114 (void) intel_overlay_switch_off(intel_crtc->overlay);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005115 mutex_unlock(&dev->struct_mutex);
5116 }
5117
5118 /* Let userspace switch the overlay on again. In most cases userspace
5119 * has to recompute where to put it anyway.
5120 */
5121}
5122
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005123/**
5124 * intel_post_enable_primary - Perform operations after enabling primary plane
5125 * @crtc: the CRTC whose primary plane was just enabled
Chris Wilsonc38c1452018-02-14 13:49:22 +00005126 * @new_crtc_state: the enabling state
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005127 *
5128 * Performs potentially sleeping operations that must be done after the primary
5129 * plane is enabled, such as updating FBC and IPS. Note that this may be
5130 * called due to an explicit primary plane update, or due to an implicit
5131 * re-enable that is caused when a sprite plane is updated to no longer
5132 * completely hide the primary plane.
5133 */
5134static void
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005135intel_post_enable_primary(struct drm_crtc *crtc,
5136 const struct intel_crtc_state *new_crtc_state)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005137{
5138 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005139 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5141 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005142
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005143 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005144 * Gen2 reports pipe underruns whenever all planes are disabled.
5145 * So don't enable underrun reporting before at least some planes
5146 * are enabled.
5147 * FIXME: Need to fix the logic to work when we turn off all planes
5148 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02005149 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005150 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005151 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5152
Ville Syrjäläaca7b682015-10-30 19:22:21 +02005153 /* Underruns don't always raise interrupts, so check manually. */
5154 intel_check_cpu_fifo_underruns(dev_priv);
5155 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005156}
5157
Ville Syrjälä2622a082016-03-09 19:07:26 +02005158/* FIXME get rid of this and use pre_plane_update */
5159static void
5160intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5161{
5162 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005163 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5165 int pipe = intel_crtc->pipe;
5166
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005167 /*
5168 * Gen2 reports pipe underruns whenever all planes are disabled.
5169 * So disable underrun reporting before all the planes get disabled.
5170 */
5171 if (IS_GEN2(dev_priv))
5172 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5173
5174 hsw_disable_ips(to_intel_crtc_state(crtc->state));
Ville Syrjälä2622a082016-03-09 19:07:26 +02005175
5176 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005177 * Vblank time updates from the shadow to live plane control register
5178 * are blocked if the memory self-refresh mode is active at that
5179 * moment. So to make sure the plane gets truly disabled, disable
5180 * first the self-refresh mode. The self-refresh enable bit in turn
5181 * will be checked/applied by the HW only at the next frame start
5182 * event which is after the vblank start event, so we need to have a
5183 * wait-for-vblank between disabling the plane and the pipe.
5184 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +02005185 if (HAS_GMCH_DISPLAY(dev_priv) &&
5186 intel_set_memory_cxsr(dev_priv, false))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005187 intel_wait_for_vblank(dev_priv, pipe);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005188}
5189
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005190static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
5191 const struct intel_crtc_state *new_crtc_state)
5192{
5193 if (!old_crtc_state->ips_enabled)
5194 return false;
5195
5196 if (needs_modeset(&new_crtc_state->base))
5197 return true;
5198
5199 return !new_crtc_state->ips_enabled;
5200}
5201
5202static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
5203 const struct intel_crtc_state *new_crtc_state)
5204{
5205 if (!new_crtc_state->ips_enabled)
5206 return false;
5207
5208 if (needs_modeset(&new_crtc_state->base))
5209 return true;
5210
5211 /*
5212 * We can't read out IPS on broadwell, assume the worst and
5213 * forcibly enable IPS on the first fastset.
5214 */
5215 if (new_crtc_state->update_pipe &&
5216 old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
5217 return true;
5218
5219 return !old_crtc_state->ips_enabled;
5220}
5221
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305222static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
5223 const struct intel_crtc_state *crtc_state)
5224{
5225 if (!crtc_state->nv12_planes)
5226 return false;
5227
Rodrigo Vivi1347d3c2018-10-31 09:28:45 -07005228 /* WA Display #0827: Gen9:all */
5229 if (IS_GEN9(dev_priv) && !IS_GEMINILAKE(dev_priv))
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305230 return true;
5231
5232 return false;
5233}
5234
Daniel Vetter5a21b662016-05-24 17:13:53 +02005235static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5236{
5237 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +05305238 struct drm_device *dev = crtc->base.dev;
5239 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005240 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5241 struct intel_crtc_state *pipe_config =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005242 intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
5243 crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005244 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005245 struct drm_plane_state *old_primary_state =
5246 drm_atomic_get_old_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005247
Chris Wilson5748b6a2016-08-04 16:32:38 +01005248 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005249
Daniel Vetter5a21b662016-05-24 17:13:53 +02005250 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005251 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005252
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005253 if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
5254 hsw_enable_ips(pipe_config);
5255
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005256 if (old_primary_state) {
5257 struct drm_plane_state *new_primary_state =
5258 drm_atomic_get_new_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005259
5260 intel_fbc_post_update(crtc);
5261
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005262 if (new_primary_state->visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005263 (needs_modeset(&pipe_config->base) ||
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005264 !old_primary_state->visible))
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005265 intel_post_enable_primary(&crtc->base, pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005266 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305267
5268 /* Display WA 827 */
5269 if (needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305270 !needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305271 skl_wa_clkgate(dev_priv, crtc->pipe, false);
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305272 }
Daniel Vetter5a21b662016-05-24 17:13:53 +02005273}
5274
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005275static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5276 struct intel_crtc_state *pipe_config)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005277{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005278 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005279 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005280 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005281 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5282 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005283 struct drm_plane_state *old_primary_state =
5284 drm_atomic_get_old_plane_state(old_state, primary);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005285 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005286 struct intel_atomic_state *old_intel_state =
5287 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005288
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005289 if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
5290 hsw_disable_ips(old_crtc_state);
5291
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005292 if (old_primary_state) {
5293 struct intel_plane_state *new_primary_state =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005294 intel_atomic_get_new_plane_state(old_intel_state,
5295 to_intel_plane(primary));
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005296
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005297 intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005298 /*
5299 * Gen2 reports pipe underruns whenever all planes are disabled.
5300 * So disable underrun reporting before all the planes get disabled.
5301 */
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005302 if (IS_GEN2(dev_priv) && old_primary_state->visible &&
5303 (modeset || !new_primary_state->base.visible))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005304 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005305 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005306
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305307 /* Display WA 827 */
5308 if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305309 needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305310 skl_wa_clkgate(dev_priv, crtc->pipe, true);
Vidya Srinivas6deef9b2018-05-12 03:03:13 +05305311 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305312
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02005313 /*
5314 * Vblank time updates from the shadow to live plane control register
5315 * are blocked if the memory self-refresh mode is active at that
5316 * moment. So to make sure the plane gets truly disabled, disable
5317 * first the self-refresh mode. The self-refresh enable bit in turn
5318 * will be checked/applied by the HW only at the next frame start
5319 * event which is after the vblank start event, so we need to have a
5320 * wait-for-vblank between disabling the plane and the pipe.
5321 */
5322 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5323 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5324 intel_wait_for_vblank(dev_priv, crtc->pipe);
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005325
Matt Ropered4a6a72016-02-23 17:20:13 -08005326 /*
5327 * IVB workaround: must disable low power watermarks for at least
5328 * one frame before enabling scaling. LP watermarks can be re-enabled
5329 * when scaling is disabled.
5330 *
5331 * WaCxSRDisabledForSpriteScaling:ivb
5332 */
Ville Syrjälä8e7a4422018-10-04 15:15:27 +03005333 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev) &&
5334 old_crtc_state->base.active)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005335 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005336
5337 /*
5338 * If we're doing a modeset, we're done. No need to do any pre-vblank
5339 * watermark programming here.
5340 */
5341 if (needs_modeset(&pipe_config->base))
5342 return;
5343
5344 /*
5345 * For platforms that support atomic watermarks, program the
5346 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5347 * will be the intermediate values that are safe for both pre- and
5348 * post- vblank; when vblank happens, the 'active' values will be set
5349 * to the final 'target' values and we'll do this again to get the
5350 * optimal watermarks. For gen9+ platforms, the values we program here
5351 * will be the final target values which will get automatically latched
5352 * at vblank time; no further programming will be necessary.
5353 *
5354 * If a platform hasn't been transitioned to atomic watermarks yet,
5355 * we'll continue to update watermarks the old way, if flags tell
5356 * us to.
5357 */
5358 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005359 dev_priv->display.initial_watermarks(old_intel_state,
5360 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005361 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005362 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005363}
5364
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005365static void intel_crtc_disable_planes(struct intel_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005366{
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005367 struct drm_device *dev = crtc->base.dev;
5368 struct intel_plane *plane;
5369 unsigned fb_bits = 0;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005370
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005371 intel_crtc_dpms_overlay_disable(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005372
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005373 for_each_intel_plane_on_crtc(dev, crtc, plane) {
5374 if (plane_mask & BIT(plane->id)) {
5375 plane->disable_plane(plane, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005376
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005377 fb_bits |= plane->frontbuffer_bit;
5378 }
5379 }
5380
5381 intel_frontbuffer_flip(to_i915(dev), fb_bits);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005382}
5383
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005384static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005385 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005386 struct drm_atomic_state *old_state)
5387{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005388 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005389 struct drm_connector *conn;
5390 int i;
5391
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005392 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005393 struct intel_encoder *encoder =
5394 to_intel_encoder(conn_state->best_encoder);
5395
5396 if (conn_state->crtc != crtc)
5397 continue;
5398
5399 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005400 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005401 }
5402}
5403
5404static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005405 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005406 struct drm_atomic_state *old_state)
5407{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005408 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005409 struct drm_connector *conn;
5410 int i;
5411
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005412 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005413 struct intel_encoder *encoder =
5414 to_intel_encoder(conn_state->best_encoder);
5415
5416 if (conn_state->crtc != crtc)
5417 continue;
5418
5419 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005420 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005421 }
5422}
5423
5424static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005425 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005426 struct drm_atomic_state *old_state)
5427{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005428 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005429 struct drm_connector *conn;
5430 int i;
5431
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005432 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005433 struct intel_encoder *encoder =
5434 to_intel_encoder(conn_state->best_encoder);
5435
5436 if (conn_state->crtc != crtc)
5437 continue;
5438
Jani Nikulac84c6fe2018-10-16 15:41:34 +03005439 if (encoder->enable)
5440 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005441 intel_opregion_notify_encoder(encoder, true);
5442 }
5443}
5444
5445static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005446 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005447 struct drm_atomic_state *old_state)
5448{
5449 struct drm_connector_state *old_conn_state;
5450 struct drm_connector *conn;
5451 int i;
5452
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005453 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005454 struct intel_encoder *encoder =
5455 to_intel_encoder(old_conn_state->best_encoder);
5456
5457 if (old_conn_state->crtc != crtc)
5458 continue;
5459
5460 intel_opregion_notify_encoder(encoder, false);
Jani Nikulac84c6fe2018-10-16 15:41:34 +03005461 if (encoder->disable)
5462 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005463 }
5464}
5465
5466static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005467 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005468 struct drm_atomic_state *old_state)
5469{
5470 struct drm_connector_state *old_conn_state;
5471 struct drm_connector *conn;
5472 int i;
5473
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005474 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005475 struct intel_encoder *encoder =
5476 to_intel_encoder(old_conn_state->best_encoder);
5477
5478 if (old_conn_state->crtc != crtc)
5479 continue;
5480
5481 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005482 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005483 }
5484}
5485
5486static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005487 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005488 struct drm_atomic_state *old_state)
5489{
5490 struct drm_connector_state *old_conn_state;
5491 struct drm_connector *conn;
5492 int i;
5493
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005494 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005495 struct intel_encoder *encoder =
5496 to_intel_encoder(old_conn_state->best_encoder);
5497
5498 if (old_conn_state->crtc != crtc)
5499 continue;
5500
5501 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005502 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005503 }
5504}
5505
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005506static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5507 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005508{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005509 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005510 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005511 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5513 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005514 struct intel_atomic_state *old_intel_state =
5515 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005516
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005517 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005518 return;
5519
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005520 /*
5521 * Sometimes spurious CPU pipe underruns happen during FDI
5522 * training, at least with VGA+HDMI cloning. Suppress them.
5523 *
5524 * On ILK we get an occasional spurious CPU pipe underruns
5525 * between eDP port A enable and vdd enable. Also PCH port
5526 * enable seems to result in the occasional CPU pipe underrun.
5527 *
5528 * Spurious PCH underruns also occur during PCH enabling.
5529 */
Ville Syrjälä2b5b6312018-05-24 22:04:06 +03005530 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5531 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005532
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02005533 if (pipe_config->has_pch_encoder)
5534 intel_prepare_shared_dpll(pipe_config);
Daniel Vetterb14b1052014-04-24 23:55:13 +02005535
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005536 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005537 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005538
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005539 intel_set_pipe_timings(pipe_config);
5540 intel_set_pipe_src_size(pipe_config);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005541
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005542 if (pipe_config->has_pch_encoder) {
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005543 intel_cpu_transcoder_set_m_n(pipe_config,
5544 &pipe_config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005545 }
5546
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005547 ironlake_set_pipeconf(pipe_config);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005548
Jesse Barnesf67a5592011-01-05 10:31:48 -08005549 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005550
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005551 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005552
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005553 if (pipe_config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005554 /* Note: FDI PLL enabling _must_ be done before we enable the
5555 * cpu pipes, hence this is separate from all the other fdi/pch
5556 * enabling. */
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02005557 ironlake_fdi_pll_enable(pipe_config);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005558 } else {
5559 assert_fdi_tx_disabled(dev_priv, pipe);
5560 assert_fdi_rx_disabled(dev_priv, pipe);
5561 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005562
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005563 ironlake_pfit_enable(pipe_config);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005564
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005565 /*
5566 * On ILK+ LUT must be loaded before the pipe is running but with
5567 * clocks enabled
5568 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005569 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005570
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005571 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005572 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02005573 intel_enable_pipe(pipe_config);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005574
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005575 if (pipe_config->has_pch_encoder)
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03005576 ironlake_pch_enable(old_intel_state, pipe_config);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005577
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005578 assert_vblank_disabled(crtc);
5579 drm_crtc_vblank_on(crtc);
5580
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005581 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005582
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005583 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005584 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005585
Ville Syrjäläea80a662018-05-24 22:04:05 +03005586 /*
5587 * Must wait for vblank to avoid spurious PCH FIFO underruns.
5588 * And a second vblank wait is needed at least on ILK with
5589 * some interlaced HDMI modes. Let's do the double wait always
5590 * in case there are more corner cases we don't know about.
5591 */
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005592 if (pipe_config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005593 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläea80a662018-05-24 22:04:05 +03005594 intel_wait_for_vblank(dev_priv, pipe);
5595 }
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005596 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005597 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005598}
5599
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005600/* IPS only exists on ULT machines and is tied to pipe A. */
5601static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5602{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005603 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005604}
5605
Imre Deaked69cd42017-10-02 10:55:57 +03005606static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
5607 enum pipe pipe, bool apply)
5608{
5609 u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
5610 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
5611
5612 if (apply)
5613 val |= mask;
5614 else
5615 val &= ~mask;
5616
5617 I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
5618}
5619
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005620static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
5621{
5622 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5623 enum pipe pipe = crtc->pipe;
5624 uint32_t val;
5625
Rodrigo Vivi443d5e32018-10-04 08:18:14 -07005626 val = MBUS_DBOX_A_CREDIT(2);
5627 val |= MBUS_DBOX_BW_CREDIT(1);
5628 val |= MBUS_DBOX_B_CREDIT(8);
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005629
5630 I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
5631}
5632
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005633static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5634 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005635{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005636 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005637 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005639 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005640 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005641 struct intel_atomic_state *old_intel_state =
5642 to_intel_atomic_state(old_state);
Imre Deaked69cd42017-10-02 10:55:57 +03005643 bool psl_clkgate_wa;
Vandita Kulkarnie16a3752018-06-21 20:43:56 +05305644 u32 pipe_chicken;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005645
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005646 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005647 return;
5648
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005649 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005650
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02005651 if (pipe_config->shared_dpll)
5652 intel_enable_shared_dpll(pipe_config);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005653
Paulo Zanonic27e9172018-04-27 16:14:36 -07005654 if (INTEL_GEN(dev_priv) >= 11)
5655 icl_map_plls_to_ports(crtc, pipe_config, old_state);
5656
Paulo Zanonic8af5272018-05-02 14:58:51 -07005657 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5658
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005659 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005660 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005661
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005662 if (!transcoder_is_dsi(cpu_transcoder))
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005663 intel_set_pipe_timings(pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005664
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005665 intel_set_pipe_src_size(pipe_config);
Daniel Vetter229fca92014-04-24 23:55:09 +02005666
Jani Nikula4d1de972016-03-18 17:05:42 +02005667 if (cpu_transcoder != TRANSCODER_EDP &&
5668 !transcoder_is_dsi(cpu_transcoder)) {
5669 I915_WRITE(PIPE_MULT(cpu_transcoder),
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005670 pipe_config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005671 }
5672
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005673 if (pipe_config->has_pch_encoder) {
Maarten Lankhorst4c354752018-10-11 12:04:49 +02005674 intel_cpu_transcoder_set_m_n(pipe_config,
5675 &pipe_config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005676 }
5677
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005678 if (!transcoder_is_dsi(cpu_transcoder))
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005679 haswell_set_pipeconf(pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005680
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005681 haswell_set_pipemisc(pipe_config);
Daniel Vetter229fca92014-04-24 23:55:09 +02005682
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005683 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005684
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005685 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005686
Imre Deaked69cd42017-10-02 10:55:57 +03005687 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
5688 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005689 pipe_config->pch_pfit.enabled;
Imre Deaked69cd42017-10-02 10:55:57 +03005690 if (psl_clkgate_wa)
5691 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
5692
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005693 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005694 skylake_pfit_enable(pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005695 else
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005696 ironlake_pfit_enable(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005697
5698 /*
5699 * On ILK+ LUT must be loaded before the pipe is running but with
5700 * clocks enabled
5701 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005702 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005703
Vandita Kulkarnie16a3752018-06-21 20:43:56 +05305704 /*
5705 * Display WA #1153: enable hardware to bypass the alpha math
5706 * and rounding for per-pixel values 00 and 0xff
5707 */
5708 if (INTEL_GEN(dev_priv) >= 11) {
5709 pipe_chicken = I915_READ(PIPE_CHICKEN(pipe));
5710 if (!(pipe_chicken & PER_PIXEL_ALPHA_BYPASS_EN))
5711 I915_WRITE_FW(PIPE_CHICKEN(pipe),
5712 pipe_chicken | PER_PIXEL_ALPHA_BYPASS_EN);
5713 }
5714
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005715 intel_ddi_set_pipe_settings(pipe_config);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005716 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005717 intel_ddi_enable_transcoder_func(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005718
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005719 if (dev_priv->display.initial_watermarks != NULL)
Ville Syrjälä3125d392016-11-28 19:37:03 +02005720 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005721
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005722 if (INTEL_GEN(dev_priv) >= 11)
5723 icl_pipe_mbus_enable(intel_crtc);
5724
Jani Nikula4d1de972016-03-18 17:05:42 +02005725 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005726 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005727 intel_enable_pipe(pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005728
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005729 if (pipe_config->has_pch_encoder)
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03005730 lpt_pch_enable(old_intel_state, pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005731
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005732 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DP_MST))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005733 intel_ddi_set_vc_payload_alloc(pipe_config, true);
Dave Airlie0e32b392014-05-02 14:02:48 +10005734
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005735 assert_vblank_disabled(crtc);
5736 drm_crtc_vblank_on(crtc);
5737
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005738 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005739
Imre Deaked69cd42017-10-02 10:55:57 +03005740 if (psl_clkgate_wa) {
5741 intel_wait_for_vblank(dev_priv, pipe);
5742 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
5743 }
5744
Paulo Zanonie4916942013-09-20 16:21:19 -03005745 /* If we change the relative order between pipe/planes enabling, we need
5746 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005747 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005748 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005749 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5750 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005751 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005752}
5753
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005754static void ironlake_pfit_disable(const struct intel_crtc_state *old_crtc_state)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005755{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005756 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5757 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5758 enum pipe pipe = crtc->pipe;
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005759
5760 /* To avoid upsetting the power well on haswell only disable the pfit if
5761 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005762 if (old_crtc_state->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005763 I915_WRITE(PF_CTL(pipe), 0);
5764 I915_WRITE(PF_WIN_POS(pipe), 0);
5765 I915_WRITE(PF_WIN_SZ(pipe), 0);
5766 }
5767}
5768
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005769static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5770 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005771{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005772 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005773 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005774 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5776 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005777
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005778 /*
5779 * Sometimes spurious CPU pipe underruns happen when the
5780 * pipe is already disabled, but FDI RX/TX is still enabled.
5781 * Happens at least with VGA+HDMI cloning. Suppress them.
5782 */
Ville Syrjälä2b5b6312018-05-24 22:04:06 +03005783 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5784 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005785
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005786 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005787
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005788 drm_crtc_vblank_off(crtc);
5789 assert_vblank_disabled(crtc);
5790
Ville Syrjälä4972f702017-11-29 17:37:32 +02005791 intel_disable_pipe(old_crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005792
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005793 ironlake_pfit_disable(old_crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005794
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005795 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005796 ironlake_fdi_disable(crtc);
5797
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005798 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005799
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005800 if (old_crtc_state->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005801 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005802
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005803 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005804 i915_reg_t reg;
5805 u32 temp;
5806
Daniel Vetterd925c592013-06-05 13:34:04 +02005807 /* disable TRANS_DP_CTL */
5808 reg = TRANS_DP_CTL(pipe);
5809 temp = I915_READ(reg);
5810 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5811 TRANS_DP_PORT_SEL_MASK);
5812 temp |= TRANS_DP_PORT_SEL_NONE;
5813 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005814
Daniel Vetterd925c592013-06-05 13:34:04 +02005815 /* disable DPLL_SEL */
5816 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005817 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005818 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005819 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005820
Daniel Vetterd925c592013-06-05 13:34:04 +02005821 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005822 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005823
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005824 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005825 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005826}
5827
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005828static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5829 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005830{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005831 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005832 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Imre Deak24a28172018-06-13 20:07:06 +03005834 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005835
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005836 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005837
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005838 drm_crtc_vblank_off(crtc);
5839 assert_vblank_disabled(crtc);
5840
Jani Nikula4d1de972016-03-18 17:05:42 +02005841 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005842 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005843 intel_disable_pipe(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005844
Imre Deak24a28172018-06-13 20:07:06 +03005845 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
5846 intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005847
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005848 if (!transcoder_is_dsi(cpu_transcoder))
Clint Taylor90c3e212018-07-10 13:02:05 -07005849 intel_ddi_disable_transcoder_func(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005850
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005851 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005852 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005853 else
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005854 ironlake_pfit_disable(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005855
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005856 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Paulo Zanonic27e9172018-04-27 16:14:36 -07005857
5858 if (INTEL_GEN(dev_priv) >= 11)
5859 icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
Imre Deakbdaa29b2018-11-01 16:04:24 +02005860
5861 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005862}
5863
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005864static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005865{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005866 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5867 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005868
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005869 if (!crtc_state->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005870 return;
5871
Daniel Vetterc0b03412013-05-28 12:05:54 +02005872 /*
5873 * The panel fitter should only be adjusted whilst the pipe is disabled,
5874 * according to register description and PRM.
5875 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005876 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5877 assert_pipe_disabled(dev_priv, crtc->pipe);
5878
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005879 I915_WRITE(PFIT_PGM_RATIOS, crtc_state->gmch_pfit.pgm_ratios);
5880 I915_WRITE(PFIT_CONTROL, crtc_state->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005881
5882 /* Border color in case we don't scale up to the full screen. Black by
5883 * default, change to something else for debugging. */
5884 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005885}
5886
Mahesh Kumar176597a2018-10-04 14:20:43 +05305887bool intel_port_is_combophy(struct drm_i915_private *dev_priv, enum port port)
5888{
5889 if (port == PORT_NONE)
5890 return false;
5891
5892 if (IS_ICELAKE(dev_priv))
5893 return port <= PORT_B;
5894
5895 return false;
5896}
5897
Paulo Zanoniac213c12018-05-21 17:25:37 -07005898bool intel_port_is_tc(struct drm_i915_private *dev_priv, enum port port)
5899{
5900 if (IS_ICELAKE(dev_priv))
5901 return port >= PORT_C && port <= PORT_F;
5902
5903 return false;
5904}
5905
5906enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
5907{
5908 if (!intel_port_is_tc(dev_priv, port))
5909 return PORT_TC_NONE;
5910
5911 return port - PORT_C;
5912}
5913
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005914enum intel_display_power_domain intel_port_to_power_domain(enum port port)
Dave Airlied05410f2014-06-05 13:22:59 +10005915{
5916 switch (port) {
5917 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005918 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005919 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005920 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005921 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005922 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005923 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005924 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005925 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005926 return POWER_DOMAIN_PORT_DDI_E_LANES;
Rodrigo Vivi9787e832018-01-29 15:22:22 -08005927 case PORT_F:
5928 return POWER_DOMAIN_PORT_DDI_F_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005929 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005930 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005931 return POWER_DOMAIN_PORT_OTHER;
5932 }
5933}
5934
Imre Deak337837a2018-11-01 16:04:23 +02005935enum intel_display_power_domain
5936intel_aux_power_domain(struct intel_digital_port *dig_port)
5937{
5938 switch (dig_port->aux_ch) {
5939 case AUX_CH_A:
5940 return POWER_DOMAIN_AUX_A;
5941 case AUX_CH_B:
5942 return POWER_DOMAIN_AUX_B;
5943 case AUX_CH_C:
5944 return POWER_DOMAIN_AUX_C;
5945 case AUX_CH_D:
5946 return POWER_DOMAIN_AUX_D;
5947 case AUX_CH_E:
5948 return POWER_DOMAIN_AUX_E;
5949 case AUX_CH_F:
5950 return POWER_DOMAIN_AUX_F;
5951 default:
5952 MISSING_CASE(dig_port->aux_ch);
5953 return POWER_DOMAIN_AUX_A;
5954 }
5955}
5956
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005957static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5958 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005959{
5960 struct drm_device *dev = crtc->dev;
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005961 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005962 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5964 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005965 u64 mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005966 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005967
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005968 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005969 return 0;
5970
Imre Deak17bd6e62018-01-09 14:20:40 +02005971 mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
5972 mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005973 if (crtc_state->pch_pfit.enabled ||
5974 crtc_state->pch_pfit.force_thru)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005975 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
Imre Deak77d22dc2014-03-05 16:20:52 +02005976
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005977 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5978 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5979
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005980 mask |= BIT_ULL(intel_encoder->power_domain);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005981 }
Imre Deak319be8a2014-03-04 19:22:57 +02005982
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005983 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
Imre Deak17bd6e62018-01-09 14:20:40 +02005984 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005985
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005986 if (crtc_state->shared_dpll)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005987 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005988
Imre Deak77d22dc2014-03-05 16:20:52 +02005989 return mask;
5990}
5991
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005992static u64
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005993modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5994 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005995{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005996 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5998 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005999 u64 domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006000
6001 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01006002 intel_crtc->enabled_power_domains = new_domains =
6003 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006004
Daniel Vetter5a21b662016-05-24 17:13:53 +02006005 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006006
6007 for_each_power_domain(domain, domains)
6008 intel_display_power_get(dev_priv, domain);
6009
Daniel Vetter5a21b662016-05-24 17:13:53 +02006010 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006011}
6012
6013static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02006014 u64 domains)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006015{
6016 enum intel_display_power_domain domain;
6017
6018 for_each_power_domain(domain, domains)
6019 intel_display_power_put(dev_priv, domain);
6020}
6021
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006022static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6023 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006024{
Ville Syrjäläff32c542017-03-02 19:14:57 +02006025 struct intel_atomic_state *old_intel_state =
6026 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006027 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006028 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006029 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006031 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006032
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006033 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006034 return;
6035
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006036 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006037 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006038
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02006039 intel_set_pipe_timings(pipe_config);
6040 intel_set_pipe_src_size(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006041
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006042 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006043 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6044 I915_WRITE(CHV_CANVAS(pipe), 0);
6045 }
6046
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02006047 i9xx_set_pipeconf(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006048
P Raviraj Sitaramc59d2da2018-09-10 19:57:14 +05306049 intel_color_set_csc(&pipe_config->base);
6050
Jesse Barnes89b667f2013-04-18 14:51:36 -07006051 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006052
Daniel Vettera72e4c92014-09-30 10:56:47 +02006053 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006054
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006055 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006056
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006057 if (IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006058 chv_prepare_pll(intel_crtc, pipe_config);
6059 chv_enable_pll(intel_crtc, pipe_config);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006060 } else {
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006061 vlv_prepare_pll(intel_crtc, pipe_config);
6062 vlv_enable_pll(intel_crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006063 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006064
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006065 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006067 i9xx_pfit_enable(pipe_config);
Jesse Barnes2dd24552013-04-25 12:55:01 -07006068
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006069 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006070
Ville Syrjäläff32c542017-03-02 19:14:57 +02006071 dev_priv->display.initial_watermarks(old_intel_state,
6072 pipe_config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02006073 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006074
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006075 assert_vblank_disabled(crtc);
6076 drm_crtc_vblank_on(crtc);
6077
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006078 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006079}
6080
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006081static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006082{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006083 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6084 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006085
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006086 I915_WRITE(FP0(crtc->pipe), crtc_state->dpll_hw_state.fp0);
6087 I915_WRITE(FP1(crtc->pipe), crtc_state->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006088}
6089
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006090static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6091 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006092{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006093 struct intel_atomic_state *old_intel_state =
6094 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006095 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006096 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006097 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006099 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006100
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006101 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006102 return;
6103
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006104 i9xx_set_pll_dividers(pipe_config);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006105
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006106 if (intel_crtc_has_dp_encoder(pipe_config))
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006107 intel_dp_set_m_n(pipe_config, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006108
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02006109 intel_set_pipe_timings(pipe_config);
6110 intel_set_pipe_src_size(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006111
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02006112 i9xx_set_pipeconf(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006113
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006114 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006115
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006116 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006117 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006118
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006119 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006120
Ville Syrjälä939994d2017-09-13 17:08:56 +03006121 i9xx_enable_pll(intel_crtc, pipe_config);
Daniel Vetterf6736a12013-06-05 13:34:30 +02006122
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006123 i9xx_pfit_enable(pipe_config);
Jesse Barnes2dd24552013-04-25 12:55:01 -07006124
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006125 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006126
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006127 if (dev_priv->display.initial_watermarks != NULL)
6128 dev_priv->display.initial_watermarks(old_intel_state,
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006129 pipe_config);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006130 else
6131 intel_update_watermarks(intel_crtc);
Ville Syrjälä4972f702017-11-29 17:37:32 +02006132 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006133
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006134 assert_vblank_disabled(crtc);
6135 drm_crtc_vblank_on(crtc);
6136
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006137 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006138}
6139
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006140static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
Daniel Vetter87476d62013-04-11 16:29:06 +02006141{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006142 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
6143 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006144
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006145 if (!old_crtc_state->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006146 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006147
6148 assert_pipe_disabled(dev_priv, crtc->pipe);
6149
Chris Wilson43031782018-09-13 14:16:26 +01006150 DRM_DEBUG_KMS("disabling pfit, current: 0x%08x\n",
6151 I915_READ(PFIT_CONTROL));
Daniel Vetter328d8e82013-05-08 10:36:31 +02006152 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006153}
6154
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006155static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6156 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006157{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006158 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006159 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006160 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6162 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006163
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006164 /*
6165 * On gen2 planes are double buffered but the pipe isn't, so we must
6166 * wait for planes to fully turn off before disabling the pipe.
6167 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006168 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02006169 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006170
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006171 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006172
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006173 drm_crtc_vblank_off(crtc);
6174 assert_vblank_disabled(crtc);
6175
Ville Syrjälä4972f702017-11-29 17:37:32 +02006176 intel_disable_pipe(old_crtc_state);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006177
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006178 i9xx_pfit_disable(old_crtc_state);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006179
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006180 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006181
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006182 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006183 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006184 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006185 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006186 vlv_disable_pll(dev_priv, pipe);
6187 else
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006188 i9xx_disable_pll(old_crtc_state);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006189 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006190
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006191 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006192
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006193 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006194 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006195
6196 if (!dev_priv->display.initial_watermarks)
6197 intel_update_watermarks(intel_crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +03006198
6199 /* clock the pipe down to 640x480@60 to potentially save power */
6200 if (IS_I830(dev_priv))
6201 i830_enable_pipe(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006202}
6203
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006204static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
6205 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006206{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006207 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006209 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006210 enum intel_display_power_domain domain;
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006211 struct intel_plane *plane;
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02006212 u64 domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006213 struct drm_atomic_state *state;
6214 struct intel_crtc_state *crtc_state;
6215 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006216
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006217 if (!intel_crtc->active)
6218 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006219
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006220 for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
6221 const struct intel_plane_state *plane_state =
6222 to_intel_plane_state(plane->base.state);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006223
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006224 if (plane_state->base.visible)
6225 intel_plane_disable_noatomic(intel_crtc, plane);
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006226 }
6227
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006228 state = drm_atomic_state_alloc(crtc->dev);
Ander Conselvan de Oliveira31bb2ef2017-01-20 16:28:45 +02006229 if (!state) {
6230 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
6231 crtc->base.id, crtc->name);
6232 return;
6233 }
6234
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006235 state->acquire_ctx = ctx;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006236
6237 /* Everything's already locked, -EDEADLK can't happen. */
6238 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6239 ret = drm_atomic_add_affected_connectors(state, crtc);
6240
6241 WARN_ON(IS_ERR(crtc_state) || ret);
6242
6243 dev_priv->display.crtc_disable(crtc_state, state);
6244
Chris Wilson08536952016-10-14 13:18:18 +01006245 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006246
Ville Syrjälä78108b72016-05-27 20:59:19 +03006247 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6248 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006249
6250 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6251 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006252 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006253 crtc->enabled = false;
6254 crtc->state->connector_mask = 0;
6255 crtc->state->encoder_mask = 0;
6256
6257 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6258 encoder->base.crtc = NULL;
6259
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006260 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02006261 intel_update_watermarks(intel_crtc);
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02006262 intel_disable_shared_dpll(to_intel_crtc_state(crtc->state));
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006263
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006264 domains = intel_crtc->enabled_power_domains;
6265 for_each_power_domain(domain, domains)
6266 intel_display_power_put(dev_priv, domain);
6267 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006268
6269 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
Ville Syrjäläd305e062017-08-30 21:57:03 +03006270 dev_priv->min_cdclk[intel_crtc->pipe] = 0;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +03006271 dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006272}
6273
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006274/*
6275 * turn all crtc's off, but do not adjust state
6276 * This has to be paired with a call to intel_modeset_setup_hw_state.
6277 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006278int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006279{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006280 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006281 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006282 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006283
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006284 state = drm_atomic_helper_suspend(dev);
6285 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006286 if (ret)
6287 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006288 else
6289 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006290 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006291}
6292
Chris Wilsonea5b2132010-08-04 13:50:23 +01006293void intel_encoder_destroy(struct drm_encoder *encoder)
6294{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006295 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006296
Chris Wilsonea5b2132010-08-04 13:50:23 +01006297 drm_encoder_cleanup(encoder);
6298 kfree(intel_encoder);
6299}
6300
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006301/* Cross check the actual hw state with our own modeset state tracking (and it's
6302 * internal consistency). */
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006303static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
6304 struct drm_connector_state *conn_state)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006305{
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006306 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006307
6308 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6309 connector->base.base.id,
6310 connector->base.name);
6311
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006312 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006313 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006314
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006315 I915_STATE_WARN(!crtc_state,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006316 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006317
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006318 if (!crtc_state)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006319 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006320
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006321 I915_STATE_WARN(!crtc_state->active,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006322 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006323
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006324 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006325 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006326
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006327 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006328 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006329
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006330 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006331 "attached encoder crtc differs from connector crtc\n");
6332 } else {
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006333 I915_STATE_WARN(crtc_state && crtc_state->active,
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006334 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006335 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006336 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006337 }
6338}
6339
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006340static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006341{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006342 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6343 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006344
6345 return 0;
6346}
6347
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006348static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006349 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006350{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006351 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006352 struct drm_atomic_state *state = pipe_config->base.state;
6353 struct intel_crtc *other_crtc;
6354 struct intel_crtc_state *other_crtc_state;
6355
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006356 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6357 pipe_name(pipe), pipe_config->fdi_lanes);
6358 if (pipe_config->fdi_lanes > 4) {
6359 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6360 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006361 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006362 }
6363
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006364 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006365 if (pipe_config->fdi_lanes > 2) {
6366 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6367 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006368 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006369 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006370 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006371 }
6372 }
6373
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00006374 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006375 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006376
6377 /* Ivybridge 3 pipe is really complicated */
6378 switch (pipe) {
6379 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006380 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006381 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006382 if (pipe_config->fdi_lanes <= 2)
6383 return 0;
6384
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006385 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006386 other_crtc_state =
6387 intel_atomic_get_crtc_state(state, other_crtc);
6388 if (IS_ERR(other_crtc_state))
6389 return PTR_ERR(other_crtc_state);
6390
6391 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006392 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6393 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006395 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006396 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006398 if (pipe_config->fdi_lanes > 2) {
6399 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6400 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006402 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006403
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006404 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006405 other_crtc_state =
6406 intel_atomic_get_crtc_state(state, other_crtc);
6407 if (IS_ERR(other_crtc_state))
6408 return PTR_ERR(other_crtc_state);
6409
6410 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006411 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006412 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006413 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006414 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006415 default:
6416 BUG();
6417 }
6418}
6419
Daniel Vettere29c22c2013-02-21 00:00:16 +01006420#define RETRY 1
6421static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006422 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006423{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006424 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006425 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006426 int lane, link_bw, fdi_dotclock, ret;
6427 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006428
Daniel Vettere29c22c2013-02-21 00:00:16 +01006429retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006430 /* FDI is a binary signal running at ~2.7GHz, encoding
6431 * each output octet as 10 bits. The actual frequency
6432 * is stored as a divider into a 100MHz clock, and the
6433 * mode pixel clock is stored in units of 1KHz.
6434 * Hence the bw of each lane in terms of the mode signal
6435 * is:
6436 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006437 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006438
Damien Lespiau241bfc32013-09-25 16:45:37 +01006439 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006440
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006441 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006442 pipe_config->pipe_bpp);
6443
6444 pipe_config->fdi_lanes = lane;
6445
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006446 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006447 link_bw, &pipe_config->fdi_m_n, false);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006449 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006450 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006451 pipe_config->pipe_bpp -= 2*3;
6452 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6453 pipe_config->pipe_bpp);
6454 needs_recompute = true;
6455 pipe_config->bw_constrained = true;
6456
6457 goto retry;
6458 }
6459
6460 if (needs_recompute)
6461 return RETRY;
6462
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006463 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006464}
6465
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006466bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006467{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006468 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6469 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6470
6471 /* IPS only exists on ULT machines and is tied to pipe A. */
6472 if (!hsw_crtc_supports_ips(crtc))
Ville Syrjälä6e644622017-08-17 17:55:09 +03006473 return false;
6474
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006475 if (!i915_modparams.enable_ips)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006476 return false;
6477
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006478 if (crtc_state->pipe_bpp > 24)
6479 return false;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006480
6481 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006482 * We compare against max which means we must take
6483 * the increased cdclk requirement into account when
6484 * calculating the new cdclk.
6485 *
6486 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006487 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006488 if (IS_BROADWELL(dev_priv) &&
6489 crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
6490 return false;
6491
6492 return true;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006493}
6494
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006495static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006496{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006497 struct drm_i915_private *dev_priv =
6498 to_i915(crtc_state->base.crtc->dev);
6499 struct intel_atomic_state *intel_state =
6500 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006501
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006502 if (!hsw_crtc_state_ips_capable(crtc_state))
6503 return false;
6504
6505 if (crtc_state->ips_force_disable)
6506 return false;
6507
Maarten Lankhorstadbe5c52017-11-22 19:39:06 +01006508 /* IPS should be fine as long as at least one plane is enabled. */
6509 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006510 return false;
6511
6512 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
6513 if (IS_BROADWELL(dev_priv) &&
6514 crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
6515 return false;
6516
6517 return true;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006518}
6519
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006520static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6521{
6522 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6523
6524 /* GDG double wide on either pipe, otherwise pipe A only */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00006525 return INTEL_GEN(dev_priv) < 4 &&
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006526 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6527}
6528
Ville Syrjäläceb99322017-01-20 20:22:05 +02006529static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6530{
6531 uint32_t pixel_rate;
6532
6533 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6534
6535 /*
6536 * We only use IF-ID interlacing. If we ever use
6537 * PF-ID we'll need to adjust the pixel_rate here.
6538 */
6539
6540 if (pipe_config->pch_pfit.enabled) {
6541 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6542 uint32_t pfit_size = pipe_config->pch_pfit.size;
6543
6544 pipe_w = pipe_config->pipe_src_w;
6545 pipe_h = pipe_config->pipe_src_h;
6546
6547 pfit_w = (pfit_size >> 16) & 0xFFFF;
6548 pfit_h = pfit_size & 0xFFFF;
6549 if (pipe_w < pfit_w)
6550 pipe_w = pfit_w;
6551 if (pipe_h < pfit_h)
6552 pipe_h = pfit_h;
6553
6554 if (WARN_ON(!pfit_w || !pfit_h))
6555 return pixel_rate;
6556
6557 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6558 pfit_w * pfit_h);
6559 }
6560
6561 return pixel_rate;
6562}
6563
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006564static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
6565{
6566 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
6567
6568 if (HAS_GMCH_DISPLAY(dev_priv))
6569 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6570 crtc_state->pixel_rate =
6571 crtc_state->base.adjusted_mode.crtc_clock;
6572 else
6573 crtc_state->pixel_rate =
6574 ilk_pipe_pixel_rate(crtc_state);
6575}
6576
Daniel Vettera43f6e02013-06-07 23:10:32 +02006577static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006578 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006579{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006580 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006581 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006582 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006583 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006584
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006585 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006586 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006587
6588 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006589 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006590 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006591 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006592 if (intel_crtc_supports_double_wide(crtc) &&
6593 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006594 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006595 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006596 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006597 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006598
Ville Syrjäläf3261152016-05-24 21:34:18 +03006599 if (adjusted_mode->crtc_clock > clock_limit) {
6600 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6601 adjusted_mode->crtc_clock, clock_limit,
6602 yesno(pipe_config->double_wide));
6603 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006604 }
Chris Wilson89749352010-09-12 18:25:19 +01006605
Shashank Sharma8c79f842018-10-12 11:53:09 +05306606 if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
6607 pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
6608 pipe_config->base.ctm) {
Shashank Sharma25edf912017-07-21 20:55:07 +05306609 /*
6610 * There is only one pipe CSC unit per pipe, and we need that
6611 * for output conversion from RGB->YCBCR. So if CTM is already
6612 * applied we can't support YCBCR420 output.
6613 */
6614 DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
6615 return -EINVAL;
6616 }
6617
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006618 /*
6619 * Pipe horizontal size must be even in:
6620 * - DVO ganged mode
6621 * - LVDS dual channel mode
6622 * - Double wide pipe
6623 */
Ville Syrjälä0574bd82017-11-23 21:04:48 +02006624 if (pipe_config->pipe_src_w & 1) {
6625 if (pipe_config->double_wide) {
6626 DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
6627 return -EINVAL;
6628 }
6629
6630 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6631 intel_is_dual_link_lvds(dev)) {
6632 DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
6633 return -EINVAL;
6634 }
6635 }
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006636
Damien Lespiau8693a822013-05-03 18:48:11 +01006637 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6638 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006639 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006640 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006641 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006642 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006643
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006644 intel_crtc_compute_pixel_rate(pipe_config);
6645
Daniel Vetter877d48d2013-04-19 11:24:43 +02006646 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006647 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006648
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006649 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006650}
6651
Zhenyu Wang2c072452009-06-05 15:38:42 +08006652static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006653intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006654{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006655 while (*num > DATA_LINK_M_N_MASK ||
6656 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006657 *num >>= 1;
6658 *den >>= 1;
6659 }
6660}
6661
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006662static void compute_m_n(unsigned int m, unsigned int n,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006663 uint32_t *ret_m, uint32_t *ret_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006664 bool constant_n)
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006665{
Jani Nikula9a86cda2017-03-27 14:33:25 +03006666 /*
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006667 * Several DP dongles in particular seem to be fussy about
6668 * too large link M/N values. Give N value as 0x8000 that
6669 * should be acceptable by specific devices. 0x8000 is the
6670 * specified fixed N value for asynchronous clock mode,
6671 * which the devices expect also in synchronous clock mode.
Jani Nikula9a86cda2017-03-27 14:33:25 +03006672 */
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006673 if (constant_n)
6674 *ret_n = 0x8000;
6675 else
6676 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
Jani Nikula9a86cda2017-03-27 14:33:25 +03006677
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006678 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6679 intel_reduce_m_n_ratio(ret_m, ret_n);
6680}
6681
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006682void
6683intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6684 int pixel_clock, int link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006685 struct intel_link_m_n *m_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006686 bool constant_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006687{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006688 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006689
6690 compute_m_n(bits_per_pixel * pixel_clock,
6691 link_clock * nlanes * 8,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006692 &m_n->gmch_m, &m_n->gmch_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006693 constant_n);
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006694
6695 compute_m_n(pixel_clock, link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006696 &m_n->link_m, &m_n->link_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006697 constant_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006698}
6699
Chris Wilsona7615032011-01-12 17:04:08 +00006700static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6701{
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00006702 if (i915_modparams.panel_use_ssc >= 0)
6703 return i915_modparams.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006704 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006705 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006706}
6707
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006708static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006709{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006710 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006711}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006712
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006713static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6714{
6715 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006716}
6717
Daniel Vetterf47709a2013-03-28 10:42:02 +01006718static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006719 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006720 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08006721{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006722 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006723 u32 fp, fp2 = 0;
6724
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006725 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006726 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006727 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006728 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006729 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006730 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006731 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006732 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006733 }
6734
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006735 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006736
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006737 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006738 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006739 crtc_state->dpll_hw_state.fp1 = fp2;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006740 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006741 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006742 }
6743}
6744
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006745static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6746 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006747{
6748 u32 reg_val;
6749
6750 /*
6751 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6752 * and set it to a reasonable value instead.
6753 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006754 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006755 reg_val &= 0xffffff00;
6756 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006757 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006758
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006759 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Imre Deaked585702017-05-10 12:21:47 +03006760 reg_val &= 0x00ffffff;
6761 reg_val |= 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006762 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006763
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006764 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006765 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006766 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006767
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006768 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006769 reg_val &= 0x00ffffff;
6770 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006771 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006772}
6773
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006774static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
6775 const struct intel_link_m_n *m_n)
Daniel Vetterb5518422013-05-03 11:49:48 +02006776{
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006777 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6778 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6779 enum pipe pipe = crtc->pipe;
Daniel Vetterb5518422013-05-03 11:49:48 +02006780
Daniel Vettere3b95f12013-05-03 11:49:49 +02006781 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6782 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6783 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6784 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006785}
6786
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02006787static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
6788 enum transcoder transcoder)
6789{
6790 if (IS_HASWELL(dev_priv))
6791 return transcoder == TRANSCODER_EDP;
6792
6793 /*
6794 * Strictly speaking some registers are available before
6795 * gen7, but we only support DRRS on gen7+
6796 */
6797 return IS_GEN7(dev_priv) || IS_CHERRYVIEW(dev_priv);
6798}
6799
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006800static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
6801 const struct intel_link_m_n *m_n,
6802 const struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006803{
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006804 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006805 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006806 enum pipe pipe = crtc->pipe;
6807 enum transcoder transcoder = crtc_state->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006808
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006809 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02006810 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6811 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6812 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6813 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02006814 /*
6815 * M2_N2 registers are set only if DRRS is supported
6816 * (to make sure the registers are not unnecessarily accessed).
Vandana Kannanf769cd22014-08-05 07:51:22 -07006817 */
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02006818 if (m2_n2 && crtc_state->has_drrs &&
6819 transcoder_has_m2_n2(dev_priv, transcoder)) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006820 I915_WRITE(PIPE_DATA_M2(transcoder),
6821 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6822 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6823 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6824 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6825 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006826 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006827 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6828 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6829 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6830 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006831 }
6832}
6833
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006834void 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 +02006835{
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006836 const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306837
6838 if (m_n == M1_N1) {
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006839 dp_m_n = &crtc_state->dp_m_n;
6840 dp_m2_n2 = &crtc_state->dp_m2_n2;
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306841 } else if (m_n == M2_N2) {
6842
6843 /*
6844 * M2_N2 registers are not supported. Hence m2_n2 divider value
6845 * needs to be programmed into M1_N1.
6846 */
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006847 dp_m_n = &crtc_state->dp_m2_n2;
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306848 } else {
6849 DRM_ERROR("Unsupported divider value\n");
6850 return;
6851 }
6852
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006853 if (crtc_state->has_pch_encoder)
6854 intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006855 else
Maarten Lankhorst4c354752018-10-11 12:04:49 +02006856 intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006857}
6858
Daniel Vetter251ac862015-06-18 10:30:24 +02006859static void vlv_compute_dpll(struct intel_crtc *crtc,
6860 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006861{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006862 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006863 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006864 if (crtc->pipe != PIPE_A)
6865 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006866
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006867 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006868 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006869 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6870 DPLL_EXT_BUFFER_ENABLE_VLV;
6871
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006872 pipe_config->dpll_hw_state.dpll_md =
6873 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6874}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006875
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006876static void chv_compute_dpll(struct intel_crtc *crtc,
6877 struct intel_crtc_state *pipe_config)
6878{
6879 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006880 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006881 if (crtc->pipe != PIPE_A)
6882 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6883
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006884 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006885 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006886 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6887
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006888 pipe_config->dpll_hw_state.dpll_md =
6889 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006890}
6891
Ville Syrjäläd288f652014-10-28 13:20:22 +02006892static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006893 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006894{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006895 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006896 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006897 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006898 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006899 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006900 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006901
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006902 /* Enable Refclk */
6903 I915_WRITE(DPLL(pipe),
6904 pipe_config->dpll_hw_state.dpll &
6905 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6906
6907 /* No need to actually set up the DPLL with DSI */
6908 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6909 return;
6910
Ville Syrjäläa5805162015-05-26 20:42:30 +03006911 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01006912
Ville Syrjäläd288f652014-10-28 13:20:22 +02006913 bestn = pipe_config->dpll.n;
6914 bestm1 = pipe_config->dpll.m1;
6915 bestm2 = pipe_config->dpll.m2;
6916 bestp1 = pipe_config->dpll.p1;
6917 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006918
Jesse Barnes89b667f2013-04-18 14:51:36 -07006919 /* See eDP HDMI DPIO driver vbios notes doc */
6920
6921 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006922 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006923 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006924
6925 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006926 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006927
6928 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006929 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006930 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006931 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006932
6933 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006934 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006935
6936 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006937 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6938 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6939 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006940 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006941
6942 /*
6943 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6944 * but we don't support that).
6945 * Note: don't use the DAC post divider as it seems unstable.
6946 */
6947 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006948 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006949
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006950 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006951 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006952
Jesse Barnes89b667f2013-04-18 14:51:36 -07006953 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006954 if (pipe_config->port_clock == 162000 ||
Maarten Lankhorst92d54b02018-10-11 12:04:50 +02006955 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
6956 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006957 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006958 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006959 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006961 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006962
Ville Syrjälä37a56502016-06-22 21:57:04 +03006963 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006964 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006965 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006966 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006967 0x0df40000);
6968 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006970 0x0df70000);
6971 } else { /* HDMI or VGA */
6972 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006973 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006974 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006975 0x0df70000);
6976 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006977 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006978 0x0df40000);
6979 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006980
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006981 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006982 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Maarten Lankhorst92d54b02018-10-11 12:04:50 +02006983 if (intel_crtc_has_dp_encoder(pipe_config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006984 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006985 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006986
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006988 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006989}
6990
Ville Syrjäläd288f652014-10-28 13:20:22 +02006991static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006992 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006993{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006994 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006995 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006996 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006997 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306998 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006999 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307000 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307001 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007002
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007003 /* Enable Refclk and SSC */
7004 I915_WRITE(DPLL(pipe),
7005 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7006
7007 /* No need to actually set up the DPLL with DSI */
7008 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7009 return;
7010
Ville Syrjäläd288f652014-10-28 13:20:22 +02007011 bestn = pipe_config->dpll.n;
7012 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7013 bestm1 = pipe_config->dpll.m1;
7014 bestm2 = pipe_config->dpll.m2 >> 22;
7015 bestp1 = pipe_config->dpll.p1;
7016 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307017 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307018 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307019 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007020
Ville Syrjäläa5805162015-05-26 20:42:30 +03007021 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007022
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007023 /* p1 and p2 divider */
7024 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7025 5 << DPIO_CHV_S1_DIV_SHIFT |
7026 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7027 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7028 1 << DPIO_CHV_K_DIV_SHIFT);
7029
7030 /* Feedback post-divider - m2 */
7031 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7032
7033 /* Feedback refclk divider - n and m1 */
7034 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7035 DPIO_CHV_M1_DIV_BY_2 |
7036 1 << DPIO_CHV_N_DIV_SHIFT);
7037
7038 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007039 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007040
7041 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307042 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7043 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7044 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7045 if (bestm2_frac)
7046 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7047 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007048
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307049 /* Program digital lock detect threshold */
7050 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7051 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7052 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7053 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7054 if (!bestm2_frac)
7055 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7056 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7057
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007058 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307059 if (vco == 5400000) {
7060 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7061 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7062 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7063 tribuf_calcntr = 0x9;
7064 } else if (vco <= 6200000) {
7065 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7066 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7067 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7068 tribuf_calcntr = 0x9;
7069 } else if (vco <= 6480000) {
7070 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7071 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7072 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7073 tribuf_calcntr = 0x8;
7074 } else {
7075 /* Not supported. Apply the same limits as in the max case */
7076 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7077 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7078 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7079 tribuf_calcntr = 0;
7080 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007081 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7082
Ville Syrjälä968040b2015-03-11 22:52:08 +02007083 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307084 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7085 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7086 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7087
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007088 /* AFC Recal */
7089 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7090 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7091 DPIO_AFC_RECAL);
7092
Ville Syrjäläa5805162015-05-26 20:42:30 +03007093 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007094}
7095
Ville Syrjäläd288f652014-10-28 13:20:22 +02007096/**
7097 * vlv_force_pll_on - forcibly enable just the PLL
7098 * @dev_priv: i915 private structure
7099 * @pipe: pipe PLL to enable
7100 * @dpll: PLL configuration
7101 *
7102 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7103 * in cases where we need the PLL enabled even when @pipe is not going to
7104 * be enabled.
7105 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007106int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007107 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007108{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007109 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007110 struct intel_crtc_state *pipe_config;
7111
7112 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7113 if (!pipe_config)
7114 return -ENOMEM;
7115
7116 pipe_config->base.crtc = &crtc->base;
7117 pipe_config->pixel_multiplier = 1;
7118 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007119
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007120 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007121 chv_compute_dpll(crtc, pipe_config);
7122 chv_prepare_pll(crtc, pipe_config);
7123 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007124 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007125 vlv_compute_dpll(crtc, pipe_config);
7126 vlv_prepare_pll(crtc, pipe_config);
7127 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007128 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007129
7130 kfree(pipe_config);
7131
7132 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007133}
7134
7135/**
7136 * vlv_force_pll_off - forcibly disable just the PLL
7137 * @dev_priv: i915 private structure
7138 * @pipe: pipe PLL to disable
7139 *
7140 * Disable the PLL for @pipe. To be used in cases where we need
7141 * the PLL enabled even when @pipe is not going to be enabled.
7142 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007143void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007144{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007145 if (IS_CHERRYVIEW(dev_priv))
7146 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007147 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007148 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007149}
7150
Daniel Vetter251ac862015-06-18 10:30:24 +02007151static void i9xx_compute_dpll(struct intel_crtc *crtc,
7152 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007153 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007154{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007155 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007156 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007157 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007158
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007159 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307160
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007161 dpll = DPLL_VGA_MODE_DIS;
7162
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007163 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007164 dpll |= DPLLB_MODE_LVDS;
7165 else
7166 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007167
Jani Nikula73f67aa2016-12-07 22:48:09 +02007168 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
7169 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007170 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007171 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007172 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007173
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007174 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7175 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007176 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007177
Ville Syrjälä37a56502016-06-22 21:57:04 +03007178 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007179 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007180
7181 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007182 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007183 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7184 else {
7185 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007186 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007187 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7188 }
7189 switch (clock->p2) {
7190 case 5:
7191 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7192 break;
7193 case 7:
7194 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7195 break;
7196 case 10:
7197 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7198 break;
7199 case 14:
7200 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7201 break;
7202 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007203 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007204 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7205
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007206 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007207 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007208 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007209 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007210 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7211 else
7212 dpll |= PLL_REF_INPUT_DREFCLK;
7213
7214 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007215 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007216
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007217 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007218 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007219 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007220 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007221 }
7222}
7223
Daniel Vetter251ac862015-06-18 10:30:24 +02007224static void i8xx_compute_dpll(struct intel_crtc *crtc,
7225 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007226 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007227{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007228 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007229 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007230 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007231 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007232
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007233 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307234
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007235 dpll = DPLL_VGA_MODE_DIS;
7236
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007237 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007238 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7239 } else {
7240 if (clock->p1 == 2)
7241 dpll |= PLL_P1_DIVIDE_BY_TWO;
7242 else
7243 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7244 if (clock->p2 == 4)
7245 dpll |= PLL_P2_DIVIDE_BY_4;
7246 }
7247
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007248 if (!IS_I830(dev_priv) &&
7249 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007250 dpll |= DPLL_DVO_2X_MODE;
7251
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007252 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007253 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007254 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7255 else
7256 dpll |= PLL_REF_INPUT_DREFCLK;
7257
7258 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007259 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007260}
7261
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007262static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007263{
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007264 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7265 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7266 enum pipe pipe = crtc->pipe;
7267 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
7268 const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007269 uint32_t crtc_vtotal, crtc_vblank_end;
7270 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007271
7272 /* We need to be careful not to changed the adjusted mode, for otherwise
7273 * the hw state checker will get angry at the mismatch. */
7274 crtc_vtotal = adjusted_mode->crtc_vtotal;
7275 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007276
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007277 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007278 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007279 crtc_vtotal -= 1;
7280 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007281
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007282 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007283 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7284 else
7285 vsyncshift = adjusted_mode->crtc_hsync_start -
7286 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007287 if (vsyncshift < 0)
7288 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007289 }
7290
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007291 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007292 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007293
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007294 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007295 (adjusted_mode->crtc_hdisplay - 1) |
7296 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007297 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007298 (adjusted_mode->crtc_hblank_start - 1) |
7299 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007300 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007301 (adjusted_mode->crtc_hsync_start - 1) |
7302 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7303
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007304 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007305 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007306 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007307 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007308 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007309 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007310 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007311 (adjusted_mode->crtc_vsync_start - 1) |
7312 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7313
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007314 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7315 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7316 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7317 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01007318 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007319 (pipe == PIPE_B || pipe == PIPE_C))
7320 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7321
Jani Nikulabc58be62016-03-18 17:05:39 +02007322}
7323
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007324static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
Jani Nikulabc58be62016-03-18 17:05:39 +02007325{
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007326 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7327 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7328 enum pipe pipe = crtc->pipe;
Jani Nikulabc58be62016-03-18 17:05:39 +02007329
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007330 /* pipesrc controls the size that is scaled from, which should
7331 * always be the user's requested size.
7332 */
7333 I915_WRITE(PIPESRC(pipe),
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007334 ((crtc_state->pipe_src_w - 1) << 16) |
7335 (crtc_state->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007336}
7337
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007338static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007339 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007340{
7341 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007342 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007343 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7344 uint32_t tmp;
7345
7346 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007347 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7348 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007349 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007350 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7351 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007352 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007353 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7354 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007355
7356 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007357 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7358 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007359 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007360 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7361 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007362 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007363 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7364 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007365
7366 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007367 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7368 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7369 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007370 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007371}
7372
7373static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7374 struct intel_crtc_state *pipe_config)
7375{
7376 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007377 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007378 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007379
7380 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007381 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7382 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7383
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007384 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7385 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007386}
7387
Daniel Vetterf6a83282014-02-11 15:28:57 -08007388void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007389 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007390{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007391 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7392 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7393 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7394 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007395
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007396 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7397 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7398 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7399 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007400
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007401 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007402 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007403
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007404 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007405
7406 mode->hsync = drm_mode_hsync(mode);
7407 mode->vrefresh = drm_mode_vrefresh(mode);
7408 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007409}
7410
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007411static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
Daniel Vetter84b046f2013-02-19 18:48:54 +01007412{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007413 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7414 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007415 uint32_t pipeconf;
7416
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007417 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007418
Ville Syrjäläe56134b2017-06-01 17:36:19 +03007419 /* we keep both pipes enabled on 830 */
7420 if (IS_I830(dev_priv))
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007421 pipeconf |= I915_READ(PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007422
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007423 if (crtc_state->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007424 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007425
Daniel Vetterff9ce462013-04-24 14:57:17 +02007426 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007427 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7428 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007429 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007430 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007431 pipeconf |= PIPECONF_DITHER_EN |
7432 PIPECONF_DITHER_TYPE_SP;
7433
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007434 switch (crtc_state->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007435 case 18:
7436 pipeconf |= PIPECONF_6BPC;
7437 break;
7438 case 24:
7439 pipeconf |= PIPECONF_8BPC;
7440 break;
7441 case 30:
7442 pipeconf |= PIPECONF_10BPC;
7443 break;
7444 default:
7445 /* Case prevented by intel_choose_pipe_bpp_dither. */
7446 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007447 }
7448 }
7449
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007450 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007451 if (INTEL_GEN(dev_priv) < 4 ||
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007452 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007453 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7454 else
7455 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7456 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007457 pipeconf |= PIPECONF_PROGRESSIVE;
7458
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007459 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007460 crtc_state->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007461 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007462
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007463 I915_WRITE(PIPECONF(crtc->pipe), pipeconf);
7464 POSTING_READ(PIPECONF(crtc->pipe));
Daniel Vetter84b046f2013-02-19 18:48:54 +01007465}
7466
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007467static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7468 struct intel_crtc_state *crtc_state)
7469{
7470 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007471 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007472 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007473 int refclk = 48000;
7474
7475 memset(&crtc_state->dpll_hw_state, 0,
7476 sizeof(crtc_state->dpll_hw_state));
7477
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007478 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007479 if (intel_panel_use_ssc(dev_priv)) {
7480 refclk = dev_priv->vbt.lvds_ssc_freq;
7481 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7482 }
7483
7484 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007485 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007486 limit = &intel_limits_i8xx_dvo;
7487 } else {
7488 limit = &intel_limits_i8xx_dac;
7489 }
7490
7491 if (!crtc_state->clock_set &&
7492 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7493 refclk, NULL, &crtc_state->dpll)) {
7494 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7495 return -EINVAL;
7496 }
7497
7498 i8xx_compute_dpll(crtc, crtc_state, NULL);
7499
7500 return 0;
7501}
7502
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007503static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7504 struct intel_crtc_state *crtc_state)
7505{
7506 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007507 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007508 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007509 int refclk = 96000;
7510
7511 memset(&crtc_state->dpll_hw_state, 0,
7512 sizeof(crtc_state->dpll_hw_state));
7513
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007514 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007515 if (intel_panel_use_ssc(dev_priv)) {
7516 refclk = dev_priv->vbt.lvds_ssc_freq;
7517 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7518 }
7519
7520 if (intel_is_dual_link_lvds(dev))
7521 limit = &intel_limits_g4x_dual_channel_lvds;
7522 else
7523 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007524 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7525 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007526 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007527 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007528 limit = &intel_limits_g4x_sdvo;
7529 } else {
7530 /* The option is for other outputs */
7531 limit = &intel_limits_i9xx_sdvo;
7532 }
7533
7534 if (!crtc_state->clock_set &&
7535 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7536 refclk, NULL, &crtc_state->dpll)) {
7537 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7538 return -EINVAL;
7539 }
7540
7541 i9xx_compute_dpll(crtc, crtc_state, NULL);
7542
7543 return 0;
7544}
7545
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007546static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7547 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007548{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007549 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007550 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007551 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007552 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007553
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007554 memset(&crtc_state->dpll_hw_state, 0,
7555 sizeof(crtc_state->dpll_hw_state));
7556
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007557 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007558 if (intel_panel_use_ssc(dev_priv)) {
7559 refclk = dev_priv->vbt.lvds_ssc_freq;
7560 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7561 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007562
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007563 limit = &intel_limits_pineview_lvds;
7564 } else {
7565 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007566 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007567
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007568 if (!crtc_state->clock_set &&
7569 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7570 refclk, NULL, &crtc_state->dpll)) {
7571 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7572 return -EINVAL;
7573 }
7574
7575 i9xx_compute_dpll(crtc, crtc_state, NULL);
7576
7577 return 0;
7578}
7579
7580static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7581 struct intel_crtc_state *crtc_state)
7582{
7583 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007584 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007585 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007586 int refclk = 96000;
7587
7588 memset(&crtc_state->dpll_hw_state, 0,
7589 sizeof(crtc_state->dpll_hw_state));
7590
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007591 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007592 if (intel_panel_use_ssc(dev_priv)) {
7593 refclk = dev_priv->vbt.lvds_ssc_freq;
7594 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007595 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007596
7597 limit = &intel_limits_i9xx_lvds;
7598 } else {
7599 limit = &intel_limits_i9xx_sdvo;
7600 }
7601
7602 if (!crtc_state->clock_set &&
7603 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7604 refclk, NULL, &crtc_state->dpll)) {
7605 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7606 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007607 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007608
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007609 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007610
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007611 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007612}
7613
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007614static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7615 struct intel_crtc_state *crtc_state)
7616{
7617 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007618 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007619
7620 memset(&crtc_state->dpll_hw_state, 0,
7621 sizeof(crtc_state->dpll_hw_state));
7622
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007623 if (!crtc_state->clock_set &&
7624 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7625 refclk, NULL, &crtc_state->dpll)) {
7626 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7627 return -EINVAL;
7628 }
7629
7630 chv_compute_dpll(crtc, crtc_state);
7631
7632 return 0;
7633}
7634
7635static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7636 struct intel_crtc_state *crtc_state)
7637{
7638 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007639 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007640
7641 memset(&crtc_state->dpll_hw_state, 0,
7642 sizeof(crtc_state->dpll_hw_state));
7643
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007644 if (!crtc_state->clock_set &&
7645 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7646 refclk, NULL, &crtc_state->dpll)) {
7647 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7648 return -EINVAL;
7649 }
7650
7651 vlv_compute_dpll(crtc, crtc_state);
7652
7653 return 0;
7654}
7655
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007656static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007657 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007658{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007659 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007660 uint32_t tmp;
7661
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007662 if (INTEL_GEN(dev_priv) <= 3 &&
7663 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007664 return;
7665
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007666 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007667 if (!(tmp & PFIT_ENABLE))
7668 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007669
Daniel Vetter06922822013-07-11 13:35:40 +02007670 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007671 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007672 if (crtc->pipe != PIPE_B)
7673 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007674 } else {
7675 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7676 return;
7677 }
7678
Daniel Vetter06922822013-07-11 13:35:40 +02007679 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007680 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007681}
7682
Jesse Barnesacbec812013-09-20 11:29:32 -07007683static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007684 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007685{
7686 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007687 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07007688 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007689 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07007690 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007691 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007692
Ville Syrjäläb5219732016-03-15 16:40:01 +02007693 /* In case of DSI, DPLL will not be used */
7694 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05307695 return;
7696
Ville Syrjäläa5805162015-05-26 20:42:30 +03007697 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007698 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007699 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007700
7701 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7702 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7703 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7704 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7705 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7706
Imre Deakdccbea32015-06-22 23:35:51 +03007707 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007708}
7709
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007710static void
7711i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7712 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007713{
7714 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007715 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007716 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
7717 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007718 enum pipe pipe;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007719 u32 val, base, offset;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007720 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007721 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007722 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007723 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007724
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007725 if (!plane->get_hw_state(plane, &pipe))
Damien Lespiau42a7b082015-02-05 19:35:13 +00007726 return;
7727
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007728 WARN_ON(pipe != crtc->pipe);
7729
Damien Lespiaud9806c92015-01-21 14:07:19 +00007730 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007731 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007732 DRM_DEBUG_KMS("failed to alloc fb\n");
7733 return;
7734 }
7735
Damien Lespiau1b842c82015-01-21 13:50:54 +00007736 fb = &intel_fb->base;
7737
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02007738 fb->dev = dev;
7739
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02007740 val = I915_READ(DSPCNTR(i9xx_plane));
7741
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007742 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00007743 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007744 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02007745 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00007746 }
7747 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007748
7749 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007750 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02007751 fb->format = drm_format_info(fourcc);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007752
Ville Syrjälä81894b22017-11-17 21:19:13 +02007753 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7754 offset = I915_READ(DSPOFFSET(i9xx_plane));
7755 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
7756 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007757 if (plane_config->tiling)
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007758 offset = I915_READ(DSPTILEOFF(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007759 else
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007760 offset = I915_READ(DSPLINOFF(i9xx_plane));
7761 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007762 } else {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007763 base = I915_READ(DSPADDR(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007764 }
7765 plane_config->base = base;
7766
7767 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007768 fb->width = ((val >> 16) & 0xfff) + 1;
7769 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007770
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007771 val = I915_READ(DSPSTRIDE(i9xx_plane));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007772 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007773
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02007774 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007775
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007776 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007777
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007778 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7779 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02007780 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00007781 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007782
Damien Lespiau2d140302015-02-05 17:22:18 +00007783 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007784}
7785
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007786static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007787 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007788{
7789 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007790 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007791 int pipe = pipe_config->cpu_transcoder;
7792 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007793 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007794 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007795 int refclk = 100000;
7796
Ville Syrjäläb5219732016-03-15 16:40:01 +02007797 /* In case of DSI, DPLL will not be used */
7798 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7799 return;
7800
Ville Syrjäläa5805162015-05-26 20:42:30 +03007801 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007802 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7803 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7804 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7805 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007806 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007807 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007808
7809 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007810 clock.m2 = (pll_dw0 & 0xff) << 22;
7811 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7812 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007813 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7814 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7815 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7816
Imre Deakdccbea32015-06-22 23:35:51 +03007817 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007818}
7819
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05307820static void intel_get_crtc_ycbcr_config(struct intel_crtc *crtc,
7821 struct intel_crtc_state *pipe_config)
7822{
7823 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7824 enum intel_output_format output = INTEL_OUTPUT_FORMAT_RGB;
7825
Shashank Sharma668b6c12018-10-12 11:53:14 +05307826 pipe_config->lspcon_downsampling = false;
7827
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05307828 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
7829 u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
7830
7831 if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
7832 bool ycbcr420_enabled = tmp & PIPEMISC_YUV420_ENABLE;
7833 bool blend = tmp & PIPEMISC_YUV420_MODE_FULL_BLEND;
7834
7835 if (ycbcr420_enabled) {
7836 /* We support 4:2:0 in full blend mode only */
7837 if (!blend)
7838 output = INTEL_OUTPUT_FORMAT_INVALID;
7839 else if (!(IS_GEMINILAKE(dev_priv) ||
7840 INTEL_GEN(dev_priv) >= 10))
7841 output = INTEL_OUTPUT_FORMAT_INVALID;
7842 else
7843 output = INTEL_OUTPUT_FORMAT_YCBCR420;
Shashank Sharma8c79f842018-10-12 11:53:09 +05307844 } else {
Shashank Sharma668b6c12018-10-12 11:53:14 +05307845 /*
7846 * Currently there is no interface defined to
7847 * check user preference between RGB/YCBCR444
7848 * or YCBCR420. So the only possible case for
7849 * YCBCR444 usage is driving YCBCR420 output
7850 * with LSPCON, when pipe is configured for
7851 * YCBCR444 output and LSPCON takes care of
7852 * downsampling it.
7853 */
7854 pipe_config->lspcon_downsampling = true;
Shashank Sharma8c79f842018-10-12 11:53:09 +05307855 output = INTEL_OUTPUT_FORMAT_YCBCR444;
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05307856 }
7857 }
7858 }
7859
7860 pipe_config->output_format = output;
7861}
7862
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007863static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007864 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007865{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007866 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02007867 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007868 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02007869 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007870
Imre Deak17290502016-02-12 18:55:11 +02007871 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7872 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02007873 return false;
7874
Shashank Sharmad9facae2018-10-12 11:53:07 +05307875 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
Daniel Vettere143a212013-07-04 12:01:15 +02007876 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02007877 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02007878
Imre Deak17290502016-02-12 18:55:11 +02007879 ret = false;
7880
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007881 tmp = I915_READ(PIPECONF(crtc->pipe));
7882 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02007883 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007884
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007885 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7886 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007887 switch (tmp & PIPECONF_BPC_MASK) {
7888 case PIPECONF_6BPC:
7889 pipe_config->pipe_bpp = 18;
7890 break;
7891 case PIPECONF_8BPC:
7892 pipe_config->pipe_bpp = 24;
7893 break;
7894 case PIPECONF_10BPC:
7895 pipe_config->pipe_bpp = 30;
7896 break;
7897 default:
7898 break;
7899 }
7900 }
7901
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007902 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007903 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007904 pipe_config->limited_color_range = true;
7905
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007906 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03007907 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7908
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007909 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02007910 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007911
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007912 i9xx_get_pfit_config(crtc, pipe_config);
7913
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007914 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02007915 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007916 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02007917 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7918 else
7919 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02007920 pipe_config->pixel_multiplier =
7921 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7922 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007923 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007924 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02007925 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02007926 tmp = I915_READ(DPLL(crtc->pipe));
7927 pipe_config->pixel_multiplier =
7928 ((tmp & SDVO_MULTIPLIER_MASK)
7929 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7930 } else {
7931 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7932 * port and will be fixed up in the encoder->get_config
7933 * function. */
7934 pipe_config->pixel_multiplier = 1;
7935 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007936 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007937 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007938 /*
7939 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7940 * on 830. Filter it out here so that we don't
7941 * report errors due to that.
7942 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007943 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007944 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7945
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007946 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7947 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007948 } else {
7949 /* Mask out read-only status bits. */
7950 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7951 DPLL_PORTC_READY_MASK |
7952 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007953 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007954
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007955 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007956 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007957 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07007958 vlv_crtc_clock_get(crtc, pipe_config);
7959 else
7960 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007961
Ville Syrjälä0f646142015-08-26 19:39:18 +03007962 /*
7963 * Normally the dotclock is filled in by the encoder .get_config()
7964 * but in case the pipe is enabled w/o any ports we need a sane
7965 * default.
7966 */
7967 pipe_config->base.adjusted_mode.crtc_clock =
7968 pipe_config->port_clock / pipe_config->pixel_multiplier;
7969
Imre Deak17290502016-02-12 18:55:11 +02007970 ret = true;
7971
7972out:
7973 intel_display_power_put(dev_priv, power_domain);
7974
7975 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007976}
7977
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007978static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007979{
Jesse Barnes13d83a62011-08-03 12:59:20 -07007980 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04007981 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007982 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007983 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007984 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007985 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007986 bool has_ck505 = false;
7987 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04007988 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007989
7990 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007991 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007992 switch (encoder->type) {
7993 case INTEL_OUTPUT_LVDS:
7994 has_panel = true;
7995 has_lvds = true;
7996 break;
7997 case INTEL_OUTPUT_EDP:
7998 has_panel = true;
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02007999 if (encoder->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008000 has_cpu_edp = true;
8001 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008002 default:
8003 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008004 }
8005 }
8006
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008007 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008008 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008009 can_ssc = has_ck505;
8010 } else {
8011 has_ck505 = false;
8012 can_ssc = true;
8013 }
8014
Lyude1c1a24d2016-06-14 11:04:09 -04008015 /* Check if any DPLLs are using the SSC source */
8016 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8017 u32 temp = I915_READ(PCH_DPLL(i));
8018
8019 if (!(temp & DPLL_VCO_ENABLE))
8020 continue;
8021
8022 if ((temp & PLL_REF_INPUT_MASK) ==
8023 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8024 using_ssc_source = true;
8025 break;
8026 }
8027 }
8028
8029 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8030 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008031
8032 /* Ironlake: try to setup display ref clock before DPLL
8033 * enabling. This is only under driver's control after
8034 * PCH B stepping, previous chipset stepping should be
8035 * ignoring this setting.
8036 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008037 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008038
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008039 /* As we must carefully and slowly disable/enable each source in turn,
8040 * compute the final state we want first and check if we need to
8041 * make any changes at all.
8042 */
8043 final = val;
8044 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008045 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008046 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008047 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008048 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8049
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008050 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008051 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008052 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008053
Keith Packard199e5d72011-09-22 12:01:57 -07008054 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008055 final |= DREF_SSC_SOURCE_ENABLE;
8056
8057 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8058 final |= DREF_SSC1_ENABLE;
8059
8060 if (has_cpu_edp) {
8061 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8062 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8063 else
8064 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8065 } else
8066 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008067 } else if (using_ssc_source) {
8068 final |= DREF_SSC_SOURCE_ENABLE;
8069 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008070 }
8071
8072 if (final == val)
8073 return;
8074
8075 /* Always enable nonspread source */
8076 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8077
8078 if (has_ck505)
8079 val |= DREF_NONSPREAD_CK505_ENABLE;
8080 else
8081 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8082
8083 if (has_panel) {
8084 val &= ~DREF_SSC_SOURCE_MASK;
8085 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008086
Keith Packard199e5d72011-09-22 12:01:57 -07008087 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008088 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008089 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008090 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008091 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008092 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008093
8094 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008095 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008096 POSTING_READ(PCH_DREF_CONTROL);
8097 udelay(200);
8098
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008099 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008100
8101 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008102 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008103 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008104 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008105 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008106 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008107 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008108 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008109 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008110
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008111 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008112 POSTING_READ(PCH_DREF_CONTROL);
8113 udelay(200);
8114 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008115 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008116
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008117 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008118
8119 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008120 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008121
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008122 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008123 POSTING_READ(PCH_DREF_CONTROL);
8124 udelay(200);
8125
Lyude1c1a24d2016-06-14 11:04:09 -04008126 if (!using_ssc_source) {
8127 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008128
Lyude1c1a24d2016-06-14 11:04:09 -04008129 /* Turn off the SSC source */
8130 val &= ~DREF_SSC_SOURCE_MASK;
8131 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008132
Lyude1c1a24d2016-06-14 11:04:09 -04008133 /* Turn off SSC1 */
8134 val &= ~DREF_SSC1_ENABLE;
8135
8136 I915_WRITE(PCH_DREF_CONTROL, val);
8137 POSTING_READ(PCH_DREF_CONTROL);
8138 udelay(200);
8139 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008140 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008141
8142 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008143}
8144
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008145static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008146{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008147 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008148
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008149 tmp = I915_READ(SOUTH_CHICKEN2);
8150 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8151 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008152
Imre Deakcf3598c2016-06-28 13:37:31 +03008153 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8154 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008155 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008156
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008157 tmp = I915_READ(SOUTH_CHICKEN2);
8158 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8159 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008160
Imre Deakcf3598c2016-06-28 13:37:31 +03008161 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8162 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008163 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008164}
8165
8166/* WaMPhyProgramming:hsw */
8167static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8168{
8169 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008170
8171 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8172 tmp &= ~(0xFF << 24);
8173 tmp |= (0x12 << 24);
8174 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8175
Paulo Zanonidde86e22012-12-01 12:04:25 -02008176 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8177 tmp |= (1 << 11);
8178 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8179
8180 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8181 tmp |= (1 << 11);
8182 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8183
Paulo Zanonidde86e22012-12-01 12:04:25 -02008184 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8185 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8186 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8187
8188 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8189 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8190 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8191
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008192 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8193 tmp &= ~(7 << 13);
8194 tmp |= (5 << 13);
8195 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008196
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008197 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8198 tmp &= ~(7 << 13);
8199 tmp |= (5 << 13);
8200 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008201
8202 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8203 tmp &= ~0xFF;
8204 tmp |= 0x1C;
8205 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8206
8207 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8208 tmp &= ~0xFF;
8209 tmp |= 0x1C;
8210 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8211
8212 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8213 tmp &= ~(0xFF << 16);
8214 tmp |= (0x1C << 16);
8215 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8216
8217 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8218 tmp &= ~(0xFF << 16);
8219 tmp |= (0x1C << 16);
8220 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8221
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008222 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8223 tmp |= (1 << 27);
8224 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008225
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008226 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8227 tmp |= (1 << 27);
8228 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008229
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008230 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8231 tmp &= ~(0xF << 28);
8232 tmp |= (4 << 28);
8233 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008234
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008235 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8236 tmp &= ~(0xF << 28);
8237 tmp |= (4 << 28);
8238 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008239}
8240
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008241/* Implements 3 different sequences from BSpec chapter "Display iCLK
8242 * Programming" based on the parameters passed:
8243 * - Sequence to enable CLKOUT_DP
8244 * - Sequence to enable CLKOUT_DP without spread
8245 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8246 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008247static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
8248 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008249{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008250 uint32_t reg, tmp;
8251
8252 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8253 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008254 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
8255 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008256 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008257
Ville Syrjäläa5805162015-05-26 20:42:30 +03008258 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008259
8260 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8261 tmp &= ~SBI_SSCCTL_DISABLE;
8262 tmp |= SBI_SSCCTL_PATHALT;
8263 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8264
8265 udelay(24);
8266
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008267 if (with_spread) {
8268 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8269 tmp &= ~SBI_SSCCTL_PATHALT;
8270 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008271
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008272 if (with_fdi) {
8273 lpt_reset_fdi_mphy(dev_priv);
8274 lpt_program_fdi_mphy(dev_priv);
8275 }
8276 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008277
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008278 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008279 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8280 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8281 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008282
Ville Syrjäläa5805162015-05-26 20:42:30 +03008283 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008284}
8285
Paulo Zanoni47701c32013-07-23 11:19:25 -03008286/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008287static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03008288{
Paulo Zanoni47701c32013-07-23 11:19:25 -03008289 uint32_t reg, tmp;
8290
Ville Syrjäläa5805162015-05-26 20:42:30 +03008291 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008292
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008293 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008294 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8295 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8296 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8297
8298 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8299 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8300 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8301 tmp |= SBI_SSCCTL_PATHALT;
8302 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8303 udelay(32);
8304 }
8305 tmp |= SBI_SSCCTL_DISABLE;
8306 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8307 }
8308
Ville Syrjäläa5805162015-05-26 20:42:30 +03008309 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008310}
8311
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008312#define BEND_IDX(steps) ((50 + (steps)) / 5)
8313
8314static const uint16_t sscdivintphase[] = {
8315 [BEND_IDX( 50)] = 0x3B23,
8316 [BEND_IDX( 45)] = 0x3B23,
8317 [BEND_IDX( 40)] = 0x3C23,
8318 [BEND_IDX( 35)] = 0x3C23,
8319 [BEND_IDX( 30)] = 0x3D23,
8320 [BEND_IDX( 25)] = 0x3D23,
8321 [BEND_IDX( 20)] = 0x3E23,
8322 [BEND_IDX( 15)] = 0x3E23,
8323 [BEND_IDX( 10)] = 0x3F23,
8324 [BEND_IDX( 5)] = 0x3F23,
8325 [BEND_IDX( 0)] = 0x0025,
8326 [BEND_IDX( -5)] = 0x0025,
8327 [BEND_IDX(-10)] = 0x0125,
8328 [BEND_IDX(-15)] = 0x0125,
8329 [BEND_IDX(-20)] = 0x0225,
8330 [BEND_IDX(-25)] = 0x0225,
8331 [BEND_IDX(-30)] = 0x0325,
8332 [BEND_IDX(-35)] = 0x0325,
8333 [BEND_IDX(-40)] = 0x0425,
8334 [BEND_IDX(-45)] = 0x0425,
8335 [BEND_IDX(-50)] = 0x0525,
8336};
8337
8338/*
8339 * Bend CLKOUT_DP
8340 * steps -50 to 50 inclusive, in steps of 5
8341 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8342 * change in clock period = -(steps / 10) * 5.787 ps
8343 */
8344static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8345{
8346 uint32_t tmp;
8347 int idx = BEND_IDX(steps);
8348
8349 if (WARN_ON(steps % 5 != 0))
8350 return;
8351
8352 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8353 return;
8354
8355 mutex_lock(&dev_priv->sb_lock);
8356
8357 if (steps % 10 != 0)
8358 tmp = 0xAAAAAAAB;
8359 else
8360 tmp = 0x00000000;
8361 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8362
8363 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8364 tmp &= 0xffff0000;
8365 tmp |= sscdivintphase[idx];
8366 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8367
8368 mutex_unlock(&dev_priv->sb_lock);
8369}
8370
8371#undef BEND_IDX
8372
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008373static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008374{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008375 struct intel_encoder *encoder;
8376 bool has_vga = false;
8377
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008378 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008379 switch (encoder->type) {
8380 case INTEL_OUTPUT_ANALOG:
8381 has_vga = true;
8382 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008383 default:
8384 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008385 }
8386 }
8387
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008388 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008389 lpt_bend_clkout_dp(dev_priv, 0);
8390 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008391 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008392 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008393 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008394}
8395
Paulo Zanonidde86e22012-12-01 12:04:25 -02008396/*
8397 * Initialize reference clocks when the driver loads
8398 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008399void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008400{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008401 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008402 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008403 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008404 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008405}
8406
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008407static void ironlake_set_pipeconf(const struct intel_crtc_state *crtc_state)
Paulo Zanonic8203562012-09-12 10:06:29 -03008408{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008409 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8410 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8411 enum pipe pipe = crtc->pipe;
Paulo Zanonic8203562012-09-12 10:06:29 -03008412 uint32_t val;
8413
Daniel Vetter78114072013-06-13 00:54:57 +02008414 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008415
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008416 switch (crtc_state->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008417 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008418 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008419 break;
8420 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008421 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008422 break;
8423 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008424 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008425 break;
8426 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008427 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008428 break;
8429 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008430 /* Case prevented by intel_choose_pipe_bpp_dither. */
8431 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008432 }
8433
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008434 if (crtc_state->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008435 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8436
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008437 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008438 val |= PIPECONF_INTERLACED_ILK;
8439 else
8440 val |= PIPECONF_PROGRESSIVE;
8441
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008442 if (crtc_state->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008443 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008444
Paulo Zanonic8203562012-09-12 10:06:29 -03008445 I915_WRITE(PIPECONF(pipe), val);
8446 POSTING_READ(PIPECONF(pipe));
8447}
8448
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008449static void haswell_set_pipeconf(const struct intel_crtc_state *crtc_state)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008450{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008451 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8452 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8453 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008454 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008455
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008456 if (IS_HASWELL(dev_priv) && crtc_state->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008457 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8458
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008459 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008460 val |= PIPECONF_INTERLACED_ILK;
8461 else
8462 val |= PIPECONF_PROGRESSIVE;
8463
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008464 I915_WRITE(PIPECONF(cpu_transcoder), val);
8465 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008466}
8467
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008468static void haswell_set_pipemisc(const struct intel_crtc_state *crtc_state)
Jani Nikula391bf042016-03-18 17:05:40 +02008469{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
8471 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008472
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00008473 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
Jani Nikula391bf042016-03-18 17:05:40 +02008474 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008475
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008476 switch (crtc_state->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008477 case 18:
8478 val |= PIPEMISC_DITHER_6_BPC;
8479 break;
8480 case 24:
8481 val |= PIPEMISC_DITHER_8_BPC;
8482 break;
8483 case 30:
8484 val |= PIPEMISC_DITHER_10_BPC;
8485 break;
8486 case 36:
8487 val |= PIPEMISC_DITHER_12_BPC;
8488 break;
8489 default:
8490 /* Case prevented by pipe_config_set_bpp. */
8491 BUG();
8492 }
8493
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008494 if (crtc_state->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008495 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8496
Shashank Sharma8c79f842018-10-12 11:53:09 +05308497 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8498 crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05308499 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
Shashank Sharma8c79f842018-10-12 11:53:09 +05308500
8501 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05308502 val |= PIPEMISC_YUV420_ENABLE |
Shashank Sharmab22ca992017-07-24 19:19:32 +05308503 PIPEMISC_YUV420_MODE_FULL_BLEND;
Shashank Sharmab22ca992017-07-24 19:19:32 +05308504
Jani Nikula391bf042016-03-18 17:05:40 +02008505 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008506 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008507}
8508
Paulo Zanonid4b19312012-11-29 11:29:32 -02008509int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8510{
8511 /*
8512 * Account for spread spectrum to avoid
8513 * oversubscribing the link. Max center spread
8514 * is 2.5%; use 5% for safety's sake.
8515 */
8516 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008517 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008518}
8519
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008520static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008521{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008522 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008523}
8524
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008525static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8526 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008527 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008528{
8529 struct drm_crtc *crtc = &intel_crtc->base;
8530 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008531 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008532 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008533 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008534
Chris Wilsonc1858122010-12-03 21:35:48 +00008535 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008536 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008537 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008538 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008539 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008540 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008541 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008542 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008543 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008544
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008545 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008546
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008547 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8548 fp |= FP_CB_TUNE;
8549
8550 if (reduced_clock) {
8551 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8552
8553 if (reduced_clock->m < factor * reduced_clock->n)
8554 fp2 |= FP_CB_TUNE;
8555 } else {
8556 fp2 = fp;
8557 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008558
Chris Wilson5eddb702010-09-11 13:48:45 +01008559 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008560
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008561 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008562 dpll |= DPLLB_MODE_LVDS;
8563 else
8564 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008565
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008566 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008567 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008568
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008569 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8570 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008571 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008572
Ville Syrjälä37a56502016-06-22 21:57:04 +03008573 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008574 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008575
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03008576 /*
8577 * The high speed IO clock is only really required for
8578 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8579 * possible to share the DPLL between CRT and HDMI. Enabling
8580 * the clock needlessly does no real harm, except use up a
8581 * bit of power potentially.
8582 *
8583 * We'll limit this to IVB with 3 pipes, since it has only two
8584 * DPLLs and so DPLL sharing is the only way to get three pipes
8585 * driving PCH ports at the same time. On SNB we could do this,
8586 * and potentially avoid enabling the second DPLL, but it's not
8587 * clear if it''s a win or loss power wise. No point in doing
8588 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8589 */
8590 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8591 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8592 dpll |= DPLL_SDVO_HIGH_SPEED;
8593
Eric Anholta07d6782011-03-30 13:01:08 -07008594 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008595 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008596 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008597 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008598
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008599 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008600 case 5:
8601 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8602 break;
8603 case 7:
8604 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8605 break;
8606 case 10:
8607 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8608 break;
8609 case 14:
8610 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8611 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008612 }
8613
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008614 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8615 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008616 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008617 else
8618 dpll |= PLL_REF_INPUT_DREFCLK;
8619
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008620 dpll |= DPLL_VCO_ENABLE;
8621
8622 crtc_state->dpll_hw_state.dpll = dpll;
8623 crtc_state->dpll_hw_state.fp0 = fp;
8624 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008625}
8626
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008627static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8628 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008629{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008630 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008631 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008632 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008633 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008634
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008635 memset(&crtc_state->dpll_hw_state, 0,
8636 sizeof(crtc_state->dpll_hw_state));
8637
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008638 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8639 if (!crtc_state->has_pch_encoder)
8640 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008641
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008642 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008643 if (intel_panel_use_ssc(dev_priv)) {
8644 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8645 dev_priv->vbt.lvds_ssc_freq);
8646 refclk = dev_priv->vbt.lvds_ssc_freq;
8647 }
8648
8649 if (intel_is_dual_link_lvds(dev)) {
8650 if (refclk == 100000)
8651 limit = &intel_limits_ironlake_dual_lvds_100m;
8652 else
8653 limit = &intel_limits_ironlake_dual_lvds;
8654 } else {
8655 if (refclk == 100000)
8656 limit = &intel_limits_ironlake_single_lvds_100m;
8657 else
8658 limit = &intel_limits_ironlake_single_lvds;
8659 }
8660 } else {
8661 limit = &intel_limits_ironlake_dac;
8662 }
8663
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008664 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008665 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8666 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008667 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8668 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008669 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008670
Gustavo A. R. Silvacbaa3312017-05-15 16:56:05 -05008671 ironlake_compute_dpll(crtc, crtc_state, NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008672
Gustavo A. R. Silvaefd38b62017-05-15 17:00:28 -05008673 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
Chris Wilson43031782018-09-13 14:16:26 +01008674 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
8675 pipe_name(crtc->pipe));
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008676 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008677 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008678
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008679 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008680}
8681
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008682static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8683 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008684{
8685 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008686 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008687 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008688
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008689 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8690 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8691 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8692 & ~TU_SIZE_MASK;
8693 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8694 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8695 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8696}
8697
8698static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8699 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008700 struct intel_link_m_n *m_n,
8701 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008702{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008703 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008704 enum pipe pipe = crtc->pipe;
8705
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008706 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008707 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8708 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8709 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8710 & ~TU_SIZE_MASK;
8711 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8712 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8713 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Maarten Lankhorst4207c8b2018-10-15 11:40:23 +02008714
8715 if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008716 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8717 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8718 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8719 & ~TU_SIZE_MASK;
8720 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8721 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8722 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8723 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008724 } else {
8725 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8726 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8727 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8728 & ~TU_SIZE_MASK;
8729 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8730 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8731 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8732 }
8733}
8734
8735void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008736 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008737{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008738 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008739 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8740 else
8741 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008742 &pipe_config->dp_m_n,
8743 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008744}
8745
Daniel Vetter72419202013-04-04 13:28:53 +02008746static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008747 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008748{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008749 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008750 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008751}
8752
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008753static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008754 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008755{
8756 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008757 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07008758 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8759 uint32_t ps_ctrl = 0;
8760 int id = -1;
8761 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008762
Chandra Kondurua1b22782015-04-07 15:28:45 -07008763 /* find scaler attached to this pipe */
8764 for (i = 0; i < crtc->num_scalers; i++) {
8765 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8766 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8767 id = i;
8768 pipe_config->pch_pfit.enabled = true;
8769 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8770 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8771 break;
8772 }
8773 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008774
Chandra Kondurua1b22782015-04-07 15:28:45 -07008775 scaler_state->scaler_id = id;
8776 if (id >= 0) {
8777 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8778 } else {
8779 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008780 }
8781}
8782
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008783static void
8784skylake_get_initial_plane_config(struct intel_crtc *crtc,
8785 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008786{
8787 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008788 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008789 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
8790 enum plane_id plane_id = plane->id;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008791 enum pipe pipe;
James Ausmus4036c782017-11-13 10:11:28 -08008792 u32 val, base, offset, stride_mult, tiling, alpha;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008793 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008794 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008795 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008796 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008797
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008798 if (!plane->get_hw_state(plane, &pipe))
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02008799 return;
8800
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008801 WARN_ON(pipe != crtc->pipe);
8802
Damien Lespiaud9806c92015-01-21 14:07:19 +00008803 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008804 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008805 DRM_DEBUG_KMS("failed to alloc fb\n");
8806 return;
8807 }
8808
Damien Lespiau1b842c82015-01-21 13:50:54 +00008809 fb = &intel_fb->base;
8810
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008811 fb->dev = dev;
8812
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008813 val = I915_READ(PLANE_CTL(pipe, plane_id));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008814
James Ausmusb5972772018-01-30 11:49:16 -02008815 if (INTEL_GEN(dev_priv) >= 11)
8816 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
8817 else
8818 pixel_format = val & PLANE_CTL_FORMAT_MASK;
James Ausmus4036c782017-11-13 10:11:28 -08008819
8820 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008821 alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
James Ausmus4036c782017-11-13 10:11:28 -08008822 alpha &= PLANE_COLOR_ALPHA_MASK;
8823 } else {
8824 alpha = val & PLANE_CTL_ALPHA_MASK;
8825 }
8826
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008827 fourcc = skl_format_to_fourcc(pixel_format,
James Ausmus4036c782017-11-13 10:11:28 -08008828 val & PLANE_CTL_ORDER_RGBX, alpha);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008829 fb->format = drm_format_info(fourcc);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008830
Damien Lespiau40f46282015-02-27 11:15:21 +00008831 tiling = val & PLANE_CTL_TILED_MASK;
8832 switch (tiling) {
8833 case PLANE_CTL_TILED_LINEAR:
Ben Widawsky2f075562017-03-24 14:29:48 -07008834 fb->modifier = DRM_FORMAT_MOD_LINEAR;
Damien Lespiau40f46282015-02-27 11:15:21 +00008835 break;
8836 case PLANE_CTL_TILED_X:
8837 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008838 fb->modifier = I915_FORMAT_MOD_X_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008839 break;
8840 case PLANE_CTL_TILED_Y:
Imre Deak914a4fd2018-10-16 19:00:11 +03008841 plane_config->tiling = I915_TILING_Y;
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07008842 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008843 fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
8844 else
8845 fb->modifier = I915_FORMAT_MOD_Y_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008846 break;
8847 case PLANE_CTL_TILED_YF:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07008848 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008849 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
8850 else
8851 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008852 break;
8853 default:
8854 MISSING_CASE(tiling);
8855 goto error;
8856 }
8857
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008858 base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008859 plane_config->base = base;
8860
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008861 offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008862
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008863 val = I915_READ(PLANE_SIZE(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008864 fb->height = ((val >> 16) & 0xfff) + 1;
8865 fb->width = ((val >> 0) & 0x1fff) + 1;
8866
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008867 val = I915_READ(PLANE_STRIDE(pipe, plane_id));
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008868 stride_mult = intel_fb_stride_alignment(fb, 0);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008869 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8870
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008871 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008872
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008873 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008874
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008875 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8876 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008877 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008878 plane_config->size);
8879
Damien Lespiau2d140302015-02-05 17:22:18 +00008880 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008881 return;
8882
8883error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01008884 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008885}
8886
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008887static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008888 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008889{
8890 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008891 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008892 uint32_t tmp;
8893
8894 tmp = I915_READ(PF_CTL(crtc->pipe));
8895
8896 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008897 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008898 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8899 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008900
8901 /* We currently do not free assignements of panel fitters on
8902 * ivb/hsw (since we don't use the higher upscaling modes which
8903 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008904 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008905 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8906 PF_PIPE_SEL_IVB(crtc->pipe));
8907 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008908 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008909}
8910
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008911static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008912 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008913{
8914 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008915 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008916 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008917 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008918 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008919
Imre Deak17290502016-02-12 18:55:11 +02008920 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8921 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008922 return false;
8923
Shashank Sharmad9facae2018-10-12 11:53:07 +05308924 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
Daniel Vettere143a212013-07-04 12:01:15 +02008925 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008926 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008927
Imre Deak17290502016-02-12 18:55:11 +02008928 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008929 tmp = I915_READ(PIPECONF(crtc->pipe));
8930 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008931 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008932
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008933 switch (tmp & PIPECONF_BPC_MASK) {
8934 case PIPECONF_6BPC:
8935 pipe_config->pipe_bpp = 18;
8936 break;
8937 case PIPECONF_8BPC:
8938 pipe_config->pipe_bpp = 24;
8939 break;
8940 case PIPECONF_10BPC:
8941 pipe_config->pipe_bpp = 30;
8942 break;
8943 case PIPECONF_12BPC:
8944 pipe_config->pipe_bpp = 36;
8945 break;
8946 default:
8947 break;
8948 }
8949
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008950 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8951 pipe_config->limited_color_range = true;
8952
Daniel Vetterab9412b2013-05-03 11:49:46 +02008953 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008954 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008955 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008956
Daniel Vetter88adfff2013-03-28 10:42:01 +01008957 pipe_config->has_pch_encoder = true;
8958
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008959 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8960 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8961 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008962
8963 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008964
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008965 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03008966 /*
8967 * The pipe->pch transcoder and pch transcoder->pll
8968 * mapping is fixed.
8969 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008970 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008971 } else {
8972 tmp = I915_READ(PCH_DPLL_SEL);
8973 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008974 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008975 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008976 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008977 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008978
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008979 pipe_config->shared_dpll =
8980 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8981 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008982
Lucas De Marchiee1398b2018-03-20 15:06:33 -07008983 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
8984 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008985
8986 tmp = pipe_config->dpll_hw_state.dpll;
8987 pipe_config->pixel_multiplier =
8988 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8989 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008990
8991 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008992 } else {
8993 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008994 }
8995
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008996 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008997 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008998
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008999 ironlake_get_pfit_config(crtc, pipe_config);
9000
Imre Deak17290502016-02-12 18:55:11 +02009001 ret = true;
9002
9003out:
9004 intel_display_power_put(dev_priv, power_domain);
9005
9006 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009007}
9008
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009009static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9010{
Chris Wilson91c8a322016-07-05 10:40:23 +01009011 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009012 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009013
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009014 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009015 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009016 pipe_name(crtc->pipe));
9017
Imre Deak75e39682018-08-06 12:58:39 +03009018 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL2),
Imre Deak9c3a16c2017-08-14 18:15:30 +03009019 "Display power well on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009020 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009021 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9022 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03009023 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009024 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009025 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009026 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -05009027 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009028 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009029 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009030 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009031 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009032 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009033 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009034
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009035 /*
9036 * In theory we can still leave IRQs enabled, as long as only the HPD
9037 * interrupts remain enabled. We used to check for that, but since it's
9038 * gen-specific and since we only disable LCPLL after we fully disable
9039 * the interrupts, the check below should be enough.
9040 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009041 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009042}
9043
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009044static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9045{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009046 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009047 return I915_READ(D_COMP_HSW);
9048 else
9049 return I915_READ(D_COMP_BDW);
9050}
9051
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009052static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9053{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009054 if (IS_HASWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009055 mutex_lock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009056 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9057 val))
Chris Wilson79cf2192016-08-24 11:16:07 +01009058 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009059 mutex_unlock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009060 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009061 I915_WRITE(D_COMP_BDW, val);
9062 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009063 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009064}
9065
9066/*
9067 * This function implements pieces of two sequences from BSpec:
9068 * - Sequence for display software to disable LCPLL
9069 * - Sequence for display software to allow package C8+
9070 * The steps implemented here are just the steps that actually touch the LCPLL
9071 * register. Callers should take care of disabling all the display engine
9072 * functions, doing the mode unset, fixing interrupts, etc.
9073 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009074static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9075 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009076{
9077 uint32_t val;
9078
9079 assert_can_disable_lcpll(dev_priv);
9080
9081 val = I915_READ(LCPLL_CTL);
9082
9083 if (switch_to_fclk) {
9084 val |= LCPLL_CD_SOURCE_FCLK;
9085 I915_WRITE(LCPLL_CTL, val);
9086
Imre Deakf53dd632016-06-28 13:37:32 +03009087 if (wait_for_us(I915_READ(LCPLL_CTL) &
9088 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009089 DRM_ERROR("Switching to FCLK failed\n");
9090
9091 val = I915_READ(LCPLL_CTL);
9092 }
9093
9094 val |= LCPLL_PLL_DISABLE;
9095 I915_WRITE(LCPLL_CTL, val);
9096 POSTING_READ(LCPLL_CTL);
9097
Chris Wilson24d84412016-06-30 15:33:07 +01009098 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009099 DRM_ERROR("LCPLL still locked\n");
9100
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009101 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009102 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009103 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009104 ndelay(100);
9105
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009106 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9107 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009108 DRM_ERROR("D_COMP RCOMP still in progress\n");
9109
9110 if (allow_power_down) {
9111 val = I915_READ(LCPLL_CTL);
9112 val |= LCPLL_POWER_DOWN_ALLOW;
9113 I915_WRITE(LCPLL_CTL, val);
9114 POSTING_READ(LCPLL_CTL);
9115 }
9116}
9117
9118/*
9119 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9120 * source.
9121 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009122static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009123{
9124 uint32_t val;
9125
9126 val = I915_READ(LCPLL_CTL);
9127
9128 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9129 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9130 return;
9131
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009132 /*
9133 * Make sure we're not on PC8 state before disabling PC8, otherwise
9134 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009135 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009136 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009137
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009138 if (val & LCPLL_POWER_DOWN_ALLOW) {
9139 val &= ~LCPLL_POWER_DOWN_ALLOW;
9140 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009141 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009142 }
9143
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009144 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009145 val |= D_COMP_COMP_FORCE;
9146 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009147 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009148
9149 val = I915_READ(LCPLL_CTL);
9150 val &= ~LCPLL_PLL_DISABLE;
9151 I915_WRITE(LCPLL_CTL, val);
9152
Chris Wilson93220c02016-06-30 15:33:08 +01009153 if (intel_wait_for_register(dev_priv,
9154 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9155 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009156 DRM_ERROR("LCPLL not locked yet\n");
9157
9158 if (val & LCPLL_CD_SOURCE_FCLK) {
9159 val = I915_READ(LCPLL_CTL);
9160 val &= ~LCPLL_CD_SOURCE_FCLK;
9161 I915_WRITE(LCPLL_CTL, val);
9162
Imre Deakf53dd632016-06-28 13:37:32 +03009163 if (wait_for_us((I915_READ(LCPLL_CTL) &
9164 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009165 DRM_ERROR("Switching back to LCPLL failed\n");
9166 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009167
Mika Kuoppala59bad942015-01-16 11:34:40 +02009168 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009169
Ville Syrjälä4c75b942016-10-31 22:37:12 +02009170 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009171 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009172}
9173
Paulo Zanoni765dab672014-03-07 20:08:18 -03009174/*
9175 * Package states C8 and deeper are really deep PC states that can only be
9176 * reached when all the devices on the system allow it, so even if the graphics
9177 * device allows PC8+, it doesn't mean the system will actually get to these
9178 * states. Our driver only allows PC8+ when going into runtime PM.
9179 *
9180 * The requirements for PC8+ are that all the outputs are disabled, the power
9181 * well is disabled and most interrupts are disabled, and these are also
9182 * requirements for runtime PM. When these conditions are met, we manually do
9183 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9184 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9185 * hang the machine.
9186 *
9187 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9188 * the state of some registers, so when we come back from PC8+ we need to
9189 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9190 * need to take care of the registers kept by RC6. Notice that this happens even
9191 * if we don't put the device in PCI D3 state (which is what currently happens
9192 * because of the runtime PM support).
9193 *
9194 * For more, read "Display Sequences for Package C8" on the hardware
9195 * documentation.
9196 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009197void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009198{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009199 uint32_t val;
9200
Paulo Zanonic67a4702013-08-19 13:18:09 -03009201 DRM_DEBUG_KMS("Enabling package C8+\n");
9202
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009203 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009204 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9205 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9206 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9207 }
9208
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009209 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009210 hsw_disable_lcpll(dev_priv, true, true);
9211}
9212
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009213void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009214{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009215 uint32_t val;
9216
Paulo Zanonic67a4702013-08-19 13:18:09 -03009217 DRM_DEBUG_KMS("Disabling package C8+\n");
9218
9219 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009220 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009221
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009222 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009223 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9224 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9225 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9226 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009227}
9228
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009229static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9230 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009231{
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03009232 struct intel_atomic_state *state =
9233 to_intel_atomic_state(crtc_state->base.state);
9234
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009235 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009236 struct intel_encoder *encoder =
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03009237 intel_get_crtc_new_encoder(state, crtc_state);
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009238
9239 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
Chris Wilson43031782018-09-13 14:16:26 +01009240 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
9241 pipe_name(crtc->pipe));
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009242 return -EINVAL;
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009243 }
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009244 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009245
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009246 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009247}
9248
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009249static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
9250 enum port port,
9251 struct intel_crtc_state *pipe_config)
9252{
9253 enum intel_dpll_id id;
9254 u32 temp;
9255
9256 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
Paulo Zanonidfbd4502017-08-25 16:40:04 -03009257 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009258
9259 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
9260 return;
9261
9262 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9263}
9264
Paulo Zanoni970888e2018-05-21 17:25:44 -07009265static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
9266 enum port port,
9267 struct intel_crtc_state *pipe_config)
9268{
9269 enum intel_dpll_id id;
9270 u32 temp;
9271
9272 /* TODO: TBT pll not implemented. */
Vandita Kulkarni8ea59e62018-10-03 12:51:59 +05309273 if (intel_port_is_combophy(dev_priv, port)) {
Paulo Zanoni970888e2018-05-21 17:25:44 -07009274 temp = I915_READ(DPCLKA_CFGCR0_ICL) &
9275 DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
9276 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
9277
Vandita Kulkarnia54270d2018-10-03 12:52:00 +05309278 if (WARN_ON(!intel_dpll_is_combophy(id)))
Paulo Zanoni970888e2018-05-21 17:25:44 -07009279 return;
Vandita Kulkarni8ea59e62018-10-03 12:51:59 +05309280 } else if (intel_port_is_tc(dev_priv, port)) {
Vandita Kulkarnicb6caf72018-10-03 12:51:58 +05309281 id = icl_port_to_mg_pll_id(port);
Vandita Kulkarni8ea59e62018-10-03 12:51:59 +05309282 } else {
9283 WARN(1, "Invalid port %x\n", port);
Paulo Zanoni970888e2018-05-21 17:25:44 -07009284 return;
9285 }
9286
9287 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9288}
9289
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309290static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9291 enum port port,
9292 struct intel_crtc_state *pipe_config)
9293{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009294 enum intel_dpll_id id;
9295
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309296 switch (port) {
9297 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +02009298 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309299 break;
9300 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +02009301 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309302 break;
9303 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +02009304 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309305 break;
9306 default:
9307 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009308 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309309 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009310
9311 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309312}
9313
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009314static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9315 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009316 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009317{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009318 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009319 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009320
9321 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009322 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009323
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009324 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009325 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009326
9327 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009328}
9329
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009330static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9331 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009332 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009333{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009334 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009335 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009336
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009337 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009338 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009339 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009340 break;
9341 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009342 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009343 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009344 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009345 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009346 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009347 case PORT_CLK_SEL_LCPLL_810:
9348 id = DPLL_ID_LCPLL_810;
9349 break;
9350 case PORT_CLK_SEL_LCPLL_1350:
9351 id = DPLL_ID_LCPLL_1350;
9352 break;
9353 case PORT_CLK_SEL_LCPLL_2700:
9354 id = DPLL_ID_LCPLL_2700;
9355 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009356 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -07009357 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009358 /* fall through */
9359 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009360 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009361 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009362
9363 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009364}
9365
Jani Nikulacf304292016-03-18 17:05:41 +02009366static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9367 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009368 u64 *power_domain_mask)
Jani Nikulacf304292016-03-18 17:05:41 +02009369{
9370 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009371 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02009372 enum intel_display_power_domain power_domain;
9373 u32 tmp;
9374
Imre Deakd9a7bc62016-05-12 16:18:50 +03009375 /*
9376 * The pipe->transcoder mapping is fixed with the exception of the eDP
9377 * transcoder handled below.
9378 */
Jani Nikulacf304292016-03-18 17:05:41 +02009379 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9380
9381 /*
9382 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9383 * consistency and less surprising code; it's in always on power).
9384 */
9385 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9386 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9387 enum pipe trans_edp_pipe;
9388 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9389 default:
9390 WARN(1, "unknown pipe linked to edp transcoder\n");
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -05009391 /* fall through */
Jani Nikulacf304292016-03-18 17:05:41 +02009392 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9393 case TRANS_DDI_EDP_INPUT_A_ON:
9394 trans_edp_pipe = PIPE_A;
9395 break;
9396 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9397 trans_edp_pipe = PIPE_B;
9398 break;
9399 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9400 trans_edp_pipe = PIPE_C;
9401 break;
9402 }
9403
9404 if (trans_edp_pipe == crtc->pipe)
9405 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9406 }
9407
9408 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9409 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9410 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009411 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikulacf304292016-03-18 17:05:41 +02009412
9413 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9414
9415 return tmp & PIPECONF_ENABLE;
9416}
9417
Jani Nikula4d1de972016-03-18 17:05:42 +02009418static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9419 struct intel_crtc_state *pipe_config,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009420 u64 *power_domain_mask)
Jani Nikula4d1de972016-03-18 17:05:42 +02009421{
9422 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009423 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +02009424 enum intel_display_power_domain power_domain;
9425 enum port port;
9426 enum transcoder cpu_transcoder;
9427 u32 tmp;
9428
Jani Nikula4d1de972016-03-18 17:05:42 +02009429 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9430 if (port == PORT_A)
9431 cpu_transcoder = TRANSCODER_DSI_A;
9432 else
9433 cpu_transcoder = TRANSCODER_DSI_C;
9434
9435 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9436 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9437 continue;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009438 *power_domain_mask |= BIT_ULL(power_domain);
Jani Nikula4d1de972016-03-18 17:05:42 +02009439
Imre Deakdb18b6a2016-03-24 12:41:40 +02009440 /*
9441 * The PLL needs to be enabled with a valid divider
9442 * configuration, otherwise accessing DSI registers will hang
9443 * the machine. See BSpec North Display Engine
9444 * registers/MIPI[BXT]. We can break out here early, since we
9445 * need the same DSI PLL to be enabled for both DSI ports.
9446 */
Jani Nikulae5186342018-07-05 16:25:08 +03009447 if (!bxt_dsi_pll_is_enabled(dev_priv))
Imre Deakdb18b6a2016-03-24 12:41:40 +02009448 break;
9449
Jani Nikula4d1de972016-03-18 17:05:42 +02009450 /* XXX: this works for video mode only */
9451 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9452 if (!(tmp & DPI_ENABLE))
9453 continue;
9454
9455 tmp = I915_READ(MIPI_CTRL(port));
9456 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9457 continue;
9458
9459 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +02009460 break;
9461 }
9462
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009463 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +02009464}
9465
Daniel Vetter26804af2014-06-25 22:01:55 +03009466static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009467 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009468{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009469 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009470 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009471 enum port port;
9472 uint32_t tmp;
9473
9474 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9475
9476 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9477
Paulo Zanoni970888e2018-05-21 17:25:44 -07009478 if (IS_ICELAKE(dev_priv))
9479 icelake_get_ddi_pll(dev_priv, port, pipe_config);
9480 else if (IS_CANNONLAKE(dev_priv))
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009481 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9482 else if (IS_GEN9_BC(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009483 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009484 else if (IS_GEN9_LP(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309485 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009486 else
9487 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009488
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009489 pll = pipe_config->shared_dpll;
9490 if (pll) {
Lucas De Marchiee1398b2018-03-20 15:06:33 -07009491 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
9492 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009493 }
9494
Daniel Vetter26804af2014-06-25 22:01:55 +03009495 /*
9496 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9497 * DDI E. So just check whether this pipe is wired to DDI E and whether
9498 * the PCH transcoder is on.
9499 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009500 if (INTEL_GEN(dev_priv) < 9 &&
Damien Lespiauca370452013-12-03 13:56:24 +00009501 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009502 pipe_config->has_pch_encoder = true;
9503
9504 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9505 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9506 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9507
9508 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9509 }
9510}
9511
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009512static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009513 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009514{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009515 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02009516 enum intel_display_power_domain power_domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009517 u64 power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009518 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009519
Imre Deake79dfb52017-07-20 01:50:57 +03009520 intel_crtc_init_scalers(crtc, pipe_config);
Imre Deak5fb9dad2017-07-20 14:28:20 +03009521
Imre Deak17290502016-02-12 18:55:11 +02009522 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9523 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009524 return false;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009525 power_domain_mask = BIT_ULL(power_domain);
Imre Deak17290502016-02-12 18:55:11 +02009526
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009527 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009528
Jani Nikulacf304292016-03-18 17:05:41 +02009529 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009530
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02009531 if (IS_GEN9_LP(dev_priv) &&
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009532 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9533 WARN_ON(active);
9534 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +02009535 }
9536
Jani Nikulacf304292016-03-18 17:05:41 +02009537 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009538 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009539
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009540 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +02009541 haswell_get_ddi_port_state(crtc, pipe_config);
9542 intel_get_pipe_timings(crtc, pipe_config);
9543 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009544
Jani Nikulabc58be62016-03-18 17:05:39 +02009545 intel_get_pipe_src_size(crtc, pipe_config);
Shashank Sharma33b7f3e2018-10-12 11:53:08 +05309546 intel_get_crtc_ycbcr_config(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009547
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009548 pipe_config->gamma_mode =
9549 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9550
Imre Deak17290502016-02-12 18:55:11 +02009551 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9552 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009553 power_domain_mask |= BIT_ULL(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009554 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009555 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009556 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009557 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009558 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009559
Maarten Lankhorst24f28452017-11-22 19:39:01 +01009560 if (hsw_crtc_supports_ips(crtc)) {
9561 if (IS_HASWELL(dev_priv))
9562 pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
9563 else {
9564 /*
9565 * We cannot readout IPS state on broadwell, set to
9566 * true so we can set it to a defined state on first
9567 * commit.
9568 */
9569 pipe_config->ips_enabled = true;
9570 }
9571 }
9572
Jani Nikula4d1de972016-03-18 17:05:42 +02009573 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9574 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009575 pipe_config->pixel_multiplier =
9576 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9577 } else {
9578 pipe_config->pixel_multiplier = 1;
9579 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009580
Imre Deak17290502016-02-12 18:55:11 +02009581out:
9582 for_each_power_domain(power_domain, power_domain_mask)
9583 intel_display_power_put(dev_priv, power_domain);
9584
Jani Nikulacf304292016-03-18 17:05:41 +02009585 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009586}
9587
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009588static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009589{
9590 struct drm_i915_private *dev_priv =
9591 to_i915(plane_state->base.plane->dev);
9592 const struct drm_framebuffer *fb = plane_state->base.fb;
9593 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9594 u32 base;
9595
9596 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9597 base = obj->phys_handle->busaddr;
9598 else
9599 base = intel_plane_ggtt_offset(plane_state);
9600
Ville Syrjäläc11ada02018-09-07 18:24:04 +03009601 base += plane_state->color_plane[0].offset;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009602
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009603 /* ILK+ do this automagically */
9604 if (HAS_GMCH_DISPLAY(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009605 plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009606 base += (plane_state->base.crtc_h *
9607 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9608
9609 return base;
9610}
9611
Ville Syrjäläed270222017-03-27 21:55:36 +03009612static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9613{
9614 int x = plane_state->base.crtc_x;
9615 int y = plane_state->base.crtc_y;
9616 u32 pos = 0;
9617
9618 if (x < 0) {
9619 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9620 x = -x;
9621 }
9622 pos |= x << CURSOR_X_SHIFT;
9623
9624 if (y < 0) {
9625 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9626 y = -y;
9627 }
9628 pos |= y << CURSOR_Y_SHIFT;
9629
9630 return pos;
9631}
9632
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009633static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9634{
9635 const struct drm_mode_config *config =
9636 &plane_state->base.plane->dev->mode_config;
9637 int width = plane_state->base.crtc_w;
9638 int height = plane_state->base.crtc_h;
9639
9640 return width > 0 && width <= config->cursor_width &&
9641 height > 0 && height <= config->cursor_height;
9642}
9643
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009644static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009645{
9646 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009647 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009648 int src_x, src_y;
9649 u32 offset;
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03009650 int ret;
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009651
9652 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
9653 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
9654
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03009655 ret = intel_plane_check_stride(plane_state);
9656 if (ret)
9657 return ret;
9658
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009659 src_x = plane_state->base.src_x >> 16;
9660 src_y = plane_state->base.src_y >> 16;
9661
9662 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9663 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
9664 plane_state, 0);
9665
9666 if (src_x != 0 || src_y != 0) {
9667 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9668 return -EINVAL;
9669 }
9670
9671 plane_state->color_plane[0].offset = offset;
9672
9673 return 0;
9674}
9675
9676static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9677 struct intel_plane_state *plane_state)
9678{
9679 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009680 int ret;
9681
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009682 if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9683 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9684 return -EINVAL;
9685 }
9686
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009687 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
9688 &crtc_state->base,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009689 DRM_PLANE_HELPER_NO_SCALING,
9690 DRM_PLANE_HELPER_NO_SCALING,
9691 true, true);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009692 if (ret)
9693 return ret;
9694
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009695 if (!plane_state->base.visible)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009696 return 0;
9697
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009698 ret = intel_plane_check_src_coordinates(plane_state);
9699 if (ret)
9700 return ret;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009701
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009702 ret = intel_cursor_check_surface(plane_state);
9703 if (ret)
9704 return ret;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009705
Ville Syrjälä659056f2017-03-27 21:55:39 +03009706 return 0;
9707}
9708
Ville Syrjäläddd57132018-09-07 18:24:02 +03009709static unsigned int
9710i845_cursor_max_stride(struct intel_plane *plane,
9711 u32 pixel_format, u64 modifier,
9712 unsigned int rotation)
9713{
9714 return 2048;
9715}
9716
Ville Syrjälä292889e2017-03-17 23:18:01 +02009717static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9718 const struct intel_plane_state *plane_state)
9719{
Ville Syrjälä292889e2017-03-17 23:18:01 +02009720 return CURSOR_ENABLE |
9721 CURSOR_GAMMA_ENABLE |
9722 CURSOR_FORMAT_ARGB |
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009723 CURSOR_STRIDE(plane_state->color_plane[0].stride);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009724}
9725
Ville Syrjälä659056f2017-03-27 21:55:39 +03009726static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9727{
Ville Syrjälä659056f2017-03-27 21:55:39 +03009728 int width = plane_state->base.crtc_w;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009729
9730 /*
9731 * 845g/865g are only limited by the width of their cursors,
9732 * the height is arbitrary up to the precision of the register.
9733 */
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009734 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009735}
9736
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009737static int i845_check_cursor(struct intel_crtc_state *crtc_state,
Ville Syrjälä659056f2017-03-27 21:55:39 +03009738 struct intel_plane_state *plane_state)
9739{
9740 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009741 int ret;
9742
9743 ret = intel_check_cursor(crtc_state, plane_state);
9744 if (ret)
9745 return ret;
9746
9747 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009748 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009749 return 0;
9750
9751 /* Check for which cursor types we support */
9752 if (!i845_cursor_size_ok(plane_state)) {
9753 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9754 plane_state->base.crtc_w,
9755 plane_state->base.crtc_h);
9756 return -EINVAL;
9757 }
9758
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009759 WARN_ON(plane_state->base.visible &&
9760 plane_state->color_plane[0].stride != fb->pitches[0]);
9761
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009762 switch (fb->pitches[0]) {
Chris Wilson560b85b2010-08-07 11:01:38 +01009763 case 256:
9764 case 512:
9765 case 1024:
9766 case 2048:
Ville Syrjälädc41c152014-08-13 11:57:05 +03009767 break;
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009768 default:
9769 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9770 fb->pitches[0]);
9771 return -EINVAL;
Chris Wilson560b85b2010-08-07 11:01:38 +01009772 }
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009773
Ville Syrjälä659056f2017-03-27 21:55:39 +03009774 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9775
9776 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009777}
9778
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009779static void i845_update_cursor(struct intel_plane *plane,
9780 const struct intel_crtc_state *crtc_state,
Chris Wilson560b85b2010-08-07 11:01:38 +01009781 const struct intel_plane_state *plane_state)
9782{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009783 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009784 u32 cntl = 0, base = 0, pos = 0, size = 0;
9785 unsigned long irqflags;
Chris Wilson560b85b2010-08-07 11:01:38 +01009786
Ville Syrjälä936e71e2016-07-26 19:06:59 +03009787 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009788 unsigned int width = plane_state->base.crtc_w;
9789 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009790
Ville Syrjäläa0864d52017-03-23 21:27:09 +02009791 cntl = plane_state->ctl;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009792 size = (height << 12) | width;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009793
9794 base = intel_cursor_base(plane_state);
9795 pos = intel_cursor_position(plane_state);
Chris Wilson4b0e3332014-05-30 16:35:26 +03009796 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009797
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009798 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9799
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009800 /* On these chipsets we can only modify the base/size/stride
9801 * whilst the cursor is disabled.
9802 */
9803 if (plane->cursor.base != base ||
9804 plane->cursor.size != size ||
9805 plane->cursor.cntl != cntl) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009806 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009807 I915_WRITE_FW(CURBASE(PIPE_A), base);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009808 I915_WRITE_FW(CURSIZE, size);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009809 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009810 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
Ville Syrjälä75343a42017-03-27 21:55:38 +03009811
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009812 plane->cursor.base = base;
9813 plane->cursor.size = size;
9814 plane->cursor.cntl = cntl;
9815 } else {
9816 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009817 }
9818
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009819 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9820}
9821
9822static void i845_disable_cursor(struct intel_plane *plane,
9823 struct intel_crtc *crtc)
9824{
9825 i845_update_cursor(plane, NULL, NULL);
Chris Wilson560b85b2010-08-07 11:01:38 +01009826}
9827
Ville Syrjäläeade6c82018-01-30 22:38:03 +02009828static bool i845_cursor_get_hw_state(struct intel_plane *plane,
9829 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009830{
9831 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9832 enum intel_display_power_domain power_domain;
9833 bool ret;
9834
9835 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
9836 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9837 return false;
9838
9839 ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
9840
Ville Syrjäläeade6c82018-01-30 22:38:03 +02009841 *pipe = PIPE_A;
9842
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009843 intel_display_power_put(dev_priv, power_domain);
9844
9845 return ret;
9846}
9847
Ville Syrjäläddd57132018-09-07 18:24:02 +03009848static unsigned int
9849i9xx_cursor_max_stride(struct intel_plane *plane,
9850 u32 pixel_format, u64 modifier,
9851 unsigned int rotation)
9852{
9853 return plane->base.dev->mode_config.cursor_width * 4;
9854}
9855
Ville Syrjälä292889e2017-03-17 23:18:01 +02009856static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9857 const struct intel_plane_state *plane_state)
9858{
9859 struct drm_i915_private *dev_priv =
9860 to_i915(plane_state->base.plane->dev);
9861 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
José Roberto de Souzac894d632018-05-18 13:15:47 -07009862 u32 cntl = 0;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009863
Ville Syrjäläe876b782018-01-30 22:38:05 +02009864 if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
9865 cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
9866
José Roberto de Souzac894d632018-05-18 13:15:47 -07009867 if (INTEL_GEN(dev_priv) <= 10) {
9868 cntl |= MCURSOR_GAMMA_ENABLE;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009869
José Roberto de Souzac894d632018-05-18 13:15:47 -07009870 if (HAS_DDI(dev_priv))
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009871 cntl |= MCURSOR_PIPE_CSC_ENABLE;
José Roberto de Souzac894d632018-05-18 13:15:47 -07009872 }
Ville Syrjälä292889e2017-03-17 23:18:01 +02009873
Ville Syrjälä32ea06b2018-01-30 22:38:01 +02009874 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
9875 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009876
9877 switch (plane_state->base.crtc_w) {
9878 case 64:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009879 cntl |= MCURSOR_MODE_64_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009880 break;
9881 case 128:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009882 cntl |= MCURSOR_MODE_128_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009883 break;
9884 case 256:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009885 cntl |= MCURSOR_MODE_256_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009886 break;
9887 default:
9888 MISSING_CASE(plane_state->base.crtc_w);
9889 return 0;
9890 }
9891
Robert Fossc2c446a2017-05-19 16:50:17 -04009892 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009893 cntl |= MCURSOR_ROTATE_180;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009894
9895 return cntl;
9896}
9897
Ville Syrjälä659056f2017-03-27 21:55:39 +03009898static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009899{
Ville Syrjälä024faac2017-03-27 21:55:42 +03009900 struct drm_i915_private *dev_priv =
9901 to_i915(plane_state->base.plane->dev);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009902 int width = plane_state->base.crtc_w;
9903 int height = plane_state->base.crtc_h;
Chris Wilson560b85b2010-08-07 11:01:38 +01009904
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009905 if (!intel_cursor_size_ok(plane_state))
Ville Syrjälädc41c152014-08-13 11:57:05 +03009906 return false;
9907
Ville Syrjälä024faac2017-03-27 21:55:42 +03009908 /* Cursor width is limited to a few power-of-two sizes */
9909 switch (width) {
Ville Syrjälä659056f2017-03-27 21:55:39 +03009910 case 256:
9911 case 128:
Ville Syrjälä659056f2017-03-27 21:55:39 +03009912 case 64:
9913 break;
9914 default:
9915 return false;
9916 }
9917
Ville Syrjälädc41c152014-08-13 11:57:05 +03009918 /*
Ville Syrjälä024faac2017-03-27 21:55:42 +03009919 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9920 * height from 8 lines up to the cursor width, when the
9921 * cursor is not rotated. Everything else requires square
9922 * cursors.
Ville Syrjälädc41c152014-08-13 11:57:05 +03009923 */
Ville Syrjälä024faac2017-03-27 21:55:42 +03009924 if (HAS_CUR_FBC(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009925 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009926 if (height < 8 || height > width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009927 return false;
9928 } else {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009929 if (height != width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009930 return false;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009931 }
9932
9933 return true;
9934}
9935
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009936static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
Ville Syrjälä659056f2017-03-27 21:55:39 +03009937 struct intel_plane_state *plane_state)
9938{
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009939 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009940 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9941 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009942 enum pipe pipe = plane->pipe;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009943 int ret;
9944
9945 ret = intel_check_cursor(crtc_state, plane_state);
9946 if (ret)
9947 return ret;
9948
9949 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009950 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009951 return 0;
9952
9953 /* Check for which cursor types we support */
9954 if (!i9xx_cursor_size_ok(plane_state)) {
9955 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9956 plane_state->base.crtc_w,
9957 plane_state->base.crtc_h);
9958 return -EINVAL;
9959 }
9960
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009961 WARN_ON(plane_state->base.visible &&
9962 plane_state->color_plane[0].stride != fb->pitches[0]);
9963
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009964 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9965 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9966 fb->pitches[0], plane_state->base.crtc_w);
9967 return -EINVAL;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009968 }
9969
9970 /*
9971 * There's something wrong with the cursor on CHV pipe C.
9972 * If it straddles the left edge of the screen then
9973 * moving it away from the edge or disabling it often
9974 * results in a pipe underrun, and often that can lead to
9975 * dead pipe (constant underrun reported, and it scans
9976 * out just a solid color). To recover from that, the
9977 * display power well must be turned off and on again.
9978 * Refuse the put the cursor into that compromised position.
9979 */
9980 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9981 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9982 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9983 return -EINVAL;
9984 }
9985
9986 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
9987
9988 return 0;
9989}
9990
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009991static void i9xx_update_cursor(struct intel_plane *plane,
9992 const struct intel_crtc_state *crtc_state,
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309993 const struct intel_plane_state *plane_state)
9994{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009995 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9996 enum pipe pipe = plane->pipe;
Ville Syrjälä024faac2017-03-27 21:55:42 +03009997 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009998 unsigned long irqflags;
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309999
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010000 if (plane_state && plane_state->base.visible) {
Ville Syrjäläa0864d52017-03-23 21:27:09 +020010001 cntl = plane_state->ctl;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010002
Ville Syrjälä024faac2017-03-27 21:55:42 +030010003 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
10004 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
10005
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010006 base = intel_cursor_base(plane_state);
10007 pos = intel_cursor_position(plane_state);
10008 }
10009
10010 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
10011
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010012 /*
10013 * On some platforms writing CURCNTR first will also
10014 * cause CURPOS to be armed by the CURBASE write.
10015 * Without the CURCNTR write the CURPOS write would
Ville Syrjälä8753d2b2017-07-14 18:52:27 +030010016 * arm itself. Thus we always start the full update
10017 * with a CURCNTR write.
10018 *
10019 * On other platforms CURPOS always requires the
10020 * CURBASE write to arm the update. Additonally
10021 * a write to any of the cursor register will cancel
10022 * an already armed cursor update. Thus leaving out
10023 * the CURBASE write after CURPOS could lead to a
10024 * cursor that doesn't appear to move, or even change
10025 * shape. Thus we always write CURBASE.
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010026 *
10027 * CURCNTR and CUR_FBC_CTL are always
10028 * armed by the CURBASE write only.
10029 */
10030 if (plane->cursor.base != base ||
Ville Syrjälä024faac2017-03-27 21:55:42 +030010031 plane->cursor.size != fbc_ctl ||
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010032 plane->cursor.cntl != cntl) {
10033 I915_WRITE_FW(CURCNTR(pipe), cntl);
10034 if (HAS_CUR_FBC(dev_priv))
10035 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
10036 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä75343a42017-03-27 21:55:38 +030010037 I915_WRITE_FW(CURBASE(pipe), base);
10038
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010039 plane->cursor.base = base;
10040 plane->cursor.size = fbc_ctl;
10041 plane->cursor.cntl = cntl;
10042 } else {
10043 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä8753d2b2017-07-14 18:52:27 +030010044 I915_WRITE_FW(CURBASE(pipe), base);
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010045 }
10046
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010047 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010048}
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010049
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010050static void i9xx_disable_cursor(struct intel_plane *plane,
10051 struct intel_crtc *crtc)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010052{
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010053 i9xx_update_cursor(plane, NULL, NULL);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010054}
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010055
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010056static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
10057 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010058{
10059 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10060 enum intel_display_power_domain power_domain;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010061 bool ret;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010062 u32 val;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010063
10064 /*
10065 * Not 100% correct for planes that can move between pipes,
10066 * but that's only the case for gen2-3 which don't have any
10067 * display power wells.
10068 */
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010069 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010070 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10071 return false;
10072
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010073 val = I915_READ(CURCNTR(plane->pipe));
10074
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +020010075 ret = val & MCURSOR_MODE;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010076
10077 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
10078 *pipe = plane->pipe;
10079 else
10080 *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
10081 MCURSOR_PIPE_SELECT_SHIFT;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010082
10083 intel_display_power_put(dev_priv, power_domain);
10084
10085 return ret;
10086}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010087
Jesse Barnes79e53942008-11-07 14:24:08 -080010088/* VESA 640x480x72Hz mode to set on the pipe */
Ville Syrjäläbacdcd52017-05-18 22:38:37 +030010089static const struct drm_display_mode load_detect_mode = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010090 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10091 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10092};
10093
Daniel Vettera8bb6812014-02-10 18:00:39 +010010094struct drm_framebuffer *
Chris Wilson24dbf512017-02-15 10:59:18 +000010095intel_framebuffer_create(struct drm_i915_gem_object *obj,
10096 struct drm_mode_fb_cmd2 *mode_cmd)
Chris Wilsond2dff872011-04-19 08:36:26 +010010097{
10098 struct intel_framebuffer *intel_fb;
10099 int ret;
10100
10101 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010102 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010103 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010104
Chris Wilson24dbf512017-02-15 10:59:18 +000010105 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010106 if (ret)
10107 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010108
10109 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010110
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010111err:
10112 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010113 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010114}
10115
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010116static int intel_modeset_disable_planes(struct drm_atomic_state *state,
10117 struct drm_crtc *crtc)
Chris Wilsond2dff872011-04-19 08:36:26 +010010118{
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010119 struct drm_plane *plane;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010120 struct drm_plane_state *plane_state;
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010121 int ret, i;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010122
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010123 ret = drm_atomic_add_affected_planes(state, crtc);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010124 if (ret)
10125 return ret;
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010126
10127 for_each_new_plane_in_state(state, plane, plane_state, i) {
10128 if (plane_state->crtc != crtc)
10129 continue;
10130
10131 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
10132 if (ret)
10133 return ret;
10134
10135 drm_atomic_set_fb_for_plane(plane_state, NULL);
10136 }
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010137
10138 return 0;
10139}
10140
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010141int intel_get_load_detect_pipe(struct drm_connector *connector,
Ville Syrjäläbacdcd52017-05-18 22:38:37 +030010142 const struct drm_display_mode *mode,
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010143 struct intel_load_detect_pipe *old,
10144 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010145{
10146 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010147 struct intel_encoder *intel_encoder =
10148 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010149 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010150 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010151 struct drm_crtc *crtc = NULL;
10152 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020010153 struct drm_i915_private *dev_priv = to_i915(dev);
Rob Clark51fd3712013-11-19 12:10:12 -050010154 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010155 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010156 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010157 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010158 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010159
Chris Wilsond2dff872011-04-19 08:36:26 +010010160 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010161 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010162 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010163
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010164 old->restore_state = NULL;
10165
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010166 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010167
Jesse Barnes79e53942008-11-07 14:24:08 -080010168 /*
10169 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010170 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010171 * - if the connector already has an assigned crtc, use it (but make
10172 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010173 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010174 * - try to find the first unused crtc that can drive this connector,
10175 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010176 */
10177
10178 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010179 if (connector->state->crtc) {
10180 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010181
Rob Clark51fd3712013-11-19 12:10:12 -050010182 ret = drm_modeset_lock(&crtc->mutex, ctx);
10183 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010184 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010185
10186 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010187 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010188 }
10189
10190 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010191 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010192 i++;
10193 if (!(encoder->possible_crtcs & (1 << i)))
10194 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010195
10196 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10197 if (ret)
10198 goto fail;
10199
10200 if (possible_crtc->state->enable) {
10201 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010202 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010203 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010204
10205 crtc = possible_crtc;
10206 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010207 }
10208
10209 /*
10210 * If we didn't find an unused CRTC, don't use any.
10211 */
10212 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010213 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Dan Carpenterf4bf77b2017-04-14 22:54:25 +030010214 ret = -ENODEV;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010215 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010216 }
10217
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010218found:
10219 intel_crtc = to_intel_crtc(crtc);
10220
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010221 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010222 restore_state = drm_atomic_state_alloc(dev);
10223 if (!state || !restore_state) {
10224 ret = -ENOMEM;
10225 goto fail;
10226 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010227
10228 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010229 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010230
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010231 connector_state = drm_atomic_get_connector_state(state, connector);
10232 if (IS_ERR(connector_state)) {
10233 ret = PTR_ERR(connector_state);
10234 goto fail;
10235 }
10236
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010237 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10238 if (ret)
10239 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010240
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010241 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10242 if (IS_ERR(crtc_state)) {
10243 ret = PTR_ERR(crtc_state);
10244 goto fail;
10245 }
10246
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010247 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010248
Chris Wilson64927112011-04-20 07:25:26 +010010249 if (!mode)
10250 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010251
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010252 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010253 if (ret)
10254 goto fail;
10255
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010256 ret = intel_modeset_disable_planes(state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010257 if (ret)
10258 goto fail;
10259
10260 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10261 if (!ret)
10262 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
Ville Syrjäläbe90cc32018-03-22 17:23:12 +020010263 if (!ret)
10264 ret = drm_atomic_add_affected_planes(restore_state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010265 if (ret) {
10266 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10267 goto fail;
10268 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010269
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010270 ret = drm_atomic_commit(state);
10271 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010272 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010273 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010274 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010275
10276 old->restore_state = restore_state;
Chris Wilson7abbd112017-01-19 11:37:49 +000010277 drm_atomic_state_put(state);
Chris Wilson71731882011-04-19 23:10:58 +010010278
Jesse Barnes79e53942008-11-07 14:24:08 -080010279 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020010280 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010281 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010282
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010283fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010010284 if (state) {
10285 drm_atomic_state_put(state);
10286 state = NULL;
10287 }
10288 if (restore_state) {
10289 drm_atomic_state_put(restore_state);
10290 restore_state = NULL;
10291 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010292
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010293 if (ret == -EDEADLK)
10294 return ret;
Rob Clark51fd3712013-11-19 12:10:12 -050010295
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010296 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010297}
10298
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010299void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010300 struct intel_load_detect_pipe *old,
10301 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010302{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010303 struct intel_encoder *intel_encoder =
10304 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010305 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010306 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010307 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010308
Chris Wilsond2dff872011-04-19 08:36:26 +010010309 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010310 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010311 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010312
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010313 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010314 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010315
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010010316 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Chris Wilson08536952016-10-14 13:18:18 +010010317 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010318 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010010319 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010320}
10321
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010322static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010323 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010324{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010325 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010326 u32 dpll = pipe_config->dpll_hw_state.dpll;
10327
10328 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010329 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010010330 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010331 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010332 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010333 return 96000;
10334 else
10335 return 48000;
10336}
10337
Jesse Barnes79e53942008-11-07 14:24:08 -080010338/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010339static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010340 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010341{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010342 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010343 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010344 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010345 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010346 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010347 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010348 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010349 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010350
10351 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010352 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010353 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010354 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010355
10356 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010357 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010358 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10359 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010360 } else {
10361 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10362 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10363 }
10364
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010365 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010366 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010367 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10368 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010369 else
10370 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010371 DPLL_FPA01_P1_POST_DIV_SHIFT);
10372
10373 switch (dpll & DPLL_MODE_MASK) {
10374 case DPLLB_MODE_DAC_SERIAL:
10375 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10376 5 : 10;
10377 break;
10378 case DPLLB_MODE_LVDS:
10379 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10380 7 : 14;
10381 break;
10382 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010383 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010384 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010385 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010386 }
10387
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010388 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +030010389 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010390 else
Imre Deakdccbea32015-06-22 23:35:51 +030010391 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010392 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010393 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010394 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010395
10396 if (is_lvds) {
10397 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10398 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010399
10400 if (lvds & LVDS_CLKB_POWER_UP)
10401 clock.p2 = 7;
10402 else
10403 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 } else {
10405 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10406 clock.p1 = 2;
10407 else {
10408 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10409 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10410 }
10411 if (dpll & PLL_P2_DIVIDE_BY_4)
10412 clock.p2 = 4;
10413 else
10414 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010415 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010416
Imre Deakdccbea32015-06-22 23:35:51 +030010417 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010418 }
10419
Ville Syrjälä18442d02013-09-13 16:00:08 +030010420 /*
10421 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010422 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010423 * encoder's get_config() function.
10424 */
Imre Deakdccbea32015-06-22 23:35:51 +030010425 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010426}
10427
Ville Syrjälä6878da02013-09-13 15:59:11 +030010428int intel_dotclock_calculate(int link_freq,
10429 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010430{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010431 /*
10432 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010433 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010434 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010435 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010436 *
10437 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010438 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010439 */
10440
Ville Syrjälä6878da02013-09-13 15:59:11 +030010441 if (!m_n->link_n)
10442 return 0;
10443
Chris Wilson31236982017-09-13 11:51:53 +010010444 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 +030010445}
10446
Ville Syrjälä18442d02013-09-13 16:00:08 +030010447static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010448 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010449{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010450 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010451
10452 /* read out port_clock from the DPLL */
10453 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010454
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010455 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010456 * In case there is an active pipe without active ports,
10457 * we may need some idea for the dotclock anyway.
10458 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010459 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010460 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010461 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010462 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010463}
10464
Ville Syrjäläde330812017-10-09 19:19:50 +030010465/* Returns the currently programmed mode of the given encoder. */
10466struct drm_display_mode *
10467intel_encoder_current_mode(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010468{
Ville Syrjäläde330812017-10-09 19:19:50 +030010469 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
10470 struct intel_crtc_state *crtc_state;
Jesse Barnes79e53942008-11-07 14:24:08 -080010471 struct drm_display_mode *mode;
Ville Syrjäläde330812017-10-09 19:19:50 +030010472 struct intel_crtc *crtc;
10473 enum pipe pipe;
10474
10475 if (!encoder->get_hw_state(encoder, &pipe))
10476 return NULL;
10477
10478 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -080010479
10480 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10481 if (!mode)
10482 return NULL;
10483
Ville Syrjäläde330812017-10-09 19:19:50 +030010484 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
10485 if (!crtc_state) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010486 kfree(mode);
10487 return NULL;
10488 }
10489
Ville Syrjäläde330812017-10-09 19:19:50 +030010490 crtc_state->base.crtc = &crtc->base;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010491
Ville Syrjäläde330812017-10-09 19:19:50 +030010492 if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
10493 kfree(crtc_state);
10494 kfree(mode);
10495 return NULL;
10496 }
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010497
Ville Syrjäläde330812017-10-09 19:19:50 +030010498 encoder->get_config(encoder, crtc_state);
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010499
Ville Syrjäläde330812017-10-09 19:19:50 +030010500 intel_mode_from_pipe_config(mode, crtc_state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010501
Ville Syrjäläde330812017-10-09 19:19:50 +030010502 kfree(crtc_state);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010503
Jesse Barnes79e53942008-11-07 14:24:08 -080010504 return mode;
10505}
10506
10507static void intel_crtc_destroy(struct drm_crtc *crtc)
10508{
10509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10510
10511 drm_crtc_cleanup(crtc);
10512 kfree(intel_crtc);
10513}
10514
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010515/**
10516 * intel_wm_need_update - Check whether watermarks need updating
10517 * @plane: drm plane
10518 * @state: new plane state
10519 *
10520 * Check current plane state versus the new one to determine whether
10521 * watermarks need to be recalculated.
10522 *
10523 * Returns true or false.
10524 */
10525static bool intel_wm_need_update(struct drm_plane *plane,
10526 struct drm_plane_state *state)
10527{
Matt Roperd21fbe82015-09-24 15:53:12 -070010528 struct intel_plane_state *new = to_intel_plane_state(state);
10529 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10530
10531 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010532 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010533 return true;
10534
10535 if (!cur->base.fb || !new->base.fb)
10536 return false;
10537
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010538 if (cur->base.fb->modifier != new->base.fb->modifier ||
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010539 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010540 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10541 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10542 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10543 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010544 return true;
10545
10546 return false;
10547}
10548
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010549static bool needs_scaling(const struct intel_plane_state *state)
Matt Roperd21fbe82015-09-24 15:53:12 -070010550{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010551 int src_w = drm_rect_width(&state->base.src) >> 16;
10552 int src_h = drm_rect_height(&state->base.src) >> 16;
10553 int dst_w = drm_rect_width(&state->base.dst);
10554 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070010555
10556 return (src_w != dst_w || src_h != dst_h);
10557}
10558
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010559int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
10560 struct drm_crtc_state *crtc_state,
10561 const struct intel_plane_state *old_plane_state,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010562 struct drm_plane_state *plane_state)
10563{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010564 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010565 struct drm_crtc *crtc = crtc_state->crtc;
10566 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010567 struct intel_plane *plane = to_intel_plane(plane_state->plane);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010568 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080010569 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010570 bool mode_changed = needs_modeset(crtc_state);
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010571 bool was_crtc_enabled = old_crtc_state->base.active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010572 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010573 bool turn_off, turn_on, visible, was_visible;
10574 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030010575 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010576
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010577 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010578 ret = skl_update_scaler_plane(
10579 to_intel_crtc_state(crtc_state),
10580 to_intel_plane_state(plane_state));
10581 if (ret)
10582 return ret;
10583 }
10584
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010585 was_visible = old_plane_state->base.visible;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010586 visible = plane_state->visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010587
10588 if (!was_crtc_enabled && WARN_ON(was_visible))
10589 was_visible = false;
10590
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010591 /*
10592 * Visibility is calculated as if the crtc was on, but
10593 * after scaler setup everything depends on it being off
10594 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030010595 *
10596 * FIXME this is wrong for watermarks. Watermarks should also
10597 * be computed as if the pipe would be active. Perhaps move
10598 * per-plane wm computation to the .check_plane() hook, and
10599 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010600 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010601 if (!is_crtc_enabled) {
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010602 plane_state->visible = visible = false;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010603 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10604 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010605
10606 if (!was_visible && !visible)
10607 return 0;
10608
Maarten Lankhorste8861672016-02-24 11:24:26 +010010609 if (fb != old_plane_state->base.fb)
10610 pipe_config->fb_changed = true;
10611
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010612 turn_off = was_visible && (!visible || mode_changed);
10613 turn_on = visible && (!was_visible || mode_changed);
10614
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010615 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010616 intel_crtc->base.base.id, intel_crtc->base.name,
10617 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010618 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010619
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010620 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 +020010621 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010622 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010623 turn_off, turn_on, mode_changed);
10624
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010625 if (turn_on) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010626 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010627 pipe_config->update_wm_pre = true;
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010628
10629 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010630 if (plane->id != PLANE_CURSOR)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010631 pipe_config->disable_cxsr = true;
10632 } else if (turn_off) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010633 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010634 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010635
Ville Syrjälä852eb002015-06-24 22:00:07 +030010636 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010637 if (plane->id != PLANE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010638 pipe_config->disable_cxsr = true;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010639 } else if (intel_wm_need_update(&plane->base, plane_state)) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010640 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010641 /* FIXME bollocks */
10642 pipe_config->update_wm_pre = true;
10643 pipe_config->update_wm_post = true;
10644 }
Ville Syrjälä852eb002015-06-24 22:00:07 +030010645 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010646
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070010647 if (visible || was_visible)
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010648 pipe_config->fb_bits |= plane->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010649
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010650 /*
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010651 * ILK/SNB DVSACNTR/Sprite Enable
10652 * IVB SPR_CTL/Sprite Enable
10653 * "When in Self Refresh Big FIFO mode, a write to enable the
10654 * plane will be internally buffered and delayed while Big FIFO
10655 * mode is exiting."
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010656 *
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010657 * Which means that enabling the sprite can take an extra frame
10658 * when we start in big FIFO mode (LP1+). Thus we need to drop
10659 * down to LP0 and wait for vblank in order to make sure the
10660 * sprite gets enabled on the next vblank after the register write.
10661 * Doing otherwise would risk enabling the sprite one frame after
10662 * we've already signalled flip completion. We can resume LP1+
10663 * once the sprite has been enabled.
10664 *
10665 *
10666 * WaCxSRDisabledForSpriteScaling:ivb
10667 * IVB SPR_SCALE/Scaling Enable
10668 * "Low Power watermarks must be disabled for at least one
10669 * frame before enabling sprite scaling, and kept disabled
10670 * until sprite scaling is disabled."
10671 *
10672 * ILK/SNB DVSASCALE/Scaling Enable
10673 * "When in Self Refresh Big FIFO mode, scaling enable will be
10674 * masked off while Big FIFO mode is exiting."
10675 *
10676 * Despite the w/a only being listed for IVB we assume that
10677 * the ILK/SNB note has similar ramifications, hence we apply
10678 * the w/a on all three platforms.
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010679 */
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010680 if (plane->id == PLANE_SPRITE0 &&
10681 (IS_GEN5(dev_priv) || IS_GEN6(dev_priv) ||
10682 IS_IVYBRIDGE(dev_priv)) &&
10683 (turn_on || (!needs_scaling(old_plane_state) &&
10684 needs_scaling(to_intel_plane_state(plane_state)))))
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010685 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010686
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010687 return 0;
10688}
10689
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010690static bool encoders_cloneable(const struct intel_encoder *a,
10691 const struct intel_encoder *b)
10692{
10693 /* masks could be asymmetric, so check both ways */
10694 return a == b || (a->cloneable & (1 << b->type) &&
10695 b->cloneable & (1 << a->type));
10696}
10697
10698static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10699 struct intel_crtc *crtc,
10700 struct intel_encoder *encoder)
10701{
10702 struct intel_encoder *source_encoder;
10703 struct drm_connector *connector;
10704 struct drm_connector_state *connector_state;
10705 int i;
10706
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010707 for_each_new_connector_in_state(state, connector, connector_state, i) {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010708 if (connector_state->crtc != &crtc->base)
10709 continue;
10710
10711 source_encoder =
10712 to_intel_encoder(connector_state->best_encoder);
10713 if (!encoders_cloneable(encoder, source_encoder))
10714 return false;
10715 }
10716
10717 return true;
10718}
10719
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +020010720static int icl_add_linked_planes(struct intel_atomic_state *state)
10721{
10722 struct intel_plane *plane, *linked;
10723 struct intel_plane_state *plane_state, *linked_plane_state;
10724 int i;
10725
10726 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10727 linked = plane_state->linked_plane;
10728
10729 if (!linked)
10730 continue;
10731
10732 linked_plane_state = intel_atomic_get_plane_state(state, linked);
10733 if (IS_ERR(linked_plane_state))
10734 return PTR_ERR(linked_plane_state);
10735
10736 WARN_ON(linked_plane_state->linked_plane != plane);
10737 WARN_ON(linked_plane_state->slave == plane_state->slave);
10738 }
10739
10740 return 0;
10741}
10742
10743static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
10744{
10745 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
10746 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10747 struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->base.state);
10748 struct intel_plane *plane, *linked;
10749 struct intel_plane_state *plane_state;
10750 int i;
10751
10752 if (INTEL_GEN(dev_priv) < 11)
10753 return 0;
10754
10755 /*
10756 * Destroy all old plane links and make the slave plane invisible
10757 * in the crtc_state->active_planes mask.
10758 */
10759 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10760 if (plane->pipe != crtc->pipe || !plane_state->linked_plane)
10761 continue;
10762
10763 plane_state->linked_plane = NULL;
10764 if (plane_state->slave && !plane_state->base.visible)
10765 crtc_state->active_planes &= ~BIT(plane->id);
10766
10767 plane_state->slave = false;
10768 }
10769
10770 if (!crtc_state->nv12_planes)
10771 return 0;
10772
10773 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10774 struct intel_plane_state *linked_state = NULL;
10775
10776 if (plane->pipe != crtc->pipe ||
10777 !(crtc_state->nv12_planes & BIT(plane->id)))
10778 continue;
10779
10780 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
10781 if (!icl_is_nv12_y_plane(linked->id))
10782 continue;
10783
10784 if (crtc_state->active_planes & BIT(linked->id))
10785 continue;
10786
10787 linked_state = intel_atomic_get_plane_state(state, linked);
10788 if (IS_ERR(linked_state))
10789 return PTR_ERR(linked_state);
10790
10791 break;
10792 }
10793
10794 if (!linked_state) {
10795 DRM_DEBUG_KMS("Need %d free Y planes for NV12\n",
10796 hweight8(crtc_state->nv12_planes));
10797
10798 return -EINVAL;
10799 }
10800
10801 plane_state->linked_plane = linked;
10802
10803 linked_state->slave = true;
10804 linked_state->linked_plane = plane;
10805 crtc_state->active_planes |= BIT(linked->id);
10806 DRM_DEBUG_KMS("Using %s as Y plane for %s\n", linked->base.name, plane->base.name);
10807 }
10808
10809 return 0;
10810}
10811
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010812static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10813 struct drm_crtc_state *crtc_state)
10814{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010815 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010816 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010818 struct intel_crtc_state *pipe_config =
10819 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010820 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020010821 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010822 bool mode_changed = needs_modeset(crtc_state);
10823
Ville Syrjälä852eb002015-06-24 22:00:07 +030010824 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010825 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020010826
Maarten Lankhorstad421372015-06-15 12:33:42 +020010827 if (mode_changed && crtc_state->enable &&
10828 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010829 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020010830 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10831 pipe_config);
10832 if (ret)
10833 return ret;
10834 }
10835
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010836 if (crtc_state->color_mgmt_changed) {
10837 ret = intel_color_check(crtc, crtc_state);
10838 if (ret)
10839 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010010840
10841 /*
10842 * Changing color management on Intel hardware is
10843 * handled as part of planes update.
10844 */
10845 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010846 }
10847
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010848 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010849 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010010850 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080010851 if (ret) {
10852 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070010853 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080010854 }
10855 }
10856
10857 if (dev_priv->display.compute_intermediate_wm &&
10858 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10859 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10860 return 0;
10861
10862 /*
10863 * Calculate 'intermediate' watermarks that satisfy both the
10864 * old state and the new state. We can program these
10865 * immediately.
10866 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010867 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080010868 intel_crtc,
10869 pipe_config);
10870 if (ret) {
10871 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
10872 return ret;
10873 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070010874 } else if (dev_priv->display.compute_intermediate_wm) {
10875 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10876 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010877 }
10878
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010879 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010880 if (mode_changed)
10881 ret = skl_update_scaler_crtc(pipe_config);
10882
10883 if (!ret)
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +020010884 ret = icl_check_nv12_planes(pipe_config);
10885 if (!ret)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +053010886 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
10887 pipe_config);
10888 if (!ret)
Ander Conselvan de Oliveira6ebc6922017-02-23 09:15:59 +020010889 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010890 pipe_config);
10891 }
10892
Maarten Lankhorst24f28452017-11-22 19:39:01 +010010893 if (HAS_IPS(dev_priv))
10894 pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
10895
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010896 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010897}
10898
Jani Nikula65b38e02015-04-13 11:26:56 +030010899static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010900 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010901};
10902
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010903static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10904{
10905 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010906 struct drm_connector_list_iter conn_iter;
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010907
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010908 drm_connector_list_iter_begin(dev, &conn_iter);
10909 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020010910 if (connector->base.state->crtc)
Thomas Zimmermannef196b52018-06-18 13:01:50 +020010911 drm_connector_put(&connector->base);
Daniel Vetter8863dc72016-05-06 15:39:03 +020010912
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010913 if (connector->base.encoder) {
10914 connector->base.state->best_encoder =
10915 connector->base.encoder;
10916 connector->base.state->crtc =
10917 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020010918
Thomas Zimmermannef196b52018-06-18 13:01:50 +020010919 drm_connector_get(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010920 } else {
10921 connector->base.state->best_encoder = NULL;
10922 connector->base.state->crtc = NULL;
10923 }
10924 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010925 drm_connector_list_iter_end(&conn_iter);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010926}
10927
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010928static int
10929connected_sink_max_bpp(const struct drm_connector_state *conn_state,
10930 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010931{
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010932 int bpp;
10933 struct drm_display_info *info = &conn_state->connector->display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010934
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010935 switch (conn_state->max_bpc) {
10936 case 6 ... 7:
10937 bpp = 6 * 3;
10938 break;
10939 case 8 ... 9:
10940 bpp = 8 * 3;
10941 break;
10942 case 10 ... 11:
10943 bpp = 10 * 3;
10944 break;
10945 case 12:
10946 bpp = 12 * 3;
10947 break;
10948 default:
10949 return -EINVAL;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010950 }
10951
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010952 if (bpp < pipe_config->pipe_bpp) {
10953 DRM_DEBUG_KMS("Limiting display bpp to %d instead of Edid bpp "
10954 "%d, requested bpp %d, max platform bpp %d\n", bpp,
10955 3 * info->bpc, 3 * conn_state->max_requested_bpc,
10956 pipe_config->pipe_bpp);
10957 pipe_config->pipe_bpp = bpp;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010958 }
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010959 return 0;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010960}
10961
10962static int
10963compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010964 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010965{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010966 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010967 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010968 struct drm_connector *connector;
10969 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010970 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010971
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010972 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
10973 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010974 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010975 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020010976 bpp = 12*3;
10977 else
10978 bpp = 8*3;
10979
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010980
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010981 pipe_config->pipe_bpp = bpp;
10982
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010983 state = pipe_config->base.state;
10984
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010985 /* Clamp display bpp to EDID value */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010986 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010987 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010988 continue;
10989
Radhakrishna Sripadaf1a12172018-10-22 18:44:00 -070010990 if (connected_sink_max_bpp(connector_state, pipe_config) < 0)
10991 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010992 }
10993
10994 return bpp;
10995}
10996
Daniel Vetter644db712013-09-19 14:53:58 +020010997static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10998{
10999 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11000 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011001 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011002 mode->crtc_hdisplay, mode->crtc_hsync_start,
11003 mode->crtc_hsync_end, mode->crtc_htotal,
11004 mode->crtc_vdisplay, mode->crtc_vsync_start,
11005 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11006}
11007
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011008static inline void
11009intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011010 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011011{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011012 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11013 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011014 m_n->gmch_m, m_n->gmch_n,
11015 m_n->link_m, m_n->link_n, m_n->tu);
11016}
11017
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011018#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
11019
11020static const char * const output_type_str[] = {
11021 OUTPUT_TYPE(UNUSED),
11022 OUTPUT_TYPE(ANALOG),
11023 OUTPUT_TYPE(DVO),
11024 OUTPUT_TYPE(SDVO),
11025 OUTPUT_TYPE(LVDS),
11026 OUTPUT_TYPE(TVOUT),
11027 OUTPUT_TYPE(HDMI),
11028 OUTPUT_TYPE(DP),
11029 OUTPUT_TYPE(EDP),
11030 OUTPUT_TYPE(DSI),
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011031 OUTPUT_TYPE(DDI),
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011032 OUTPUT_TYPE(DP_MST),
11033};
11034
11035#undef OUTPUT_TYPE
11036
11037static void snprintf_output_types(char *buf, size_t len,
11038 unsigned int output_types)
11039{
11040 char *str = buf;
11041 int i;
11042
11043 str[0] = '\0';
11044
11045 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
11046 int r;
11047
11048 if ((output_types & BIT(i)) == 0)
11049 continue;
11050
11051 r = snprintf(str, len, "%s%s",
11052 str != buf ? "," : "", output_type_str[i]);
11053 if (r >= len)
11054 break;
11055 str += r;
11056 len -= r;
11057
11058 output_types &= ~BIT(i);
11059 }
11060
11061 WARN_ON_ONCE(output_types != 0);
11062}
11063
Shashank Sharmad9facae2018-10-12 11:53:07 +053011064static const char * const output_format_str[] = {
11065 [INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
11066 [INTEL_OUTPUT_FORMAT_RGB] = "RGB",
Shashank Sharma33b7f3e2018-10-12 11:53:08 +053011067 [INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
Shashank Sharma8c79f842018-10-12 11:53:09 +053011068 [INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
Shashank Sharmad9facae2018-10-12 11:53:07 +053011069};
11070
11071static const char *output_formats(enum intel_output_format format)
11072{
Shashank Sharma33b7f3e2018-10-12 11:53:08 +053011073 if (format >= ARRAY_SIZE(output_format_str))
Shashank Sharmad9facae2018-10-12 11:53:07 +053011074 format = INTEL_OUTPUT_FORMAT_INVALID;
11075 return output_format_str[format];
11076}
11077
Daniel Vetterc0b03412013-05-28 12:05:54 +020011078static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011079 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011080 const char *context)
11081{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011082 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011083 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011084 struct drm_plane *plane;
11085 struct intel_plane *intel_plane;
11086 struct intel_plane_state *state;
11087 struct drm_framebuffer *fb;
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011088 char buf[64];
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011089
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000011090 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11091 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011092
Ville Syrjälä40b2be42017-10-10 15:11:59 +030011093 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
11094 DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
11095 buf, pipe_config->output_types);
11096
Shashank Sharmad9facae2018-10-12 11:53:07 +053011097 DRM_DEBUG_KMS("output format: %s\n",
11098 output_formats(pipe_config->output_format));
11099
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011100 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11101 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020011102 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011103
11104 if (pipe_config->has_pch_encoder)
11105 intel_dump_m_n_config(pipe_config, "fdi",
11106 pipe_config->fdi_lanes,
11107 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011108
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011109 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011110 intel_dump_m_n_config(pipe_config, "dp m_n",
11111 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000011112 if (pipe_config->has_drrs)
11113 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11114 pipe_config->lane_count,
11115 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011116 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011117
Daniel Vetter55072d12014-11-20 16:10:28 +010011118 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011119 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010011120
Daniel Vetterc0b03412013-05-28 12:05:54 +020011121 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011122 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011123 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011124 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11125 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011126 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011127 pipe_config->port_clock,
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011128 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11129 pipe_config->pixel_rate);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011130
11131 if (INTEL_GEN(dev_priv) >= 9)
11132 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11133 crtc->num_scalers,
11134 pipe_config->scaler_state.scaler_users,
11135 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011136
11137 if (HAS_GMCH_DISPLAY(dev_priv))
11138 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11139 pipe_config->gmch_pfit.control,
11140 pipe_config->gmch_pfit.pgm_ratios,
11141 pipe_config->gmch_pfit.lvds_border_bits);
11142 else
11143 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11144 pipe_config->pch_pfit.pos,
11145 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000011146 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011147
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011148 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11149 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011150
Ander Conselvan de Oliveiraf50b79f2016-12-29 17:22:12 +020011151 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011152
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011153 DRM_DEBUG_KMS("planes on this crtc\n");
11154 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011155 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011156 intel_plane = to_intel_plane(plane);
11157 if (intel_plane->pipe != crtc->pipe)
11158 continue;
11159
11160 state = to_intel_plane_state(plane->state);
11161 fb = state->base.fb;
11162 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030011163 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11164 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011165 continue;
11166 }
11167
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011168 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11169 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011170 fb->base.id, fb->width, fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +020011171 drm_get_format_name(fb->format->format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011172 if (INTEL_GEN(dev_priv) >= 9)
11173 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11174 state->scaler_id,
11175 state->base.src.x1 >> 16,
11176 state->base.src.y1 >> 16,
11177 drm_rect_width(&state->base.src) >> 16,
11178 drm_rect_height(&state->base.src) >> 16,
11179 state->base.dst.x1, state->base.dst.y1,
11180 drm_rect_width(&state->base.dst),
11181 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011182 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011183}
11184
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011185static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011186{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011187 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011188 struct drm_connector *connector;
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011189 struct drm_connector_list_iter conn_iter;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011190 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011191 unsigned int used_mst_ports = 0;
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011192 bool ret = true;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011193
11194 /*
11195 * Walk the connector list instead of the encoder
11196 * list to detect the problem on ddi platforms
11197 * where there's just one encoder per digital port.
11198 */
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011199 drm_connector_list_iter_begin(dev, &conn_iter);
11200 drm_for_each_connector_iter(connector, &conn_iter) {
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011201 struct drm_connector_state *connector_state;
11202 struct intel_encoder *encoder;
11203
Maarten Lankhorst8b694492018-04-09 14:46:55 +020011204 connector_state = drm_atomic_get_new_connector_state(state, connector);
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011205 if (!connector_state)
11206 connector_state = connector->state;
11207
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011208 if (!connector_state->best_encoder)
11209 continue;
11210
11211 encoder = to_intel_encoder(connector_state->best_encoder);
11212
11213 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011214
11215 switch (encoder->type) {
11216 unsigned int port_mask;
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011217 case INTEL_OUTPUT_DDI:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011218 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011219 break;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -050011220 /* else: fall through */
Ville Syrjäläcca05022016-06-22 21:57:06 +030011221 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011222 case INTEL_OUTPUT_HDMI:
11223 case INTEL_OUTPUT_EDP:
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020011224 port_mask = 1 << encoder->port;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011225
11226 /* the same port mustn't appear more than once */
11227 if (used_ports & port_mask)
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011228 ret = false;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011229
11230 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011231 break;
11232 case INTEL_OUTPUT_DP_MST:
11233 used_mst_ports |=
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020011234 1 << encoder->port;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011235 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011236 default:
11237 break;
11238 }
11239 }
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011240 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011241
Ville Syrjälä477321e2016-07-28 17:50:40 +030011242 /* can't mix MST and SST/HDMI on the same port */
11243 if (used_ports & used_mst_ports)
11244 return false;
11245
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011246 return ret;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011247}
11248
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011249static void
11250clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11251{
Ville Syrjäläff32c542017-03-02 19:14:57 +020011252 struct drm_i915_private *dev_priv =
11253 to_i915(crtc_state->base.crtc->dev);
Chandra Konduru663a3642015-04-07 15:28:41 -070011254 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011255 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011256 struct intel_shared_dpll *shared_dpll;
Ville Syrjäläff32c542017-03-02 19:14:57 +020011257 struct intel_crtc_wm_state wm_state;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011258 bool force_thru, ips_force_disable;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011259
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011260 /* FIXME: before the switch to atomic started, a new pipe_config was
11261 * kzalloc'd. Code that depends on any field being zero should be
11262 * fixed, so that the crtc_state can be safely duplicated. For now,
11263 * only fields that are know to not cause problems are preserved. */
11264
Chandra Konduru663a3642015-04-07 15:28:41 -070011265 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011266 shared_dpll = crtc_state->shared_dpll;
11267 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011268 force_thru = crtc_state->pch_pfit.force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011269 ips_force_disable = crtc_state->ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011270 if (IS_G4X(dev_priv) ||
11271 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011272 wm_state = crtc_state->wm;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011273
Chris Wilsond2fa80a2017-03-03 15:46:44 +000011274 /* Keep base drm_crtc_state intact, only clear our extended struct */
11275 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11276 memset(&crtc_state->base + 1, 0,
11277 sizeof(*crtc_state) - sizeof(crtc_state->base));
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011278
Chandra Konduru663a3642015-04-07 15:28:41 -070011279 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011280 crtc_state->shared_dpll = shared_dpll;
11281 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011282 crtc_state->pch_pfit.force_thru = force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011283 crtc_state->ips_force_disable = ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011284 if (IS_G4X(dev_priv) ||
11285 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011286 crtc_state->wm = wm_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011287}
11288
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011289static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011290intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011291 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011292{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011293 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020011294 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011295 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011296 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011297 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011298 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011299 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011300
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011301 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011302
Daniel Vettere143a212013-07-04 12:01:15 +020011303 pipe_config->cpu_transcoder =
11304 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011305
Imre Deak2960bc92013-07-30 13:36:32 +030011306 /*
11307 * Sanitize sync polarity flags based on requested ones. If neither
11308 * positive or negative polarity is requested, treat this as meaning
11309 * negative polarity.
11310 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011311 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011312 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011313 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011314
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011315 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011316 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011317 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011318
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011319 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11320 pipe_config);
11321 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011322 goto fail;
11323
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011324 /*
11325 * Determine the real pipe dimensions. Note that stereo modes can
11326 * increase the actual pipe size due to the frame doubling and
11327 * insertion of additional space for blanks between the frame. This
11328 * is stored in the crtc timings. We use the requested mode to do this
11329 * computation to clearly distinguish it from the adjusted mode, which
11330 * can be changed by the connectors in the below retry loop.
11331 */
Daniel Vetter196cd5d2017-01-25 07:26:56 +010011332 drm_mode_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011333 &pipe_config->pipe_src_w,
11334 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011335
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011336 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011337 if (connector_state->crtc != crtc)
11338 continue;
11339
11340 encoder = to_intel_encoder(connector_state->best_encoder);
11341
Ville Syrjäläe25148d2016-06-22 21:57:09 +030011342 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11343 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11344 goto fail;
11345 }
11346
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011347 /*
11348 * Determine output_types before calling the .compute_config()
11349 * hooks so that the hooks can use this information safely.
11350 */
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011351 if (encoder->compute_output_type)
11352 pipe_config->output_types |=
11353 BIT(encoder->compute_output_type(encoder, pipe_config,
11354 connector_state));
11355 else
11356 pipe_config->output_types |= BIT(encoder->type);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011357 }
11358
Daniel Vettere29c22c2013-02-21 00:00:16 +010011359encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011360 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011361 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011362 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011363
Daniel Vetter135c81b2013-07-21 21:37:09 +020011364 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011365 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11366 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011367
Daniel Vetter7758a112012-07-08 19:40:39 +020011368 /* Pass our mode to the connectors and the CRTC to give them a chance to
11369 * adjust it according to limitations or connector properties, and also
11370 * a chance to reject the mode entirely.
11371 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011372 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011373 if (connector_state->crtc != crtc)
11374 continue;
11375
11376 encoder = to_intel_encoder(connector_state->best_encoder);
11377
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020011378 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020011379 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011380 goto fail;
11381 }
11382 }
11383
Daniel Vetterff9a6752013-06-01 17:16:21 +020011384 /* Set default port clock if not overwritten by the encoder. Needs to be
11385 * done afterwards in case the encoder adjusts the mode. */
11386 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011387 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011388 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011389
Daniel Vettera43f6e02013-06-07 23:10:32 +020011390 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011391 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011392 DRM_DEBUG_KMS("CRTC fixup failed\n");
11393 goto fail;
11394 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011395
11396 if (ret == RETRY) {
11397 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11398 ret = -EINVAL;
11399 goto fail;
11400 }
11401
11402 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11403 retry = false;
11404 goto encoder_retry;
11405 }
11406
Daniel Vettere8fa4272015-08-12 11:43:34 +020011407 /* Dithering seems to not pass-through bits correctly when it should, so
Manasi Navare611032b2017-01-24 08:21:49 -080011408 * only enable it on 6bpc panels and when its not a compliance
11409 * test requesting 6bpc video pattern.
11410 */
11411 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11412 !pipe_config->dither_force_disable;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020011413 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011414 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011415
Daniel Vetter7758a112012-07-08 19:40:39 +020011416fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011417 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011418}
11419
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011420static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011421{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011422 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011423
11424 if (clock1 == clock2)
11425 return true;
11426
11427 if (!clock1 || !clock2)
11428 return false;
11429
11430 diff = abs(clock1 - clock2);
11431
11432 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11433 return true;
11434
11435 return false;
11436}
11437
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011438static bool
11439intel_compare_m_n(unsigned int m, unsigned int n,
11440 unsigned int m2, unsigned int n2,
11441 bool exact)
11442{
11443 if (m == m2 && n == n2)
11444 return true;
11445
11446 if (exact || !m || !n || !m2 || !n2)
11447 return false;
11448
11449 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11450
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011451 if (n > n2) {
11452 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011453 m2 <<= 1;
11454 n2 <<= 1;
11455 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011456 } else if (n < n2) {
11457 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011458 m <<= 1;
11459 n <<= 1;
11460 }
11461 }
11462
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011463 if (n != n2)
11464 return false;
11465
11466 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011467}
11468
11469static bool
11470intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11471 struct intel_link_m_n *m2_n2,
11472 bool adjust)
11473{
11474 if (m_n->tu == m2_n2->tu &&
11475 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11476 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11477 intel_compare_m_n(m_n->link_m, m_n->link_n,
11478 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11479 if (adjust)
11480 *m2_n2 = *m_n;
11481
11482 return true;
11483 }
11484
11485 return false;
11486}
11487
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011488static void __printf(3, 4)
11489pipe_config_err(bool adjust, const char *name, const char *format, ...)
11490{
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011491 struct va_format vaf;
11492 va_list args;
11493
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011494 va_start(args, format);
11495 vaf.fmt = format;
11496 vaf.va = &args;
11497
Joe Perches99a95482018-03-13 15:02:15 -070011498 if (adjust)
11499 drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
11500 else
11501 drm_err("mismatch in %s %pV", name, &vaf);
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011502
11503 va_end(args);
11504}
11505
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011506static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011507intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011508 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011509 struct intel_crtc_state *pipe_config,
11510 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011511{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011512 bool ret = true;
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011513 bool fixup_inherited = adjust &&
11514 (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
11515 !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011516
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011517#define PIPE_CONF_CHECK_X(name) do { \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011518 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011519 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011520 "(expected 0x%08x, found 0x%08x)\n", \
11521 current_config->name, \
11522 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011523 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011524 } \
11525} while (0)
Daniel Vetter66e985c2013-06-05 13:34:20 +020011526
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011527#define PIPE_CONF_CHECK_I(name) do { \
Daniel Vetter08a24032013-04-19 11:25:34 +020011528 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011529 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter08a24032013-04-19 11:25:34 +020011530 "(expected %i, found %i)\n", \
11531 current_config->name, \
11532 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011533 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011534 } \
11535} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011536
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011537#define PIPE_CONF_CHECK_BOOL(name) do { \
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011538 if (current_config->name != pipe_config->name) { \
11539 pipe_config_err(adjust, __stringify(name), \
11540 "(expected %s, found %s)\n", \
11541 yesno(current_config->name), \
11542 yesno(pipe_config->name)); \
11543 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011544 } \
11545} while (0)
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011546
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011547/*
11548 * Checks state where we only read out the enabling, but not the entire
11549 * state itself (like full infoframes or ELD for audio). These states
11550 * require a full modeset on bootup to fix up.
11551 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011552#define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011553 if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
11554 PIPE_CONF_CHECK_BOOL(name); \
11555 } else { \
11556 pipe_config_err(adjust, __stringify(name), \
11557 "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
11558 yesno(current_config->name), \
11559 yesno(pipe_config->name)); \
11560 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011561 } \
11562} while (0)
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011563
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011564#define PIPE_CONF_CHECK_P(name) do { \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011565 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011566 pipe_config_err(adjust, __stringify(name), \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011567 "(expected %p, found %p)\n", \
11568 current_config->name, \
11569 pipe_config->name); \
11570 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011571 } \
11572} while (0)
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011573
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011574#define PIPE_CONF_CHECK_M_N(name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011575 if (!intel_compare_link_m_n(&current_config->name, \
11576 &pipe_config->name,\
11577 adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011578 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011579 "(expected tu %i gmch %i/%i link %i/%i, " \
11580 "found tu %i, gmch %i/%i link %i/%i)\n", \
11581 current_config->name.tu, \
11582 current_config->name.gmch_m, \
11583 current_config->name.gmch_n, \
11584 current_config->name.link_m, \
11585 current_config->name.link_n, \
11586 pipe_config->name.tu, \
11587 pipe_config->name.gmch_m, \
11588 pipe_config->name.gmch_n, \
11589 pipe_config->name.link_m, \
11590 pipe_config->name.link_n); \
11591 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011592 } \
11593} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011594
Daniel Vetter55c561a2016-03-30 11:34:36 +020011595/* This is required for BDW+ where there is only one set of registers for
11596 * switching between high and low RR.
11597 * This macro can be used whenever a comparison has to be made between one
11598 * hw state and multiple sw state variables.
11599 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011600#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011601 if (!intel_compare_link_m_n(&current_config->name, \
11602 &pipe_config->name, adjust) && \
11603 !intel_compare_link_m_n(&current_config->alt_name, \
11604 &pipe_config->name, adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011605 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011606 "(expected tu %i gmch %i/%i link %i/%i, " \
11607 "or tu %i gmch %i/%i link %i/%i, " \
11608 "found tu %i, gmch %i/%i link %i/%i)\n", \
11609 current_config->name.tu, \
11610 current_config->name.gmch_m, \
11611 current_config->name.gmch_n, \
11612 current_config->name.link_m, \
11613 current_config->name.link_n, \
11614 current_config->alt_name.tu, \
11615 current_config->alt_name.gmch_m, \
11616 current_config->alt_name.gmch_n, \
11617 current_config->alt_name.link_m, \
11618 current_config->alt_name.link_n, \
11619 pipe_config->name.tu, \
11620 pipe_config->name.gmch_m, \
11621 pipe_config->name.gmch_n, \
11622 pipe_config->name.link_m, \
11623 pipe_config->name.link_n); \
11624 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011625 } \
11626} while (0)
Daniel Vetter88adfff2013-03-28 10:42:01 +010011627
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011628#define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011629 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011630 pipe_config_err(adjust, __stringify(name), \
11631 "(%x) (expected %i, found %i)\n", \
11632 (mask), \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011633 current_config->name & (mask), \
11634 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011635 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011636 } \
11637} while (0)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011638
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011639#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011640 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011641 pipe_config_err(adjust, __stringify(name), \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011642 "(expected %i, found %i)\n", \
11643 current_config->name, \
11644 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011645 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011646 } \
11647} while (0)
Ville Syrjälä5e550652013-09-06 23:29:07 +030011648
Daniel Vetterbb760062013-06-06 14:55:52 +020011649#define PIPE_CONF_QUIRK(quirk) \
11650 ((current_config->quirks | pipe_config->quirks) & (quirk))
11651
Daniel Vettereccb1402013-05-22 00:50:22 +020011652 PIPE_CONF_CHECK_I(cpu_transcoder);
11653
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011654 PIPE_CONF_CHECK_BOOL(has_pch_encoder);
Daniel Vetter08a24032013-04-19 11:25:34 +020011655 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011656 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020011657
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011658 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030011659 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011660
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011661 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011662 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011663
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011664 if (current_config->has_drrs)
11665 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11666 } else
11667 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011668
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011669 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020011670
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011671 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11672 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11673 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11674 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11675 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11676 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011677
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011678 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11679 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11680 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11681 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11682 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11683 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011684
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011685 PIPE_CONF_CHECK_I(pixel_multiplier);
Shashank Sharmad9facae2018-10-12 11:53:07 +053011686 PIPE_CONF_CHECK_I(output_format);
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011687 PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011688 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010011689 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011690 PIPE_CONF_CHECK_BOOL(limited_color_range);
Shashank Sharma15953632017-03-13 16:54:03 +053011691
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011692 PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
11693 PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011694 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011695
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011696 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011697
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011698 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011699 DRM_MODE_FLAG_INTERLACE);
11700
Daniel Vetterbb760062013-06-06 14:55:52 +020011701 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011702 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011703 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011704 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011705 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011706 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011707 DRM_MODE_FLAG_PVSYNC);
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_NVSYNC);
11710 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011711
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011712 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020011713 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011714 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020011715 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011716 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020011717
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011718 if (!adjust) {
11719 PIPE_CONF_CHECK_I(pipe_src_w);
11720 PIPE_CONF_CHECK_I(pipe_src_h);
11721
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011722 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011723 if (current_config->pch_pfit.enabled) {
11724 PIPE_CONF_CHECK_X(pch_pfit.pos);
11725 PIPE_CONF_CHECK_X(pch_pfit.size);
11726 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011727
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011728 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011729 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011730 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070011731
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011732 PIPE_CONF_CHECK_BOOL(double_wide);
Ville Syrjälä282740f2013-09-04 18:30:03 +030011733
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011734 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011735 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011736 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011737 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11738 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011739 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010011740 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011741 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11742 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11743 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Paulo Zanoni2de38132017-09-22 17:53:42 -030011744 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
11745 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
11746 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
11747 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
11748 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
11749 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
11750 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
11751 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
11752 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
11753 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
11754 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
11755 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
Paulo Zanonic27e9172018-04-27 16:14:36 -070011756 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
11757 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
11758 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
11759 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
11760 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
11761 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
11762 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
11763 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
11764 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
11765 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011766
Ville Syrjälä47eacba2016-04-12 22:14:35 +030011767 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11768 PIPE_CONF_CHECK_X(dsi_pll.div);
11769
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011770 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011771 PIPE_CONF_CHECK_I(pipe_bpp);
11772
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011773 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011774 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011775
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030011776 PIPE_CONF_CHECK_I(min_voltage_level);
11777
Daniel Vetter66e985c2013-06-05 13:34:20 +020011778#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011779#undef PIPE_CONF_CHECK_I
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011780#undef PIPE_CONF_CHECK_BOOL
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011781#undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011782#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011783#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011784#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011785#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011786
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011787 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011788}
11789
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011790static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11791 const struct intel_crtc_state *pipe_config)
11792{
11793 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011794 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011795 &pipe_config->fdi_m_n);
11796 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11797
11798 /*
11799 * FDI already provided one idea for the dotclock.
11800 * Yell if the encoder disagrees.
11801 */
11802 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11803 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11804 fdi_dotclock, dotclock);
11805 }
11806}
11807
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011808static void verify_wm_state(struct drm_crtc *crtc,
11809 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000011810{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011811 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000011812 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011813 struct skl_pipe_wm hw_wm, *sw_wm;
11814 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11815 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11817 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011818 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000011819
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011820 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000011821 return;
11822
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011823 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020011824 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011825
Damien Lespiau08db6652014-11-04 17:06:52 +000011826 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11827 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11828
Mahesh Kumar74bd8002018-04-26 19:55:15 +053011829 if (INTEL_GEN(dev_priv) >= 11)
11830 if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
11831 DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
11832 sw_ddb->enabled_slices,
11833 hw_ddb.enabled_slices);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011834 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070011835 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011836 hw_plane_wm = &hw_wm.planes[plane];
11837 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000011838
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011839 /* Watermarks */
11840 for (level = 0; level <= max_level; level++) {
11841 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11842 &sw_plane_wm->wm[level]))
11843 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000011844
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011845 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",
11846 pipe_name(pipe), plane + 1, level,
11847 sw_plane_wm->wm[level].plane_en,
11848 sw_plane_wm->wm[level].plane_res_b,
11849 sw_plane_wm->wm[level].plane_res_l,
11850 hw_plane_wm->wm[level].plane_en,
11851 hw_plane_wm->wm[level].plane_res_b,
11852 hw_plane_wm->wm[level].plane_res_l);
11853 }
11854
11855 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11856 &sw_plane_wm->trans_wm)) {
11857 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",
11858 pipe_name(pipe), plane + 1,
11859 sw_plane_wm->trans_wm.plane_en,
11860 sw_plane_wm->trans_wm.plane_res_b,
11861 sw_plane_wm->trans_wm.plane_res_l,
11862 hw_plane_wm->trans_wm.plane_en,
11863 hw_plane_wm->trans_wm.plane_res_b,
11864 hw_plane_wm->trans_wm.plane_res_l);
11865 }
11866
11867 /* DDB */
11868 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11869 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11870
11871 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011872 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 -040011873 pipe_name(pipe), plane + 1,
11874 sw_ddb_entry->start, sw_ddb_entry->end,
11875 hw_ddb_entry->start, hw_ddb_entry->end);
11876 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011877 }
11878
Lyude27082492016-08-24 07:48:10 +020011879 /*
11880 * cursor
11881 * If the cursor plane isn't active, we may not have updated it's ddb
11882 * allocation. In that case since the ddb allocation will be updated
11883 * once the plane becomes visible, we can skip this check
11884 */
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030011885 if (1) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011886 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11887 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011888
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011889 /* Watermarks */
11890 for (level = 0; level <= max_level; level++) {
11891 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11892 &sw_plane_wm->wm[level]))
11893 continue;
11894
11895 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",
11896 pipe_name(pipe), level,
11897 sw_plane_wm->wm[level].plane_en,
11898 sw_plane_wm->wm[level].plane_res_b,
11899 sw_plane_wm->wm[level].plane_res_l,
11900 hw_plane_wm->wm[level].plane_en,
11901 hw_plane_wm->wm[level].plane_res_b,
11902 hw_plane_wm->wm[level].plane_res_l);
11903 }
11904
11905 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11906 &sw_plane_wm->trans_wm)) {
11907 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11908 pipe_name(pipe),
11909 sw_plane_wm->trans_wm.plane_en,
11910 sw_plane_wm->trans_wm.plane_res_b,
11911 sw_plane_wm->trans_wm.plane_res_l,
11912 hw_plane_wm->trans_wm.plane_en,
11913 hw_plane_wm->trans_wm.plane_res_b,
11914 hw_plane_wm->trans_wm.plane_res_l);
11915 }
11916
11917 /* DDB */
11918 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11919 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
11920
11921 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011922 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020011923 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011924 sw_ddb_entry->start, sw_ddb_entry->end,
11925 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020011926 }
Damien Lespiau08db6652014-11-04 17:06:52 +000011927 }
11928}
11929
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011930static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011931verify_connector_state(struct drm_device *dev,
11932 struct drm_atomic_state *state,
11933 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011934{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011935 struct drm_connector *connector;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011936 struct drm_connector_state *new_conn_state;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011937 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011938
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011939 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011940 struct drm_encoder *encoder = connector->encoder;
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011941 struct drm_crtc_state *crtc_state = NULL;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011942
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011943 if (new_conn_state->crtc != crtc)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011944 continue;
11945
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011946 if (crtc)
11947 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
11948
11949 intel_connector_verify_state(crtc_state, new_conn_state);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011950
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011951 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011952 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011953 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011954}
11955
11956static void
Daniel Vetter86b04262017-03-01 10:52:26 +010011957verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011958{
11959 struct intel_encoder *encoder;
Daniel Vetter86b04262017-03-01 10:52:26 +010011960 struct drm_connector *connector;
11961 struct drm_connector_state *old_conn_state, *new_conn_state;
11962 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011963
Damien Lespiaub2784e12014-08-05 11:29:37 +010011964 for_each_intel_encoder(dev, encoder) {
Daniel Vetter86b04262017-03-01 10:52:26 +010011965 bool enabled = false, found = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011966 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011967
11968 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11969 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011970 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011971
Daniel Vetter86b04262017-03-01 10:52:26 +010011972 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
11973 new_conn_state, i) {
11974 if (old_conn_state->best_encoder == &encoder->base)
11975 found = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011976
Daniel Vetter86b04262017-03-01 10:52:26 +010011977 if (new_conn_state->best_encoder != &encoder->base)
11978 continue;
11979 found = enabled = true;
11980
11981 I915_STATE_WARN(new_conn_state->crtc !=
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011982 encoder->base.crtc,
11983 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011984 }
Daniel Vetter86b04262017-03-01 10:52:26 +010011985
11986 if (!found)
11987 continue;
Dave Airlie0e32b392014-05-02 14:02:48 +100011988
Rob Clarke2c719b2014-12-15 13:56:32 -050011989 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011990 "encoder's enabled state mismatch "
11991 "(expected %i, found %i)\n",
11992 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011993
11994 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011995 bool active;
11996
11997 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011998 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011999 "encoder detached but still enabled on pipe %c.\n",
12000 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012001 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012002 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012003}
12004
12005static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012006verify_crtc_state(struct drm_crtc *crtc,
12007 struct drm_crtc_state *old_crtc_state,
12008 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012009{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012010 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012011 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012012 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12014 struct intel_crtc_state *pipe_config, *sw_config;
12015 struct drm_atomic_state *old_state;
12016 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012017
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012018 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020012019 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012020 pipe_config = to_intel_crtc_state(old_crtc_state);
12021 memset(pipe_config, 0, sizeof(*pipe_config));
12022 pipe_config->base.crtc = crtc;
12023 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012024
Ville Syrjälä78108b72016-05-27 20:59:19 +030012025 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012026
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012027 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012028
Ville Syrjäläe56134b2017-06-01 17:36:19 +030012029 /* we keep both pipes enabled on 830 */
12030 if (IS_I830(dev_priv))
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012031 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012032
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012033 I915_STATE_WARN(new_crtc_state->active != active,
12034 "crtc active state doesn't match with hw state "
12035 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012036
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012037 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12038 "transitional active state does not match atomic hw state "
12039 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012040
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012041 for_each_encoder_on_crtc(dev, crtc, encoder) {
12042 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012043
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012044 active = encoder->get_hw_state(encoder, &pipe);
12045 I915_STATE_WARN(active != new_crtc_state->active,
12046 "[ENCODER:%i] active %i with crtc active %i\n",
12047 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012048
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012049 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12050 "Encoder connected to wrong pipe %c\n",
12051 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012052
Ville Syrjäläe1214b92017-10-27 22:31:23 +030012053 if (active)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012054 encoder->get_config(encoder, pipe_config);
12055 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012056
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020012057 intel_crtc_compute_pixel_rate(pipe_config);
12058
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012059 if (!new_crtc_state->active)
12060 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012061
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012062 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012063
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020012064 sw_config = to_intel_crtc_state(new_crtc_state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012065 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012066 pipe_config, false)) {
12067 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12068 intel_dump_pipe_config(intel_crtc, pipe_config,
12069 "[hw state]");
12070 intel_dump_pipe_config(intel_crtc, sw_config,
12071 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012072 }
12073}
12074
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012075static void
Ville Syrjäläcff109f2017-11-17 21:19:17 +020012076intel_verify_planes(struct intel_atomic_state *state)
12077{
12078 struct intel_plane *plane;
12079 const struct intel_plane_state *plane_state;
12080 int i;
12081
12082 for_each_new_intel_plane_in_state(state, plane,
12083 plane_state, i)
12084 assert_plane(plane, plane_state->base.visible);
12085}
12086
12087static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012088verify_single_dpll_state(struct drm_i915_private *dev_priv,
12089 struct intel_shared_dpll *pll,
12090 struct drm_crtc *crtc,
12091 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012092{
12093 struct intel_dpll_hw_state dpll_hw_state;
Ville Syrjälä40560e22018-06-26 22:47:11 +030012094 unsigned int crtc_mask;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012095 bool active;
12096
12097 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12098
Lucas De Marchi72f775f2018-03-20 15:06:34 -070012099 DRM_DEBUG_KMS("%s\n", pll->info->name);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012100
Lucas De Marchiee1398b2018-03-20 15:06:33 -070012101 active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012102
Lucas De Marchi5cd281f2018-03-20 15:06:36 -070012103 if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012104 I915_STATE_WARN(!pll->on && pll->active_mask,
12105 "pll in active use but not on in sw tracking\n");
12106 I915_STATE_WARN(pll->on && !pll->active_mask,
12107 "pll is on but not used by any active crtc\n");
12108 I915_STATE_WARN(pll->on != active,
12109 "pll on state mismatch (expected %i, found %i)\n",
12110 pll->on, active);
12111 }
12112
12113 if (!crtc) {
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012114 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012115 "more active pll users than references: %x vs %x\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012116 pll->active_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012117
12118 return;
12119 }
12120
Ville Syrjälä40560e22018-06-26 22:47:11 +030012121 crtc_mask = drm_crtc_mask(crtc);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012122
12123 if (new_state->active)
12124 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12125 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12126 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12127 else
12128 I915_STATE_WARN(pll->active_mask & crtc_mask,
12129 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12130 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12131
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012132 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012133 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012134 crtc_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012135
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012136 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012137 &dpll_hw_state,
12138 sizeof(dpll_hw_state)),
12139 "pll hw state mismatch\n");
12140}
12141
12142static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012143verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12144 struct drm_crtc_state *old_crtc_state,
12145 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012146{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012147 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012148 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12149 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12150
12151 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012152 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012153
12154 if (old_state->shared_dpll &&
12155 old_state->shared_dpll != new_state->shared_dpll) {
Ville Syrjälä40560e22018-06-26 22:47:11 +030012156 unsigned int crtc_mask = drm_crtc_mask(crtc);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012157 struct intel_shared_dpll *pll = old_state->shared_dpll;
12158
12159 I915_STATE_WARN(pll->active_mask & crtc_mask,
12160 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12161 pipe_name(drm_crtc_index(crtc)));
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012162 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012163 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12164 pipe_name(drm_crtc_index(crtc)));
12165 }
12166}
12167
12168static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012169intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012170 struct drm_atomic_state *state,
12171 struct drm_crtc_state *old_state,
12172 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012173{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012174 if (!needs_modeset(new_state) &&
12175 !to_intel_crtc_state(new_state)->update_pipe)
12176 return;
12177
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012178 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012179 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012180 verify_crtc_state(crtc, old_state, new_state);
12181 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012182}
12183
12184static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012185verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012186{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012187 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012188 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012189
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012190 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012191 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012192}
Daniel Vetter53589012013-06-05 13:34:16 +020012193
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012194static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012195intel_modeset_verify_disabled(struct drm_device *dev,
12196 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012197{
Daniel Vetter86b04262017-03-01 10:52:26 +010012198 verify_encoder_state(dev, state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012199 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012200 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020012201}
12202
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012203static void update_scanline_offset(const struct intel_crtc_state *crtc_state)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012204{
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012205 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012206 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012207
12208 /*
12209 * The scanline counter increments at the leading edge of hsync.
12210 *
12211 * On most platforms it starts counting from vtotal-1 on the
12212 * first active line. That means the scanline counter value is
12213 * always one less than what we would expect. Ie. just after
12214 * start of vblank, which also occurs at start of hsync (on the
12215 * last active line), the scanline counter will read vblank_start-1.
12216 *
12217 * On gen2 the scanline counter starts counting from 1 instead
12218 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12219 * to keep the value positive), instead of adding one.
12220 *
12221 * On HSW+ the behaviour of the scanline counter depends on the output
12222 * type. For DP ports it behaves like most other platforms, but on HDMI
12223 * there's an extra 1 line difference. So we need to add two instead of
12224 * one to the value.
Ville Syrjäläec1b4ee2016-12-15 19:47:34 +020012225 *
12226 * On VLV/CHV DSI the scanline counter would appear to increment
12227 * approx. 1/3 of a scanline before start of vblank. Unfortunately
12228 * that means we can't tell whether we're in vblank or not while
12229 * we're on that particular line. We must still set scanline_offset
12230 * to 1 so that the vblank timestamps come out correct when we query
12231 * the scanline counter from within the vblank interrupt handler.
12232 * However if queried just before the start of vblank we'll get an
12233 * answer that's slightly in the future.
Ville Syrjälä80715b22014-05-15 20:23:23 +030012234 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012235 if (IS_GEN2(dev_priv)) {
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012236 const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012237 int vtotal;
12238
Ville Syrjälä124abe02015-09-08 13:40:45 +030012239 vtotal = adjusted_mode->crtc_vtotal;
12240 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012241 vtotal /= 2;
12242
12243 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012244 } else if (HAS_DDI(dev_priv) &&
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012245 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012246 crtc->scanline_offset = 2;
12247 } else
12248 crtc->scanline_offset = 1;
12249}
12250
Maarten Lankhorstad421372015-06-15 12:33:42 +020012251static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012252{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012253 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012254 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012255 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012256 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012257 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012258
12259 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012260 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012261
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012262 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012264 struct intel_shared_dpll *old_dpll =
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012265 to_intel_crtc_state(old_crtc_state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012266
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012267 if (!needs_modeset(new_crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012268 continue;
12269
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012270 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012271
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012272 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012273 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012274
Ander Conselvan de Oliveiraa1c414e2016-12-29 17:22:07 +020012275 intel_release_shared_dpll(old_dpll, intel_crtc, state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012276 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012277}
12278
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012279/*
12280 * This implements the workaround described in the "notes" section of the mode
12281 * set sequence documentation. When going from no pipes or single pipe to
12282 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12283 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12284 */
12285static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12286{
12287 struct drm_crtc_state *crtc_state;
12288 struct intel_crtc *intel_crtc;
12289 struct drm_crtc *crtc;
12290 struct intel_crtc_state *first_crtc_state = NULL;
12291 struct intel_crtc_state *other_crtc_state = NULL;
12292 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12293 int i;
12294
12295 /* look at all crtc's that are going to be enabled in during modeset */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012296 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012297 intel_crtc = to_intel_crtc(crtc);
12298
12299 if (!crtc_state->active || !needs_modeset(crtc_state))
12300 continue;
12301
12302 if (first_crtc_state) {
12303 other_crtc_state = to_intel_crtc_state(crtc_state);
12304 break;
12305 } else {
12306 first_crtc_state = to_intel_crtc_state(crtc_state);
12307 first_pipe = intel_crtc->pipe;
12308 }
12309 }
12310
12311 /* No workaround needed? */
12312 if (!first_crtc_state)
12313 return 0;
12314
12315 /* w/a possibly needed, check how many crtc's are already enabled. */
12316 for_each_intel_crtc(state->dev, intel_crtc) {
12317 struct intel_crtc_state *pipe_config;
12318
12319 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12320 if (IS_ERR(pipe_config))
12321 return PTR_ERR(pipe_config);
12322
12323 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12324
12325 if (!pipe_config->base.active ||
12326 needs_modeset(&pipe_config->base))
12327 continue;
12328
12329 /* 2 or more enabled crtcs means no need for w/a */
12330 if (enabled_pipe != INVALID_PIPE)
12331 return 0;
12332
12333 enabled_pipe = intel_crtc->pipe;
12334 }
12335
12336 if (enabled_pipe != INVALID_PIPE)
12337 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12338 else if (other_crtc_state)
12339 other_crtc_state->hsw_workaround_pipe = first_pipe;
12340
12341 return 0;
12342}
12343
Ville Syrjälä8d965612016-11-14 18:35:10 +020012344static int intel_lock_all_pipes(struct drm_atomic_state *state)
12345{
12346 struct drm_crtc *crtc;
12347
12348 /* Add all pipes to the state */
12349 for_each_crtc(state->dev, crtc) {
12350 struct drm_crtc_state *crtc_state;
12351
12352 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12353 if (IS_ERR(crtc_state))
12354 return PTR_ERR(crtc_state);
12355 }
12356
12357 return 0;
12358}
12359
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012360static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12361{
12362 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012363
Ville Syrjälä8d965612016-11-14 18:35:10 +020012364 /*
12365 * Add all pipes to the state, and force
12366 * a modeset on all the active ones.
12367 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012368 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012369 struct drm_crtc_state *crtc_state;
12370 int ret;
12371
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012372 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12373 if (IS_ERR(crtc_state))
12374 return PTR_ERR(crtc_state);
12375
12376 if (!crtc_state->active || needs_modeset(crtc_state))
12377 continue;
12378
12379 crtc_state->mode_changed = true;
12380
12381 ret = drm_atomic_add_affected_connectors(state, crtc);
12382 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012383 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012384
12385 ret = drm_atomic_add_affected_planes(state, crtc);
12386 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012387 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012388 }
12389
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012390 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012391}
12392
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012393static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012394{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012395 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012396 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012397 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012398 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012399 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012400
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012401 if (!check_digital_port_conflicts(state)) {
12402 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12403 return -EINVAL;
12404 }
12405
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012406 intel_state->modeset = true;
12407 intel_state->active_crtcs = dev_priv->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012408 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12409 intel_state->cdclk.actual = dev_priv->cdclk.actual;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012410
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012411 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12412 if (new_crtc_state->active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012413 intel_state->active_crtcs |= 1 << i;
12414 else
12415 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070012416
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012417 if (old_crtc_state->active != new_crtc_state->active)
Matt Roper8b4a7d02016-05-12 07:06:00 -070012418 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012419 }
12420
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012421 /*
12422 * See if the config requires any additional preparation, e.g.
12423 * to adjust global state with pipes off. We need to do this
12424 * here so we can get the modeset_pipe updated config for the new
12425 * mode set on this crtc. For other crtcs we need to use the
12426 * adjusted_mode bits in the crtc directly.
12427 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012428 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030012429 ret = dev_priv->display.modeset_calc_cdclk(state);
12430 if (ret < 0)
12431 return ret;
12432
Ville Syrjälä8d965612016-11-14 18:35:10 +020012433 /*
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012434 * Writes to dev_priv->cdclk.logical must protected by
Ville Syrjälä8d965612016-11-14 18:35:10 +020012435 * holding all the crtc locks, even if we don't end up
12436 * touching the hardware
12437 */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012438 if (intel_cdclk_changed(&dev_priv->cdclk.logical,
12439 &intel_state->cdclk.logical)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012440 ret = intel_lock_all_pipes(state);
12441 if (ret < 0)
12442 return ret;
12443 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012444
Ville Syrjälä8d965612016-11-14 18:35:10 +020012445 /* All pipes must be switched off while we change the cdclk. */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012446 if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
12447 &intel_state->cdclk.actual)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012448 ret = intel_modeset_all_pipes(state);
12449 if (ret < 0)
12450 return ret;
12451 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010012452
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012453 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12454 intel_state->cdclk.logical.cdclk,
12455 intel_state->cdclk.actual.cdclk);
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030012456 DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
12457 intel_state->cdclk.logical.voltage_level,
12458 intel_state->cdclk.actual.voltage_level);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012459 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012460 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012461 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012462
Maarten Lankhorstad421372015-06-15 12:33:42 +020012463 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012464
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012465 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012466 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012467
Maarten Lankhorstad421372015-06-15 12:33:42 +020012468 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012469}
12470
Matt Roperaa363132015-09-24 15:53:18 -070012471/*
12472 * Handle calculation of various watermark data at the end of the atomic check
12473 * phase. The code here should be run after the per-crtc and per-plane 'check'
12474 * handlers to ensure that all derived state has been updated.
12475 */
Matt Roper55994c22016-05-12 07:06:08 -070012476static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070012477{
12478 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070012479 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070012480
12481 /* Is there platform-specific watermark information to calculate? */
12482 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070012483 return dev_priv->display.compute_global_watermarks(state);
12484
12485 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070012486}
12487
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012488/**
12489 * intel_atomic_check - validate state object
12490 * @dev: drm device
12491 * @state: state to validate
12492 */
12493static int intel_atomic_check(struct drm_device *dev,
12494 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012495{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012496 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070012497 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012498 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012499 struct drm_crtc_state *old_crtc_state, *crtc_state;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012500 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012501 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012502
Maarten Lankhorst8c58f732018-02-21 10:28:08 +010012503 /* Catch I915_MODE_FLAG_INHERITED */
12504 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
12505 crtc_state, i) {
12506 if (crtc_state->mode.private_flags !=
12507 old_crtc_state->mode.private_flags)
12508 crtc_state->mode_changed = true;
12509 }
12510
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012511 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012512 if (ret)
12513 return ret;
12514
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012515 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012516 struct intel_crtc_state *pipe_config =
12517 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012518
Daniel Vetter26495482015-07-15 14:15:52 +020012519 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012520 continue;
12521
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012522 if (!crtc_state->enable) {
12523 any_ms = true;
12524 continue;
12525 }
12526
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012527 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012528 if (ret) {
12529 intel_dump_pipe_config(to_intel_crtc(crtc),
12530 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012531 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012532 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012533
Michal Wajdeczko4f044a82017-09-19 19:38:44 +000012534 if (i915_modparams.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012535 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012536 to_intel_crtc_state(old_crtc_state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012537 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012538 crtc_state->mode_changed = false;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012539 pipe_config->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020012540 }
12541
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012542 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020012543 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012544
Daniel Vetter26495482015-07-15 14:15:52 +020012545 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12546 needs_modeset(crtc_state) ?
12547 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012548 }
12549
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012550 if (any_ms) {
12551 ret = intel_modeset_checks(state);
12552
12553 if (ret)
12554 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012555 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012556 intel_state->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012557 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012558
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +020012559 ret = icl_add_linked_planes(intel_state);
12560 if (ret)
12561 return ret;
12562
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012563 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070012564 if (ret)
12565 return ret;
12566
Ville Syrjälädd576022017-11-17 21:19:14 +020012567 intel_fbc_choose_crtc(dev_priv, intel_state);
Matt Roper55994c22016-05-12 07:06:08 -070012568 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012569}
12570
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012571static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010012572 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012573{
Chris Wilsonfd700752017-07-26 17:00:36 +010012574 return drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012575}
12576
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012577u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12578{
12579 struct drm_device *dev = crtc->base.dev;
12580
12581 if (!dev->max_vblank_count)
Dhinakaran Pandiyan734cbbf2018-02-02 21:12:54 -080012582 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012583
12584 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12585}
12586
Lyude896e5bb2016-08-24 07:48:09 +020012587static void intel_update_crtc(struct drm_crtc *crtc,
12588 struct drm_atomic_state *state,
12589 struct drm_crtc_state *old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012590 struct drm_crtc_state *new_crtc_state)
Lyude896e5bb2016-08-24 07:48:09 +020012591{
12592 struct drm_device *dev = crtc->dev;
12593 struct drm_i915_private *dev_priv = to_i915(dev);
12594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst6c246b82018-09-20 12:27:08 +020012595 struct intel_crtc_state *old_intel_cstate = to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012596 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12597 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012598 struct intel_plane_state *new_plane_state =
12599 intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
12600 to_intel_plane(crtc->primary));
Lyude896e5bb2016-08-24 07:48:09 +020012601
12602 if (modeset) {
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020012603 update_scanline_offset(pipe_config);
Lyude896e5bb2016-08-24 07:48:09 +020012604 dev_priv->display.crtc_enable(pipe_config, state);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012605
12606 /* vblanks work again, re-enable pipe CRC. */
12607 intel_crtc_enable_pipe_crc(intel_crtc);
Lyude896e5bb2016-08-24 07:48:09 +020012608 } else {
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012609 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12610 pipe_config);
Lyude896e5bb2016-08-24 07:48:09 +020012611 }
12612
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012613 if (new_plane_state)
12614 intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
Lyude896e5bb2016-08-24 07:48:09 +020012615
Maarten Lankhorst6c246b82018-09-20 12:27:08 +020012616 intel_begin_crtc_commit(crtc, old_crtc_state);
12617
12618 intel_update_planes_on_crtc(to_intel_atomic_state(state), intel_crtc,
12619 old_intel_cstate, pipe_config);
12620
12621 intel_finish_crtc_commit(crtc, old_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012622}
12623
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012624static void intel_update_crtcs(struct drm_atomic_state *state)
Lyude896e5bb2016-08-24 07:48:09 +020012625{
12626 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012627 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyude896e5bb2016-08-24 07:48:09 +020012628 int i;
12629
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012630 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12631 if (!new_crtc_state->active)
Lyude896e5bb2016-08-24 07:48:09 +020012632 continue;
12633
12634 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012635 new_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012636 }
12637}
12638
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012639static void skl_update_crtcs(struct drm_atomic_state *state)
Lyude27082492016-08-24 07:48:10 +020012640{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012641 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020012642 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12643 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040012644 struct intel_crtc *intel_crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012645 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040012646 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020012647 unsigned int updated = 0;
12648 bool progress;
12649 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012650 int i;
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012651 u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
12652 u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012653
12654 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12655
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012656 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012657 /* ignore allocations for crtc's that have been turned off. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012658 if (new_crtc_state->active)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012659 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012660
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012661 /* If 2nd DBuf slice required, enable it here */
12662 if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
12663 icl_dbuf_slices_update(dev_priv, required_slices);
12664
Lyude27082492016-08-24 07:48:10 +020012665 /*
12666 * Whenever the number of active pipes changes, we need to make sure we
12667 * update the pipes in the right order so that their ddb allocations
12668 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12669 * cause pipe underruns and other bad stuff.
12670 */
12671 do {
Lyude27082492016-08-24 07:48:10 +020012672 progress = false;
12673
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012674 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Lyude27082492016-08-24 07:48:10 +020012675 bool vbl_wait = false;
12676 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040012677
12678 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä21794812017-08-23 18:22:26 +030012679 cstate = to_intel_crtc_state(new_crtc_state);
Lyudece0ba282016-09-15 10:46:35 -040012680 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020012681
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012682 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020012683 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012684
Mika Kahola2b685042017-10-10 13:17:03 +030012685 if (skl_ddb_allocation_overlaps(dev_priv,
12686 entries,
12687 &cstate->wm.skl.ddb,
12688 i))
Lyude27082492016-08-24 07:48:10 +020012689 continue;
12690
12691 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012692 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012693
12694 /*
12695 * If this is an already active pipe, it's DDB changed,
12696 * and this isn't the last pipe that needs updating
12697 * then we need to wait for a vblank to pass for the
12698 * new ddb allocation to take effect.
12699 */
Lyudece0ba282016-09-15 10:46:35 -040012700 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010012701 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012702 !new_crtc_state->active_changed &&
Lyude27082492016-08-24 07:48:10 +020012703 intel_state->wm_results.dirty_pipes != updated)
12704 vbl_wait = true;
12705
12706 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012707 new_crtc_state);
Lyude27082492016-08-24 07:48:10 +020012708
12709 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012710 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020012711
12712 progress = true;
12713 }
12714 } while (progress);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012715
12716 /* If 2nd DBuf slice is no more required disable it */
12717 if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
12718 icl_dbuf_slices_update(dev_priv, required_slices);
Lyude27082492016-08-24 07:48:10 +020012719}
12720
Chris Wilsonba318c62017-02-02 20:47:41 +000012721static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12722{
12723 struct intel_atomic_state *state, *next;
12724 struct llist_node *freed;
12725
12726 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12727 llist_for_each_entry_safe(state, next, freed, freed)
12728 drm_atomic_state_put(&state->base);
12729}
12730
12731static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12732{
12733 struct drm_i915_private *dev_priv =
12734 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12735
12736 intel_atomic_helper_free_state(dev_priv);
12737}
12738
Daniel Vetter9db529a2017-08-08 10:08:28 +020012739static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
12740{
12741 struct wait_queue_entry wait_fence, wait_reset;
12742 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
12743
12744 init_wait_entry(&wait_fence, 0);
12745 init_wait_entry(&wait_reset, 0);
12746 for (;;) {
12747 prepare_to_wait(&intel_state->commit_ready.wait,
12748 &wait_fence, TASK_UNINTERRUPTIBLE);
12749 prepare_to_wait(&dev_priv->gpu_error.wait_queue,
12750 &wait_reset, TASK_UNINTERRUPTIBLE);
12751
12752
12753 if (i915_sw_fence_done(&intel_state->commit_ready)
12754 || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
12755 break;
12756
12757 schedule();
12758 }
12759 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
12760 finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
12761}
12762
Chris Wilson8d52e442018-06-23 11:39:51 +010012763static void intel_atomic_cleanup_work(struct work_struct *work)
12764{
12765 struct drm_atomic_state *state =
12766 container_of(work, struct drm_atomic_state, commit_work);
12767 struct drm_i915_private *i915 = to_i915(state->dev);
12768
12769 drm_atomic_helper_cleanup_planes(&i915->drm, state);
12770 drm_atomic_helper_commit_cleanup_done(state);
12771 drm_atomic_state_put(state);
12772
12773 intel_atomic_helper_free_state(i915);
12774}
12775
Daniel Vetter94f05022016-06-14 18:01:00 +020012776static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012777{
Daniel Vetter94f05022016-06-14 18:01:00 +020012778 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012779 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012780 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012781 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012782 struct intel_crtc_state *new_intel_crtc_state, *old_intel_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012783 struct drm_crtc *crtc;
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012784 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020012785 u64 put_domains[I915_MAX_PIPES] = {};
Chris Wilsone95433c2016-10-28 13:58:27 +010012786 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012787
Daniel Vetter9db529a2017-08-08 10:08:28 +020012788 intel_atomic_commit_fence_wait(intel_state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020012789
Daniel Vetterea0000f2016-06-13 16:13:46 +020012790 drm_atomic_helper_wait_for_dependencies(state);
12791
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012792 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012793 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012794
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012795 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012796 old_intel_crtc_state = to_intel_crtc_state(old_crtc_state);
12797 new_intel_crtc_state = to_intel_crtc_state(new_crtc_state);
12798 intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012799
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012800 if (needs_modeset(new_crtc_state) ||
12801 to_intel_crtc_state(new_crtc_state)->update_pipe) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012802
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012803 put_domains[intel_crtc->pipe] =
Daniel Vetter5a21b662016-05-24 17:13:53 +020012804 modeset_get_crtc_power_domains(crtc,
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012805 new_intel_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012806 }
12807
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012808 if (!needs_modeset(new_crtc_state))
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012809 continue;
12810
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012811 intel_pre_plane_update(old_intel_crtc_state, new_intel_crtc_state);
Daniel Vetter460da9162013-03-27 00:44:51 +010012812
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012813 if (old_crtc_state->active) {
Maarten Lankhorstf59e9702018-09-20 12:27:07 +020012814 intel_crtc_disable_planes(intel_crtc, old_intel_crtc_state->active_planes);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012815
12816 /*
12817 * We need to disable pipe CRC before disabling the pipe,
12818 * or we race against vblank off.
12819 */
12820 intel_crtc_disable_pipe_crc(intel_crtc);
12821
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012822 dev_priv->display.crtc_disable(old_intel_crtc_state, state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012823 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020012824 intel_fbc_disable(intel_crtc);
Maarten Lankhorst65c307f2018-10-05 11:52:44 +020012825 intel_disable_shared_dpll(old_intel_crtc_state);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020012826
12827 /*
12828 * Underruns don't always raise
12829 * interrupts, so check manually.
12830 */
12831 intel_check_cpu_fifo_underruns(dev_priv);
12832 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010012833
Ville Syrjäläa748fae2018-10-25 16:05:36 +030012834 /* FIXME unify this for all platforms */
12835 if (!new_crtc_state->active &&
12836 !HAS_GMCH_DISPLAY(dev_priv) &&
12837 dev_priv->display.initial_watermarks)
12838 dev_priv->display.initial_watermarks(intel_state,
12839 new_intel_crtc_state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012840 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012841 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012842
Daniel Vetter7a1530d72017-12-07 15:32:02 +010012843 /* FIXME: Eventually get rid of our intel_crtc->config pointer */
12844 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
12845 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012846
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012847 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012848 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010012849
Ville Syrjäläb0587e42017-01-26 21:52:01 +020012850 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010012851
Lyude656d1b82016-08-17 15:55:54 -040012852 /*
12853 * SKL workaround: bspec recommends we disable the SAGV when we
12854 * have more then one pipe enabled
12855 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030012856 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012857 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012858
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012859 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012860 }
Daniel Vetter47fab732012-10-26 10:58:18 +020012861
Lyude896e5bb2016-08-24 07:48:09 +020012862 /* Complete the events for pipes that have now been disabled */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012863 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12864 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012865
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012866 /* Complete events for now disable pipes here. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012867 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012868 spin_lock_irq(&dev->event_lock);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012869 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012870 spin_unlock_irq(&dev->event_lock);
12871
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012872 new_crtc_state->event = NULL;
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012873 }
Matt Ropered4a6a72016-02-23 17:20:13 -080012874 }
12875
Lyude896e5bb2016-08-24 07:48:09 +020012876 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012877 dev_priv->display.update_crtcs(state);
Lyude896e5bb2016-08-24 07:48:09 +020012878
Daniel Vetter94f05022016-06-14 18:01:00 +020012879 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12880 * already, but still need the state for the delayed optimization. To
12881 * fix this:
12882 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12883 * - schedule that vblank worker _before_ calling hw_done
12884 * - at the start of commit_tail, cancel it _synchrously
12885 * - switch over to the vblank wait helper in the core after that since
12886 * we don't need out special handling any more.
12887 */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012888 drm_atomic_helper_wait_for_flip_done(dev, state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012889
12890 /*
12891 * Now that the vblank has passed, we can go ahead and program the
12892 * optimal watermarks on platforms that need two-step watermark
12893 * programming.
12894 *
12895 * TODO: Move this (and other cleanup) to an async worker eventually.
12896 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012897 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012898 new_intel_crtc_state = to_intel_crtc_state(new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012899
12900 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010012901 dev_priv->display.optimize_watermarks(intel_state,
Maarten Lankhorsta1cccdcf2018-09-20 12:27:04 +020012902 new_intel_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012903 }
12904
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012905 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012906 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12907
12908 if (put_domains[i])
12909 modeset_put_power_domains(dev_priv, put_domains[i]);
12910
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012911 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012912 }
12913
Ville Syrjäläcff109f2017-11-17 21:19:17 +020012914 if (intel_state->modeset)
12915 intel_verify_planes(intel_state);
12916
Paulo Zanoni56feca92016-09-22 18:00:28 -030012917 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012918 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012919
Daniel Vetter94f05022016-06-14 18:01:00 +020012920 drm_atomic_helper_commit_hw_done(state);
12921
Chris Wilsond5553c02017-05-04 12:55:08 +010012922 if (intel_state->modeset) {
12923 /* As one of the primary mmio accessors, KMS has a high
12924 * likelihood of triggering bugs in unclaimed access. After we
12925 * finish modesetting, see if an error has been flagged, and if
12926 * so enable debugging for the next modeset - and hope we catch
12927 * the culprit.
12928 */
12929 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012930 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
Chris Wilsond5553c02017-05-04 12:55:08 +010012931 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020012932
Chris Wilson8d52e442018-06-23 11:39:51 +010012933 /*
12934 * Defer the cleanup of the old state to a separate worker to not
12935 * impede the current task (userspace for blocking modesets) that
12936 * are executed inline. For out-of-line asynchronous modesets/flips,
12937 * deferring to a new worker seems overkill, but we would place a
12938 * schedule point (cond_resched()) here anyway to keep latencies
12939 * down.
12940 */
12941 INIT_WORK(&state->commit_work, intel_atomic_cleanup_work);
Chris Wilson41db6452018-07-12 12:57:29 +010012942 queue_work(system_highpri_wq, &state->commit_work);
Daniel Vetter94f05022016-06-14 18:01:00 +020012943}
12944
12945static void intel_atomic_commit_work(struct work_struct *work)
12946{
Chris Wilsonc004a902016-10-28 13:58:45 +010012947 struct drm_atomic_state *state =
12948 container_of(work, struct drm_atomic_state, commit_work);
12949
Daniel Vetter94f05022016-06-14 18:01:00 +020012950 intel_atomic_commit_tail(state);
12951}
12952
Chris Wilsonc004a902016-10-28 13:58:45 +010012953static int __i915_sw_fence_call
12954intel_atomic_commit_ready(struct i915_sw_fence *fence,
12955 enum i915_sw_fence_notify notify)
12956{
12957 struct intel_atomic_state *state =
12958 container_of(fence, struct intel_atomic_state, commit_ready);
12959
12960 switch (notify) {
12961 case FENCE_COMPLETE:
Daniel Vetter42b062b2017-08-08 10:08:27 +020012962 /* we do blocking waits in the worker, nothing to do here */
Chris Wilsonc004a902016-10-28 13:58:45 +010012963 break;
Chris Wilsonc004a902016-10-28 13:58:45 +010012964 case FENCE_FREE:
Chris Wilsoneb955ee2017-01-23 21:29:39 +000012965 {
12966 struct intel_atomic_helper *helper =
12967 &to_i915(state->base.dev)->atomic_helper;
12968
12969 if (llist_add(&state->freed, &helper->free_list))
12970 schedule_work(&helper->free_work);
12971 break;
12972 }
Chris Wilsonc004a902016-10-28 13:58:45 +010012973 }
12974
12975 return NOTIFY_DONE;
12976}
12977
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012978static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12979{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012980 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012981 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012982 int i;
12983
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012984 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012985 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012986 intel_fb_obj(new_plane_state->fb),
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012987 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012988}
12989
Daniel Vetter94f05022016-06-14 18:01:00 +020012990/**
12991 * intel_atomic_commit - commit validated state object
12992 * @dev: DRM device
12993 * @state: the top-level driver state object
12994 * @nonblock: nonblocking commit
12995 *
12996 * This function commits a top-level state object that has been validated
12997 * with drm_atomic_helper_check().
12998 *
Daniel Vetter94f05022016-06-14 18:01:00 +020012999 * RETURNS
13000 * Zero for success or -errno.
13001 */
13002static int intel_atomic_commit(struct drm_device *dev,
13003 struct drm_atomic_state *state,
13004 bool nonblock)
13005{
13006 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013007 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013008 int ret = 0;
13009
Chris Wilsonc004a902016-10-28 13:58:45 +010013010 drm_atomic_state_get(state);
13011 i915_sw_fence_init(&intel_state->commit_ready,
13012 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020013013
Ville Syrjälä440df932017-03-29 17:21:23 +030013014 /*
13015 * The intel_legacy_cursor_update() fast path takes care
13016 * of avoiding the vblank waits for simple cursor
13017 * movement and flips. For cursor on/off and size changes,
13018 * we want to perform the vblank waits so that watermark
13019 * updates happen during the correct frames. Gen9+ have
13020 * double buffered watermarks and so shouldn't need this.
13021 *
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020013022 * Unset state->legacy_cursor_update before the call to
13023 * drm_atomic_helper_setup_commit() because otherwise
13024 * drm_atomic_helper_wait_for_flip_done() is a noop and
13025 * we get FIFO underruns because we didn't wait
13026 * for vblank.
Ville Syrjälä440df932017-03-29 17:21:23 +030013027 *
13028 * FIXME doing watermarks and fb cleanup from a vblank worker
13029 * (assuming we had any) would solve these problems.
13030 */
Maarten Lankhorst213f1bd2017-09-19 14:14:19 +020013031 if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
13032 struct intel_crtc_state *new_crtc_state;
13033 struct intel_crtc *crtc;
13034 int i;
13035
13036 for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
13037 if (new_crtc_state->wm.need_postvbl_update ||
13038 new_crtc_state->update_wm_post)
13039 state->legacy_cursor_update = false;
13040 }
Ville Syrjälä440df932017-03-29 17:21:23 +030013041
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020013042 ret = intel_atomic_prepare_commit(dev, state);
13043 if (ret) {
13044 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13045 i915_sw_fence_commit(&intel_state->commit_ready);
13046 return ret;
13047 }
13048
13049 ret = drm_atomic_helper_setup_commit(state, nonblock);
13050 if (!ret)
13051 ret = drm_atomic_helper_swap_state(state, true);
13052
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020013053 if (ret) {
13054 i915_sw_fence_commit(&intel_state->commit_ready);
13055
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020013056 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020013057 return ret;
13058 }
Daniel Vetter94f05022016-06-14 18:01:00 +020013059 dev_priv->wm.distrust_bios_wm = false;
Ander Conselvan de Oliveira3c0fb582016-12-29 17:22:08 +020013060 intel_shared_dpll_swap_state(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013061 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013062
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013063 if (intel_state->modeset) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030013064 memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
13065 sizeof(intel_state->min_cdclk));
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030013066 memcpy(dev_priv->min_voltage_level,
13067 intel_state->min_voltage_level,
13068 sizeof(intel_state->min_voltage_level));
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013069 dev_priv->active_crtcs = intel_state->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020013070 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13071 dev_priv->cdclk.actual = intel_state->cdclk.actual;
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010013072 }
13073
Chris Wilson08536952016-10-14 13:18:18 +010013074 drm_atomic_state_get(state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020013075 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
Chris Wilsonc004a902016-10-28 13:58:45 +010013076
13077 i915_sw_fence_commit(&intel_state->commit_ready);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020013078 if (nonblock && intel_state->modeset) {
13079 queue_work(dev_priv->modeset_wq, &state->commit_work);
13080 } else if (nonblock) {
Daniel Vetter42b062b2017-08-08 10:08:27 +020013081 queue_work(system_unbound_wq, &state->commit_work);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020013082 } else {
13083 if (intel_state->modeset)
13084 flush_workqueue(dev_priv->modeset_wq);
Daniel Vetter94f05022016-06-14 18:01:00 +020013085 intel_atomic_commit_tail(state);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020013086 }
Mika Kuoppala75714942015-12-16 09:26:48 +020013087
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013088 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013089}
13090
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013091static const struct drm_crtc_funcs intel_crtc_funcs = {
Daniel Vetter3fab2f02017-04-03 10:32:57 +020013092 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013093 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013094 .destroy = intel_crtc_destroy,
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010013095 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013096 .atomic_duplicate_state = intel_crtc_duplicate_state,
13097 .atomic_destroy_state = intel_crtc_destroy_state,
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +010013098 .set_crc_source = intel_crtc_set_crc_source,
Mahesh Kumara8c20832018-07-13 19:29:38 +053013099 .verify_crc_source = intel_crtc_verify_crc_source,
Mahesh Kumar260bc552018-07-13 19:29:39 +053013100 .get_crc_sources = intel_crtc_get_crc_sources,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013101};
13102
Chris Wilson74d290f2017-08-17 13:37:06 +010013103struct wait_rps_boost {
13104 struct wait_queue_entry wait;
13105
13106 struct drm_crtc *crtc;
Chris Wilsone61e0f52018-02-21 09:56:36 +000013107 struct i915_request *request;
Chris Wilson74d290f2017-08-17 13:37:06 +010013108};
13109
13110static int do_rps_boost(struct wait_queue_entry *_wait,
13111 unsigned mode, int sync, void *key)
13112{
13113 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
Chris Wilsone61e0f52018-02-21 09:56:36 +000013114 struct i915_request *rq = wait->request;
Chris Wilson74d290f2017-08-17 13:37:06 +010013115
Chris Wilsone9af4ea2018-01-18 13:16:09 +000013116 /*
13117 * If we missed the vblank, but the request is already running it
13118 * is reasonable to assume that it will complete before the next
13119 * vblank without our intervention, so leave RPS alone.
13120 */
Chris Wilsone61e0f52018-02-21 09:56:36 +000013121 if (!i915_request_started(rq))
Chris Wilsone9af4ea2018-01-18 13:16:09 +000013122 gen6_rps_boost(rq, NULL);
Chris Wilsone61e0f52018-02-21 09:56:36 +000013123 i915_request_put(rq);
Chris Wilson74d290f2017-08-17 13:37:06 +010013124
13125 drm_crtc_vblank_put(wait->crtc);
13126
13127 list_del(&wait->wait.entry);
13128 kfree(wait);
13129 return 1;
13130}
13131
13132static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
13133 struct dma_fence *fence)
13134{
13135 struct wait_rps_boost *wait;
13136
13137 if (!dma_fence_is_i915(fence))
13138 return;
13139
13140 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
13141 return;
13142
13143 if (drm_crtc_vblank_get(crtc))
13144 return;
13145
13146 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
13147 if (!wait) {
13148 drm_crtc_vblank_put(crtc);
13149 return;
13150 }
13151
13152 wait->request = to_request(dma_fence_get(fence));
13153 wait->crtc = crtc;
13154
13155 wait->wait.func = do_rps_boost;
13156 wait->wait.flags = 0;
13157
13158 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
13159}
13160
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013161static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
13162{
13163 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
13164 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
13165 struct drm_framebuffer *fb = plane_state->base.fb;
13166 struct i915_vma *vma;
13167
13168 if (plane->id == PLANE_CURSOR &&
13169 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13170 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13171 const int align = intel_cursor_alignment(dev_priv);
Chris Wilson4a477652018-08-17 09:24:05 +010013172 int err;
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013173
Chris Wilson4a477652018-08-17 09:24:05 +010013174 err = i915_gem_object_attach_phys(obj, align);
13175 if (err)
13176 return err;
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013177 }
13178
13179 vma = intel_pin_and_fence_fb_obj(fb,
Ville Syrjäläf5929c52018-09-07 18:24:06 +030013180 &plane_state->view,
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013181 intel_plane_uses_fence(plane_state),
13182 &plane_state->flags);
13183 if (IS_ERR(vma))
13184 return PTR_ERR(vma);
13185
13186 plane_state->vma = vma;
13187
13188 return 0;
13189}
13190
13191static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
13192{
13193 struct i915_vma *vma;
13194
13195 vma = fetch_and_zero(&old_plane_state->vma);
13196 if (vma)
13197 intel_unpin_fb_vma(vma, old_plane_state->flags);
13198}
13199
Chris Wilsonb7268c52018-04-18 19:40:52 +010013200static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
13201{
13202 struct i915_sched_attr attr = {
13203 .priority = I915_PRIORITY_DISPLAY,
13204 };
13205
13206 i915_gem_object_wait_priority(obj, 0, &attr);
13207}
13208
Matt Roper6beb8c232014-12-01 15:40:14 -080013209/**
13210 * intel_prepare_plane_fb - Prepare fb for usage on plane
13211 * @plane: drm plane to prepare for
Chris Wilsonc38c1452018-02-14 13:49:22 +000013212 * @new_state: the plane state being prepared
Matt Roper6beb8c232014-12-01 15:40:14 -080013213 *
13214 * Prepares a framebuffer for usage on a display plane. Generally this
13215 * involves pinning the underlying object and updating the frontbuffer tracking
13216 * bits. Some older platforms need special physical address handling for
13217 * cursor planes.
13218 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013219 * Must be called with struct_mutex held.
13220 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013221 * Returns 0 on success, negative error code on failure.
13222 */
13223int
13224intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013225 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013226{
Chris Wilsonc004a902016-10-28 13:58:45 +010013227 struct intel_atomic_state *intel_state =
13228 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000013229 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013230 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013231 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013232 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010013233 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013234
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013235 if (old_obj) {
13236 struct drm_crtc_state *crtc_state =
Maarten Lankhorst8b694492018-04-09 14:46:55 +020013237 drm_atomic_get_new_crtc_state(new_state->state,
13238 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013239
13240 /* Big Hammer, we also need to ensure that any pending
13241 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13242 * current scanout is retired before unpinning the old
13243 * framebuffer. Note that we rely on userspace rendering
13244 * into the buffer attached to the pipe they are waiting
13245 * on. If not, userspace generates a GPU hang with IPEHR
13246 * point to the MI_WAIT_FOR_EVENT.
13247 *
13248 * This should only fail upon a hung GPU, in which case we
13249 * can safely continue.
13250 */
Chris Wilsonc004a902016-10-28 13:58:45 +010013251 if (needs_modeset(crtc_state)) {
13252 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13253 old_obj->resv, NULL,
13254 false, 0,
13255 GFP_KERNEL);
13256 if (ret < 0)
13257 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013258 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013259 }
13260
Chris Wilsonc004a902016-10-28 13:58:45 +010013261 if (new_state->fence) { /* explicit fencing */
13262 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13263 new_state->fence,
13264 I915_FENCE_TIMEOUT,
13265 GFP_KERNEL);
13266 if (ret < 0)
13267 return ret;
13268 }
13269
Chris Wilsonc37efb92016-06-17 08:28:47 +010013270 if (!obj)
13271 return 0;
13272
Chris Wilson4d3088c2017-07-26 17:00:38 +010013273 ret = i915_gem_object_pin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010013274 if (ret)
13275 return ret;
13276
Chris Wilson4d3088c2017-07-26 17:00:38 +010013277 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13278 if (ret) {
13279 i915_gem_object_unpin_pages(obj);
13280 return ret;
13281 }
13282
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013283 ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
Chris Wilsonfd700752017-07-26 17:00:36 +010013284
Chris Wilsonfd700752017-07-26 17:00:36 +010013285 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson4d3088c2017-07-26 17:00:38 +010013286 i915_gem_object_unpin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010013287 if (ret)
13288 return ret;
13289
Chris Wilsone2f34962018-10-01 15:47:54 +010013290 fb_obj_bump_render_priority(obj);
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080013291 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
13292
Chris Wilsonc004a902016-10-28 13:58:45 +010013293 if (!new_state->fence) { /* implicit fencing */
Chris Wilson74d290f2017-08-17 13:37:06 +010013294 struct dma_fence *fence;
13295
Chris Wilsonc004a902016-10-28 13:58:45 +010013296 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13297 obj->resv, NULL,
13298 false, I915_FENCE_TIMEOUT,
13299 GFP_KERNEL);
13300 if (ret < 0)
13301 return ret;
Chris Wilson74d290f2017-08-17 13:37:06 +010013302
13303 fence = reservation_object_get_excl_rcu(obj->resv);
13304 if (fence) {
13305 add_rps_boost_after_vblank(new_state->crtc, fence);
13306 dma_fence_put(fence);
13307 }
13308 } else {
13309 add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
Chris Wilsonc004a902016-10-28 13:58:45 +010013310 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020013311
Chris Wilson60548c52018-07-31 14:26:29 +010013312 /*
13313 * We declare pageflips to be interactive and so merit a small bias
13314 * towards upclocking to deliver the frame on time. By only changing
13315 * the RPS thresholds to sample more regularly and aim for higher
13316 * clocks we can hopefully deliver low power workloads (like kodi)
13317 * that are not quite steady state without resorting to forcing
13318 * maximum clocks following a vblank miss (see do_rps_boost()).
13319 */
13320 if (!intel_state->rps_interactive) {
13321 intel_rps_mark_interactive(dev_priv, true);
13322 intel_state->rps_interactive = true;
13323 }
13324
Chris Wilsond07f0e52016-10-28 13:58:44 +010013325 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080013326}
13327
Matt Roper38f3ce32014-12-02 07:45:25 -080013328/**
13329 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13330 * @plane: drm plane to clean up for
Chris Wilsonc38c1452018-02-14 13:49:22 +000013331 * @old_state: the state from the previous modeset
Matt Roper38f3ce32014-12-02 07:45:25 -080013332 *
13333 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013334 *
13335 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013336 */
13337void
13338intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013339 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013340{
Chris Wilson60548c52018-07-31 14:26:29 +010013341 struct intel_atomic_state *intel_state =
13342 to_intel_atomic_state(old_state->state);
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013343 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper38f3ce32014-12-02 07:45:25 -080013344
Chris Wilson60548c52018-07-31 14:26:29 +010013345 if (intel_state->rps_interactive) {
13346 intel_rps_mark_interactive(dev_priv, false);
13347 intel_state->rps_interactive = false;
13348 }
13349
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013350 /* Should only be called after a successful intel_prepare_plane_fb()! */
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013351 mutex_lock(&dev_priv->drm.struct_mutex);
13352 intel_plane_unpin_fb(to_intel_plane_state(old_state));
13353 mutex_unlock(&dev_priv->drm.struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013354}
13355
Chandra Konduru6156a452015-04-27 13:48:39 -070013356int
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013357skl_max_scale(const struct intel_crtc_state *crtc_state,
13358 u32 pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -070013359{
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013360 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
13361 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru77224cd2018-04-09 09:11:13 +053013362 int max_scale, mult;
13363 int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
Chandra Konduru6156a452015-04-27 13:48:39 -070013364
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013365 if (!crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013366 return DRM_PLANE_HELPER_NO_SCALING;
13367
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013368 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13369 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13370
Rodrigo Vivi43037c82017-10-03 15:31:42 -070013371 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013372 max_dotclk *= 2;
13373
13374 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013375 return DRM_PLANE_HELPER_NO_SCALING;
13376
13377 /*
13378 * skl max scale is lower of:
13379 * close to 3 but not 3, -1 is for that purpose
13380 * or
13381 * cdclk/crtc_clock
13382 */
Chandra Konduru77224cd2018-04-09 09:11:13 +053013383 mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
13384 tmpclk1 = (1 << 16) * mult - 1;
13385 tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
13386 max_scale = min(tmpclk1, tmpclk2);
Chandra Konduru6156a452015-04-27 13:48:39 -070013387
13388 return max_scale;
13389}
13390
Daniel Vetter5a21b662016-05-24 17:13:53 +020013391static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13392 struct drm_crtc_state *old_crtc_state)
13393{
13394 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040013395 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013396 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013397 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020013398 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013399 struct intel_atomic_state *old_intel_state =
13400 to_intel_atomic_state(old_crtc_state->state);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013401 struct intel_crtc_state *intel_cstate =
13402 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
13403 bool modeset = needs_modeset(&intel_cstate->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013404
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013405 if (!modeset &&
13406 (intel_cstate->base.color_mgmt_changed ||
13407 intel_cstate->update_pipe)) {
Ville Syrjälä5c857e62017-08-23 18:22:20 +030013408 intel_color_set_csc(&intel_cstate->base);
13409 intel_color_load_luts(&intel_cstate->base);
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013410 }
13411
Daniel Vetter5a21b662016-05-24 17:13:53 +020013412 /* Perform vblank evasion around commit operation */
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013413 intel_pipe_update_start(intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013414
13415 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013416 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013417
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013418 if (intel_cstate->update_pipe)
Ville Syrjälä1a15b772017-08-23 18:22:25 +030013419 intel_update_pipe_config(old_intel_cstate, intel_cstate);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013420 else if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +020013421 skl_detach_scalers(intel_cstate);
Lyude62e0fb82016-08-22 12:50:08 -040013422
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013423out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013424 if (dev_priv->display.atomic_update_watermarks)
13425 dev_priv->display.atomic_update_watermarks(old_intel_state,
13426 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013427}
13428
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013429void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
13430 struct intel_crtc_state *crtc_state)
13431{
13432 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13433
13434 if (!IS_GEN2(dev_priv))
13435 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
13436
13437 if (crtc_state->has_pch_encoder) {
13438 enum pipe pch_transcoder =
13439 intel_crtc_pch_transcoder(crtc);
13440
13441 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
13442 }
13443}
13444
Daniel Vetter5a21b662016-05-24 17:13:53 +020013445static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13446 struct drm_crtc_state *old_crtc_state)
13447{
13448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013449 struct intel_atomic_state *old_intel_state =
13450 to_intel_atomic_state(old_crtc_state->state);
13451 struct intel_crtc_state *new_crtc_state =
13452 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013453
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013454 intel_pipe_update_end(new_crtc_state);
Maarten Lankhorst33a49862017-11-13 15:40:43 +010013455
13456 if (new_crtc_state->update_pipe &&
13457 !needs_modeset(&new_crtc_state->base) &&
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013458 old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
13459 intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013460}
13461
Matt Ropercf4c7c12014-12-04 10:27:42 -080013462/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013463 * intel_plane_destroy - destroy a plane
13464 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013465 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013466 * Common destruction function for all types of planes (primary, cursor,
13467 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013468 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013469void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013470{
Matt Roper465c1202014-05-29 08:06:54 -070013471 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030013472 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070013473}
13474
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013475static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
13476 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013477{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013478 switch (modifier) {
13479 case DRM_FORMAT_MOD_LINEAR:
13480 case I915_FORMAT_MOD_X_TILED:
13481 break;
13482 default:
13483 return false;
13484 }
13485
Ben Widawsky714244e2017-08-01 09:58:16 -070013486 switch (format) {
13487 case DRM_FORMAT_C8:
13488 case DRM_FORMAT_RGB565:
13489 case DRM_FORMAT_XRGB1555:
13490 case DRM_FORMAT_XRGB8888:
13491 return modifier == DRM_FORMAT_MOD_LINEAR ||
13492 modifier == I915_FORMAT_MOD_X_TILED;
13493 default:
13494 return false;
13495 }
13496}
13497
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013498static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
13499 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013500{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013501 switch (modifier) {
13502 case DRM_FORMAT_MOD_LINEAR:
13503 case I915_FORMAT_MOD_X_TILED:
13504 break;
13505 default:
13506 return false;
13507 }
13508
Ben Widawsky714244e2017-08-01 09:58:16 -070013509 switch (format) {
13510 case DRM_FORMAT_C8:
13511 case DRM_FORMAT_RGB565:
13512 case DRM_FORMAT_XRGB8888:
13513 case DRM_FORMAT_XBGR8888:
13514 case DRM_FORMAT_XRGB2101010:
13515 case DRM_FORMAT_XBGR2101010:
13516 return modifier == DRM_FORMAT_MOD_LINEAR ||
13517 modifier == I915_FORMAT_MOD_X_TILED;
13518 default:
13519 return false;
13520 }
13521}
13522
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013523static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
13524 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013525{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013526 return modifier == DRM_FORMAT_MOD_LINEAR &&
13527 format == DRM_FORMAT_ARGB8888;
Ben Widawsky714244e2017-08-01 09:58:16 -070013528}
13529
Ville Syrjälä679bfe82018-10-05 15:58:07 +030013530static const struct drm_plane_funcs i965_plane_funcs = {
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013531 .update_plane = drm_atomic_helper_update_plane,
13532 .disable_plane = drm_atomic_helper_disable_plane,
13533 .destroy = intel_plane_destroy,
13534 .atomic_get_property = intel_plane_atomic_get_property,
13535 .atomic_set_property = intel_plane_atomic_set_property,
13536 .atomic_duplicate_state = intel_plane_duplicate_state,
13537 .atomic_destroy_state = intel_plane_destroy_state,
13538 .format_mod_supported = i965_plane_format_mod_supported,
13539};
13540
Ville Syrjälä679bfe82018-10-05 15:58:07 +030013541static const struct drm_plane_funcs i8xx_plane_funcs = {
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013542 .update_plane = drm_atomic_helper_update_plane,
13543 .disable_plane = drm_atomic_helper_disable_plane,
13544 .destroy = intel_plane_destroy,
13545 .atomic_get_property = intel_plane_atomic_get_property,
13546 .atomic_set_property = intel_plane_atomic_set_property,
13547 .atomic_duplicate_state = intel_plane_duplicate_state,
13548 .atomic_destroy_state = intel_plane_destroy_state,
13549 .format_mod_supported = i8xx_plane_format_mod_supported,
Matt Roper465c1202014-05-29 08:06:54 -070013550};
13551
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013552static int
13553intel_legacy_cursor_update(struct drm_plane *plane,
13554 struct drm_crtc *crtc,
13555 struct drm_framebuffer *fb,
13556 int crtc_x, int crtc_y,
13557 unsigned int crtc_w, unsigned int crtc_h,
13558 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013559 uint32_t src_w, uint32_t src_h,
13560 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013561{
13562 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13563 int ret;
13564 struct drm_plane_state *old_plane_state, *new_plane_state;
13565 struct intel_plane *intel_plane = to_intel_plane(plane);
13566 struct drm_framebuffer *old_fb;
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013567 struct intel_crtc_state *crtc_state =
13568 to_intel_crtc_state(crtc->state);
13569 struct intel_crtc_state *new_crtc_state;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013570
13571 /*
13572 * When crtc is inactive or there is a modeset pending,
13573 * wait for it to complete in the slowpath
13574 */
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013575 if (!crtc_state->base.active || needs_modeset(&crtc_state->base) ||
13576 crtc_state->update_pipe)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013577 goto slow;
13578
13579 old_plane_state = plane->state;
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013580 /*
13581 * Don't do an async update if there is an outstanding commit modifying
13582 * the plane. This prevents our async update's changes from getting
13583 * overridden by a previous synchronous update's state.
13584 */
13585 if (old_plane_state->commit &&
13586 !try_wait_for_completion(&old_plane_state->commit->hw_done))
13587 goto slow;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013588
13589 /*
13590 * If any parameters change that may affect watermarks,
13591 * take the slowpath. Only changing fb or position should be
13592 * in the fastpath.
13593 */
13594 if (old_plane_state->crtc != crtc ||
13595 old_plane_state->src_w != src_w ||
13596 old_plane_state->src_h != src_h ||
13597 old_plane_state->crtc_w != crtc_w ||
13598 old_plane_state->crtc_h != crtc_h ||
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013599 !old_plane_state->fb != !fb)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013600 goto slow;
13601
13602 new_plane_state = intel_plane_duplicate_state(plane);
13603 if (!new_plane_state)
13604 return -ENOMEM;
13605
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013606 new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(crtc));
13607 if (!new_crtc_state) {
13608 ret = -ENOMEM;
13609 goto out_free;
13610 }
13611
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013612 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13613
13614 new_plane_state->src_x = src_x;
13615 new_plane_state->src_y = src_y;
13616 new_plane_state->src_w = src_w;
13617 new_plane_state->src_h = src_h;
13618 new_plane_state->crtc_x = crtc_x;
13619 new_plane_state->crtc_y = crtc_y;
13620 new_plane_state->crtc_w = crtc_w;
13621 new_plane_state->crtc_h = crtc_h;
13622
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013623 ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
13624 to_intel_plane_state(old_plane_state),
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013625 to_intel_plane_state(new_plane_state));
13626 if (ret)
13627 goto out_free;
13628
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013629 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13630 if (ret)
13631 goto out_free;
13632
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013633 ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
13634 if (ret)
13635 goto out_unlock;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013636
Dhinakaran Pandiyana694e222018-03-06 19:34:19 -080013637 intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013638
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080013639 old_fb = old_plane_state->fb;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013640 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13641 intel_plane->frontbuffer_bit);
13642
13643 /* Swap plane state */
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013644 plane->state = new_plane_state;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013645
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013646 /*
13647 * We cannot swap crtc_state as it may be in use by an atomic commit or
13648 * page flip that's running simultaneously. If we swap crtc_state and
13649 * destroy the old state, we will cause a use-after-free there.
13650 *
13651 * Only update active_planes, which is needed for our internal
13652 * bookkeeping. Either value will do the right thing when updating
13653 * planes atomically. If the cursor was part of the atomic update then
13654 * we would have taken the slowpath.
13655 */
13656 crtc_state->active_planes = new_crtc_state->active_planes;
13657
Ville Syrjälä72259532017-03-02 19:15:05 +020013658 if (plane->state->visible) {
13659 trace_intel_update_plane(plane, to_intel_crtc(crtc));
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013660 intel_plane->update_plane(intel_plane, crtc_state,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013661 to_intel_plane_state(plane->state));
Ville Syrjälä72259532017-03-02 19:15:05 +020013662 } else {
13663 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013664 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
Ville Syrjälä72259532017-03-02 19:15:05 +020013665 }
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013666
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013667 intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013668
13669out_unlock:
13670 mutex_unlock(&dev_priv->drm.struct_mutex);
13671out_free:
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013672 if (new_crtc_state)
13673 intel_crtc_destroy_state(crtc, &new_crtc_state->base);
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013674 if (ret)
13675 intel_plane_destroy_state(plane, new_plane_state);
13676 else
13677 intel_plane_destroy_state(plane, old_plane_state);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013678 return ret;
13679
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013680slow:
13681 return drm_atomic_helper_update_plane(plane, crtc, fb,
13682 crtc_x, crtc_y, crtc_w, crtc_h,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013683 src_x, src_y, src_w, src_h, ctx);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013684}
13685
13686static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13687 .update_plane = intel_legacy_cursor_update,
13688 .disable_plane = drm_atomic_helper_disable_plane,
13689 .destroy = intel_plane_destroy,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013690 .atomic_get_property = intel_plane_atomic_get_property,
13691 .atomic_set_property = intel_plane_atomic_set_property,
13692 .atomic_duplicate_state = intel_plane_duplicate_state,
13693 .atomic_destroy_state = intel_plane_destroy_state,
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013694 .format_mod_supported = intel_cursor_format_mod_supported,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013695};
13696
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013697static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
13698 enum i9xx_plane_id i9xx_plane)
13699{
13700 if (!HAS_FBC(dev_priv))
13701 return false;
13702
13703 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
13704 return i9xx_plane == PLANE_A; /* tied to pipe A */
13705 else if (IS_IVYBRIDGE(dev_priv))
13706 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
13707 i9xx_plane == PLANE_C;
13708 else if (INTEL_GEN(dev_priv) >= 4)
13709 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
13710 else
13711 return i9xx_plane == PLANE_A;
13712}
13713
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013714static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020013715intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070013716{
Ville Syrjälä881440a2018-10-05 15:58:17 +030013717 struct intel_plane *plane;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013718 const struct drm_plane_funcs *plane_funcs;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013719 unsigned int supported_rotations;
Ville Syrjälädeb19682018-10-05 15:58:08 +030013720 unsigned int possible_crtcs;
Ville Syrjälä881440a2018-10-05 15:58:17 +030013721 const u64 *modifiers;
13722 const u32 *formats;
13723 int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013724 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013725
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013726 if (INTEL_GEN(dev_priv) >= 9)
13727 return skl_universal_plane_create(dev_priv, pipe,
13728 PLANE_PRIMARY);
13729
Ville Syrjälä881440a2018-10-05 15:58:17 +030013730 plane = intel_plane_alloc();
13731 if (IS_ERR(plane))
13732 return plane;
Matt Roperea2c67b2014-12-23 10:41:52 -080013733
Ville Syrjälä881440a2018-10-05 15:58:17 +030013734 plane->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013735 /*
13736 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13737 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13738 */
13739 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
Ville Syrjälä881440a2018-10-05 15:58:17 +030013740 plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013741 else
Ville Syrjälä881440a2018-10-05 15:58:17 +030013742 plane->i9xx_plane = (enum i9xx_plane_id) pipe;
13743 plane->id = PLANE_PRIMARY;
13744 plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013745
Ville Syrjälä881440a2018-10-05 15:58:17 +030013746 plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
13747 if (plane->has_fbc) {
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013748 struct intel_fbc *fbc = &dev_priv->fbc;
13749
Ville Syrjälä881440a2018-10-05 15:58:17 +030013750 fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013751 }
13752
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013753 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä881440a2018-10-05 15:58:17 +030013754 formats = i965_primary_formats;
Damien Lespiau568db4f2015-05-12 16:13:18 +010013755 num_formats = ARRAY_SIZE(i965_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013756 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013757
Ville Syrjälä881440a2018-10-05 15:58:17 +030013758 plane->max_stride = i9xx_plane_max_stride;
13759 plane->update_plane = i9xx_update_plane;
13760 plane->disable_plane = i9xx_disable_plane;
13761 plane->get_hw_state = i9xx_plane_get_hw_state;
13762 plane->check_plane = i9xx_plane_check;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013763
13764 plane_funcs = &i965_plane_funcs;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013765 } else {
Ville Syrjälä881440a2018-10-05 15:58:17 +030013766 formats = i8xx_primary_formats;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013767 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013768 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013769
Ville Syrjälä881440a2018-10-05 15:58:17 +030013770 plane->max_stride = i9xx_plane_max_stride;
13771 plane->update_plane = i9xx_update_plane;
13772 plane->disable_plane = i9xx_disable_plane;
13773 plane->get_hw_state = i9xx_plane_get_hw_state;
13774 plane->check_plane = i9xx_plane_check;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013775
13776 plane_funcs = &i8xx_plane_funcs;
Matt Roper465c1202014-05-29 08:06:54 -070013777 }
13778
Ville Syrjälädeb19682018-10-05 15:58:08 +030013779 possible_crtcs = BIT(pipe);
13780
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013781 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä881440a2018-10-05 15:58:17 +030013782 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013783 possible_crtcs, plane_funcs,
Ville Syrjälä881440a2018-10-05 15:58:17 +030013784 formats, num_formats, modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013785 DRM_PLANE_TYPE_PRIMARY,
13786 "primary %c", pipe_name(pipe));
13787 else
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,
Ville Syrjäläed150302017-11-17 21:19:10 +020013792 "plane %c",
Ville Syrjälä881440a2018-10-05 15:58:17 +030013793 plane_name(plane->i9xx_plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013794 if (ret)
13795 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053013796
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013797 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020013798 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013799 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13800 DRM_MODE_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100013801 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013802 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013803 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013804 } else {
Robert Fossc2c446a2017-05-19 16:50:17 -040013805 supported_rotations = DRM_MODE_ROTATE_0;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013806 }
13807
Dave Airlie5481e272016-10-25 16:36:13 +100013808 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä881440a2018-10-05 15:58:17 +030013809 drm_plane_create_rotation_property(&plane->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013810 DRM_MODE_ROTATE_0,
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013811 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053013812
Ville Syrjälä881440a2018-10-05 15:58:17 +030013813 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
Matt Roperea2c67b2014-12-23 10:41:52 -080013814
Ville Syrjälä881440a2018-10-05 15:58:17 +030013815 return plane;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013816
13817fail:
Ville Syrjälä881440a2018-10-05 15:58:17 +030013818 intel_plane_free(plane);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013819
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013820 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070013821}
13822
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013823static struct intel_plane *
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013824intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13825 enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070013826{
Ville Syrjälädeb19682018-10-05 15:58:08 +030013827 unsigned int possible_crtcs;
Ville Syrjäläc539b572018-10-05 15:58:14 +030013828 struct intel_plane *cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013829 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070013830
Ville Syrjäläc539b572018-10-05 15:58:14 +030013831 cursor = intel_plane_alloc();
13832 if (IS_ERR(cursor))
13833 return cursor;
Matt Roperea2c67b2014-12-23 10:41:52 -080013834
Matt Roper3d7d6512014-06-10 08:28:13 -070013835 cursor->pipe = pipe;
Ville Syrjäläed150302017-11-17 21:19:10 +020013836 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013837 cursor->id = PLANE_CURSOR;
Ville Syrjäläc19e1122018-01-23 20:33:43 +020013838 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013839
13840 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjäläddd57132018-09-07 18:24:02 +030013841 cursor->max_stride = i845_cursor_max_stride;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013842 cursor->update_plane = i845_update_cursor;
13843 cursor->disable_plane = i845_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013844 cursor->get_hw_state = i845_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013845 cursor->check_plane = i845_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013846 } else {
Ville Syrjäläddd57132018-09-07 18:24:02 +030013847 cursor->max_stride = i9xx_cursor_max_stride;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013848 cursor->update_plane = i9xx_update_cursor;
13849 cursor->disable_plane = i9xx_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013850 cursor->get_hw_state = i9xx_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013851 cursor->check_plane = i9xx_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013852 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013853
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030013854 cursor->cursor.base = ~0;
13855 cursor->cursor.cntl = ~0;
Ville Syrjälä024faac2017-03-27 21:55:42 +030013856
13857 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13858 cursor->cursor.size = ~0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013859
Ville Syrjälädeb19682018-10-05 15:58:08 +030013860 possible_crtcs = BIT(pipe);
13861
Ville Syrjälä580503c2016-10-31 22:37:00 +020013862 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013863 possible_crtcs, &intel_cursor_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013864 intel_cursor_formats,
13865 ARRAY_SIZE(intel_cursor_formats),
Ben Widawsky714244e2017-08-01 09:58:16 -070013866 cursor_format_modifiers,
13867 DRM_PLANE_TYPE_CURSOR,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013868 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013869 if (ret)
13870 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013871
Dave Airlie5481e272016-10-25 16:36:13 +100013872 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013873 drm_plane_create_rotation_property(&cursor->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013874 DRM_MODE_ROTATE_0,
13875 DRM_MODE_ROTATE_0 |
13876 DRM_MODE_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013877
Matt Roperea2c67b2014-12-23 10:41:52 -080013878 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13879
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013880 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013881
13882fail:
Ville Syrjäläc539b572018-10-05 15:58:14 +030013883 intel_plane_free(cursor);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013884
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013885 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070013886}
13887
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013888static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13889 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013890{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013891 struct intel_crtc_scaler_state *scaler_state =
13892 &crtc_state->scaler_state;
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013893 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013894 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013895
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013896 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13897 if (!crtc->num_scalers)
13898 return;
13899
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013900 for (i = 0; i < crtc->num_scalers; i++) {
13901 struct intel_scaler *scaler = &scaler_state->scalers[i];
13902
13903 scaler->in_use = 0;
Maarten Lankhorst0aaf29b2018-09-21 16:44:37 +020013904 scaler->mode = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013905 }
13906
13907 scaler_state->scaler_id = -1;
13908}
13909
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013910static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013911{
13912 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013913 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013914 struct intel_plane *primary = NULL;
13915 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013916 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013917
Daniel Vetter955382f2013-09-19 14:05:45 +020013918 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013919 if (!intel_crtc)
13920 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080013921
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013922 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013923 if (!crtc_state) {
13924 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013925 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013926 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013927 intel_crtc->config = crtc_state;
13928 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013929 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013930
Ville Syrjälä580503c2016-10-31 22:37:00 +020013931 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013932 if (IS_ERR(primary)) {
13933 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070013934 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013935 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013936 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013937
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013938 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013939 struct intel_plane *plane;
13940
Ville Syrjälä580503c2016-10-31 22:37:00 +020013941 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013942 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013943 ret = PTR_ERR(plane);
13944 goto fail;
13945 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013946 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013947 }
13948
Ville Syrjälä580503c2016-10-31 22:37:00 +020013949 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013950 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013951 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070013952 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013953 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013954 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013955
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013956 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013957 &primary->base, &cursor->base,
13958 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030013959 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070013960 if (ret)
13961 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013962
Jesse Barnes80824002009-09-10 15:28:06 -070013963 intel_crtc->pipe = pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013964
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013965 /* initialize shared scalers */
13966 intel_crtc_init_scalers(intel_crtc, crtc_state);
13967
Ville Syrjälä1947fd12018-03-05 19:41:22 +020013968 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
13969 dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
13970 dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
13971
13972 if (INTEL_GEN(dev_priv) < 9) {
13973 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
13974
13975 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13976 dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
13977 dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
13978 }
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013979
Jesse Barnes79e53942008-11-07 14:24:08 -080013980 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013981
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013982 intel_color_init(&intel_crtc->base);
13983
Daniel Vetter87b6b102014-05-15 15:33:46 +020013984 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013985
13986 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013987
13988fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013989 /*
13990 * drm_mode_config_cleanup() will free up any
13991 * crtcs/planes already initialized.
13992 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013993 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013994 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013995
13996 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013997}
13998
Ville Syrjälä6a20fe72018-02-07 18:48:41 +020013999int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
14000 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014001{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014002 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014003 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014004 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014005
Keith Packard418da172017-03-14 23:25:07 -070014006 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014007 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014008 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014009
Rob Clark7707e652014-07-17 23:30:04 -040014010 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014011 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014012
Daniel Vetterc05422d2009-08-11 16:05:30 +020014013 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014014}
14015
Daniel Vetter66a92782012-07-12 20:08:18 +020014016static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014017{
Daniel Vetter66a92782012-07-12 20:08:18 +020014018 struct drm_device *dev = encoder->base.dev;
14019 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014020 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014021 int entry = 0;
14022
Damien Lespiaub2784e12014-08-05 11:29:37 +010014023 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014024 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014025 index_mask |= (1 << entry);
14026
Jesse Barnes79e53942008-11-07 14:24:08 -080014027 entry++;
14028 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014029
Jesse Barnes79e53942008-11-07 14:24:08 -080014030 return index_mask;
14031}
14032
Ville Syrjälä646d5772016-10-31 22:37:14 +020014033static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000014034{
Ville Syrjälä646d5772016-10-31 22:37:14 +020014035 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000014036 return false;
14037
14038 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14039 return false;
14040
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014041 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014042 return false;
14043
14044 return true;
14045}
14046
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014047static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014048{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014049 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000014050 return false;
14051
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014052 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014053 return false;
14054
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014055 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014056 return false;
14057
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014058 if (HAS_PCH_LPT_H(dev_priv) &&
14059 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014060 return false;
14061
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014062 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014063 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 +020014064 return false;
14065
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014066 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014067 return false;
14068
14069 return true;
14070}
14071
Imre Deak8090ba82016-08-10 14:07:33 +030014072void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14073{
14074 int pps_num;
14075 int pps_idx;
14076
14077 if (HAS_DDI(dev_priv))
14078 return;
14079 /*
14080 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14081 * everywhere where registers can be write protected.
14082 */
14083 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14084 pps_num = 2;
14085 else
14086 pps_num = 1;
14087
14088 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14089 u32 val = I915_READ(PP_CONTROL(pps_idx));
14090
14091 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14092 I915_WRITE(PP_CONTROL(pps_idx), val);
14093 }
14094}
14095
Imre Deak44cb7342016-08-10 14:07:29 +030014096static void intel_pps_init(struct drm_i915_private *dev_priv)
14097{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020014098 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
Imre Deak44cb7342016-08-10 14:07:29 +030014099 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14100 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14101 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14102 else
14103 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030014104
14105 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030014106}
14107
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014108static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080014109{
Chris Wilson4ef69c72010-09-09 15:14:28 +010014110 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014111 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014112
Imre Deak44cb7342016-08-10 14:07:29 +030014113 intel_pps_init(dev_priv);
14114
Chris Wilsonfc0c5a92018-08-15 21:12:07 +010014115 if (INTEL_INFO(dev_priv)->num_pipes == 0)
14116 return;
14117
Imre Deak97a824e12016-06-21 11:51:47 +030014118 /*
14119 * intel_edp_init_connector() depends on this completing first, to
14120 * prevent the registeration of both eDP and LVDS and the incorrect
14121 * sharing of the PPS.
14122 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014123 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014124
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014125 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014126 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014127
Paulo Zanoni00c92d92018-05-21 17:25:47 -070014128 if (IS_ICELAKE(dev_priv)) {
14129 intel_ddi_init(dev_priv, PORT_A);
14130 intel_ddi_init(dev_priv, PORT_B);
14131 intel_ddi_init(dev_priv, PORT_C);
14132 intel_ddi_init(dev_priv, PORT_D);
14133 intel_ddi_init(dev_priv, PORT_E);
14134 intel_ddi_init(dev_priv, PORT_F);
Madhav Chauhanbf4d57f2018-10-30 13:56:23 +020014135 icl_dsi_init(dev_priv);
Paulo Zanoni00c92d92018-05-21 17:25:47 -070014136 } else if (IS_GEN9_LP(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053014137 /*
14138 * FIXME: Broxton doesn't support port detection via the
14139 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14140 * detect the ports.
14141 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014142 intel_ddi_init(dev_priv, PORT_A);
14143 intel_ddi_init(dev_priv, PORT_B);
14144 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014145
Jani Nikulae5186342018-07-05 16:25:08 +030014146 vlv_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014147 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014148 int found;
14149
Jesse Barnesde31fac2015-03-06 15:53:32 -080014150 /*
14151 * Haswell uses DDI functions to detect digital outputs.
14152 * On SKL pre-D0 the strap isn't connected, so we assume
14153 * it's there.
14154 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014155 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014156 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014157 if (found || IS_GEN9_BC(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014158 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014159
Rodrigo Vivi9787e832018-01-29 15:22:22 -080014160 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014161 * register */
14162 found = I915_READ(SFUSE_STRAP);
14163
14164 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014165 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014166 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014167 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014168 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014169 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi9787e832018-01-29 15:22:22 -080014170 if (found & SFUSE_STRAP_DDIF_DETECTED)
14171 intel_ddi_init(dev_priv, PORT_F);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014172 /*
14173 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14174 */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014175 if (IS_GEN9_BC(dev_priv) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014176 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14177 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14178 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014179 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014180
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014181 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014182 int found;
Jani Nikula7b91bf72017-08-18 12:30:19 +030014183 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014184
Ville Syrjälä646d5772016-10-31 22:37:14 +020014185 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014186 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014187
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014188 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014189 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014190 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014191 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014192 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014193 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014194 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014195 }
14196
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014197 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014198 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014199
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014200 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014201 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014202
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014203 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014204 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014205
Daniel Vetter270b3042012-10-27 15:52:05 +020014206 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014207 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014208 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014209 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014210
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014211 /*
14212 * The DP_DETECTED bit is the latched state of the DDC
14213 * SDA pin at boot. However since eDP doesn't require DDC
14214 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14215 * eDP ports may have been muxed to an alternate function.
14216 * Thus we can't rely on the DP_DETECTED bit alone to detect
14217 * eDP ports. Consult the VBT as well as DP_DETECTED to
14218 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014219 *
14220 * Sadly the straps seem to be missing sometimes even for HDMI
14221 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14222 * and VBT for the presence of the port. Additionally we can't
14223 * trust the port type the VBT declares as we've seen at least
14224 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014225 */
Jani Nikula7b91bf72017-08-18 12:30:19 +030014226 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014227 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14228 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014229 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014230 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014231 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014232
Jani Nikula7b91bf72017-08-18 12:30:19 +030014233 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014234 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14235 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014236 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014237 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014238 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014239
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014240 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014241 /*
14242 * eDP not supported on port D,
14243 * so no need to worry about it
14244 */
14245 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14246 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014247 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014248 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014249 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014250 }
14251
Jani Nikulae5186342018-07-05 16:25:08 +030014252 vlv_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014253 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014254 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014255
Paulo Zanonie2debe92013-02-18 19:00:27 -030014256 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014257 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014258 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014259 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014260 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014261 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014262 }
Ma Ling27185ae2009-08-24 13:50:23 +080014263
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014264 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014265 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014266 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014267
14268 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014269
Paulo Zanonie2debe92013-02-18 19:00:27 -030014270 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014271 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014272 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014273 }
Ma Ling27185ae2009-08-24 13:50:23 +080014274
Paulo Zanonie2debe92013-02-18 19:00:27 -030014275 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014276
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014277 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014278 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014279 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014280 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014281 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014282 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014283 }
Ma Ling27185ae2009-08-24 13:50:23 +080014284
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014285 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014286 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014287 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014288 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014289
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000014290 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014291 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014292
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014293 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014294
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014295 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014296 encoder->base.possible_crtcs = encoder->crtc_mask;
14297 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014298 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014299 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014300
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014301 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020014302
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014303 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080014304}
14305
14306static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14307{
14308 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014309 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014310
Daniel Vetteref2d6332014-02-10 18:00:38 +010014311 drm_framebuffer_cleanup(fb);
Chris Wilson70001cd2017-02-16 09:46:21 +000014312
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014313 i915_gem_object_lock(obj);
14314 WARN_ON(!obj->framebuffer_references--);
14315 i915_gem_object_unlock(obj);
Chris Wilsondd689282017-03-01 15:41:28 +000014316
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014317 i915_gem_object_put(obj);
Chris Wilson70001cd2017-02-16 09:46:21 +000014318
Jesse Barnes79e53942008-11-07 14:24:08 -080014319 kfree(intel_fb);
14320}
14321
14322static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014323 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014324 unsigned int *handle)
14325{
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014326 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014327
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014328 if (obj->userptr.mm) {
14329 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14330 return -EINVAL;
14331 }
14332
Chris Wilson05394f32010-11-08 19:18:58 +000014333 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014334}
14335
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014336static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14337 struct drm_file *file,
14338 unsigned flags, unsigned color,
14339 struct drm_clip_rect *clips,
14340 unsigned num_clips)
14341{
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014342 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014343
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014344 i915_gem_object_flush_if_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +000014345 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014346
14347 return 0;
14348}
14349
Jesse Barnes79e53942008-11-07 14:24:08 -080014350static const struct drm_framebuffer_funcs intel_fb_funcs = {
14351 .destroy = intel_user_framebuffer_destroy,
14352 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014353 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014354};
14355
Damien Lespiaub3218032015-02-27 11:15:18 +000014356static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014357u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
Dhinakaran Pandiyan4c8d3512018-10-26 12:53:42 -070014358 u32 pixel_format, u64 fb_modifier)
Damien Lespiaub3218032015-02-27 11:15:18 +000014359{
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014360 struct intel_crtc *crtc;
14361 struct intel_plane *plane;
Damien Lespiaub3218032015-02-27 11:15:18 +000014362
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014363 /*
14364 * We assume the primary plane for pipe A has
14365 * the highest stride limits of them all.
14366 */
14367 crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
14368 plane = to_intel_plane(crtc->base.primary);
Ville Syrjäläac484962016-01-20 21:05:26 +020014369
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014370 return plane->max_stride(plane, pixel_format, fb_modifier,
14371 DRM_MODE_ROTATE_0);
Damien Lespiaub3218032015-02-27 11:15:18 +000014372}
14373
Chris Wilson24dbf512017-02-15 10:59:18 +000014374static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14375 struct drm_i915_gem_object *obj,
14376 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014377{
Chris Wilson24dbf512017-02-15 10:59:18 +000014378 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014379 struct drm_framebuffer *fb = &intel_fb->base;
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014380 struct drm_format_name_buf format_name;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014381 u32 pitch_limit;
Chris Wilsondd689282017-03-01 15:41:28 +000014382 unsigned int tiling, stride;
Chris Wilson24dbf512017-02-15 10:59:18 +000014383 int ret = -EINVAL;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014384 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -080014385
Chris Wilsondd689282017-03-01 15:41:28 +000014386 i915_gem_object_lock(obj);
14387 obj->framebuffer_references++;
14388 tiling = i915_gem_object_get_tiling(obj);
14389 stride = i915_gem_object_get_stride(obj);
14390 i915_gem_object_unlock(obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014391
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014392 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014393 /*
14394 * If there's a fence, enforce that
14395 * the fb modifier and tiling mode match.
14396 */
14397 if (tiling != I915_TILING_NONE &&
14398 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014399 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014400 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014401 }
14402 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014403 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014404 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014405 } else if (tiling == I915_TILING_Y) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014406 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014407 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014408 }
14409 }
14410
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014411 /* Passed in modifier sanity checking. */
14412 switch (mode_cmd->modifier[0]) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014413 case I915_FORMAT_MOD_Y_TILED_CCS:
14414 case I915_FORMAT_MOD_Yf_TILED_CCS:
14415 switch (mode_cmd->pixel_format) {
14416 case DRM_FORMAT_XBGR8888:
14417 case DRM_FORMAT_ABGR8888:
14418 case DRM_FORMAT_XRGB8888:
14419 case DRM_FORMAT_ARGB8888:
14420 break;
14421 default:
14422 DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
14423 goto err;
14424 }
14425 /* fall through */
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014426 case I915_FORMAT_MOD_Yf_TILED:
Paulo Zanonief51e0a2018-09-24 17:19:11 -070014427 if (mode_cmd->pixel_format == DRM_FORMAT_C8) {
14428 DRM_DEBUG_KMS("Indexed format does not support Yf tiling\n");
14429 goto err;
14430 }
14431 /* fall through */
14432 case I915_FORMAT_MOD_Y_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014433 if (INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014434 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14435 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014436 goto err;
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014437 }
Paulo Zanonief51e0a2018-09-24 17:19:11 -070014438 break;
Ben Widawsky2f075562017-03-24 14:29:48 -070014439 case DRM_FORMAT_MOD_LINEAR:
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014440 case I915_FORMAT_MOD_X_TILED:
14441 break;
14442 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014443 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14444 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014445 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014446 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014447
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014448 /*
14449 * gen2/3 display engine uses the fence if present,
14450 * so the tiling mode must match the fb modifier exactly.
14451 */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014452 if (INTEL_GEN(dev_priv) < 4 &&
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014453 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014454 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014455 goto err;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014456 }
14457
Dhinakaran Pandiyan4c8d3512018-10-26 12:53:42 -070014458 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->pixel_format,
14459 mode_cmd->modifier[0]);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014460 if (mode_cmd->pitches[0] > pitch_limit) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014461 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
Ben Widawsky2f075562017-03-24 14:29:48 -070014462 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014463 "tiled" : "linear",
14464 mode_cmd->pitches[0], pitch_limit);
Chris Wilson24dbf512017-02-15 10:59:18 +000014465 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014466 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014467
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014468 /*
14469 * If there's a fence, enforce that
14470 * the fb pitch and fence stride match.
14471 */
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014472 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14473 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14474 mode_cmd->pitches[0], stride);
Chris Wilson24dbf512017-02-15 10:59:18 +000014475 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014476 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014477
Ville Syrjälä57779d02012-10-31 17:50:14 +020014478 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014479 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014480 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014481 case DRM_FORMAT_RGB565:
14482 case DRM_FORMAT_XRGB8888:
14483 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014484 break;
14485 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014486 if (INTEL_GEN(dev_priv) > 3) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014487 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14488 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014489 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014490 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014491 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014492 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014493 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014494 INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014495 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14496 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014497 goto err;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014498 }
14499 break;
14500 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014501 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014502 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014503 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014504 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14505 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014506 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014507 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014508 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014509 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014510 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014511 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14512 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014513 goto err;
Damien Lespiau75312082015-05-15 19:06:01 +010014514 }
14515 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014516 case DRM_FORMAT_YUYV:
14517 case DRM_FORMAT_UYVY:
14518 case DRM_FORMAT_YVYU:
14519 case DRM_FORMAT_VYUY:
Ville Syrjäläab330812017-04-21 21:14:32 +030014520 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014521 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14522 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014523 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014524 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014525 break;
Chandra Kondurue44134f2018-05-12 03:03:15 +053014526 case DRM_FORMAT_NV12:
Chandra Kondurue44134f2018-05-12 03:03:15 +053014527 if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
Maarten Lankhorst26ee5bc2018-10-22 15:45:14 +020014528 IS_BROXTON(dev_priv)) {
Chandra Kondurue44134f2018-05-12 03:03:15 +053014529 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14530 drm_get_format_name(mode_cmd->pixel_format,
14531 &format_name));
14532 goto err;
14533 }
14534 break;
Chris Wilson57cd6502010-08-08 12:34:44 +010014535 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014536 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14537 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014538 goto err;
Chris Wilson57cd6502010-08-08 12:34:44 +010014539 }
14540
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014541 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14542 if (mode_cmd->offsets[0] != 0)
Chris Wilson24dbf512017-02-15 10:59:18 +000014543 goto err;
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014544
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014545 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014546
Chandra Kondurue44134f2018-05-12 03:03:15 +053014547 if (fb->format->format == DRM_FORMAT_NV12 &&
14548 (fb->width < SKL_MIN_YUV_420_SRC_W ||
14549 fb->height < SKL_MIN_YUV_420_SRC_H ||
14550 (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
14551 DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
Ville Syrjälä3b909462018-10-29 16:00:31 +020014552 goto err;
Chandra Kondurue44134f2018-05-12 03:03:15 +053014553 }
14554
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014555 for (i = 0; i < fb->format->num_planes; i++) {
14556 u32 stride_alignment;
14557
14558 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
14559 DRM_DEBUG_KMS("bad plane %d handle\n", i);
Christophe JAILLET37875d62017-09-10 10:56:42 +020014560 goto err;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014561 }
14562
14563 stride_alignment = intel_fb_stride_alignment(fb, i);
14564
14565 /*
14566 * Display WA #0531: skl,bxt,kbl,glk
14567 *
14568 * Render decompression and plane width > 3840
14569 * combined with horizontal panning requires the
14570 * plane stride to be a multiple of 4. We'll just
14571 * require the entire fb to accommodate that to avoid
14572 * potential runtime errors at plane configuration time.
14573 */
14574 if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -070014575 is_ccs_modifier(fb->modifier))
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014576 stride_alignment *= 4;
14577
14578 if (fb->pitches[i] & (stride_alignment - 1)) {
14579 DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
14580 i, fb->pitches[i], stride_alignment);
14581 goto err;
14582 }
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014583
Daniel Stonea268bcd2018-05-18 15:30:08 +010014584 fb->obj[i] = &obj->base;
14585 }
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014586
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014587 ret = intel_fill_fb_info(dev_priv, fb);
Ville Syrjälä6687c902015-09-15 13:16:41 +030014588 if (ret)
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014589 goto err;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014590
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014591 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080014592 if (ret) {
14593 DRM_ERROR("framebuffer init failed %d\n", ret);
Chris Wilson24dbf512017-02-15 10:59:18 +000014594 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -080014595 }
14596
Jesse Barnes79e53942008-11-07 14:24:08 -080014597 return 0;
Chris Wilson24dbf512017-02-15 10:59:18 +000014598
14599err:
Chris Wilsondd689282017-03-01 15:41:28 +000014600 i915_gem_object_lock(obj);
14601 obj->framebuffer_references--;
14602 i915_gem_object_unlock(obj);
Chris Wilson24dbf512017-02-15 10:59:18 +000014603 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014604}
14605
Jesse Barnes79e53942008-11-07 14:24:08 -080014606static struct drm_framebuffer *
14607intel_user_framebuffer_create(struct drm_device *dev,
14608 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014609 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014610{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014611 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014612 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014613 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014614
Chris Wilson03ac0642016-07-20 13:31:51 +010014615 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14616 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014617 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014618
Chris Wilson24dbf512017-02-15 10:59:18 +000014619 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014620 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010014621 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014622
14623 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014624}
14625
Chris Wilson778e23a2016-12-05 14:29:39 +000014626static void intel_atomic_state_free(struct drm_atomic_state *state)
14627{
14628 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14629
14630 drm_atomic_state_default_release(state);
14631
14632 i915_sw_fence_fini(&intel_state->commit_ready);
14633
14634 kfree(state);
14635}
14636
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014637static enum drm_mode_status
14638intel_mode_valid(struct drm_device *dev,
14639 const struct drm_display_mode *mode)
14640{
Ville Syrjäläad77c532018-06-15 20:44:05 +030014641 struct drm_i915_private *dev_priv = to_i915(dev);
14642 int hdisplay_max, htotal_max;
14643 int vdisplay_max, vtotal_max;
14644
Ville Syrjäläe4dd27a2018-05-24 15:54:03 +030014645 /*
14646 * Can't reject DBLSCAN here because Xorg ddxen can add piles
14647 * of DBLSCAN modes to the output's mode list when they detect
14648 * the scaling mode property on the connector. And they don't
14649 * ask the kernel to validate those modes in any way until
14650 * modeset time at which point the client gets a protocol error.
14651 * So in order to not upset those clients we silently ignore the
14652 * DBLSCAN flag on such connectors. For other connectors we will
14653 * reject modes with the DBLSCAN flag in encoder->compute_config().
14654 * And we always reject DBLSCAN modes in connector->mode_valid()
14655 * as we never want such modes on the connector's mode list.
14656 */
14657
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014658 if (mode->vscan > 1)
14659 return MODE_NO_VSCAN;
14660
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014661 if (mode->flags & DRM_MODE_FLAG_HSKEW)
14662 return MODE_H_ILLEGAL;
14663
14664 if (mode->flags & (DRM_MODE_FLAG_CSYNC |
14665 DRM_MODE_FLAG_NCSYNC |
14666 DRM_MODE_FLAG_PCSYNC))
14667 return MODE_HSYNC;
14668
14669 if (mode->flags & (DRM_MODE_FLAG_BCAST |
14670 DRM_MODE_FLAG_PIXMUX |
14671 DRM_MODE_FLAG_CLKDIV2))
14672 return MODE_BAD;
14673
Ville Syrjäläad77c532018-06-15 20:44:05 +030014674 if (INTEL_GEN(dev_priv) >= 9 ||
14675 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
14676 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
14677 vdisplay_max = 4096;
14678 htotal_max = 8192;
14679 vtotal_max = 8192;
14680 } else if (INTEL_GEN(dev_priv) >= 3) {
14681 hdisplay_max = 4096;
14682 vdisplay_max = 4096;
14683 htotal_max = 8192;
14684 vtotal_max = 8192;
14685 } else {
14686 hdisplay_max = 2048;
14687 vdisplay_max = 2048;
14688 htotal_max = 4096;
14689 vtotal_max = 4096;
14690 }
14691
14692 if (mode->hdisplay > hdisplay_max ||
14693 mode->hsync_start > htotal_max ||
14694 mode->hsync_end > htotal_max ||
14695 mode->htotal > htotal_max)
14696 return MODE_H_ILLEGAL;
14697
14698 if (mode->vdisplay > vdisplay_max ||
14699 mode->vsync_start > vtotal_max ||
14700 mode->vsync_end > vtotal_max ||
14701 mode->vtotal > vtotal_max)
14702 return MODE_V_ILLEGAL;
14703
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014704 return MODE_OK;
14705}
14706
Jesse Barnes79e53942008-11-07 14:24:08 -080014707static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014708 .fb_create = intel_user_framebuffer_create,
Ville Syrjäläbbfb6ce2017-08-01 09:58:12 -070014709 .get_format_info = intel_get_format_info,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014710 .output_poll_changed = intel_fbdev_output_poll_changed,
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014711 .mode_valid = intel_mode_valid,
Matt Roper5ee67f12015-01-21 16:35:44 -080014712 .atomic_check = intel_atomic_check,
14713 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014714 .atomic_state_alloc = intel_atomic_state_alloc,
14715 .atomic_state_clear = intel_atomic_state_clear,
Chris Wilson778e23a2016-12-05 14:29:39 +000014716 .atomic_state_free = intel_atomic_state_free,
Jesse Barnes79e53942008-11-07 14:24:08 -080014717};
14718
Imre Deak88212942016-03-16 13:38:53 +020014719/**
14720 * intel_init_display_hooks - initialize the display modesetting hooks
14721 * @dev_priv: device private
14722 */
14723void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014724{
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +020014725 intel_init_cdclk_hooks(dev_priv);
14726
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014727 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014728 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014729 dev_priv->display.get_initial_plane_config =
14730 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014731 dev_priv->display.crtc_compute_clock =
14732 haswell_crtc_compute_clock;
14733 dev_priv->display.crtc_enable = haswell_crtc_enable;
14734 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014735 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014736 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014737 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014738 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014739 dev_priv->display.crtc_compute_clock =
14740 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014741 dev_priv->display.crtc_enable = haswell_crtc_enable;
14742 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014743 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014744 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014745 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014746 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014747 dev_priv->display.crtc_compute_clock =
14748 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014749 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14750 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014751 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014752 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014753 dev_priv->display.get_initial_plane_config =
14754 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014755 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14756 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14757 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14758 } else if (IS_VALLEYVIEW(dev_priv)) {
14759 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14760 dev_priv->display.get_initial_plane_config =
14761 i9xx_get_initial_plane_config;
14762 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014763 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14764 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014765 } else if (IS_G4X(dev_priv)) {
14766 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14767 dev_priv->display.get_initial_plane_config =
14768 i9xx_get_initial_plane_config;
14769 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14770 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14771 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014772 } else if (IS_PINEVIEW(dev_priv)) {
14773 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14774 dev_priv->display.get_initial_plane_config =
14775 i9xx_get_initial_plane_config;
14776 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14777 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14778 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014779 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014780 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014781 dev_priv->display.get_initial_plane_config =
14782 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014783 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014784 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14785 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014786 } else {
14787 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14788 dev_priv->display.get_initial_plane_config =
14789 i9xx_get_initial_plane_config;
14790 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14791 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14792 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014793 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014794
Imre Deak88212942016-03-16 13:38:53 +020014795 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014796 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014797 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014798 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014799 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014800 /* FIXME: detect B0+ stepping and use auto training */
14801 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014802 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014803 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014804 }
14805
Rodrigo Vivibd30ca22017-09-26 14:13:46 -070014806 if (INTEL_GEN(dev_priv) >= 9)
Lyude27082492016-08-24 07:48:10 +020014807 dev_priv->display.update_crtcs = skl_update_crtcs;
14808 else
14809 dev_priv->display.update_crtcs = intel_update_crtcs;
Jesse Barnese70236a2009-09-21 10:42:27 -070014810}
14811
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014812/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014813static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014814{
David Weinehall52a05c32016-08-22 13:32:44 +030014815 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014816 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014817 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014818
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014819 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030014820 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014821 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014822 sr1 = inb(VGA_SR_DATA);
14823 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030014824 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014825 udelay(300);
14826
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014827 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014828 POSTING_READ(vga_reg);
14829}
14830
Daniel Vetterf8175862012-04-10 15:50:11 +020014831void intel_modeset_init_hw(struct drm_device *dev)
14832{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014833 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014834
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014835 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +030014836 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020014837 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
Daniel Vetterf8175862012-04-10 15:50:11 +020014838}
14839
Matt Roperd93c0372015-12-03 11:37:41 -080014840/*
14841 * Calculate what we think the watermarks should be for the state we've read
14842 * out of the hardware and then immediately program those watermarks so that
14843 * we ensure the hardware settings match our internal state.
14844 *
14845 * We can calculate what we think WM's should be by creating a duplicate of the
14846 * current state (which was constructed during hardware readout) and running it
14847 * through the atomic check code to calculate new watermark values in the
14848 * state object.
14849 */
14850static void sanitize_watermarks(struct drm_device *dev)
14851{
14852 struct drm_i915_private *dev_priv = to_i915(dev);
14853 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014854 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014855 struct drm_crtc *crtc;
14856 struct drm_crtc_state *cstate;
14857 struct drm_modeset_acquire_ctx ctx;
14858 int ret;
14859 int i;
14860
14861 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080014862 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080014863 return;
14864
14865 /*
14866 * We need to hold connection_mutex before calling duplicate_state so
14867 * that the connector loop is protected.
14868 */
14869 drm_modeset_acquire_init(&ctx, 0);
14870retry:
Matt Roper0cd12622016-01-12 07:13:37 -080014871 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080014872 if (ret == -EDEADLK) {
14873 drm_modeset_backoff(&ctx);
14874 goto retry;
14875 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080014876 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014877 }
14878
14879 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14880 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080014881 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014882
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014883 intel_state = to_intel_atomic_state(state);
14884
Matt Ropered4a6a72016-02-23 17:20:13 -080014885 /*
14886 * Hardware readout is the only time we don't want to calculate
14887 * intermediate watermarks (since we don't trust the current
14888 * watermarks).
14889 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020014890 if (!HAS_GMCH_DISPLAY(dev_priv))
14891 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080014892
Matt Roperd93c0372015-12-03 11:37:41 -080014893 ret = intel_atomic_check(dev, state);
14894 if (ret) {
14895 /*
14896 * If we fail here, it means that the hardware appears to be
14897 * programmed in a way that shouldn't be possible, given our
14898 * understanding of watermark requirements. This might mean a
14899 * mistake in the hardware readout code or a mistake in the
14900 * watermark calculations for a given platform. Raise a WARN
14901 * so that this is noticeable.
14902 *
14903 * If this actually happens, we'll have to just leave the
14904 * BIOS-programmed watermarks untouched and hope for the best.
14905 */
14906 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014907 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014908 }
14909
14910 /* Write calculated watermark values back */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010014911 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roperd93c0372015-12-03 11:37:41 -080014912 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14913
Matt Ropered4a6a72016-02-23 17:20:13 -080014914 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014915 dev_priv->display.optimize_watermarks(intel_state, cs);
Maarten Lankhorst556fe362017-11-10 12:34:53 +010014916
14917 to_intel_crtc_state(crtc->state)->wm = cs->wm;
Matt Roperd93c0372015-12-03 11:37:41 -080014918 }
14919
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014920put_state:
Chris Wilson08536952016-10-14 13:18:18 +010014921 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080014922fail:
Matt Roperd93c0372015-12-03 11:37:41 -080014923 drm_modeset_drop_locks(&ctx);
14924 drm_modeset_acquire_fini(&ctx);
14925}
14926
Chris Wilson58ecd9d2017-11-05 13:49:05 +000014927static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
14928{
14929 if (IS_GEN5(dev_priv)) {
14930 u32 fdi_pll_clk =
14931 I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
14932
14933 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
14934 } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
14935 dev_priv->fdi_pll_freq = 270000;
14936 } else {
14937 return;
14938 }
14939
14940 DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
14941}
14942
Azhar Shaikh516a49c2018-07-06 11:37:30 -070014943static int intel_initial_commit(struct drm_device *dev)
14944{
14945 struct drm_atomic_state *state = NULL;
14946 struct drm_modeset_acquire_ctx ctx;
14947 struct drm_crtc *crtc;
14948 struct drm_crtc_state *crtc_state;
14949 int ret = 0;
14950
14951 state = drm_atomic_state_alloc(dev);
14952 if (!state)
14953 return -ENOMEM;
14954
14955 drm_modeset_acquire_init(&ctx, 0);
14956
14957retry:
14958 state->acquire_ctx = &ctx;
14959
14960 drm_for_each_crtc(crtc, dev) {
14961 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14962 if (IS_ERR(crtc_state)) {
14963 ret = PTR_ERR(crtc_state);
14964 goto out;
14965 }
14966
14967 if (crtc_state->active) {
14968 ret = drm_atomic_add_affected_planes(state, crtc);
14969 if (ret)
14970 goto out;
14971 }
14972 }
14973
14974 ret = drm_atomic_commit(state);
14975
14976out:
14977 if (ret == -EDEADLK) {
14978 drm_atomic_state_clear(state);
14979 drm_modeset_backoff(&ctx);
14980 goto retry;
14981 }
14982
14983 drm_atomic_state_put(state);
14984
14985 drm_modeset_drop_locks(&ctx);
14986 drm_modeset_acquire_fini(&ctx);
14987
14988 return ret;
14989}
14990
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014991int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080014992{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030014993 struct drm_i915_private *dev_priv = to_i915(dev);
14994 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014995 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014996 struct intel_crtc *crtc;
Azhar Shaikh516a49c2018-07-06 11:37:30 -070014997 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014998
Ville Syrjälä757fffc2017-11-13 15:36:22 +020014999 dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
15000
Jesse Barnes79e53942008-11-07 14:24:08 -080015001 drm_mode_config_init(dev);
15002
15003 dev->mode_config.min_width = 0;
15004 dev->mode_config.min_height = 0;
15005
Dave Airlie019d96c2011-09-29 16:20:42 +010015006 dev->mode_config.preferred_depth = 24;
15007 dev->mode_config.prefer_shadow = 1;
15008
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015009 dev->mode_config.allow_fb_modifiers = true;
15010
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015011 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015012
Andrea Arcangeli400c19d2017-04-07 01:23:45 +020015013 init_llist_head(&dev_priv->atomic_helper.free_list);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015014 INIT_WORK(&dev_priv->atomic_helper.free_work,
Chris Wilsonba318c62017-02-02 20:47:41 +000015015 intel_atomic_helper_free_state_worker);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015016
Jani Nikula27a981b2018-10-17 12:35:39 +030015017 intel_init_quirks(dev_priv);
Jesse Barnesb690e962010-07-19 13:53:12 -070015018
José Roberto de Souzaacde44b2018-11-07 16:16:45 -080015019 intel_fbc_init(dev_priv);
15020
Ville Syrjälä62d75df2016-10-31 22:37:25 +020015021 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015022
Lukas Wunner69f92f62015-07-15 13:57:35 +020015023 /*
15024 * There may be no VBT; and if the BIOS enabled SSC we can
15025 * just keep using it to avoid unnecessary flicker. Whereas if the
15026 * BIOS isn't using it, don't assume it will work even if the VBT
15027 * indicates as much.
15028 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015029 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020015030 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15031 DREF_SSC1_ENABLE);
15032
15033 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15034 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15035 bios_lvds_use_ssc ? "en" : "dis",
15036 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15037 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15038 }
15039 }
15040
Ville Syrjäläad77c532018-06-15 20:44:05 +030015041 /* maximum framebuffer dimensions */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015042 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015043 dev->mode_config.max_width = 2048;
15044 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015045 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015046 dev->mode_config.max_width = 4096;
15047 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015048 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015049 dev->mode_config.max_width = 8192;
15050 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015051 }
Damien Lespiau068be562014-03-28 14:17:49 +000015052
Jani Nikula2a307c22016-11-30 17:43:04 +020015053 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15054 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015055 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015056 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä98fac1d2018-06-15 20:44:04 +030015057 dev->mode_config.cursor_width = 64;
15058 dev->mode_config.cursor_height = 64;
Damien Lespiau068be562014-03-28 14:17:49 +000015059 } else {
Ville Syrjälä98fac1d2018-06-15 20:44:04 +030015060 dev->mode_config.cursor_width = 256;
15061 dev->mode_config.cursor_height = 256;
Damien Lespiau068be562014-03-28 14:17:49 +000015062 }
15063
Matthew Auld73ebd502017-12-11 15:18:20 +000015064 dev->mode_config.fb_base = ggtt->gmadr.start;
Jesse Barnes79e53942008-11-07 14:24:08 -080015065
Zhao Yakui28c97732009-10-09 11:39:41 +080015066 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015067 INTEL_INFO(dev_priv)->num_pipes,
15068 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015069
Damien Lespiau055e3932014-08-18 13:49:10 +010015070 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015071 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015072 if (ret) {
15073 drm_mode_config_cleanup(dev);
15074 return ret;
15075 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015076 }
15077
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015078 intel_shared_dpll_init(dev);
Chris Wilson58ecd9d2017-11-05 13:49:05 +000015079 intel_update_fdi_pll_freq(dev_priv);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015080
Ville Syrjälä5be6e332017-02-20 16:04:43 +020015081 intel_update_czclk(dev_priv);
15082 intel_modeset_init_hw(dev);
15083
Ville Syrjäläb2045352016-05-13 23:41:27 +030015084 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020015085 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030015086
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015087 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015088 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015089 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000015090
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015091 drm_modeset_lock_all(dev);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015092 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015093 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015094
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015095 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015096 struct intel_initial_plane_config plane_config = {};
15097
Jesse Barnes46f297f2014-03-07 08:57:48 -080015098 if (!crtc->active)
15099 continue;
15100
Jesse Barnes46f297f2014-03-07 08:57:48 -080015101 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015102 * Note that reserving the BIOS fb up front prevents us
15103 * from stuffing other stolen allocations like the ring
15104 * on top. This prevents some ugliness at boot time, and
15105 * can even allow for smooth boot transitions if the BIOS
15106 * fb is large enough for the active pipe configuration.
15107 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015108 dev_priv->display.get_initial_plane_config(crtc,
15109 &plane_config);
15110
15111 /*
15112 * If the fb is shared between multiple heads, we'll
15113 * just get the first one.
15114 */
15115 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015116 }
Matt Roperd93c0372015-12-03 11:37:41 -080015117
15118 /*
15119 * Make sure hardware watermarks really match the state we read out.
15120 * Note that we need to do this after reconstructing the BIOS fb's
15121 * since the watermark calculation done here will use pstate->fb.
15122 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020015123 if (!HAS_GMCH_DISPLAY(dev_priv))
15124 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015125
Azhar Shaikh516a49c2018-07-06 11:37:30 -070015126 /*
15127 * Force all active planes to recompute their states. So that on
15128 * mode_setcrtc after probe, all the intel_plane_state variables
15129 * are already calculated and there is no assert_plane warnings
15130 * during bootup.
15131 */
15132 ret = intel_initial_commit(dev);
15133 if (ret)
15134 DRM_DEBUG_KMS("Initial commit in probe failed.\n");
15135
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015136 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010015137}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015138
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015139void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15140{
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020015141 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015142 /* 640x480@60Hz, ~25175 kHz */
15143 struct dpll clock = {
15144 .m1 = 18,
15145 .m2 = 7,
15146 .p1 = 13,
15147 .p2 = 4,
15148 .n = 2,
15149 };
15150 u32 dpll, fp;
15151 int i;
15152
15153 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
15154
15155 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
15156 pipe_name(pipe), clock.vco, clock.dot);
15157
15158 fp = i9xx_dpll_compute_fp(&clock);
15159 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
15160 DPLL_VGA_MODE_DIS |
15161 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
15162 PLL_P2_DIVIDE_BY_4 |
15163 PLL_REF_INPUT_DREFCLK |
15164 DPLL_VCO_ENABLE;
15165
15166 I915_WRITE(FP0(pipe), fp);
15167 I915_WRITE(FP1(pipe), fp);
15168
15169 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
15170 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
15171 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
15172 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
15173 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
15174 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
15175 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
15176
15177 /*
15178 * Apparently we need to have VGA mode enabled prior to changing
15179 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
15180 * dividers, even though the register value does change.
15181 */
15182 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
15183 I915_WRITE(DPLL(pipe), dpll);
15184
15185 /* Wait for the clocks to stabilize. */
15186 POSTING_READ(DPLL(pipe));
15187 udelay(150);
15188
15189 /* The pixel multiplier can only be updated once the
15190 * DPLL is enabled and the clocks are stable.
15191 *
15192 * So write it again.
15193 */
15194 I915_WRITE(DPLL(pipe), dpll);
15195
15196 /* We do this three times for luck */
15197 for (i = 0; i < 3 ; i++) {
15198 I915_WRITE(DPLL(pipe), dpll);
15199 POSTING_READ(DPLL(pipe));
15200 udelay(150); /* wait for warmup */
15201 }
15202
15203 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
15204 POSTING_READ(PIPECONF(pipe));
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020015205
15206 intel_wait_for_pipe_scanline_moving(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015207}
15208
15209void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15210{
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015211 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15212
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015213 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
15214 pipe_name(pipe));
15215
Ville Syrjälä5816d9c2017-11-29 14:54:11 +020015216 WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
15217 WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
15218 WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +020015219 WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
15220 WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015221
15222 I915_WRITE(PIPECONF(pipe), 0);
15223 POSTING_READ(PIPECONF(pipe));
15224
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015225 intel_wait_for_pipe_scanline_stopped(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015226
15227 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
15228 POSTING_READ(DPLL(pipe));
15229}
15230
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015231static void
15232intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
15233{
15234 struct intel_crtc *crtc;
Daniel Vetterfa555832012-10-10 23:14:00 +020015235
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015236 if (INTEL_GEN(dev_priv) >= 4)
15237 return;
Daniel Vetterfa555832012-10-10 23:14:00 +020015238
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015239 for_each_intel_crtc(&dev_priv->drm, crtc) {
15240 struct intel_plane *plane =
15241 to_intel_plane(crtc->base.primary);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015242 struct intel_crtc *plane_crtc;
15243 enum pipe pipe;
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015244
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015245 if (!plane->get_hw_state(plane, &pipe))
15246 continue;
15247
15248 if (pipe == crtc->pipe)
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015249 continue;
15250
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +030015251 DRM_DEBUG_KMS("[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
15252 plane->base.base.id, plane->base.name);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015253
15254 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15255 intel_plane_disable_noatomic(plane_crtc, plane);
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015256 }
Daniel Vetterfa555832012-10-10 23:14:00 +020015257}
15258
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015259static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15260{
15261 struct drm_device *dev = crtc->base.dev;
15262 struct intel_encoder *encoder;
15263
15264 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15265 return true;
15266
15267 return false;
15268}
15269
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015270static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15271{
15272 struct drm_device *dev = encoder->base.dev;
15273 struct intel_connector *connector;
15274
15275 for_each_connector_on_encoder(dev, &encoder->base, connector)
15276 return connector;
15277
15278 return NULL;
15279}
15280
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015281static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015282 enum pipe pch_transcoder)
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015283{
15284 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015285 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015286}
15287
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015288static void intel_sanitize_crtc(struct intel_crtc *crtc,
15289 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter24929352012-07-02 20:28:59 +020015290{
15291 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015292 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015293 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
15294 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015295
Daniel Vetter24929352012-07-02 20:28:59 +020015296 /* Clear any frame start delays used for debugging left by the BIOS */
Ville Syrjälä738a8142017-11-15 22:04:42 +020015297 if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020015298 i915_reg_t reg = PIPECONF(cpu_transcoder);
15299
15300 I915_WRITE(reg,
15301 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15302 }
Daniel Vetter24929352012-07-02 20:28:59 +020015303
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015304 if (crtc_state->base.active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015305 struct intel_plane *plane;
15306
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015307 /* Disable everything but the primary plane */
15308 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015309 const struct intel_plane_state *plane_state =
15310 to_intel_plane_state(plane->base.state);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015311
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015312 if (plane_state->base.visible &&
15313 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
15314 intel_plane_disable_noatomic(crtc, plane);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015315 }
Daniel Vetter96256042015-02-13 21:03:42 +010015316 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015317
Daniel Vetter24929352012-07-02 20:28:59 +020015318 /* Adjust the state of the output pipe according to whether we
15319 * have active connectors/encoders. */
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015320 if (crtc_state->base.active && !intel_crtc_has_encoders(crtc))
Ville Syrjäläda1d0e22017-06-01 17:36:14 +030015321 intel_crtc_disable_noatomic(&crtc->base, ctx);
Daniel Vetter24929352012-07-02 20:28:59 +020015322
Maarten Lankhorst1b52ad42018-10-11 12:04:53 +020015323 if (crtc_state->base.active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015324 /*
15325 * We start out with underrun reporting disabled to avoid races.
15326 * For correct bookkeeping mark this on active crtcs.
15327 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015328 * Also on gmch platforms we dont have any hardware bits to
15329 * disable the underrun reporting. Which means we need to start
15330 * out with underrun reporting disabled also on inactive pipes,
15331 * since otherwise we'll complain about the garbage we read when
15332 * e.g. coming up after runtime pm.
15333 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015334 * No protection against concurrent access is required - at
15335 * worst a fifo underrun happens which also sets this to false.
15336 */
15337 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015338 /*
15339 * We track the PCH trancoder underrun reporting state
15340 * within the crtc. With crtc for pipe A housing the underrun
15341 * reporting state for PCH transcoder A, crtc for pipe B housing
15342 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15343 * and marking underrun reporting as disabled for the non-existing
15344 * PCH transcoders B and C would prevent enabling the south
15345 * error interrupt (see cpt_can_enable_serr_int()).
15346 */
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015347 if (has_pch_trancoder(dev_priv, crtc->pipe))
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015348 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010015349 }
Daniel Vetter24929352012-07-02 20:28:59 +020015350}
15351
15352static void intel_sanitize_encoder(struct intel_encoder *encoder)
15353{
Imre Deak70332ac2018-11-01 16:04:27 +020015354 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015355 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020015356
15357 /* We need to check both for a crtc link (meaning that the
15358 * encoder is active and trying to read from a pipe) and the
15359 * pipe itself being active. */
15360 bool has_active_crtc = encoder->base.crtc &&
15361 to_intel_crtc(encoder->base.crtc)->active;
15362
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015363 connector = intel_encoder_find_connector(encoder);
15364 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015365 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15366 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015367 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015368
15369 /* Connector is active, but has no active pipe. This is
15370 * fallout from our resume register restoring. Disable
15371 * the encoder manually again. */
15372 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015373 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15374
Daniel Vetter24929352012-07-02 20:28:59 +020015375 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15376 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015377 encoder->base.name);
Jani Nikulac84c6fe2018-10-16 15:41:34 +030015378 if (encoder->disable)
15379 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015380 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015381 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020015382 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015383 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015384
15385 /* Inconsistent output/port/pipe state happens presumably due to
15386 * a bug in one of the get_hw_state functions. Or someplace else
15387 * in our code, like the register restore mess on resume. Clamp
15388 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015389
15390 connector->base.dpms = DRM_MODE_DPMS_OFF;
15391 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015392 }
Maarten Lankhorstd6cae4a2018-05-16 10:50:38 +020015393
15394 /* notify opregion of the sanitized encoder state */
15395 intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
Imre Deak70332ac2018-11-01 16:04:27 +020015396
15397 if (INTEL_GEN(dev_priv) >= 11)
15398 icl_sanitize_encoder_pll_mapping(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015399}
15400
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015401void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015402{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015403 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015404
Imre Deak04098752014-02-18 00:02:16 +020015405 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15406 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015407 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020015408 }
15409}
15410
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015411void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020015412{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015413 /* This function can be called both from intel_modeset_setup_hw_state or
15414 * at a very early point in our resume sequence, where the power well
15415 * structures are not yet restored. Since this function is at a very
15416 * paranoid "someone might have enabled VGA while we were not looking"
15417 * level, just check if the power well is enabled instead of trying to
15418 * follow the "don't touch the power well if we don't need it" policy
15419 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015420 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015421 return;
15422
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015423 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020015424
15425 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015426}
15427
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015428/* FIXME read out full plane state for all planes */
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015429static void readout_plane_state(struct drm_i915_private *dev_priv)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015430{
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015431 struct intel_plane *plane;
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015432 struct intel_crtc *crtc;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015433
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015434 for_each_intel_plane(&dev_priv->drm, plane) {
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015435 struct intel_plane_state *plane_state =
15436 to_intel_plane_state(plane->base.state);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015437 struct intel_crtc_state *crtc_state;
15438 enum pipe pipe = PIPE_A;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020015439 bool visible;
15440
15441 visible = plane->get_hw_state(plane, &pipe);
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015442
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015443 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15444 crtc_state = to_intel_crtc_state(crtc->base.state);
15445
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015446 intel_set_plane_visible(crtc_state, plane_state, visible);
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +030015447
15448 DRM_DEBUG_KMS("[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
15449 plane->base.base.id, plane->base.name,
15450 enableddisabled(visible), pipe_name(pipe));
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015451 }
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015452
15453 for_each_intel_crtc(&dev_priv->drm, crtc) {
15454 struct intel_crtc_state *crtc_state =
15455 to_intel_crtc_state(crtc->base.state);
15456
15457 fixup_active_planes(crtc_state);
15458 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015459}
15460
Daniel Vetter30e984d2013-06-05 13:34:17 +020015461static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015462{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015463 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015464 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015465 struct intel_crtc *crtc;
15466 struct intel_encoder *encoder;
15467 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015468 struct drm_connector_list_iter conn_iter;
Daniel Vetter53589012013-06-05 13:34:16 +020015469 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015470
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015471 dev_priv->active_crtcs = 0;
15472
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015473 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015474 struct intel_crtc_state *crtc_state =
15475 to_intel_crtc_state(crtc->base.state);
Daniel Vetter3b117c82013-04-17 20:15:07 +020015476
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015477 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015478 memset(crtc_state, 0, sizeof(*crtc_state));
15479 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015480
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015481 crtc_state->base.active = crtc_state->base.enable =
15482 dev_priv->display.get_pipe_config(crtc, crtc_state);
15483
15484 crtc->base.enabled = crtc_state->base.enable;
15485 crtc->active = crtc_state->base.active;
15486
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015487 if (crtc_state->base.active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015488 dev_priv->active_crtcs |= 1 << crtc->pipe;
15489
Ville Syrjälä78108b72016-05-27 20:59:19 +030015490 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15491 crtc->base.base.id, crtc->base.name,
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015492 enableddisabled(crtc_state->base.active));
Daniel Vetter24929352012-07-02 20:28:59 +020015493 }
15494
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015495 readout_plane_state(dev_priv);
15496
Daniel Vetter53589012013-06-05 13:34:16 +020015497 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15498 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15499
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015500 pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
15501 &pll->state.hw_state);
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015502 pll->state.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015503 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015504 struct intel_crtc_state *crtc_state =
15505 to_intel_crtc_state(crtc->base.state);
15506
15507 if (crtc_state->base.active &&
15508 crtc_state->shared_dpll == pll)
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015509 pll->state.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015510 }
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015511 pll->active_mask = pll->state.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015512
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015513 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015514 pll->info->name, pll->state.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015515 }
15516
Damien Lespiaub2784e12014-08-05 11:29:37 +010015517 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015518 pipe = 0;
15519
15520 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015521 struct intel_crtc_state *crtc_state;
15522
Ville Syrjälä98187832016-10-31 22:37:10 +020015523 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015524 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015525
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015526 encoder->base.crtc = &crtc->base;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015527 encoder->get_config(encoder, crtc_state);
Daniel Vetter24929352012-07-02 20:28:59 +020015528 } else {
15529 encoder->base.crtc = NULL;
15530 }
15531
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015532 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015533 encoder->base.base.id, encoder->base.name,
15534 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015535 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015536 }
15537
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015538 drm_connector_list_iter_begin(dev, &conn_iter);
15539 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter24929352012-07-02 20:28:59 +020015540 if (connector->get_hw_state(connector)) {
15541 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015542
15543 encoder = connector->encoder;
15544 connector->base.encoder = &encoder->base;
15545
15546 if (encoder->base.crtc &&
15547 encoder->base.crtc->state->active) {
15548 /*
15549 * This has to be done during hardware readout
15550 * because anything calling .crtc_disable may
15551 * rely on the connector_mask being accurate.
15552 */
15553 encoder->base.crtc->state->connector_mask |=
Ville Syrjälä40560e22018-06-26 22:47:11 +030015554 drm_connector_mask(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015555 encoder->base.crtc->state->encoder_mask |=
Ville Syrjälä40560e22018-06-26 22:47:11 +030015556 drm_encoder_mask(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015557 }
15558
Daniel Vetter24929352012-07-02 20:28:59 +020015559 } else {
15560 connector->base.dpms = DRM_MODE_DPMS_OFF;
15561 connector->base.encoder = NULL;
15562 }
15563 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015564 connector->base.base.id, connector->base.name,
15565 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020015566 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015567 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015568
15569 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015570 struct intel_crtc_state *crtc_state =
15571 to_intel_crtc_state(crtc->base.state);
Ville Syrjäläd305e062017-08-30 21:57:03 +030015572 int min_cdclk = 0;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015573
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015574 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015575 if (crtc_state->base.active) {
15576 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
Ville Syrjäläbd4cd032018-04-26 19:30:15 +030015577 crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
15578 crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015579 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015580 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15581
15582 /*
15583 * The initial mode needs to be set in order to keep
15584 * the atomic core happy. It wants a valid mode if the
15585 * crtc's enabled, so we do the above call.
15586 *
Daniel Vetter7800fb62016-12-19 09:24:23 +010015587 * But we don't set all the derived state fully, hence
15588 * set a flag to indicate that a full recalculation is
15589 * needed on the next commit.
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015590 */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015591 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015592
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020015593 intel_crtc_compute_pixel_rate(crtc_state);
15594
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015595 if (dev_priv->display.modeset_calc_cdclk) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030015596 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015597 if (WARN_ON(min_cdclk < 0))
15598 min_cdclk = 0;
15599 }
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015600
Daniel Vetter5caa0fe2017-05-09 16:03:29 +020015601 drm_calc_timestamping_constants(&crtc->base,
15602 &crtc_state->base.adjusted_mode);
Maarten Lankhorstf2bdd112018-10-11 12:04:52 +020015603 update_scanline_offset(crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015604 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015605
Ville Syrjäläd305e062017-08-30 21:57:03 +030015606 dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030015607 dev_priv->min_voltage_level[crtc->pipe] =
15608 crtc_state->min_voltage_level;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015609
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015610 intel_pipe_config_sanity_check(dev_priv, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015611 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015612}
15613
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015614static void
15615get_encoder_power_domains(struct drm_i915_private *dev_priv)
15616{
15617 struct intel_encoder *encoder;
15618
15619 for_each_intel_encoder(&dev_priv->drm, encoder) {
15620 u64 get_domains;
15621 enum intel_display_power_domain domain;
Imre Deak52528052018-06-21 21:44:49 +030015622 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015623
15624 if (!encoder->get_power_domains)
15625 continue;
15626
Imre Deak52528052018-06-21 21:44:49 +030015627 /*
Imre Deakb79ebe72018-07-05 15:26:54 +030015628 * MST-primary and inactive encoders don't have a crtc state
15629 * and neither of these require any power domain references.
Imre Deak52528052018-06-21 21:44:49 +030015630 */
Imre Deakb79ebe72018-07-05 15:26:54 +030015631 if (!encoder->base.crtc)
15632 continue;
Imre Deak52528052018-06-21 21:44:49 +030015633
Imre Deakb79ebe72018-07-05 15:26:54 +030015634 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
Imre Deak52528052018-06-21 21:44:49 +030015635 get_domains = encoder->get_power_domains(encoder, crtc_state);
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015636 for_each_power_domain(domain, get_domains)
15637 intel_display_power_get(dev_priv, domain);
15638 }
15639}
15640
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015641static void intel_early_display_was(struct drm_i915_private *dev_priv)
15642{
15643 /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
15644 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
15645 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
15646 DARBF_GATING_DIS);
15647
15648 if (IS_HASWELL(dev_priv)) {
15649 /*
15650 * WaRsPkgCStateDisplayPMReq:hsw
15651 * System hang if this isn't done before disabling all planes!
15652 */
15653 I915_WRITE(CHICKEN_PAR1_1,
15654 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
15655 }
15656}
15657
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015658/* Scan out the current hw modeset state,
15659 * and sanitizes it to the current state
15660 */
15661static void
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015662intel_modeset_setup_hw_state(struct drm_device *dev,
15663 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015664{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015665 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015666 struct intel_crtc *crtc;
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015667 struct intel_crtc_state *crtc_state;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015668 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015669 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015670
Imre Deak2cd9a682018-08-16 15:37:57 +030015671 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
15672
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015673 intel_early_display_was(dev_priv);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015674 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015675
15676 /* HW state is read out, now we need to sanitize this mess. */
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015677 get_encoder_power_domains(dev_priv);
15678
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015679 /*
15680 * intel_sanitize_plane_mapping() may need to do vblank
15681 * waits, so we need vblank interrupts restored beforehand.
15682 */
15683 for_each_intel_crtc(&dev_priv->drm, crtc) {
15684 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015685
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015686 if (crtc->base.state->active)
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015687 drm_crtc_vblank_on(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015688 }
15689
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015690 intel_sanitize_plane_mapping(dev_priv);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015691
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015692 for_each_intel_encoder(dev, encoder)
15693 intel_sanitize_encoder(encoder);
15694
15695 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015696 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015697 intel_sanitize_crtc(crtc, ctx);
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015698 intel_dump_pipe_config(crtc, crtc_state,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015699 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015700 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015701
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015702 intel_modeset_update_connector_atomic_state(dev);
15703
Daniel Vetter35c95372013-07-17 06:55:04 +020015704 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15705 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15706
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015707 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015708 continue;
15709
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015710 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
15711 pll->info->name);
Daniel Vetter35c95372013-07-17 06:55:04 +020015712
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015713 pll->info->funcs->disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015714 pll->on = false;
15715 }
15716
Ville Syrjälä04548cb2017-04-21 21:14:29 +030015717 if (IS_G4X(dev_priv)) {
15718 g4x_wm_get_hw_state(dev);
15719 g4x_wm_sanitize(dev_priv);
15720 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015721 vlv_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015722 vlv_wm_sanitize(dev_priv);
Rodrigo Vivia029fa42017-08-09 13:52:48 -070015723 } else if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat30789992014-11-04 17:06:45 +000015724 skl_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015725 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015726 ilk_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015727 }
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015728
15729 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020015730 u64 put_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015731
Maarten Lankhorst91d78192018-10-11 12:04:54 +020015732 crtc_state = to_intel_crtc_state(crtc->base.state);
15733 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015734 if (WARN_ON(put_domains))
15735 modeset_put_power_domains(dev_priv, put_domains);
15736 }
Imre Deak2cd9a682018-08-16 15:37:57 +030015737
15738 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015739
15740 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015741}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015742
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015743void intel_display_resume(struct drm_device *dev)
15744{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015745 struct drm_i915_private *dev_priv = to_i915(dev);
15746 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15747 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015748 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015749
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015750 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030015751 if (state)
15752 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015753
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015754 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015755
Maarten Lankhorst73974892016-08-05 23:28:27 +030015756 while (1) {
15757 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15758 if (ret != -EDEADLK)
15759 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015760
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015761 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015762 }
15763
Maarten Lankhorst73974892016-08-05 23:28:27 +030015764 if (!ret)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010015765 ret = __intel_display_resume(dev, state, &ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +030015766
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +053015767 intel_enable_ipc(dev_priv);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015768 drm_modeset_drop_locks(&ctx);
15769 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015770
Chris Wilson08536952016-10-14 13:18:18 +010015771 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015772 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson3c5e37f2017-01-15 12:58:25 +000015773 if (state)
15774 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015775}
15776
Manasi Navare886c6b82017-10-26 14:52:00 -070015777static void intel_hpd_poll_fini(struct drm_device *dev)
15778{
15779 struct intel_connector *connector;
15780 struct drm_connector_list_iter conn_iter;
15781
Chris Wilson448aa912017-11-28 11:01:47 +000015782 /* Kill all the work that may have been queued by hpd. */
Manasi Navare886c6b82017-10-26 14:52:00 -070015783 drm_connector_list_iter_begin(dev, &conn_iter);
15784 for_each_intel_connector_iter(connector, &conn_iter) {
15785 if (connector->modeset_retry_work.func)
15786 cancel_work_sync(&connector->modeset_retry_work);
Ramalingam Cd3dacc72018-10-29 15:15:46 +053015787 if (connector->hdcp.shim) {
15788 cancel_delayed_work_sync(&connector->hdcp.check_work);
15789 cancel_work_sync(&connector->hdcp.prop_work);
Sean Paulee5e5e72018-01-08 14:55:39 -050015790 }
Manasi Navare886c6b82017-10-26 14:52:00 -070015791 }
15792 drm_connector_list_iter_end(&conn_iter);
15793}
15794
Jesse Barnes79e53942008-11-07 14:24:08 -080015795void intel_modeset_cleanup(struct drm_device *dev)
15796{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015797 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070015798
Chris Wilson8bcf9f72018-07-10 10:44:20 +010015799 flush_workqueue(dev_priv->modeset_wq);
15800
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015801 flush_work(&dev_priv->atomic_helper.free_work);
15802 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15803
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015804 /*
15805 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015806 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015807 * experience fancy races otherwise.
15808 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015809 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015810
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015811 /*
15812 * Due to the hpd irq storm handling the hotplug work can re-arm the
15813 * poll handlers. Hence disable polling after hpd handling is shut down.
15814 */
Manasi Navare886c6b82017-10-26 14:52:00 -070015815 intel_hpd_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015816
Daniel Vetter4f256d82017-07-15 00:46:55 +020015817 /* poll work can call into fbdev, hence clean that up afterwards */
15818 intel_fbdev_fini(dev_priv);
15819
Jesse Barnes723bfd72010-10-07 16:01:13 -070015820 intel_unregister_dsm_handler();
15821
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015822 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015823
Chris Wilson1630fe72011-07-08 12:22:42 +010015824 /* flush any delayed tasks or pending work */
15825 flush_scheduled_work();
15826
Jesse Barnes79e53942008-11-07 14:24:08 -080015827 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015828
Chris Wilson1ee8da62016-05-12 12:43:23 +010015829 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015830
Tvrtko Ursulin40196442016-12-01 14:16:42 +000015831 intel_teardown_gmbus(dev_priv);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020015832
15833 destroy_workqueue(dev_priv->modeset_wq);
José Roberto de Souzaacde44b2018-11-07 16:16:45 -080015834
15835 intel_fbc_cleanup_cfb(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015836}
15837
Dave Airlie28d52042009-09-21 14:33:58 +100015838/*
15839 * set vga decode state - true == enable VGA decode
15840 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015841int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100015842{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015843 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015844 u16 gmch_ctrl;
15845
Chris Wilson75fa0412014-02-07 18:37:02 -020015846 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15847 DRM_ERROR("failed to read control word\n");
15848 return -EIO;
15849 }
15850
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015851 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15852 return 0;
15853
Dave Airlie28d52042009-09-21 14:33:58 +100015854 if (state)
15855 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15856 else
15857 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015858
15859 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15860 DRM_ERROR("failed to write control word\n");
15861 return -EIO;
15862 }
15863
Dave Airlie28d52042009-09-21 14:33:58 +100015864 return 0;
15865}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015866
Chris Wilson98a2f412016-10-12 10:05:18 +010015867#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15868
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015869struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015870
15871 u32 power_well_driver;
15872
Chris Wilson63b66e52013-08-08 15:12:06 +020015873 int num_transcoders;
15874
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015875 struct intel_cursor_error_state {
15876 u32 control;
15877 u32 position;
15878 u32 base;
15879 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015880 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015881
15882 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015883 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015884 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015885 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015886 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015887
15888 struct intel_plane_error_state {
15889 u32 control;
15890 u32 stride;
15891 u32 size;
15892 u32 pos;
15893 u32 addr;
15894 u32 surface;
15895 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015896 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015897
15898 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015899 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015900 enum transcoder cpu_transcoder;
15901
15902 u32 conf;
15903
15904 u32 htotal;
15905 u32 hblank;
15906 u32 hsync;
15907 u32 vtotal;
15908 u32 vblank;
15909 u32 vsync;
15910 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015911};
15912
15913struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010015914intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015915{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015916 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015917 int transcoders[] = {
15918 TRANSCODER_A,
15919 TRANSCODER_B,
15920 TRANSCODER_C,
15921 TRANSCODER_EDP,
15922 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015923 int i;
15924
Chris Wilsonc0336662016-05-06 15:40:21 +010015925 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020015926 return NULL;
15927
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015928 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015929 if (error == NULL)
15930 return NULL;
15931
Chris Wilsonc0336662016-05-06 15:40:21 +010015932 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Imre Deak75e39682018-08-06 12:58:39 +030015933 error->power_well_driver = I915_READ(HSW_PWR_WELL_CTL2);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015934
Damien Lespiau055e3932014-08-18 13:49:10 +010015935 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015936 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015937 __intel_display_power_is_enabled(dev_priv,
15938 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015939 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015940 continue;
15941
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015942 error->cursor[i].control = I915_READ(CURCNTR(i));
15943 error->cursor[i].position = I915_READ(CURPOS(i));
15944 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015945
15946 error->plane[i].control = I915_READ(DSPCNTR(i));
15947 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015948 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015949 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015950 error->plane[i].pos = I915_READ(DSPPOS(i));
15951 }
Chris Wilsonc0336662016-05-06 15:40:21 +010015952 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030015953 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010015954 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015955 error->plane[i].surface = I915_READ(DSPSURF(i));
15956 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15957 }
15958
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015959 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015960
Chris Wilsonc0336662016-05-06 15:40:21 +010015961 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030015962 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015963 }
15964
Jani Nikula4d1de972016-03-18 17:05:42 +020015965 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010015966 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030015967 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020015968 error->num_transcoders++; /* Account for eDP. */
15969
15970 for (i = 0; i < error->num_transcoders; i++) {
15971 enum transcoder cpu_transcoder = transcoders[i];
15972
Imre Deakddf9c532013-11-27 22:02:02 +020015973 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015974 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015975 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015976 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015977 continue;
15978
Chris Wilson63b66e52013-08-08 15:12:06 +020015979 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15980
15981 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15982 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15983 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15984 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15985 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15986 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15987 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015988 }
15989
15990 return error;
15991}
15992
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015993#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15994
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015995void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015996intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015997 struct intel_display_error_state *error)
15998{
Chris Wilson5a4c6f12017-02-14 16:46:11 +000015999 struct drm_i915_private *dev_priv = m->i915;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016000 int i;
16001
Chris Wilson63b66e52013-08-08 15:12:06 +020016002 if (!error)
16003 return;
16004
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016005 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010016006 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016007 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016008 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016009 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016010 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016011 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016012 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016013 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016014 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016015
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016016 err_printf(m, "Plane [%d]:\n", i);
16017 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16018 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000016019 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016020 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16021 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016022 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010016023 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016024 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000016025 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016026 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16027 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016028 }
16029
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016030 err_printf(m, "Cursor [%d]:\n", i);
16031 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16032 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16033 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016034 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016035
16036 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016037 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016038 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016039 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016040 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016041 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16042 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16043 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16044 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16045 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16046 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16047 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16048 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016049}
Chris Wilson98a2f412016-10-12 10:05:18 +010016050
16051#endif