blob: 741fc5b4f9d926b061505ff6abe28579ad95f44a [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010037#include "intel_frontbuffer.h"
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000040#include "i915_gem_clflush.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020041#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070042#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080043#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080044#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010045#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070047#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
Daniel Vetter72fdb40c2018-09-05 15:57:11 +020049#include <drm/drm_atomic_uapi.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080050#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080051#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080052
Matt Roper465c1202014-05-29 08:06:54 -070053/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010054static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010055 DRM_FORMAT_C8,
56 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070057 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070059};
60
61/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010062static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010063 DRM_FORMAT_C8,
64 DRM_FORMAT_RGB565,
65 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070066 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010067 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_XBGR2101010,
69};
70
Ben Widawsky714244e2017-08-01 09:58:16 -070071static const uint64_t i9xx_format_modifiers[] = {
72 I915_FORMAT_MOD_X_TILED,
73 DRM_FORMAT_MOD_LINEAR,
74 DRM_FORMAT_MOD_INVALID
75};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Ben Widawsky714244e2017-08-01 09:58:16 -070082static const uint64_t cursor_format_modifiers[] = {
83 DRM_FORMAT_MOD_LINEAR,
84 DRM_FORMAT_MOD_INVALID
85};
86
Jesse Barnesf1f644d2013-06-27 00:39:25 +030087static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020088 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030089static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020090 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030091
Chris Wilson24dbf512017-02-15 10:59:18 +000092static int intel_framebuffer_init(struct intel_framebuffer *ifb,
93 struct drm_i915_gem_object *obj,
94 struct drm_mode_fb_cmd2 *mode_cmd);
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +020095static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state);
96static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
Daniel Vetter29407aa2014-04-24 23:55:08 +020097static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070098 struct intel_link_m_n *m_n,
99 struct intel_link_m_n *m2_n2);
Maarten Lankhorstfdf73512018-10-04 11:45:52 +0200100static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
101static void ironlake_set_pipeconf(const struct intel_crtc_state *crtc_state);
102static void haswell_set_pipeconf(const struct intel_crtc_state *crtc_state);
103static void haswell_set_pipemisc(const struct intel_crtc_state *crtc_state);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200104static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200105 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200106static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200107 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200108static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
109static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Nabendu Maiti1c74eea2016-11-29 11:23:14 +0530110static void intel_crtc_init_scalers(struct intel_crtc *crtc,
111 struct intel_crtc_state *crtc_state);
Maarten Lankhorstb2562712018-10-04 11:45:53 +0200112static void skylake_pfit_enable(const struct intel_crtc_state *crtc_state);
113static void ironlake_pfit_disable(const struct intel_crtc_state *old_crtc_state);
114static void ironlake_pfit_enable(const struct intel_crtc_state *crtc_state);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +0300115static void intel_modeset_setup_hw_state(struct drm_device *dev,
116 struct drm_modeset_acquire_ctx *ctx);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200117static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100118
Ma Lingd4906092009-03-18 20:13:27 +0800119struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300120 struct {
121 int min, max;
122 } dot, vco, n, m, m1, m2, p, p1;
123
124 struct {
125 int dot_limit;
126 int p2_slow, p2_fast;
127 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800128};
Jesse Barnes79e53942008-11-07 14:24:08 -0800129
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300130/* returns HPLL frequency in kHz */
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200131int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300132{
133 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
134
135 /* Obtain SKU information */
136 mutex_lock(&dev_priv->sb_lock);
137 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
138 CCK_FUSE_HPLL_FREQ_MASK;
139 mutex_unlock(&dev_priv->sb_lock);
140
141 return vco_freq[hpll_freq] * 1000;
142}
143
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200144int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
145 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300146{
147 u32 val;
148 int divider;
149
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300150 mutex_lock(&dev_priv->sb_lock);
151 val = vlv_cck_read(dev_priv, reg);
152 mutex_unlock(&dev_priv->sb_lock);
153
154 divider = val & CCK_FREQUENCY_VALUES;
155
156 WARN((val & CCK_FREQUENCY_STATUS) !=
157 (divider << CCK_FREQUENCY_STATUS_SHIFT),
158 "%s change in progress\n", name);
159
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200160 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
161}
162
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +0200163int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
164 const char *name, u32 reg)
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200165{
166 if (dev_priv->hpll_freq == 0)
Ville Syrjälä49cd97a2017-02-07 20:33:45 +0200167 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200168
169 return vlv_get_cck_clock(dev_priv, name, reg,
170 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300171}
172
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300173static void intel_update_czclk(struct drm_i915_private *dev_priv)
174{
Wayne Boyer666a4532015-12-09 12:29:35 -0800175 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300176 return;
177
178 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
179 CCK_CZ_CLOCK_CONTROL);
180
181 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
182}
183
Chris Wilson021357a2010-09-07 20:54:59 +0100184static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200185intel_fdi_link_freq(struct drm_i915_private *dev_priv,
186 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100187{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200188 if (HAS_DDI(dev_priv))
189 return pipe_config->port_clock; /* SPLL */
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200190 else
Chris Wilson58ecd9d2017-11-05 13:49:05 +0000191 return dev_priv->fdi_pll_freq;
Chris Wilson021357a2010-09-07 20:54:59 +0100192}
193
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300194static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400195 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200196 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200197 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400198 .m = { .min = 96, .max = 140 },
199 .m1 = { .min = 18, .max = 26 },
200 .m2 = { .min = 6, .max = 16 },
201 .p = { .min = 4, .max = 128 },
202 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700203 .p2 = { .dot_limit = 165000,
204 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700205};
206
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300207static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200208 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200209 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200210 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200211 .m = { .min = 96, .max = 140 },
212 .m1 = { .min = 18, .max = 26 },
213 .m2 = { .min = 6, .max = 16 },
214 .p = { .min = 4, .max = 128 },
215 .p1 = { .min = 2, .max = 33 },
216 .p2 = { .dot_limit = 165000,
217 .p2_slow = 4, .p2_fast = 4 },
218};
219
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300220static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400221 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200222 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200223 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400224 .m = { .min = 96, .max = 140 },
225 .m1 = { .min = 18, .max = 26 },
226 .m2 = { .min = 6, .max = 16 },
227 .p = { .min = 4, .max = 128 },
228 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700229 .p2 = { .dot_limit = 165000,
230 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700231};
Eric Anholt273e27c2011-03-30 13:01:10 -0700232
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300233static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400234 .dot = { .min = 20000, .max = 400000 },
235 .vco = { .min = 1400000, .max = 2800000 },
236 .n = { .min = 1, .max = 6 },
237 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100238 .m1 = { .min = 8, .max = 18 },
239 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400240 .p = { .min = 5, .max = 80 },
241 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700242 .p2 = { .dot_limit = 200000,
243 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700244};
245
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300246static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400247 .dot = { .min = 20000, .max = 400000 },
248 .vco = { .min = 1400000, .max = 2800000 },
249 .n = { .min = 1, .max = 6 },
250 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100251 .m1 = { .min = 8, .max = 18 },
252 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400253 .p = { .min = 7, .max = 98 },
254 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700255 .p2 = { .dot_limit = 112000,
256 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Eric Anholt273e27c2011-03-30 13:01:10 -0700259
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300260static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .dot = { .min = 25000, .max = 270000 },
262 .vco = { .min = 1750000, .max = 3500000},
263 .n = { .min = 1, .max = 4 },
264 .m = { .min = 104, .max = 138 },
265 .m1 = { .min = 17, .max = 23 },
266 .m2 = { .min = 5, .max = 11 },
267 .p = { .min = 10, .max = 30 },
268 .p1 = { .min = 1, .max = 3},
269 .p2 = { .dot_limit = 270000,
270 .p2_slow = 10,
271 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800272 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300275static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700276 .dot = { .min = 22000, .max = 400000 },
277 .vco = { .min = 1750000, .max = 3500000},
278 .n = { .min = 1, .max = 4 },
279 .m = { .min = 104, .max = 138 },
280 .m1 = { .min = 16, .max = 23 },
281 .m2 = { .min = 5, .max = 11 },
282 .p = { .min = 5, .max = 80 },
283 .p1 = { .min = 1, .max = 8},
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700286};
287
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300288static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .dot = { .min = 20000, .max = 115000 },
290 .vco = { .min = 1750000, .max = 3500000 },
291 .n = { .min = 1, .max = 3 },
292 .m = { .min = 104, .max = 138 },
293 .m1 = { .min = 17, .max = 23 },
294 .m2 = { .min = 5, .max = 11 },
295 .p = { .min = 28, .max = 112 },
296 .p1 = { .min = 2, .max = 8 },
297 .p2 = { .dot_limit = 0,
298 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800299 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
301
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300302static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700303 .dot = { .min = 80000, .max = 224000 },
304 .vco = { .min = 1750000, .max = 3500000 },
305 .n = { .min = 1, .max = 3 },
306 .m = { .min = 104, .max = 138 },
307 .m1 = { .min = 17, .max = 23 },
308 .m2 = { .min = 5, .max = 11 },
309 .p = { .min = 14, .max = 42 },
310 .p1 = { .min = 2, .max = 6 },
311 .p2 = { .dot_limit = 0,
312 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800313 },
Keith Packarde4b36692009-06-05 19:22:17 -0700314};
315
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300316static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400317 .dot = { .min = 20000, .max = 400000},
318 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400320 .n = { .min = 3, .max = 6 },
321 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700322 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400323 .m1 = { .min = 0, .max = 0 },
324 .m2 = { .min = 0, .max = 254 },
325 .p = { .min = 5, .max = 80 },
326 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700327 .p2 = { .dot_limit = 200000,
328 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700329};
330
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300331static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400332 .dot = { .min = 20000, .max = 400000 },
333 .vco = { .min = 1700000, .max = 3500000 },
334 .n = { .min = 3, .max = 6 },
335 .m = { .min = 2, .max = 256 },
336 .m1 = { .min = 0, .max = 0 },
337 .m2 = { .min = 0, .max = 254 },
338 .p = { .min = 7, .max = 112 },
339 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700340 .p2 = { .dot_limit = 112000,
341 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700342};
343
Eric Anholt273e27c2011-03-30 13:01:10 -0700344/* Ironlake / Sandybridge
345 *
346 * We calculate clock using (register_value + 2) for N/M1/M2, so here
347 * the range value for them is (actual_value - 2).
348 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300349static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700350 .dot = { .min = 25000, .max = 350000 },
351 .vco = { .min = 1760000, .max = 3510000 },
352 .n = { .min = 1, .max = 5 },
353 .m = { .min = 79, .max = 127 },
354 .m1 = { .min = 12, .max = 22 },
355 .m2 = { .min = 5, .max = 9 },
356 .p = { .min = 5, .max = 80 },
357 .p1 = { .min = 1, .max = 8 },
358 .p2 = { .dot_limit = 225000,
359 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700360};
361
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300362static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .dot = { .min = 25000, .max = 350000 },
364 .vco = { .min = 1760000, .max = 3510000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 79, .max = 118 },
367 .m1 = { .min = 12, .max = 22 },
368 .m2 = { .min = 5, .max = 9 },
369 .p = { .min = 28, .max = 112 },
370 .p1 = { .min = 2, .max = 8 },
371 .p2 = { .dot_limit = 225000,
372 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800373};
374
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300375static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700376 .dot = { .min = 25000, .max = 350000 },
377 .vco = { .min = 1760000, .max = 3510000 },
378 .n = { .min = 1, .max = 3 },
379 .m = { .min = 79, .max = 127 },
380 .m1 = { .min = 12, .max = 22 },
381 .m2 = { .min = 5, .max = 9 },
382 .p = { .min = 14, .max = 56 },
383 .p1 = { .min = 2, .max = 8 },
384 .p2 = { .dot_limit = 225000,
385 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800386};
387
Eric Anholt273e27c2011-03-30 13:01:10 -0700388/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300389static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 .dot = { .min = 25000, .max = 350000 },
391 .vco = { .min = 1760000, .max = 3510000 },
392 .n = { .min = 1, .max = 2 },
393 .m = { .min = 79, .max = 126 },
394 .m1 = { .min = 12, .max = 22 },
395 .m2 = { .min = 5, .max = 9 },
396 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400397 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700398 .p2 = { .dot_limit = 225000,
399 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800400};
401
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300402static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700403 .dot = { .min = 25000, .max = 350000 },
404 .vco = { .min = 1760000, .max = 3510000 },
405 .n = { .min = 1, .max = 3 },
406 .m = { .min = 79, .max = 126 },
407 .m1 = { .min = 12, .max = 22 },
408 .m2 = { .min = 5, .max = 9 },
409 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400410 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700411 .p2 = { .dot_limit = 225000,
412 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800413};
414
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300415static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300416 /*
417 * These are the data rate limits (measured in fast clocks)
418 * since those are the strictest limits we have. The fast
419 * clock and actual rate limits are more relaxed, so checking
420 * them would make no difference.
421 */
422 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200423 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700424 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700425 .m1 = { .min = 2, .max = 3 },
426 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300427 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300428 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700429};
430
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300431static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300432 /*
433 * These are the data rate limits (measured in fast clocks)
434 * since those are the strictest limits we have. The fast
435 * clock and actual rate limits are more relaxed, so checking
436 * them would make no difference.
437 */
438 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200439 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300440 .n = { .min = 1, .max = 1 },
441 .m1 = { .min = 2, .max = 2 },
442 .m2 = { .min = 24 << 22, .max = 175 << 22 },
443 .p1 = { .min = 2, .max = 4 },
444 .p2 = { .p2_slow = 1, .p2_fast = 14 },
445};
446
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300447static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200448 /* FIXME: find real dot limits */
449 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530450 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200451 .n = { .min = 1, .max = 1 },
452 .m1 = { .min = 2, .max = 2 },
453 /* FIXME: find real m2 limits */
454 .m2 = { .min = 2 << 22, .max = 255 << 22 },
455 .p1 = { .min = 2, .max = 4 },
456 .p2 = { .p2_slow = 1, .p2_fast = 20 },
457};
458
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530459static void
Vidya Srinivas6deef9b602018-05-12 03:03:13 +0530460skl_wa_528(struct drm_i915_private *dev_priv, int pipe, bool enable)
461{
462 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
463 return;
464
465 if (enable)
466 I915_WRITE(CHICKEN_PIPESL_1(pipe), HSW_FBCQ_DIS);
467 else
468 I915_WRITE(CHICKEN_PIPESL_1(pipe), 0);
469}
470
471static void
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530472skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
473{
Vidya Srinivas6deef9b602018-05-12 03:03:13 +0530474 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +0530475 return;
476
477 if (enable)
478 I915_WRITE(CLKGATE_DIS_PSL(pipe),
479 DUPS1_GATING_DIS | DUPS2_GATING_DIS);
480 else
481 I915_WRITE(CLKGATE_DIS_PSL(pipe),
482 I915_READ(CLKGATE_DIS_PSL(pipe)) &
483 ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
484}
485
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200486static bool
Maarten Lankhorst24f28452017-11-22 19:39:01 +0100487needs_modeset(const struct drm_crtc_state *state)
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200488{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200489 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200490}
491
Imre Deakdccbea32015-06-22 23:35:51 +0300492/*
493 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
494 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
495 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
496 * The helpers' return value is the rate of the clock that is fed to the
497 * display engine's pipe which can be the above fast dot clock rate or a
498 * divided-down version of it.
499 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500500/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300501static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800502{
Shaohua Li21778322009-02-23 15:19:16 +0800503 clock->m = clock->m2 + 2;
504 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200505 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300506 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300507 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
508 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300509
510 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800511}
512
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200513static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
514{
515 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
516}
517
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300518static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800519{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200520 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800521 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200522 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300523 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300524 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
525 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300526
527 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800528}
529
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300530static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300531{
532 clock->m = clock->m1 * clock->m2;
533 clock->p = clock->p1 * clock->p2;
534 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300535 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300536 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
537 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300538
539 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300540}
541
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300542int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300543{
544 clock->m = clock->m1 * clock->m2;
545 clock->p = clock->p1 * clock->p2;
546 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300547 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300548 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
549 clock->n << 22);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300551
552 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300553}
554
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800555#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Chris Wilsonc38c1452018-02-14 13:49:22 +0000556
557/*
Jesse Barnes79e53942008-11-07 14:24:08 -0800558 * Returns whether the given set of divisors are valid for a given refclk with
559 * the given connectors.
560 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100561static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300562 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300563 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800564{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300565 if (clock->n < limit->n.min || limit->n.max < clock->n)
566 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800567 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400568 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400570 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800571 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400572 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300573
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100574 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200575 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300576 if (clock->m1 <= clock->m2)
577 INTELPllInvalid("m1 <= m2\n");
578
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100579 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200580 !IS_GEN9_LP(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300581 if (clock->p < limit->p.min || limit->p.max < clock->p)
582 INTELPllInvalid("p out of range\n");
583 if (clock->m < limit->m.min || limit->m.max < clock->m)
584 INTELPllInvalid("m out of range\n");
585 }
586
Jesse Barnes79e53942008-11-07 14:24:08 -0800587 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400588 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
590 * connector, etc., rather than just a single range.
591 */
592 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400593 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800594
595 return true;
596}
597
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300598static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300599i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300600 const struct intel_crtc_state *crtc_state,
601 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800602{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300603 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800604
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300605 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100607 * For LVDS just rely on its current settings for dual-channel.
608 * We haven't figured out how to reliably set up different
609 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100611 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300612 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300614 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 } else {
616 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300617 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800618 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300619 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800620 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300621}
622
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200623/*
624 * Returns a set of divisors for the desired target clock with the given
625 * refclk, or FALSE. The returned values represent the clock equation:
626 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
627 *
628 * Target and reference clocks are specified in kHz.
629 *
630 * If match_clock is provided, then best_clock P divider must match the P
631 * divider from @match_clock used for LVDS downclocking.
632 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300633static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300634i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300635 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300636 int target, int refclk, struct dpll *match_clock,
637 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300638{
639 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300640 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300641 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800642
Akshay Joshi0206e352011-08-16 15:34:10 -0400643 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800644
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300645 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
646
Zhao Yakui42158662009-11-20 11:24:18 +0800647 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
648 clock.m1++) {
649 for (clock.m2 = limit->m2.min;
650 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200651 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800652 break;
653 for (clock.n = limit->n.min;
654 clock.n <= limit->n.max; clock.n++) {
655 for (clock.p1 = limit->p1.min;
656 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 int this_err;
658
Imre Deakdccbea32015-06-22 23:35:51 +0300659 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100660 if (!intel_PLL_is_valid(to_i915(dev),
661 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000662 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800664 if (match_clock &&
665 clock.p != match_clock->p)
666 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800667
668 this_err = abs(clock.dot - target);
669 if (this_err < err) {
670 *best_clock = clock;
671 err = this_err;
672 }
673 }
674 }
675 }
676 }
677
678 return (err != target);
679}
680
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200681/*
682 * Returns a set of divisors for the desired target clock with the given
683 * refclk, or FALSE. The returned values represent the clock equation:
684 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
685 *
686 * Target and reference clocks are specified in kHz.
687 *
688 * If match_clock is provided, then best_clock P divider must match the P
689 * divider from @match_clock used for LVDS downclocking.
690 */
Ma Lingd4906092009-03-18 20:13:27 +0800691static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300692pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200693 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300694 int target, int refclk, struct dpll *match_clock,
695 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200696{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300697 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300698 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200699 int err = target;
700
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200701 memset(best_clock, 0, sizeof(*best_clock));
702
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
704
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200705 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
706 clock.m1++) {
707 for (clock.m2 = limit->m2.min;
708 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200709 for (clock.n = limit->n.min;
710 clock.n <= limit->n.max; clock.n++) {
711 for (clock.p1 = limit->p1.min;
712 clock.p1 <= limit->p1.max; clock.p1++) {
713 int this_err;
714
Imre Deakdccbea32015-06-22 23:35:51 +0300715 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100716 if (!intel_PLL_is_valid(to_i915(dev),
717 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800718 &clock))
719 continue;
720 if (match_clock &&
721 clock.p != match_clock->p)
722 continue;
723
724 this_err = abs(clock.dot - target);
725 if (this_err < err) {
726 *best_clock = clock;
727 err = this_err;
728 }
729 }
730 }
731 }
732 }
733
734 return (err != target);
735}
736
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200737/*
738 * Returns a set of divisors for the desired target clock with the given
739 * refclk, or FALSE. The returned values represent the clock equation:
740 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200741 *
742 * Target and reference clocks are specified in kHz.
743 *
744 * If match_clock is provided, then best_clock P divider must match the P
745 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200746 */
Ma Lingd4906092009-03-18 20:13:27 +0800747static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300748g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200749 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300750 int target, int refclk, struct dpll *match_clock,
751 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800752{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300753 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300754 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800755 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300756 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400757 /* approximately equals target * 0.00585 */
758 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800759
760 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300761
762 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
763
Ma Lingd4906092009-03-18 20:13:27 +0800764 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200765 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800766 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200767 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800768 for (clock.m1 = limit->m1.max;
769 clock.m1 >= limit->m1.min; clock.m1--) {
770 for (clock.m2 = limit->m2.max;
771 clock.m2 >= limit->m2.min; clock.m2--) {
772 for (clock.p1 = limit->p1.max;
773 clock.p1 >= limit->p1.min; clock.p1--) {
774 int this_err;
775
Imre Deakdccbea32015-06-22 23:35:51 +0300776 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100777 if (!intel_PLL_is_valid(to_i915(dev),
778 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000779 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800780 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000781
782 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800783 if (this_err < err_most) {
784 *best_clock = clock;
785 err_most = this_err;
786 max_n = clock.n;
787 found = true;
788 }
789 }
790 }
791 }
792 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800793 return found;
794}
Ma Lingd4906092009-03-18 20:13:27 +0800795
Imre Deakd5dd62b2015-03-17 11:40:03 +0200796/*
797 * Check if the calculated PLL configuration is more optimal compared to the
798 * best configuration and error found so far. Return the calculated error.
799 */
800static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300801 const struct dpll *calculated_clock,
802 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200803 unsigned int best_error_ppm,
804 unsigned int *error_ppm)
805{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200806 /*
807 * For CHV ignore the error and consider only the P value.
808 * Prefer a bigger P value based on HW requirements.
809 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100810 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200811 *error_ppm = 0;
812
813 return calculated_clock->p > best_clock->p;
814 }
815
Imre Deak24be4e42015-03-17 11:40:04 +0200816 if (WARN_ON_ONCE(!target_freq))
817 return false;
818
Imre Deakd5dd62b2015-03-17 11:40:03 +0200819 *error_ppm = div_u64(1000000ULL *
820 abs(target_freq - calculated_clock->dot),
821 target_freq);
822 /*
823 * Prefer a better P value over a better (smaller) error if the error
824 * is small. Ensure this preference for future configurations too by
825 * setting the error to 0.
826 */
827 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
828 *error_ppm = 0;
829
830 return true;
831 }
832
833 return *error_ppm + 10 < best_error_ppm;
834}
835
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200836/*
837 * Returns a set of divisors for the desired target clock with the given
838 * refclk, or FALSE. The returned values represent the clock equation:
839 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
840 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800841static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300842vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200843 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300844 int target, int refclk, struct dpll *match_clock,
845 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700846{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200847 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300848 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300849 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300850 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300851 /* min update 19.2 MHz */
852 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300853 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700854
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300855 target *= 5; /* fast clock */
856
857 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700858
859 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300860 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300861 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300862 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300863 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300864 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700865 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300866 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200867 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300868
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300869 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
870 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300871
Imre Deakdccbea32015-06-22 23:35:51 +0300872 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300873
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100874 if (!intel_PLL_is_valid(to_i915(dev),
875 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300876 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300877 continue;
878
Imre Deakd5dd62b2015-03-17 11:40:03 +0200879 if (!vlv_PLL_is_optimal(dev, target,
880 &clock,
881 best_clock,
882 bestppm, &ppm))
883 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300884
Imre Deakd5dd62b2015-03-17 11:40:03 +0200885 *best_clock = clock;
886 bestppm = ppm;
887 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700888 }
889 }
890 }
891 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700892
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300893 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700894}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700895
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200896/*
897 * Returns a set of divisors for the desired target clock with the given
898 * refclk, or FALSE. The returned values represent the clock equation:
899 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
900 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300901static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300902chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200903 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300904 int target, int refclk, struct dpll *match_clock,
905 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300906{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200907 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300908 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200909 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300910 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300911 uint64_t m2;
912 int found = false;
913
914 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200915 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300916
917 /*
918 * Based on hardware doc, the n always set to 1, and m1 always
919 * set to 2. If requires to support 200Mhz refclk, we need to
920 * revisit this because n may not 1 anymore.
921 */
922 clock.n = 1, clock.m1 = 2;
923 target *= 5; /* fast clock */
924
925 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
926 for (clock.p2 = limit->p2.p2_fast;
927 clock.p2 >= limit->p2.p2_slow;
928 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200929 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300930
931 clock.p = clock.p1 * clock.p2;
932
933 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
934 clock.n) << 22, refclk * clock.m1);
935
936 if (m2 > INT_MAX/clock.m1)
937 continue;
938
939 clock.m2 = m2;
940
Imre Deakdccbea32015-06-22 23:35:51 +0300941 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300942
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100943 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300944 continue;
945
Imre Deak9ca3ba02015-03-17 11:40:05 +0200946 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
947 best_error_ppm, &error_ppm))
948 continue;
949
950 *best_clock = clock;
951 best_error_ppm = error_ppm;
952 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300953 }
954 }
955
956 return found;
957}
958
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200959bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300960 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200961{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200962 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300963 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200964
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200965 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200966 target_clock, refclk, NULL, best_clock);
967}
968
Ville Syrjälä525b9312016-10-31 22:37:02 +0200969bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300970{
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300971 /* Be paranoid as we can arrive here with only partial
972 * state retrieved from the hardware during setup.
973 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100974 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300975 * as Haswell has gained clock readout/fastboot support.
976 *
Ville Syrjäläcd30fbc2018-05-25 21:50:40 +0300977 * We can ditch the crtc->primary->state->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300978 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700979 *
980 * FIXME: The intel_crtc->active here should be switched to
981 * crtc->state->active once we have proper CRTC states wired up
982 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300983 */
Ville Syrjälä525b9312016-10-31 22:37:02 +0200984 return crtc->active && crtc->base.primary->state->fb &&
985 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300986}
987
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200988enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
989 enum pipe pipe)
990{
Ville Syrjälä98187832016-10-31 22:37:10 +0200991 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200992
Ville Syrjäläe2af48c2016-10-31 22:37:05 +0200993 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200994}
995
Ville Syrjälä8fedd642017-11-29 17:37:30 +0200996static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
997 enum pipe pipe)
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300998{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200999 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001000 u32 line1, line2;
1001 u32 line_mask;
1002
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001003 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001004 line_mask = DSL_LINEMASK_GEN2;
1005 else
1006 line_mask = DSL_LINEMASK_GEN3;
1007
1008 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001009 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001010 line2 = I915_READ(reg) & line_mask;
1011
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001012 return line1 != line2;
1013}
1014
1015static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1016{
1017 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1018 enum pipe pipe = crtc->pipe;
1019
1020 /* Wait for the display line to settle/start moving */
1021 if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1022 DRM_ERROR("pipe %c scanline %s wait timed out\n",
1023 pipe_name(pipe), onoff(state));
1024}
1025
1026static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1027{
1028 wait_for_pipe_scanline_moving(crtc, false);
1029}
1030
1031static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1032{
1033 wait_for_pipe_scanline_moving(crtc, true);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001034}
1035
Ville Syrjälä4972f702017-11-29 17:37:32 +02001036static void
1037intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001038{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001039 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001040 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001041
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001042 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001043 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001044 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001045
Keith Packardab7ad7f2010-10-03 00:33:06 -07001046 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001047 if (intel_wait_for_register(dev_priv,
1048 reg, I965_PIPECONF_ACTIVE, 0,
1049 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001050 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001051 } else {
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001052 intel_wait_for_pipe_scanline_stopped(crtc);
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001054}
1055
Jesse Barnesb24e7172011-01-04 15:09:30 -08001056/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001057void assert_pll(struct drm_i915_private *dev_priv,
1058 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001059{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001060 u32 val;
1061 bool cur_state;
1062
Ville Syrjälä649636e2015-09-22 19:50:01 +03001063 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001064 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001065 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001066 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001067 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001068}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001069
Jani Nikula23538ef2013-08-27 15:12:22 +03001070/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001071void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001072{
1073 u32 val;
1074 bool cur_state;
1075
Ville Syrjäläa5805162015-05-26 20:42:30 +03001076 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001077 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001078 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001079
1080 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001081 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001082 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001083 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001084}
Jani Nikula23538ef2013-08-27 15:12:22 +03001085
Jesse Barnes040484a2011-01-03 12:14:26 -08001086static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1087 enum pipe pipe, bool state)
1088{
Jesse Barnes040484a2011-01-03 12:14:26 -08001089 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001090 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1091 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001092
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001093 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001094 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001095 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001096 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001097 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001098 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001099 cur_state = !!(val & FDI_TX_ENABLE);
1100 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001101 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001102 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001103 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001104}
1105#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1106#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1107
1108static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1109 enum pipe pipe, bool state)
1110{
Jesse Barnes040484a2011-01-03 12:14:26 -08001111 u32 val;
1112 bool cur_state;
1113
Ville Syrjälä649636e2015-09-22 19:50:01 +03001114 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001115 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001116 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001117 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001118 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001119}
1120#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1121#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1122
1123static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1124 enum pipe pipe)
1125{
Jesse Barnes040484a2011-01-03 12:14:26 -08001126 u32 val;
1127
1128 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001129 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001130 return;
1131
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001132 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001133 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001134 return;
1135
Ville Syrjälä649636e2015-09-22 19:50:01 +03001136 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001137 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 -08001138}
1139
Daniel Vetter55607e82013-06-16 21:42:39 +02001140void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1141 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001142{
Jesse Barnes040484a2011-01-03 12:14:26 -08001143 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001144 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001145
Ville Syrjälä649636e2015-09-22 19:50:01 +03001146 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001147 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001148 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001149 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001150 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001151}
1152
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001153void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001154{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001155 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001156 u32 val;
Ville Syrjälä10ed55e2018-05-23 17:57:18 +03001157 enum pipe panel_pipe = INVALID_PIPE;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001158 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001159
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001160 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001161 return;
1162
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001163 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001164 u32 port_sel;
1165
Imre Deak44cb7342016-08-10 14:07:29 +03001166 pp_reg = PP_CONTROL(0);
1167 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001168
Ville Syrjälä4c23dea2018-05-18 18:29:30 +03001169 switch (port_sel) {
1170 case PANEL_PORT_SELECT_LVDS:
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001171 intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
Ville Syrjälä4c23dea2018-05-18 18:29:30 +03001172 break;
1173 case PANEL_PORT_SELECT_DPA:
1174 intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1175 break;
1176 case PANEL_PORT_SELECT_DPC:
1177 intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1178 break;
1179 case PANEL_PORT_SELECT_DPD:
1180 intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1181 break;
1182 default:
1183 MISSING_CASE(port_sel);
1184 break;
1185 }
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001186 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001187 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001188 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001189 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001190 } else {
Ville Syrjäläf0d2b752018-05-18 18:29:31 +03001191 u32 port_sel;
1192
Imre Deak44cb7342016-08-10 14:07:29 +03001193 pp_reg = PP_CONTROL(0);
Ville Syrjäläf0d2b752018-05-18 18:29:31 +03001194 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1195
1196 WARN_ON(port_sel != PANEL_PORT_SELECT_LVDS);
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001197 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
Jesse Barnesea0760c2011-01-04 15:09:32 -08001198 }
1199
1200 val = I915_READ(pp_reg);
1201 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001202 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001203 locked = false;
1204
Rob Clarke2c719b2014-12-15 13:56:32 -05001205 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001206 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001207 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001208}
1209
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001210void assert_pipe(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001212{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001213 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001214 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1215 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001216 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001217
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001218 /* we keep both pipes enabled on 830 */
1219 if (IS_I830(dev_priv))
Daniel Vetter8e636782012-01-22 01:36:48 +01001220 state = true;
1221
Imre Deak4feed0e2016-02-12 18:55:14 +02001222 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1223 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001224 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001225 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001226
1227 intel_display_power_put(dev_priv, power_domain);
1228 } else {
1229 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001230 }
1231
Rob Clarke2c719b2014-12-15 13:56:32 -05001232 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001233 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001234 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001235}
1236
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001237static void assert_plane(struct intel_plane *plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001238{
Ville Syrjäläeade6c82018-01-30 22:38:03 +02001239 enum pipe pipe;
1240 bool cur_state;
1241
1242 cur_state = plane->get_hw_state(plane, &pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001243
Rob Clarke2c719b2014-12-15 13:56:32 -05001244 I915_STATE_WARN(cur_state != state,
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001245 "%s assertion failure (expected %s, current %s)\n",
1246 plane->base.name, onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001247}
1248
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001249#define assert_plane_enabled(p) assert_plane(p, true)
1250#define assert_plane_disabled(p) assert_plane(p, false)
Chris Wilson931872f2012-01-16 23:01:13 +00001251
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001252static void assert_planes_disabled(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001253{
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001254 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1255 struct intel_plane *plane;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001256
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001257 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1258 assert_plane_disabled(plane);
Jesse Barnes19332d72013-03-28 09:55:38 -07001259}
1260
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001261static void assert_vblank_disabled(struct drm_crtc *crtc)
1262{
Rob Clarke2c719b2014-12-15 13:56:32 -05001263 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001264 drm_crtc_vblank_put(crtc);
1265}
1266
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001267void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1268 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001269{
Jesse Barnes92f25842011-01-04 15:09:34 -08001270 u32 val;
1271 bool enabled;
1272
Ville Syrjälä649636e2015-09-22 19:50:01 +03001273 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001274 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001275 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001276 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1277 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001278}
1279
Jesse Barnes291906f2011-02-02 12:28:03 -08001280static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001281 enum pipe pipe, enum port port,
1282 i915_reg_t dp_reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001283{
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001284 enum pipe port_pipe;
1285 bool state;
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001286
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001287 state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1288
1289 I915_STATE_WARN(state && port_pipe == pipe,
1290 "PCH DP %c enabled on transcoder %c, should be disabled\n",
1291 port_name(port), pipe_name(pipe));
1292
1293 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1294 "IBX PCH DP %c still using transcoder B\n",
1295 port_name(port));
Jesse Barnes291906f2011-02-02 12:28:03 -08001296}
1297
1298static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjälä76203462018-05-14 20:24:21 +03001299 enum pipe pipe, enum port port,
1300 i915_reg_t hdmi_reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001301{
Ville Syrjälä76203462018-05-14 20:24:21 +03001302 enum pipe port_pipe;
1303 bool state;
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001304
Ville Syrjälä76203462018-05-14 20:24:21 +03001305 state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1306
1307 I915_STATE_WARN(state && port_pipe == pipe,
1308 "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1309 port_name(port), pipe_name(pipe));
1310
1311 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1312 "IBX PCH HDMI %c still using transcoder B\n",
1313 port_name(port));
Jesse Barnes291906f2011-02-02 12:28:03 -08001314}
1315
1316static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1317 enum pipe pipe)
1318{
Ville Syrjälä6102a8e2018-05-14 20:24:19 +03001319 enum pipe port_pipe;
Jesse Barnes291906f2011-02-02 12:28:03 -08001320
Ville Syrjälä59b74c42018-05-18 18:29:28 +03001321 assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1322 assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1323 assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001324
Ville Syrjälä6102a8e2018-05-14 20:24:19 +03001325 I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1326 port_pipe == pipe,
1327 "PCH VGA enabled on transcoder %c, should be disabled\n",
1328 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001329
Ville Syrjäläa44628b2018-05-14 21:28:27 +03001330 I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1331 port_pipe == pipe,
1332 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1333 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001334
Ville Syrjälä76203462018-05-14 20:24:21 +03001335 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1336 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1337 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001338}
1339
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001340static void _vlv_enable_pll(struct intel_crtc *crtc,
1341 const struct intel_crtc_state *pipe_config)
1342{
1343 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1344 enum pipe pipe = crtc->pipe;
1345
1346 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1347 POSTING_READ(DPLL(pipe));
1348 udelay(150);
1349
Chris Wilson2c30b432016-06-30 15:32:54 +01001350 if (intel_wait_for_register(dev_priv,
1351 DPLL(pipe),
1352 DPLL_LOCK_VLV,
1353 DPLL_LOCK_VLV,
1354 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001355 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1356}
1357
Ville Syrjäläd288f652014-10-28 13:20:22 +02001358static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001359 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001360{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001361 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001362 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001363
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001364 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001365
Daniel Vetter87442f72013-06-06 00:52:17 +02001366 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001367 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001368
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001369 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1370 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001371
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001372 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1373 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001374}
1375
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001376
1377static void _chv_enable_pll(struct intel_crtc *crtc,
1378 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001379{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001380 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001381 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001382 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001383 u32 tmp;
1384
Ville Syrjäläa5805162015-05-26 20:42:30 +03001385 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001386
1387 /* Enable back the 10bit clock to display controller */
1388 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1389 tmp |= DPIO_DCLKP_EN;
1390 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1391
Ville Syrjälä54433e92015-05-26 20:42:31 +03001392 mutex_unlock(&dev_priv->sb_lock);
1393
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001394 /*
1395 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1396 */
1397 udelay(1);
1398
1399 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001400 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001401
1402 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001403 if (intel_wait_for_register(dev_priv,
1404 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1405 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001406 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001407}
1408
1409static void chv_enable_pll(struct intel_crtc *crtc,
1410 const struct intel_crtc_state *pipe_config)
1411{
1412 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1413 enum pipe pipe = crtc->pipe;
1414
1415 assert_pipe_disabled(dev_priv, pipe);
1416
1417 /* PLL is protected by panel, make sure we can write it */
1418 assert_panel_unlocked(dev_priv, pipe);
1419
1420 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1421 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001422
Ville Syrjäläc2317752016-03-15 16:39:56 +02001423 if (pipe != PIPE_A) {
1424 /*
1425 * WaPixelRepeatModeFixForC0:chv
1426 *
1427 * DPLLCMD is AWOL. Use chicken bits to propagate
1428 * the value from DPLLBMD to either pipe B or C.
1429 */
Ville Syrjälädfa311f2017-09-13 17:08:54 +03001430 I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
Ville Syrjäläc2317752016-03-15 16:39:56 +02001431 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1432 I915_WRITE(CBR4_VLV, 0);
1433 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1434
1435 /*
1436 * DPLLB VGA mode also seems to cause problems.
1437 * We should always have it disabled.
1438 */
1439 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1440 } else {
1441 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1442 POSTING_READ(DPLL_MD(pipe));
1443 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001444}
1445
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001446static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001447{
1448 struct intel_crtc *crtc;
1449 int count = 0;
1450
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001451 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001452 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001453 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1454 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001455
1456 return count;
1457}
1458
Ville Syrjälä939994d2017-09-13 17:08:56 +03001459static void i9xx_enable_pll(struct intel_crtc *crtc,
1460 const struct intel_crtc_state *crtc_state)
Daniel Vetter87442f72013-06-06 00:52:17 +02001461{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001462 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001463 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjälä939994d2017-09-13 17:08:56 +03001464 u32 dpll = crtc_state->dpll_hw_state.dpll;
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001465 int i;
Daniel Vetter87442f72013-06-06 00:52:17 +02001466
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001467 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001468
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001469 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001470 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001471 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001472
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001473 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001474 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001475 /*
1476 * It appears to be important that we don't enable this
1477 * for the current pipe before otherwise configuring the
1478 * PLL. No idea how this should be handled if multiple
1479 * DVO outputs are enabled simultaneosly.
1480 */
1481 dpll |= DPLL_DVO_2X_MODE;
1482 I915_WRITE(DPLL(!crtc->pipe),
1483 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1484 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001485
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001486 /*
1487 * Apparently we need to have VGA mode enabled prior to changing
1488 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1489 * dividers, even though the register value does change.
1490 */
1491 I915_WRITE(reg, 0);
1492
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001493 I915_WRITE(reg, dpll);
1494
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001495 /* Wait for the clocks to stabilize. */
1496 POSTING_READ(reg);
1497 udelay(150);
1498
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001499 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001500 I915_WRITE(DPLL_MD(crtc->pipe),
Ville Syrjälä939994d2017-09-13 17:08:56 +03001501 crtc_state->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001502 } else {
1503 /* The pixel multiplier can only be updated once the
1504 * DPLL is enabled and the clocks are stable.
1505 *
1506 * So write it again.
1507 */
1508 I915_WRITE(reg, dpll);
1509 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001510
1511 /* We do this three times for luck */
Ville Syrjäläbb408dd2017-06-01 17:36:15 +03001512 for (i = 0; i < 3; i++) {
1513 I915_WRITE(reg, dpll);
1514 POSTING_READ(reg);
1515 udelay(150); /* wait for warmup */
1516 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001517}
1518
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02001519static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001520{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02001521 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001522 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001523 enum pipe pipe = crtc->pipe;
1524
1525 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001526 if (IS_I830(dev_priv) &&
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02001527 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001528 !intel_num_dvo_pipes(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001529 I915_WRITE(DPLL(PIPE_B),
1530 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1531 I915_WRITE(DPLL(PIPE_A),
1532 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1533 }
1534
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001535 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001536 if (IS_I830(dev_priv))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001537 return;
1538
1539 /* Make sure the pipe isn't still relying on us */
1540 assert_pipe_disabled(dev_priv, pipe);
1541
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001542 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001543 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001544}
1545
Jesse Barnesf6071162013-10-01 10:41:38 -07001546static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1547{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001548 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001549
1550 /* Make sure the pipe isn't still relying on us */
1551 assert_pipe_disabled(dev_priv, pipe);
1552
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001553 val = DPLL_INTEGRATED_REF_CLK_VLV |
1554 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1555 if (pipe != PIPE_A)
1556 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1557
Jesse Barnesf6071162013-10-01 10:41:38 -07001558 I915_WRITE(DPLL(pipe), val);
1559 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001560}
1561
1562static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1563{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001564 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001565 u32 val;
1566
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001567 /* Make sure the pipe isn't still relying on us */
1568 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001569
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001570 val = DPLL_SSC_REF_CLK_CHV |
1571 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001572 if (pipe != PIPE_A)
1573 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001574
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001575 I915_WRITE(DPLL(pipe), val);
1576 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001577
Ville Syrjäläa5805162015-05-26 20:42:30 +03001578 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001579
1580 /* Disable 10bit clock to display controller */
1581 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1582 val &= ~DPIO_DCLKP_EN;
1583 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1584
Ville Syrjäläa5805162015-05-26 20:42:30 +03001585 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001586}
1587
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001588void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001589 struct intel_digital_port *dport,
1590 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001591{
1592 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001593 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001594
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02001595 switch (dport->base.port) {
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001596 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001597 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001598 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001599 break;
1600 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001601 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001602 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001603 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001604 break;
1605 case PORT_D:
1606 port_mask = DPLL_PORTD_READY_MASK;
1607 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001608 break;
1609 default:
1610 BUG();
1611 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001612
Chris Wilson370004d2016-06-30 15:32:56 +01001613 if (intel_wait_for_register(dev_priv,
1614 dpll_reg, port_mask, expected_mask,
1615 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001616 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 +02001617 port_name(dport->base.port),
1618 I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001619}
1620
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001621static void ironlake_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001622{
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001623 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1624 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1625 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001626 i915_reg_t reg;
1627 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001628
Jesse Barnes040484a2011-01-03 12:14:26 -08001629 /* Make sure PCH DPLL is enabled */
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001630 assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001631
1632 /* FDI must be feeding us bits for PCH ports */
1633 assert_fdi_tx_enabled(dev_priv, pipe);
1634 assert_fdi_rx_enabled(dev_priv, pipe);
1635
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001636 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001637 /* Workaround: Set the timing override bit before enabling the
1638 * pch transcoder. */
1639 reg = TRANS_CHICKEN2(pipe);
1640 val = I915_READ(reg);
1641 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1642 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001643 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001644
Daniel Vetterab9412b2013-05-03 11:49:46 +02001645 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001646 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001647 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001648
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001649 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001650 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001651 * Make the BPC in transcoder be consistent with
1652 * that in pipeconf reg. For HDMI we must use 8bpc
1653 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001654 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001655 val &= ~PIPECONF_BPC_MASK;
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001656 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001657 val |= PIPECONF_8BPC;
1658 else
1659 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001660 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001661
1662 val &= ~TRANS_INTERLACE_MASK;
1663 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001664 if (HAS_PCH_IBX(dev_priv) &&
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02001665 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001666 val |= TRANS_LEGACY_INTERLACED_ILK;
1667 else
1668 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001669 else
1670 val |= TRANS_PROGRESSIVE;
1671
Jesse Barnes040484a2011-01-03 12:14:26 -08001672 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001673 if (intel_wait_for_register(dev_priv,
1674 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1675 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001676 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001677}
1678
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001679static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001680 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001681{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001682 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001683
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001684 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001685 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001686 assert_fdi_rx_enabled(dev_priv, PIPE_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001687
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001688 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001689 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001690 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001691 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001692
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001693 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001694 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001695
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001696 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1697 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001698 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001699 else
1700 val |= TRANS_PROGRESSIVE;
1701
Daniel Vetterab9412b2013-05-03 11:49:46 +02001702 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001703 if (intel_wait_for_register(dev_priv,
1704 LPT_TRANSCONF,
1705 TRANS_STATE_ENABLE,
1706 TRANS_STATE_ENABLE,
1707 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001708 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001709}
1710
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001711static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1712 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001713{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001714 i915_reg_t reg;
1715 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001716
1717 /* FDI relies on the transcoder */
1718 assert_fdi_tx_disabled(dev_priv, pipe);
1719 assert_fdi_rx_disabled(dev_priv, pipe);
1720
Jesse Barnes291906f2011-02-02 12:28:03 -08001721 /* Ports must be off as well */
1722 assert_pch_ports_disabled(dev_priv, pipe);
1723
Daniel Vetterab9412b2013-05-03 11:49:46 +02001724 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001725 val = I915_READ(reg);
1726 val &= ~TRANS_ENABLE;
1727 I915_WRITE(reg, val);
1728 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001729 if (intel_wait_for_register(dev_priv,
1730 reg, TRANS_STATE_ENABLE, 0,
1731 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001732 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001733
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001734 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001735 /* Workaround: Clear the timing override chicken bit again. */
1736 reg = TRANS_CHICKEN2(pipe);
1737 val = I915_READ(reg);
1738 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1739 I915_WRITE(reg, val);
1740 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001741}
1742
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001743void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001744{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001745 u32 val;
1746
Daniel Vetterab9412b2013-05-03 11:49:46 +02001747 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001748 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001749 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001750 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001751 if (intel_wait_for_register(dev_priv,
1752 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1753 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001754 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001755
1756 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001757 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001758 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001759 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001760}
1761
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001762enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
Ville Syrjälä65f21302016-10-14 20:02:53 +03001763{
1764 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1765
Ville Syrjälä65f21302016-10-14 20:02:53 +03001766 if (HAS_PCH_LPT(dev_priv))
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001767 return PIPE_A;
Ville Syrjälä65f21302016-10-14 20:02:53 +03001768 else
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001769 return crtc->pipe;
Ville Syrjälä65f21302016-10-14 20:02:53 +03001770}
1771
Ville Syrjälä4972f702017-11-29 17:37:32 +02001772static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001773{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001774 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
1775 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1776 enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
Paulo Zanoni03722642014-01-17 13:51:09 -02001777 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001778 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001779 u32 val;
1780
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001781 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1782
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001783 assert_planes_disabled(crtc);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001784
Jesse Barnesb24e7172011-01-04 15:09:30 -08001785 /*
1786 * A pipe without a PLL won't actually be able to drive bits from
1787 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1788 * need the check.
1789 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001790 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001791 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001792 assert_dsi_pll_enabled(dev_priv);
1793 else
1794 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001795 } else {
Ville Syrjälä4972f702017-11-29 17:37:32 +02001796 if (new_crtc_state->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001797 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001798 assert_fdi_rx_pll_enabled(dev_priv,
Matthias Kaehlckea2196032017-07-17 11:14:03 -07001799 intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001800 assert_fdi_tx_pll_enabled(dev_priv,
1801 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001802 }
1803 /* FIXME: assert CPU port conditions for SNB+ */
1804 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001805
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001806 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001807 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001808 if (val & PIPECONF_ENABLE) {
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001809 /* we keep both pipes enabled on 830 */
1810 WARN_ON(!IS_I830(dev_priv));
Chris Wilson00d70b12011-03-17 07:18:29 +00001811 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001812 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001813
1814 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001815 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001816
1817 /*
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001818 * Until the pipe starts PIPEDSL reads will return a stale value,
1819 * which causes an apparent vblank timestamp jump when PIPEDSL
1820 * resets to its proper value. That also messes up the frame count
1821 * when it's derived from the timestamps. So let's wait for the
1822 * pipe to start properly before we call drm_crtc_vblank_on()
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001823 */
Ville Syrjälä4972f702017-11-29 17:37:32 +02001824 if (dev_priv->drm.max_vblank_count == 0)
Ville Syrjälä8fedd642017-11-29 17:37:30 +02001825 intel_wait_for_pipe_scanline_moving(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001826}
1827
Ville Syrjälä4972f702017-11-29 17:37:32 +02001828static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001829{
Ville Syrjälä4972f702017-11-29 17:37:32 +02001830 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Chris Wilsonfac5e232016-07-04 11:34:36 +01001831 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä4972f702017-11-29 17:37:32 +02001832 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001833 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001834 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001835 u32 val;
1836
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001837 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1838
Jesse Barnesb24e7172011-01-04 15:09:30 -08001839 /*
1840 * Make sure planes won't keep trying to pump pixels to us,
1841 * or we might hang the display.
1842 */
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02001843 assert_planes_disabled(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001844
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001845 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001846 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001847 if ((val & PIPECONF_ENABLE) == 0)
1848 return;
1849
Ville Syrjälä67adc642014-08-15 01:21:57 +03001850 /*
1851 * Double wide has implications for planes
1852 * so best keep it disabled when not needed.
1853 */
Ville Syrjälä4972f702017-11-29 17:37:32 +02001854 if (old_crtc_state->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03001855 val &= ~PIPECONF_DOUBLE_WIDE;
1856
1857 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläe56134b2017-06-01 17:36:19 +03001858 if (!IS_I830(dev_priv))
Ville Syrjälä67adc642014-08-15 01:21:57 +03001859 val &= ~PIPECONF_ENABLE;
1860
1861 I915_WRITE(reg, val);
1862 if ((val & PIPECONF_ENABLE) == 0)
Ville Syrjälä4972f702017-11-29 17:37:32 +02001863 intel_wait_for_pipe_off(old_crtc_state);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001864}
1865
Ville Syrjälä832be822016-01-12 21:08:33 +02001866static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1867{
1868 return IS_GEN2(dev_priv) ? 2048 : 4096;
1869}
1870
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001871static unsigned int
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001872intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001873{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001874 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001875 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001876
1877 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07001878 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001879 return cpp;
1880 case I915_FORMAT_MOD_X_TILED:
1881 if (IS_GEN2(dev_priv))
1882 return 128;
1883 else
1884 return 512;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001885 case I915_FORMAT_MOD_Y_TILED_CCS:
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001886 if (color_plane == 1)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001887 return 128;
1888 /* fall through */
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001889 case I915_FORMAT_MOD_Y_TILED:
1890 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
1891 return 128;
1892 else
1893 return 512;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001894 case I915_FORMAT_MOD_Yf_TILED_CCS:
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001895 if (color_plane == 1)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07001896 return 128;
1897 /* fall through */
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001898 case I915_FORMAT_MOD_Yf_TILED:
1899 switch (cpp) {
1900 case 1:
1901 return 64;
1902 case 2:
1903 case 4:
1904 return 128;
1905 case 8:
1906 case 16:
1907 return 256;
1908 default:
1909 MISSING_CASE(cpp);
1910 return cpp;
1911 }
1912 break;
1913 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001914 MISSING_CASE(fb->modifier);
Ville Syrjälä7b49f942016-01-12 21:08:32 +02001915 return cpp;
1916 }
1917}
1918
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001919static unsigned int
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001920intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08001921{
Ben Widawsky2f075562017-03-24 14:29:48 -07001922 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä832be822016-01-12 21:08:33 +02001923 return 1;
1924 else
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001925 return intel_tile_size(to_i915(fb->dev)) /
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001926 intel_tile_width_bytes(fb, color_plane);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001927}
1928
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001929/* Return the tile dimensions in pixel units */
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001930static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001931 unsigned int *tile_width,
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001932 unsigned int *tile_height)
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001933{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001934 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
1935 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001936
1937 *tile_width = tile_width_bytes / cpp;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001938 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02001939}
1940
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001941unsigned int
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001942intel_fb_align_height(const struct drm_framebuffer *fb,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001943 int color_plane, unsigned int height)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00001944{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001945 unsigned int tile_height = intel_tile_height(fb, color_plane);
Ville Syrjälä832be822016-01-12 21:08:33 +02001946
1947 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08001948}
1949
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02001950unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
1951{
1952 unsigned int size = 0;
1953 int i;
1954
1955 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
1956 size += rot_info->plane[i].width * rot_info->plane[i].height;
1957
1958 return size;
1959}
1960
Daniel Vetter75c82a52015-10-14 16:51:04 +02001961static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02001962intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
1963 const struct drm_framebuffer *fb,
1964 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00001965{
Chris Wilson7b92c042017-01-14 00:28:26 +00001966 view->type = I915_GGTT_VIEW_NORMAL;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03001967 if (drm_rotation_90_or_270(rotation)) {
Chris Wilson7b92c042017-01-14 00:28:26 +00001968 view->type = I915_GGTT_VIEW_ROTATED;
Chris Wilson8bab11932017-01-14 00:28:25 +00001969 view->rotated = to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02001970 }
1971}
1972
Ville Syrjäläfabac482017-03-27 21:55:43 +03001973static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
1974{
1975 if (IS_I830(dev_priv))
1976 return 16 * 1024;
1977 else if (IS_I85X(dev_priv))
1978 return 256;
Ville Syrjäläd9e15512017-03-27 21:55:45 +03001979 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
1980 return 32;
Ville Syrjäläfabac482017-03-27 21:55:43 +03001981 else
1982 return 4 * 1024;
1983}
1984
Ville Syrjälä603525d2016-01-12 21:08:37 +02001985static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001986{
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00001987 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001988 return 256 * 1024;
Jani Nikulac0f86832016-12-07 12:13:04 +02001989 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08001990 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001991 return 128 * 1024;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00001992 else if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001993 return 4 * 1024;
1994 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03001995 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03001996}
1997
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02001998static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03001999 int color_plane)
Ville Syrjälä603525d2016-01-12 21:08:37 +02002000{
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002001 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2002
Ville Syrjäläb90c1ee2017-03-07 21:42:07 +02002003 /* AUX_DIST needs only 4K alignment */
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002004 if (color_plane == 1)
Ville Syrjäläb90c1ee2017-03-07 21:42:07 +02002005 return 4096;
2006
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002007 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07002008 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjälä603525d2016-01-12 21:08:37 +02002009 return intel_linear_alignment(dev_priv);
2010 case I915_FORMAT_MOD_X_TILED:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002011 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä603525d2016-01-12 21:08:37 +02002012 return 256 * 1024;
2013 return 0;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002014 case I915_FORMAT_MOD_Y_TILED_CCS:
2015 case I915_FORMAT_MOD_Yf_TILED_CCS:
Ville Syrjälä603525d2016-01-12 21:08:37 +02002016 case I915_FORMAT_MOD_Y_TILED:
2017 case I915_FORMAT_MOD_Yf_TILED:
2018 return 1 * 1024 * 1024;
2019 default:
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002020 MISSING_CASE(fb->modifier);
Ville Syrjälä603525d2016-01-12 21:08:37 +02002021 return 0;
2022 }
2023}
2024
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002025static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2026{
2027 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2028 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2029
Ville Syrjälä32febd92018-02-21 18:02:33 +02002030 return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002031}
2032
Chris Wilson058d88c2016-08-15 10:49:06 +01002033struct i915_vma *
Chris Wilson59354852018-02-20 13:42:06 +00002034intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002035 const struct i915_ggtt_view *view,
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002036 bool uses_fence,
Chris Wilson59354852018-02-20 13:42:06 +00002037 unsigned long *out_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002038{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002039 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002040 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002041 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Chris Wilson058d88c2016-08-15 10:49:06 +01002042 struct i915_vma *vma;
Chris Wilson59354852018-02-20 13:42:06 +00002043 unsigned int pinctl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002044 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002045
Matt Roperebcdd392014-07-09 16:22:11 -07002046 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2047
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002048 alignment = intel_surf_alignment(fb, 0);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002049
Chris Wilson693db182013-03-05 14:52:39 +00002050 /* Note that the w/a also requires 64 PTE of padding following the
2051 * bo. We currently fill all unused PTE with the shadow page and so
2052 * we should always have valid PTE following the scanout preventing
2053 * the VT-d warning.
2054 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002055 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002056 alignment = 256 * 1024;
2057
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002058 /*
2059 * Global gtt pte registers are special registers which actually forward
2060 * writes to a chunk of system memory. Which means that there is no risk
2061 * that the register values disappear as soon as we call
2062 * intel_runtime_pm_put(), so it is correct to wrap only the
2063 * pin/unpin/fence and not more.
2064 */
2065 intel_runtime_pm_get(dev_priv);
2066
Daniel Vetter9db529a2017-08-08 10:08:28 +02002067 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2068
Chris Wilson59354852018-02-20 13:42:06 +00002069 pinctl = 0;
2070
2071 /* Valleyview is definitely limited to scanning out the first
2072 * 512MiB. Lets presume this behaviour was inherited from the
2073 * g4x display engine and that all earlier gen are similarly
2074 * limited. Testing suggests that it is a little more
2075 * complicated than this. For example, Cherryview appears quite
2076 * happy to scanout from anywhere within its global aperture.
2077 */
2078 if (HAS_GMCH_DISPLAY(dev_priv))
2079 pinctl |= PIN_MAPPABLE;
2080
2081 vma = i915_gem_object_pin_to_display_plane(obj,
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002082 alignment, view, pinctl);
Chris Wilson49ef5292016-08-18 17:17:00 +01002083 if (IS_ERR(vma))
2084 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002085
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002086 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002087 int ret;
2088
Chris Wilson49ef5292016-08-18 17:17:00 +01002089 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2090 * fence, whereas 965+ only requires a fence if using
2091 * framebuffer compression. For simplicity, we always, when
2092 * possible, install a fence as the cost is not that onerous.
2093 *
2094 * If we fail to fence the tiled scanout, then either the
2095 * modeset will reject the change (which is highly unlikely as
2096 * the affected systems, all but one, do not have unmappable
2097 * space) or we will not be able to enable full powersaving
2098 * techniques (also likely not to apply due to various limits
2099 * FBC and the like impose on the size of the buffer, which
2100 * presumably we violated anyway with this unmappable buffer).
2101 * Anyway, it is presumably better to stumble onwards with
2102 * something and try to run the system in a "less than optimal"
2103 * mode that matches the user configuration.
2104 */
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002105 ret = i915_vma_pin_fence(vma);
2106 if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
Chris Wilson75097022018-03-05 10:33:12 +00002107 i915_gem_object_unpin_from_display_plane(vma);
Ville Syrjälä85798ac2018-02-21 18:02:30 +02002108 vma = ERR_PTR(ret);
2109 goto err;
2110 }
2111
2112 if (ret == 0 && vma->fence)
Chris Wilson59354852018-02-20 13:42:06 +00002113 *out_flags |= PLANE_HAS_FENCE;
Vivek Kasireddy98072162015-10-29 18:54:38 -07002114 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002115
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002116 i915_vma_get(vma);
Chris Wilson49ef5292016-08-18 17:17:00 +01002117err:
Daniel Vetter9db529a2017-08-08 10:08:28 +02002118 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2119
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002120 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002121 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002122}
2123
Chris Wilson59354852018-02-20 13:42:06 +00002124void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002125{
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002126 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002127
Chris Wilson59354852018-02-20 13:42:06 +00002128 if (flags & PLANE_HAS_FENCE)
2129 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002130 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002131 i915_vma_put(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002132}
2133
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002134static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002135 unsigned int rotation)
2136{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002137 if (drm_rotation_90_or_270(rotation))
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002138 return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002139 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002140 return fb->pitches[color_plane];
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002141}
2142
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002143/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002144 * Convert the x/y offsets into a linear offset.
2145 * Only valid with 0/180 degree rotation, which is fine since linear
2146 * offset is only used with linear buffers on pre-hsw and tiled buffers
2147 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2148 */
2149u32 intel_fb_xy_to_linear(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{
Ville Syrjälä29490562016-01-20 18:02:50 +02002153 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002154 unsigned int cpp = fb->format->cpp[color_plane];
2155 unsigned int pitch = state->color_plane[color_plane].stride;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002156
2157 return y * pitch + x * cpp;
2158}
2159
2160/*
2161 * Add the x/y offsets derived from fb->offsets[] to the user
2162 * specified plane src x/y offsets. The resulting x/y offsets
2163 * specify the start of scanout from the beginning of the gtt mapping.
2164 */
2165void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002166 const struct intel_plane_state *state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002167 int color_plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002168
2169{
Ville Syrjälä29490562016-01-20 18:02:50 +02002170 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2171 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002172
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002173 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002174 *x += intel_fb->rotated[color_plane].x;
2175 *y += intel_fb->rotated[color_plane].y;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002176 } else {
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002177 *x += intel_fb->normal[color_plane].x;
2178 *y += intel_fb->normal[color_plane].y;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002179 }
2180}
2181
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002182static u32 intel_adjust_tile_offset(int *x, int *y,
2183 unsigned int tile_width,
2184 unsigned int tile_height,
2185 unsigned int tile_size,
2186 unsigned int pitch_tiles,
2187 u32 old_offset,
2188 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002189{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002190 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002191 unsigned int tiles;
2192
2193 WARN_ON(old_offset & (tile_size - 1));
2194 WARN_ON(new_offset & (tile_size - 1));
2195 WARN_ON(new_offset > old_offset);
2196
2197 tiles = (old_offset - new_offset) / tile_size;
2198
2199 *y += tiles / pitch_tiles * tile_height;
2200 *x += tiles % pitch_tiles * tile_width;
2201
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002202 /* minimize x in case it got needlessly big */
2203 *y += *x / pitch_pixels * tile_height;
2204 *x %= pitch_pixels;
2205
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002206 return new_offset;
2207}
2208
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002209static u32 intel_adjust_aligned_offset(int *x, int *y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002210 const struct drm_framebuffer *fb,
2211 int color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002212 unsigned int rotation,
Ville Syrjälädf79cf42018-09-11 18:01:39 +03002213 unsigned int pitch,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002214 u32 old_offset, u32 new_offset)
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002215{
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002216 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002217 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002218
2219 WARN_ON(new_offset > old_offset);
2220
Ben Widawsky2f075562017-03-24 14:29:48 -07002221 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002222 unsigned int tile_size, tile_width, tile_height;
2223 unsigned int pitch_tiles;
2224
2225 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002226 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002227
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002228 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002229 pitch_tiles = pitch / tile_height;
2230 swap(tile_width, tile_height);
2231 } else {
2232 pitch_tiles = pitch / (tile_width * cpp);
2233 }
2234
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002235 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2236 tile_size, pitch_tiles,
2237 old_offset, new_offset);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002238 } else {
2239 old_offset += *y * pitch + *x * cpp;
2240
2241 *y = (old_offset - new_offset) / pitch;
2242 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2243 }
2244
2245 return new_offset;
2246}
2247
2248/*
Ville Syrjälä303ba692017-08-24 22:10:49 +03002249 * Adjust the tile offset by moving the difference into
2250 * the x/y offsets.
2251 */
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002252static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2253 const struct intel_plane_state *state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002254 int color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002255 u32 old_offset, u32 new_offset)
Ville Syrjälä303ba692017-08-24 22:10:49 +03002256{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002257 return intel_adjust_aligned_offset(x, y, state->base.fb, color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002258 state->base.rotation,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002259 state->color_plane[color_plane].stride,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002260 old_offset, new_offset);
Ville Syrjälä303ba692017-08-24 22:10:49 +03002261}
2262
2263/*
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002264 * Computes the aligned offset to the base tile and adjusts
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002265 * x, y. bytes per pixel is assumed to be a power-of-two.
2266 *
2267 * In the 90/270 rotated case, x and y are assumed
2268 * to be already rotated to match the rotated GTT view, and
2269 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002270 *
2271 * This function is used when computing the derived information
2272 * under intel_framebuffer, so using any of that information
2273 * here is not allowed. Anything under drm_framebuffer can be
2274 * used. This is why the user has to pass in the pitch since it
2275 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002276 */
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002277static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2278 int *x, int *y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002279 const struct drm_framebuffer *fb,
2280 int color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002281 unsigned int pitch,
2282 unsigned int rotation,
2283 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002284{
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002285 uint64_t fb_modifier = fb->modifier;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002286 unsigned int cpp = fb->format->cpp[color_plane];
Ville Syrjälä6687c902015-09-15 13:16:41 +03002287 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002288
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002289 if (alignment)
2290 alignment--;
2291
Ben Widawsky2f075562017-03-24 14:29:48 -07002292 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002293 unsigned int tile_size, tile_width, tile_height;
2294 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002295
Ville Syrjäläd8433102016-01-12 21:08:35 +02002296 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002297 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002298
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002299 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002300 pitch_tiles = pitch / tile_height;
2301 swap(tile_width, tile_height);
2302 } else {
2303 pitch_tiles = pitch / (tile_width * cpp);
2304 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002305
Ville Syrjäläd8433102016-01-12 21:08:35 +02002306 tile_rows = *y / tile_height;
2307 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002308
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002309 tiles = *x / tile_width;
2310 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002311
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002312 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2313 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002314
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002315 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2316 tile_size, pitch_tiles,
2317 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002318 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002319 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002320 offset_aligned = offset & ~alignment;
2321
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002322 *y = (offset & alignment) / pitch;
2323 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002324 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002325
2326 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002327}
2328
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002329static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2330 const struct intel_plane_state *state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002331 int color_plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002332{
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002333 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2334 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
Ville Syrjälä29490562016-01-20 18:02:50 +02002335 const struct drm_framebuffer *fb = state->base.fb;
2336 unsigned int rotation = state->base.rotation;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002337 int pitch = state->color_plane[color_plane].stride;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03002338 u32 alignment;
2339
2340 if (intel_plane->id == PLANE_CURSOR)
2341 alignment = intel_cursor_alignment(dev_priv);
2342 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002343 alignment = intel_surf_alignment(fb, color_plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002344
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002345 return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002346 pitch, rotation, alignment);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002347}
2348
Ville Syrjälä303ba692017-08-24 22:10:49 +03002349/* Convert the fb->offset[] into x/y offsets */
2350static int intel_fb_offset_to_xy(int *x, int *y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002351 const struct drm_framebuffer *fb,
2352 int color_plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002353{
Ville Syrjälä303ba692017-08-24 22:10:49 +03002354 struct drm_i915_private *dev_priv = to_i915(fb->dev);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002355
Ville Syrjälä303ba692017-08-24 22:10:49 +03002356 if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002357 fb->offsets[color_plane] % intel_tile_size(dev_priv))
Ville Syrjälä303ba692017-08-24 22:10:49 +03002358 return -EINVAL;
2359
2360 *x = 0;
2361 *y = 0;
2362
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002363 intel_adjust_aligned_offset(x, y,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002364 fb, color_plane, DRM_MODE_ROTATE_0,
2365 fb->pitches[color_plane],
2366 fb->offsets[color_plane], 0);
Ville Syrjälä303ba692017-08-24 22:10:49 +03002367
2368 return 0;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002369}
2370
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002371static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2372{
2373 switch (fb_modifier) {
2374 case I915_FORMAT_MOD_X_TILED:
2375 return I915_TILING_X;
2376 case I915_FORMAT_MOD_Y_TILED:
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002377 case I915_FORMAT_MOD_Y_TILED_CCS:
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002378 return I915_TILING_Y;
2379 default:
2380 return I915_TILING_NONE;
2381 }
2382}
2383
Ville Syrjälä16af25f2018-01-19 16:41:52 +02002384/*
2385 * From the Sky Lake PRM:
2386 * "The Color Control Surface (CCS) contains the compression status of
2387 * the cache-line pairs. The compression state of the cache-line pair
2388 * is specified by 2 bits in the CCS. Each CCS cache-line represents
2389 * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2390 * cache-line-pairs. CCS is always Y tiled."
2391 *
2392 * Since cache line pairs refers to horizontally adjacent cache lines,
2393 * each cache line in the CCS corresponds to an area of 32x16 cache
2394 * lines on the main surface. Since each pixel is 4 bytes, this gives
2395 * us a ratio of one byte in the CCS for each 8x16 pixels in the
2396 * main surface.
2397 */
Ville Syrjäläbbfb6ce2017-08-01 09:58:12 -07002398static const struct drm_format_info ccs_formats[] = {
2399 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2400 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2401 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2402 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2403};
2404
2405static const struct drm_format_info *
2406lookup_format_info(const struct drm_format_info formats[],
2407 int num_formats, u32 format)
2408{
2409 int i;
2410
2411 for (i = 0; i < num_formats; i++) {
2412 if (formats[i].format == format)
2413 return &formats[i];
2414 }
2415
2416 return NULL;
2417}
2418
2419static const struct drm_format_info *
2420intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2421{
2422 switch (cmd->modifier[0]) {
2423 case I915_FORMAT_MOD_Y_TILED_CCS:
2424 case I915_FORMAT_MOD_Yf_TILED_CCS:
2425 return lookup_format_info(ccs_formats,
2426 ARRAY_SIZE(ccs_formats),
2427 cmd->pixel_format);
2428 default:
2429 return NULL;
2430 }
2431}
2432
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07002433bool is_ccs_modifier(u64 modifier)
2434{
2435 return modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2436 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2437}
2438
Ville Syrjälä6687c902015-09-15 13:16:41 +03002439static int
2440intel_fill_fb_info(struct drm_i915_private *dev_priv,
2441 struct drm_framebuffer *fb)
2442{
2443 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2444 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
Daniel Stonea5ff7a42018-05-18 15:30:07 +01002445 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002446 u32 gtt_offset_rotated = 0;
2447 unsigned int max_size = 0;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +02002448 int i, num_planes = fb->format->num_planes;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002449 unsigned int tile_size = intel_tile_size(dev_priv);
2450
2451 for (i = 0; i < num_planes; i++) {
2452 unsigned int width, height;
2453 unsigned int cpp, size;
2454 u32 offset;
2455 int x, y;
Ville Syrjälä303ba692017-08-24 22:10:49 +03002456 int ret;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002457
Ville Syrjälä353c8592016-12-14 23:30:57 +02002458 cpp = fb->format->cpp[i];
Ville Syrjälä145fcb12016-11-18 21:53:06 +02002459 width = drm_framebuffer_plane_width(fb->width, fb, i);
2460 height = drm_framebuffer_plane_height(fb->height, fb, i);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002461
Ville Syrjälä303ba692017-08-24 22:10:49 +03002462 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
2463 if (ret) {
2464 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2465 i, fb->offsets[i]);
2466 return ret;
2467 }
Ville Syrjälä6687c902015-09-15 13:16:41 +03002468
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07002469 if (is_ccs_modifier(fb->modifier) && i == 1) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002470 int hsub = fb->format->hsub;
2471 int vsub = fb->format->vsub;
2472 int tile_width, tile_height;
2473 int main_x, main_y;
2474 int ccs_x, ccs_y;
2475
2476 intel_tile_dims(fb, i, &tile_width, &tile_height);
Ville Syrjälä303ba692017-08-24 22:10:49 +03002477 tile_width *= hsub;
2478 tile_height *= vsub;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002479
Ville Syrjälä303ba692017-08-24 22:10:49 +03002480 ccs_x = (x * hsub) % tile_width;
2481 ccs_y = (y * vsub) % tile_height;
2482 main_x = intel_fb->normal[0].x % tile_width;
2483 main_y = intel_fb->normal[0].y % tile_height;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002484
2485 /*
2486 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2487 * x/y offsets must match between CCS and the main surface.
2488 */
2489 if (main_x != ccs_x || main_y != ccs_y) {
2490 DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2491 main_x, main_y,
2492 ccs_x, ccs_y,
2493 intel_fb->normal[0].x,
2494 intel_fb->normal[0].y,
2495 x, y);
2496 return -EINVAL;
2497 }
2498 }
2499
Ville Syrjälä6687c902015-09-15 13:16:41 +03002500 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002501 * The fence (if used) is aligned to the start of the object
2502 * so having the framebuffer wrap around across the edge of the
2503 * fenced region doesn't really work. We have no API to configure
2504 * the fence start offset within the object (nor could we probably
2505 * on gen2/3). So it's just easier if we just require that the
2506 * fb layout agrees with the fence layout. We already check that the
2507 * fb stride matches the fence stride elsewhere.
2508 */
Daniel Stonea5ff7a42018-05-18 15:30:07 +01002509 if (i == 0 && i915_gem_object_is_tiled(obj) &&
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002510 (x + width) * cpp > fb->pitches[i]) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +02002511 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2512 i, fb->offsets[i]);
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002513 return -EINVAL;
2514 }
2515
2516 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002517 * First pixel of the framebuffer from
2518 * the start of the normal gtt mapping.
2519 */
2520 intel_fb->normal[i].x = x;
2521 intel_fb->normal[i].y = y;
2522
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002523 offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
2524 fb->pitches[i],
2525 DRM_MODE_ROTATE_0,
2526 tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002527 offset /= tile_size;
2528
Ben Widawsky2f075562017-03-24 14:29:48 -07002529 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002530 unsigned int tile_width, tile_height;
2531 unsigned int pitch_tiles;
2532 struct drm_rect r;
2533
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002534 intel_tile_dims(fb, i, &tile_width, &tile_height);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002535
2536 rot_info->plane[i].offset = offset;
2537 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2538 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2539 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2540
2541 intel_fb->rotated[i].pitch =
2542 rot_info->plane[i].height * tile_height;
2543
2544 /* how many tiles does this plane need */
2545 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2546 /*
2547 * If the plane isn't horizontally tile aligned,
2548 * we need one more tile.
2549 */
2550 if (x != 0)
2551 size++;
2552
2553 /* rotate the x/y offsets to match the GTT view */
2554 r.x1 = x;
2555 r.y1 = y;
2556 r.x2 = x + width;
2557 r.y2 = y + height;
2558 drm_rect_rotate(&r,
2559 rot_info->plane[i].width * tile_width,
2560 rot_info->plane[i].height * tile_height,
Robert Fossc2c446a2017-05-19 16:50:17 -04002561 DRM_MODE_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002562 x = r.x1;
2563 y = r.y1;
2564
2565 /* rotate the tile dimensions to match the GTT view */
2566 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2567 swap(tile_width, tile_height);
2568
2569 /*
2570 * We only keep the x/y offsets, so push all of the
2571 * gtt offset into the x/y offsets.
2572 */
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002573 intel_adjust_tile_offset(&x, &y,
2574 tile_width, tile_height,
2575 tile_size, pitch_tiles,
2576 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002577
2578 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2579
2580 /*
2581 * First pixel of the framebuffer from
2582 * the start of the rotated gtt mapping.
2583 */
2584 intel_fb->rotated[i].x = x;
2585 intel_fb->rotated[i].y = y;
2586 } else {
2587 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2588 x * cpp, tile_size);
2589 }
2590
2591 /* how many tiles in total needed in the bo */
2592 max_size = max(max_size, offset + size);
2593 }
2594
Ville Syrjälä4e050472018-09-12 21:04:43 +03002595 if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
2596 DRM_DEBUG_KMS("fb too big for bo (need %llu bytes, have %zu bytes)\n",
2597 mul_u32_u32(max_size, tile_size), obj->base.size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002598 return -EINVAL;
2599 }
2600
2601 return 0;
2602}
2603
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002604static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002605{
2606 switch (format) {
2607 case DISPPLANE_8BPP:
2608 return DRM_FORMAT_C8;
2609 case DISPPLANE_BGRX555:
2610 return DRM_FORMAT_XRGB1555;
2611 case DISPPLANE_BGRX565:
2612 return DRM_FORMAT_RGB565;
2613 default:
2614 case DISPPLANE_BGRX888:
2615 return DRM_FORMAT_XRGB8888;
2616 case DISPPLANE_RGBX888:
2617 return DRM_FORMAT_XBGR8888;
2618 case DISPPLANE_BGRX101010:
2619 return DRM_FORMAT_XRGB2101010;
2620 case DISPPLANE_RGBX101010:
2621 return DRM_FORMAT_XBGR2101010;
2622 }
2623}
2624
Mahesh Kumarddf34312018-04-09 09:11:03 +05302625int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002626{
2627 switch (format) {
2628 case PLANE_CTL_FORMAT_RGB_565:
2629 return DRM_FORMAT_RGB565;
Mahesh Kumarf34a2912018-04-09 09:11:02 +05302630 case PLANE_CTL_FORMAT_NV12:
2631 return DRM_FORMAT_NV12;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002632 default:
2633 case PLANE_CTL_FORMAT_XRGB_8888:
2634 if (rgb_order) {
2635 if (alpha)
2636 return DRM_FORMAT_ABGR8888;
2637 else
2638 return DRM_FORMAT_XBGR8888;
2639 } else {
2640 if (alpha)
2641 return DRM_FORMAT_ARGB8888;
2642 else
2643 return DRM_FORMAT_XRGB8888;
2644 }
2645 case PLANE_CTL_FORMAT_XRGB_2101010:
2646 if (rgb_order)
2647 return DRM_FORMAT_XBGR2101010;
2648 else
2649 return DRM_FORMAT_XRGB2101010;
2650 }
2651}
2652
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002653static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002654intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2655 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002656{
2657 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002658 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002659 struct drm_i915_gem_object *obj = NULL;
2660 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002661 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002662 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2663 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2664 PAGE_SIZE);
2665
2666 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002667
Chris Wilsonff2652e2014-03-10 08:07:02 +00002668 if (plane_config->size == 0)
2669 return false;
2670
Paulo Zanoni3badb492015-09-23 12:52:23 -03002671 /* If the FB is too big, just don't use it since fbdev is not very
2672 * important and we should probably use that space with FBC or other
2673 * features. */
Matthew Auldb1ace602017-12-11 15:18:21 +00002674 if (size_aligned * 2 > dev_priv->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002675 return false;
2676
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002677 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00002678 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002679 base_aligned,
2680 base_aligned,
2681 size_aligned);
Chris Wilson24dbf512017-02-15 10:59:18 +00002682 mutex_unlock(&dev->struct_mutex);
2683 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002684 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002685
Chris Wilson3e510a82016-08-05 10:14:23 +01002686 if (plane_config->tiling == I915_TILING_X)
2687 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002688
Ville Syrjälä438b74a2016-12-14 23:32:55 +02002689 mode_cmd.pixel_format = fb->format->format;
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002690 mode_cmd.width = fb->width;
2691 mode_cmd.height = fb->height;
2692 mode_cmd.pitches[0] = fb->pitches[0];
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002693 mode_cmd.modifier[0] = fb->modifier;
Daniel Vetter18c52472015-02-10 17:16:09 +00002694 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002695
Chris Wilson24dbf512017-02-15 10:59:18 +00002696 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002697 DRM_DEBUG_KMS("intel fb init failed\n");
2698 goto out_unref_obj;
2699 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002700
Jesse Barnes484b41d2014-03-07 08:57:55 -08002701
Daniel Vetterf6936e22015-03-26 12:17:05 +01002702 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002703 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002704
2705out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002706 i915_gem_object_put(obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002707 return false;
2708}
2709
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002710static void
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002711intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2712 struct intel_plane_state *plane_state,
2713 bool visible)
2714{
2715 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2716
2717 plane_state->base.visible = visible;
2718
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002719 if (visible)
Ville Syrjälä40560e22018-06-26 22:47:11 +03002720 crtc_state->base.plane_mask |= drm_plane_mask(&plane->base);
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002721 else
Ville Syrjälä40560e22018-06-26 22:47:11 +03002722 crtc_state->base.plane_mask &= ~drm_plane_mask(&plane->base);
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002723}
2724
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002725static void fixup_active_planes(struct intel_crtc_state *crtc_state)
2726{
2727 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2728 struct drm_plane *plane;
2729
2730 /*
2731 * Active_planes aliases if multiple "primary" or cursor planes
2732 * have been used on the same (or wrong) pipe. plane_mask uses
2733 * unique ids, hence we can use that to reconstruct active_planes.
2734 */
2735 crtc_state->active_planes = 0;
2736
2737 drm_for_each_plane_mask(plane, &dev_priv->drm,
2738 crtc_state->base.plane_mask)
2739 crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
2740}
2741
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002742static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
2743 struct intel_plane *plane)
2744{
2745 struct intel_crtc_state *crtc_state =
2746 to_intel_crtc_state(crtc->base.state);
2747 struct intel_plane_state *plane_state =
2748 to_intel_plane_state(plane->base.state);
2749
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +03002750 DRM_DEBUG_KMS("Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
2751 plane->base.base.id, plane->base.name,
2752 crtc->base.base.id, crtc->base.name);
2753
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002754 intel_set_plane_visible(crtc_state, plane_state, false);
Ville Syrjälä62358aa2018-10-03 17:50:17 +03002755 fixup_active_planes(crtc_state);
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002756
2757 if (plane->id == PLANE_PRIMARY)
2758 intel_pre_disable_primary_noatomic(&crtc->base);
2759
2760 trace_intel_disable_plane(&plane->base, crtc);
2761 plane->disable_plane(plane, crtc);
2762}
2763
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002764static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002765intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2766 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002767{
2768 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002769 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002770 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002771 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002772 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002773 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002774 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002775 struct intel_plane_state *intel_state =
2776 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002777 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002778
Damien Lespiau2d140302015-02-05 17:22:18 +00002779 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002780 return;
2781
Daniel Vetterf6936e22015-03-26 12:17:05 +01002782 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002783 fb = &plane_config->fb->base;
2784 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002785 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002786
Damien Lespiau2d140302015-02-05 17:22:18 +00002787 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002788
2789 /*
2790 * Failed to alloc the obj, check to see if we should share
2791 * an fb with another CRTC instead
2792 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002793 for_each_crtc(dev, c) {
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002794 struct intel_plane_state *state;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002795
2796 if (c == &intel_crtc->base)
2797 continue;
2798
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002799 if (!to_intel_crtc(c)->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002800 continue;
2801
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002802 state = to_intel_plane_state(c->primary->state);
2803 if (!state->vma)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002804 continue;
2805
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002806 if (intel_plane_ggtt_offset(state) == plane_config->base) {
Ville Syrjälä8bc20f62018-03-22 17:22:59 +02002807 fb = state->base.fb;
Harsha Sharmac3ed1102017-10-09 17:36:43 +05302808 drm_framebuffer_get(fb);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002809 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002810 }
2811 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002812
Matt Roper200757f2015-12-03 11:37:36 -08002813 /*
2814 * We've failed to reconstruct the BIOS FB. Current display state
2815 * indicates that the primary plane is visible, but has a NULL FB,
2816 * which will lead to problems later if we don't fix it up. The
2817 * simplest solution is to just disable the primary plane now and
2818 * pretend the BIOS never had it enabled.
2819 */
Ville Syrjäläb1e01592017-11-17 21:19:09 +02002820 intel_plane_disable_noatomic(intel_crtc, intel_plane);
Matt Roper200757f2015-12-03 11:37:36 -08002821
Daniel Vetter88595ac2015-03-26 12:42:24 +01002822 return;
2823
2824valid_fb:
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002825 intel_fill_fb_ggtt_view(&intel_state->view, fb,
2826 intel_state->base.rotation);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03002827 intel_state->color_plane[0].stride =
2828 intel_fb_pitch(fb, 0, intel_state->base.rotation);
2829
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002830 mutex_lock(&dev->struct_mutex);
2831 intel_state->vma =
Chris Wilson59354852018-02-20 13:42:06 +00002832 intel_pin_and_fence_fb_obj(fb,
Ville Syrjäläf5929c52018-09-07 18:24:06 +03002833 &intel_state->view,
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +02002834 intel_plane_uses_fence(intel_state),
Chris Wilson59354852018-02-20 13:42:06 +00002835 &intel_state->flags);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002836 mutex_unlock(&dev->struct_mutex);
2837 if (IS_ERR(intel_state->vma)) {
2838 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2839 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2840
2841 intel_state->vma = NULL;
Harsha Sharmac3ed1102017-10-09 17:36:43 +05302842 drm_framebuffer_put(fb);
Chris Wilsonbe1e3412017-01-16 15:21:27 +00002843 return;
2844 }
2845
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08002846 obj = intel_fb_obj(fb);
2847 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
2848
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002849 plane_state->src_x = 0;
2850 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002851 plane_state->src_w = fb->width << 16;
2852 plane_state->src_h = fb->height << 16;
2853
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002854 plane_state->crtc_x = 0;
2855 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002856 plane_state->crtc_w = fb->width;
2857 plane_state->crtc_h = fb->height;
2858
Rob Clark1638d302016-11-05 11:08:08 -04002859 intel_state->base.src = drm_plane_state_src(plane_state);
2860 intel_state->base.dst = drm_plane_state_dest(plane_state);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002861
Chris Wilson3e510a82016-08-05 10:14:23 +01002862 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002863 dev_priv->preserve_bios_swizzle = true;
2864
Ville Syrjäläcd30fbc2018-05-25 21:50:40 +03002865 plane_state->fb = fb;
2866 plane_state->crtc = &intel_crtc->base;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +02002867
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002868 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2869 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002870}
2871
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002872static int skl_max_plane_width(const struct drm_framebuffer *fb,
2873 int color_plane,
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002874 unsigned int rotation)
2875{
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03002876 int cpp = fb->format->cpp[color_plane];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002877
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002878 switch (fb->modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07002879 case DRM_FORMAT_MOD_LINEAR:
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002880 case I915_FORMAT_MOD_X_TILED:
2881 switch (cpp) {
2882 case 8:
2883 return 4096;
2884 case 4:
2885 case 2:
2886 case 1:
2887 return 8192;
2888 default:
2889 MISSING_CASE(cpp);
2890 break;
2891 }
2892 break;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002893 case I915_FORMAT_MOD_Y_TILED_CCS:
2894 case I915_FORMAT_MOD_Yf_TILED_CCS:
2895 /* FIXME AUX plane? */
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002896 case I915_FORMAT_MOD_Y_TILED:
2897 case I915_FORMAT_MOD_Yf_TILED:
2898 switch (cpp) {
2899 case 8:
2900 return 2048;
2901 case 4:
2902 return 4096;
2903 case 2:
2904 case 1:
2905 return 8192;
2906 default:
2907 MISSING_CASE(cpp);
2908 break;
2909 }
2910 break;
2911 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002912 MISSING_CASE(fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002913 }
2914
2915 return 2048;
2916}
2917
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002918static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
2919 int main_x, int main_y, u32 main_offset)
2920{
2921 const struct drm_framebuffer *fb = plane_state->base.fb;
2922 int hsub = fb->format->hsub;
2923 int vsub = fb->format->vsub;
Ville Syrjäläc11ada02018-09-07 18:24:04 +03002924 int aux_x = plane_state->color_plane[1].x;
2925 int aux_y = plane_state->color_plane[1].y;
2926 u32 aux_offset = plane_state->color_plane[1].offset;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002927 u32 alignment = intel_surf_alignment(fb, 1);
2928
2929 while (aux_offset >= main_offset && aux_y <= main_y) {
2930 int x, y;
2931
2932 if (aux_x == main_x && aux_y == main_y)
2933 break;
2934
2935 if (aux_offset == 0)
2936 break;
2937
2938 x = aux_x / hsub;
2939 y = aux_y / vsub;
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002940 aux_offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 1,
2941 aux_offset, aux_offset - alignment);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002942 aux_x = x * hsub + aux_x % hsub;
2943 aux_y = y * vsub + aux_y % vsub;
2944 }
2945
2946 if (aux_x != main_x || aux_y != main_y)
2947 return false;
2948
Ville Syrjäläc11ada02018-09-07 18:24:04 +03002949 plane_state->color_plane[1].offset = aux_offset;
2950 plane_state->color_plane[1].x = aux_x;
2951 plane_state->color_plane[1].y = aux_y;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002952
2953 return true;
2954}
2955
Ville Syrjälä73266592018-09-07 18:24:11 +03002956static int skl_check_main_surface(struct intel_plane_state *plane_state)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002957{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002958 const struct drm_framebuffer *fb = plane_state->base.fb;
2959 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002960 int x = plane_state->base.src.x1 >> 16;
2961 int y = plane_state->base.src.y1 >> 16;
2962 int w = drm_rect_width(&plane_state->base.src) >> 16;
2963 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002964 int max_width = skl_max_plane_width(fb, 0, rotation);
2965 int max_height = 4096;
Ville Syrjäläc11ada02018-09-07 18:24:04 +03002966 u32 alignment, offset, aux_offset = plane_state->color_plane[1].offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002967
2968 if (w > max_width || h > max_height) {
2969 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2970 w, h, max_width, max_height);
2971 return -EINVAL;
2972 }
2973
2974 intel_add_fb_offsets(&x, &y, plane_state, 0);
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002975 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02002976 alignment = intel_surf_alignment(fb, 0);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002977
2978 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002979 * AUX surface offset is specified as the distance from the
2980 * main surface offset, and it must be non-negative. Make
2981 * sure that is what we will get.
2982 */
2983 if (offset > aux_offset)
Ville Syrjälä6d19a442018-09-07 18:24:01 +03002984 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
2985 offset, aux_offset & ~(alignment - 1));
Ville Syrjälä8d970652016-01-28 16:30:28 +02002986
2987 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002988 * When using an X-tiled surface, the plane blows up
2989 * if the x offset + width exceed the stride.
2990 *
2991 * TODO: linear and Y-tiled seem fine, Yf untested,
2992 */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002993 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
Ville Syrjälä353c8592016-12-14 23:30:57 +02002994 int cpp = fb->format->cpp[0];
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002995
Ville Syrjälädf79cf42018-09-11 18:01:39 +03002996 while ((x + w) * cpp > plane_state->color_plane[0].stride) {
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002997 if (offset == 0) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07002998 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002999 return -EINVAL;
3000 }
3001
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003002 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3003 offset, offset - alignment);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003004 }
3005 }
3006
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003007 /*
3008 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3009 * they match with the main surface x/y offsets.
3010 */
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07003011 if (is_ccs_modifier(fb->modifier)) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003012 while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
3013 if (offset == 0)
3014 break;
3015
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003016 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3017 offset, offset - alignment);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003018 }
3019
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003020 if (x != plane_state->color_plane[1].x || y != plane_state->color_plane[1].y) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003021 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
3022 return -EINVAL;
3023 }
3024 }
3025
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003026 plane_state->color_plane[0].offset = offset;
3027 plane_state->color_plane[0].x = x;
3028 plane_state->color_plane[0].y = y;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003029
3030 return 0;
3031}
3032
Maarten Lankhorst5d794282018-05-12 03:03:14 +05303033static int
Ville Syrjälä73266592018-09-07 18:24:11 +03003034skl_check_nv12_surface(struct intel_plane_state *plane_state)
Maarten Lankhorst5d794282018-05-12 03:03:14 +05303035{
3036 /* Display WA #1106 */
3037 if (plane_state->base.rotation !=
3038 (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90) &&
3039 plane_state->base.rotation != DRM_MODE_ROTATE_270)
3040 return 0;
3041
3042 /*
3043 * src coordinates are rotated here.
3044 * We check height but report it as width
3045 */
3046 if (((drm_rect_height(&plane_state->base.src) >> 16) % 4) != 0) {
3047 DRM_DEBUG_KMS("src width must be multiple "
3048 "of 4 for rotated NV12\n");
3049 return -EINVAL;
3050 }
3051
3052 return 0;
3053}
3054
Ville Syrjälä8d970652016-01-28 16:30:28 +02003055static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3056{
3057 const struct drm_framebuffer *fb = plane_state->base.fb;
3058 unsigned int rotation = plane_state->base.rotation;
3059 int max_width = skl_max_plane_width(fb, 1, rotation);
3060 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02003061 int x = plane_state->base.src.x1 >> 17;
3062 int y = plane_state->base.src.y1 >> 17;
3063 int w = drm_rect_width(&plane_state->base.src) >> 17;
3064 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003065 u32 offset;
3066
3067 intel_add_fb_offsets(&x, &y, plane_state, 1);
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003068 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
Ville Syrjälä8d970652016-01-28 16:30:28 +02003069
3070 /* FIXME not quite sure how/if these apply to the chroma plane */
3071 if (w > max_width || h > max_height) {
3072 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
3073 w, h, max_width, max_height);
3074 return -EINVAL;
3075 }
3076
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003077 plane_state->color_plane[1].offset = offset;
3078 plane_state->color_plane[1].x = x;
3079 plane_state->color_plane[1].y = y;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003080
3081 return 0;
3082}
3083
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003084static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3085{
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003086 const struct drm_framebuffer *fb = plane_state->base.fb;
3087 int src_x = plane_state->base.src.x1 >> 16;
3088 int src_y = plane_state->base.src.y1 >> 16;
3089 int hsub = fb->format->hsub;
3090 int vsub = fb->format->vsub;
3091 int x = src_x / hsub;
3092 int y = src_y / vsub;
3093 u32 offset;
3094
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003095 intel_add_fb_offsets(&x, &y, plane_state, 1);
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003096 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003097
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003098 plane_state->color_plane[1].offset = offset;
3099 plane_state->color_plane[1].x = x * hsub + src_x % hsub;
3100 plane_state->color_plane[1].y = y * vsub + src_y % vsub;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003101
3102 return 0;
3103}
3104
Ville Syrjälä73266592018-09-07 18:24:11 +03003105int skl_check_plane_surface(struct intel_plane_state *plane_state)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003106{
3107 const struct drm_framebuffer *fb = plane_state->base.fb;
3108 unsigned int rotation = plane_state->base.rotation;
3109 int ret;
3110
Ville Syrjäläf5929c52018-09-07 18:24:06 +03003111 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003112 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
3113 plane_state->color_plane[1].stride = intel_fb_pitch(fb, 1, rotation);
3114
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03003115 ret = intel_plane_check_stride(plane_state);
3116 if (ret)
3117 return ret;
3118
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003119 /* HW only has 8 bits pixel precision, disable plane if invisible */
3120 if (!(plane_state->base.alpha >> 8))
3121 plane_state->base.visible = false;
3122
Ville Syrjäläa5e4c7d2016-11-07 22:20:54 +02003123 if (!plane_state->base.visible)
3124 return 0;
3125
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003126 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003127 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02003128 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03003129 fb->width << 16, fb->height << 16,
Robert Fossc2c446a2017-05-19 16:50:17 -04003130 DRM_MODE_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003131
Ville Syrjälä8d970652016-01-28 16:30:28 +02003132 /*
3133 * Handle the AUX surface first since
3134 * the main surface setup depends on it.
3135 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003136 if (fb->format->format == DRM_FORMAT_NV12) {
Ville Syrjälä73266592018-09-07 18:24:11 +03003137 ret = skl_check_nv12_surface(plane_state);
Maarten Lankhorst5d794282018-05-12 03:03:14 +05303138 if (ret)
3139 return ret;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003140 ret = skl_check_nv12_aux_surface(plane_state);
3141 if (ret)
3142 return ret;
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -07003143 } else if (is_ccs_modifier(fb->modifier)) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003144 ret = skl_check_ccs_aux_surface(plane_state);
3145 if (ret)
3146 return ret;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003147 } else {
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003148 plane_state->color_plane[1].offset = ~0xfff;
3149 plane_state->color_plane[1].x = 0;
3150 plane_state->color_plane[1].y = 0;
Ville Syrjälä8d970652016-01-28 16:30:28 +02003151 }
3152
Ville Syrjälä73266592018-09-07 18:24:11 +03003153 ret = skl_check_main_surface(plane_state);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003154 if (ret)
3155 return ret;
3156
3157 return 0;
3158}
3159
Ville Syrjäläddd57132018-09-07 18:24:02 +03003160unsigned int
3161i9xx_plane_max_stride(struct intel_plane *plane,
3162 u32 pixel_format, u64 modifier,
3163 unsigned int rotation)
3164{
3165 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3166
3167 if (!HAS_GMCH_DISPLAY(dev_priv)) {
3168 return 32*1024;
3169 } else if (INTEL_GEN(dev_priv) >= 4) {
3170 if (modifier == I915_FORMAT_MOD_X_TILED)
3171 return 16*1024;
3172 else
3173 return 32*1024;
3174 } else if (INTEL_GEN(dev_priv) >= 3) {
3175 if (modifier == I915_FORMAT_MOD_X_TILED)
3176 return 8*1024;
3177 else
3178 return 16*1024;
3179 } else {
3180 if (plane->i9xx_plane == PLANE_C)
3181 return 4*1024;
3182 else
3183 return 8*1024;
3184 }
3185}
3186
Ville Syrjälä7145f602017-03-23 21:27:07 +02003187static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
3188 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07003189{
Ville Syrjälä7145f602017-03-23 21:27:07 +02003190 struct drm_i915_private *dev_priv =
3191 to_i915(plane_state->base.plane->dev);
3192 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3193 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003194 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003195 u32 dspcntr;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003196
Ville Syrjälä7145f602017-03-23 21:27:07 +02003197 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003198
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02003199 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
3200 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Ville Syrjälä7145f602017-03-23 21:27:07 +02003201 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003202
Ville Syrjälä6a4407a2017-03-23 21:27:08 +02003203 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3204 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003205
Ville Syrjäläc154d1e2018-01-30 22:38:02 +02003206 if (INTEL_GEN(dev_priv) < 5)
Ville Syrjäläd509e282017-03-27 21:55:32 +03003207 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003208
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003209 switch (fb->format->format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02003210 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003211 dspcntr |= DISPPLANE_8BPP;
3212 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003213 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003214 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003215 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003216 case DRM_FORMAT_RGB565:
3217 dspcntr |= DISPPLANE_BGRX565;
3218 break;
3219 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003220 dspcntr |= DISPPLANE_BGRX888;
3221 break;
3222 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003223 dspcntr |= DISPPLANE_RGBX888;
3224 break;
3225 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003226 dspcntr |= DISPPLANE_BGRX101010;
3227 break;
3228 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003229 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003230 break;
3231 default:
Ville Syrjälä7145f602017-03-23 21:27:07 +02003232 MISSING_CASE(fb->format->format);
3233 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003234 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003235
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003236 if (INTEL_GEN(dev_priv) >= 4 &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003237 fb->modifier == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003238 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003239
Robert Fossc2c446a2017-05-19 16:50:17 -04003240 if (rotation & DRM_MODE_ROTATE_180)
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003241 dspcntr |= DISPPLANE_ROTATE_180;
3242
Robert Fossc2c446a2017-05-19 16:50:17 -04003243 if (rotation & DRM_MODE_REFLECT_X)
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003244 dspcntr |= DISPPLANE_MIRROR;
3245
Ville Syrjälä7145f602017-03-23 21:27:07 +02003246 return dspcntr;
3247}
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003248
Ville Syrjäläf9407ae2017-03-23 21:27:12 +02003249int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003250{
3251 struct drm_i915_private *dev_priv =
3252 to_i915(plane_state->base.plane->dev);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003253 const struct drm_framebuffer *fb = plane_state->base.fb;
3254 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003255 int src_x = plane_state->base.src.x1 >> 16;
3256 int src_y = plane_state->base.src.y1 >> 16;
3257 u32 offset;
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03003258 int ret;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003259
Ville Syrjäläf5929c52018-09-07 18:24:06 +03003260 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003261 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
3262
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03003263 ret = intel_plane_check_stride(plane_state);
3264 if (ret)
3265 return ret;
3266
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003267 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003268
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003269 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä6d19a442018-09-07 18:24:01 +03003270 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
3271 plane_state, 0);
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003272 else
3273 offset = 0;
Daniel Vettere506a0c2012-07-05 12:17:29 +02003274
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003275 /* HSW/BDW do this automagically in hardware */
3276 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003277 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3278 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3279
Robert Fossc2c446a2017-05-19 16:50:17 -04003280 if (rotation & DRM_MODE_ROTATE_180) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003281 src_x += src_w - 1;
3282 src_y += src_h - 1;
Robert Fossc2c446a2017-05-19 16:50:17 -04003283 } else if (rotation & DRM_MODE_REFLECT_X) {
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003284 src_x += src_w - 1;
3285 }
Sonika Jindal48404c12014-08-22 14:06:04 +05303286 }
3287
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003288 plane_state->color_plane[0].offset = offset;
3289 plane_state->color_plane[0].x = src_x;
3290 plane_state->color_plane[0].y = src_y;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003291
3292 return 0;
3293}
3294
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03003295static int
3296i9xx_plane_check(struct intel_crtc_state *crtc_state,
3297 struct intel_plane_state *plane_state)
3298{
3299 int ret;
3300
Ville Syrjälä25721f82018-09-07 18:24:12 +03003301 ret = chv_plane_check_rotation(plane_state);
3302 if (ret)
3303 return ret;
3304
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03003305 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
3306 &crtc_state->base,
3307 DRM_PLANE_HELPER_NO_SCALING,
3308 DRM_PLANE_HELPER_NO_SCALING,
3309 false, true);
3310 if (ret)
3311 return ret;
3312
3313 if (!plane_state->base.visible)
3314 return 0;
3315
3316 ret = intel_plane_check_src_coordinates(plane_state);
3317 if (ret)
3318 return ret;
3319
3320 ret = i9xx_check_plane_surface(plane_state);
3321 if (ret)
3322 return ret;
3323
3324 plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
3325
3326 return 0;
3327}
3328
Ville Syrjäläed150302017-11-17 21:19:10 +02003329static void i9xx_update_plane(struct intel_plane *plane,
3330 const struct intel_crtc_state *crtc_state,
3331 const struct intel_plane_state *plane_state)
Ville Syrjälä7145f602017-03-23 21:27:07 +02003332{
Ville Syrjäläed150302017-11-17 21:19:10 +02003333 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläed150302017-11-17 21:19:10 +02003334 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003335 u32 linear_offset;
Ville Syrjäläa0864d52017-03-23 21:27:09 +02003336 u32 dspcntr = plane_state->ctl;
Ville Syrjäläed150302017-11-17 21:19:10 +02003337 i915_reg_t reg = DSPCNTR(i9xx_plane);
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003338 int x = plane_state->color_plane[0].x;
3339 int y = plane_state->color_plane[0].y;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003340 unsigned long irqflags;
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003341 u32 dspaddr_offset;
Ville Syrjälä7145f602017-03-23 21:27:07 +02003342
Ville Syrjälä29490562016-01-20 18:02:50 +02003343 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003344
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003345 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjäläc11ada02018-09-07 18:24:04 +03003346 dspaddr_offset = plane_state->color_plane[0].offset;
Ville Syrjälä5b7fcc42017-03-23 21:27:10 +02003347 else
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003348 dspaddr_offset = linear_offset;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003349
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003350 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3351
Ville Syrjälä78587de2017-03-09 17:44:32 +02003352 if (INTEL_GEN(dev_priv) < 4) {
3353 /* pipesrc and dspsize control the size that is scaled from,
3354 * which should always be the user's requested size.
3355 */
Ville Syrjäläed150302017-11-17 21:19:10 +02003356 I915_WRITE_FW(DSPSIZE(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003357 ((crtc_state->pipe_src_h - 1) << 16) |
3358 (crtc_state->pipe_src_w - 1));
Ville Syrjäläed150302017-11-17 21:19:10 +02003359 I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
3360 } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
3361 I915_WRITE_FW(PRIMSIZE(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003362 ((crtc_state->pipe_src_h - 1) << 16) |
3363 (crtc_state->pipe_src_w - 1));
Ville Syrjäläed150302017-11-17 21:19:10 +02003364 I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
3365 I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
Ville Syrjälä78587de2017-03-09 17:44:32 +02003366 }
3367
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003368 I915_WRITE_FW(reg, dspcntr);
Sonika Jindal48404c12014-08-22 14:06:04 +05303369
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003370 I915_WRITE_FW(DSPSTRIDE(i9xx_plane), plane_state->color_plane[0].stride);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003371 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläed150302017-11-17 21:19:10 +02003372 I915_WRITE_FW(DSPSURF(i9xx_plane),
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003373 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003374 dspaddr_offset);
Ville Syrjäläed150302017-11-17 21:19:10 +02003375 I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
Ville Syrjälä3ba35e52017-03-23 21:27:11 +02003376 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläed150302017-11-17 21:19:10 +02003377 I915_WRITE_FW(DSPSURF(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003378 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003379 dspaddr_offset);
Ville Syrjäläed150302017-11-17 21:19:10 +02003380 I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
3381 I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003382 } else {
Ville Syrjäläed150302017-11-17 21:19:10 +02003383 I915_WRITE_FW(DSPADDR(i9xx_plane),
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003384 intel_plane_ggtt_offset(plane_state) +
Juha-Pekka Heikkilae2888812017-10-17 23:08:08 +03003385 dspaddr_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003386 }
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003387 POSTING_READ_FW(reg);
3388
3389 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003390}
3391
Ville Syrjäläed150302017-11-17 21:19:10 +02003392static void i9xx_disable_plane(struct intel_plane *plane,
3393 struct intel_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003394{
Ville Syrjäläed150302017-11-17 21:19:10 +02003395 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3396 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003397 unsigned long irqflags;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003398
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003399 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3400
Ville Syrjäläed150302017-11-17 21:19:10 +02003401 I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
3402 if (INTEL_GEN(dev_priv) >= 4)
3403 I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003404 else
Ville Syrjäläed150302017-11-17 21:19:10 +02003405 I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
3406 POSTING_READ_FW(DSPCNTR(i9xx_plane));
Ville Syrjälädd584fc2017-03-09 17:44:33 +02003407
3408 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003409}
3410
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003411static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
3412 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003413{
Ville Syrjäläed150302017-11-17 21:19:10 +02003414 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003415 enum intel_display_power_domain power_domain;
Ville Syrjäläed150302017-11-17 21:19:10 +02003416 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003417 bool ret;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003418 u32 val;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003419
3420 /*
3421 * Not 100% correct for planes that can move between pipes,
3422 * but that's only the case for gen2-4 which don't have any
3423 * display power wells.
3424 */
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003425 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003426 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
3427 return false;
3428
Ville Syrjäläeade6c82018-01-30 22:38:03 +02003429 val = I915_READ(DSPCNTR(i9xx_plane));
3430
3431 ret = val & DISPLAY_PLANE_ENABLE;
3432
3433 if (INTEL_GEN(dev_priv) >= 5)
3434 *pipe = plane->pipe;
3435 else
3436 *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
3437 DISPPLANE_SEL_PIPE_SHIFT;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02003438
3439 intel_display_power_put(dev_priv, power_domain);
3440
3441 return ret;
3442}
3443
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003444static u32
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003445intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
Damien Lespiaub3218032015-02-27 11:15:18 +00003446{
Ben Widawsky2f075562017-03-24 14:29:48 -07003447 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003448 return 64;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003449 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003450 return intel_tile_width_bytes(fb, color_plane);
Damien Lespiaub3218032015-02-27 11:15:18 +00003451}
3452
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003453static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3454{
3455 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003456 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003457
3458 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3459 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3460 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003461}
3462
Chandra Kondurua1b22782015-04-07 15:28:45 -07003463/*
3464 * This function detaches (aka. unbinds) unused scalers in hardware
3465 */
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003466static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003467{
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3469 const struct intel_crtc_scaler_state *scaler_state =
3470 &crtc_state->scaler_state;
Chandra Kondurua1b22782015-04-07 15:28:45 -07003471 int i;
3472
Chandra Kondurua1b22782015-04-07 15:28:45 -07003473 /* loop through and disable scalers that aren't in use */
3474 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003475 if (!scaler_state->scalers[i].in_use)
3476 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003477 }
3478}
3479
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003480u32 skl_plane_stride(const struct intel_plane_state *plane_state,
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003481 int color_plane)
Ville Syrjäläd2196772016-01-28 18:33:11 +02003482{
Ville Syrjälädf79cf42018-09-11 18:01:39 +03003483 const struct drm_framebuffer *fb = plane_state->base.fb;
3484 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003485 u32 stride = plane_state->color_plane[color_plane].stride;
Ville Syrjälä1b500532017-03-07 21:42:08 +02003486
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003487 if (color_plane >= fb->format->num_planes)
Ville Syrjälä1b500532017-03-07 21:42:08 +02003488 return 0;
3489
Ville Syrjäläd2196772016-01-28 18:33:11 +02003490 /*
3491 * The stride is either expressed as a multiple of 64 bytes chunks for
3492 * linear buffers or in number of tiles for tiled buffers.
3493 */
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003494 if (drm_rotation_90_or_270(rotation))
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003495 stride /= intel_tile_height(fb, color_plane);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02003496 else
Ville Syrjälä5d2a1952018-09-07 18:24:07 +03003497 stride /= intel_fb_stride_alignment(fb, color_plane);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003498
3499 return stride;
3500}
3501
Ville Syrjälä2e881262017-03-17 23:17:56 +02003502static u32 skl_plane_ctl_format(uint32_t pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -07003503{
Chandra Konduru6156a452015-04-27 13:48:39 -07003504 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003505 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003506 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003507 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003508 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003509 case DRM_FORMAT_XBGR8888:
James Ausmus4036c782017-11-13 10:11:28 -08003510 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003511 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003512 case DRM_FORMAT_XRGB8888:
Chandra Konduru6156a452015-04-27 13:48:39 -07003513 case DRM_FORMAT_ARGB8888:
James Ausmus4036c782017-11-13 10:11:28 -08003514 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003515 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003516 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003517 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003518 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003519 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003520 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003521 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003522 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003523 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003524 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003525 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003526 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru77224cd2018-04-09 09:11:13 +05303527 case DRM_FORMAT_NV12:
3528 return PLANE_CTL_FORMAT_NV12;
Chandra Konduru6156a452015-04-27 13:48:39 -07003529 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003530 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003531 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003532
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003533 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003534}
3535
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003536static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
James Ausmus4036c782017-11-13 10:11:28 -08003537{
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003538 if (!plane_state->base.fb->format->has_alpha)
3539 return PLANE_CTL_ALPHA_DISABLE;
3540
3541 switch (plane_state->base.pixel_blend_mode) {
3542 case DRM_MODE_BLEND_PIXEL_NONE:
3543 return PLANE_CTL_ALPHA_DISABLE;
3544 case DRM_MODE_BLEND_PREMULTI:
James Ausmus4036c782017-11-13 10:11:28 -08003545 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003546 case DRM_MODE_BLEND_COVERAGE:
3547 return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
James Ausmus4036c782017-11-13 10:11:28 -08003548 default:
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003549 MISSING_CASE(plane_state->base.pixel_blend_mode);
James Ausmus4036c782017-11-13 10:11:28 -08003550 return PLANE_CTL_ALPHA_DISABLE;
3551 }
3552}
3553
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003554static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
James Ausmus4036c782017-11-13 10:11:28 -08003555{
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003556 if (!plane_state->base.fb->format->has_alpha)
3557 return PLANE_COLOR_ALPHA_DISABLE;
3558
3559 switch (plane_state->base.pixel_blend_mode) {
3560 case DRM_MODE_BLEND_PIXEL_NONE:
3561 return PLANE_COLOR_ALPHA_DISABLE;
3562 case DRM_MODE_BLEND_PREMULTI:
James Ausmus4036c782017-11-13 10:11:28 -08003563 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003564 case DRM_MODE_BLEND_COVERAGE:
3565 return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
James Ausmus4036c782017-11-13 10:11:28 -08003566 default:
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003567 MISSING_CASE(plane_state->base.pixel_blend_mode);
James Ausmus4036c782017-11-13 10:11:28 -08003568 return PLANE_COLOR_ALPHA_DISABLE;
3569 }
3570}
3571
Ville Syrjälä2e881262017-03-17 23:17:56 +02003572static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
Chandra Konduru6156a452015-04-27 13:48:39 -07003573{
Chandra Konduru6156a452015-04-27 13:48:39 -07003574 switch (fb_modifier) {
Ben Widawsky2f075562017-03-24 14:29:48 -07003575 case DRM_FORMAT_MOD_LINEAR:
Chandra Konduru6156a452015-04-27 13:48:39 -07003576 break;
3577 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003578 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003579 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003580 return PLANE_CTL_TILED_Y;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003581 case I915_FORMAT_MOD_Y_TILED_CCS:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07003582 return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003583 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003584 return PLANE_CTL_TILED_YF;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07003585 case I915_FORMAT_MOD_Yf_TILED_CCS:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07003586 return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003587 default:
3588 MISSING_CASE(fb_modifier);
3589 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003590
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003591 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003592}
3593
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003594static u32 skl_plane_ctl_rotate(unsigned int rotate)
Chandra Konduru6156a452015-04-27 13:48:39 -07003595{
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003596 switch (rotate) {
Robert Fossc2c446a2017-05-19 16:50:17 -04003597 case DRM_MODE_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003598 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303599 /*
Robert Fossc2c446a2017-05-19 16:50:17 -04003600 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
Sonika Jindal1e8df162015-05-20 13:40:48 +05303601 * while i915 HW rotation is clockwise, thats why this swapping.
3602 */
Robert Fossc2c446a2017-05-19 16:50:17 -04003603 case DRM_MODE_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303604 return PLANE_CTL_ROTATE_270;
Robert Fossc2c446a2017-05-19 16:50:17 -04003605 case DRM_MODE_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003606 return PLANE_CTL_ROTATE_180;
Robert Fossc2c446a2017-05-19 16:50:17 -04003607 case DRM_MODE_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303608 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003609 default:
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003610 MISSING_CASE(rotate);
3611 }
3612
3613 return 0;
3614}
3615
3616static u32 cnl_plane_ctl_flip(unsigned int reflect)
3617{
3618 switch (reflect) {
3619 case 0:
3620 break;
3621 case DRM_MODE_REFLECT_X:
3622 return PLANE_CTL_FLIP_HORIZONTAL;
3623 case DRM_MODE_REFLECT_Y:
3624 default:
3625 MISSING_CASE(reflect);
Chandra Konduru6156a452015-04-27 13:48:39 -07003626 }
3627
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003628 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003629}
3630
Ville Syrjälä2e881262017-03-17 23:17:56 +02003631u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3632 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003633{
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003634 struct drm_i915_private *dev_priv =
3635 to_i915(plane_state->base.plane->dev);
3636 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003637 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä2e881262017-03-17 23:17:56 +02003638 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003639 u32 plane_ctl;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003640
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003641 plane_ctl = PLANE_CTL_ENABLE;
3642
James Ausmus4036c782017-11-13 10:11:28 -08003643 if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003644 plane_ctl |= skl_plane_ctl_alpha(plane_state);
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003645 plane_ctl |=
3646 PLANE_CTL_PIPE_GAMMA_ENABLE |
3647 PLANE_CTL_PIPE_CSC_ENABLE |
3648 PLANE_CTL_PLANE_GAMMA_DISABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003649
3650 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3651 plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003652
3653 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3654 plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
Ander Conselvan de Oliveira47f9ea82017-01-26 13:24:22 +02003655 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003656
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003657 plane_ctl |= skl_plane_ctl_format(fb->format->format);
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003658 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
Joonas Lahtinen5f8e3f52017-12-15 13:38:00 -08003659 plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
3660
3661 if (INTEL_GEN(dev_priv) >= 10)
3662 plane_ctl |= cnl_plane_ctl_flip(rotation &
3663 DRM_MODE_REFLECT_MASK);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003664
Ville Syrjälä2e881262017-03-17 23:17:56 +02003665 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3666 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3667 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3668 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3669
Ville Syrjälä46f788b2017-03-17 23:17:55 +02003670 return plane_ctl;
3671}
3672
James Ausmus4036c782017-11-13 10:11:28 -08003673u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
3674 const struct intel_plane_state *plane_state)
3675{
James Ausmus077ef1f2018-03-28 14:57:56 -07003676 struct drm_i915_private *dev_priv =
3677 to_i915(plane_state->base.plane->dev);
James Ausmus4036c782017-11-13 10:11:28 -08003678 const struct drm_framebuffer *fb = plane_state->base.fb;
3679 u32 plane_color_ctl = 0;
3680
James Ausmus077ef1f2018-03-28 14:57:56 -07003681 if (INTEL_GEN(dev_priv) < 11) {
3682 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
3683 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
3684 }
James Ausmus4036c782017-11-13 10:11:28 -08003685 plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
Maarten Lankhorstb2081522018-08-15 12:34:05 +02003686 plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
James Ausmus4036c782017-11-13 10:11:28 -08003687
Ayan Kumar Halder9bace652018-07-17 18:13:43 +01003688 if (fb->format->is_yuv) {
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003689 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3690 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
3691 else
3692 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
Ville Syrjäläc8624ed2018-02-14 21:23:27 +02003693
3694 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3695 plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
Ville Syrjäläb0f5c0b2018-02-14 21:23:25 +02003696 }
Ville Syrjälä012d79e2018-05-21 21:56:12 +03003697
James Ausmus4036c782017-11-13 10:11:28 -08003698 return plane_color_ctl;
3699}
3700
Maarten Lankhorst73974892016-08-05 23:28:27 +03003701static int
3702__intel_display_resume(struct drm_device *dev,
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003703 struct drm_atomic_state *state,
3704 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorst73974892016-08-05 23:28:27 +03003705{
3706 struct drm_crtc_state *crtc_state;
3707 struct drm_crtc *crtc;
3708 int i, ret;
3709
Ville Syrjäläaecd36b2017-06-01 17:36:13 +03003710 intel_modeset_setup_hw_state(dev, ctx);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003711 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003712
3713 if (!state)
3714 return 0;
3715
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01003716 /*
3717 * We've duplicated the state, pointers to the old state are invalid.
3718 *
3719 * Don't attempt to use the old state until we commit the duplicated state.
3720 */
3721 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst73974892016-08-05 23:28:27 +03003722 /*
3723 * Force recalculation even if we restore
3724 * current state. With fast modeset this may not result
3725 * in a modeset when the state is compatible.
3726 */
3727 crtc_state->mode_changed = true;
3728 }
3729
3730 /* ignore any reset values/BIOS leftovers in the WM registers */
Ville Syrjälä602ae832017-03-02 19:15:02 +02003731 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3732 to_intel_atomic_state(state)->skip_intermediate_wm = true;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003733
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003734 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003735
3736 WARN_ON(ret == -EDEADLK);
3737 return ret;
3738}
3739
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003740static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3741{
Ville Syrjäläae981042016-08-05 23:28:30 +03003742 return intel_has_gpu_reset(dev_priv) &&
3743 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003744}
3745
Chris Wilsonc0336662016-05-06 15:40:21 +01003746void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003747{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003748 struct drm_device *dev = &dev_priv->drm;
3749 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3750 struct drm_atomic_state *state;
3751 int ret;
3752
Daniel Vetterce87ea12017-07-19 14:54:55 +02003753 /* reset doesn't touch the display */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00003754 if (!i915_modparams.force_reset_modeset_test &&
Daniel Vetterce87ea12017-07-19 14:54:55 +02003755 !gpu_reset_clobbers_display(dev_priv))
3756 return;
3757
Daniel Vetter9db529a2017-08-08 10:08:28 +02003758 /* We have a modeset vs reset deadlock, defensively unbreak it. */
3759 set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
3760 wake_up_all(&dev_priv->gpu_error.wait_queue);
3761
3762 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
3763 DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
3764 i915_gem_set_wedged(dev_priv);
3765 }
Daniel Vetter97154ec2017-08-08 10:08:26 +02003766
Maarten Lankhorst73974892016-08-05 23:28:27 +03003767 /*
3768 * Need mode_config.mutex so that we don't
3769 * trample ongoing ->detect() and whatnot.
3770 */
3771 mutex_lock(&dev->mode_config.mutex);
3772 drm_modeset_acquire_init(ctx, 0);
3773 while (1) {
3774 ret = drm_modeset_lock_all_ctx(dev, ctx);
3775 if (ret != -EDEADLK)
3776 break;
3777
3778 drm_modeset_backoff(ctx);
3779 }
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003780 /*
3781 * Disabling the crtcs gracefully seems nicer. Also the
3782 * g33 docs say we should at least disable all the planes.
3783 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003784 state = drm_atomic_helper_duplicate_state(dev, ctx);
3785 if (IS_ERR(state)) {
3786 ret = PTR_ERR(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003787 DRM_ERROR("Duplicating state failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003788 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003789 }
3790
3791 ret = drm_atomic_helper_disable_all(dev, ctx);
3792 if (ret) {
3793 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Ander Conselvan de Oliveira1e5a15d2017-01-18 14:34:28 +02003794 drm_atomic_state_put(state);
3795 return;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003796 }
3797
3798 dev_priv->modeset_restore_state = state;
3799 state->acquire_ctx = ctx;
Ville Syrjälä75147472014-11-24 18:28:11 +02003800}
3801
Chris Wilsonc0336662016-05-06 15:40:21 +01003802void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003803{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003804 struct drm_device *dev = &dev_priv->drm;
3805 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
Chris Wilson40da1d32018-04-05 13:37:14 +01003806 struct drm_atomic_state *state;
Maarten Lankhorst73974892016-08-05 23:28:27 +03003807 int ret;
3808
Daniel Vetterce87ea12017-07-19 14:54:55 +02003809 /* reset doesn't touch the display */
Chris Wilson40da1d32018-04-05 13:37:14 +01003810 if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
Daniel Vetterce87ea12017-07-19 14:54:55 +02003811 return;
3812
Chris Wilson40da1d32018-04-05 13:37:14 +01003813 state = fetch_and_zero(&dev_priv->modeset_restore_state);
Daniel Vetterce87ea12017-07-19 14:54:55 +02003814 if (!state)
3815 goto unlock;
3816
Ville Syrjälä75147472014-11-24 18:28:11 +02003817 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003818 if (!gpu_reset_clobbers_display(dev_priv)) {
Daniel Vetterce87ea12017-07-19 14:54:55 +02003819 /* for testing only restore the display */
3820 ret = __intel_display_resume(dev, state, ctx);
Chris Wilson942d5d02017-08-28 11:46:04 +01003821 if (ret)
3822 DRM_ERROR("Restoring old state failed with %i\n", ret);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003823 } else {
3824 /*
3825 * The display has been reset as well,
3826 * so need a full re-initialization.
3827 */
3828 intel_runtime_pm_disable_interrupts(dev_priv);
3829 intel_runtime_pm_enable_interrupts(dev_priv);
3830
Imre Deak51f59202016-09-14 13:04:13 +03003831 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003832 intel_modeset_init_hw(dev);
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02003833 intel_init_clock_gating(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003834
3835 spin_lock_irq(&dev_priv->irq_lock);
3836 if (dev_priv->display.hpd_irq_setup)
3837 dev_priv->display.hpd_irq_setup(dev_priv);
3838 spin_unlock_irq(&dev_priv->irq_lock);
3839
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003840 ret = __intel_display_resume(dev, state, ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003841 if (ret)
3842 DRM_ERROR("Restoring old state failed with %i\n", ret);
3843
3844 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003845 }
3846
Daniel Vetterce87ea12017-07-19 14:54:55 +02003847 drm_atomic_state_put(state);
3848unlock:
Maarten Lankhorst73974892016-08-05 23:28:27 +03003849 drm_modeset_drop_locks(ctx);
3850 drm_modeset_acquire_fini(ctx);
3851 mutex_unlock(&dev->mode_config.mutex);
Daniel Vetter9db529a2017-08-08 10:08:28 +02003852
3853 clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
Ville Syrjälä75147472014-11-24 18:28:11 +02003854}
3855
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003856static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
3857 const struct intel_crtc_state *new_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003858{
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003859 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003860 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003861
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003862 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003863 crtc->base.mode = new_crtc_state->base.mode;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003864
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003865 /*
3866 * Update pipe size and adjust fitter if needed: the reason for this is
3867 * that in compute_mode_changes we check the native mode (not the pfit
3868 * mode) to see if we can flip rather than do a full mode set. In the
3869 * fastboot case, we'll flip, but if we don't update the pipesrc and
3870 * pfit state, we'll end up with a big fb scanned out into the wrong
3871 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003872 */
3873
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003874 I915_WRITE(PIPESRC(crtc->pipe),
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003875 ((new_crtc_state->pipe_src_w - 1) << 16) |
3876 (new_crtc_state->pipe_src_h - 1));
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003877
3878 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003879 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +02003880 skl_detach_scalers(new_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003881
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003882 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003883 skylake_pfit_enable(new_crtc_state);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003884 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä1a15b772017-08-23 18:22:25 +03003885 if (new_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003886 ironlake_pfit_enable(new_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003887 else if (old_crtc_state->pch_pfit.enabled)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02003888 ironlake_pfit_disable(old_crtc_state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003889 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003890}
3891
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003892static void intel_fdi_normal_train(struct intel_crtc *crtc)
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003893{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003894 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003895 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003896 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003897 i915_reg_t reg;
3898 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003899
3900 /* enable normal train */
3901 reg = FDI_TX_CTL(pipe);
3902 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003903 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003904 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3905 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003906 } else {
3907 temp &= ~FDI_LINK_TRAIN_NONE;
3908 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003909 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003910 I915_WRITE(reg, temp);
3911
3912 reg = FDI_RX_CTL(pipe);
3913 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003914 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003915 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3916 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3917 } else {
3918 temp &= ~FDI_LINK_TRAIN_NONE;
3919 temp |= FDI_LINK_TRAIN_NONE;
3920 }
3921 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3922
3923 /* wait one idle pattern time */
3924 POSTING_READ(reg);
3925 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003926
3927 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003928 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003929 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3930 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003931}
3932
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003933/* The FDI link training functions for ILK/Ibexpeak. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003934static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3935 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003936{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003937 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003938 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02003939 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003940 i915_reg_t reg;
3941 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003942
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003943 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003944 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003945
Adam Jacksone1a44742010-06-25 15:32:14 -04003946 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3947 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003948 reg = FDI_RX_IMR(pipe);
3949 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003950 temp &= ~FDI_RX_SYMBOL_LOCK;
3951 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003952 I915_WRITE(reg, temp);
3953 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003954 udelay(150);
3955
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003956 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003957 reg = FDI_TX_CTL(pipe);
3958 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003959 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02003960 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003961 temp &= ~FDI_LINK_TRAIN_NONE;
3962 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003963 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003964
Chris Wilson5eddb702010-09-11 13:48:45 +01003965 reg = FDI_RX_CTL(pipe);
3966 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003967 temp &= ~FDI_LINK_TRAIN_NONE;
3968 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003969 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3970
3971 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003972 udelay(150);
3973
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003974 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003975 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3976 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3977 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003978
Chris Wilson5eddb702010-09-11 13:48:45 +01003979 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003980 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003981 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003982 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3983
3984 if ((temp & FDI_RX_BIT_LOCK)) {
3985 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003986 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003987 break;
3988 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003989 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003990 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003991 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003992
3993 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003994 reg = FDI_TX_CTL(pipe);
3995 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003996 temp &= ~FDI_LINK_TRAIN_NONE;
3997 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003998 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003999
Chris Wilson5eddb702010-09-11 13:48:45 +01004000 reg = FDI_RX_CTL(pipe);
4001 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004002 temp &= ~FDI_LINK_TRAIN_NONE;
4003 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01004004 I915_WRITE(reg, temp);
4005
4006 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004007 udelay(150);
4008
Chris Wilson5eddb702010-09-11 13:48:45 +01004009 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04004010 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004011 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004012 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4013
4014 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004015 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004016 DRM_DEBUG_KMS("FDI train 2 done.\n");
4017 break;
4018 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004019 }
Adam Jacksone1a44742010-06-25 15:32:14 -04004020 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01004021 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004022
4023 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07004024
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004025}
4026
Akshay Joshi0206e352011-08-16 15:34:10 -04004027static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004028 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
4029 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
4030 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
4031 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
4032};
4033
4034/* The FDI link training functions for SNB/Cougarpoint. */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004035static void gen6_fdi_link_train(struct intel_crtc *crtc,
4036 const struct intel_crtc_state *crtc_state)
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004037{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004038 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004039 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004040 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004041 i915_reg_t reg;
4042 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004043
Adam Jacksone1a44742010-06-25 15:32:14 -04004044 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4045 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01004046 reg = FDI_RX_IMR(pipe);
4047 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004048 temp &= ~FDI_RX_SYMBOL_LOCK;
4049 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01004050 I915_WRITE(reg, temp);
4051
4052 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04004053 udelay(150);
4054
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004055 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01004056 reg = FDI_TX_CTL(pipe);
4057 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004058 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004059 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004060 temp &= ~FDI_LINK_TRAIN_NONE;
4061 temp |= FDI_LINK_TRAIN_PATTERN_1;
4062 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4063 /* SNB-B */
4064 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01004065 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004066
Daniel Vetterd74cf322012-10-26 10:58:13 +02004067 I915_WRITE(FDI_RX_MISC(pipe),
4068 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4069
Chris Wilson5eddb702010-09-11 13:48:45 +01004070 reg = FDI_RX_CTL(pipe);
4071 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004072 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004073 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4074 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4075 } else {
4076 temp &= ~FDI_LINK_TRAIN_NONE;
4077 temp |= FDI_LINK_TRAIN_PATTERN_1;
4078 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004079 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4080
4081 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004082 udelay(150);
4083
Akshay Joshi0206e352011-08-16 15:34:10 -04004084 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004085 reg = FDI_TX_CTL(pipe);
4086 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004087 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4088 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004089 I915_WRITE(reg, temp);
4090
4091 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004092 udelay(500);
4093
Sean Paulfa37d392012-03-02 12:53:39 -05004094 for (retry = 0; retry < 5; retry++) {
4095 reg = FDI_RX_IIR(pipe);
4096 temp = I915_READ(reg);
4097 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4098 if (temp & FDI_RX_BIT_LOCK) {
4099 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4100 DRM_DEBUG_KMS("FDI train 1 done.\n");
4101 break;
4102 }
4103 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004104 }
Sean Paulfa37d392012-03-02 12:53:39 -05004105 if (retry < 5)
4106 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004107 }
4108 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004109 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004110
4111 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01004112 reg = FDI_TX_CTL(pipe);
4113 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004114 temp &= ~FDI_LINK_TRAIN_NONE;
4115 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004116 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004117 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4118 /* SNB-B */
4119 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
4120 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004121 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004122
Chris Wilson5eddb702010-09-11 13:48:45 +01004123 reg = FDI_RX_CTL(pipe);
4124 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004125 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004126 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4127 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
4128 } else {
4129 temp &= ~FDI_LINK_TRAIN_NONE;
4130 temp |= FDI_LINK_TRAIN_PATTERN_2;
4131 }
Chris Wilson5eddb702010-09-11 13:48:45 +01004132 I915_WRITE(reg, temp);
4133
4134 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004135 udelay(150);
4136
Akshay Joshi0206e352011-08-16 15:34:10 -04004137 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004138 reg = FDI_TX_CTL(pipe);
4139 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004140 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4141 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 I915_WRITE(reg, temp);
4143
4144 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004145 udelay(500);
4146
Sean Paulfa37d392012-03-02 12:53:39 -05004147 for (retry = 0; retry < 5; retry++) {
4148 reg = FDI_RX_IIR(pipe);
4149 temp = I915_READ(reg);
4150 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4151 if (temp & FDI_RX_SYMBOL_LOCK) {
4152 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4153 DRM_DEBUG_KMS("FDI train 2 done.\n");
4154 break;
4155 }
4156 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004157 }
Sean Paulfa37d392012-03-02 12:53:39 -05004158 if (retry < 5)
4159 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004160 }
4161 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004163
4164 DRM_DEBUG_KMS("FDI train done.\n");
4165}
4166
Jesse Barnes357555c2011-04-28 15:09:55 -07004167/* Manual link training for Ivy Bridge A0 parts */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004168static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4169 const struct intel_crtc_state *crtc_state)
Jesse Barnes357555c2011-04-28 15:09:55 -07004170{
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004171 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004172 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004173 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004174 i915_reg_t reg;
4175 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004176
4177 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4178 for train result */
4179 reg = FDI_RX_IMR(pipe);
4180 temp = I915_READ(reg);
4181 temp &= ~FDI_RX_SYMBOL_LOCK;
4182 temp &= ~FDI_RX_BIT_LOCK;
4183 I915_WRITE(reg, temp);
4184
4185 POSTING_READ(reg);
4186 udelay(150);
4187
Daniel Vetter01a415f2012-10-27 15:58:40 +02004188 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4189 I915_READ(FDI_RX_IIR(pipe)));
4190
Jesse Barnes139ccd32013-08-19 11:04:55 -07004191 /* Try each vswing and preemphasis setting twice before moving on */
4192 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4193 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004194 reg = FDI_TX_CTL(pipe);
4195 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004196 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4197 temp &= ~FDI_TX_ENABLE;
4198 I915_WRITE(reg, temp);
4199
4200 reg = FDI_RX_CTL(pipe);
4201 temp = I915_READ(reg);
4202 temp &= ~FDI_LINK_TRAIN_AUTO;
4203 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4204 temp &= ~FDI_RX_ENABLE;
4205 I915_WRITE(reg, temp);
4206
4207 /* enable CPU FDI TX and PCH FDI RX */
4208 reg = FDI_TX_CTL(pipe);
4209 temp = I915_READ(reg);
4210 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004211 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004212 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004213 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004214 temp |= snb_b_fdi_train_param[j/2];
4215 temp |= FDI_COMPOSITE_SYNC;
4216 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4217
4218 I915_WRITE(FDI_RX_MISC(pipe),
4219 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4220
4221 reg = FDI_RX_CTL(pipe);
4222 temp = I915_READ(reg);
4223 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4224 temp |= FDI_COMPOSITE_SYNC;
4225 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4226
4227 POSTING_READ(reg);
4228 udelay(1); /* should be 0.5us */
4229
4230 for (i = 0; i < 4; i++) {
4231 reg = FDI_RX_IIR(pipe);
4232 temp = I915_READ(reg);
4233 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4234
4235 if (temp & FDI_RX_BIT_LOCK ||
4236 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4237 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4238 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4239 i);
4240 break;
4241 }
4242 udelay(1); /* should be 0.5us */
4243 }
4244 if (i == 4) {
4245 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4246 continue;
4247 }
4248
4249 /* Train 2 */
4250 reg = FDI_TX_CTL(pipe);
4251 temp = I915_READ(reg);
4252 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4253 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4254 I915_WRITE(reg, temp);
4255
4256 reg = FDI_RX_CTL(pipe);
4257 temp = I915_READ(reg);
4258 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4259 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004260 I915_WRITE(reg, temp);
4261
4262 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004263 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004264
Jesse Barnes139ccd32013-08-19 11:04:55 -07004265 for (i = 0; i < 4; i++) {
4266 reg = FDI_RX_IIR(pipe);
4267 temp = I915_READ(reg);
4268 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004269
Jesse Barnes139ccd32013-08-19 11:04:55 -07004270 if (temp & FDI_RX_SYMBOL_LOCK ||
4271 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4272 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4273 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4274 i);
4275 goto train_done;
4276 }
4277 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004278 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004279 if (i == 4)
4280 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004281 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004282
Jesse Barnes139ccd32013-08-19 11:04:55 -07004283train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004284 DRM_DEBUG_KMS("FDI train done.\n");
4285}
4286
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004287static void ironlake_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004288{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
4290 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004291 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004292 i915_reg_t reg;
4293 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004294
Jesse Barnes0e23b992010-09-10 11:10:00 -07004295 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004296 reg = FDI_RX_CTL(pipe);
4297 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004298 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02004299 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004300 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004301 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4302
4303 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004304 udelay(200);
4305
4306 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004307 temp = I915_READ(reg);
4308 I915_WRITE(reg, temp | FDI_PCDCLK);
4309
4310 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004311 udelay(200);
4312
Paulo Zanoni20749732012-11-23 15:30:38 -02004313 /* Enable CPU FDI TX PLL, always on for Ironlake */
4314 reg = FDI_TX_CTL(pipe);
4315 temp = I915_READ(reg);
4316 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4317 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004318
Paulo Zanoni20749732012-11-23 15:30:38 -02004319 POSTING_READ(reg);
4320 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004321 }
4322}
4323
Daniel Vetter88cefb62012-08-12 19:27:14 +02004324static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4325{
4326 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004327 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004328 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004329 i915_reg_t reg;
4330 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004331
4332 /* Switch from PCDclk to Rawclk */
4333 reg = FDI_RX_CTL(pipe);
4334 temp = I915_READ(reg);
4335 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4336
4337 /* Disable CPU FDI TX PLL */
4338 reg = FDI_TX_CTL(pipe);
4339 temp = I915_READ(reg);
4340 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4341
4342 POSTING_READ(reg);
4343 udelay(100);
4344
4345 reg = FDI_RX_CTL(pipe);
4346 temp = I915_READ(reg);
4347 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4348
4349 /* Wait for the clocks to turn off. */
4350 POSTING_READ(reg);
4351 udelay(100);
4352}
4353
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004354static void ironlake_fdi_disable(struct drm_crtc *crtc)
4355{
4356 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004357 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004358 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4359 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004360 i915_reg_t reg;
4361 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004362
4363 /* disable CPU FDI tx and PCH FDI rx */
4364 reg = FDI_TX_CTL(pipe);
4365 temp = I915_READ(reg);
4366 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4367 POSTING_READ(reg);
4368
4369 reg = FDI_RX_CTL(pipe);
4370 temp = I915_READ(reg);
4371 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004372 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004373 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4374
4375 POSTING_READ(reg);
4376 udelay(100);
4377
4378 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004379 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004380 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004381
4382 /* still set train pattern 1 */
4383 reg = FDI_TX_CTL(pipe);
4384 temp = I915_READ(reg);
4385 temp &= ~FDI_LINK_TRAIN_NONE;
4386 temp |= FDI_LINK_TRAIN_PATTERN_1;
4387 I915_WRITE(reg, temp);
4388
4389 reg = FDI_RX_CTL(pipe);
4390 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004391 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004392 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4393 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4394 } else {
4395 temp &= ~FDI_LINK_TRAIN_NONE;
4396 temp |= FDI_LINK_TRAIN_PATTERN_1;
4397 }
4398 /* BPC in FDI rx is consistent with that in PIPECONF */
4399 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004400 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004401 I915_WRITE(reg, temp);
4402
4403 POSTING_READ(reg);
4404 udelay(100);
4405}
4406
Chris Wilson49d73912016-11-29 09:50:08 +00004407bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004408{
Daniel Vetterfa058872017-07-20 19:57:52 +02004409 struct drm_crtc *crtc;
4410 bool cleanup_done;
Chris Wilson5dce5b932014-01-20 10:17:36 +00004411
Daniel Vetterfa058872017-07-20 19:57:52 +02004412 drm_for_each_crtc(crtc, &dev_priv->drm) {
4413 struct drm_crtc_commit *commit;
4414 spin_lock(&crtc->commit_lock);
4415 commit = list_first_entry_or_null(&crtc->commit_list,
4416 struct drm_crtc_commit, commit_entry);
4417 cleanup_done = commit ?
4418 try_wait_for_completion(&commit->cleanup_done) : true;
4419 spin_unlock(&crtc->commit_lock);
4420
4421 if (cleanup_done)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004422 continue;
4423
Daniel Vetterfa058872017-07-20 19:57:52 +02004424 drm_crtc_wait_one_vblank(crtc);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004425
4426 return true;
4427 }
4428
4429 return false;
4430}
4431
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004432void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004433{
4434 u32 temp;
4435
4436 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4437
4438 mutex_lock(&dev_priv->sb_lock);
4439
4440 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4441 temp |= SBI_SSCCTL_DISABLE;
4442 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4443
4444 mutex_unlock(&dev_priv->sb_lock);
4445}
4446
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004447/* Program iCLKIP clock to the desired frequency */
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004448static void lpt_program_iclkip(struct intel_crtc *crtc)
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004449{
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004450 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4451 int clock = crtc->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004452 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4453 u32 temp;
4454
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004455 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004456
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004457 /* The iCLK virtual clock root frequency is in MHz,
4458 * but the adjusted_mode->crtc_clock in in KHz. To get the
4459 * divisors, it is necessary to divide one by another, so we
4460 * convert the virtual clock precision to KHz here for higher
4461 * precision.
4462 */
4463 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004464 u32 iclk_virtual_root_freq = 172800 * 1000;
4465 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004466 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004467
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004468 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4469 clock << auxdiv);
4470 divsel = (desired_divisor / iclk_pi_range) - 2;
4471 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004472
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004473 /*
4474 * Near 20MHz is a corner case which is
4475 * out of range for the 7-bit divisor
4476 */
4477 if (divsel <= 0x7f)
4478 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004479 }
4480
4481 /* This should not happen with any sane values */
4482 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4483 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4484 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4485 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4486
4487 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 +03004488 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004489 auxdiv,
4490 divsel,
4491 phasedir,
4492 phaseinc);
4493
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004494 mutex_lock(&dev_priv->sb_lock);
4495
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004496 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004497 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004498 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4499 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4500 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4501 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4502 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4503 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004504 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004505
4506 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004507 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004508 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4509 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004510 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004511
4512 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004513 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004514 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004515 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004516
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004517 mutex_unlock(&dev_priv->sb_lock);
4518
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004519 /* Wait for initialization time */
4520 udelay(24);
4521
4522 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4523}
4524
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004525int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4526{
4527 u32 divsel, phaseinc, auxdiv;
4528 u32 iclk_virtual_root_freq = 172800 * 1000;
4529 u32 iclk_pi_range = 64;
4530 u32 desired_divisor;
4531 u32 temp;
4532
4533 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4534 return 0;
4535
4536 mutex_lock(&dev_priv->sb_lock);
4537
4538 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4539 if (temp & SBI_SSCCTL_DISABLE) {
4540 mutex_unlock(&dev_priv->sb_lock);
4541 return 0;
4542 }
4543
4544 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4545 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4546 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4547 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4548 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4549
4550 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4551 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4552 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4553
4554 mutex_unlock(&dev_priv->sb_lock);
4555
4556 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4557
4558 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4559 desired_divisor << auxdiv);
4560}
4561
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004562static void ironlake_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
Daniel Vetter275f01b22013-05-03 11:49:47 +02004563 enum pipe pch_transcoder)
4564{
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004565 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4566 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4567 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004568
4569 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4570 I915_READ(HTOTAL(cpu_transcoder)));
4571 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4572 I915_READ(HBLANK(cpu_transcoder)));
4573 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4574 I915_READ(HSYNC(cpu_transcoder)));
4575
4576 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4577 I915_READ(VTOTAL(cpu_transcoder)));
4578 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4579 I915_READ(VBLANK(cpu_transcoder)));
4580 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4581 I915_READ(VSYNC(cpu_transcoder)));
4582 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4583 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4584}
4585
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004586static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004587{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004588 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004589 uint32_t temp;
4590
4591 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004592 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004593 return;
4594
4595 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4596 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4597
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004598 temp &= ~FDI_BC_BIFURCATION_SELECT;
4599 if (enable)
4600 temp |= FDI_BC_BIFURCATION_SELECT;
4601
4602 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004603 I915_WRITE(SOUTH_CHICKEN1, temp);
4604 POSTING_READ(SOUTH_CHICKEN1);
4605}
4606
4607static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4608{
4609 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004610
4611 switch (intel_crtc->pipe) {
4612 case PIPE_A:
4613 break;
4614 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004615 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004616 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004617 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004618 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004619
4620 break;
4621 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004622 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004623
4624 break;
4625 default:
4626 BUG();
4627 }
4628}
4629
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004630/*
4631 * Finds the encoder associated with the given CRTC. This can only be
4632 * used when we know that the CRTC isn't feeding multiple encoders!
4633 */
4634static struct intel_encoder *
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004635intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
4636 const struct intel_crtc_state *crtc_state)
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004637{
4638 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004639 const struct drm_connector_state *connector_state;
4640 const struct drm_connector *connector;
4641 struct intel_encoder *encoder = NULL;
4642 int num_encoders = 0;
4643 int i;
4644
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004645 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
Ville Syrjäläf606bc62018-05-18 18:29:25 +03004646 if (connector_state->crtc != &crtc->base)
4647 continue;
4648
4649 encoder = to_intel_encoder(connector_state->best_encoder);
4650 num_encoders++;
4651 }
4652
4653 WARN(num_encoders != 1, "%d encoders for pipe %c\n",
4654 num_encoders, pipe_name(crtc->pipe));
4655
4656 return encoder;
4657}
4658
Jesse Barnesf67a5592011-01-05 10:31:48 -08004659/*
4660 * Enable PCH resources required for PCH ports:
4661 * - PCH PLLs
4662 * - FDI training & RX/TX
4663 * - update transcoder timings
4664 * - DP transcoding bits
4665 * - transcoder
4666 */
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004667static void ironlake_pch_enable(const struct intel_atomic_state *state,
4668 const struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08004669{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004670 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004671 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004672 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004673 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004674 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004675
Daniel Vetterab9412b2013-05-03 11:49:46 +02004676 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004677
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004678 if (IS_IVYBRIDGE(dev_priv))
Ander Conselvan de Oliveira4cbe4b22017-03-02 14:58:51 +02004679 ivybridge_update_fdi_bc_bifurcation(crtc);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004680
Daniel Vettercd986ab2012-10-26 10:58:12 +02004681 /* Write the TU size bits before fdi link training, so that error
4682 * detection works. */
4683 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4684 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4685
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004686 /* For PCH output, training FDI link */
Ander Conselvan de Oliveiradc4a1092017-03-02 14:58:54 +02004687 dev_priv->display.fdi_link_train(crtc, crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004688
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004689 /* We need to program the right clock selection before writing the pixel
4690 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004691 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004692 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004693
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004694 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004695 temp |= TRANS_DPLL_ENABLE(pipe);
4696 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004697 if (crtc_state->shared_dpll ==
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004698 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004699 temp |= sel;
4700 else
4701 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004702 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004703 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004704
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004705 /* XXX: pch pll's can be enabled any time before we enable the PCH
4706 * transcoder, and we actually should do this to not upset any PCH
4707 * transcoder that already use the clock when we share it.
4708 *
4709 * Note that enable_shared_dpll tries to do the right thing, but
4710 * get_shared_dpll unconditionally resets the pll - we need that to have
4711 * the right LVDS enable sequence. */
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02004712 intel_enable_shared_dpll(crtc_state);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004713
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004714 /* set transcoder timing, panel must allow it */
4715 assert_panel_unlocked(dev_priv, pipe);
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004716 ironlake_pch_transcoder_set_timings(crtc_state, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004717
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004718 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004719
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004720 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004721 if (HAS_PCH_CPT(dev_priv) &&
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004722 intel_crtc_has_dp_encoder(crtc_state)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004723 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004724 &crtc_state->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004725 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004726 i915_reg_t reg = TRANS_DP_CTL(pipe);
Ville Syrjäläf67dc6d2018-05-18 18:29:26 +03004727 enum port port;
4728
Chris Wilson5eddb702010-09-11 13:48:45 +01004729 temp = I915_READ(reg);
4730 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004731 TRANS_DP_SYNC_MASK |
4732 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004733 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004734 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004735
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004736 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004737 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004738 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004739 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004740
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004741 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
Ville Syrjäläf67dc6d2018-05-18 18:29:26 +03004742 WARN_ON(port < PORT_B || port > PORT_D);
4743 temp |= TRANS_DP_PORT_SEL(port);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004744
Chris Wilson5eddb702010-09-11 13:48:45 +01004745 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004746 }
4747
Maarten Lankhorst7efd90f2018-10-04 11:45:55 +02004748 ironlake_enable_pch_transcoder(crtc_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004749}
4750
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03004751static void lpt_pch_enable(const struct intel_atomic_state *state,
4752 const struct intel_crtc_state *crtc_state)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004753{
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004754 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveira0dcdc382017-03-02 14:58:52 +02004755 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira2ce42272017-03-02 14:58:53 +02004756 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004757
Matthias Kaehlckea2196032017-07-17 11:14:03 -07004758 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004759
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004760 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004761
Paulo Zanoni0540e482012-10-31 18:12:40 -02004762 /* Set transcoder timing. */
Maarten Lankhorst5e1cdf52018-10-04 11:45:58 +02004763 ironlake_pch_transcoder_set_timings(crtc_state, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004764
Paulo Zanoni937bb612012-10-31 18:12:47 -02004765 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004766}
4767
Daniel Vettera1520312013-05-03 11:49:50 +02004768static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004769{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004770 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004771 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004772 u32 temp;
4773
4774 temp = I915_READ(dslreg);
4775 udelay(500);
4776 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004777 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004778 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004779 }
4780}
4781
Ville Syrjälä0a599522018-05-21 21:56:13 +03004782/*
4783 * The hardware phase 0.0 refers to the center of the pixel.
4784 * We want to start from the top/left edge which is phase
4785 * -0.5. That matches how the hardware calculates the scaling
4786 * factors (from top-left of the first pixel to bottom-right
4787 * of the last pixel, as opposed to the pixel centers).
4788 *
4789 * For 4:2:0 subsampled chroma planes we obviously have to
4790 * adjust that so that the chroma sample position lands in
4791 * the right spot.
4792 *
4793 * Note that for packed YCbCr 4:2:2 formats there is no way to
4794 * control chroma siting. The hardware simply replicates the
4795 * chroma samples for both of the luma samples, and thus we don't
4796 * actually get the expected MPEG2 chroma siting convention :(
4797 * The same behaviour is observed on pre-SKL platforms as well.
4798 */
4799u16 skl_scaler_calc_phase(int sub, bool chroma_cosited)
4800{
4801 int phase = -0x8000;
4802 u16 trip = 0;
4803
4804 if (chroma_cosited)
4805 phase += (sub - 1) * 0x8000 / sub;
4806
4807 if (phase < 0)
4808 phase = 0x10000 + phase;
4809 else
4810 trip = PS_PHASE_TRIP;
4811
4812 return ((phase >> 2) & PS_PHASE_MASK) | trip;
4813}
4814
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004815static int
4816skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004817 unsigned int scaler_user, int *scaler_id,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304818 int src_w, int src_h, int dst_w, int dst_h,
4819 bool plane_scaler_check,
4820 uint32_t pixel_format)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004821{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004822 struct intel_crtc_scaler_state *scaler_state =
4823 &crtc_state->scaler_state;
4824 struct intel_crtc *intel_crtc =
4825 to_intel_crtc(crtc_state->base.crtc);
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304826 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4827 const struct drm_display_mode *adjusted_mode =
4828 &crtc_state->base.adjusted_mode;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004829 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004830
Ville Syrjäläd96a7d22017-03-31 21:00:54 +03004831 /*
4832 * Src coordinates are already rotated by 270 degrees for
4833 * the 90/270 degree plane rotation cases (to match the
4834 * GTT mapping), hence no need to account for rotation here.
4835 */
4836 need_scaling = src_w != dst_w || src_h != dst_h;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004837
Chandra Konduru77224cd2018-04-09 09:11:13 +05304838 if (plane_scaler_check)
4839 if (pixel_format == DRM_FORMAT_NV12)
4840 need_scaling = true;
4841
Shashank Sharmae5c05932017-07-21 20:55:05 +05304842 if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
4843 need_scaling = true;
4844
Chandra Kondurua1b22782015-04-07 15:28:45 -07004845 /*
Mahesh Kumar7f58cbb2017-06-30 17:41:00 +05304846 * Scaling/fitting not supported in IF-ID mode in GEN9+
4847 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4848 * Once NV12 is enabled, handle it here while allocating scaler
4849 * for NV12.
4850 */
4851 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
4852 need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4853 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4854 return -EINVAL;
4855 }
4856
4857 /*
Chandra Kondurua1b22782015-04-07 15:28:45 -07004858 * if plane is being disabled or scaler is no more required or force detach
4859 * - free scaler binded to this plane/crtc
4860 * - in order to do this, update crtc->scaler_usage
4861 *
4862 * Here scaler state in crtc_state is set free so that
4863 * scaler can be assigned to other user. Actual register
4864 * update to free the scaler is done in plane/panel-fit programming.
4865 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4866 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004867 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004868 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004869 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004870 scaler_state->scalers[*scaler_id].in_use = 0;
4871
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004872 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4873 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4874 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004875 scaler_state->scaler_users);
4876 *scaler_id = -1;
4877 }
4878 return 0;
4879 }
4880
Chandra Konduru77224cd2018-04-09 09:11:13 +05304881 if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
Maarten Lankhorst5d794282018-05-12 03:03:14 +05304882 (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
Chandra Konduru77224cd2018-04-09 09:11:13 +05304883 DRM_DEBUG_KMS("NV12: src dimensions not met\n");
4884 return -EINVAL;
4885 }
4886
Chandra Kondurua1b22782015-04-07 15:28:45 -07004887 /* range checks */
4888 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
Nabendu Maiti323301a2018-03-23 10:24:18 -07004889 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4890 (IS_GEN11(dev_priv) &&
4891 (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
4892 dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
4893 (!IS_GEN11(dev_priv) &&
4894 (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4895 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004896 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004897 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004898 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004899 return -EINVAL;
4900 }
4901
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004902 /* mark this plane as a scaler user in crtc_state */
4903 scaler_state->scaler_users |= (1 << scaler_user);
4904 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4905 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4906 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4907 scaler_state->scaler_users);
4908
4909 return 0;
4910}
4911
4912/**
4913 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4914 *
4915 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004916 *
4917 * Return
4918 * 0 - scaler_usage updated successfully
4919 * error - requested scaling cannot be supported or other error condition
4920 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004921int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004922{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004923 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004924
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004925 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Chandra Konduru77224cd2018-04-09 09:11:13 +05304926 &state->scaler_state.scaler_id,
4927 state->pipe_src_w, state->pipe_src_h,
4928 adjusted_mode->crtc_hdisplay,
4929 adjusted_mode->crtc_vdisplay, false, 0);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004930}
4931
4932/**
4933 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
Chris Wilsonc38c1452018-02-14 13:49:22 +00004934 * @crtc_state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004935 * @plane_state: atomic plane state to update
4936 *
4937 * Return
4938 * 0 - scaler_usage updated successfully
4939 * error - requested scaling cannot be supported or other error condition
4940 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004941static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4942 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004943{
4944
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004945 struct intel_plane *intel_plane =
4946 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004947 struct drm_framebuffer *fb = plane_state->base.fb;
4948 int ret;
4949
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004950 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004951
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004952 ret = skl_update_scaler(crtc_state, force_detach,
4953 drm_plane_index(&intel_plane->base),
4954 &plane_state->scaler_id,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004955 drm_rect_width(&plane_state->base.src) >> 16,
4956 drm_rect_height(&plane_state->base.src) >> 16,
4957 drm_rect_width(&plane_state->base.dst),
Chandra Konduru77224cd2018-04-09 09:11:13 +05304958 drm_rect_height(&plane_state->base.dst),
4959 fb ? true : false, fb ? fb->format->format : 0);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004960
4961 if (ret || plane_state->scaler_id < 0)
4962 return ret;
4963
Chandra Kondurua1b22782015-04-07 15:28:45 -07004964 /* check colorkey */
Ville Syrjälä6ec5bd32018-02-02 22:42:31 +02004965 if (plane_state->ckey.flags) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004966 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4967 intel_plane->base.base.id,
4968 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004969 return -EINVAL;
4970 }
4971
4972 /* Check src format */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004973 switch (fb->format->format) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004974 case DRM_FORMAT_RGB565:
4975 case DRM_FORMAT_XBGR8888:
4976 case DRM_FORMAT_XRGB8888:
4977 case DRM_FORMAT_ABGR8888:
4978 case DRM_FORMAT_ARGB8888:
4979 case DRM_FORMAT_XRGB2101010:
4980 case DRM_FORMAT_XBGR2101010:
4981 case DRM_FORMAT_YUYV:
4982 case DRM_FORMAT_YVYU:
4983 case DRM_FORMAT_UYVY:
4984 case DRM_FORMAT_VYUY:
Chandra Konduru77224cd2018-04-09 09:11:13 +05304985 case DRM_FORMAT_NV12:
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004986 break;
4987 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004988 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4989 intel_plane->base.base.id, intel_plane->base.name,
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004990 fb->base.id, fb->format->format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004991 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004992 }
4993
Chandra Kondurua1b22782015-04-07 15:28:45 -07004994 return 0;
4995}
4996
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004997static void skylake_scaler_disable(struct intel_crtc *crtc)
4998{
4999 int i;
5000
5001 for (i = 0; i < crtc->num_scalers; i++)
5002 skl_detach_scaler(crtc, i);
5003}
5004
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005005static void skylake_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005006{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005007 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5008 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5009 enum pipe pipe = crtc->pipe;
5010 const struct intel_crtc_scaler_state *scaler_state =
5011 &crtc_state->scaler_state;
Chandra Kondurua1b22782015-04-07 15:28:45 -07005012
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005013 if (crtc_state->pch_pfit.enabled) {
Ville Syrjälä0a599522018-05-21 21:56:13 +03005014 u16 uv_rgb_hphase, uv_rgb_vphase;
Chandra Kondurua1b22782015-04-07 15:28:45 -07005015 int id;
5016
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005017 if (WARN_ON(crtc_state->scaler_state.scaler_id < 0))
Chandra Kondurua1b22782015-04-07 15:28:45 -07005018 return;
Chandra Kondurua1b22782015-04-07 15:28:45 -07005019
Ville Syrjälä0a599522018-05-21 21:56:13 +03005020 uv_rgb_hphase = skl_scaler_calc_phase(1, false);
5021 uv_rgb_vphase = skl_scaler_calc_phase(1, false);
5022
Chandra Kondurua1b22782015-04-07 15:28:45 -07005023 id = scaler_state->scaler_id;
5024 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
5025 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
Ville Syrjälä0a599522018-05-21 21:56:13 +03005026 I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
5027 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
5028 I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
5029 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005030 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc_state->pch_pfit.pos);
5031 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc_state->pch_pfit.size);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005032 }
5033}
5034
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005035static void ironlake_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnesb074cec2013-04-25 12:55:02 -07005036{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005037 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5038 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07005039 int pipe = crtc->pipe;
5040
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005041 if (crtc_state->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07005042 /* Force use of hard-coded filter coefficients
5043 * as some pre-programmed values are broken,
5044 * e.g. x201.
5045 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005046 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07005047 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
5048 PF_PIPE_SEL_IVB(pipe));
5049 else
5050 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005051 I915_WRITE(PF_WIN_POS(pipe), crtc_state->pch_pfit.pos);
5052 I915_WRITE(PF_WIN_SZ(pipe), crtc_state->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08005053 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005054}
5055
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005056void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005057{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005058 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03005059 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005060 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005061
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005062 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005063 return;
5064
Maarten Lankhorst307e4492016-03-23 14:33:28 +01005065 /*
5066 * We can only enable IPS after we enable a plane and wait for a vblank
5067 * This function is called from post_plane_update, which is run after
5068 * a vblank wait.
5069 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005070 WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02005071
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005072 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005073 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjälä61843f02017-09-12 18:34:11 +03005074 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
5075 IPS_ENABLE | IPS_PCODE_CONTROL));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005076 mutex_unlock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005077 /* Quoting Art Runyan: "its not safe to expect any particular
5078 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08005079 * mailbox." Moreover, the mailbox may return a bogus state,
5080 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005081 */
5082 } else {
5083 I915_WRITE(IPS_CTL, IPS_ENABLE);
5084 /* The bit only becomes 1 in the next vblank, so this wait here
5085 * is essentially intel_wait_for_vblank. If we don't have this
5086 * and don't wait for vblanks until the end of crtc_enable, then
5087 * the HW state readout code will complain that the expected
5088 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01005089 if (intel_wait_for_register(dev_priv,
5090 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
5091 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005092 DRM_ERROR("Timed out waiting for IPS enable\n");
5093 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005094}
5095
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005096void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005097{
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005098 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005099 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005100 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005101
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005102 if (!crtc_state->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03005103 return;
5104
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005105 if (IS_BROADWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005106 mutex_lock(&dev_priv->pcu_lock);
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005107 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005108 mutex_unlock(&dev_priv->pcu_lock);
Imre Deakacb3ef02018-09-05 13:00:05 +03005109 /*
5110 * Wait for PCODE to finish disabling IPS. The BSpec specified
5111 * 42ms timeout value leads to occasional timeouts so use 100ms
5112 * instead.
5113 */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01005114 if (intel_wait_for_register(dev_priv,
5115 IPS_CTL, IPS_ENABLE, 0,
Imre Deakacb3ef02018-09-05 13:00:05 +03005116 100))
Ben Widawsky23d0b132014-04-10 14:32:41 -07005117 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08005118 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07005119 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08005120 POSTING_READ(IPS_CTL);
5121 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03005122
5123 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005124 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03005125}
5126
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005127static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005128{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005129 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005130 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005131
5132 mutex_lock(&dev->struct_mutex);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005133 (void) intel_overlay_switch_off(intel_crtc->overlay);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005134 mutex_unlock(&dev->struct_mutex);
5135 }
5136
5137 /* Let userspace switch the overlay on again. In most cases userspace
5138 * has to recompute where to put it anyway.
5139 */
5140}
5141
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005142/**
5143 * intel_post_enable_primary - Perform operations after enabling primary plane
5144 * @crtc: the CRTC whose primary plane was just enabled
Chris Wilsonc38c1452018-02-14 13:49:22 +00005145 * @new_crtc_state: the enabling state
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005146 *
5147 * Performs potentially sleeping operations that must be done after the primary
5148 * plane is enabled, such as updating FBC and IPS. Note that this may be
5149 * called due to an explicit primary plane update, or due to an implicit
5150 * re-enable that is caused when a sprite plane is updated to no longer
5151 * completely hide the primary plane.
5152 */
5153static void
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005154intel_post_enable_primary(struct drm_crtc *crtc,
5155 const struct intel_crtc_state *new_crtc_state)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005156{
5157 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005158 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5160 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005161
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005162 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005163 * Gen2 reports pipe underruns whenever all planes are disabled.
5164 * So don't enable underrun reporting before at least some planes
5165 * are enabled.
5166 * FIXME: Need to fix the logic to work when we turn off all planes
5167 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02005168 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005169 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005170 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5171
Ville Syrjäläaca7b682015-10-30 19:22:21 +02005172 /* Underruns don't always raise interrupts, so check manually. */
5173 intel_check_cpu_fifo_underruns(dev_priv);
5174 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005175}
5176
Ville Syrjälä2622a082016-03-09 19:07:26 +02005177/* FIXME get rid of this and use pre_plane_update */
5178static void
5179intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5180{
5181 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005182 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5184 int pipe = intel_crtc->pipe;
5185
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005186 /*
5187 * Gen2 reports pipe underruns whenever all planes are disabled.
5188 * So disable underrun reporting before all the planes get disabled.
5189 */
5190 if (IS_GEN2(dev_priv))
5191 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5192
5193 hsw_disable_ips(to_intel_crtc_state(crtc->state));
Ville Syrjälä2622a082016-03-09 19:07:26 +02005194
5195 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005196 * Vblank time updates from the shadow to live plane control register
5197 * are blocked if the memory self-refresh mode is active at that
5198 * moment. So to make sure the plane gets truly disabled, disable
5199 * first the self-refresh mode. The self-refresh enable bit in turn
5200 * will be checked/applied by the HW only at the next frame start
5201 * event which is after the vblank start event, so we need to have a
5202 * wait-for-vblank between disabling the plane and the pipe.
5203 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +02005204 if (HAS_GMCH_DISPLAY(dev_priv) &&
5205 intel_set_memory_cxsr(dev_priv, false))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005206 intel_wait_for_vblank(dev_priv, pipe);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005207}
5208
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005209static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
5210 const struct intel_crtc_state *new_crtc_state)
5211{
5212 if (!old_crtc_state->ips_enabled)
5213 return false;
5214
5215 if (needs_modeset(&new_crtc_state->base))
5216 return true;
5217
5218 return !new_crtc_state->ips_enabled;
5219}
5220
5221static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
5222 const struct intel_crtc_state *new_crtc_state)
5223{
5224 if (!new_crtc_state->ips_enabled)
5225 return false;
5226
5227 if (needs_modeset(&new_crtc_state->base))
5228 return true;
5229
5230 /*
5231 * We can't read out IPS on broadwell, assume the worst and
5232 * forcibly enable IPS on the first fastset.
5233 */
5234 if (new_crtc_state->update_pipe &&
5235 old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
5236 return true;
5237
5238 return !old_crtc_state->ips_enabled;
5239}
5240
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305241static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
5242 const struct intel_crtc_state *crtc_state)
5243{
5244 if (!crtc_state->nv12_planes)
5245 return false;
5246
5247 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
5248 return false;
5249
5250 if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
5251 IS_CANNONLAKE(dev_priv))
5252 return true;
5253
5254 return false;
5255}
5256
Daniel Vetter5a21b662016-05-24 17:13:53 +02005257static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5258{
5259 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Vidya Srinivasc4a4efa2018-04-09 09:11:09 +05305260 struct drm_device *dev = crtc->base.dev;
5261 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005262 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5263 struct intel_crtc_state *pipe_config =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005264 intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
5265 crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005266 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005267 struct drm_plane_state *old_primary_state =
5268 drm_atomic_get_old_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005269
Chris Wilson5748b6a2016-08-04 16:32:38 +01005270 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005271
Daniel Vetter5a21b662016-05-24 17:13:53 +02005272 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005273 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005274
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005275 if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
5276 hsw_enable_ips(pipe_config);
5277
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005278 if (old_primary_state) {
5279 struct drm_plane_state *new_primary_state =
5280 drm_atomic_get_new_plane_state(old_state, primary);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005281
5282 intel_fbc_post_update(crtc);
5283
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005284 if (new_primary_state->visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005285 (needs_modeset(&pipe_config->base) ||
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005286 !old_primary_state->visible))
Maarten Lankhorst199ea382017-11-10 12:35:00 +01005287 intel_post_enable_primary(&crtc->base, pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005288 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305289
5290 /* Display WA 827 */
5291 if (needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b602018-05-12 03:03:13 +05305292 !needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305293 skl_wa_clkgate(dev_priv, crtc->pipe, false);
Vidya Srinivas6deef9b602018-05-12 03:03:13 +05305294 skl_wa_528(dev_priv, crtc->pipe, false);
5295 }
Daniel Vetter5a21b662016-05-24 17:13:53 +02005296}
5297
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005298static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5299 struct intel_crtc_state *pipe_config)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005300{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005301 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005302 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005303 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005304 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5305 struct drm_plane *primary = crtc->base.primary;
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005306 struct drm_plane_state *old_primary_state =
5307 drm_atomic_get_old_plane_state(old_state, primary);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005308 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005309 struct intel_atomic_state *old_intel_state =
5310 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005311
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005312 if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
5313 hsw_disable_ips(old_crtc_state);
5314
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005315 if (old_primary_state) {
5316 struct intel_plane_state *new_primary_state =
Ville Syrjäläf9a8c142017-08-23 18:22:24 +03005317 intel_atomic_get_new_plane_state(old_intel_state,
5318 to_intel_plane(primary));
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005319
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005320 intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005321 /*
5322 * Gen2 reports pipe underruns whenever all planes are disabled.
5323 * So disable underrun reporting before all the planes get disabled.
5324 */
Maarten Lankhorst8b694492018-04-09 14:46:55 +02005325 if (IS_GEN2(dev_priv) && old_primary_state->visible &&
5326 (modeset || !new_primary_state->base.visible))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01005327 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005328 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005329
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305330 /* Display WA 827 */
5331 if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
Vidya Srinivas6deef9b602018-05-12 03:03:13 +05305332 needs_nv12_wa(dev_priv, pipe_config)) {
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305333 skl_wa_clkgate(dev_priv, crtc->pipe, true);
Vidya Srinivas6deef9b602018-05-12 03:03:13 +05305334 skl_wa_528(dev_priv, crtc->pipe, true);
5335 }
Maarten Lankhorst8e021152018-05-12 03:03:12 +05305336
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02005337 /*
5338 * Vblank time updates from the shadow to live plane control register
5339 * are blocked if the memory self-refresh mode is active at that
5340 * moment. So to make sure the plane gets truly disabled, disable
5341 * first the self-refresh mode. The self-refresh enable bit in turn
5342 * will be checked/applied by the HW only at the next frame start
5343 * event which is after the vblank start event, so we need to have a
5344 * wait-for-vblank between disabling the plane and the pipe.
5345 */
5346 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5347 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5348 intel_wait_for_vblank(dev_priv, crtc->pipe);
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005349
Matt Ropered4a6a72016-02-23 17:20:13 -08005350 /*
5351 * IVB workaround: must disable low power watermarks for at least
5352 * one frame before enabling scaling. LP watermarks can be re-enabled
5353 * when scaling is disabled.
5354 *
5355 * WaCxSRDisabledForSpriteScaling:ivb
5356 */
Ville Syrjälä8e7a4422018-10-04 15:15:27 +03005357 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev) &&
5358 old_crtc_state->base.active)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005359 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005360
5361 /*
5362 * If we're doing a modeset, we're done. No need to do any pre-vblank
5363 * watermark programming here.
5364 */
5365 if (needs_modeset(&pipe_config->base))
5366 return;
5367
5368 /*
5369 * For platforms that support atomic watermarks, program the
5370 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5371 * will be the intermediate values that are safe for both pre- and
5372 * post- vblank; when vblank happens, the 'active' values will be set
5373 * to the final 'target' values and we'll do this again to get the
5374 * optimal watermarks. For gen9+ platforms, the values we program here
5375 * will be the final target values which will get automatically latched
5376 * at vblank time; no further programming will be necessary.
5377 *
5378 * If a platform hasn't been transitioned to atomic watermarks yet,
5379 * we'll continue to update watermarks the old way, if flags tell
5380 * us to.
5381 */
5382 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005383 dev_priv->display.initial_watermarks(old_intel_state,
5384 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005385 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005386 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005387}
5388
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005389static void intel_crtc_disable_planes(struct intel_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005390{
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005391 struct drm_device *dev = crtc->base.dev;
5392 struct intel_plane *plane;
5393 unsigned fb_bits = 0;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005394
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005395 intel_crtc_dpms_overlay_disable(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005396
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005397 for_each_intel_plane_on_crtc(dev, crtc, plane) {
5398 if (plane_mask & BIT(plane->id)) {
5399 plane->disable_plane(plane, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005400
Maarten Lankhorstf59e9702018-09-20 12:27:07 +02005401 fb_bits |= plane->frontbuffer_bit;
5402 }
5403 }
5404
5405 intel_frontbuffer_flip(to_i915(dev), fb_bits);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005406}
5407
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005408static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005409 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005410 struct drm_atomic_state *old_state)
5411{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005412 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005413 struct drm_connector *conn;
5414 int i;
5415
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005416 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005417 struct intel_encoder *encoder =
5418 to_intel_encoder(conn_state->best_encoder);
5419
5420 if (conn_state->crtc != crtc)
5421 continue;
5422
5423 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005424 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005425 }
5426}
5427
5428static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005429 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005430 struct drm_atomic_state *old_state)
5431{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005432 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005433 struct drm_connector *conn;
5434 int i;
5435
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005436 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005437 struct intel_encoder *encoder =
5438 to_intel_encoder(conn_state->best_encoder);
5439
5440 if (conn_state->crtc != crtc)
5441 continue;
5442
5443 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005444 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005445 }
5446}
5447
5448static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005449 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005450 struct drm_atomic_state *old_state)
5451{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005452 struct drm_connector_state *conn_state;
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005453 struct drm_connector *conn;
5454 int i;
5455
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005456 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005457 struct intel_encoder *encoder =
5458 to_intel_encoder(conn_state->best_encoder);
5459
5460 if (conn_state->crtc != crtc)
5461 continue;
5462
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005463 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005464 intel_opregion_notify_encoder(encoder, true);
5465 }
5466}
5467
5468static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005469 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005470 struct drm_atomic_state *old_state)
5471{
5472 struct drm_connector_state *old_conn_state;
5473 struct drm_connector *conn;
5474 int i;
5475
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005476 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005477 struct intel_encoder *encoder =
5478 to_intel_encoder(old_conn_state->best_encoder);
5479
5480 if (old_conn_state->crtc != crtc)
5481 continue;
5482
5483 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005484 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005485 }
5486}
5487
5488static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005489 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005490 struct drm_atomic_state *old_state)
5491{
5492 struct drm_connector_state *old_conn_state;
5493 struct drm_connector *conn;
5494 int i;
5495
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005496 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005497 struct intel_encoder *encoder =
5498 to_intel_encoder(old_conn_state->best_encoder);
5499
5500 if (old_conn_state->crtc != crtc)
5501 continue;
5502
5503 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005504 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005505 }
5506}
5507
5508static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005509 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005510 struct drm_atomic_state *old_state)
5511{
5512 struct drm_connector_state *old_conn_state;
5513 struct drm_connector *conn;
5514 int i;
5515
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +01005516 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005517 struct intel_encoder *encoder =
5518 to_intel_encoder(old_conn_state->best_encoder);
5519
5520 if (old_conn_state->crtc != crtc)
5521 continue;
5522
5523 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005524 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005525 }
5526}
5527
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005528static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5529 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005530{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005531 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005532 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005533 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5535 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005536 struct intel_atomic_state *old_intel_state =
5537 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005538
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005539 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005540 return;
5541
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005542 /*
5543 * Sometimes spurious CPU pipe underruns happen during FDI
5544 * training, at least with VGA+HDMI cloning. Suppress them.
5545 *
5546 * On ILK we get an occasional spurious CPU pipe underruns
5547 * between eDP port A enable and vdd enable. Also PCH port
5548 * enable seems to result in the occasional CPU pipe underrun.
5549 *
5550 * Spurious PCH underruns also occur during PCH enabling.
5551 */
Ville Syrjälä2b5b6312018-05-24 22:04:06 +03005552 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5553 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005554
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02005555 if (pipe_config->has_pch_encoder)
5556 intel_prepare_shared_dpll(pipe_config);
Daniel Vetterb14b1052014-04-24 23:55:13 +02005557
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005558 if (intel_crtc_has_dp_encoder(pipe_config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305559 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005560
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005561 intel_set_pipe_timings(pipe_config);
5562 intel_set_pipe_src_size(pipe_config);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005563
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005564 if (pipe_config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005565 intel_cpu_transcoder_set_m_n(intel_crtc,
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005566 &pipe_config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005567 }
5568
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005569 ironlake_set_pipeconf(pipe_config);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005570
Jesse Barnesf67a5592011-01-05 10:31:48 -08005571 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005572
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005573 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005574
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005575 if (pipe_config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005576 /* Note: FDI PLL enabling _must_ be done before we enable the
5577 * cpu pipes, hence this is separate from all the other fdi/pch
5578 * enabling. */
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02005579 ironlake_fdi_pll_enable(pipe_config);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005580 } else {
5581 assert_fdi_tx_disabled(dev_priv, pipe);
5582 assert_fdi_rx_disabled(dev_priv, pipe);
5583 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005584
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005585 ironlake_pfit_enable(pipe_config);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005586
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005587 /*
5588 * On ILK+ LUT must be loaded before the pipe is running but with
5589 * clocks enabled
5590 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005591 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005592
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005593 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005594 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02005595 intel_enable_pipe(pipe_config);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005596
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005597 if (pipe_config->has_pch_encoder)
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03005598 ironlake_pch_enable(old_intel_state, pipe_config);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005599
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005600 assert_vblank_disabled(crtc);
5601 drm_crtc_vblank_on(crtc);
5602
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005603 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005604
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005605 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005606 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005607
Ville Syrjäläea80a662018-05-24 22:04:05 +03005608 /*
5609 * Must wait for vblank to avoid spurious PCH FIFO underruns.
5610 * And a second vblank wait is needed at least on ILK with
5611 * some interlaced HDMI modes. Let's do the double wait always
5612 * in case there are more corner cases we don't know about.
5613 */
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005614 if (pipe_config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005615 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläea80a662018-05-24 22:04:05 +03005616 intel_wait_for_vblank(dev_priv, pipe);
5617 }
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005618 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005619 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005620}
5621
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005622/* IPS only exists on ULT machines and is tied to pipe A. */
5623static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5624{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005625 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005626}
5627
Imre Deaked69cd42017-10-02 10:55:57 +03005628static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
5629 enum pipe pipe, bool apply)
5630{
5631 u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
5632 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
5633
5634 if (apply)
5635 val |= mask;
5636 else
5637 val &= ~mask;
5638
5639 I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
5640}
5641
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005642static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
5643{
5644 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5645 enum pipe pipe = crtc->pipe;
5646 uint32_t val;
5647
Rodrigo Vivi443d5e32018-10-04 08:18:14 -07005648 val = MBUS_DBOX_A_CREDIT(2);
5649 val |= MBUS_DBOX_BW_CREDIT(1);
5650 val |= MBUS_DBOX_B_CREDIT(8);
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005651
5652 I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
5653}
5654
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005655static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5656 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005657{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005658 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005659 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005661 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005662 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005663 struct intel_atomic_state *old_intel_state =
5664 to_intel_atomic_state(old_state);
Imre Deaked69cd42017-10-02 10:55:57 +03005665 bool psl_clkgate_wa;
Vandita Kulkarnie16a3752018-06-21 20:43:56 +05305666 u32 pipe_chicken;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005667
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005668 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005669 return;
5670
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005671 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005672
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02005673 if (pipe_config->shared_dpll)
5674 intel_enable_shared_dpll(pipe_config);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005675
Paulo Zanonic27e9172018-04-27 16:14:36 -07005676 if (INTEL_GEN(dev_priv) >= 11)
5677 icl_map_plls_to_ports(crtc, pipe_config, old_state);
5678
Paulo Zanonic8af5272018-05-02 14:58:51 -07005679 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5680
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005681 if (intel_crtc_has_dp_encoder(pipe_config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305682 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005683
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005684 if (!transcoder_is_dsi(cpu_transcoder))
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005685 intel_set_pipe_timings(pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005686
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02005687 intel_set_pipe_src_size(pipe_config);
Daniel Vetter229fca92014-04-24 23:55:09 +02005688
Jani Nikula4d1de972016-03-18 17:05:42 +02005689 if (cpu_transcoder != TRANSCODER_EDP &&
5690 !transcoder_is_dsi(cpu_transcoder)) {
5691 I915_WRITE(PIPE_MULT(cpu_transcoder),
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005692 pipe_config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005693 }
5694
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005695 if (pipe_config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005696 intel_cpu_transcoder_set_m_n(intel_crtc,
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005697 &pipe_config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005698 }
5699
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005700 if (!transcoder_is_dsi(cpu_transcoder))
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005701 haswell_set_pipeconf(pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005702
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02005703 haswell_set_pipemisc(pipe_config);
Daniel Vetter229fca92014-04-24 23:55:09 +02005704
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005705 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005706
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005707 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005708
Imre Deaked69cd42017-10-02 10:55:57 +03005709 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
5710 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005711 pipe_config->pch_pfit.enabled;
Imre Deaked69cd42017-10-02 10:55:57 +03005712 if (psl_clkgate_wa)
5713 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
5714
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005715 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005716 skylake_pfit_enable(pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005717 else
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005718 ironlake_pfit_enable(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005719
5720 /*
5721 * On ILK+ LUT must be loaded before the pipe is running but with
5722 * clocks enabled
5723 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005724 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005725
Vandita Kulkarnie16a3752018-06-21 20:43:56 +05305726 /*
5727 * Display WA #1153: enable hardware to bypass the alpha math
5728 * and rounding for per-pixel values 00 and 0xff
5729 */
5730 if (INTEL_GEN(dev_priv) >= 11) {
5731 pipe_chicken = I915_READ(PIPE_CHICKEN(pipe));
5732 if (!(pipe_chicken & PER_PIXEL_ALPHA_BYPASS_EN))
5733 I915_WRITE_FW(PIPE_CHICKEN(pipe),
5734 pipe_chicken | PER_PIXEL_ALPHA_BYPASS_EN);
5735 }
5736
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005737 intel_ddi_set_pipe_settings(pipe_config);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005738 if (!transcoder_is_dsi(cpu_transcoder))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005739 intel_ddi_enable_transcoder_func(pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005740
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005741 if (dev_priv->display.initial_watermarks != NULL)
Ville Syrjälä3125d392016-11-28 19:37:03 +02005742 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005743
Mahesh Kumarc3cc39c2018-02-05 15:21:31 -02005744 if (INTEL_GEN(dev_priv) >= 11)
5745 icl_pipe_mbus_enable(intel_crtc);
5746
Jani Nikula4d1de972016-03-18 17:05:42 +02005747 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005748 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005749 intel_enable_pipe(pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005750
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005751 if (pipe_config->has_pch_encoder)
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03005752 lpt_pch_enable(old_intel_state, pipe_config);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005753
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005754 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DP_MST))
Ander Conselvan de Oliveira3dc38ee2017-03-02 14:58:56 +02005755 intel_ddi_set_vc_payload_alloc(pipe_config, true);
Dave Airlie0e32b392014-05-02 14:02:48 +10005756
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005757 assert_vblank_disabled(crtc);
5758 drm_crtc_vblank_on(crtc);
5759
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005760 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005761
Imre Deaked69cd42017-10-02 10:55:57 +03005762 if (psl_clkgate_wa) {
5763 intel_wait_for_vblank(dev_priv, pipe);
5764 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
5765 }
5766
Paulo Zanonie4916942013-09-20 16:21:19 -03005767 /* If we change the relative order between pipe/planes enabling, we need
5768 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005769 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005770 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005771 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5772 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005773 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005774}
5775
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005776static void ironlake_pfit_disable(const struct intel_crtc_state *old_crtc_state)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005777{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005778 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5779 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5780 enum pipe pipe = crtc->pipe;
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005781
5782 /* To avoid upsetting the power well on haswell only disable the pfit if
5783 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005784 if (old_crtc_state->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005785 I915_WRITE(PF_CTL(pipe), 0);
5786 I915_WRITE(PF_WIN_POS(pipe), 0);
5787 I915_WRITE(PF_WIN_SZ(pipe), 0);
5788 }
5789}
5790
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005791static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5792 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005793{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005794 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005795 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005796 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5798 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005799
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005800 /*
5801 * Sometimes spurious CPU pipe underruns happen when the
5802 * pipe is already disabled, but FDI RX/TX is still enabled.
5803 * Happens at least with VGA+HDMI cloning. Suppress them.
5804 */
Ville Syrjälä2b5b6312018-05-24 22:04:06 +03005805 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5806 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005807
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005808 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005809
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005810 drm_crtc_vblank_off(crtc);
5811 assert_vblank_disabled(crtc);
5812
Ville Syrjälä4972f702017-11-29 17:37:32 +02005813 intel_disable_pipe(old_crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005814
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005815 ironlake_pfit_disable(old_crtc_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005816
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005817 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005818 ironlake_fdi_disable(crtc);
5819
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005820 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005821
Maarten Lankhorst6f405632018-10-04 11:46:04 +02005822 if (old_crtc_state->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005823 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005824
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005825 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005826 i915_reg_t reg;
5827 u32 temp;
5828
Daniel Vetterd925c592013-06-05 13:34:04 +02005829 /* disable TRANS_DP_CTL */
5830 reg = TRANS_DP_CTL(pipe);
5831 temp = I915_READ(reg);
5832 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5833 TRANS_DP_PORT_SEL_MASK);
5834 temp |= TRANS_DP_PORT_SEL_NONE;
5835 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005836
Daniel Vetterd925c592013-06-05 13:34:04 +02005837 /* disable DPLL_SEL */
5838 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005839 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005840 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005841 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005842
Daniel Vetterd925c592013-06-05 13:34:04 +02005843 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005844 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005845
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005846 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005847 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005848}
5849
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005850static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5851 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005852{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005853 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005854 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Imre Deak24a28172018-06-13 20:07:06 +03005856 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005857
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005858 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005859
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005860 drm_crtc_vblank_off(crtc);
5861 assert_vblank_disabled(crtc);
5862
Jani Nikula4d1de972016-03-18 17:05:42 +02005863 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005864 if (!transcoder_is_dsi(cpu_transcoder))
Ville Syrjälä4972f702017-11-29 17:37:32 +02005865 intel_disable_pipe(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005866
Imre Deak24a28172018-06-13 20:07:06 +03005867 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
5868 intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005869
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005870 if (!transcoder_is_dsi(cpu_transcoder))
Clint Taylor90c3e212018-07-10 13:02:05 -07005871 intel_ddi_disable_transcoder_func(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005872
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005873 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005874 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005875 else
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005876 ironlake_pfit_disable(old_crtc_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005877
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005878 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Paulo Zanonic27e9172018-04-27 16:14:36 -07005879
5880 if (INTEL_GEN(dev_priv) >= 11)
5881 icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005882}
5883
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005884static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005885{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005886 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5887 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005888
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005889 if (!crtc_state->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005890 return;
5891
Daniel Vetterc0b03412013-05-28 12:05:54 +02005892 /*
5893 * The panel fitter should only be adjusted whilst the pipe is disabled,
5894 * according to register description and PRM.
5895 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005896 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5897 assert_pipe_disabled(dev_priv, crtc->pipe);
5898
Maarten Lankhorstb2562712018-10-04 11:45:53 +02005899 I915_WRITE(PFIT_PGM_RATIOS, crtc_state->gmch_pfit.pgm_ratios);
5900 I915_WRITE(PFIT_CONTROL, crtc_state->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005901
5902 /* Border color in case we don't scale up to the full screen. Black by
5903 * default, change to something else for debugging. */
5904 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005905}
5906
Paulo Zanoniac213c12018-05-21 17:25:37 -07005907bool intel_port_is_tc(struct drm_i915_private *dev_priv, enum port port)
5908{
5909 if (IS_ICELAKE(dev_priv))
5910 return port >= PORT_C && port <= PORT_F;
5911
5912 return false;
5913}
5914
5915enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
5916{
5917 if (!intel_port_is_tc(dev_priv, port))
5918 return PORT_TC_NONE;
5919
5920 return port - PORT_C;
5921}
5922
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005923enum intel_display_power_domain intel_port_to_power_domain(enum port port)
Dave Airlied05410f2014-06-05 13:22:59 +10005924{
5925 switch (port) {
5926 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005927 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005928 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005929 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005930 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005931 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005932 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005933 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005934 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005935 return POWER_DOMAIN_PORT_DDI_E_LANES;
Rodrigo Vivi9787e832018-01-29 15:22:22 -08005936 case PORT_F:
5937 return POWER_DOMAIN_PORT_DDI_F_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005938 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005939 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005940 return POWER_DOMAIN_PORT_OTHER;
5941 }
5942}
5943
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005944static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5945 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005946{
5947 struct drm_device *dev = crtc->dev;
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005948 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005949 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5951 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005952 u64 mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005953 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005954
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005955 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005956 return 0;
5957
Imre Deak17bd6e62018-01-09 14:20:40 +02005958 mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
5959 mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005960 if (crtc_state->pch_pfit.enabled ||
5961 crtc_state->pch_pfit.force_thru)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005962 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
Imre Deak77d22dc2014-03-05 16:20:52 +02005963
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005964 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5965 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5966
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02005967 mask |= BIT_ULL(intel_encoder->power_domain);
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005968 }
Imre Deak319be8a2014-03-04 19:22:57 +02005969
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005970 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
Imre Deak17bd6e62018-01-09 14:20:40 +02005971 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
Maarten Lankhorst37255d82016-12-15 15:29:43 +01005972
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005973 if (crtc_state->shared_dpll)
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005974 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005975
Imre Deak77d22dc2014-03-05 16:20:52 +02005976 return mask;
5977}
5978
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02005979static u64
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005980modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5981 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005982{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005983 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005984 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5985 enum intel_display_power_domain domain;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02005986 u64 domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005987
5988 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005989 intel_crtc->enabled_power_domains = new_domains =
5990 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005991
Daniel Vetter5a21b662016-05-24 17:13:53 +02005992 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005993
5994 for_each_power_domain(domain, domains)
5995 intel_display_power_get(dev_priv, domain);
5996
Daniel Vetter5a21b662016-05-24 17:13:53 +02005997 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005998}
5999
6000static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02006001 u64 domains)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02006002{
6003 enum intel_display_power_domain domain;
6004
6005 for_each_power_domain(domain, domains)
6006 intel_display_power_put(dev_priv, domain);
6007}
6008
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006009static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6010 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006011{
Ville Syrjäläff32c542017-03-02 19:14:57 +02006012 struct intel_atomic_state *old_intel_state =
6013 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006014 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006015 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006016 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006018 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006019
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006020 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006021 return;
6022
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006023 if (intel_crtc_has_dp_encoder(pipe_config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306024 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006025
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02006026 intel_set_pipe_timings(pipe_config);
6027 intel_set_pipe_src_size(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006028
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006029 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006030 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6031 I915_WRITE(CHV_CANVAS(pipe), 0);
6032 }
6033
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02006034 i9xx_set_pipeconf(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006035
P Raviraj Sitaramc59d2da2018-09-10 19:57:14 +05306036 intel_color_set_csc(&pipe_config->base);
6037
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006039
Daniel Vettera72e4c92014-09-30 10:56:47 +02006040 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006041
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006042 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006043
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006044 if (IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006045 chv_prepare_pll(intel_crtc, pipe_config);
6046 chv_enable_pll(intel_crtc, pipe_config);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006047 } else {
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006048 vlv_prepare_pll(intel_crtc, pipe_config);
6049 vlv_enable_pll(intel_crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006050 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006051
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006052 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006053
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006054 i9xx_pfit_enable(pipe_config);
Jesse Barnes2dd24552013-04-25 12:55:01 -07006055
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006056 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006057
Ville Syrjäläff32c542017-03-02 19:14:57 +02006058 dev_priv->display.initial_watermarks(old_intel_state,
6059 pipe_config);
Ville Syrjälä4972f702017-11-29 17:37:32 +02006060 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006061
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006062 assert_vblank_disabled(crtc);
6063 drm_crtc_vblank_on(crtc);
6064
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006065 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066}
6067
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006068static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006069{
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006070 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6071 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006072
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006073 I915_WRITE(FP0(crtc->pipe), crtc_state->dpll_hw_state.fp0);
6074 I915_WRITE(FP1(crtc->pipe), crtc_state->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006075}
6076
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006077static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6078 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006079{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006080 struct intel_atomic_state *old_intel_state =
6081 to_intel_atomic_state(old_state);
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006082 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006083 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006084 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006086 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006087
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006088 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006089 return;
6090
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006091 i9xx_set_pll_dividers(pipe_config);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006092
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006093 if (intel_crtc_has_dp_encoder(pipe_config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306094 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006095
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02006096 intel_set_pipe_timings(pipe_config);
6097 intel_set_pipe_src_size(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006098
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02006099 i9xx_set_pipeconf(pipe_config);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006100
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006101 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006102
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006103 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006104 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006105
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006106 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006107
Ville Syrjälä939994d2017-09-13 17:08:56 +03006108 i9xx_enable_pll(intel_crtc, pipe_config);
Daniel Vetterf6736a12013-06-05 13:34:30 +02006109
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006110 i9xx_pfit_enable(pipe_config);
Jesse Barnes2dd24552013-04-25 12:55:01 -07006111
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006112 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006113
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006114 if (dev_priv->display.initial_watermarks != NULL)
6115 dev_priv->display.initial_watermarks(old_intel_state,
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006116 pipe_config);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03006117 else
6118 intel_update_watermarks(intel_crtc);
Ville Syrjälä4972f702017-11-29 17:37:32 +02006119 intel_enable_pipe(pipe_config);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006120
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006121 assert_vblank_disabled(crtc);
6122 drm_crtc_vblank_on(crtc);
6123
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006124 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006125}
6126
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006127static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
Daniel Vetter87476d62013-04-11 16:29:06 +02006128{
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006129 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
6130 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006131
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006132 if (!old_crtc_state->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006133 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006134
6135 assert_pipe_disabled(dev_priv, crtc->pipe);
6136
Chris Wilson43031782018-09-13 14:16:26 +01006137 DRM_DEBUG_KMS("disabling pfit, current: 0x%08x\n",
6138 I915_READ(PFIT_CONTROL));
Daniel Vetter328d8e82013-05-08 10:36:31 +02006139 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006140}
6141
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006142static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6143 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006144{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006145 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006146 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006147 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6149 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006150
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006151 /*
6152 * On gen2 planes are double buffered but the pipe isn't, so we must
6153 * wait for planes to fully turn off before disabling the pipe.
6154 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006155 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02006156 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006157
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006158 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006159
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006160 drm_crtc_vblank_off(crtc);
6161 assert_vblank_disabled(crtc);
6162
Ville Syrjälä4972f702017-11-29 17:37:32 +02006163 intel_disable_pipe(old_crtc_state);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006164
Maarten Lankhorstb2562712018-10-04 11:45:53 +02006165 i9xx_pfit_disable(old_crtc_state);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006166
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006167 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006168
Maarten Lankhorst6f405632018-10-04 11:46:04 +02006169 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006170 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006171 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006172 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006173 vlv_disable_pll(dev_priv, pipe);
6174 else
Maarten Lankhorstb2354c72018-10-04 11:45:57 +02006175 i9xx_disable_pll(old_crtc_state);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006176 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006177
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006178 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006179
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006180 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006181 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006182
6183 if (!dev_priv->display.initial_watermarks)
6184 intel_update_watermarks(intel_crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +03006185
6186 /* clock the pipe down to 640x480@60 to potentially save power */
6187 if (IS_I830(dev_priv))
6188 i830_enable_pipe(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006189}
6190
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006191static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
6192 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006193{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006194 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006196 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006197 enum intel_display_power_domain domain;
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006198 struct intel_plane *plane;
Ander Conselvan de Oliveirad2d15012017-02-13 16:57:33 +02006199 u64 domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006200 struct drm_atomic_state *state;
6201 struct intel_crtc_state *crtc_state;
6202 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006203
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006204 if (!intel_crtc->active)
6205 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006206
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006207 for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
6208 const struct intel_plane_state *plane_state =
6209 to_intel_plane_state(plane->base.state);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006210
Ville Syrjäläb1e01592017-11-17 21:19:09 +02006211 if (plane_state->base.visible)
6212 intel_plane_disable_noatomic(intel_crtc, plane);
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006213 }
6214
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006215 state = drm_atomic_state_alloc(crtc->dev);
Ander Conselvan de Oliveira31bb2ef2017-01-20 16:28:45 +02006216 if (!state) {
6217 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
6218 crtc->base.id, crtc->name);
6219 return;
6220 }
6221
Ville Syrjäläda1d0e22017-06-01 17:36:14 +03006222 state->acquire_ctx = ctx;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006223
6224 /* Everything's already locked, -EDEADLK can't happen. */
6225 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6226 ret = drm_atomic_add_affected_connectors(state, crtc);
6227
6228 WARN_ON(IS_ERR(crtc_state) || ret);
6229
6230 dev_priv->display.crtc_disable(crtc_state, state);
6231
Chris Wilson08536952016-10-14 13:18:18 +01006232 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006233
Ville Syrjälä78108b72016-05-27 20:59:19 +03006234 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6235 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006236
6237 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6238 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006239 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006240 crtc->enabled = false;
6241 crtc->state->connector_mask = 0;
6242 crtc->state->encoder_mask = 0;
6243
6244 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6245 encoder->base.crtc = NULL;
6246
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006247 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02006248 intel_update_watermarks(intel_crtc);
Maarten Lankhorst65c307f2018-10-05 11:52:44 +02006249 intel_disable_shared_dpll(to_intel_crtc_state(crtc->state));
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006250
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006251 domains = intel_crtc->enabled_power_domains;
6252 for_each_power_domain(domain, domains)
6253 intel_display_power_put(dev_priv, domain);
6254 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006255
6256 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
Ville Syrjäläd305e062017-08-30 21:57:03 +03006257 dev_priv->min_cdclk[intel_crtc->pipe] = 0;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +03006258 dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006259}
6260
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006261/*
6262 * turn all crtc's off, but do not adjust state
6263 * This has to be paired with a call to intel_modeset_setup_hw_state.
6264 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006265int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006266{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006267 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006268 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006269 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006270
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006271 state = drm_atomic_helper_suspend(dev);
6272 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006273 if (ret)
6274 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006275 else
6276 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006277 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006278}
6279
Chris Wilsonea5b2132010-08-04 13:50:23 +01006280void intel_encoder_destroy(struct drm_encoder *encoder)
6281{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006282 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006283
Chris Wilsonea5b2132010-08-04 13:50:23 +01006284 drm_encoder_cleanup(encoder);
6285 kfree(intel_encoder);
6286}
6287
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006288/* Cross check the actual hw state with our own modeset state tracking (and it's
6289 * internal consistency). */
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006290static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
6291 struct drm_connector_state *conn_state)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006292{
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006293 struct intel_connector *connector = to_intel_connector(conn_state->connector);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006294
6295 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6296 connector->base.base.id,
6297 connector->base.name);
6298
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006299 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006300 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006301
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006302 I915_STATE_WARN(!crtc_state,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006303 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006304
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006305 if (!crtc_state)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006306 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006307
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006308 I915_STATE_WARN(!crtc_state->active,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006309 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006310
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006311 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006312 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006313
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006314 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006315 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006316
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006317 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006318 "attached encoder crtc differs from connector crtc\n");
6319 } else {
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006320 I915_STATE_WARN(crtc_state && crtc_state->active,
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006321 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst749d98b2017-05-11 10:28:43 +02006322 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006323 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006324 }
6325}
6326
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006327int intel_connector_init(struct intel_connector *connector)
6328{
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02006329 struct intel_digital_connector_state *conn_state;
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006330
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02006331 /*
6332 * Allocate enough memory to hold intel_digital_connector_state,
6333 * This might be a few bytes too many, but for connectors that don't
6334 * need it we'll free the state and allocate a smaller one on the first
6335 * succesful commit anyway.
6336 */
6337 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
6338 if (!conn_state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006339 return -ENOMEM;
6340
Maarten Lankhorst11c1a9e2017-05-01 15:37:57 +02006341 __drm_atomic_helper_connector_reset(&connector->base,
6342 &conn_state->base);
6343
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006344 return 0;
6345}
6346
6347struct intel_connector *intel_connector_alloc(void)
6348{
6349 struct intel_connector *connector;
6350
6351 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6352 if (!connector)
6353 return NULL;
6354
6355 if (intel_connector_init(connector) < 0) {
6356 kfree(connector);
6357 return NULL;
6358 }
6359
6360 return connector;
6361}
6362
James Ausmus091a4f92017-10-13 11:01:44 -07006363/*
6364 * Free the bits allocated by intel_connector_alloc.
6365 * This should only be used after intel_connector_alloc has returned
6366 * successfully, and before drm_connector_init returns successfully.
6367 * Otherwise the destroy callbacks for the connector and the state should
6368 * take care of proper cleanup/free
6369 */
6370void intel_connector_free(struct intel_connector *connector)
6371{
6372 kfree(to_intel_digital_connector_state(connector->base.state));
6373 kfree(connector);
6374}
6375
Daniel Vetterf0947c32012-07-02 13:10:34 +02006376/* Simple connector->get_hw_state implementation for encoders that support only
6377 * one connector and no cloning and hence the encoder state determines the state
6378 * of the connector. */
6379bool intel_connector_get_hw_state(struct intel_connector *connector)
6380{
Daniel Vetter24929352012-07-02 20:28:59 +02006381 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006382 struct intel_encoder *encoder = connector->encoder;
6383
6384 return encoder->get_hw_state(encoder, &pipe);
6385}
6386
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006388{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006389 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6390 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006391
6392 return 0;
6393}
6394
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006395static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006396 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006398 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006399 struct drm_atomic_state *state = pipe_config->base.state;
6400 struct intel_crtc *other_crtc;
6401 struct intel_crtc_state *other_crtc_state;
6402
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006403 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6404 pipe_name(pipe), pipe_config->fdi_lanes);
6405 if (pipe_config->fdi_lanes > 4) {
6406 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6407 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006409 }
6410
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006411 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006412 if (pipe_config->fdi_lanes > 2) {
6413 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6414 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006416 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006417 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418 }
6419 }
6420
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00006421 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423
6424 /* Ivybridge 3 pipe is really complicated */
6425 switch (pipe) {
6426 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006428 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006429 if (pipe_config->fdi_lanes <= 2)
6430 return 0;
6431
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006432 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433 other_crtc_state =
6434 intel_atomic_get_crtc_state(state, other_crtc);
6435 if (IS_ERR(other_crtc_state))
6436 return PTR_ERR(other_crtc_state);
6437
6438 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006439 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6440 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006441 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006442 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006443 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006444 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006445 if (pipe_config->fdi_lanes > 2) {
6446 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6447 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006448 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006449 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006450
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02006451 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 other_crtc_state =
6453 intel_atomic_get_crtc_state(state, other_crtc);
6454 if (IS_ERR(other_crtc_state))
6455 return PTR_ERR(other_crtc_state);
6456
6457 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006458 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006459 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006460 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006462 default:
6463 BUG();
6464 }
6465}
6466
Daniel Vettere29c22c2013-02-21 00:00:16 +01006467#define RETRY 1
6468static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006469 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006470{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006471 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006472 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006473 int lane, link_bw, fdi_dotclock, ret;
6474 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006475
Daniel Vettere29c22c2013-02-21 00:00:16 +01006476retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006477 /* FDI is a binary signal running at ~2.7GHz, encoding
6478 * each output octet as 10 bits. The actual frequency
6479 * is stored as a divider into a 100MHz clock, and the
6480 * mode pixel clock is stored in units of 1KHz.
6481 * Hence the bw of each lane in terms of the mode signal
6482 * is:
6483 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006484 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006485
Damien Lespiau241bfc32013-09-25 16:45:37 +01006486 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006487
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006488 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006489 pipe_config->pipe_bpp);
6490
6491 pipe_config->fdi_lanes = lane;
6492
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006493 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006494 link_bw, &pipe_config->fdi_m_n, false);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006495
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006496 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006497 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006498 pipe_config->pipe_bpp -= 2*3;
6499 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6500 pipe_config->pipe_bpp);
6501 needs_recompute = true;
6502 pipe_config->bw_constrained = true;
6503
6504 goto retry;
6505 }
6506
6507 if (needs_recompute)
6508 return RETRY;
6509
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006510 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006511}
6512
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006513bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006514{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006515 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6516 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6517
6518 /* IPS only exists on ULT machines and is tied to pipe A. */
6519 if (!hsw_crtc_supports_ips(crtc))
Ville Syrjälä6e644622017-08-17 17:55:09 +03006520 return false;
6521
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006522 if (!i915_modparams.enable_ips)
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006523 return false;
6524
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006525 if (crtc_state->pipe_bpp > 24)
6526 return false;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006527
6528 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006529 * We compare against max which means we must take
6530 * the increased cdclk requirement into account when
6531 * calculating the new cdclk.
6532 *
6533 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006534 */
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006535 if (IS_BROADWELL(dev_priv) &&
6536 crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
6537 return false;
6538
6539 return true;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006540}
6541
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006542static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006543{
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006544 struct drm_i915_private *dev_priv =
6545 to_i915(crtc_state->base.crtc->dev);
6546 struct intel_atomic_state *intel_state =
6547 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006548
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006549 if (!hsw_crtc_state_ips_capable(crtc_state))
6550 return false;
6551
6552 if (crtc_state->ips_force_disable)
6553 return false;
6554
Maarten Lankhorstadbe5c52017-11-22 19:39:06 +01006555 /* IPS should be fine as long as at least one plane is enabled. */
6556 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
Maarten Lankhorst24f28452017-11-22 19:39:01 +01006557 return false;
6558
6559 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
6560 if (IS_BROADWELL(dev_priv) &&
6561 crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
6562 return false;
6563
6564 return true;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006565}
6566
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006567static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6568{
6569 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6570
6571 /* GDG double wide on either pipe, otherwise pipe A only */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00006572 return INTEL_GEN(dev_priv) < 4 &&
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006573 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6574}
6575
Ville Syrjäläceb99322017-01-20 20:22:05 +02006576static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6577{
6578 uint32_t pixel_rate;
6579
6580 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6581
6582 /*
6583 * We only use IF-ID interlacing. If we ever use
6584 * PF-ID we'll need to adjust the pixel_rate here.
6585 */
6586
6587 if (pipe_config->pch_pfit.enabled) {
6588 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6589 uint32_t pfit_size = pipe_config->pch_pfit.size;
6590
6591 pipe_w = pipe_config->pipe_src_w;
6592 pipe_h = pipe_config->pipe_src_h;
6593
6594 pfit_w = (pfit_size >> 16) & 0xFFFF;
6595 pfit_h = pfit_size & 0xFFFF;
6596 if (pipe_w < pfit_w)
6597 pipe_w = pfit_w;
6598 if (pipe_h < pfit_h)
6599 pipe_h = pfit_h;
6600
6601 if (WARN_ON(!pfit_w || !pfit_h))
6602 return pixel_rate;
6603
6604 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6605 pfit_w * pfit_h);
6606 }
6607
6608 return pixel_rate;
6609}
6610
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006611static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
6612{
6613 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
6614
6615 if (HAS_GMCH_DISPLAY(dev_priv))
6616 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6617 crtc_state->pixel_rate =
6618 crtc_state->base.adjusted_mode.crtc_clock;
6619 else
6620 crtc_state->pixel_rate =
6621 ilk_pipe_pixel_rate(crtc_state);
6622}
6623
Daniel Vettera43f6e02013-06-07 23:10:32 +02006624static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006625 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006626{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006627 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006628 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006629 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006630 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006631
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006632 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006633 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006634
6635 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006636 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006637 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006638 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006639 if (intel_crtc_supports_double_wide(crtc) &&
6640 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006641 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006642 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006643 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006644 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006645
Ville Syrjäläf3261152016-05-24 21:34:18 +03006646 if (adjusted_mode->crtc_clock > clock_limit) {
6647 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6648 adjusted_mode->crtc_clock, clock_limit,
6649 yesno(pipe_config->double_wide));
6650 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006651 }
Chris Wilson89749352010-09-12 18:25:19 +01006652
Shashank Sharma25edf912017-07-21 20:55:07 +05306653 if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
6654 /*
6655 * There is only one pipe CSC unit per pipe, and we need that
6656 * for output conversion from RGB->YCBCR. So if CTM is already
6657 * applied we can't support YCBCR420 output.
6658 */
6659 DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
6660 return -EINVAL;
6661 }
6662
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006663 /*
6664 * Pipe horizontal size must be even in:
6665 * - DVO ganged mode
6666 * - LVDS dual channel mode
6667 * - Double wide pipe
6668 */
Ville Syrjälä0574bd82017-11-23 21:04:48 +02006669 if (pipe_config->pipe_src_w & 1) {
6670 if (pipe_config->double_wide) {
6671 DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
6672 return -EINVAL;
6673 }
6674
6675 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6676 intel_is_dual_link_lvds(dev)) {
6677 DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
6678 return -EINVAL;
6679 }
6680 }
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006681
Damien Lespiau8693a822013-05-03 18:48:11 +01006682 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6683 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006684 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01006685 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006686 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006687 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006688
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02006689 intel_crtc_compute_pixel_rate(pipe_config);
6690
Daniel Vetter877d48d2013-04-19 11:24:43 +02006691 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006692 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006693
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006694 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006695}
6696
Zhenyu Wang2c072452009-06-05 15:38:42 +08006697static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006698intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006699{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006700 while (*num > DATA_LINK_M_N_MASK ||
6701 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006702 *num >>= 1;
6703 *den >>= 1;
6704 }
6705}
6706
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006707static void compute_m_n(unsigned int m, unsigned int n,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006708 uint32_t *ret_m, uint32_t *ret_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006709 bool constant_n)
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006710{
Jani Nikula9a86cda2017-03-27 14:33:25 +03006711 /*
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006712 * Several DP dongles in particular seem to be fussy about
6713 * too large link M/N values. Give N value as 0x8000 that
6714 * should be acceptable by specific devices. 0x8000 is the
6715 * specified fixed N value for asynchronous clock mode,
6716 * which the devices expect also in synchronous clock mode.
Jani Nikula9a86cda2017-03-27 14:33:25 +03006717 */
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006718 if (constant_n)
6719 *ret_n = 0x8000;
6720 else
6721 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
Jani Nikula9a86cda2017-03-27 14:33:25 +03006722
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006723 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6724 intel_reduce_m_n_ratio(ret_m, ret_n);
6725}
6726
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006727void
6728intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6729 int pixel_clock, int link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006730 struct intel_link_m_n *m_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006731 bool constant_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006732{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006733 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006734
6735 compute_m_n(bits_per_pixel * pixel_clock,
6736 link_clock * nlanes * 8,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006737 &m_n->gmch_m, &m_n->gmch_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006738 constant_n);
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006739
6740 compute_m_n(pixel_clock, link_clock,
Jani Nikulab31e85e2017-05-18 14:10:25 +03006741 &m_n->link_m, &m_n->link_n,
Lee, Shawn C53ca2ed2018-09-11 23:22:50 -07006742 constant_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006743}
6744
Chris Wilsona7615032011-01-12 17:04:08 +00006745static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6746{
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00006747 if (i915_modparams.panel_use_ssc >= 0)
6748 return i915_modparams.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006749 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006750 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006751}
6752
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006753static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006754{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006755 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006756}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006757
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006758static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6759{
6760 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006761}
6762
Daniel Vetterf47709a2013-03-28 10:42:02 +01006763static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006764 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03006765 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08006766{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006767 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006768 u32 fp, fp2 = 0;
6769
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02006770 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006771 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006772 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006773 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006774 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006775 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006776 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006777 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006778 }
6779
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006780 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006781
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006782 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006783 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006784 crtc_state->dpll_hw_state.fp1 = fp2;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006785 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006786 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006787 }
6788}
6789
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006790static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6791 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006792{
6793 u32 reg_val;
6794
6795 /*
6796 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6797 * and set it to a reasonable value instead.
6798 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006799 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006800 reg_val &= 0xffffff00;
6801 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006802 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006803
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006804 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Imre Deaked585702017-05-10 12:21:47 +03006805 reg_val &= 0x00ffffff;
6806 reg_val |= 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006807 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006808
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006809 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006810 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006811 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006812
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006813 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006814 reg_val &= 0x00ffffff;
6815 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006816 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006817}
6818
Daniel Vetterb5518422013-05-03 11:49:48 +02006819static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6820 struct intel_link_m_n *m_n)
6821{
6822 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006823 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006824 int pipe = crtc->pipe;
6825
Daniel Vettere3b95f12013-05-03 11:49:49 +02006826 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6827 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6828 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6829 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006830}
6831
6832static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006833 struct intel_link_m_n *m_n,
6834 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006835{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006836 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02006837 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006838 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006839
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00006840 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02006841 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6842 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6843 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6844 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006845 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6846 * for gen < 8) and if DRRS is supported (to make sure the
6847 * registers are not unnecessarily accessed).
6848 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006849 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6850 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006851 I915_WRITE(PIPE_DATA_M2(transcoder),
6852 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6853 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6854 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6855 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6856 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006857 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006858 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6859 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6860 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6861 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006862 }
6863}
6864
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306865void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006866{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306867 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6868
6869 if (m_n == M1_N1) {
6870 dp_m_n = &crtc->config->dp_m_n;
6871 dp_m2_n2 = &crtc->config->dp_m2_n2;
6872 } else if (m_n == M2_N2) {
6873
6874 /*
6875 * M2_N2 registers are not supported. Hence m2_n2 divider value
6876 * needs to be programmed into M1_N1.
6877 */
6878 dp_m_n = &crtc->config->dp_m2_n2;
6879 } else {
6880 DRM_ERROR("Unsupported divider value\n");
6881 return;
6882 }
6883
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006884 if (crtc->config->has_pch_encoder)
6885 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006886 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306887 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006888}
6889
Daniel Vetter251ac862015-06-18 10:30:24 +02006890static void vlv_compute_dpll(struct intel_crtc *crtc,
6891 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006892{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006893 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006894 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006895 if (crtc->pipe != PIPE_A)
6896 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006897
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006898 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006899 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006900 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6901 DPLL_EXT_BUFFER_ENABLE_VLV;
6902
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006903 pipe_config->dpll_hw_state.dpll_md =
6904 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6905}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006906
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006907static void chv_compute_dpll(struct intel_crtc *crtc,
6908 struct intel_crtc_state *pipe_config)
6909{
6910 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006911 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006912 if (crtc->pipe != PIPE_A)
6913 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6914
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006915 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006916 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006917 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6918
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02006919 pipe_config->dpll_hw_state.dpll_md =
6920 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006921}
6922
Ville Syrjäläd288f652014-10-28 13:20:22 +02006923static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006924 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006925{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006926 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006927 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006928 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006929 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006930 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006931 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006932
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006933 /* Enable Refclk */
6934 I915_WRITE(DPLL(pipe),
6935 pipe_config->dpll_hw_state.dpll &
6936 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6937
6938 /* No need to actually set up the DPLL with DSI */
6939 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6940 return;
6941
Ville Syrjäläa5805162015-05-26 20:42:30 +03006942 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01006943
Ville Syrjäläd288f652014-10-28 13:20:22 +02006944 bestn = pipe_config->dpll.n;
6945 bestm1 = pipe_config->dpll.m1;
6946 bestm2 = pipe_config->dpll.m2;
6947 bestp1 = pipe_config->dpll.p1;
6948 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006949
Jesse Barnes89b667f2013-04-18 14:51:36 -07006950 /* See eDP HDMI DPIO driver vbios notes doc */
6951
6952 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006953 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006954 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006955
6956 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006957 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006958
6959 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006960 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006961 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006963
6964 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006965 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006966
6967 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006968 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6969 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6970 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006971 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006972
6973 /*
6974 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6975 * but we don't support that).
6976 * Note: don't use the DAC post divider as it seems unstable.
6977 */
6978 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006979 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006980
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006981 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006982 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006983
Jesse Barnes89b667f2013-04-18 14:51:36 -07006984 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006985 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006986 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6987 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006988 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03006989 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006990 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006991 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006992 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006993
Ville Syrjälä37a56502016-06-22 21:57:04 +03006994 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006995 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006996 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006997 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006998 0x0df40000);
6999 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007000 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007001 0x0df70000);
7002 } else { /* HDMI or VGA */
7003 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007004 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007005 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007006 0x0df70000);
7007 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007008 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007009 0x0df40000);
7010 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007011
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007012 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007013 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007014 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007015 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007016 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007017
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007018 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007019 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007020}
7021
Ville Syrjäläd288f652014-10-28 13:20:22 +02007022static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007023 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007024{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007025 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007026 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007027 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007028 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307029 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007030 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307031 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307032 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007033
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007034 /* Enable Refclk and SSC */
7035 I915_WRITE(DPLL(pipe),
7036 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7037
7038 /* No need to actually set up the DPLL with DSI */
7039 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7040 return;
7041
Ville Syrjäläd288f652014-10-28 13:20:22 +02007042 bestn = pipe_config->dpll.n;
7043 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7044 bestm1 = pipe_config->dpll.m1;
7045 bestm2 = pipe_config->dpll.m2 >> 22;
7046 bestp1 = pipe_config->dpll.p1;
7047 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307048 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307049 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307050 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007051
Ville Syrjäläa5805162015-05-26 20:42:30 +03007052 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007053
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007054 /* p1 and p2 divider */
7055 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7056 5 << DPIO_CHV_S1_DIV_SHIFT |
7057 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7058 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7059 1 << DPIO_CHV_K_DIV_SHIFT);
7060
7061 /* Feedback post-divider - m2 */
7062 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7063
7064 /* Feedback refclk divider - n and m1 */
7065 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7066 DPIO_CHV_M1_DIV_BY_2 |
7067 1 << DPIO_CHV_N_DIV_SHIFT);
7068
7069 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007070 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007071
7072 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307073 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7074 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7075 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7076 if (bestm2_frac)
7077 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7078 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007079
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307080 /* Program digital lock detect threshold */
7081 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7082 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7083 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7084 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7085 if (!bestm2_frac)
7086 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7087 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7088
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007089 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307090 if (vco == 5400000) {
7091 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7092 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7093 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7094 tribuf_calcntr = 0x9;
7095 } else if (vco <= 6200000) {
7096 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7097 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7098 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7099 tribuf_calcntr = 0x9;
7100 } else if (vco <= 6480000) {
7101 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7102 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7103 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7104 tribuf_calcntr = 0x8;
7105 } else {
7106 /* Not supported. Apply the same limits as in the max case */
7107 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7108 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7109 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7110 tribuf_calcntr = 0;
7111 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007112 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7113
Ville Syrjälä968040b2015-03-11 22:52:08 +02007114 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307115 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7116 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7117 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7118
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007119 /* AFC Recal */
7120 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7121 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7122 DPIO_AFC_RECAL);
7123
Ville Syrjäläa5805162015-05-26 20:42:30 +03007124 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007125}
7126
Ville Syrjäläd288f652014-10-28 13:20:22 +02007127/**
7128 * vlv_force_pll_on - forcibly enable just the PLL
7129 * @dev_priv: i915 private structure
7130 * @pipe: pipe PLL to enable
7131 * @dpll: PLL configuration
7132 *
7133 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7134 * in cases where we need the PLL enabled even when @pipe is not going to
7135 * be enabled.
7136 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007137int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007138 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007139{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007140 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007141 struct intel_crtc_state *pipe_config;
7142
7143 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7144 if (!pipe_config)
7145 return -ENOMEM;
7146
7147 pipe_config->base.crtc = &crtc->base;
7148 pipe_config->pixel_multiplier = 1;
7149 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007150
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007151 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007152 chv_compute_dpll(crtc, pipe_config);
7153 chv_prepare_pll(crtc, pipe_config);
7154 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007155 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007156 vlv_compute_dpll(crtc, pipe_config);
7157 vlv_prepare_pll(crtc, pipe_config);
7158 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007159 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007160
7161 kfree(pipe_config);
7162
7163 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007164}
7165
7166/**
7167 * vlv_force_pll_off - forcibly disable just the PLL
7168 * @dev_priv: i915 private structure
7169 * @pipe: pipe PLL to disable
7170 *
7171 * Disable the PLL for @pipe. To be used in cases where we need
7172 * the PLL enabled even when @pipe is not going to be enabled.
7173 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007174void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007175{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007176 if (IS_CHERRYVIEW(dev_priv))
7177 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007178 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02007179 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007180}
7181
Daniel Vetter251ac862015-06-18 10:30:24 +02007182static void i9xx_compute_dpll(struct intel_crtc *crtc,
7183 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007184 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007185{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007186 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007187 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007188 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007189
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007190 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307191
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007192 dpll = DPLL_VGA_MODE_DIS;
7193
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007194 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007195 dpll |= DPLLB_MODE_LVDS;
7196 else
7197 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007198
Jani Nikula73f67aa2016-12-07 22:48:09 +02007199 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
7200 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007201 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007202 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007203 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007204
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007205 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7206 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007207 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007208
Ville Syrjälä37a56502016-06-22 21:57:04 +03007209 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007210 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007211
7212 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007213 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007214 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7215 else {
7216 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007217 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007218 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7219 }
7220 switch (clock->p2) {
7221 case 5:
7222 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7223 break;
7224 case 7:
7225 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7226 break;
7227 case 10:
7228 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7229 break;
7230 case 14:
7231 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7232 break;
7233 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007234 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007235 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7236
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007237 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007238 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007239 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007240 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007241 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7242 else
7243 dpll |= PLL_REF_INPUT_DREFCLK;
7244
7245 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007246 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007247
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007248 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007249 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007250 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007251 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007252 }
7253}
7254
Daniel Vetter251ac862015-06-18 10:30:24 +02007255static void i8xx_compute_dpll(struct intel_crtc *crtc,
7256 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007257 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007258{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007259 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007260 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007261 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007262 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007263
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007264 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307265
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007266 dpll = DPLL_VGA_MODE_DIS;
7267
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007268 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007269 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7270 } else {
7271 if (clock->p1 == 2)
7272 dpll |= PLL_P1_DIVIDE_BY_TWO;
7273 else
7274 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7275 if (clock->p2 == 4)
7276 dpll |= PLL_P2_DIVIDE_BY_4;
7277 }
7278
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007279 if (!IS_I830(dev_priv) &&
7280 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007281 dpll |= DPLL_DVO_2X_MODE;
7282
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007283 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007284 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007285 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7286 else
7287 dpll |= PLL_REF_INPUT_DREFCLK;
7288
7289 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007290 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007291}
7292
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007293static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007294{
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007295 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7296 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7297 enum pipe pipe = crtc->pipe;
7298 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
7299 const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007300 uint32_t crtc_vtotal, crtc_vblank_end;
7301 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007302
7303 /* We need to be careful not to changed the adjusted mode, for otherwise
7304 * the hw state checker will get angry at the mismatch. */
7305 crtc_vtotal = adjusted_mode->crtc_vtotal;
7306 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007307
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007308 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007309 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007310 crtc_vtotal -= 1;
7311 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007312
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007313 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007314 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7315 else
7316 vsyncshift = adjusted_mode->crtc_hsync_start -
7317 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007318 if (vsyncshift < 0)
7319 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007320 }
7321
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007322 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007323 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007324
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007325 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007326 (adjusted_mode->crtc_hdisplay - 1) |
7327 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007328 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007329 (adjusted_mode->crtc_hblank_start - 1) |
7330 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007331 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007332 (adjusted_mode->crtc_hsync_start - 1) |
7333 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7334
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007335 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007336 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007337 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007338 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007339 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007340 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007341 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007342 (adjusted_mode->crtc_vsync_start - 1) |
7343 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7344
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007345 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7346 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7347 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7348 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01007349 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007350 (pipe == PIPE_B || pipe == PIPE_C))
7351 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7352
Jani Nikulabc58be62016-03-18 17:05:39 +02007353}
7354
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007355static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
Jani Nikulabc58be62016-03-18 17:05:39 +02007356{
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007357 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7358 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7359 enum pipe pipe = crtc->pipe;
Jani Nikulabc58be62016-03-18 17:05:39 +02007360
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007361 /* pipesrc controls the size that is scaled from, which should
7362 * always be the user's requested size.
7363 */
7364 I915_WRITE(PIPESRC(pipe),
Maarten Lankhorst44fe7f32018-10-04 11:45:54 +02007365 ((crtc_state->pipe_src_w - 1) << 16) |
7366 (crtc_state->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007367}
7368
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007369static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007370 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007371{
7372 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007373 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007374 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7375 uint32_t tmp;
7376
7377 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007378 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7379 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007380 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007381 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7382 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007383 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007384 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7385 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007386
7387 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007388 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7389 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007390 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007391 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7392 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007393 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007394 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7395 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007396
7397 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007398 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7399 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7400 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007401 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007402}
7403
7404static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7405 struct intel_crtc_state *pipe_config)
7406{
7407 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007408 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007409 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007410
7411 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007412 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7413 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7414
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007415 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7416 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007417}
7418
Daniel Vetterf6a83282014-02-11 15:28:57 -08007419void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007420 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007421{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007422 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7423 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7424 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7425 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007426
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007427 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7428 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7429 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7430 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007431
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007432 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007433 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007434
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007435 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007436
7437 mode->hsync = drm_mode_hsync(mode);
7438 mode->vrefresh = drm_mode_vrefresh(mode);
7439 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007440}
7441
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007442static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
Daniel Vetter84b046f2013-02-19 18:48:54 +01007443{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007444 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7445 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007446 uint32_t pipeconf;
7447
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007448 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007449
Ville Syrjäläe56134b2017-06-01 17:36:19 +03007450 /* we keep both pipes enabled on 830 */
7451 if (IS_I830(dev_priv))
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007452 pipeconf |= I915_READ(PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007453
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007454 if (crtc_state->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007455 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007456
Daniel Vetterff9ce462013-04-24 14:57:17 +02007457 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007458 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7459 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007460 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007461 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007462 pipeconf |= PIPECONF_DITHER_EN |
7463 PIPECONF_DITHER_TYPE_SP;
7464
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007465 switch (crtc_state->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007466 case 18:
7467 pipeconf |= PIPECONF_6BPC;
7468 break;
7469 case 24:
7470 pipeconf |= PIPECONF_8BPC;
7471 break;
7472 case 30:
7473 pipeconf |= PIPECONF_10BPC;
7474 break;
7475 default:
7476 /* Case prevented by intel_choose_pipe_bpp_dither. */
7477 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007478 }
7479 }
7480
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007481 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007482 if (INTEL_GEN(dev_priv) < 4 ||
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007483 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007484 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7485 else
7486 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7487 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007488 pipeconf |= PIPECONF_PROGRESSIVE;
7489
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007490 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007491 crtc_state->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007492 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007493
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02007494 I915_WRITE(PIPECONF(crtc->pipe), pipeconf);
7495 POSTING_READ(PIPECONF(crtc->pipe));
Daniel Vetter84b046f2013-02-19 18:48:54 +01007496}
7497
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007498static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7499 struct intel_crtc_state *crtc_state)
7500{
7501 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007502 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007503 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007504 int refclk = 48000;
7505
7506 memset(&crtc_state->dpll_hw_state, 0,
7507 sizeof(crtc_state->dpll_hw_state));
7508
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007509 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007510 if (intel_panel_use_ssc(dev_priv)) {
7511 refclk = dev_priv->vbt.lvds_ssc_freq;
7512 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7513 }
7514
7515 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007516 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007517 limit = &intel_limits_i8xx_dvo;
7518 } else {
7519 limit = &intel_limits_i8xx_dac;
7520 }
7521
7522 if (!crtc_state->clock_set &&
7523 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7524 refclk, NULL, &crtc_state->dpll)) {
7525 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7526 return -EINVAL;
7527 }
7528
7529 i8xx_compute_dpll(crtc, crtc_state, NULL);
7530
7531 return 0;
7532}
7533
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007534static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7535 struct intel_crtc_state *crtc_state)
7536{
7537 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007538 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007539 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007540 int refclk = 96000;
7541
7542 memset(&crtc_state->dpll_hw_state, 0,
7543 sizeof(crtc_state->dpll_hw_state));
7544
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007545 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007546 if (intel_panel_use_ssc(dev_priv)) {
7547 refclk = dev_priv->vbt.lvds_ssc_freq;
7548 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7549 }
7550
7551 if (intel_is_dual_link_lvds(dev))
7552 limit = &intel_limits_g4x_dual_channel_lvds;
7553 else
7554 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007555 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7556 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007557 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007558 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007559 limit = &intel_limits_g4x_sdvo;
7560 } else {
7561 /* The option is for other outputs */
7562 limit = &intel_limits_i9xx_sdvo;
7563 }
7564
7565 if (!crtc_state->clock_set &&
7566 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7567 refclk, NULL, &crtc_state->dpll)) {
7568 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7569 return -EINVAL;
7570 }
7571
7572 i9xx_compute_dpll(crtc, crtc_state, NULL);
7573
7574 return 0;
7575}
7576
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007577static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7578 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007579{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007580 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007581 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007582 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007583 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007584
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007585 memset(&crtc_state->dpll_hw_state, 0,
7586 sizeof(crtc_state->dpll_hw_state));
7587
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007588 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007589 if (intel_panel_use_ssc(dev_priv)) {
7590 refclk = dev_priv->vbt.lvds_ssc_freq;
7591 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7592 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007593
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007594 limit = &intel_limits_pineview_lvds;
7595 } else {
7596 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007597 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007598
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007599 if (!crtc_state->clock_set &&
7600 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7601 refclk, NULL, &crtc_state->dpll)) {
7602 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7603 return -EINVAL;
7604 }
7605
7606 i9xx_compute_dpll(crtc, crtc_state, NULL);
7607
7608 return 0;
7609}
7610
7611static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7612 struct intel_crtc_state *crtc_state)
7613{
7614 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007615 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007616 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007617 int refclk = 96000;
7618
7619 memset(&crtc_state->dpll_hw_state, 0,
7620 sizeof(crtc_state->dpll_hw_state));
7621
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007622 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007623 if (intel_panel_use_ssc(dev_priv)) {
7624 refclk = dev_priv->vbt.lvds_ssc_freq;
7625 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007626 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007627
7628 limit = &intel_limits_i9xx_lvds;
7629 } else {
7630 limit = &intel_limits_i9xx_sdvo;
7631 }
7632
7633 if (!crtc_state->clock_set &&
7634 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7635 refclk, NULL, &crtc_state->dpll)) {
7636 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7637 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007638 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007639
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007640 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007641
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007642 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007643}
7644
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007645static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7646 struct intel_crtc_state *crtc_state)
7647{
7648 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007649 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007650
7651 memset(&crtc_state->dpll_hw_state, 0,
7652 sizeof(crtc_state->dpll_hw_state));
7653
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007654 if (!crtc_state->clock_set &&
7655 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7656 refclk, NULL, &crtc_state->dpll)) {
7657 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7658 return -EINVAL;
7659 }
7660
7661 chv_compute_dpll(crtc, crtc_state);
7662
7663 return 0;
7664}
7665
7666static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7667 struct intel_crtc_state *crtc_state)
7668{
7669 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007670 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007671
7672 memset(&crtc_state->dpll_hw_state, 0,
7673 sizeof(crtc_state->dpll_hw_state));
7674
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007675 if (!crtc_state->clock_set &&
7676 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7677 refclk, NULL, &crtc_state->dpll)) {
7678 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7679 return -EINVAL;
7680 }
7681
7682 vlv_compute_dpll(crtc, crtc_state);
7683
7684 return 0;
7685}
7686
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007687static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007688 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007689{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007690 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007691 uint32_t tmp;
7692
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007693 if (INTEL_GEN(dev_priv) <= 3 &&
7694 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007695 return;
7696
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007697 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007698 if (!(tmp & PFIT_ENABLE))
7699 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007700
Daniel Vetter06922822013-07-11 13:35:40 +02007701 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007702 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007703 if (crtc->pipe != PIPE_B)
7704 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007705 } else {
7706 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7707 return;
7708 }
7709
Daniel Vetter06922822013-07-11 13:35:40 +02007710 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007711 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007712}
7713
Jesse Barnesacbec812013-09-20 11:29:32 -07007714static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007715 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007716{
7717 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007718 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07007719 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007720 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07007721 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007722 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007723
Ville Syrjäläb5219732016-03-15 16:40:01 +02007724 /* In case of DSI, DPLL will not be used */
7725 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05307726 return;
7727
Ville Syrjäläa5805162015-05-26 20:42:30 +03007728 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007729 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007730 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007731
7732 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7733 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7734 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7735 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7736 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7737
Imre Deakdccbea32015-06-22 23:35:51 +03007738 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007739}
7740
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007741static void
7742i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7743 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007744{
7745 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007746 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007747 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
7748 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007749 enum pipe pipe;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007750 u32 val, base, offset;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007751 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007752 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007753 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007754 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007755
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007756 if (!plane->get_hw_state(plane, &pipe))
Damien Lespiau42a7b082015-02-05 19:35:13 +00007757 return;
7758
Ville Syrjäläeade6c82018-01-30 22:38:03 +02007759 WARN_ON(pipe != crtc->pipe);
7760
Damien Lespiaud9806c92015-01-21 14:07:19 +00007761 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007762 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007763 DRM_DEBUG_KMS("failed to alloc fb\n");
7764 return;
7765 }
7766
Damien Lespiau1b842c82015-01-21 13:50:54 +00007767 fb = &intel_fb->base;
7768
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02007769 fb->dev = dev;
7770
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02007771 val = I915_READ(DSPCNTR(i9xx_plane));
7772
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007773 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00007774 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007775 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02007776 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00007777 }
7778 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007779
7780 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007781 fourcc = i9xx_format_to_fourcc(pixel_format);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02007782 fb->format = drm_format_info(fourcc);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007783
Ville Syrjälä81894b22017-11-17 21:19:13 +02007784 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7785 offset = I915_READ(DSPOFFSET(i9xx_plane));
7786 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
7787 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007788 if (plane_config->tiling)
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007789 offset = I915_READ(DSPTILEOFF(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007790 else
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007791 offset = I915_READ(DSPLINOFF(i9xx_plane));
7792 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007793 } else {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007794 base = I915_READ(DSPADDR(i9xx_plane));
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007795 }
7796 plane_config->base = base;
7797
7798 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007799 fb->width = ((val >> 16) & 0xfff) + 1;
7800 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007801
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007802 val = I915_READ(DSPSTRIDE(i9xx_plane));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007803 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007804
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02007805 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007806
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007807 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007808
Ville Syrjälä282e83e2017-11-17 21:19:12 +02007809 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7810 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02007811 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiau2844a922015-01-20 12:51:48 +00007812 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007813
Damien Lespiau2d140302015-02-05 17:22:18 +00007814 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007815}
7816
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007817static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007818 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007819{
7820 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007821 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007822 int pipe = pipe_config->cpu_transcoder;
7823 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007824 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007825 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007826 int refclk = 100000;
7827
Ville Syrjäläb5219732016-03-15 16:40:01 +02007828 /* In case of DSI, DPLL will not be used */
7829 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7830 return;
7831
Ville Syrjäläa5805162015-05-26 20:42:30 +03007832 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007833 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7834 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7835 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7836 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007837 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007838 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007839
7840 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007841 clock.m2 = (pll_dw0 & 0xff) << 22;
7842 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7843 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007844 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7845 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7846 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7847
Imre Deakdccbea32015-06-22 23:35:51 +03007848 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007849}
7850
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007851static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007852 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007853{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007854 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02007855 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007856 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02007857 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007858
Imre Deak17290502016-02-12 18:55:11 +02007859 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7860 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02007861 return false;
7862
Daniel Vettere143a212013-07-04 12:01:15 +02007863 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02007864 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02007865
Imre Deak17290502016-02-12 18:55:11 +02007866 ret = false;
7867
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007868 tmp = I915_READ(PIPECONF(crtc->pipe));
7869 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02007870 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007871
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007872 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7873 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007874 switch (tmp & PIPECONF_BPC_MASK) {
7875 case PIPECONF_6BPC:
7876 pipe_config->pipe_bpp = 18;
7877 break;
7878 case PIPECONF_8BPC:
7879 pipe_config->pipe_bpp = 24;
7880 break;
7881 case PIPECONF_10BPC:
7882 pipe_config->pipe_bpp = 30;
7883 break;
7884 default:
7885 break;
7886 }
7887 }
7888
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007889 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08007890 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007891 pipe_config->limited_color_range = true;
7892
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007893 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03007894 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7895
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007896 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02007897 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007898
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007899 i9xx_get_pfit_config(crtc, pipe_config);
7900
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007901 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02007902 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007903 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02007904 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7905 else
7906 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02007907 pipe_config->pixel_multiplier =
7908 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7909 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007910 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007911 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02007912 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02007913 tmp = I915_READ(DPLL(crtc->pipe));
7914 pipe_config->pixel_multiplier =
7915 ((tmp & SDVO_MULTIPLIER_MASK)
7916 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7917 } else {
7918 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7919 * port and will be fixed up in the encoder->get_config
7920 * function. */
7921 pipe_config->pixel_multiplier = 1;
7922 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007923 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007924 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007925 /*
7926 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7927 * on 830. Filter it out here so that we don't
7928 * report errors due to that.
7929 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007930 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007931 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7932
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007933 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7934 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007935 } else {
7936 /* Mask out read-only status bits. */
7937 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7938 DPLL_PORTC_READY_MASK |
7939 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007940 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007941
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007942 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007943 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007944 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07007945 vlv_crtc_clock_get(crtc, pipe_config);
7946 else
7947 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007948
Ville Syrjälä0f646142015-08-26 19:39:18 +03007949 /*
7950 * Normally the dotclock is filled in by the encoder .get_config()
7951 * but in case the pipe is enabled w/o any ports we need a sane
7952 * default.
7953 */
7954 pipe_config->base.adjusted_mode.crtc_clock =
7955 pipe_config->port_clock / pipe_config->pixel_multiplier;
7956
Imre Deak17290502016-02-12 18:55:11 +02007957 ret = true;
7958
7959out:
7960 intel_display_power_put(dev_priv, power_domain);
7961
7962 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007963}
7964
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007965static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007966{
Jesse Barnes13d83a62011-08-03 12:59:20 -07007967 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04007968 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007969 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007970 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007971 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007972 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007973 bool has_ck505 = false;
7974 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04007975 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007976
7977 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02007978 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007979 switch (encoder->type) {
7980 case INTEL_OUTPUT_LVDS:
7981 has_panel = true;
7982 has_lvds = true;
7983 break;
7984 case INTEL_OUTPUT_EDP:
7985 has_panel = true;
Ville Syrjälä8f4f2792017-11-09 17:24:34 +02007986 if (encoder->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007987 has_cpu_edp = true;
7988 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007989 default:
7990 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007991 }
7992 }
7993
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007994 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007995 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007996 can_ssc = has_ck505;
7997 } else {
7998 has_ck505 = false;
7999 can_ssc = true;
8000 }
8001
Lyude1c1a24d2016-06-14 11:04:09 -04008002 /* Check if any DPLLs are using the SSC source */
8003 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8004 u32 temp = I915_READ(PCH_DPLL(i));
8005
8006 if (!(temp & DPLL_VCO_ENABLE))
8007 continue;
8008
8009 if ((temp & PLL_REF_INPUT_MASK) ==
8010 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8011 using_ssc_source = true;
8012 break;
8013 }
8014 }
8015
8016 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8017 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008018
8019 /* Ironlake: try to setup display ref clock before DPLL
8020 * enabling. This is only under driver's control after
8021 * PCH B stepping, previous chipset stepping should be
8022 * ignoring this setting.
8023 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008024 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008025
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008026 /* As we must carefully and slowly disable/enable each source in turn,
8027 * compute the final state we want first and check if we need to
8028 * make any changes at all.
8029 */
8030 final = val;
8031 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008032 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008033 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008034 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008035 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8036
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008037 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008038 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008039 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008040
Keith Packard199e5d72011-09-22 12:01:57 -07008041 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008042 final |= DREF_SSC_SOURCE_ENABLE;
8043
8044 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8045 final |= DREF_SSC1_ENABLE;
8046
8047 if (has_cpu_edp) {
8048 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8049 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8050 else
8051 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8052 } else
8053 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008054 } else if (using_ssc_source) {
8055 final |= DREF_SSC_SOURCE_ENABLE;
8056 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008057 }
8058
8059 if (final == val)
8060 return;
8061
8062 /* Always enable nonspread source */
8063 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8064
8065 if (has_ck505)
8066 val |= DREF_NONSPREAD_CK505_ENABLE;
8067 else
8068 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8069
8070 if (has_panel) {
8071 val &= ~DREF_SSC_SOURCE_MASK;
8072 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008073
Keith Packard199e5d72011-09-22 12:01:57 -07008074 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008075 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008076 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008077 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008078 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008079 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008080
8081 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008082 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008083 POSTING_READ(PCH_DREF_CONTROL);
8084 udelay(200);
8085
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008086 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008087
8088 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008089 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008090 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008091 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008092 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008093 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008094 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008095 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008096 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008097
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008098 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008099 POSTING_READ(PCH_DREF_CONTROL);
8100 udelay(200);
8101 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008102 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008103
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008104 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008105
8106 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008107 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008108
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008109 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008110 POSTING_READ(PCH_DREF_CONTROL);
8111 udelay(200);
8112
Lyude1c1a24d2016-06-14 11:04:09 -04008113 if (!using_ssc_source) {
8114 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008115
Lyude1c1a24d2016-06-14 11:04:09 -04008116 /* Turn off the SSC source */
8117 val &= ~DREF_SSC_SOURCE_MASK;
8118 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008119
Lyude1c1a24d2016-06-14 11:04:09 -04008120 /* Turn off SSC1 */
8121 val &= ~DREF_SSC1_ENABLE;
8122
8123 I915_WRITE(PCH_DREF_CONTROL, val);
8124 POSTING_READ(PCH_DREF_CONTROL);
8125 udelay(200);
8126 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008127 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008128
8129 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008130}
8131
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008132static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008133{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008134 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008135
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008136 tmp = I915_READ(SOUTH_CHICKEN2);
8137 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8138 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008139
Imre Deakcf3598c2016-06-28 13:37:31 +03008140 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8141 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008142 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008143
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008144 tmp = I915_READ(SOUTH_CHICKEN2);
8145 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8146 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008147
Imre Deakcf3598c2016-06-28 13:37:31 +03008148 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8149 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008150 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008151}
8152
8153/* WaMPhyProgramming:hsw */
8154static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8155{
8156 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008157
8158 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8159 tmp &= ~(0xFF << 24);
8160 tmp |= (0x12 << 24);
8161 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8162
Paulo Zanonidde86e22012-12-01 12:04:25 -02008163 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8164 tmp |= (1 << 11);
8165 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8166
8167 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8168 tmp |= (1 << 11);
8169 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8170
Paulo Zanonidde86e22012-12-01 12:04:25 -02008171 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8172 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8173 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8174
8175 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8176 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8177 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8178
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008179 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8180 tmp &= ~(7 << 13);
8181 tmp |= (5 << 13);
8182 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008183
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008184 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8185 tmp &= ~(7 << 13);
8186 tmp |= (5 << 13);
8187 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008188
8189 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8190 tmp &= ~0xFF;
8191 tmp |= 0x1C;
8192 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8193
8194 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8195 tmp &= ~0xFF;
8196 tmp |= 0x1C;
8197 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8198
8199 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8200 tmp &= ~(0xFF << 16);
8201 tmp |= (0x1C << 16);
8202 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8203
8204 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8205 tmp &= ~(0xFF << 16);
8206 tmp |= (0x1C << 16);
8207 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8208
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008209 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8210 tmp |= (1 << 27);
8211 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008212
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008213 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8214 tmp |= (1 << 27);
8215 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008216
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008217 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8218 tmp &= ~(0xF << 28);
8219 tmp |= (4 << 28);
8220 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008221
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008222 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8223 tmp &= ~(0xF << 28);
8224 tmp |= (4 << 28);
8225 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008226}
8227
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008228/* Implements 3 different sequences from BSpec chapter "Display iCLK
8229 * Programming" based on the parameters passed:
8230 * - Sequence to enable CLKOUT_DP
8231 * - Sequence to enable CLKOUT_DP without spread
8232 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8233 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008234static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
8235 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008236{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008237 uint32_t reg, tmp;
8238
8239 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8240 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008241 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
8242 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008243 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008244
Ville Syrjäläa5805162015-05-26 20:42:30 +03008245 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008246
8247 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8248 tmp &= ~SBI_SSCCTL_DISABLE;
8249 tmp |= SBI_SSCCTL_PATHALT;
8250 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8251
8252 udelay(24);
8253
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008254 if (with_spread) {
8255 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8256 tmp &= ~SBI_SSCCTL_PATHALT;
8257 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008258
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008259 if (with_fdi) {
8260 lpt_reset_fdi_mphy(dev_priv);
8261 lpt_program_fdi_mphy(dev_priv);
8262 }
8263 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008264
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008265 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008266 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8267 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8268 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008269
Ville Syrjäläa5805162015-05-26 20:42:30 +03008270 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008271}
8272
Paulo Zanoni47701c32013-07-23 11:19:25 -03008273/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008274static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03008275{
Paulo Zanoni47701c32013-07-23 11:19:25 -03008276 uint32_t reg, tmp;
8277
Ville Syrjäläa5805162015-05-26 20:42:30 +03008278 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008279
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008280 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008281 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8282 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8283 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8284
8285 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8286 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8287 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8288 tmp |= SBI_SSCCTL_PATHALT;
8289 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8290 udelay(32);
8291 }
8292 tmp |= SBI_SSCCTL_DISABLE;
8293 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8294 }
8295
Ville Syrjäläa5805162015-05-26 20:42:30 +03008296 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008297}
8298
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008299#define BEND_IDX(steps) ((50 + (steps)) / 5)
8300
8301static const uint16_t sscdivintphase[] = {
8302 [BEND_IDX( 50)] = 0x3B23,
8303 [BEND_IDX( 45)] = 0x3B23,
8304 [BEND_IDX( 40)] = 0x3C23,
8305 [BEND_IDX( 35)] = 0x3C23,
8306 [BEND_IDX( 30)] = 0x3D23,
8307 [BEND_IDX( 25)] = 0x3D23,
8308 [BEND_IDX( 20)] = 0x3E23,
8309 [BEND_IDX( 15)] = 0x3E23,
8310 [BEND_IDX( 10)] = 0x3F23,
8311 [BEND_IDX( 5)] = 0x3F23,
8312 [BEND_IDX( 0)] = 0x0025,
8313 [BEND_IDX( -5)] = 0x0025,
8314 [BEND_IDX(-10)] = 0x0125,
8315 [BEND_IDX(-15)] = 0x0125,
8316 [BEND_IDX(-20)] = 0x0225,
8317 [BEND_IDX(-25)] = 0x0225,
8318 [BEND_IDX(-30)] = 0x0325,
8319 [BEND_IDX(-35)] = 0x0325,
8320 [BEND_IDX(-40)] = 0x0425,
8321 [BEND_IDX(-45)] = 0x0425,
8322 [BEND_IDX(-50)] = 0x0525,
8323};
8324
8325/*
8326 * Bend CLKOUT_DP
8327 * steps -50 to 50 inclusive, in steps of 5
8328 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8329 * change in clock period = -(steps / 10) * 5.787 ps
8330 */
8331static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8332{
8333 uint32_t tmp;
8334 int idx = BEND_IDX(steps);
8335
8336 if (WARN_ON(steps % 5 != 0))
8337 return;
8338
8339 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8340 return;
8341
8342 mutex_lock(&dev_priv->sb_lock);
8343
8344 if (steps % 10 != 0)
8345 tmp = 0xAAAAAAAB;
8346 else
8347 tmp = 0x00000000;
8348 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8349
8350 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8351 tmp &= 0xffff0000;
8352 tmp |= sscdivintphase[idx];
8353 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8354
8355 mutex_unlock(&dev_priv->sb_lock);
8356}
8357
8358#undef BEND_IDX
8359
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008360static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008361{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008362 struct intel_encoder *encoder;
8363 bool has_vga = false;
8364
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008365 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008366 switch (encoder->type) {
8367 case INTEL_OUTPUT_ANALOG:
8368 has_vga = true;
8369 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008370 default:
8371 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008372 }
8373 }
8374
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008375 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008376 lpt_bend_clkout_dp(dev_priv, 0);
8377 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008378 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008379 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008380 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008381}
8382
Paulo Zanonidde86e22012-12-01 12:04:25 -02008383/*
8384 * Initialize reference clocks when the driver loads
8385 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008386void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008387{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008388 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008389 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008390 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008391 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008392}
8393
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008394static void ironlake_set_pipeconf(const struct intel_crtc_state *crtc_state)
Paulo Zanonic8203562012-09-12 10:06:29 -03008395{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008396 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8397 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8398 enum pipe pipe = crtc->pipe;
Paulo Zanonic8203562012-09-12 10:06:29 -03008399 uint32_t val;
8400
Daniel Vetter78114072013-06-13 00:54:57 +02008401 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008402
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008403 switch (crtc_state->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008404 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008405 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008406 break;
8407 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008408 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008409 break;
8410 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008411 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008412 break;
8413 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008414 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008415 break;
8416 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008417 /* Case prevented by intel_choose_pipe_bpp_dither. */
8418 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008419 }
8420
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008421 if (crtc_state->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008422 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8423
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008424 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008425 val |= PIPECONF_INTERLACED_ILK;
8426 else
8427 val |= PIPECONF_PROGRESSIVE;
8428
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008429 if (crtc_state->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008430 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008431
Paulo Zanonic8203562012-09-12 10:06:29 -03008432 I915_WRITE(PIPECONF(pipe), val);
8433 POSTING_READ(PIPECONF(pipe));
8434}
8435
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008436static void haswell_set_pipeconf(const struct intel_crtc_state *crtc_state)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008437{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008438 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8439 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8440 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008441 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008442
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008443 if (IS_HASWELL(dev_priv) && crtc_state->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008444 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8445
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008446 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008447 val |= PIPECONF_INTERLACED_ILK;
8448 else
8449 val |= PIPECONF_PROGRESSIVE;
8450
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008451 I915_WRITE(PIPECONF(cpu_transcoder), val);
8452 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008453}
8454
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008455static void haswell_set_pipemisc(const struct intel_crtc_state *crtc_state)
Jani Nikula391bf042016-03-18 17:05:40 +02008456{
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
8458 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008459
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00008460 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
Jani Nikula391bf042016-03-18 17:05:40 +02008461 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008462
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008463 switch (crtc_state->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008464 case 18:
8465 val |= PIPEMISC_DITHER_6_BPC;
8466 break;
8467 case 24:
8468 val |= PIPEMISC_DITHER_8_BPC;
8469 break;
8470 case 30:
8471 val |= PIPEMISC_DITHER_10_BPC;
8472 break;
8473 case 36:
8474 val |= PIPEMISC_DITHER_12_BPC;
8475 break;
8476 default:
8477 /* Case prevented by pipe_config_set_bpp. */
8478 BUG();
8479 }
8480
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008481 if (crtc_state->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008482 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8483
Maarten Lankhorstfdf73512018-10-04 11:45:52 +02008484 if (crtc_state->ycbcr420) {
Shashank Sharmab22ca992017-07-24 19:19:32 +05308485 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
8486 PIPEMISC_YUV420_ENABLE |
8487 PIPEMISC_YUV420_MODE_FULL_BLEND;
8488 }
8489
Jani Nikula391bf042016-03-18 17:05:40 +02008490 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008491 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008492}
8493
Paulo Zanonid4b19312012-11-29 11:29:32 -02008494int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8495{
8496 /*
8497 * Account for spread spectrum to avoid
8498 * oversubscribing the link. Max center spread
8499 * is 2.5%; use 5% for safety's sake.
8500 */
8501 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008502 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008503}
8504
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008505static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008506{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008507 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008508}
8509
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008510static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8511 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008512 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008513{
8514 struct drm_crtc *crtc = &intel_crtc->base;
8515 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008516 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008517 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008518 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008519
Chris Wilsonc1858122010-12-03 21:35:48 +00008520 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008521 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008522 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008523 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008524 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008525 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008526 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008527 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008528 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008529
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008530 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008531
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008532 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8533 fp |= FP_CB_TUNE;
8534
8535 if (reduced_clock) {
8536 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8537
8538 if (reduced_clock->m < factor * reduced_clock->n)
8539 fp2 |= FP_CB_TUNE;
8540 } else {
8541 fp2 = fp;
8542 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008543
Chris Wilson5eddb702010-09-11 13:48:45 +01008544 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008545
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008546 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008547 dpll |= DPLLB_MODE_LVDS;
8548 else
8549 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008550
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008551 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008552 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008553
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008554 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8555 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008556 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008557
Ville Syrjälä37a56502016-06-22 21:57:04 +03008558 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008559 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008560
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03008561 /*
8562 * The high speed IO clock is only really required for
8563 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8564 * possible to share the DPLL between CRT and HDMI. Enabling
8565 * the clock needlessly does no real harm, except use up a
8566 * bit of power potentially.
8567 *
8568 * We'll limit this to IVB with 3 pipes, since it has only two
8569 * DPLLs and so DPLL sharing is the only way to get three pipes
8570 * driving PCH ports at the same time. On SNB we could do this,
8571 * and potentially avoid enabling the second DPLL, but it's not
8572 * clear if it''s a win or loss power wise. No point in doing
8573 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8574 */
8575 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8576 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8577 dpll |= DPLL_SDVO_HIGH_SPEED;
8578
Eric Anholta07d6782011-03-30 13:01:08 -07008579 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008580 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008581 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008582 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008583
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008584 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008585 case 5:
8586 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8587 break;
8588 case 7:
8589 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8590 break;
8591 case 10:
8592 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8593 break;
8594 case 14:
8595 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8596 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008597 }
8598
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008599 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8600 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008601 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008602 else
8603 dpll |= PLL_REF_INPUT_DREFCLK;
8604
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008605 dpll |= DPLL_VCO_ENABLE;
8606
8607 crtc_state->dpll_hw_state.dpll = dpll;
8608 crtc_state->dpll_hw_state.fp0 = fp;
8609 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008610}
8611
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008612static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8613 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008614{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008615 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008616 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008617 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008618 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008619
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008620 memset(&crtc_state->dpll_hw_state, 0,
8621 sizeof(crtc_state->dpll_hw_state));
8622
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008623 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8624 if (!crtc_state->has_pch_encoder)
8625 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008626
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008627 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008628 if (intel_panel_use_ssc(dev_priv)) {
8629 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8630 dev_priv->vbt.lvds_ssc_freq);
8631 refclk = dev_priv->vbt.lvds_ssc_freq;
8632 }
8633
8634 if (intel_is_dual_link_lvds(dev)) {
8635 if (refclk == 100000)
8636 limit = &intel_limits_ironlake_dual_lvds_100m;
8637 else
8638 limit = &intel_limits_ironlake_dual_lvds;
8639 } else {
8640 if (refclk == 100000)
8641 limit = &intel_limits_ironlake_single_lvds_100m;
8642 else
8643 limit = &intel_limits_ironlake_single_lvds;
8644 }
8645 } else {
8646 limit = &intel_limits_ironlake_dac;
8647 }
8648
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008649 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008650 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8651 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008652 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8653 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008654 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008655
Gustavo A. R. Silvacbaa3312017-05-15 16:56:05 -05008656 ironlake_compute_dpll(crtc, crtc_state, NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008657
Gustavo A. R. Silvaefd38b62017-05-15 17:00:28 -05008658 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
Chris Wilson43031782018-09-13 14:16:26 +01008659 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
8660 pipe_name(crtc->pipe));
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008661 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008662 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008663
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008664 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008665}
8666
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008667static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8668 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008669{
8670 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008671 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008672 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008673
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008674 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8675 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8676 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8677 & ~TU_SIZE_MASK;
8678 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8679 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8680 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8681}
8682
8683static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8684 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008685 struct intel_link_m_n *m_n,
8686 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008687{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008688 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008689 enum pipe pipe = crtc->pipe;
8690
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008691 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008692 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8693 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8694 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8695 & ~TU_SIZE_MASK;
8696 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8697 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8698 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008699 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8700 * gen < 8) and if DRRS is supported (to make sure the
8701 * registers are not unnecessarily read).
8702 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008703 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008704 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008705 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8706 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8707 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8708 & ~TU_SIZE_MASK;
8709 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8710 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8711 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8712 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008713 } else {
8714 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8715 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8716 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8717 & ~TU_SIZE_MASK;
8718 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8719 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8720 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8721 }
8722}
8723
8724void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008725 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008726{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008727 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008728 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8729 else
8730 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008731 &pipe_config->dp_m_n,
8732 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008733}
8734
Daniel Vetter72419202013-04-04 13:28:53 +02008735static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008736 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008737{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008738 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008739 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008740}
8741
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008742static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008743 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008744{
8745 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008746 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07008747 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8748 uint32_t ps_ctrl = 0;
8749 int id = -1;
8750 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008751
Chandra Kondurua1b22782015-04-07 15:28:45 -07008752 /* find scaler attached to this pipe */
8753 for (i = 0; i < crtc->num_scalers; i++) {
8754 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8755 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8756 id = i;
8757 pipe_config->pch_pfit.enabled = true;
8758 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8759 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8760 break;
8761 }
8762 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008763
Chandra Kondurua1b22782015-04-07 15:28:45 -07008764 scaler_state->scaler_id = id;
8765 if (id >= 0) {
8766 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8767 } else {
8768 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008769 }
8770}
8771
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008772static void
8773skylake_get_initial_plane_config(struct intel_crtc *crtc,
8774 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008775{
8776 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008777 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008778 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
8779 enum plane_id plane_id = plane->id;
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008780 enum pipe pipe;
James Ausmus4036c782017-11-13 10:11:28 -08008781 u32 val, base, offset, stride_mult, tiling, alpha;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008782 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008783 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008784 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008785 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008786
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008787 if (!plane->get_hw_state(plane, &pipe))
Ville Syrjälä2924b8c2017-11-17 21:19:16 +02008788 return;
8789
Ville Syrjäläeade6c82018-01-30 22:38:03 +02008790 WARN_ON(pipe != crtc->pipe);
8791
Damien Lespiaud9806c92015-01-21 14:07:19 +00008792 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008793 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008794 DRM_DEBUG_KMS("failed to alloc fb\n");
8795 return;
8796 }
8797
Damien Lespiau1b842c82015-01-21 13:50:54 +00008798 fb = &intel_fb->base;
8799
Ville Syrjäläd2e9f5f2016-11-18 21:52:53 +02008800 fb->dev = dev;
8801
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008802 val = I915_READ(PLANE_CTL(pipe, plane_id));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008803
James Ausmusb5972772018-01-30 11:49:16 -02008804 if (INTEL_GEN(dev_priv) >= 11)
8805 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
8806 else
8807 pixel_format = val & PLANE_CTL_FORMAT_MASK;
James Ausmus4036c782017-11-13 10:11:28 -08008808
8809 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008810 alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
James Ausmus4036c782017-11-13 10:11:28 -08008811 alpha &= PLANE_COLOR_ALPHA_MASK;
8812 } else {
8813 alpha = val & PLANE_CTL_ALPHA_MASK;
8814 }
8815
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008816 fourcc = skl_format_to_fourcc(pixel_format,
James Ausmus4036c782017-11-13 10:11:28 -08008817 val & PLANE_CTL_ORDER_RGBX, alpha);
Ville Syrjälä2f3f4762016-11-18 21:52:57 +02008818 fb->format = drm_format_info(fourcc);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008819
Damien Lespiau40f46282015-02-27 11:15:21 +00008820 tiling = val & PLANE_CTL_TILED_MASK;
8821 switch (tiling) {
8822 case PLANE_CTL_TILED_LINEAR:
Ben Widawsky2f075562017-03-24 14:29:48 -07008823 fb->modifier = DRM_FORMAT_MOD_LINEAR;
Damien Lespiau40f46282015-02-27 11:15:21 +00008824 break;
8825 case PLANE_CTL_TILED_X:
8826 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008827 fb->modifier = I915_FORMAT_MOD_X_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008828 break;
8829 case PLANE_CTL_TILED_Y:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07008830 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008831 fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
8832 else
8833 fb->modifier = I915_FORMAT_MOD_Y_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008834 break;
8835 case PLANE_CTL_TILED_YF:
Dhinakaran Pandiyan53867b42018-08-21 18:50:53 -07008836 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07008837 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
8838 else
8839 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00008840 break;
8841 default:
8842 MISSING_CASE(tiling);
8843 goto error;
8844 }
8845
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008846 base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008847 plane_config->base = base;
8848
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008849 offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008850
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008851 val = I915_READ(PLANE_SIZE(pipe, plane_id));
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008852 fb->height = ((val >> 16) & 0xfff) + 1;
8853 fb->width = ((val >> 0) & 0x1fff) + 1;
8854
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008855 val = I915_READ(PLANE_STRIDE(pipe, plane_id));
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008856 stride_mult = intel_fb_stride_alignment(fb, 0);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008857 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8858
Ville Syrjäläd88c4af2017-03-07 21:42:06 +02008859 aligned_height = intel_fb_align_height(fb, 0, fb->height);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008860
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008861 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008862
Ville Syrjälä282e83e2017-11-17 21:19:12 +02008863 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8864 crtc->base.name, plane->base.name, fb->width, fb->height,
Ville Syrjälä272725c2016-12-14 23:32:20 +02008865 fb->format->cpp[0] * 8, base, fb->pitches[0],
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008866 plane_config->size);
8867
Damien Lespiau2d140302015-02-05 17:22:18 +00008868 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008869 return;
8870
8871error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01008872 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008873}
8874
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008875static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008876 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008877{
8878 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008879 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008880 uint32_t tmp;
8881
8882 tmp = I915_READ(PF_CTL(crtc->pipe));
8883
8884 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008885 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008886 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8887 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008888
8889 /* We currently do not free assignements of panel fitters on
8890 * ivb/hsw (since we don't use the higher upscaling modes which
8891 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008892 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008893 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8894 PF_PIPE_SEL_IVB(crtc->pipe));
8895 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008896 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008897}
8898
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008899static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008900 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008901{
8902 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008903 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008904 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008905 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008906 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008907
Imre Deak17290502016-02-12 18:55:11 +02008908 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8909 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008910 return false;
8911
Daniel Vettere143a212013-07-04 12:01:15 +02008912 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008913 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008914
Imre Deak17290502016-02-12 18:55:11 +02008915 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008916 tmp = I915_READ(PIPECONF(crtc->pipe));
8917 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008918 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008919
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008920 switch (tmp & PIPECONF_BPC_MASK) {
8921 case PIPECONF_6BPC:
8922 pipe_config->pipe_bpp = 18;
8923 break;
8924 case PIPECONF_8BPC:
8925 pipe_config->pipe_bpp = 24;
8926 break;
8927 case PIPECONF_10BPC:
8928 pipe_config->pipe_bpp = 30;
8929 break;
8930 case PIPECONF_12BPC:
8931 pipe_config->pipe_bpp = 36;
8932 break;
8933 default:
8934 break;
8935 }
8936
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008937 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8938 pipe_config->limited_color_range = true;
8939
Daniel Vetterab9412b2013-05-03 11:49:46 +02008940 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008941 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008942 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008943
Daniel Vetter88adfff2013-03-28 10:42:01 +01008944 pipe_config->has_pch_encoder = true;
8945
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008946 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8947 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8948 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008949
8950 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008951
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008952 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03008953 /*
8954 * The pipe->pch transcoder and pch transcoder->pll
8955 * mapping is fixed.
8956 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008957 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008958 } else {
8959 tmp = I915_READ(PCH_DPLL_SEL);
8960 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008961 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008962 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008963 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008964 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008965
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008966 pipe_config->shared_dpll =
8967 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8968 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008969
Lucas De Marchiee1398b2018-03-20 15:06:33 -07008970 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
8971 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008972
8973 tmp = pipe_config->dpll_hw_state.dpll;
8974 pipe_config->pixel_multiplier =
8975 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8976 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008977
8978 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008979 } else {
8980 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008981 }
8982
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008983 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008984 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008985
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008986 ironlake_get_pfit_config(crtc, pipe_config);
8987
Imre Deak17290502016-02-12 18:55:11 +02008988 ret = true;
8989
8990out:
8991 intel_display_power_put(dev_priv, power_domain);
8992
8993 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008994}
8995
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008996static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8997{
Chris Wilson91c8a322016-07-05 10:40:23 +01008998 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008999 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009000
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009001 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009002 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009003 pipe_name(crtc->pipe));
9004
Imre Deak75e39682018-08-06 12:58:39 +03009005 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL2),
Imre Deak9c3a16c2017-08-14 18:15:30 +03009006 "Display power well on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009007 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009008 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9009 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03009010 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009011 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009012 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009013 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -05009014 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009015 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009016 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009017 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009018 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009019 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009020 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009021
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009022 /*
9023 * In theory we can still leave IRQs enabled, as long as only the HPD
9024 * interrupts remain enabled. We used to check for that, but since it's
9025 * gen-specific and since we only disable LCPLL after we fully disable
9026 * the interrupts, the check below should be enough.
9027 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009028 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009029}
9030
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009031static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9032{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009033 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009034 return I915_READ(D_COMP_HSW);
9035 else
9036 return I915_READ(D_COMP_BDW);
9037}
9038
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009039static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9040{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01009041 if (IS_HASWELL(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009042 mutex_lock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009043 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9044 val))
Chris Wilson79cf2192016-08-24 11:16:07 +01009045 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009046 mutex_unlock(&dev_priv->pcu_lock);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009047 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009048 I915_WRITE(D_COMP_BDW, val);
9049 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009050 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009051}
9052
9053/*
9054 * This function implements pieces of two sequences from BSpec:
9055 * - Sequence for display software to disable LCPLL
9056 * - Sequence for display software to allow package C8+
9057 * The steps implemented here are just the steps that actually touch the LCPLL
9058 * register. Callers should take care of disabling all the display engine
9059 * functions, doing the mode unset, fixing interrupts, etc.
9060 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009061static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9062 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009063{
9064 uint32_t val;
9065
9066 assert_can_disable_lcpll(dev_priv);
9067
9068 val = I915_READ(LCPLL_CTL);
9069
9070 if (switch_to_fclk) {
9071 val |= LCPLL_CD_SOURCE_FCLK;
9072 I915_WRITE(LCPLL_CTL, val);
9073
Imre Deakf53dd632016-06-28 13:37:32 +03009074 if (wait_for_us(I915_READ(LCPLL_CTL) &
9075 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009076 DRM_ERROR("Switching to FCLK failed\n");
9077
9078 val = I915_READ(LCPLL_CTL);
9079 }
9080
9081 val |= LCPLL_PLL_DISABLE;
9082 I915_WRITE(LCPLL_CTL, val);
9083 POSTING_READ(LCPLL_CTL);
9084
Chris Wilson24d84412016-06-30 15:33:07 +01009085 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009086 DRM_ERROR("LCPLL still locked\n");
9087
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009088 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009089 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009090 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009091 ndelay(100);
9092
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009093 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9094 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009095 DRM_ERROR("D_COMP RCOMP still in progress\n");
9096
9097 if (allow_power_down) {
9098 val = I915_READ(LCPLL_CTL);
9099 val |= LCPLL_POWER_DOWN_ALLOW;
9100 I915_WRITE(LCPLL_CTL, val);
9101 POSTING_READ(LCPLL_CTL);
9102 }
9103}
9104
9105/*
9106 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9107 * source.
9108 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009109static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009110{
9111 uint32_t val;
9112
9113 val = I915_READ(LCPLL_CTL);
9114
9115 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9116 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9117 return;
9118
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009119 /*
9120 * Make sure we're not on PC8 state before disabling PC8, otherwise
9121 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009122 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009123 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009124
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009125 if (val & LCPLL_POWER_DOWN_ALLOW) {
9126 val &= ~LCPLL_POWER_DOWN_ALLOW;
9127 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009128 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009129 }
9130
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009131 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009132 val |= D_COMP_COMP_FORCE;
9133 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009134 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009135
9136 val = I915_READ(LCPLL_CTL);
9137 val &= ~LCPLL_PLL_DISABLE;
9138 I915_WRITE(LCPLL_CTL, val);
9139
Chris Wilson93220c02016-06-30 15:33:08 +01009140 if (intel_wait_for_register(dev_priv,
9141 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9142 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009143 DRM_ERROR("LCPLL not locked yet\n");
9144
9145 if (val & LCPLL_CD_SOURCE_FCLK) {
9146 val = I915_READ(LCPLL_CTL);
9147 val &= ~LCPLL_CD_SOURCE_FCLK;
9148 I915_WRITE(LCPLL_CTL, val);
9149
Imre Deakf53dd632016-06-28 13:37:32 +03009150 if (wait_for_us((I915_READ(LCPLL_CTL) &
9151 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009152 DRM_ERROR("Switching back to LCPLL failed\n");
9153 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009154
Mika Kuoppala59bad942015-01-16 11:34:40 +02009155 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009156
Ville Syrjälä4c75b942016-10-31 22:37:12 +02009157 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +03009158 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009159}
9160
Paulo Zanoni765dab672014-03-07 20:08:18 -03009161/*
9162 * Package states C8 and deeper are really deep PC states that can only be
9163 * reached when all the devices on the system allow it, so even if the graphics
9164 * device allows PC8+, it doesn't mean the system will actually get to these
9165 * states. Our driver only allows PC8+ when going into runtime PM.
9166 *
9167 * The requirements for PC8+ are that all the outputs are disabled, the power
9168 * well is disabled and most interrupts are disabled, and these are also
9169 * requirements for runtime PM. When these conditions are met, we manually do
9170 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9171 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9172 * hang the machine.
9173 *
9174 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9175 * the state of some registers, so when we come back from PC8+ we need to
9176 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9177 * need to take care of the registers kept by RC6. Notice that this happens even
9178 * if we don't put the device in PCI D3 state (which is what currently happens
9179 * because of the runtime PM support).
9180 *
9181 * For more, read "Display Sequences for Package C8" on the hardware
9182 * documentation.
9183 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009184void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009185{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009186 uint32_t val;
9187
Paulo Zanonic67a4702013-08-19 13:18:09 -03009188 DRM_DEBUG_KMS("Enabling package C8+\n");
9189
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009190 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009191 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9192 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9193 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9194 }
9195
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009196 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009197 hsw_disable_lcpll(dev_priv, true, true);
9198}
9199
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009200void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009201{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009202 uint32_t val;
9203
Paulo Zanonic67a4702013-08-19 13:18:09 -03009204 DRM_DEBUG_KMS("Disabling package C8+\n");
9205
9206 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009207 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009208
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009209 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009210 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9211 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9212 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9213 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009214}
9215
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009216static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9217 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009218{
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03009219 struct intel_atomic_state *state =
9220 to_intel_atomic_state(crtc_state->base.state);
9221
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009222 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009223 struct intel_encoder *encoder =
Ville Syrjälä5a0b3852018-05-18 18:29:27 +03009224 intel_get_crtc_new_encoder(state, crtc_state);
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009225
9226 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
Chris Wilson43031782018-09-13 14:16:26 +01009227 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
9228 pipe_name(crtc->pipe));
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009229 return -EINVAL;
Paulo Zanoni44a126b2017-03-22 15:58:45 -03009230 }
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009231 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009232
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009233 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009234}
9235
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009236static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
9237 enum port port,
9238 struct intel_crtc_state *pipe_config)
9239{
9240 enum intel_dpll_id id;
9241 u32 temp;
9242
9243 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
Paulo Zanonidfbd4502017-08-25 16:40:04 -03009244 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
Kahola, Mika8b0f7e02017-06-09 15:26:03 -07009245
9246 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
9247 return;
9248
9249 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9250}
9251
Paulo Zanoni970888e2018-05-21 17:25:44 -07009252static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
9253 enum port port,
9254 struct intel_crtc_state *pipe_config)
9255{
9256 enum intel_dpll_id id;
9257 u32 temp;
9258
9259 /* TODO: TBT pll not implemented. */
9260 switch (port) {
9261 case PORT_A:
9262 case PORT_B:
9263 temp = I915_READ(DPCLKA_CFGCR0_ICL) &
9264 DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
9265 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
9266
9267 if (WARN_ON(id != DPLL_ID_ICL_DPLL0 && id != DPLL_ID_ICL_DPLL1))
9268 return;
9269 break;
9270 case PORT_C:
9271 id = DPLL_ID_ICL_MGPLL1;
9272 break;
9273 case PORT_D:
9274 id = DPLL_ID_ICL_MGPLL2;
9275 break;
9276 case PORT_E:
9277 id = DPLL_ID_ICL_MGPLL3;
9278 break;
9279 case PORT_F:
9280 id = DPLL_ID_ICL_MGPLL4;
9281 break;
9282 default:
9283 MISSING_CASE(port);
9284 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);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009546
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009547 pipe_config->gamma_mode =
9548 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9549
Rodrigo Vivibd30ca22017-09-26 14:13:46 -07009550 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
Shashank Sharmab22ca992017-07-24 19:19:32 +05309551 u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
9552 bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
9553
Rodrigo Vivibd30ca22017-09-26 14:13:46 -07009554 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Shashank Sharmab22ca992017-07-24 19:19:32 +05309555 bool blend_mode_420 = tmp &
9556 PIPEMISC_YUV420_MODE_FULL_BLEND;
9557
9558 pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
9559 if (pipe_config->ycbcr420 != clrspace_yuv ||
9560 pipe_config->ycbcr420 != blend_mode_420)
9561 DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
9562 } else if (clrspace_yuv) {
9563 DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
9564 }
9565 }
9566
Imre Deak17290502016-02-12 18:55:11 +02009567 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9568 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +02009569 power_domain_mask |= BIT_ULL(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009570 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009571 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009572 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009573 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009574 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009575
Maarten Lankhorst24f28452017-11-22 19:39:01 +01009576 if (hsw_crtc_supports_ips(crtc)) {
9577 if (IS_HASWELL(dev_priv))
9578 pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
9579 else {
9580 /*
9581 * We cannot readout IPS state on broadwell, set to
9582 * true so we can set it to a defined state on first
9583 * commit.
9584 */
9585 pipe_config->ips_enabled = true;
9586 }
9587 }
9588
Jani Nikula4d1de972016-03-18 17:05:42 +02009589 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9590 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -07009591 pipe_config->pixel_multiplier =
9592 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9593 } else {
9594 pipe_config->pixel_multiplier = 1;
9595 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009596
Imre Deak17290502016-02-12 18:55:11 +02009597out:
9598 for_each_power_domain(power_domain, power_domain_mask)
9599 intel_display_power_put(dev_priv, power_domain);
9600
Jani Nikulacf304292016-03-18 17:05:41 +02009601 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009602}
9603
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009604static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009605{
9606 struct drm_i915_private *dev_priv =
9607 to_i915(plane_state->base.plane->dev);
9608 const struct drm_framebuffer *fb = plane_state->base.fb;
9609 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9610 u32 base;
9611
9612 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9613 base = obj->phys_handle->busaddr;
9614 else
9615 base = intel_plane_ggtt_offset(plane_state);
9616
Ville Syrjäläc11ada02018-09-07 18:24:04 +03009617 base += plane_state->color_plane[0].offset;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009618
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009619 /* ILK+ do this automagically */
9620 if (HAS_GMCH_DISPLAY(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009621 plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjälä1cecc832017-03-27 21:55:34 +03009622 base += (plane_state->base.crtc_h *
9623 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9624
9625 return base;
9626}
9627
Ville Syrjäläed270222017-03-27 21:55:36 +03009628static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9629{
9630 int x = plane_state->base.crtc_x;
9631 int y = plane_state->base.crtc_y;
9632 u32 pos = 0;
9633
9634 if (x < 0) {
9635 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9636 x = -x;
9637 }
9638 pos |= x << CURSOR_X_SHIFT;
9639
9640 if (y < 0) {
9641 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9642 y = -y;
9643 }
9644 pos |= y << CURSOR_Y_SHIFT;
9645
9646 return pos;
9647}
9648
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009649static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9650{
9651 const struct drm_mode_config *config =
9652 &plane_state->base.plane->dev->mode_config;
9653 int width = plane_state->base.crtc_w;
9654 int height = plane_state->base.crtc_h;
9655
9656 return width > 0 && width <= config->cursor_width &&
9657 height > 0 && height <= config->cursor_height;
9658}
9659
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009660static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009661{
9662 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009663 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009664 int src_x, src_y;
9665 u32 offset;
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03009666 int ret;
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009667
9668 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
9669 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
9670
Ville Syrjäläfc3fed52018-09-18 17:02:43 +03009671 ret = intel_plane_check_stride(plane_state);
9672 if (ret)
9673 return ret;
9674
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009675 src_x = plane_state->base.src_x >> 16;
9676 src_y = plane_state->base.src_y >> 16;
9677
9678 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9679 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
9680 plane_state, 0);
9681
9682 if (src_x != 0 || src_y != 0) {
9683 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9684 return -EINVAL;
9685 }
9686
9687 plane_state->color_plane[0].offset = offset;
9688
9689 return 0;
9690}
9691
9692static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9693 struct intel_plane_state *plane_state)
9694{
9695 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009696 int ret;
9697
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009698 if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9699 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9700 return -EINVAL;
9701 }
9702
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009703 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
9704 &crtc_state->base,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +02009705 DRM_PLANE_HELPER_NO_SCALING,
9706 DRM_PLANE_HELPER_NO_SCALING,
9707 true, true);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009708 if (ret)
9709 return ret;
9710
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009711 if (!plane_state->base.visible)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009712 return 0;
9713
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +03009714 ret = intel_plane_check_src_coordinates(plane_state);
9715 if (ret)
9716 return ret;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009717
Ville Syrjäläfce8d232018-09-07 18:24:13 +03009718 ret = intel_cursor_check_surface(plane_state);
9719 if (ret)
9720 return ret;
Ville Syrjälä1e7b4fd2017-03-27 21:55:44 +03009721
Ville Syrjälä659056f2017-03-27 21:55:39 +03009722 return 0;
9723}
9724
Ville Syrjäläddd57132018-09-07 18:24:02 +03009725static unsigned int
9726i845_cursor_max_stride(struct intel_plane *plane,
9727 u32 pixel_format, u64 modifier,
9728 unsigned int rotation)
9729{
9730 return 2048;
9731}
9732
Ville Syrjälä292889e2017-03-17 23:18:01 +02009733static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9734 const struct intel_plane_state *plane_state)
9735{
Ville Syrjälä292889e2017-03-17 23:18:01 +02009736 return CURSOR_ENABLE |
9737 CURSOR_GAMMA_ENABLE |
9738 CURSOR_FORMAT_ARGB |
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009739 CURSOR_STRIDE(plane_state->color_plane[0].stride);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009740}
9741
Ville Syrjälä659056f2017-03-27 21:55:39 +03009742static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9743{
Ville Syrjälä659056f2017-03-27 21:55:39 +03009744 int width = plane_state->base.crtc_w;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009745
9746 /*
9747 * 845g/865g are only limited by the width of their cursors,
9748 * the height is arbitrary up to the precision of the register.
9749 */
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009750 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009751}
9752
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009753static int i845_check_cursor(struct intel_crtc_state *crtc_state,
Ville Syrjälä659056f2017-03-27 21:55:39 +03009754 struct intel_plane_state *plane_state)
9755{
9756 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009757 int ret;
9758
9759 ret = intel_check_cursor(crtc_state, plane_state);
9760 if (ret)
9761 return ret;
9762
9763 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009764 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009765 return 0;
9766
9767 /* Check for which cursor types we support */
9768 if (!i845_cursor_size_ok(plane_state)) {
9769 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9770 plane_state->base.crtc_w,
9771 plane_state->base.crtc_h);
9772 return -EINVAL;
9773 }
9774
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009775 WARN_ON(plane_state->base.visible &&
9776 plane_state->color_plane[0].stride != fb->pitches[0]);
9777
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009778 switch (fb->pitches[0]) {
Chris Wilson560b85b2010-08-07 11:01:38 +01009779 case 256:
9780 case 512:
9781 case 1024:
9782 case 2048:
Ville Syrjälädc41c152014-08-13 11:57:05 +03009783 break;
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009784 default:
9785 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9786 fb->pitches[0]);
9787 return -EINVAL;
Chris Wilson560b85b2010-08-07 11:01:38 +01009788 }
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009789
Ville Syrjälä659056f2017-03-27 21:55:39 +03009790 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9791
9792 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009793}
9794
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009795static void i845_update_cursor(struct intel_plane *plane,
9796 const struct intel_crtc_state *crtc_state,
Chris Wilson560b85b2010-08-07 11:01:38 +01009797 const struct intel_plane_state *plane_state)
9798{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +03009799 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009800 u32 cntl = 0, base = 0, pos = 0, size = 0;
9801 unsigned long irqflags;
Chris Wilson560b85b2010-08-07 11:01:38 +01009802
Ville Syrjälä936e71e2016-07-26 19:06:59 +03009803 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +01009804 unsigned int width = plane_state->base.crtc_w;
9805 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009806
Ville Syrjäläa0864d52017-03-23 21:27:09 +02009807 cntl = plane_state->ctl;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009808 size = (height << 12) | width;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009809
9810 base = intel_cursor_base(plane_state);
9811 pos = intel_cursor_position(plane_state);
Chris Wilson4b0e3332014-05-30 16:35:26 +03009812 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009813
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009814 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9815
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009816 /* On these chipsets we can only modify the base/size/stride
9817 * whilst the cursor is disabled.
9818 */
9819 if (plane->cursor.base != base ||
9820 plane->cursor.size != size ||
9821 plane->cursor.cntl != cntl) {
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009822 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009823 I915_WRITE_FW(CURBASE(PIPE_A), base);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009824 I915_WRITE_FW(CURSIZE, size);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009825 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädd584fc2017-03-09 17:44:33 +02009826 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
Ville Syrjälä75343a42017-03-27 21:55:38 +03009827
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +03009828 plane->cursor.base = base;
9829 plane->cursor.size = size;
9830 plane->cursor.cntl = cntl;
9831 } else {
9832 I915_WRITE_FW(CURPOS(PIPE_A), pos);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009833 }
9834
Ville Syrjälä75343a42017-03-27 21:55:38 +03009835 POSTING_READ_FW(CURCNTR(PIPE_A));
Ville Syrjäläb2d03b02017-03-27 21:55:37 +03009836
9837 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9838}
9839
9840static void i845_disable_cursor(struct intel_plane *plane,
9841 struct intel_crtc *crtc)
9842{
9843 i845_update_cursor(plane, NULL, NULL);
Chris Wilson560b85b2010-08-07 11:01:38 +01009844}
9845
Ville Syrjäläeade6c82018-01-30 22:38:03 +02009846static bool i845_cursor_get_hw_state(struct intel_plane *plane,
9847 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009848{
9849 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9850 enum intel_display_power_domain power_domain;
9851 bool ret;
9852
9853 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
9854 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9855 return false;
9856
9857 ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
9858
Ville Syrjäläeade6c82018-01-30 22:38:03 +02009859 *pipe = PIPE_A;
9860
Ville Syrjälä51f5a0962017-11-17 21:19:08 +02009861 intel_display_power_put(dev_priv, power_domain);
9862
9863 return ret;
9864}
9865
Ville Syrjäläddd57132018-09-07 18:24:02 +03009866static unsigned int
9867i9xx_cursor_max_stride(struct intel_plane *plane,
9868 u32 pixel_format, u64 modifier,
9869 unsigned int rotation)
9870{
9871 return plane->base.dev->mode_config.cursor_width * 4;
9872}
9873
Ville Syrjälä292889e2017-03-17 23:18:01 +02009874static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9875 const struct intel_plane_state *plane_state)
9876{
9877 struct drm_i915_private *dev_priv =
9878 to_i915(plane_state->base.plane->dev);
9879 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
José Roberto de Souzac894d632018-05-18 13:15:47 -07009880 u32 cntl = 0;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009881
Ville Syrjäläe876b782018-01-30 22:38:05 +02009882 if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
9883 cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
9884
José Roberto de Souzac894d632018-05-18 13:15:47 -07009885 if (INTEL_GEN(dev_priv) <= 10) {
9886 cntl |= MCURSOR_GAMMA_ENABLE;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009887
José Roberto de Souzac894d632018-05-18 13:15:47 -07009888 if (HAS_DDI(dev_priv))
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009889 cntl |= MCURSOR_PIPE_CSC_ENABLE;
José Roberto de Souzac894d632018-05-18 13:15:47 -07009890 }
Ville Syrjälä292889e2017-03-17 23:18:01 +02009891
Ville Syrjälä32ea06b2018-01-30 22:38:01 +02009892 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
9893 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
Ville Syrjälä292889e2017-03-17 23:18:01 +02009894
9895 switch (plane_state->base.crtc_w) {
9896 case 64:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009897 cntl |= MCURSOR_MODE_64_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009898 break;
9899 case 128:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009900 cntl |= MCURSOR_MODE_128_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009901 break;
9902 case 256:
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009903 cntl |= MCURSOR_MODE_256_ARGB_AX;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009904 break;
9905 default:
9906 MISSING_CASE(plane_state->base.crtc_w);
9907 return 0;
9908 }
9909
Robert Fossc2c446a2017-05-19 16:50:17 -04009910 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +02009911 cntl |= MCURSOR_ROTATE_180;
Ville Syrjälä292889e2017-03-17 23:18:01 +02009912
9913 return cntl;
9914}
9915
Ville Syrjälä659056f2017-03-27 21:55:39 +03009916static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +01009917{
Ville Syrjälä024faac2017-03-27 21:55:42 +03009918 struct drm_i915_private *dev_priv =
9919 to_i915(plane_state->base.plane->dev);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009920 int width = plane_state->base.crtc_w;
9921 int height = plane_state->base.crtc_h;
Chris Wilson560b85b2010-08-07 11:01:38 +01009922
Ville Syrjälä3637ecf2017-03-27 21:55:40 +03009923 if (!intel_cursor_size_ok(plane_state))
Ville Syrjälädc41c152014-08-13 11:57:05 +03009924 return false;
9925
Ville Syrjälä024faac2017-03-27 21:55:42 +03009926 /* Cursor width is limited to a few power-of-two sizes */
9927 switch (width) {
Ville Syrjälä659056f2017-03-27 21:55:39 +03009928 case 256:
9929 case 128:
Ville Syrjälä659056f2017-03-27 21:55:39 +03009930 case 64:
9931 break;
9932 default:
9933 return false;
9934 }
9935
Ville Syrjälädc41c152014-08-13 11:57:05 +03009936 /*
Ville Syrjälä024faac2017-03-27 21:55:42 +03009937 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9938 * height from 8 lines up to the cursor width, when the
9939 * cursor is not rotated. Everything else requires square
9940 * cursors.
Ville Syrjälädc41c152014-08-13 11:57:05 +03009941 */
Ville Syrjälä024faac2017-03-27 21:55:42 +03009942 if (HAS_CUR_FBC(dev_priv) &&
Dave Airliea82256b2017-05-30 15:25:28 +10009943 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009944 if (height < 8 || height > width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009945 return false;
9946 } else {
Ville Syrjälä024faac2017-03-27 21:55:42 +03009947 if (height != width)
Ville Syrjälädc41c152014-08-13 11:57:05 +03009948 return false;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009949 }
9950
9951 return true;
9952}
9953
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009954static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
Ville Syrjälä659056f2017-03-27 21:55:39 +03009955 struct intel_plane_state *plane_state)
9956{
Ville Syrjäläeb0f5042018-08-28 17:27:06 +03009957 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä659056f2017-03-27 21:55:39 +03009958 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9959 const struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009960 enum pipe pipe = plane->pipe;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009961 int ret;
9962
9963 ret = intel_check_cursor(crtc_state, plane_state);
9964 if (ret)
9965 return ret;
9966
9967 /* if we want to turn off the cursor ignore width and height */
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009968 if (!fb)
Ville Syrjälä659056f2017-03-27 21:55:39 +03009969 return 0;
9970
9971 /* Check for which cursor types we support */
9972 if (!i9xx_cursor_size_ok(plane_state)) {
9973 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9974 plane_state->base.crtc_w,
9975 plane_state->base.crtc_h);
9976 return -EINVAL;
9977 }
9978
Ville Syrjälädf79cf42018-09-11 18:01:39 +03009979 WARN_ON(plane_state->base.visible &&
9980 plane_state->color_plane[0].stride != fb->pitches[0]);
9981
Ville Syrjälä1e1bb872017-03-27 21:55:41 +03009982 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9983 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9984 fb->pitches[0], plane_state->base.crtc_w);
9985 return -EINVAL;
Ville Syrjälä659056f2017-03-27 21:55:39 +03009986 }
9987
9988 /*
9989 * There's something wrong with the cursor on CHV pipe C.
9990 * If it straddles the left edge of the screen then
9991 * moving it away from the edge or disabling it often
9992 * results in a pipe underrun, and often that can lead to
9993 * dead pipe (constant underrun reported, and it scans
9994 * out just a solid color). To recover from that, the
9995 * display power well must be turned off and on again.
9996 * Refuse the put the cursor into that compromised position.
9997 */
9998 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9999 plane_state->base.visible && plane_state->base.crtc_x < 0) {
10000 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
10001 return -EINVAL;
10002 }
10003
10004 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
10005
10006 return 0;
10007}
10008
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010009static void i9xx_update_cursor(struct intel_plane *plane,
10010 const struct intel_crtc_state *crtc_state,
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010011 const struct intel_plane_state *plane_state)
10012{
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030010013 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10014 enum pipe pipe = plane->pipe;
Ville Syrjälä024faac2017-03-27 21:55:42 +030010015 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010016 unsigned long irqflags;
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010017
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010018 if (plane_state && plane_state->base.visible) {
Ville Syrjäläa0864d52017-03-23 21:27:09 +020010019 cntl = plane_state->ctl;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010020
Ville Syrjälä024faac2017-03-27 21:55:42 +030010021 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
10022 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
10023
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010024 base = intel_cursor_base(plane_state);
10025 pos = intel_cursor_position(plane_state);
10026 }
10027
10028 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
10029
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010030 /*
10031 * On some platforms writing CURCNTR first will also
10032 * cause CURPOS to be armed by the CURBASE write.
10033 * Without the CURCNTR write the CURPOS write would
Ville Syrjälä8753d2b2017-07-14 18:52:27 +030010034 * arm itself. Thus we always start the full update
10035 * with a CURCNTR write.
10036 *
10037 * On other platforms CURPOS always requires the
10038 * CURBASE write to arm the update. Additonally
10039 * a write to any of the cursor register will cancel
10040 * an already armed cursor update. Thus leaving out
10041 * the CURBASE write after CURPOS could lead to a
10042 * cursor that doesn't appear to move, or even change
10043 * shape. Thus we always write CURBASE.
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010044 *
10045 * CURCNTR and CUR_FBC_CTL are always
10046 * armed by the CURBASE write only.
10047 */
10048 if (plane->cursor.base != base ||
Ville Syrjälä024faac2017-03-27 21:55:42 +030010049 plane->cursor.size != fbc_ctl ||
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010050 plane->cursor.cntl != cntl) {
10051 I915_WRITE_FW(CURCNTR(pipe), cntl);
10052 if (HAS_CUR_FBC(dev_priv))
10053 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
10054 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä75343a42017-03-27 21:55:38 +030010055 I915_WRITE_FW(CURBASE(pipe), base);
10056
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010057 plane->cursor.base = base;
10058 plane->cursor.size = fbc_ctl;
10059 plane->cursor.cntl = cntl;
10060 } else {
10061 I915_WRITE_FW(CURPOS(pipe), pos);
Ville Syrjälä8753d2b2017-07-14 18:52:27 +030010062 I915_WRITE_FW(CURBASE(pipe), base);
Ville Syrjäläe11ffdd2017-03-27 21:55:46 +030010063 }
10064
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010065 POSTING_READ_FW(CURBASE(pipe));
10066
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010067 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010068}
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010069
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010070static void i9xx_disable_cursor(struct intel_plane *plane,
10071 struct intel_crtc *crtc)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010072{
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030010073 i9xx_update_cursor(plane, NULL, NULL);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010074}
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010075
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010076static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
10077 enum pipe *pipe)
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010078{
10079 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10080 enum intel_display_power_domain power_domain;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010081 bool ret;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010082 u32 val;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010083
10084 /*
10085 * Not 100% correct for planes that can move between pipes,
10086 * but that's only the case for gen2-3 which don't have any
10087 * display power wells.
10088 */
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010089 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010090 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10091 return false;
10092
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010093 val = I915_READ(CURCNTR(plane->pipe));
10094
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +020010095 ret = val & MCURSOR_MODE;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020010096
10097 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
10098 *pipe = plane->pipe;
10099 else
10100 *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
10101 MCURSOR_PIPE_SELECT_SHIFT;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020010102
10103 intel_display_power_put(dev_priv, power_domain);
10104
10105 return ret;
10106}
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010107
Jesse Barnes79e53942008-11-07 14:24:08 -080010108/* VESA 640x480x72Hz mode to set on the pipe */
Ville Syrjäläbacdcd52017-05-18 22:38:37 +030010109static const struct drm_display_mode load_detect_mode = {
Jesse Barnes79e53942008-11-07 14:24:08 -080010110 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10111 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10112};
10113
Daniel Vettera8bb6812014-02-10 18:00:39 +010010114struct drm_framebuffer *
Chris Wilson24dbf512017-02-15 10:59:18 +000010115intel_framebuffer_create(struct drm_i915_gem_object *obj,
10116 struct drm_mode_fb_cmd2 *mode_cmd)
Chris Wilsond2dff872011-04-19 08:36:26 +010010117{
10118 struct intel_framebuffer *intel_fb;
10119 int ret;
10120
10121 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010122 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010123 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010124
Chris Wilson24dbf512017-02-15 10:59:18 +000010125 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010126 if (ret)
10127 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010128
10129 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010130
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010131err:
10132 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010133 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010134}
10135
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010136static int intel_modeset_disable_planes(struct drm_atomic_state *state,
10137 struct drm_crtc *crtc)
Chris Wilsond2dff872011-04-19 08:36:26 +010010138{
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010139 struct drm_plane *plane;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010140 struct drm_plane_state *plane_state;
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010141 int ret, i;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010142
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010143 ret = drm_atomic_add_affected_planes(state, crtc);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010144 if (ret)
10145 return ret;
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010146
10147 for_each_new_plane_in_state(state, plane, plane_state, i) {
10148 if (plane_state->crtc != crtc)
10149 continue;
10150
10151 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
10152 if (ret)
10153 return ret;
10154
10155 drm_atomic_set_fb_for_plane(plane_state, NULL);
10156 }
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010157
10158 return 0;
10159}
10160
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010161int intel_get_load_detect_pipe(struct drm_connector *connector,
Ville Syrjäläbacdcd52017-05-18 22:38:37 +030010162 const struct drm_display_mode *mode,
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010163 struct intel_load_detect_pipe *old,
10164 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010165{
10166 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010167 struct intel_encoder *intel_encoder =
10168 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010169 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010170 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010171 struct drm_crtc *crtc = NULL;
10172 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020010173 struct drm_i915_private *dev_priv = to_i915(dev);
Rob Clark51fd3712013-11-19 12:10:12 -050010174 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010175 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010176 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010177 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010178 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010179
Chris Wilsond2dff872011-04-19 08:36:26 +010010180 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010181 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010182 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010183
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010184 old->restore_state = NULL;
10185
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010186 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010187
Jesse Barnes79e53942008-11-07 14:24:08 -080010188 /*
10189 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010190 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010191 * - if the connector already has an assigned crtc, use it (but make
10192 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010193 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010194 * - try to find the first unused crtc that can drive this connector,
10195 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010196 */
10197
10198 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010199 if (connector->state->crtc) {
10200 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010201
Rob Clark51fd3712013-11-19 12:10:12 -050010202 ret = drm_modeset_lock(&crtc->mutex, ctx);
10203 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010204 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010205
10206 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010207 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010208 }
10209
10210 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010211 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010212 i++;
10213 if (!(encoder->possible_crtcs & (1 << i)))
10214 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010215
10216 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10217 if (ret)
10218 goto fail;
10219
10220 if (possible_crtc->state->enable) {
10221 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010222 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010223 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010224
10225 crtc = possible_crtc;
10226 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010227 }
10228
10229 /*
10230 * If we didn't find an unused CRTC, don't use any.
10231 */
10232 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010233 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Dan Carpenterf4bf77b2017-04-14 22:54:25 +030010234 ret = -ENODEV;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010235 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010236 }
10237
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010238found:
10239 intel_crtc = to_intel_crtc(crtc);
10240
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010241 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010242 restore_state = drm_atomic_state_alloc(dev);
10243 if (!state || !restore_state) {
10244 ret = -ENOMEM;
10245 goto fail;
10246 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010247
10248 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010249 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010250
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010251 connector_state = drm_atomic_get_connector_state(state, connector);
10252 if (IS_ERR(connector_state)) {
10253 ret = PTR_ERR(connector_state);
10254 goto fail;
10255 }
10256
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010257 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10258 if (ret)
10259 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010260
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010261 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10262 if (IS_ERR(crtc_state)) {
10263 ret = PTR_ERR(crtc_state);
10264 goto fail;
10265 }
10266
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010267 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010268
Chris Wilson64927112011-04-20 07:25:26 +010010269 if (!mode)
10270 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010271
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010272 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010273 if (ret)
10274 goto fail;
10275
Ville Syrjälä20bdc112017-12-20 10:35:45 +010010276 ret = intel_modeset_disable_planes(state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010277 if (ret)
10278 goto fail;
10279
10280 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10281 if (!ret)
10282 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
Ville Syrjäläbe90cc32018-03-22 17:23:12 +020010283 if (!ret)
10284 ret = drm_atomic_add_affected_planes(restore_state, crtc);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010285 if (ret) {
10286 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10287 goto fail;
10288 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010289
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010290 ret = drm_atomic_commit(state);
10291 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010292 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010293 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010294 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010295
10296 old->restore_state = restore_state;
Chris Wilson7abbd112017-01-19 11:37:49 +000010297 drm_atomic_state_put(state);
Chris Wilson71731882011-04-19 23:10:58 +010010298
Jesse Barnes79e53942008-11-07 14:24:08 -080010299 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020010300 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010301 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010302
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010303fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010010304 if (state) {
10305 drm_atomic_state_put(state);
10306 state = NULL;
10307 }
10308 if (restore_state) {
10309 drm_atomic_state_put(restore_state);
10310 restore_state = NULL;
10311 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010312
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +020010313 if (ret == -EDEADLK)
10314 return ret;
Rob Clark51fd3712013-11-19 12:10:12 -050010315
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010316 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010317}
10318
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010319void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010320 struct intel_load_detect_pipe *old,
10321 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010322{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010323 struct intel_encoder *intel_encoder =
10324 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010325 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010326 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010327 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010328
Chris Wilsond2dff872011-04-19 08:36:26 +010010329 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010330 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010331 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010332
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010333 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010334 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010335
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010010336 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
Chris Wilson08536952016-10-14 13:18:18 +010010337 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010338 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010010339 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010340}
10341
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010342static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010343 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010344{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010345 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010346 u32 dpll = pipe_config->dpll_hw_state.dpll;
10347
10348 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010349 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010010350 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010351 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010352 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010353 return 96000;
10354 else
10355 return 48000;
10356}
10357
Jesse Barnes79e53942008-11-07 14:24:08 -080010358/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010359static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010360 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010361{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010362 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010363 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010364 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010365 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010366 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010367 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010368 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010369 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010370
10371 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010372 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010373 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010374 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010375
10376 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010377 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010378 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10379 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010380 } else {
10381 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10382 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10383 }
10384
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010010385 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010386 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010387 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10388 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010389 else
10390 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010391 DPLL_FPA01_P1_POST_DIV_SHIFT);
10392
10393 switch (dpll & DPLL_MODE_MASK) {
10394 case DPLLB_MODE_DAC_SERIAL:
10395 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10396 5 : 10;
10397 break;
10398 case DPLLB_MODE_LVDS:
10399 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10400 7 : 14;
10401 break;
10402 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010403 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010405 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010406 }
10407
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020010408 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +030010409 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010410 else
Imre Deakdccbea32015-06-22 23:35:51 +030010411 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010412 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010413 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010414 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010415
10416 if (is_lvds) {
10417 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10418 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010419
10420 if (lvds & LVDS_CLKB_POWER_UP)
10421 clock.p2 = 7;
10422 else
10423 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010424 } else {
10425 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10426 clock.p1 = 2;
10427 else {
10428 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10429 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10430 }
10431 if (dpll & PLL_P2_DIVIDE_BY_4)
10432 clock.p2 = 4;
10433 else
10434 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010435 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010436
Imre Deakdccbea32015-06-22 23:35:51 +030010437 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010438 }
10439
Ville Syrjälä18442d02013-09-13 16:00:08 +030010440 /*
10441 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010442 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010443 * encoder's get_config() function.
10444 */
Imre Deakdccbea32015-06-22 23:35:51 +030010445 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010446}
10447
Ville Syrjälä6878da02013-09-13 15:59:11 +030010448int intel_dotclock_calculate(int link_freq,
10449 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010450{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010451 /*
10452 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010453 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010454 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010455 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010456 *
10457 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010458 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010459 */
10460
Ville Syrjälä6878da02013-09-13 15:59:11 +030010461 if (!m_n->link_n)
10462 return 0;
10463
Chris Wilson31236982017-09-13 11:51:53 +010010464 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 +030010465}
10466
Ville Syrjälä18442d02013-09-13 16:00:08 +030010467static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010468 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010469{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010470 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010471
10472 /* read out port_clock from the DPLL */
10473 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010474
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010475 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010476 * In case there is an active pipe without active ports,
10477 * we may need some idea for the dotclock anyway.
10478 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010479 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010480 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010481 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010482 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010483}
10484
Ville Syrjäläde330812017-10-09 19:19:50 +030010485/* Returns the currently programmed mode of the given encoder. */
10486struct drm_display_mode *
10487intel_encoder_current_mode(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080010488{
Ville Syrjäläde330812017-10-09 19:19:50 +030010489 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
10490 struct intel_crtc_state *crtc_state;
Jesse Barnes79e53942008-11-07 14:24:08 -080010491 struct drm_display_mode *mode;
Ville Syrjäläde330812017-10-09 19:19:50 +030010492 struct intel_crtc *crtc;
10493 enum pipe pipe;
10494
10495 if (!encoder->get_hw_state(encoder, &pipe))
10496 return NULL;
10497
10498 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -080010499
10500 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10501 if (!mode)
10502 return NULL;
10503
Ville Syrjäläde330812017-10-09 19:19:50 +030010504 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
10505 if (!crtc_state) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010506 kfree(mode);
10507 return NULL;
10508 }
10509
Ville Syrjäläde330812017-10-09 19:19:50 +030010510 crtc_state->base.crtc = &crtc->base;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010511
Ville Syrjäläde330812017-10-09 19:19:50 +030010512 if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
10513 kfree(crtc_state);
10514 kfree(mode);
10515 return NULL;
10516 }
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010517
Ville Syrjäläde330812017-10-09 19:19:50 +030010518 encoder->get_config(encoder, crtc_state);
Ville Syrjäläe30a1542016-04-01 18:37:25 +030010519
Ville Syrjäläde330812017-10-09 19:19:50 +030010520 intel_mode_from_pipe_config(mode, crtc_state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010521
Ville Syrjäläde330812017-10-09 19:19:50 +030010522 kfree(crtc_state);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010523
Jesse Barnes79e53942008-11-07 14:24:08 -080010524 return mode;
10525}
10526
10527static void intel_crtc_destroy(struct drm_crtc *crtc)
10528{
10529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10530
10531 drm_crtc_cleanup(crtc);
10532 kfree(intel_crtc);
10533}
10534
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010535/**
10536 * intel_wm_need_update - Check whether watermarks need updating
10537 * @plane: drm plane
10538 * @state: new plane state
10539 *
10540 * Check current plane state versus the new one to determine whether
10541 * watermarks need to be recalculated.
10542 *
10543 * Returns true or false.
10544 */
10545static bool intel_wm_need_update(struct drm_plane *plane,
10546 struct drm_plane_state *state)
10547{
Matt Roperd21fbe82015-09-24 15:53:12 -070010548 struct intel_plane_state *new = to_intel_plane_state(state);
10549 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10550
10551 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010552 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010553 return true;
10554
10555 if (!cur->base.fb || !new->base.fb)
10556 return false;
10557
Ville Syrjäläbae781b2016-11-16 13:33:16 +020010558 if (cur->base.fb->modifier != new->base.fb->modifier ||
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010559 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010560 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10561 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10562 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10563 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010564 return true;
10565
10566 return false;
10567}
10568
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010569static bool needs_scaling(const struct intel_plane_state *state)
Matt Roperd21fbe82015-09-24 15:53:12 -070010570{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010571 int src_w = drm_rect_width(&state->base.src) >> 16;
10572 int src_h = drm_rect_height(&state->base.src) >> 16;
10573 int dst_w = drm_rect_width(&state->base.dst);
10574 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070010575
10576 return (src_w != dst_w || src_h != dst_h);
10577}
10578
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010579int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
10580 struct drm_crtc_state *crtc_state,
10581 const struct intel_plane_state *old_plane_state,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010582 struct drm_plane_state *plane_state)
10583{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010584 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010585 struct drm_crtc *crtc = crtc_state->crtc;
10586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010587 struct intel_plane *plane = to_intel_plane(plane_state->plane);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010588 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080010589 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010590 bool mode_changed = needs_modeset(crtc_state);
Ville Syrjäläb2b55502017-08-23 18:22:23 +030010591 bool was_crtc_enabled = old_crtc_state->base.active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010592 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010593 bool turn_off, turn_on, visible, was_visible;
10594 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030010595 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010596
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010597 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010598 ret = skl_update_scaler_plane(
10599 to_intel_crtc_state(crtc_state),
10600 to_intel_plane_state(plane_state));
10601 if (ret)
10602 return ret;
10603 }
10604
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010605 was_visible = old_plane_state->base.visible;
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010606 visible = plane_state->visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010607
10608 if (!was_crtc_enabled && WARN_ON(was_visible))
10609 was_visible = false;
10610
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010611 /*
10612 * Visibility is calculated as if the crtc was on, but
10613 * after scaler setup everything depends on it being off
10614 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030010615 *
10616 * FIXME this is wrong for watermarks. Watermarks should also
10617 * be computed as if the pipe would be active. Perhaps move
10618 * per-plane wm computation to the .check_plane() hook, and
10619 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010010620 */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010621 if (!is_crtc_enabled) {
Maarten Lankhorst1d4258d2017-01-12 10:43:45 +010010622 plane_state->visible = visible = false;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010623 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10624 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010625
10626 if (!was_visible && !visible)
10627 return 0;
10628
Maarten Lankhorste8861672016-02-24 11:24:26 +010010629 if (fb != old_plane_state->base.fb)
10630 pipe_config->fb_changed = true;
10631
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010632 turn_off = was_visible && (!visible || mode_changed);
10633 turn_on = visible && (!was_visible || mode_changed);
10634
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010635 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010636 intel_crtc->base.base.id, intel_crtc->base.name,
10637 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010638 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010639
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010640 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 +020010641 plane->base.base.id, plane->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030010642 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010643 turn_off, turn_on, mode_changed);
10644
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010645 if (turn_on) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010646 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010647 pipe_config->update_wm_pre = true;
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010648
10649 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010650 if (plane->id != PLANE_CURSOR)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010651 pipe_config->disable_cxsr = true;
10652 } else if (turn_off) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010653 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010654 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010010655
Ville Syrjälä852eb002015-06-24 22:00:07 +030010656 /* must disable cxsr around plane enable/disable */
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010657 if (plane->id != PLANE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010010658 pipe_config->disable_cxsr = true;
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010659 } else if (intel_wm_need_update(&plane->base, plane_state)) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +030010660 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjäläb4ede6d2017-03-02 19:15:01 +020010661 /* FIXME bollocks */
10662 pipe_config->update_wm_pre = true;
10663 pipe_config->update_wm_post = true;
10664 }
Ville Syrjälä852eb002015-06-24 22:00:07 +030010665 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010666
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070010667 if (visible || was_visible)
Ville Syrjäläe9728bd2017-03-02 19:14:51 +020010668 pipe_config->fb_bits |= plane->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010669
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010670 /*
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010671 * ILK/SNB DVSACNTR/Sprite Enable
10672 * IVB SPR_CTL/Sprite Enable
10673 * "When in Self Refresh Big FIFO mode, a write to enable the
10674 * plane will be internally buffered and delayed while Big FIFO
10675 * mode is exiting."
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010676 *
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010677 * Which means that enabling the sprite can take an extra frame
10678 * when we start in big FIFO mode (LP1+). Thus we need to drop
10679 * down to LP0 and wait for vblank in order to make sure the
10680 * sprite gets enabled on the next vblank after the register write.
10681 * Doing otherwise would risk enabling the sprite one frame after
10682 * we've already signalled flip completion. We can resume LP1+
10683 * once the sprite has been enabled.
10684 *
10685 *
10686 * WaCxSRDisabledForSpriteScaling:ivb
10687 * IVB SPR_SCALE/Scaling Enable
10688 * "Low Power watermarks must be disabled for at least one
10689 * frame before enabling sprite scaling, and kept disabled
10690 * until sprite scaling is disabled."
10691 *
10692 * ILK/SNB DVSASCALE/Scaling Enable
10693 * "When in Self Refresh Big FIFO mode, scaling enable will be
10694 * masked off while Big FIFO mode is exiting."
10695 *
10696 * Despite the w/a only being listed for IVB we assume that
10697 * the ILK/SNB note has similar ramifications, hence we apply
10698 * the w/a on all three platforms.
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010699 */
Ville Syrjälä8e7a4422018-10-04 15:15:27 +030010700 if (plane->id == PLANE_SPRITE0 &&
10701 (IS_GEN5(dev_priv) || IS_GEN6(dev_priv) ||
10702 IS_IVYBRIDGE(dev_priv)) &&
10703 (turn_on || (!needs_scaling(old_plane_state) &&
10704 needs_scaling(to_intel_plane_state(plane_state)))))
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010010705 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010706
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020010707 return 0;
10708}
10709
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010710static bool encoders_cloneable(const struct intel_encoder *a,
10711 const struct intel_encoder *b)
10712{
10713 /* masks could be asymmetric, so check both ways */
10714 return a == b || (a->cloneable & (1 << b->type) &&
10715 b->cloneable & (1 << a->type));
10716}
10717
10718static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10719 struct intel_crtc *crtc,
10720 struct intel_encoder *encoder)
10721{
10722 struct intel_encoder *source_encoder;
10723 struct drm_connector *connector;
10724 struct drm_connector_state *connector_state;
10725 int i;
10726
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010727 for_each_new_connector_in_state(state, connector, connector_state, i) {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010728 if (connector_state->crtc != &crtc->base)
10729 continue;
10730
10731 source_encoder =
10732 to_intel_encoder(connector_state->best_encoder);
10733 if (!encoders_cloneable(encoder, source_encoder))
10734 return false;
10735 }
10736
10737 return true;
10738}
10739
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010740static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10741 struct drm_crtc_state *crtc_state)
10742{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010743 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010744 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020010746 struct intel_crtc_state *pipe_config =
10747 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010748 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020010749 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010750 bool mode_changed = needs_modeset(crtc_state);
10751
Ville Syrjälä852eb002015-06-24 22:00:07 +030010752 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020010753 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020010754
Maarten Lankhorstad421372015-06-15 12:33:42 +020010755 if (mode_changed && crtc_state->enable &&
10756 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010757 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020010758 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10759 pipe_config);
10760 if (ret)
10761 return ret;
10762 }
10763
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010764 if (crtc_state->color_mgmt_changed) {
10765 ret = intel_color_check(crtc, crtc_state);
10766 if (ret)
10767 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010010768
10769 /*
10770 * Changing color management on Intel hardware is
10771 * handled as part of planes update.
10772 */
10773 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000010774 }
10775
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010776 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010777 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010010778 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080010779 if (ret) {
10780 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070010781 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080010782 }
10783 }
10784
10785 if (dev_priv->display.compute_intermediate_wm &&
10786 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10787 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10788 return 0;
10789
10790 /*
10791 * Calculate 'intermediate' watermarks that satisfy both the
10792 * old state and the new state. We can program these
10793 * immediately.
10794 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010795 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080010796 intel_crtc,
10797 pipe_config);
10798 if (ret) {
10799 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
10800 return ret;
10801 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070010802 } else if (dev_priv->display.compute_intermediate_wm) {
10803 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10804 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070010805 }
10806
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010807 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010808 if (mode_changed)
10809 ret = skl_update_scaler_crtc(pipe_config);
10810
10811 if (!ret)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +053010812 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
10813 pipe_config);
10814 if (!ret)
Ander Conselvan de Oliveira6ebc6922017-02-23 09:15:59 +020010815 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010816 pipe_config);
10817 }
10818
Maarten Lankhorst24f28452017-11-22 19:39:01 +010010819 if (HAS_IPS(dev_priv))
10820 pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
10821
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020010822 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010823}
10824
Jani Nikula65b38e02015-04-13 11:26:56 +030010825static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020010826 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010827};
10828
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010829static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10830{
10831 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010832 struct drm_connector_list_iter conn_iter;
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010833
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010834 drm_connector_list_iter_begin(dev, &conn_iter);
10835 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020010836 if (connector->base.state->crtc)
Thomas Zimmermannef196b52018-06-18 13:01:50 +020010837 drm_connector_put(&connector->base);
Daniel Vetter8863dc72016-05-06 15:39:03 +020010838
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010839 if (connector->base.encoder) {
10840 connector->base.state->best_encoder =
10841 connector->base.encoder;
10842 connector->base.state->crtc =
10843 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020010844
Thomas Zimmermannef196b52018-06-18 13:01:50 +020010845 drm_connector_get(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010846 } else {
10847 connector->base.state->best_encoder = NULL;
10848 connector->base.state->crtc = NULL;
10849 }
10850 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010010851 drm_connector_list_iter_end(&conn_iter);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010852}
10853
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010854static void
Robin Schroereba905b2014-05-18 02:24:50 +020010855connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010856 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010857{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010858 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010859 int bpp = pipe_config->pipe_bpp;
10860
10861 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010862 connector->base.base.id,
10863 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010864
10865 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010866 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010867 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010868 bpp, info->bpc * 3);
10869 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010870 }
10871
Mario Kleiner196f9542016-07-06 12:05:45 +020010872 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030010873 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020010874 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10875 bpp);
10876 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010877 }
10878}
10879
10880static int
10881compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010882 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010883{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010884 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010885 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010886 struct drm_connector *connector;
10887 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010888 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010889
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010890 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
10891 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010892 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010010893 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020010894 bpp = 12*3;
10895 else
10896 bpp = 8*3;
10897
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010898
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010899 pipe_config->pipe_bpp = bpp;
10900
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010901 state = pipe_config->base.state;
10902
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010903 /* Clamp display bpp to EDID value */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010010904 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010905 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020010906 continue;
10907
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030010908 connected_sink_compute_bpp(to_intel_connector(connector),
10909 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010910 }
10911
10912 return bpp;
10913}
10914
Daniel Vetter644db712013-09-19 14:53:58 +020010915static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10916{
10917 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10918 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010919 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010920 mode->crtc_hdisplay, mode->crtc_hsync_start,
10921 mode->crtc_hsync_end, mode->crtc_htotal,
10922 mode->crtc_vdisplay, mode->crtc_vsync_start,
10923 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10924}
10925
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010926static inline void
10927intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000010928 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010929{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000010930 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10931 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000010932 m_n->gmch_m, m_n->gmch_n,
10933 m_n->link_m, m_n->link_n, m_n->tu);
10934}
10935
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010936#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
10937
10938static const char * const output_type_str[] = {
10939 OUTPUT_TYPE(UNUSED),
10940 OUTPUT_TYPE(ANALOG),
10941 OUTPUT_TYPE(DVO),
10942 OUTPUT_TYPE(SDVO),
10943 OUTPUT_TYPE(LVDS),
10944 OUTPUT_TYPE(TVOUT),
10945 OUTPUT_TYPE(HDMI),
10946 OUTPUT_TYPE(DP),
10947 OUTPUT_TYPE(EDP),
10948 OUTPUT_TYPE(DSI),
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030010949 OUTPUT_TYPE(DDI),
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010950 OUTPUT_TYPE(DP_MST),
10951};
10952
10953#undef OUTPUT_TYPE
10954
10955static void snprintf_output_types(char *buf, size_t len,
10956 unsigned int output_types)
10957{
10958 char *str = buf;
10959 int i;
10960
10961 str[0] = '\0';
10962
10963 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
10964 int r;
10965
10966 if ((output_types & BIT(i)) == 0)
10967 continue;
10968
10969 r = snprintf(str, len, "%s%s",
10970 str != buf ? "," : "", output_type_str[i]);
10971 if (r >= len)
10972 break;
10973 str += r;
10974 len -= r;
10975
10976 output_types &= ~BIT(i);
10977 }
10978
10979 WARN_ON_ONCE(output_types != 0);
10980}
10981
Daniel Vetterc0b03412013-05-28 12:05:54 +020010982static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010983 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020010984 const char *context)
10985{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010986 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010987 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010988 struct drm_plane *plane;
10989 struct intel_plane *intel_plane;
10990 struct intel_plane_state *state;
10991 struct drm_framebuffer *fb;
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010992 char buf[64];
Chandra Konduru6a60cd82015-04-07 15:28:40 -070010993
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000010994 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
10995 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010996
Ville Syrjälä40b2be42017-10-10 15:11:59 +030010997 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
10998 DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
10999 buf, pipe_config->output_types);
11000
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011001 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11002 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020011003 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011004
11005 if (pipe_config->has_pch_encoder)
11006 intel_dump_m_n_config(pipe_config, "fdi",
11007 pipe_config->fdi_lanes,
11008 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011009
Shashank Sharmab22ca992017-07-24 19:19:32 +053011010 if (pipe_config->ycbcr420)
11011 DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
11012
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011013 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000011014 intel_dump_m_n_config(pipe_config, "dp m_n",
11015 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000011016 if (pipe_config->has_drrs)
11017 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11018 pipe_config->lane_count,
11019 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000011020 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011021
Daniel Vetter55072d12014-11-20 16:10:28 +010011022 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011023 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010011024
Daniel Vetterc0b03412013-05-28 12:05:54 +020011025 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011026 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011027 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011028 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11029 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011030 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011031 pipe_config->port_clock,
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011032 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11033 pipe_config->pixel_rate);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011034
11035 if (INTEL_GEN(dev_priv) >= 9)
11036 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11037 crtc->num_scalers,
11038 pipe_config->scaler_state.scaler_users,
11039 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011040
11041 if (HAS_GMCH_DISPLAY(dev_priv))
11042 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11043 pipe_config->gmch_pfit.control,
11044 pipe_config->gmch_pfit.pgm_ratios,
11045 pipe_config->gmch_pfit.lvds_border_bits);
11046 else
11047 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11048 pipe_config->pch_pfit.pos,
11049 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000011050 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000011051
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000011052 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11053 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011054
Ander Conselvan de Oliveiraf50b79f2016-12-29 17:22:12 +020011055 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011056
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011057 DRM_DEBUG_KMS("planes on this crtc\n");
11058 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011059 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011060 intel_plane = to_intel_plane(plane);
11061 if (intel_plane->pipe != crtc->pipe)
11062 continue;
11063
11064 state = to_intel_plane_state(plane->state);
11065 fb = state->base.fb;
11066 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030011067 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11068 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011069 continue;
11070 }
11071
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011072 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11073 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000011074 fb->base.id, fb->width, fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +020011075 drm_get_format_name(fb->format->format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000011076 if (INTEL_GEN(dev_priv) >= 9)
11077 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11078 state->scaler_id,
11079 state->base.src.x1 >> 16,
11080 state->base.src.y1 >> 16,
11081 drm_rect_width(&state->base.src) >> 16,
11082 drm_rect_height(&state->base.src) >> 16,
11083 state->base.dst.x1, state->base.dst.y1,
11084 drm_rect_width(&state->base.dst),
11085 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011086 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011087}
11088
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011089static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011090{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011091 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011092 struct drm_connector *connector;
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011093 struct drm_connector_list_iter conn_iter;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011094 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011095 unsigned int used_mst_ports = 0;
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011096 bool ret = true;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011097
11098 /*
11099 * Walk the connector list instead of the encoder
11100 * list to detect the problem on ddi platforms
11101 * where there's just one encoder per digital port.
11102 */
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011103 drm_connector_list_iter_begin(dev, &conn_iter);
11104 drm_for_each_connector_iter(connector, &conn_iter) {
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011105 struct drm_connector_state *connector_state;
11106 struct intel_encoder *encoder;
11107
Maarten Lankhorst8b694492018-04-09 14:46:55 +020011108 connector_state = drm_atomic_get_new_connector_state(state, connector);
Ville Syrjälä0bff4852015-12-10 18:22:31 +020011109 if (!connector_state)
11110 connector_state = connector->state;
11111
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011112 if (!connector_state->best_encoder)
11113 continue;
11114
11115 encoder = to_intel_encoder(connector_state->best_encoder);
11116
11117 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011118
11119 switch (encoder->type) {
11120 unsigned int port_mask;
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011121 case INTEL_OUTPUT_DDI:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010011122 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011123 break;
Gustavo A. R. Silvaf0d759f2018-06-28 17:35:41 -050011124 /* else: fall through */
Ville Syrjäläcca05022016-06-22 21:57:06 +030011125 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011126 case INTEL_OUTPUT_HDMI:
11127 case INTEL_OUTPUT_EDP:
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020011128 port_mask = 1 << encoder->port;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011129
11130 /* the same port mustn't appear more than once */
11131 if (used_ports & port_mask)
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011132 ret = false;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011133
11134 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011135 break;
11136 case INTEL_OUTPUT_DP_MST:
11137 used_mst_ports |=
Ville Syrjälä8f4f2792017-11-09 17:24:34 +020011138 1 << encoder->port;
Ville Syrjälä477321e2016-07-28 17:50:40 +030011139 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011140 default:
11141 break;
11142 }
11143 }
Gustavo Padovan2fd96b42017-05-11 16:10:44 -030011144 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011145
Ville Syrjälä477321e2016-07-28 17:50:40 +030011146 /* can't mix MST and SST/HDMI on the same port */
11147 if (used_ports & used_mst_ports)
11148 return false;
11149
Maarten Lankhorstbd67a8c2018-02-15 10:14:25 +010011150 return ret;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011151}
11152
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011153static void
11154clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11155{
Ville Syrjäläff32c542017-03-02 19:14:57 +020011156 struct drm_i915_private *dev_priv =
11157 to_i915(crtc_state->base.crtc->dev);
Chandra Konduru663a3642015-04-07 15:28:41 -070011158 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011159 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011160 struct intel_shared_dpll *shared_dpll;
Ville Syrjäläff32c542017-03-02 19:14:57 +020011161 struct intel_crtc_wm_state wm_state;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011162 bool force_thru, ips_force_disable;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011163
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011164 /* FIXME: before the switch to atomic started, a new pipe_config was
11165 * kzalloc'd. Code that depends on any field being zero should be
11166 * fixed, so that the crtc_state can be safely duplicated. For now,
11167 * only fields that are know to not cause problems are preserved. */
11168
Chandra Konduru663a3642015-04-07 15:28:41 -070011169 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011170 shared_dpll = crtc_state->shared_dpll;
11171 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011172 force_thru = crtc_state->pch_pfit.force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011173 ips_force_disable = crtc_state->ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011174 if (IS_G4X(dev_priv) ||
11175 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011176 wm_state = crtc_state->wm;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011177
Chris Wilsond2fa80a2017-03-03 15:46:44 +000011178 /* Keep base drm_crtc_state intact, only clear our extended struct */
11179 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11180 memset(&crtc_state->base + 1, 0,
11181 sizeof(*crtc_state) - sizeof(crtc_state->base));
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011182
Chandra Konduru663a3642015-04-07 15:28:41 -070011183 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011184 crtc_state->shared_dpll = shared_dpll;
11185 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020011186 crtc_state->pch_pfit.force_thru = force_thru;
Ville Syrjälä6e644622017-08-17 17:55:09 +030011187 crtc_state->ips_force_disable = ips_force_disable;
Ville Syrjälä04548cb2017-04-21 21:14:29 +030011188 if (IS_G4X(dev_priv) ||
11189 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjäläff32c542017-03-02 19:14:57 +020011190 crtc_state->wm = wm_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011191}
11192
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011193static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011194intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011195 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011196{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020011197 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020011198 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011199 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011200 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011201 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011202 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011203 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011204
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011205 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011206
Daniel Vettere143a212013-07-04 12:01:15 +020011207 pipe_config->cpu_transcoder =
11208 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011209
Imre Deak2960bc92013-07-30 13:36:32 +030011210 /*
11211 * Sanitize sync polarity flags based on requested ones. If neither
11212 * positive or negative polarity is requested, treat this as meaning
11213 * negative polarity.
11214 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011215 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011216 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011217 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011218
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011219 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011220 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011221 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011222
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011223 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11224 pipe_config);
11225 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011226 goto fail;
11227
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011228 /*
11229 * Determine the real pipe dimensions. Note that stereo modes can
11230 * increase the actual pipe size due to the frame doubling and
11231 * insertion of additional space for blanks between the frame. This
11232 * is stored in the crtc timings. We use the requested mode to do this
11233 * computation to clearly distinguish it from the adjusted mode, which
11234 * can be changed by the connectors in the below retry loop.
11235 */
Daniel Vetter196cd5d2017-01-25 07:26:56 +010011236 drm_mode_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011237 &pipe_config->pipe_src_w,
11238 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011239
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011240 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011241 if (connector_state->crtc != crtc)
11242 continue;
11243
11244 encoder = to_intel_encoder(connector_state->best_encoder);
11245
Ville Syrjäläe25148d2016-06-22 21:57:09 +030011246 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11247 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11248 goto fail;
11249 }
11250
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011251 /*
11252 * Determine output_types before calling the .compute_config()
11253 * hooks so that the hooks can use this information safely.
11254 */
Ville Syrjälä7e732ca2017-10-27 22:31:24 +030011255 if (encoder->compute_output_type)
11256 pipe_config->output_types |=
11257 BIT(encoder->compute_output_type(encoder, pipe_config,
11258 connector_state));
11259 else
11260 pipe_config->output_types |= BIT(encoder->type);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011261 }
11262
Daniel Vettere29c22c2013-02-21 00:00:16 +010011263encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011264 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011265 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011266 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011267
Daniel Vetter135c81b2013-07-21 21:37:09 +020011268 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011269 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11270 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011271
Daniel Vetter7758a112012-07-08 19:40:39 +020011272 /* Pass our mode to the connectors and the CRTC to give them a chance to
11273 * adjust it according to limitations or connector properties, and also
11274 * a chance to reject the mode entirely.
11275 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011276 for_each_new_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011277 if (connector_state->crtc != crtc)
11278 continue;
11279
11280 encoder = to_intel_encoder(connector_state->best_encoder);
11281
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020011282 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020011283 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011284 goto fail;
11285 }
11286 }
11287
Daniel Vetterff9a6752013-06-01 17:16:21 +020011288 /* Set default port clock if not overwritten by the encoder. Needs to be
11289 * done afterwards in case the encoder adjusts the mode. */
11290 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011291 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011292 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011293
Daniel Vettera43f6e02013-06-07 23:10:32 +020011294 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011295 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011296 DRM_DEBUG_KMS("CRTC fixup failed\n");
11297 goto fail;
11298 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011299
11300 if (ret == RETRY) {
11301 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11302 ret = -EINVAL;
11303 goto fail;
11304 }
11305
11306 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11307 retry = false;
11308 goto encoder_retry;
11309 }
11310
Daniel Vettere8fa4272015-08-12 11:43:34 +020011311 /* Dithering seems to not pass-through bits correctly when it should, so
Manasi Navare611032b2017-01-24 08:21:49 -080011312 * only enable it on 6bpc panels and when its not a compliance
11313 * test requesting 6bpc video pattern.
11314 */
11315 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11316 !pipe_config->dither_force_disable;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020011317 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011318 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011319
Daniel Vetter7758a112012-07-08 19:40:39 +020011320fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011321 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011322}
11323
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011324static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011325{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011326 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011327
11328 if (clock1 == clock2)
11329 return true;
11330
11331 if (!clock1 || !clock2)
11332 return false;
11333
11334 diff = abs(clock1 - clock2);
11335
11336 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11337 return true;
11338
11339 return false;
11340}
11341
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011342static bool
11343intel_compare_m_n(unsigned int m, unsigned int n,
11344 unsigned int m2, unsigned int n2,
11345 bool exact)
11346{
11347 if (m == m2 && n == n2)
11348 return true;
11349
11350 if (exact || !m || !n || !m2 || !n2)
11351 return false;
11352
11353 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11354
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011355 if (n > n2) {
11356 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011357 m2 <<= 1;
11358 n2 <<= 1;
11359 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011360 } else if (n < n2) {
11361 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011362 m <<= 1;
11363 n <<= 1;
11364 }
11365 }
11366
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010011367 if (n != n2)
11368 return false;
11369
11370 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011371}
11372
11373static bool
11374intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11375 struct intel_link_m_n *m2_n2,
11376 bool adjust)
11377{
11378 if (m_n->tu == m2_n2->tu &&
11379 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11380 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11381 intel_compare_m_n(m_n->link_m, m_n->link_n,
11382 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11383 if (adjust)
11384 *m2_n2 = *m_n;
11385
11386 return true;
11387 }
11388
11389 return false;
11390}
11391
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011392static void __printf(3, 4)
11393pipe_config_err(bool adjust, const char *name, const char *format, ...)
11394{
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011395 struct va_format vaf;
11396 va_list args;
11397
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011398 va_start(args, format);
11399 vaf.fmt = format;
11400 vaf.va = &args;
11401
Joe Perches99a95482018-03-13 15:02:15 -070011402 if (adjust)
11403 drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
11404 else
11405 drm_err("mismatch in %s %pV", name, &vaf);
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011406
11407 va_end(args);
11408}
11409
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011410static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011411intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011412 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011413 struct intel_crtc_state *pipe_config,
11414 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011415{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011416 bool ret = true;
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011417 bool fixup_inherited = adjust &&
11418 (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
11419 !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011420
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011421#define PIPE_CONF_CHECK_X(name) do { \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011422 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011423 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter66e985c2013-06-05 13:34:20 +020011424 "(expected 0x%08x, found 0x%08x)\n", \
11425 current_config->name, \
11426 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011427 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011428 } \
11429} while (0)
Daniel Vetter66e985c2013-06-05 13:34:20 +020011430
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011431#define PIPE_CONF_CHECK_I(name) do { \
Daniel Vetter08a24032013-04-19 11:25:34 +020011432 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011433 pipe_config_err(adjust, __stringify(name), \
Daniel Vetter08a24032013-04-19 11:25:34 +020011434 "(expected %i, found %i)\n", \
11435 current_config->name, \
11436 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011437 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011438 } \
11439} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011440
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011441#define PIPE_CONF_CHECK_BOOL(name) do { \
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011442 if (current_config->name != pipe_config->name) { \
11443 pipe_config_err(adjust, __stringify(name), \
11444 "(expected %s, found %s)\n", \
11445 yesno(current_config->name), \
11446 yesno(pipe_config->name)); \
11447 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011448 } \
11449} while (0)
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011450
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011451/*
11452 * Checks state where we only read out the enabling, but not the entire
11453 * state itself (like full infoframes or ELD for audio). These states
11454 * require a full modeset on bootup to fix up.
11455 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011456#define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011457 if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
11458 PIPE_CONF_CHECK_BOOL(name); \
11459 } else { \
11460 pipe_config_err(adjust, __stringify(name), \
11461 "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
11462 yesno(current_config->name), \
11463 yesno(pipe_config->name)); \
11464 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011465 } \
11466} while (0)
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011467
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011468#define PIPE_CONF_CHECK_P(name) do { \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011469 if (current_config->name != pipe_config->name) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011470 pipe_config_err(adjust, __stringify(name), \
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011471 "(expected %p, found %p)\n", \
11472 current_config->name, \
11473 pipe_config->name); \
11474 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011475 } \
11476} while (0)
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011477
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011478#define PIPE_CONF_CHECK_M_N(name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011479 if (!intel_compare_link_m_n(&current_config->name, \
11480 &pipe_config->name,\
11481 adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011482 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011483 "(expected tu %i gmch %i/%i link %i/%i, " \
11484 "found tu %i, gmch %i/%i link %i/%i)\n", \
11485 current_config->name.tu, \
11486 current_config->name.gmch_m, \
11487 current_config->name.gmch_n, \
11488 current_config->name.link_m, \
11489 current_config->name.link_n, \
11490 pipe_config->name.tu, \
11491 pipe_config->name.gmch_m, \
11492 pipe_config->name.gmch_n, \
11493 pipe_config->name.link_m, \
11494 pipe_config->name.link_n); \
11495 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011496 } \
11497} while (0)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011498
Daniel Vetter55c561a2016-03-30 11:34:36 +020011499/* This is required for BDW+ where there is only one set of registers for
11500 * switching between high and low RR.
11501 * This macro can be used whenever a comparison has to be made between one
11502 * hw state and multiple sw state variables.
11503 */
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011504#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011505 if (!intel_compare_link_m_n(&current_config->name, \
11506 &pipe_config->name, adjust) && \
11507 !intel_compare_link_m_n(&current_config->alt_name, \
11508 &pipe_config->name, adjust)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011509 pipe_config_err(adjust, __stringify(name), \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011510 "(expected tu %i gmch %i/%i link %i/%i, " \
11511 "or tu %i gmch %i/%i link %i/%i, " \
11512 "found tu %i, gmch %i/%i link %i/%i)\n", \
11513 current_config->name.tu, \
11514 current_config->name.gmch_m, \
11515 current_config->name.gmch_n, \
11516 current_config->name.link_m, \
11517 current_config->name.link_n, \
11518 current_config->alt_name.tu, \
11519 current_config->alt_name.gmch_m, \
11520 current_config->alt_name.gmch_n, \
11521 current_config->alt_name.link_m, \
11522 current_config->alt_name.link_n, \
11523 pipe_config->name.tu, \
11524 pipe_config->name.gmch_m, \
11525 pipe_config->name.gmch_n, \
11526 pipe_config->name.link_m, \
11527 pipe_config->name.link_n); \
11528 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011529 } \
11530} while (0)
Daniel Vetter88adfff2013-03-28 10:42:01 +010011531
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011532#define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011533 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011534 pipe_config_err(adjust, __stringify(name), \
11535 "(%x) (expected %i, found %i)\n", \
11536 (mask), \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011537 current_config->name & (mask), \
11538 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011539 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011540 } \
11541} while (0)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011542
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011543#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011544 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Tvrtko Ursulin4e8048f2016-12-06 10:50:20 +000011545 pipe_config_err(adjust, __stringify(name), \
Ville Syrjälä5e550652013-09-06 23:29:07 +030011546 "(expected %i, found %i)\n", \
11547 current_config->name, \
11548 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011549 ret = false; \
Ville Syrjäläeadd2722018-03-16 20:36:25 +020011550 } \
11551} while (0)
Ville Syrjälä5e550652013-09-06 23:29:07 +030011552
Daniel Vetterbb760062013-06-06 14:55:52 +020011553#define PIPE_CONF_QUIRK(quirk) \
11554 ((current_config->quirks | pipe_config->quirks) & (quirk))
11555
Daniel Vettereccb1402013-05-22 00:50:22 +020011556 PIPE_CONF_CHECK_I(cpu_transcoder);
11557
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011558 PIPE_CONF_CHECK_BOOL(has_pch_encoder);
Daniel Vetter08a24032013-04-19 11:25:34 +020011559 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011560 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020011561
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011562 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030011563 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011564
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011565 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011566 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011567
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011568 if (current_config->has_drrs)
11569 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11570 } else
11571 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011572
Ville Syrjälä253c84c2016-06-22 21:57:01 +030011573 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020011574
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011575 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11576 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11577 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11578 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11579 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11580 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011581
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011582 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11583 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11584 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11585 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11586 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11587 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011588
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011589 PIPE_CONF_CHECK_I(pixel_multiplier);
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011590 PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010011591 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010011592 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011593 PIPE_CONF_CHECK_BOOL(limited_color_range);
Shashank Sharma15953632017-03-13 16:54:03 +053011594
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011595 PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
11596 PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011597 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011598 PIPE_CONF_CHECK_BOOL(ycbcr420);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011599
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011600 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011601
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011602 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011603 DRM_MODE_FLAG_INTERLACE);
11604
Daniel Vetterbb760062013-06-06 14:55:52 +020011605 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011606 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011607 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011608 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011609 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011610 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011611 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011612 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011613 DRM_MODE_FLAG_NVSYNC);
11614 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011615
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011616 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020011617 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011618 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020011619 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030011620 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020011621
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011622 if (!adjust) {
11623 PIPE_CONF_CHECK_I(pipe_src_w);
11624 PIPE_CONF_CHECK_I(pipe_src_h);
11625
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011626 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020011627 if (current_config->pch_pfit.enabled) {
11628 PIPE_CONF_CHECK_X(pch_pfit.pos);
11629 PIPE_CONF_CHECK_X(pch_pfit.size);
11630 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011631
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011632 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011633 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020011634 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070011635
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011636 PIPE_CONF_CHECK_BOOL(double_wide);
Ville Syrjälä282740f2013-09-04 18:30:03 +030011637
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011638 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011639 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011640 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011641 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11642 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011643 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010011644 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011645 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11646 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11647 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Paulo Zanoni2de38132017-09-22 17:53:42 -030011648 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
11649 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
11650 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
11651 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
11652 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
11653 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
11654 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
11655 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
11656 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
11657 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
11658 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
11659 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
Paulo Zanonic27e9172018-04-27 16:14:36 -070011660 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
11661 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
11662 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
11663 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
11664 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
11665 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
11666 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
11667 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
11668 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
11669 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011670
Ville Syrjälä47eacba2016-04-12 22:14:35 +030011671 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11672 PIPE_CONF_CHECK_X(dsi_pll.div);
11673
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011674 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011675 PIPE_CONF_CHECK_I(pipe_bpp);
11676
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011677 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011678 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011679
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030011680 PIPE_CONF_CHECK_I(min_voltage_level);
11681
Daniel Vetter66e985c2013-06-05 13:34:20 +020011682#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011683#undef PIPE_CONF_CHECK_I
Maarten Lankhorstd640bf72017-11-10 12:34:55 +010011684#undef PIPE_CONF_CHECK_BOOL
Maarten Lankhorst4493e092017-11-10 12:34:56 +010011685#undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011686#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011687#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011688#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011689#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011690
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011691 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011692}
11693
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011694static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11695 const struct intel_crtc_state *pipe_config)
11696{
11697 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011698 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011699 &pipe_config->fdi_m_n);
11700 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11701
11702 /*
11703 * FDI already provided one idea for the dotclock.
11704 * Yell if the encoder disagrees.
11705 */
11706 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11707 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11708 fdi_dotclock, dotclock);
11709 }
11710}
11711
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011712static void verify_wm_state(struct drm_crtc *crtc,
11713 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000011714{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011715 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000011716 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011717 struct skl_pipe_wm hw_wm, *sw_wm;
11718 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11719 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11721 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011722 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000011723
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011724 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000011725 return;
11726
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011727 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020011728 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011729
Damien Lespiau08db6652014-11-04 17:06:52 +000011730 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11731 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11732
Mahesh Kumar74bd8002018-04-26 19:55:15 +053011733 if (INTEL_GEN(dev_priv) >= 11)
11734 if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
11735 DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
11736 sw_ddb->enabled_slices,
11737 hw_ddb.enabled_slices);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011738 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070011739 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011740 hw_plane_wm = &hw_wm.planes[plane];
11741 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000011742
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011743 /* Watermarks */
11744 for (level = 0; level <= max_level; level++) {
11745 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11746 &sw_plane_wm->wm[level]))
11747 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000011748
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011749 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",
11750 pipe_name(pipe), plane + 1, level,
11751 sw_plane_wm->wm[level].plane_en,
11752 sw_plane_wm->wm[level].plane_res_b,
11753 sw_plane_wm->wm[level].plane_res_l,
11754 hw_plane_wm->wm[level].plane_en,
11755 hw_plane_wm->wm[level].plane_res_b,
11756 hw_plane_wm->wm[level].plane_res_l);
11757 }
11758
11759 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11760 &sw_plane_wm->trans_wm)) {
11761 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",
11762 pipe_name(pipe), plane + 1,
11763 sw_plane_wm->trans_wm.plane_en,
11764 sw_plane_wm->trans_wm.plane_res_b,
11765 sw_plane_wm->trans_wm.plane_res_l,
11766 hw_plane_wm->trans_wm.plane_en,
11767 hw_plane_wm->trans_wm.plane_res_b,
11768 hw_plane_wm->trans_wm.plane_res_l);
11769 }
11770
11771 /* DDB */
11772 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11773 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11774
11775 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011776 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 -040011777 pipe_name(pipe), plane + 1,
11778 sw_ddb_entry->start, sw_ddb_entry->end,
11779 hw_ddb_entry->start, hw_ddb_entry->end);
11780 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011781 }
11782
Lyude27082492016-08-24 07:48:10 +020011783 /*
11784 * cursor
11785 * If the cursor plane isn't active, we may not have updated it's ddb
11786 * allocation. In that case since the ddb allocation will be updated
11787 * once the plane becomes visible, we can skip this check
11788 */
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030011789 if (1) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011790 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11791 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011792
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011793 /* Watermarks */
11794 for (level = 0; level <= max_level; level++) {
11795 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11796 &sw_plane_wm->wm[level]))
11797 continue;
11798
11799 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",
11800 pipe_name(pipe), level,
11801 sw_plane_wm->wm[level].plane_en,
11802 sw_plane_wm->wm[level].plane_res_b,
11803 sw_plane_wm->wm[level].plane_res_l,
11804 hw_plane_wm->wm[level].plane_en,
11805 hw_plane_wm->wm[level].plane_res_b,
11806 hw_plane_wm->wm[level].plane_res_l);
11807 }
11808
11809 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11810 &sw_plane_wm->trans_wm)) {
11811 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11812 pipe_name(pipe),
11813 sw_plane_wm->trans_wm.plane_en,
11814 sw_plane_wm->trans_wm.plane_res_b,
11815 sw_plane_wm->trans_wm.plane_res_l,
11816 hw_plane_wm->trans_wm.plane_en,
11817 hw_plane_wm->trans_wm.plane_res_b,
11818 hw_plane_wm->trans_wm.plane_res_l);
11819 }
11820
11821 /* DDB */
11822 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11823 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
11824
11825 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040011826 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020011827 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040011828 sw_ddb_entry->start, sw_ddb_entry->end,
11829 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020011830 }
Damien Lespiau08db6652014-11-04 17:06:52 +000011831 }
11832}
11833
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011834static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011835verify_connector_state(struct drm_device *dev,
11836 struct drm_atomic_state *state,
11837 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011838{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011839 struct drm_connector *connector;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011840 struct drm_connector_state *new_conn_state;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010011841 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011842
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011843 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011844 struct drm_encoder *encoder = connector->encoder;
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011845 struct drm_crtc_state *crtc_state = NULL;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011846
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011847 if (new_conn_state->crtc != crtc)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011848 continue;
11849
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011850 if (crtc)
11851 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
11852
11853 intel_connector_verify_state(crtc_state, new_conn_state);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011854
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010011855 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020011856 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011857 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011858}
11859
11860static void
Daniel Vetter86b04262017-03-01 10:52:26 +010011861verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011862{
11863 struct intel_encoder *encoder;
Daniel Vetter86b04262017-03-01 10:52:26 +010011864 struct drm_connector *connector;
11865 struct drm_connector_state *old_conn_state, *new_conn_state;
11866 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011867
Damien Lespiaub2784e12014-08-05 11:29:37 +010011868 for_each_intel_encoder(dev, encoder) {
Daniel Vetter86b04262017-03-01 10:52:26 +010011869 bool enabled = false, found = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011870 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011871
11872 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11873 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011874 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011875
Daniel Vetter86b04262017-03-01 10:52:26 +010011876 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
11877 new_conn_state, i) {
11878 if (old_conn_state->best_encoder == &encoder->base)
11879 found = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011880
Daniel Vetter86b04262017-03-01 10:52:26 +010011881 if (new_conn_state->best_encoder != &encoder->base)
11882 continue;
11883 found = enabled = true;
11884
11885 I915_STATE_WARN(new_conn_state->crtc !=
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011886 encoder->base.crtc,
11887 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011888 }
Daniel Vetter86b04262017-03-01 10:52:26 +010011889
11890 if (!found)
11891 continue;
Dave Airlie0e32b392014-05-02 14:02:48 +100011892
Rob Clarke2c719b2014-12-15 13:56:32 -050011893 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011894 "encoder's enabled state mismatch "
11895 "(expected %i, found %i)\n",
11896 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011897
11898 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011899 bool active;
11900
11901 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011902 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011903 "encoder detached but still enabled on pipe %c.\n",
11904 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020011905 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011906 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011907}
11908
11909static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011910verify_crtc_state(struct drm_crtc *crtc,
11911 struct drm_crtc_state *old_crtc_state,
11912 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011913{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011914 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011915 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011916 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11918 struct intel_crtc_state *pipe_config, *sw_config;
11919 struct drm_atomic_state *old_state;
11920 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011921
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011922 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020011923 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011924 pipe_config = to_intel_crtc_state(old_crtc_state);
11925 memset(pipe_config, 0, sizeof(*pipe_config));
11926 pipe_config->base.crtc = crtc;
11927 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011928
Ville Syrjälä78108b72016-05-27 20:59:19 +030011929 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020011930
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011931 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011932
Ville Syrjäläe56134b2017-06-01 17:36:19 +030011933 /* we keep both pipes enabled on 830 */
11934 if (IS_I830(dev_priv))
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011935 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011936
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011937 I915_STATE_WARN(new_crtc_state->active != active,
11938 "crtc active state doesn't match with hw state "
11939 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011940
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011941 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
11942 "transitional active state does not match atomic hw state "
11943 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011944
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011945 for_each_encoder_on_crtc(dev, crtc, encoder) {
11946 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011947
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011948 active = encoder->get_hw_state(encoder, &pipe);
11949 I915_STATE_WARN(active != new_crtc_state->active,
11950 "[ENCODER:%i] active %i with crtc active %i\n",
11951 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011952
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011953 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
11954 "Encoder connected to wrong pipe %c\n",
11955 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011956
Ville Syrjäläe1214b92017-10-27 22:31:23 +030011957 if (active)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011958 encoder->get_config(encoder, pipe_config);
11959 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011960
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020011961 intel_crtc_compute_pixel_rate(pipe_config);
11962
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011963 if (!new_crtc_state->active)
11964 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011965
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011966 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011967
Maarten Lankhorst749d98b2017-05-11 10:28:43 +020011968 sw_config = to_intel_crtc_state(new_crtc_state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000011969 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011970 pipe_config, false)) {
11971 I915_STATE_WARN(1, "pipe state doesn't match!\n");
11972 intel_dump_pipe_config(intel_crtc, pipe_config,
11973 "[hw state]");
11974 intel_dump_pipe_config(intel_crtc, sw_config,
11975 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011976 }
11977}
11978
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011979static void
Ville Syrjäläcff109f2017-11-17 21:19:17 +020011980intel_verify_planes(struct intel_atomic_state *state)
11981{
11982 struct intel_plane *plane;
11983 const struct intel_plane_state *plane_state;
11984 int i;
11985
11986 for_each_new_intel_plane_in_state(state, plane,
11987 plane_state, i)
11988 assert_plane(plane, plane_state->base.visible);
11989}
11990
11991static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020011992verify_single_dpll_state(struct drm_i915_private *dev_priv,
11993 struct intel_shared_dpll *pll,
11994 struct drm_crtc *crtc,
11995 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011996{
11997 struct intel_dpll_hw_state dpll_hw_state;
Ville Syrjälä40560e22018-06-26 22:47:11 +030011998 unsigned int crtc_mask;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010011999 bool active;
12000
12001 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12002
Lucas De Marchi72f775f2018-03-20 15:06:34 -070012003 DRM_DEBUG_KMS("%s\n", pll->info->name);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012004
Lucas De Marchiee1398b2018-03-20 15:06:33 -070012005 active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012006
Lucas De Marchi5cd281f2018-03-20 15:06:36 -070012007 if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012008 I915_STATE_WARN(!pll->on && pll->active_mask,
12009 "pll in active use but not on in sw tracking\n");
12010 I915_STATE_WARN(pll->on && !pll->active_mask,
12011 "pll is on but not used by any active crtc\n");
12012 I915_STATE_WARN(pll->on != active,
12013 "pll on state mismatch (expected %i, found %i)\n",
12014 pll->on, active);
12015 }
12016
12017 if (!crtc) {
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012018 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012019 "more active pll users than references: %x vs %x\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012020 pll->active_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012021
12022 return;
12023 }
12024
Ville Syrjälä40560e22018-06-26 22:47:11 +030012025 crtc_mask = drm_crtc_mask(crtc);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012026
12027 if (new_state->active)
12028 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12029 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12030 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12031 else
12032 I915_STATE_WARN(pll->active_mask & crtc_mask,
12033 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12034 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12035
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012036 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012037 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012038 crtc_mask, pll->state.crtc_mask);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012039
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012040 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012041 &dpll_hw_state,
12042 sizeof(dpll_hw_state)),
12043 "pll hw state mismatch\n");
12044}
12045
12046static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012047verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12048 struct drm_crtc_state *old_crtc_state,
12049 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012050{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012051 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012052 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12053 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12054
12055 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012056 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012057
12058 if (old_state->shared_dpll &&
12059 old_state->shared_dpll != new_state->shared_dpll) {
Ville Syrjälä40560e22018-06-26 22:47:11 +030012060 unsigned int crtc_mask = drm_crtc_mask(crtc);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012061 struct intel_shared_dpll *pll = old_state->shared_dpll;
12062
12063 I915_STATE_WARN(pll->active_mask & crtc_mask,
12064 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12065 pipe_name(drm_crtc_index(crtc)));
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020012066 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012067 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12068 pipe_name(drm_crtc_index(crtc)));
12069 }
12070}
12071
12072static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012073intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012074 struct drm_atomic_state *state,
12075 struct drm_crtc_state *old_state,
12076 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012077{
Daniel Vetter5a21b662016-05-24 17:13:53 +020012078 if (!needs_modeset(new_state) &&
12079 !to_intel_crtc_state(new_state)->update_pipe)
12080 return;
12081
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012082 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012083 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012084 verify_crtc_state(crtc, old_state, new_state);
12085 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012086}
12087
12088static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012089verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012090{
Chris Wilsonfac5e232016-07-04 11:34:36 +010012091 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012092 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012093
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012094 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012095 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012096}
Daniel Vetter53589012013-06-05 13:34:16 +020012097
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012098static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012099intel_modeset_verify_disabled(struct drm_device *dev,
12100 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012101{
Daniel Vetter86b04262017-03-01 10:52:26 +010012102 verify_encoder_state(dev, state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012103 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012104 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020012105}
12106
Ville Syrjälä80715b22014-05-15 20:23:23 +030012107static void update_scanline_offset(struct intel_crtc *crtc)
12108{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012109 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012110
12111 /*
12112 * The scanline counter increments at the leading edge of hsync.
12113 *
12114 * On most platforms it starts counting from vtotal-1 on the
12115 * first active line. That means the scanline counter value is
12116 * always one less than what we would expect. Ie. just after
12117 * start of vblank, which also occurs at start of hsync (on the
12118 * last active line), the scanline counter will read vblank_start-1.
12119 *
12120 * On gen2 the scanline counter starts counting from 1 instead
12121 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12122 * to keep the value positive), instead of adding one.
12123 *
12124 * On HSW+ the behaviour of the scanline counter depends on the output
12125 * type. For DP ports it behaves like most other platforms, but on HDMI
12126 * there's an extra 1 line difference. So we need to add two instead of
12127 * one to the value.
Ville Syrjäläec1b4ee2016-12-15 19:47:34 +020012128 *
12129 * On VLV/CHV DSI the scanline counter would appear to increment
12130 * approx. 1/3 of a scanline before start of vblank. Unfortunately
12131 * that means we can't tell whether we're in vblank or not while
12132 * we're on that particular line. We must still set scanline_offset
12133 * to 1 so that the vblank timestamps come out correct when we query
12134 * the scanline counter from within the vblank interrupt handler.
12135 * However if queried just before the start of vblank we'll get an
12136 * answer that's slightly in the future.
Ville Syrjälä80715b22014-05-15 20:23:23 +030012137 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012138 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030012139 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012140 int vtotal;
12141
Ville Syrjälä124abe02015-09-08 13:40:45 +030012142 vtotal = adjusted_mode->crtc_vtotal;
12143 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030012144 vtotal /= 2;
12145
12146 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012147 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030012148 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012149 crtc->scanline_offset = 2;
12150 } else
12151 crtc->scanline_offset = 1;
12152}
12153
Maarten Lankhorstad421372015-06-15 12:33:42 +020012154static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012155{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012156 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012157 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012158 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012159 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012160 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012161
12162 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012163 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012164
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012165 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012167 struct intel_shared_dpll *old_dpll =
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012168 to_intel_crtc_state(old_crtc_state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012169
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012170 if (!needs_modeset(new_crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012171 continue;
12172
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012173 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012174
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012175 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010012176 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012177
Ander Conselvan de Oliveiraa1c414e2016-12-29 17:22:07 +020012178 intel_release_shared_dpll(old_dpll, intel_crtc, state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012179 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012180}
12181
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012182/*
12183 * This implements the workaround described in the "notes" section of the mode
12184 * set sequence documentation. When going from no pipes or single pipe to
12185 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12186 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12187 */
12188static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12189{
12190 struct drm_crtc_state *crtc_state;
12191 struct intel_crtc *intel_crtc;
12192 struct drm_crtc *crtc;
12193 struct intel_crtc_state *first_crtc_state = NULL;
12194 struct intel_crtc_state *other_crtc_state = NULL;
12195 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12196 int i;
12197
12198 /* look at all crtc's that are going to be enabled in during modeset */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012199 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012200 intel_crtc = to_intel_crtc(crtc);
12201
12202 if (!crtc_state->active || !needs_modeset(crtc_state))
12203 continue;
12204
12205 if (first_crtc_state) {
12206 other_crtc_state = to_intel_crtc_state(crtc_state);
12207 break;
12208 } else {
12209 first_crtc_state = to_intel_crtc_state(crtc_state);
12210 first_pipe = intel_crtc->pipe;
12211 }
12212 }
12213
12214 /* No workaround needed? */
12215 if (!first_crtc_state)
12216 return 0;
12217
12218 /* w/a possibly needed, check how many crtc's are already enabled. */
12219 for_each_intel_crtc(state->dev, intel_crtc) {
12220 struct intel_crtc_state *pipe_config;
12221
12222 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12223 if (IS_ERR(pipe_config))
12224 return PTR_ERR(pipe_config);
12225
12226 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12227
12228 if (!pipe_config->base.active ||
12229 needs_modeset(&pipe_config->base))
12230 continue;
12231
12232 /* 2 or more enabled crtcs means no need for w/a */
12233 if (enabled_pipe != INVALID_PIPE)
12234 return 0;
12235
12236 enabled_pipe = intel_crtc->pipe;
12237 }
12238
12239 if (enabled_pipe != INVALID_PIPE)
12240 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12241 else if (other_crtc_state)
12242 other_crtc_state->hsw_workaround_pipe = first_pipe;
12243
12244 return 0;
12245}
12246
Ville Syrjälä8d965612016-11-14 18:35:10 +020012247static int intel_lock_all_pipes(struct drm_atomic_state *state)
12248{
12249 struct drm_crtc *crtc;
12250
12251 /* Add all pipes to the state */
12252 for_each_crtc(state->dev, crtc) {
12253 struct drm_crtc_state *crtc_state;
12254
12255 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12256 if (IS_ERR(crtc_state))
12257 return PTR_ERR(crtc_state);
12258 }
12259
12260 return 0;
12261}
12262
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012263static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12264{
12265 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012266
Ville Syrjälä8d965612016-11-14 18:35:10 +020012267 /*
12268 * Add all pipes to the state, and force
12269 * a modeset on all the active ones.
12270 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012271 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012272 struct drm_crtc_state *crtc_state;
12273 int ret;
12274
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012275 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12276 if (IS_ERR(crtc_state))
12277 return PTR_ERR(crtc_state);
12278
12279 if (!crtc_state->active || needs_modeset(crtc_state))
12280 continue;
12281
12282 crtc_state->mode_changed = true;
12283
12284 ret = drm_atomic_add_affected_connectors(state, crtc);
12285 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012286 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012287
12288 ret = drm_atomic_add_affected_planes(state, crtc);
12289 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012290 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012291 }
12292
Ville Syrjälä9780aad2016-11-14 18:35:11 +020012293 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012294}
12295
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012296static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012297{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012298 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012299 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012300 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012301 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012302 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012303
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012304 if (!check_digital_port_conflicts(state)) {
12305 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12306 return -EINVAL;
12307 }
12308
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012309 intel_state->modeset = true;
12310 intel_state->active_crtcs = dev_priv->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012311 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12312 intel_state->cdclk.actual = dev_priv->cdclk.actual;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012313
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012314 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12315 if (new_crtc_state->active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012316 intel_state->active_crtcs |= 1 << i;
12317 else
12318 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070012319
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012320 if (old_crtc_state->active != new_crtc_state->active)
Matt Roper8b4a7d02016-05-12 07:06:00 -070012321 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012322 }
12323
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012324 /*
12325 * See if the config requires any additional preparation, e.g.
12326 * to adjust global state with pipes off. We need to do this
12327 * here so we can get the modeset_pipe updated config for the new
12328 * mode set on this crtc. For other crtcs we need to use the
12329 * adjusted_mode bits in the crtc directly.
12330 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012331 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030012332 ret = dev_priv->display.modeset_calc_cdclk(state);
12333 if (ret < 0)
12334 return ret;
12335
Ville Syrjälä8d965612016-11-14 18:35:10 +020012336 /*
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012337 * Writes to dev_priv->cdclk.logical must protected by
Ville Syrjälä8d965612016-11-14 18:35:10 +020012338 * holding all the crtc locks, even if we don't end up
12339 * touching the hardware
12340 */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012341 if (intel_cdclk_changed(&dev_priv->cdclk.logical,
12342 &intel_state->cdclk.logical)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012343 ret = intel_lock_all_pipes(state);
12344 if (ret < 0)
12345 return ret;
12346 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012347
Ville Syrjälä8d965612016-11-14 18:35:10 +020012348 /* All pipes must be switched off while we change the cdclk. */
Ville Syrjälä64600bd2017-10-24 12:52:08 +030012349 if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
12350 &intel_state->cdclk.actual)) {
Ville Syrjälä8d965612016-11-14 18:35:10 +020012351 ret = intel_modeset_all_pipes(state);
12352 if (ret < 0)
12353 return ret;
12354 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010012355
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012356 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12357 intel_state->cdclk.logical.cdclk,
12358 intel_state->cdclk.actual.cdclk);
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030012359 DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
12360 intel_state->cdclk.logical.voltage_level,
12361 intel_state->cdclk.actual.voltage_level);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012362 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012363 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012364 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012365
Maarten Lankhorstad421372015-06-15 12:33:42 +020012366 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012367
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012368 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012369 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012370
Maarten Lankhorstad421372015-06-15 12:33:42 +020012371 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012372}
12373
Matt Roperaa363132015-09-24 15:53:18 -070012374/*
12375 * Handle calculation of various watermark data at the end of the atomic check
12376 * phase. The code here should be run after the per-crtc and per-plane 'check'
12377 * handlers to ensure that all derived state has been updated.
12378 */
Matt Roper55994c22016-05-12 07:06:08 -070012379static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070012380{
12381 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070012382 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070012383
12384 /* Is there platform-specific watermark information to calculate? */
12385 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070012386 return dev_priv->display.compute_global_watermarks(state);
12387
12388 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070012389}
12390
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012391/**
12392 * intel_atomic_check - validate state object
12393 * @dev: drm device
12394 * @state: state to validate
12395 */
12396static int intel_atomic_check(struct drm_device *dev,
12397 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012398{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012399 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070012400 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012401 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012402 struct drm_crtc_state *old_crtc_state, *crtc_state;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012403 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012404 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012405
Maarten Lankhorst8c58f732018-02-21 10:28:08 +010012406 /* Catch I915_MODE_FLAG_INHERITED */
12407 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
12408 crtc_state, i) {
12409 if (crtc_state->mode.private_flags !=
12410 old_crtc_state->mode.private_flags)
12411 crtc_state->mode_changed = true;
12412 }
12413
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012414 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012415 if (ret)
12416 return ret;
12417
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012418 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012419 struct intel_crtc_state *pipe_config =
12420 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012421
Daniel Vetter26495482015-07-15 14:15:52 +020012422 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012423 continue;
12424
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012425 if (!crtc_state->enable) {
12426 any_ms = true;
12427 continue;
12428 }
12429
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012430 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012431 if (ret) {
12432 intel_dump_pipe_config(to_intel_crtc(crtc),
12433 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012434 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020012435 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012436
Michal Wajdeczko4f044a82017-09-19 19:38:44 +000012437 if (i915_modparams.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012438 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012439 to_intel_crtc_state(old_crtc_state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012440 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020012441 crtc_state->mode_changed = false;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012442 pipe_config->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020012443 }
12444
Daniel Vetteraf4a8792016-05-09 09:31:25 +020012445 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020012446 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012447
Daniel Vetter26495482015-07-15 14:15:52 +020012448 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12449 needs_modeset(crtc_state) ?
12450 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012451 }
12452
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012453 if (any_ms) {
12454 ret = intel_modeset_checks(state);
12455
12456 if (ret)
12457 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012458 } else {
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012459 intel_state->cdclk.logical = dev_priv->cdclk.logical;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020012460 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012461
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020012462 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070012463 if (ret)
12464 return ret;
12465
Ville Syrjälädd576022017-11-17 21:19:14 +020012466 intel_fbc_choose_crtc(dev_priv, intel_state);
Matt Roper55994c22016-05-12 07:06:08 -070012467 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012468}
12469
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012470static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010012471 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012472{
Chris Wilsonfd700752017-07-26 17:00:36 +010012473 return drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020012474}
12475
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012476u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12477{
12478 struct drm_device *dev = crtc->base.dev;
12479
12480 if (!dev->max_vblank_count)
Dhinakaran Pandiyan734cbbf2018-02-02 21:12:54 -080012481 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
Maarten Lankhorsta2991412016-05-17 15:07:48 +020012482
12483 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12484}
12485
Lyude896e5bb2016-08-24 07:48:09 +020012486static void intel_update_crtc(struct drm_crtc *crtc,
12487 struct drm_atomic_state *state,
12488 struct drm_crtc_state *old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012489 struct drm_crtc_state *new_crtc_state)
Lyude896e5bb2016-08-24 07:48:09 +020012490{
12491 struct drm_device *dev = crtc->dev;
12492 struct drm_i915_private *dev_priv = to_i915(dev);
12493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst6c246b82018-09-20 12:27:08 +020012494 struct intel_crtc_state *old_intel_cstate = to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012495 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12496 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012497 struct intel_plane_state *new_plane_state =
12498 intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
12499 to_intel_plane(crtc->primary));
Lyude896e5bb2016-08-24 07:48:09 +020012500
12501 if (modeset) {
12502 update_scanline_offset(intel_crtc);
12503 dev_priv->display.crtc_enable(pipe_config, state);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012504
12505 /* vblanks work again, re-enable pipe CRC. */
12506 intel_crtc_enable_pipe_crc(intel_crtc);
Lyude896e5bb2016-08-24 07:48:09 +020012507 } else {
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012508 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12509 pipe_config);
Lyude896e5bb2016-08-24 07:48:09 +020012510 }
12511
Maarten Lankhorst8b694492018-04-09 14:46:55 +020012512 if (new_plane_state)
12513 intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
Lyude896e5bb2016-08-24 07:48:09 +020012514
Maarten Lankhorst6c246b82018-09-20 12:27:08 +020012515 intel_begin_crtc_commit(crtc, old_crtc_state);
12516
12517 intel_update_planes_on_crtc(to_intel_atomic_state(state), intel_crtc,
12518 old_intel_cstate, pipe_config);
12519
12520 intel_finish_crtc_commit(crtc, old_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012521}
12522
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012523static void intel_update_crtcs(struct drm_atomic_state *state)
Lyude896e5bb2016-08-24 07:48:09 +020012524{
12525 struct drm_crtc *crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012526 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyude896e5bb2016-08-24 07:48:09 +020012527 int i;
12528
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012529 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12530 if (!new_crtc_state->active)
Lyude896e5bb2016-08-24 07:48:09 +020012531 continue;
12532
12533 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012534 new_crtc_state);
Lyude896e5bb2016-08-24 07:48:09 +020012535 }
12536}
12537
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012538static void skl_update_crtcs(struct drm_atomic_state *state)
Lyude27082492016-08-24 07:48:10 +020012539{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012540 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020012541 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12542 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040012543 struct intel_crtc *intel_crtc;
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012544 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040012545 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020012546 unsigned int updated = 0;
12547 bool progress;
12548 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012549 int i;
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012550 u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
12551 u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012552
12553 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12554
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012555 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012556 /* ignore allocations for crtc's that have been turned off. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012557 if (new_crtc_state->active)
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012558 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012559
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012560 /* If 2nd DBuf slice required, enable it here */
12561 if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
12562 icl_dbuf_slices_update(dev_priv, required_slices);
12563
Lyude27082492016-08-24 07:48:10 +020012564 /*
12565 * Whenever the number of active pipes changes, we need to make sure we
12566 * update the pipes in the right order so that their ddb allocations
12567 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12568 * cause pipe underruns and other bad stuff.
12569 */
12570 do {
Lyude27082492016-08-24 07:48:10 +020012571 progress = false;
12572
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012573 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Lyude27082492016-08-24 07:48:10 +020012574 bool vbl_wait = false;
12575 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040012576
12577 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä21794812017-08-23 18:22:26 +030012578 cstate = to_intel_crtc_state(new_crtc_state);
Lyudece0ba282016-09-15 10:46:35 -040012579 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020012580
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012581 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020012582 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012583
Mika Kahola2b685042017-10-10 13:17:03 +030012584 if (skl_ddb_allocation_overlaps(dev_priv,
12585 entries,
12586 &cstate->wm.skl.ddb,
12587 i))
Lyude27082492016-08-24 07:48:10 +020012588 continue;
12589
12590 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010012591 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020012592
12593 /*
12594 * If this is an already active pipe, it's DDB changed,
12595 * and this isn't the last pipe that needs updating
12596 * then we need to wait for a vblank to pass for the
12597 * new ddb allocation to take effect.
12598 */
Lyudece0ba282016-09-15 10:46:35 -040012599 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010012600 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012601 !new_crtc_state->active_changed &&
Lyude27082492016-08-24 07:48:10 +020012602 intel_state->wm_results.dirty_pipes != updated)
12603 vbl_wait = true;
12604
12605 intel_update_crtc(crtc, state, old_crtc_state,
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012606 new_crtc_state);
Lyude27082492016-08-24 07:48:10 +020012607
12608 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020012609 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020012610
12611 progress = true;
12612 }
12613 } while (progress);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +053012614
12615 /* If 2nd DBuf slice is no more required disable it */
12616 if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
12617 icl_dbuf_slices_update(dev_priv, required_slices);
Lyude27082492016-08-24 07:48:10 +020012618}
12619
Chris Wilsonba318c62017-02-02 20:47:41 +000012620static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12621{
12622 struct intel_atomic_state *state, *next;
12623 struct llist_node *freed;
12624
12625 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12626 llist_for_each_entry_safe(state, next, freed, freed)
12627 drm_atomic_state_put(&state->base);
12628}
12629
12630static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12631{
12632 struct drm_i915_private *dev_priv =
12633 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12634
12635 intel_atomic_helper_free_state(dev_priv);
12636}
12637
Daniel Vetter9db529a2017-08-08 10:08:28 +020012638static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
12639{
12640 struct wait_queue_entry wait_fence, wait_reset;
12641 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
12642
12643 init_wait_entry(&wait_fence, 0);
12644 init_wait_entry(&wait_reset, 0);
12645 for (;;) {
12646 prepare_to_wait(&intel_state->commit_ready.wait,
12647 &wait_fence, TASK_UNINTERRUPTIBLE);
12648 prepare_to_wait(&dev_priv->gpu_error.wait_queue,
12649 &wait_reset, TASK_UNINTERRUPTIBLE);
12650
12651
12652 if (i915_sw_fence_done(&intel_state->commit_ready)
12653 || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
12654 break;
12655
12656 schedule();
12657 }
12658 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
12659 finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
12660}
12661
Chris Wilson8d52e442018-06-23 11:39:51 +010012662static void intel_atomic_cleanup_work(struct work_struct *work)
12663{
12664 struct drm_atomic_state *state =
12665 container_of(work, struct drm_atomic_state, commit_work);
12666 struct drm_i915_private *i915 = to_i915(state->dev);
12667
12668 drm_atomic_helper_cleanup_planes(&i915->drm, state);
12669 drm_atomic_helper_commit_cleanup_done(state);
12670 drm_atomic_state_put(state);
12671
12672 intel_atomic_helper_free_state(i915);
12673}
12674
Daniel Vetter94f05022016-06-14 18:01:00 +020012675static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012676{
Daniel Vetter94f05022016-06-14 18:01:00 +020012677 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012678 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012679 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012680 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012681 struct intel_crtc_state *new_intel_crtc_state, *old_intel_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020012682 struct drm_crtc *crtc;
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012683 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020012684 u64 put_domains[I915_MAX_PIPES] = {};
Chris Wilsone95433c2016-10-28 13:58:27 +010012685 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012686
Daniel Vetter9db529a2017-08-08 10:08:28 +020012687 intel_atomic_commit_fence_wait(intel_state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020012688
Daniel Vetterea0000f2016-06-13 16:13:46 +020012689 drm_atomic_helper_wait_for_dependencies(state);
12690
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012691 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012692 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012693
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012694 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012695 old_intel_crtc_state = to_intel_crtc_state(old_crtc_state);
12696 new_intel_crtc_state = to_intel_crtc_state(new_crtc_state);
12697 intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012698
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012699 if (needs_modeset(new_crtc_state) ||
12700 to_intel_crtc_state(new_crtc_state)->update_pipe) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012701
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012702 put_domains[intel_crtc->pipe] =
Daniel Vetter5a21b662016-05-24 17:13:53 +020012703 modeset_get_crtc_power_domains(crtc,
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012704 new_intel_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012705 }
12706
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012707 if (!needs_modeset(new_crtc_state))
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012708 continue;
12709
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012710 intel_pre_plane_update(old_intel_crtc_state, new_intel_crtc_state);
Daniel Vetter460da9162013-03-27 00:44:51 +010012711
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020012712 if (old_crtc_state->active) {
Maarten Lankhorstf59e9702018-09-20 12:27:07 +020012713 intel_crtc_disable_planes(intel_crtc, old_intel_crtc_state->active_planes);
Maarten Lankhorst033b7a22018-03-08 13:02:02 +010012714
12715 /*
12716 * We need to disable pipe CRC before disabling the pipe,
12717 * or we race against vblank off.
12718 */
12719 intel_crtc_disable_pipe_crc(intel_crtc);
12720
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012721 dev_priv->display.crtc_disable(old_intel_crtc_state, state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012722 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020012723 intel_fbc_disable(intel_crtc);
Maarten Lankhorst65c307f2018-10-05 11:52:44 +020012724 intel_disable_shared_dpll(old_intel_crtc_state);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020012725
12726 /*
12727 * Underruns don't always raise
12728 * interrupts, so check manually.
12729 */
12730 intel_check_cpu_fifo_underruns(dev_priv);
12731 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010012732
Ville Syrjälä21794812017-08-23 18:22:26 +030012733 if (!new_crtc_state->active) {
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012734 /*
12735 * Make sure we don't call initial_watermarks
12736 * for ILK-style watermark updates.
Ville Syrjäläff32c542017-03-02 19:14:57 +020012737 *
12738 * No clue what this is supposed to achieve.
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012739 */
Ville Syrjäläff32c542017-03-02 19:14:57 +020012740 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012741 dev_priv->display.initial_watermarks(intel_state,
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012742 new_intel_crtc_state);
Maarten Lankhorste62929b2016-11-08 13:55:33 +010012743 }
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012744 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012745 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012746
Daniel Vetter7a1530d72017-12-07 15:32:02 +010012747 /* FIXME: Eventually get rid of our intel_crtc->config pointer */
12748 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
12749 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012750
Maarten Lankhorst565602d2015-12-10 12:33:57 +010012751 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012752 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010012753
Ville Syrjäläb0587e42017-01-26 21:52:01 +020012754 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010012755
Lyude656d1b82016-08-17 15:55:54 -040012756 /*
12757 * SKL workaround: bspec recommends we disable the SAGV when we
12758 * have more then one pipe enabled
12759 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030012760 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012761 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012762
Maarten Lankhorst677100c2016-11-08 13:55:41 +010012763 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012764 }
Daniel Vetter47fab732012-10-26 10:58:18 +020012765
Lyude896e5bb2016-08-24 07:48:09 +020012766 /* Complete the events for pipes that have now been disabled */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012767 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12768 bool modeset = needs_modeset(new_crtc_state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020012769
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012770 /* Complete events for now disable pipes here. */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012771 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012772 spin_lock_irq(&dev->event_lock);
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012773 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012774 spin_unlock_irq(&dev->event_lock);
12775
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012776 new_crtc_state->event = NULL;
Daniel Vetter1f7528c2016-06-13 16:13:45 +020012777 }
Matt Ropered4a6a72016-02-23 17:20:13 -080012778 }
12779
Lyude896e5bb2016-08-24 07:48:09 +020012780 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012781 dev_priv->display.update_crtcs(state);
Lyude896e5bb2016-08-24 07:48:09 +020012782
Daniel Vetter94f05022016-06-14 18:01:00 +020012783 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12784 * already, but still need the state for the delayed optimization. To
12785 * fix this:
12786 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12787 * - schedule that vblank worker _before_ calling hw_done
12788 * - at the start of commit_tail, cancel it _synchrously
12789 * - switch over to the vblank wait helper in the core after that since
12790 * we don't need out special handling any more.
12791 */
Maarten Lankhorstb44d5c02017-09-04 12:48:33 +020012792 drm_atomic_helper_wait_for_flip_done(dev, state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012793
12794 /*
12795 * Now that the vblank has passed, we can go ahead and program the
12796 * optimal watermarks on platforms that need two-step watermark
12797 * programming.
12798 *
12799 * TODO: Move this (and other cleanup) to an async worker eventually.
12800 */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012801 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012802 new_intel_crtc_state = to_intel_crtc_state(new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012803
12804 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010012805 dev_priv->display.optimize_watermarks(intel_state,
Maarten Lankhorsta1cccdc2018-09-20 12:27:04 +020012806 new_intel_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012807 }
12808
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012809 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012810 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12811
12812 if (put_domains[i])
12813 modeset_put_power_domains(dev_priv, put_domains[i]);
12814
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012815 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012816 }
12817
Ville Syrjäläcff109f2017-11-17 21:19:17 +020012818 if (intel_state->modeset)
12819 intel_verify_planes(intel_state);
12820
Paulo Zanoni56feca92016-09-22 18:00:28 -030012821 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030012822 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040012823
Daniel Vetter94f05022016-06-14 18:01:00 +020012824 drm_atomic_helper_commit_hw_done(state);
12825
Chris Wilsond5553c02017-05-04 12:55:08 +010012826 if (intel_state->modeset) {
12827 /* As one of the primary mmio accessors, KMS has a high
12828 * likelihood of triggering bugs in unclaimed access. After we
12829 * finish modesetting, see if an error has been flagged, and if
12830 * so enable debugging for the next modeset - and hope we catch
12831 * the culprit.
12832 */
12833 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012834 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
Chris Wilsond5553c02017-05-04 12:55:08 +010012835 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020012836
Chris Wilson8d52e442018-06-23 11:39:51 +010012837 /*
12838 * Defer the cleanup of the old state to a separate worker to not
12839 * impede the current task (userspace for blocking modesets) that
12840 * are executed inline. For out-of-line asynchronous modesets/flips,
12841 * deferring to a new worker seems overkill, but we would place a
12842 * schedule point (cond_resched()) here anyway to keep latencies
12843 * down.
12844 */
12845 INIT_WORK(&state->commit_work, intel_atomic_cleanup_work);
Chris Wilson41db6452018-07-12 12:57:29 +010012846 queue_work(system_highpri_wq, &state->commit_work);
Daniel Vetter94f05022016-06-14 18:01:00 +020012847}
12848
12849static void intel_atomic_commit_work(struct work_struct *work)
12850{
Chris Wilsonc004a902016-10-28 13:58:45 +010012851 struct drm_atomic_state *state =
12852 container_of(work, struct drm_atomic_state, commit_work);
12853
Daniel Vetter94f05022016-06-14 18:01:00 +020012854 intel_atomic_commit_tail(state);
12855}
12856
Chris Wilsonc004a902016-10-28 13:58:45 +010012857static int __i915_sw_fence_call
12858intel_atomic_commit_ready(struct i915_sw_fence *fence,
12859 enum i915_sw_fence_notify notify)
12860{
12861 struct intel_atomic_state *state =
12862 container_of(fence, struct intel_atomic_state, commit_ready);
12863
12864 switch (notify) {
12865 case FENCE_COMPLETE:
Daniel Vetter42b062b2017-08-08 10:08:27 +020012866 /* we do blocking waits in the worker, nothing to do here */
Chris Wilsonc004a902016-10-28 13:58:45 +010012867 break;
Chris Wilsonc004a902016-10-28 13:58:45 +010012868 case FENCE_FREE:
Chris Wilsoneb955ee2017-01-23 21:29:39 +000012869 {
12870 struct intel_atomic_helper *helper =
12871 &to_i915(state->base.dev)->atomic_helper;
12872
12873 if (llist_add(&state->freed, &helper->free_list))
12874 schedule_work(&helper->free_work);
12875 break;
12876 }
Chris Wilsonc004a902016-10-28 13:58:45 +010012877 }
12878
12879 return NOTIFY_DONE;
12880}
12881
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012882static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12883{
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012884 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012885 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012886 int i;
12887
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012888 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012889 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010012890 intel_fb_obj(new_plane_state->fb),
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010012891 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012892}
12893
Daniel Vetter94f05022016-06-14 18:01:00 +020012894/**
12895 * intel_atomic_commit - commit validated state object
12896 * @dev: DRM device
12897 * @state: the top-level driver state object
12898 * @nonblock: nonblocking commit
12899 *
12900 * This function commits a top-level state object that has been validated
12901 * with drm_atomic_helper_check().
12902 *
Daniel Vetter94f05022016-06-14 18:01:00 +020012903 * RETURNS
12904 * Zero for success or -errno.
12905 */
12906static int intel_atomic_commit(struct drm_device *dev,
12907 struct drm_atomic_state *state,
12908 bool nonblock)
12909{
12910 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010012911 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020012912 int ret = 0;
12913
Chris Wilsonc004a902016-10-28 13:58:45 +010012914 drm_atomic_state_get(state);
12915 i915_sw_fence_init(&intel_state->commit_ready,
12916 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020012917
Ville Syrjälä440df932017-03-29 17:21:23 +030012918 /*
12919 * The intel_legacy_cursor_update() fast path takes care
12920 * of avoiding the vblank waits for simple cursor
12921 * movement and flips. For cursor on/off and size changes,
12922 * we want to perform the vblank waits so that watermark
12923 * updates happen during the correct frames. Gen9+ have
12924 * double buffered watermarks and so shouldn't need this.
12925 *
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020012926 * Unset state->legacy_cursor_update before the call to
12927 * drm_atomic_helper_setup_commit() because otherwise
12928 * drm_atomic_helper_wait_for_flip_done() is a noop and
12929 * we get FIFO underruns because we didn't wait
12930 * for vblank.
Ville Syrjälä440df932017-03-29 17:21:23 +030012931 *
12932 * FIXME doing watermarks and fb cleanup from a vblank worker
12933 * (assuming we had any) would solve these problems.
12934 */
Maarten Lankhorst213f1bd2017-09-19 14:14:19 +020012935 if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
12936 struct intel_crtc_state *new_crtc_state;
12937 struct intel_crtc *crtc;
12938 int i;
12939
12940 for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
12941 if (new_crtc_state->wm.need_postvbl_update ||
12942 new_crtc_state->update_wm_post)
12943 state->legacy_cursor_update = false;
12944 }
Ville Syrjälä440df932017-03-29 17:21:23 +030012945
Maarten Lankhorst3cf50c62017-09-19 14:14:18 +020012946 ret = intel_atomic_prepare_commit(dev, state);
12947 if (ret) {
12948 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
12949 i915_sw_fence_commit(&intel_state->commit_ready);
12950 return ret;
12951 }
12952
12953 ret = drm_atomic_helper_setup_commit(state, nonblock);
12954 if (!ret)
12955 ret = drm_atomic_helper_swap_state(state, true);
12956
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020012957 if (ret) {
12958 i915_sw_fence_commit(&intel_state->commit_ready);
12959
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020012960 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorst0806f4e2017-07-11 16:33:07 +020012961 return ret;
12962 }
Daniel Vetter94f05022016-06-14 18:01:00 +020012963 dev_priv->wm.distrust_bios_wm = false;
Ander Conselvan de Oliveira3c0fb582016-12-29 17:22:08 +020012964 intel_shared_dpll_swap_state(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020012965 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020012966
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012967 if (intel_state->modeset) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030012968 memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
12969 sizeof(intel_state->min_cdclk));
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030012970 memcpy(dev_priv->min_voltage_level,
12971 intel_state->min_voltage_level,
12972 sizeof(intel_state->min_voltage_level));
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012973 dev_priv->active_crtcs = intel_state->active_crtcs;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020012974 dev_priv->cdclk.logical = intel_state->cdclk.logical;
12975 dev_priv->cdclk.actual = intel_state->cdclk.actual;
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010012976 }
12977
Chris Wilson08536952016-10-14 13:18:18 +010012978 drm_atomic_state_get(state);
Daniel Vetter42b062b2017-08-08 10:08:27 +020012979 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
Chris Wilsonc004a902016-10-28 13:58:45 +010012980
12981 i915_sw_fence_commit(&intel_state->commit_ready);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020012982 if (nonblock && intel_state->modeset) {
12983 queue_work(dev_priv->modeset_wq, &state->commit_work);
12984 } else if (nonblock) {
Daniel Vetter42b062b2017-08-08 10:08:27 +020012985 queue_work(system_unbound_wq, &state->commit_work);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020012986 } else {
12987 if (intel_state->modeset)
12988 flush_workqueue(dev_priv->modeset_wq);
Daniel Vetter94f05022016-06-14 18:01:00 +020012989 intel_atomic_commit_tail(state);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020012990 }
Mika Kuoppala75714942015-12-16 09:26:48 +020012991
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012992 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012993}
12994
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012995static const struct drm_crtc_funcs intel_crtc_funcs = {
Daniel Vetter3fab2f02017-04-03 10:32:57 +020012996 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012997 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012998 .destroy = intel_crtc_destroy,
Maarten Lankhorst4c01ded2016-12-22 11:33:23 +010012999 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013000 .atomic_duplicate_state = intel_crtc_duplicate_state,
13001 .atomic_destroy_state = intel_crtc_destroy_state,
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +010013002 .set_crc_source = intel_crtc_set_crc_source,
Mahesh Kumara8c20832018-07-13 19:29:38 +053013003 .verify_crc_source = intel_crtc_verify_crc_source,
Mahesh Kumar260bc552018-07-13 19:29:39 +053013004 .get_crc_sources = intel_crtc_get_crc_sources,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013005};
13006
Chris Wilson74d290f2017-08-17 13:37:06 +010013007struct wait_rps_boost {
13008 struct wait_queue_entry wait;
13009
13010 struct drm_crtc *crtc;
Chris Wilsone61e0f52018-02-21 09:56:36 +000013011 struct i915_request *request;
Chris Wilson74d290f2017-08-17 13:37:06 +010013012};
13013
13014static int do_rps_boost(struct wait_queue_entry *_wait,
13015 unsigned mode, int sync, void *key)
13016{
13017 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
Chris Wilsone61e0f52018-02-21 09:56:36 +000013018 struct i915_request *rq = wait->request;
Chris Wilson74d290f2017-08-17 13:37:06 +010013019
Chris Wilsone9af4ea2018-01-18 13:16:09 +000013020 /*
13021 * If we missed the vblank, but the request is already running it
13022 * is reasonable to assume that it will complete before the next
13023 * vblank without our intervention, so leave RPS alone.
13024 */
Chris Wilsone61e0f52018-02-21 09:56:36 +000013025 if (!i915_request_started(rq))
Chris Wilsone9af4ea2018-01-18 13:16:09 +000013026 gen6_rps_boost(rq, NULL);
Chris Wilsone61e0f52018-02-21 09:56:36 +000013027 i915_request_put(rq);
Chris Wilson74d290f2017-08-17 13:37:06 +010013028
13029 drm_crtc_vblank_put(wait->crtc);
13030
13031 list_del(&wait->wait.entry);
13032 kfree(wait);
13033 return 1;
13034}
13035
13036static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
13037 struct dma_fence *fence)
13038{
13039 struct wait_rps_boost *wait;
13040
13041 if (!dma_fence_is_i915(fence))
13042 return;
13043
13044 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
13045 return;
13046
13047 if (drm_crtc_vblank_get(crtc))
13048 return;
13049
13050 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
13051 if (!wait) {
13052 drm_crtc_vblank_put(crtc);
13053 return;
13054 }
13055
13056 wait->request = to_request(dma_fence_get(fence));
13057 wait->crtc = crtc;
13058
13059 wait->wait.func = do_rps_boost;
13060 wait->wait.flags = 0;
13061
13062 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
13063}
13064
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013065static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
13066{
13067 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
13068 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
13069 struct drm_framebuffer *fb = plane_state->base.fb;
13070 struct i915_vma *vma;
13071
13072 if (plane->id == PLANE_CURSOR &&
13073 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13074 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13075 const int align = intel_cursor_alignment(dev_priv);
Chris Wilson4a477652018-08-17 09:24:05 +010013076 int err;
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013077
Chris Wilson4a477652018-08-17 09:24:05 +010013078 err = i915_gem_object_attach_phys(obj, align);
13079 if (err)
13080 return err;
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013081 }
13082
13083 vma = intel_pin_and_fence_fb_obj(fb,
Ville Syrjäläf5929c52018-09-07 18:24:06 +030013084 &plane_state->view,
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013085 intel_plane_uses_fence(plane_state),
13086 &plane_state->flags);
13087 if (IS_ERR(vma))
13088 return PTR_ERR(vma);
13089
13090 plane_state->vma = vma;
13091
13092 return 0;
13093}
13094
13095static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
13096{
13097 struct i915_vma *vma;
13098
13099 vma = fetch_and_zero(&old_plane_state->vma);
13100 if (vma)
13101 intel_unpin_fb_vma(vma, old_plane_state->flags);
13102}
13103
Chris Wilsonb7268c52018-04-18 19:40:52 +010013104static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
13105{
13106 struct i915_sched_attr attr = {
13107 .priority = I915_PRIORITY_DISPLAY,
13108 };
13109
13110 i915_gem_object_wait_priority(obj, 0, &attr);
13111}
13112
Matt Roper6beb8c232014-12-01 15:40:14 -080013113/**
13114 * intel_prepare_plane_fb - Prepare fb for usage on plane
13115 * @plane: drm plane to prepare for
Chris Wilsonc38c1452018-02-14 13:49:22 +000013116 * @new_state: the plane state being prepared
Matt Roper6beb8c232014-12-01 15:40:14 -080013117 *
13118 * Prepares a framebuffer for usage on a display plane. Generally this
13119 * involves pinning the underlying object and updating the frontbuffer tracking
13120 * bits. Some older platforms need special physical address handling for
13121 * cursor planes.
13122 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013123 * Must be called with struct_mutex held.
13124 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013125 * Returns 0 on success, negative error code on failure.
13126 */
13127int
13128intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013129 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013130{
Chris Wilsonc004a902016-10-28 13:58:45 +010013131 struct intel_atomic_state *intel_state =
13132 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000013133 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013134 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013135 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013136 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010013137 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013138
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013139 if (old_obj) {
13140 struct drm_crtc_state *crtc_state =
Maarten Lankhorst8b694492018-04-09 14:46:55 +020013141 drm_atomic_get_new_crtc_state(new_state->state,
13142 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013143
13144 /* Big Hammer, we also need to ensure that any pending
13145 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13146 * current scanout is retired before unpinning the old
13147 * framebuffer. Note that we rely on userspace rendering
13148 * into the buffer attached to the pipe they are waiting
13149 * on. If not, userspace generates a GPU hang with IPEHR
13150 * point to the MI_WAIT_FOR_EVENT.
13151 *
13152 * This should only fail upon a hung GPU, in which case we
13153 * can safely continue.
13154 */
Chris Wilsonc004a902016-10-28 13:58:45 +010013155 if (needs_modeset(crtc_state)) {
13156 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13157 old_obj->resv, NULL,
13158 false, 0,
13159 GFP_KERNEL);
13160 if (ret < 0)
13161 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013162 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013163 }
13164
Chris Wilsonc004a902016-10-28 13:58:45 +010013165 if (new_state->fence) { /* explicit fencing */
13166 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13167 new_state->fence,
13168 I915_FENCE_TIMEOUT,
13169 GFP_KERNEL);
13170 if (ret < 0)
13171 return ret;
13172 }
13173
Chris Wilsonc37efb92016-06-17 08:28:47 +010013174 if (!obj)
13175 return 0;
13176
Chris Wilson4d3088c2017-07-26 17:00:38 +010013177 ret = i915_gem_object_pin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010013178 if (ret)
13179 return ret;
13180
Chris Wilson4d3088c2017-07-26 17:00:38 +010013181 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13182 if (ret) {
13183 i915_gem_object_unpin_pages(obj);
13184 return ret;
13185 }
13186
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013187 ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
Chris Wilsonfd700752017-07-26 17:00:36 +010013188
Chris Wilsonfd700752017-07-26 17:00:36 +010013189 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson4d3088c2017-07-26 17:00:38 +010013190 i915_gem_object_unpin_pages(obj);
Chris Wilsonfd700752017-07-26 17:00:36 +010013191 if (ret)
13192 return ret;
13193
Chris Wilsone2f34962018-10-01 15:47:54 +010013194 fb_obj_bump_render_priority(obj);
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080013195 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
13196
Chris Wilsonc004a902016-10-28 13:58:45 +010013197 if (!new_state->fence) { /* implicit fencing */
Chris Wilson74d290f2017-08-17 13:37:06 +010013198 struct dma_fence *fence;
13199
Chris Wilsonc004a902016-10-28 13:58:45 +010013200 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13201 obj->resv, NULL,
13202 false, I915_FENCE_TIMEOUT,
13203 GFP_KERNEL);
13204 if (ret < 0)
13205 return ret;
Chris Wilson74d290f2017-08-17 13:37:06 +010013206
13207 fence = reservation_object_get_excl_rcu(obj->resv);
13208 if (fence) {
13209 add_rps_boost_after_vblank(new_state->crtc, fence);
13210 dma_fence_put(fence);
13211 }
13212 } else {
13213 add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
Chris Wilsonc004a902016-10-28 13:58:45 +010013214 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020013215
Chris Wilson60548c52018-07-31 14:26:29 +010013216 /*
13217 * We declare pageflips to be interactive and so merit a small bias
13218 * towards upclocking to deliver the frame on time. By only changing
13219 * the RPS thresholds to sample more regularly and aim for higher
13220 * clocks we can hopefully deliver low power workloads (like kodi)
13221 * that are not quite steady state without resorting to forcing
13222 * maximum clocks following a vblank miss (see do_rps_boost()).
13223 */
13224 if (!intel_state->rps_interactive) {
13225 intel_rps_mark_interactive(dev_priv, true);
13226 intel_state->rps_interactive = true;
13227 }
13228
Chris Wilsond07f0e52016-10-28 13:58:44 +010013229 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080013230}
13231
Matt Roper38f3ce32014-12-02 07:45:25 -080013232/**
13233 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13234 * @plane: drm plane to clean up for
Chris Wilsonc38c1452018-02-14 13:49:22 +000013235 * @old_state: the state from the previous modeset
Matt Roper38f3ce32014-12-02 07:45:25 -080013236 *
13237 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013238 *
13239 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013240 */
13241void
13242intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010013243 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013244{
Chris Wilson60548c52018-07-31 14:26:29 +010013245 struct intel_atomic_state *intel_state =
13246 to_intel_atomic_state(old_state->state);
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013247 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper38f3ce32014-12-02 07:45:25 -080013248
Chris Wilson60548c52018-07-31 14:26:29 +010013249 if (intel_state->rps_interactive) {
13250 intel_rps_mark_interactive(dev_priv, false);
13251 intel_state->rps_interactive = false;
13252 }
13253
Chris Wilsonbe1e3412017-01-16 15:21:27 +000013254 /* Should only be called after a successful intel_prepare_plane_fb()! */
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013255 mutex_lock(&dev_priv->drm.struct_mutex);
13256 intel_plane_unpin_fb(to_intel_plane_state(old_state));
13257 mutex_unlock(&dev_priv->drm.struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013258}
13259
Chandra Konduru6156a452015-04-27 13:48:39 -070013260int
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013261skl_max_scale(const struct intel_crtc_state *crtc_state,
13262 u32 pixel_format)
Chandra Konduru6156a452015-04-27 13:48:39 -070013263{
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013264 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
13265 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru77224cd2018-04-09 09:11:13 +053013266 int max_scale, mult;
13267 int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
Chandra Konduru6156a452015-04-27 13:48:39 -070013268
Ville Syrjälä4e0b83a2018-09-07 18:24:09 +030013269 if (!crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013270 return DRM_PLANE_HELPER_NO_SCALING;
13271
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013272 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13273 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13274
Rodrigo Vivi43037c82017-10-03 15:31:42 -070013275 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Ander Conselvan de Oliveira5b7280f2017-02-23 09:15:58 +020013276 max_dotclk *= 2;
13277
13278 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013279 return DRM_PLANE_HELPER_NO_SCALING;
13280
13281 /*
13282 * skl max scale is lower of:
13283 * close to 3 but not 3, -1 is for that purpose
13284 * or
13285 * cdclk/crtc_clock
13286 */
Chandra Konduru77224cd2018-04-09 09:11:13 +053013287 mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
13288 tmpclk1 = (1 << 16) * mult - 1;
13289 tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
13290 max_scale = min(tmpclk1, tmpclk2);
Chandra Konduru6156a452015-04-27 13:48:39 -070013291
13292 return max_scale;
13293}
13294
Daniel Vetter5a21b662016-05-24 17:13:53 +020013295static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13296 struct drm_crtc_state *old_crtc_state)
13297{
13298 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040013299 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013301 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020013302 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013303 struct intel_atomic_state *old_intel_state =
13304 to_intel_atomic_state(old_crtc_state->state);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013305 struct intel_crtc_state *intel_cstate =
13306 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
13307 bool modeset = needs_modeset(&intel_cstate->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013308
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013309 if (!modeset &&
13310 (intel_cstate->base.color_mgmt_changed ||
13311 intel_cstate->update_pipe)) {
Ville Syrjälä5c857e62017-08-23 18:22:20 +030013312 intel_color_set_csc(&intel_cstate->base);
13313 intel_color_load_luts(&intel_cstate->base);
Maarten Lankhorst567f0792017-02-28 15:28:47 +010013314 }
13315
Daniel Vetter5a21b662016-05-24 17:13:53 +020013316 /* Perform vblank evasion around commit operation */
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013317 intel_pipe_update_start(intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013318
13319 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013320 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013321
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013322 if (intel_cstate->update_pipe)
Ville Syrjälä1a15b772017-08-23 18:22:25 +030013323 intel_update_pipe_config(old_intel_cstate, intel_cstate);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013324 else if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorst15cbe5d2018-10-04 11:45:56 +020013325 skl_detach_scalers(intel_cstate);
Lyude62e0fb82016-08-22 12:50:08 -040013326
Maarten Lankhorste62929b2016-11-08 13:55:33 +010013327out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010013328 if (dev_priv->display.atomic_update_watermarks)
13329 dev_priv->display.atomic_update_watermarks(old_intel_state,
13330 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013331}
13332
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013333void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
13334 struct intel_crtc_state *crtc_state)
13335{
13336 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13337
13338 if (!IS_GEN2(dev_priv))
13339 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
13340
13341 if (crtc_state->has_pch_encoder) {
13342 enum pipe pch_transcoder =
13343 intel_crtc_pch_transcoder(crtc);
13344
13345 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
13346 }
13347}
13348
Daniel Vetter5a21b662016-05-24 17:13:53 +020013349static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13350 struct drm_crtc_state *old_crtc_state)
13351{
13352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013353 struct intel_atomic_state *old_intel_state =
13354 to_intel_atomic_state(old_crtc_state->state);
13355 struct intel_crtc_state *new_crtc_state =
13356 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013357
Ville Syrjäläd3a8fb32017-08-23 18:22:21 +030013358 intel_pipe_update_end(new_crtc_state);
Maarten Lankhorst33a49862017-11-13 15:40:43 +010013359
13360 if (new_crtc_state->update_pipe &&
13361 !needs_modeset(&new_crtc_state->base) &&
Maarten Lankhorstd52ad9c2018-03-28 12:05:26 +020013362 old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
13363 intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013364}
13365
Matt Ropercf4c7c12014-12-04 10:27:42 -080013366/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013367 * intel_plane_destroy - destroy a plane
13368 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013369 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013370 * Common destruction function for all types of planes (primary, cursor,
13371 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013372 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013373void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013374{
Matt Roper465c1202014-05-29 08:06:54 -070013375 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030013376 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070013377}
13378
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013379static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
13380 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013381{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013382 switch (modifier) {
13383 case DRM_FORMAT_MOD_LINEAR:
13384 case I915_FORMAT_MOD_X_TILED:
13385 break;
13386 default:
13387 return false;
13388 }
13389
Ben Widawsky714244e2017-08-01 09:58:16 -070013390 switch (format) {
13391 case DRM_FORMAT_C8:
13392 case DRM_FORMAT_RGB565:
13393 case DRM_FORMAT_XRGB1555:
13394 case DRM_FORMAT_XRGB8888:
13395 return modifier == DRM_FORMAT_MOD_LINEAR ||
13396 modifier == I915_FORMAT_MOD_X_TILED;
13397 default:
13398 return false;
13399 }
13400}
13401
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013402static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
13403 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013404{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013405 switch (modifier) {
13406 case DRM_FORMAT_MOD_LINEAR:
13407 case I915_FORMAT_MOD_X_TILED:
13408 break;
13409 default:
13410 return false;
13411 }
13412
Ben Widawsky714244e2017-08-01 09:58:16 -070013413 switch (format) {
13414 case DRM_FORMAT_C8:
13415 case DRM_FORMAT_RGB565:
13416 case DRM_FORMAT_XRGB8888:
13417 case DRM_FORMAT_XBGR8888:
13418 case DRM_FORMAT_XRGB2101010:
13419 case DRM_FORMAT_XBGR2101010:
13420 return modifier == DRM_FORMAT_MOD_LINEAR ||
13421 modifier == I915_FORMAT_MOD_X_TILED;
13422 default:
13423 return false;
13424 }
13425}
13426
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013427static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
13428 u32 format, u64 modifier)
Ben Widawsky714244e2017-08-01 09:58:16 -070013429{
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013430 return modifier == DRM_FORMAT_MOD_LINEAR &&
13431 format == DRM_FORMAT_ARGB8888;
Ben Widawsky714244e2017-08-01 09:58:16 -070013432}
13433
Ville Syrjälä679bfe82018-10-05 15:58:07 +030013434static const struct drm_plane_funcs i965_plane_funcs = {
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013435 .update_plane = drm_atomic_helper_update_plane,
13436 .disable_plane = drm_atomic_helper_disable_plane,
13437 .destroy = intel_plane_destroy,
13438 .atomic_get_property = intel_plane_atomic_get_property,
13439 .atomic_set_property = intel_plane_atomic_set_property,
13440 .atomic_duplicate_state = intel_plane_duplicate_state,
13441 .atomic_destroy_state = intel_plane_destroy_state,
13442 .format_mod_supported = i965_plane_format_mod_supported,
13443};
13444
Ville Syrjälä679bfe82018-10-05 15:58:07 +030013445static const struct drm_plane_funcs i8xx_plane_funcs = {
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013446 .update_plane = drm_atomic_helper_update_plane,
13447 .disable_plane = drm_atomic_helper_disable_plane,
13448 .destroy = intel_plane_destroy,
13449 .atomic_get_property = intel_plane_atomic_get_property,
13450 .atomic_set_property = intel_plane_atomic_set_property,
13451 .atomic_duplicate_state = intel_plane_duplicate_state,
13452 .atomic_destroy_state = intel_plane_destroy_state,
13453 .format_mod_supported = i8xx_plane_format_mod_supported,
Matt Roper465c1202014-05-29 08:06:54 -070013454};
13455
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013456static int
13457intel_legacy_cursor_update(struct drm_plane *plane,
13458 struct drm_crtc *crtc,
13459 struct drm_framebuffer *fb,
13460 int crtc_x, int crtc_y,
13461 unsigned int crtc_w, unsigned int crtc_h,
13462 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013463 uint32_t src_w, uint32_t src_h,
13464 struct drm_modeset_acquire_ctx *ctx)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013465{
13466 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13467 int ret;
13468 struct drm_plane_state *old_plane_state, *new_plane_state;
13469 struct intel_plane *intel_plane = to_intel_plane(plane);
13470 struct drm_framebuffer *old_fb;
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013471 struct intel_crtc_state *crtc_state =
13472 to_intel_crtc_state(crtc->state);
13473 struct intel_crtc_state *new_crtc_state;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013474
13475 /*
13476 * When crtc is inactive or there is a modeset pending,
13477 * wait for it to complete in the slowpath
13478 */
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013479 if (!crtc_state->base.active || needs_modeset(&crtc_state->base) ||
13480 crtc_state->update_pipe)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013481 goto slow;
13482
13483 old_plane_state = plane->state;
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013484 /*
13485 * Don't do an async update if there is an outstanding commit modifying
13486 * the plane. This prevents our async update's changes from getting
13487 * overridden by a previous synchronous update's state.
13488 */
13489 if (old_plane_state->commit &&
13490 !try_wait_for_completion(&old_plane_state->commit->hw_done))
13491 goto slow;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013492
13493 /*
13494 * If any parameters change that may affect watermarks,
13495 * take the slowpath. Only changing fb or position should be
13496 * in the fastpath.
13497 */
13498 if (old_plane_state->crtc != crtc ||
13499 old_plane_state->src_w != src_w ||
13500 old_plane_state->src_h != src_h ||
13501 old_plane_state->crtc_w != crtc_w ||
13502 old_plane_state->crtc_h != crtc_h ||
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013503 !old_plane_state->fb != !fb)
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013504 goto slow;
13505
13506 new_plane_state = intel_plane_duplicate_state(plane);
13507 if (!new_plane_state)
13508 return -ENOMEM;
13509
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013510 new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(crtc));
13511 if (!new_crtc_state) {
13512 ret = -ENOMEM;
13513 goto out_free;
13514 }
13515
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013516 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13517
13518 new_plane_state->src_x = src_x;
13519 new_plane_state->src_y = src_y;
13520 new_plane_state->src_w = src_w;
13521 new_plane_state->src_h = src_h;
13522 new_plane_state->crtc_x = crtc_x;
13523 new_plane_state->crtc_y = crtc_y;
13524 new_plane_state->crtc_w = crtc_w;
13525 new_plane_state->crtc_h = crtc_h;
13526
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013527 ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
13528 to_intel_plane_state(old_plane_state),
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013529 to_intel_plane_state(new_plane_state));
13530 if (ret)
13531 goto out_free;
13532
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013533 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13534 if (ret)
13535 goto out_free;
13536
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013537 ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
13538 if (ret)
13539 goto out_unlock;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013540
Dhinakaran Pandiyana694e222018-03-06 19:34:19 -080013541 intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013542
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -080013543 old_fb = old_plane_state->fb;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013544 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13545 intel_plane->frontbuffer_bit);
13546
13547 /* Swap plane state */
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013548 plane->state = new_plane_state;
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013549
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013550 /*
13551 * We cannot swap crtc_state as it may be in use by an atomic commit or
13552 * page flip that's running simultaneously. If we swap crtc_state and
13553 * destroy the old state, we will cause a use-after-free there.
13554 *
13555 * Only update active_planes, which is needed for our internal
13556 * bookkeeping. Either value will do the right thing when updating
13557 * planes atomically. If the cursor was part of the atomic update then
13558 * we would have taken the slowpath.
13559 */
13560 crtc_state->active_planes = new_crtc_state->active_planes;
13561
Ville Syrjälä72259532017-03-02 19:15:05 +020013562 if (plane->state->visible) {
13563 trace_intel_update_plane(plane, to_intel_crtc(crtc));
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013564 intel_plane->update_plane(intel_plane, crtc_state,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +020013565 to_intel_plane_state(plane->state));
Ville Syrjälä72259532017-03-02 19:15:05 +020013566 } else {
13567 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
Ville Syrjälä282dbf92017-03-27 21:55:33 +030013568 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
Ville Syrjälä72259532017-03-02 19:15:05 +020013569 }
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013570
Ville Syrjäläef1a1912018-02-21 18:02:34 +020013571 intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013572
13573out_unlock:
13574 mutex_unlock(&dev_priv->drm.struct_mutex);
13575out_free:
Maarten Lankhorstc249c5f2018-09-20 12:27:05 +020013576 if (new_crtc_state)
13577 intel_crtc_destroy_state(crtc, &new_crtc_state->base);
Maarten Lankhorst669c9212017-09-04 12:48:38 +020013578 if (ret)
13579 intel_plane_destroy_state(plane, new_plane_state);
13580 else
13581 intel_plane_destroy_state(plane, old_plane_state);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013582 return ret;
13583
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013584slow:
13585 return drm_atomic_helper_update_plane(plane, crtc, fb,
13586 crtc_x, crtc_y, crtc_w, crtc_h,
Daniel Vetter34a2ab52017-03-22 22:50:41 +010013587 src_x, src_y, src_w, src_h, ctx);
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013588}
13589
13590static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13591 .update_plane = intel_legacy_cursor_update,
13592 .disable_plane = drm_atomic_helper_disable_plane,
13593 .destroy = intel_plane_destroy,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013594 .atomic_get_property = intel_plane_atomic_get_property,
13595 .atomic_set_property = intel_plane_atomic_set_property,
13596 .atomic_duplicate_state = intel_plane_duplicate_state,
13597 .atomic_destroy_state = intel_plane_destroy_state,
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013598 .format_mod_supported = intel_cursor_format_mod_supported,
Maarten Lankhorstf79f2692016-12-12 11:34:55 +010013599};
13600
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013601static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
13602 enum i9xx_plane_id i9xx_plane)
13603{
13604 if (!HAS_FBC(dev_priv))
13605 return false;
13606
13607 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
13608 return i9xx_plane == PLANE_A; /* tied to pipe A */
13609 else if (IS_IVYBRIDGE(dev_priv))
13610 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
13611 i9xx_plane == PLANE_C;
13612 else if (INTEL_GEN(dev_priv) >= 4)
13613 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
13614 else
13615 return i9xx_plane == PLANE_A;
13616}
13617
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013618static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020013619intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070013620{
Ville Syrjälä881440a2018-10-05 15:58:17 +030013621 struct intel_plane *plane;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013622 const struct drm_plane_funcs *plane_funcs;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013623 unsigned int supported_rotations;
Ville Syrjälädeb19682018-10-05 15:58:08 +030013624 unsigned int possible_crtcs;
Ville Syrjälä881440a2018-10-05 15:58:17 +030013625 const u64 *modifiers;
13626 const u32 *formats;
13627 int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013628 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013629
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013630 if (INTEL_GEN(dev_priv) >= 9)
13631 return skl_universal_plane_create(dev_priv, pipe,
13632 PLANE_PRIMARY);
13633
Ville Syrjälä881440a2018-10-05 15:58:17 +030013634 plane = intel_plane_alloc();
13635 if (IS_ERR(plane))
13636 return plane;
Matt Roperea2c67b2014-12-23 10:41:52 -080013637
Ville Syrjälä881440a2018-10-05 15:58:17 +030013638 plane->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013639 /*
13640 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13641 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13642 */
13643 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
Ville Syrjälä881440a2018-10-05 15:58:17 +030013644 plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020013645 else
Ville Syrjälä881440a2018-10-05 15:58:17 +030013646 plane->i9xx_plane = (enum i9xx_plane_id) pipe;
13647 plane->id = PLANE_PRIMARY;
13648 plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013649
Ville Syrjälä881440a2018-10-05 15:58:17 +030013650 plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
13651 if (plane->has_fbc) {
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013652 struct intel_fbc *fbc = &dev_priv->fbc;
13653
Ville Syrjälä881440a2018-10-05 15:58:17 +030013654 fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
Ville Syrjäläcf1805e2018-02-21 19:31:01 +020013655 }
13656
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013657 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä881440a2018-10-05 15:58:17 +030013658 formats = i965_primary_formats;
Damien Lespiau568db4f2015-05-12 16:13:18 +010013659 num_formats = ARRAY_SIZE(i965_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013660 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013661
Ville Syrjälä881440a2018-10-05 15:58:17 +030013662 plane->max_stride = i9xx_plane_max_stride;
13663 plane->update_plane = i9xx_update_plane;
13664 plane->disable_plane = i9xx_disable_plane;
13665 plane->get_hw_state = i9xx_plane_get_hw_state;
13666 plane->check_plane = i9xx_plane_check;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013667
13668 plane_funcs = &i965_plane_funcs;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013669 } else {
Ville Syrjälä881440a2018-10-05 15:58:17 +030013670 formats = i8xx_primary_formats;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013671 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Ben Widawsky714244e2017-08-01 09:58:16 -070013672 modifiers = i9xx_format_modifiers;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013673
Ville Syrjälä881440a2018-10-05 15:58:17 +030013674 plane->max_stride = i9xx_plane_max_stride;
13675 plane->update_plane = i9xx_update_plane;
13676 plane->disable_plane = i9xx_disable_plane;
13677 plane->get_hw_state = i9xx_plane_get_hw_state;
13678 plane->check_plane = i9xx_plane_check;
Ville Syrjäläa38189c2018-05-18 19:21:59 +030013679
13680 plane_funcs = &i8xx_plane_funcs;
Matt Roper465c1202014-05-29 08:06:54 -070013681 }
13682
Ville Syrjälädeb19682018-10-05 15:58:08 +030013683 possible_crtcs = BIT(pipe);
13684
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013685 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä881440a2018-10-05 15:58:17 +030013686 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013687 possible_crtcs, plane_funcs,
Ville Syrjälä881440a2018-10-05 15:58:17 +030013688 formats, num_formats, modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013689 DRM_PLANE_TYPE_PRIMARY,
13690 "primary %c", pipe_name(pipe));
13691 else
Ville Syrjälä881440a2018-10-05 15:58:17 +030013692 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013693 possible_crtcs, plane_funcs,
Ville Syrjälä881440a2018-10-05 15:58:17 +030013694 formats, num_formats, modifiers,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013695 DRM_PLANE_TYPE_PRIMARY,
Ville Syrjäläed150302017-11-17 21:19:10 +020013696 "plane %c",
Ville Syrjälä881440a2018-10-05 15:58:17 +030013697 plane_name(plane->i9xx_plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013698 if (ret)
13699 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053013700
Ville Syrjäläb7c80602018-10-05 15:58:15 +030013701 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020013702 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013703 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13704 DRM_MODE_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100013705 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013706 supported_rotations =
Robert Fossc2c446a2017-05-19 16:50:17 -040013707 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013708 } else {
Robert Fossc2c446a2017-05-19 16:50:17 -040013709 supported_rotations = DRM_MODE_ROTATE_0;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013710 }
13711
Dave Airlie5481e272016-10-25 16:36:13 +100013712 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä881440a2018-10-05 15:58:17 +030013713 drm_plane_create_rotation_property(&plane->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013714 DRM_MODE_ROTATE_0,
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013715 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053013716
Ville Syrjälä881440a2018-10-05 15:58:17 +030013717 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
Matt Roperea2c67b2014-12-23 10:41:52 -080013718
Ville Syrjälä881440a2018-10-05 15:58:17 +030013719 return plane;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013720
13721fail:
Ville Syrjälä881440a2018-10-05 15:58:17 +030013722 intel_plane_free(plane);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013723
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013724 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070013725}
13726
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013727static struct intel_plane *
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013728intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13729 enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070013730{
Ville Syrjälädeb19682018-10-05 15:58:08 +030013731 unsigned int possible_crtcs;
Ville Syrjäläc539b572018-10-05 15:58:14 +030013732 struct intel_plane *cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013733 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070013734
Ville Syrjäläc539b572018-10-05 15:58:14 +030013735 cursor = intel_plane_alloc();
13736 if (IS_ERR(cursor))
13737 return cursor;
Matt Roperea2c67b2014-12-23 10:41:52 -080013738
Matt Roper3d7d6512014-06-10 08:28:13 -070013739 cursor->pipe = pipe;
Ville Syrjäläed150302017-11-17 21:19:10 +020013740 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020013741 cursor->id = PLANE_CURSOR;
Ville Syrjäläc19e1122018-01-23 20:33:43 +020013742 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013743
13744 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjäläddd57132018-09-07 18:24:02 +030013745 cursor->max_stride = i845_cursor_max_stride;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013746 cursor->update_plane = i845_update_cursor;
13747 cursor->disable_plane = i845_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013748 cursor->get_hw_state = i845_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013749 cursor->check_plane = i845_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013750 } else {
Ville Syrjäläddd57132018-09-07 18:24:02 +030013751 cursor->max_stride = i9xx_cursor_max_stride;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013752 cursor->update_plane = i9xx_update_cursor;
13753 cursor->disable_plane = i9xx_disable_cursor;
Ville Syrjälä51f5a0962017-11-17 21:19:08 +020013754 cursor->get_hw_state = i9xx_cursor_get_hw_state;
Ville Syrjälä659056f2017-03-27 21:55:39 +030013755 cursor->check_plane = i9xx_check_cursor;
Ville Syrjäläb2d03b02017-03-27 21:55:37 +030013756 }
Matt Roper3d7d6512014-06-10 08:28:13 -070013757
Ville Syrjäläcd5dcbf2017-03-27 21:55:35 +030013758 cursor->cursor.base = ~0;
13759 cursor->cursor.cntl = ~0;
Ville Syrjälä024faac2017-03-27 21:55:42 +030013760
13761 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13762 cursor->cursor.size = ~0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013763
Ville Syrjälädeb19682018-10-05 15:58:08 +030013764 possible_crtcs = BIT(pipe);
13765
Ville Syrjälä580503c2016-10-31 22:37:00 +020013766 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
Ville Syrjälädeb19682018-10-05 15:58:08 +030013767 possible_crtcs, &intel_cursor_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013768 intel_cursor_formats,
13769 ARRAY_SIZE(intel_cursor_formats),
Ben Widawsky714244e2017-08-01 09:58:16 -070013770 cursor_format_modifiers,
13771 DRM_PLANE_TYPE_CURSOR,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030013772 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013773 if (ret)
13774 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013775
Dave Airlie5481e272016-10-25 16:36:13 +100013776 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030013777 drm_plane_create_rotation_property(&cursor->base,
Robert Fossc2c446a2017-05-19 16:50:17 -040013778 DRM_MODE_ROTATE_0,
13779 DRM_MODE_ROTATE_0 |
13780 DRM_MODE_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013781
Matt Roperea2c67b2014-12-23 10:41:52 -080013782 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13783
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013784 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013785
13786fail:
Ville Syrjäläc539b572018-10-05 15:58:14 +030013787 intel_plane_free(cursor);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013788
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013789 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070013790}
13791
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013792static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13793 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013794{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013795 struct intel_crtc_scaler_state *scaler_state =
13796 &crtc_state->scaler_state;
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013797 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013798 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013799
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013800 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13801 if (!crtc->num_scalers)
13802 return;
13803
Ville Syrjälä65edccc2016-10-31 22:37:01 +020013804 for (i = 0; i < crtc->num_scalers; i++) {
13805 struct intel_scaler *scaler = &scaler_state->scalers[i];
13806
13807 scaler->in_use = 0;
Maarten Lankhorst0aaf29b2018-09-21 16:44:37 +020013808 scaler->mode = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013809 }
13810
13811 scaler_state->scaler_id = -1;
13812}
13813
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013814static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013815{
13816 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013817 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013818 struct intel_plane *primary = NULL;
13819 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013820 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013821
Daniel Vetter955382f2013-09-19 14:05:45 +020013822 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013823 if (!intel_crtc)
13824 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080013825
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013826 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013827 if (!crtc_state) {
13828 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013829 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013830 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013831 intel_crtc->config = crtc_state;
13832 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013833 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013834
Ville Syrjälä580503c2016-10-31 22:37:00 +020013835 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013836 if (IS_ERR(primary)) {
13837 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070013838 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013839 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013840 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013841
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013842 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013843 struct intel_plane *plane;
13844
Ville Syrjälä580503c2016-10-31 22:37:00 +020013845 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013846 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013847 ret = PTR_ERR(plane);
13848 goto fail;
13849 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013850 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030013851 }
13852
Ville Syrjälä580503c2016-10-31 22:37:00 +020013853 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020013854 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013855 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070013856 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013857 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020013858 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070013859
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020013860 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013861 &primary->base, &cursor->base,
13862 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030013863 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070013864 if (ret)
13865 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013866
Jesse Barnes80824002009-09-10 15:28:06 -070013867 intel_crtc->pipe = pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013868
Nabendu Maiti1c74eea2016-11-29 11:23:14 +053013869 /* initialize shared scalers */
13870 intel_crtc_init_scalers(intel_crtc, crtc_state);
13871
Ville Syrjälä1947fd12018-03-05 19:41:22 +020013872 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
13873 dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
13874 dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
13875
13876 if (INTEL_GEN(dev_priv) < 9) {
13877 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
13878
13879 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13880 dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
13881 dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
13882 }
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013883
Jesse Barnes79e53942008-11-07 14:24:08 -080013884 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013885
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000013886 intel_color_init(&intel_crtc->base);
13887
Daniel Vetter87b6b102014-05-15 15:33:46 +020013888 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013889
13890 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070013891
13892fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013893 /*
13894 * drm_mode_config_cleanup() will free up any
13895 * crtcs/planes already initialized.
13896 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013897 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013898 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030013899
13900 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013901}
13902
Jesse Barnes752aa882013-10-31 18:55:49 +020013903enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13904{
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013905 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013906
Rob Clark51fd3712013-11-19 12:10:12 -050013907 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013908
Daniel Vetter51ec53d2017-03-01 10:52:24 +010013909 if (!connector->base.state->crtc)
Jesse Barnes752aa882013-10-31 18:55:49 +020013910 return INVALID_PIPE;
13911
Daniel Vetter51ec53d2017-03-01 10:52:24 +010013912 return to_intel_crtc(connector->base.state->crtc)->pipe;
Jesse Barnes752aa882013-10-31 18:55:49 +020013913}
13914
Ville Syrjälä6a20fe72018-02-07 18:48:41 +020013915int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
13916 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013917{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013918 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013919 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013920 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013921
Keith Packard418da172017-03-14 23:25:07 -070013922 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010013923 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013924 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013925
Rob Clark7707e652014-07-17 23:30:04 -040013926 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013927 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013928
Daniel Vetterc05422d2009-08-11 16:05:30 +020013929 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013930}
13931
Daniel Vetter66a92782012-07-12 20:08:18 +020013932static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013933{
Daniel Vetter66a92782012-07-12 20:08:18 +020013934 struct drm_device *dev = encoder->base.dev;
13935 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013936 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013937 int entry = 0;
13938
Damien Lespiaub2784e12014-08-05 11:29:37 +010013939 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013940 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013941 index_mask |= (1 << entry);
13942
Jesse Barnes79e53942008-11-07 14:24:08 -080013943 entry++;
13944 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013945
Jesse Barnes79e53942008-11-07 14:24:08 -080013946 return index_mask;
13947}
13948
Ville Syrjälä646d5772016-10-31 22:37:14 +020013949static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000013950{
Ville Syrjälä646d5772016-10-31 22:37:14 +020013951 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000013952 return false;
13953
13954 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13955 return false;
13956
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010013957 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013958 return false;
13959
13960 return true;
13961}
13962
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013963static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070013964{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013965 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000013966 return false;
13967
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010013968 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013969 return false;
13970
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013971 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013972 return false;
13973
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013974 if (HAS_PCH_LPT_H(dev_priv) &&
13975 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020013976 return false;
13977
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020013978 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013979 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 +020013980 return false;
13981
Ville Syrjäläe4abb732015-12-01 23:31:33 +020013982 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070013983 return false;
13984
13985 return true;
13986}
13987
Imre Deak8090ba82016-08-10 14:07:33 +030013988void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
13989{
13990 int pps_num;
13991 int pps_idx;
13992
13993 if (HAS_DDI(dev_priv))
13994 return;
13995 /*
13996 * This w/a is needed at least on CPT/PPT, but to be sure apply it
13997 * everywhere where registers can be write protected.
13998 */
13999 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14000 pps_num = 2;
14001 else
14002 pps_num = 1;
14003
14004 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14005 u32 val = I915_READ(PP_CONTROL(pps_idx));
14006
14007 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14008 I915_WRITE(PP_CONTROL(pps_idx), val);
14009 }
14010}
14011
Imre Deak44cb7342016-08-10 14:07:29 +030014012static void intel_pps_init(struct drm_i915_private *dev_priv)
14013{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020014014 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
Imre Deak44cb7342016-08-10 14:07:29 +030014015 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14016 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14017 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14018 else
14019 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030014020
14021 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030014022}
14023
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014024static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080014025{
Chris Wilson4ef69c72010-09-09 15:14:28 +010014026 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014027 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014028
Imre Deak44cb7342016-08-10 14:07:29 +030014029 intel_pps_init(dev_priv);
14030
Chris Wilsonfc0c5a92018-08-15 21:12:07 +010014031 if (INTEL_INFO(dev_priv)->num_pipes == 0)
14032 return;
14033
Imre Deak97a824e12016-06-21 11:51:47 +030014034 /*
14035 * intel_edp_init_connector() depends on this completing first, to
14036 * prevent the registeration of both eDP and LVDS and the incorrect
14037 * sharing of the PPS.
14038 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014039 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014040
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014041 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014042 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014043
Paulo Zanoni00c92d92018-05-21 17:25:47 -070014044 if (IS_ICELAKE(dev_priv)) {
14045 intel_ddi_init(dev_priv, PORT_A);
14046 intel_ddi_init(dev_priv, PORT_B);
14047 intel_ddi_init(dev_priv, PORT_C);
14048 intel_ddi_init(dev_priv, PORT_D);
14049 intel_ddi_init(dev_priv, PORT_E);
14050 intel_ddi_init(dev_priv, PORT_F);
14051 } else if (IS_GEN9_LP(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053014052 /*
14053 * FIXME: Broxton doesn't support port detection via the
14054 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14055 * detect the ports.
14056 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014057 intel_ddi_init(dev_priv, PORT_A);
14058 intel_ddi_init(dev_priv, PORT_B);
14059 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014060
Jani Nikulae5186342018-07-05 16:25:08 +030014061 vlv_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010014062 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014063 int found;
14064
Jesse Barnesde31fac2015-03-06 15:53:32 -080014065 /*
14066 * Haswell uses DDI functions to detect digital outputs.
14067 * On SKL pre-D0 the strap isn't connected, so we assume
14068 * it's there.
14069 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014070 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014071 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014072 if (found || IS_GEN9_BC(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014073 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014074
Rodrigo Vivi9787e832018-01-29 15:22:22 -080014075 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014076 * register */
14077 found = I915_READ(SFUSE_STRAP);
14078
14079 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014080 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014081 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014082 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014083 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014084 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi9787e832018-01-29 15:22:22 -080014085 if (found & SFUSE_STRAP_DDIF_DETECTED)
14086 intel_ddi_init(dev_priv, PORT_F);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014087 /*
14088 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14089 */
Rodrigo Vivib976dc52017-01-23 10:32:37 -080014090 if (IS_GEN9_BC(dev_priv) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014091 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14092 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14093 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014094 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014095
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014096 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014097 int found;
Jani Nikula7b91bf72017-08-18 12:30:19 +030014098 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014099
Ville Syrjälä646d5772016-10-31 22:37:14 +020014100 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014101 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014102
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014103 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014104 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014105 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014106 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014107 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014108 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014109 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014110 }
14111
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014112 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014113 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014114
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014115 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014116 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014117
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014118 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014119 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014120
Daniel Vetter270b3042012-10-27 15:52:05 +020014121 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014122 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014123 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014124 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014125
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014126 /*
14127 * The DP_DETECTED bit is the latched state of the DDC
14128 * SDA pin at boot. However since eDP doesn't require DDC
14129 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14130 * eDP ports may have been muxed to an alternate function.
14131 * Thus we can't rely on the DP_DETECTED bit alone to detect
14132 * eDP ports. Consult the VBT as well as DP_DETECTED to
14133 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014134 *
14135 * Sadly the straps seem to be missing sometimes even for HDMI
14136 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14137 * and VBT for the presence of the port. Additionally we can't
14138 * trust the port type the VBT declares as we've seen at least
14139 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014140 */
Jani Nikula7b91bf72017-08-18 12:30:19 +030014141 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014142 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14143 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014144 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014145 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014146 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014147
Jani Nikula7b91bf72017-08-18 12:30:19 +030014148 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014149 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14150 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014151 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014152 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014153 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014154
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014155 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014156 /*
14157 * eDP not supported on port D,
14158 * so no need to worry about it
14159 */
14160 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14161 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014162 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014163 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014164 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014165 }
14166
Jani Nikulae5186342018-07-05 16:25:08 +030014167 vlv_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014168 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014169 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014170
Paulo Zanonie2debe92013-02-18 19:00:27 -030014171 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014172 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014173 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014174 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014175 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014176 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014177 }
Ma Ling27185ae2009-08-24 13:50:23 +080014178
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014179 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014180 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014181 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014182
14183 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014184
Paulo Zanonie2debe92013-02-18 19:00:27 -030014185 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014186 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014187 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014188 }
Ma Ling27185ae2009-08-24 13:50:23 +080014189
Paulo Zanonie2debe92013-02-18 19:00:27 -030014190 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014191
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014192 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014193 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014194 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014195 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014196 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014197 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014198 }
Ma Ling27185ae2009-08-24 13:50:23 +080014199
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014200 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014201 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010014202 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014203 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014204
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000014205 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014206 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080014207
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014208 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014209
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014210 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014211 encoder->base.possible_crtcs = encoder->crtc_mask;
14212 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014213 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014214 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014215
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014216 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020014217
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020014218 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080014219}
14220
14221static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14222{
14223 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014224 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014225
Daniel Vetteref2d6332014-02-10 18:00:38 +010014226 drm_framebuffer_cleanup(fb);
Chris Wilson70001cd2017-02-16 09:46:21 +000014227
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014228 i915_gem_object_lock(obj);
14229 WARN_ON(!obj->framebuffer_references--);
14230 i915_gem_object_unlock(obj);
Chris Wilsondd689282017-03-01 15:41:28 +000014231
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014232 i915_gem_object_put(obj);
Chris Wilson70001cd2017-02-16 09:46:21 +000014233
Jesse Barnes79e53942008-11-07 14:24:08 -080014234 kfree(intel_fb);
14235}
14236
14237static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014238 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014239 unsigned int *handle)
14240{
Daniel Stonea5ff7a42018-05-18 15:30:07 +010014241 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014242
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014243 if (obj->userptr.mm) {
14244 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14245 return -EINVAL;
14246 }
14247
Chris Wilson05394f32010-11-08 19:18:58 +000014248 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014249}
14250
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014251static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14252 struct drm_file *file,
14253 unsigned flags, unsigned color,
14254 struct drm_clip_rect *clips,
14255 unsigned num_clips)
14256{
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014257 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014258
Chris Wilson5a97bcc2017-02-22 11:40:46 +000014259 i915_gem_object_flush_if_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +000014260 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014261
14262 return 0;
14263}
14264
Jesse Barnes79e53942008-11-07 14:24:08 -080014265static const struct drm_framebuffer_funcs intel_fb_funcs = {
14266 .destroy = intel_user_framebuffer_destroy,
14267 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014268 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014269};
14270
Damien Lespiaub3218032015-02-27 11:15:18 +000014271static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014272u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14273 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000014274{
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014275 struct intel_crtc *crtc;
14276 struct intel_plane *plane;
Damien Lespiaub3218032015-02-27 11:15:18 +000014277
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014278 /*
14279 * We assume the primary plane for pipe A has
14280 * the highest stride limits of them all.
14281 */
14282 crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
14283 plane = to_intel_plane(crtc->base.primary);
Ville Syrjäläac484962016-01-20 21:05:26 +020014284
Ville Syrjälä645d91f2018-09-07 18:24:03 +030014285 return plane->max_stride(plane, pixel_format, fb_modifier,
14286 DRM_MODE_ROTATE_0);
Damien Lespiaub3218032015-02-27 11:15:18 +000014287}
14288
Chris Wilson24dbf512017-02-15 10:59:18 +000014289static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14290 struct drm_i915_gem_object *obj,
14291 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014292{
Chris Wilson24dbf512017-02-15 10:59:18 +000014293 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014294 struct drm_framebuffer *fb = &intel_fb->base;
Eric Engestromb3c11ac2016-11-12 01:12:56 +000014295 struct drm_format_name_buf format_name;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014296 u32 pitch_limit;
Chris Wilsondd689282017-03-01 15:41:28 +000014297 unsigned int tiling, stride;
Chris Wilson24dbf512017-02-15 10:59:18 +000014298 int ret = -EINVAL;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014299 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -080014300
Chris Wilsondd689282017-03-01 15:41:28 +000014301 i915_gem_object_lock(obj);
14302 obj->framebuffer_references++;
14303 tiling = i915_gem_object_get_tiling(obj);
14304 stride = i915_gem_object_get_stride(obj);
14305 i915_gem_object_unlock(obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014306
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014307 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014308 /*
14309 * If there's a fence, enforce that
14310 * the fb modifier and tiling mode match.
14311 */
14312 if (tiling != I915_TILING_NONE &&
14313 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014314 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014315 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014316 }
14317 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014318 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014319 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014320 } else if (tiling == I915_TILING_Y) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014321 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
Chris Wilson24dbf512017-02-15 10:59:18 +000014322 goto err;
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014323 }
14324 }
14325
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014326 /* Passed in modifier sanity checking. */
14327 switch (mode_cmd->modifier[0]) {
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014328 case I915_FORMAT_MOD_Y_TILED_CCS:
14329 case I915_FORMAT_MOD_Yf_TILED_CCS:
14330 switch (mode_cmd->pixel_format) {
14331 case DRM_FORMAT_XBGR8888:
14332 case DRM_FORMAT_ABGR8888:
14333 case DRM_FORMAT_XRGB8888:
14334 case DRM_FORMAT_ARGB8888:
14335 break;
14336 default:
14337 DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
14338 goto err;
14339 }
14340 /* fall through */
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014341 case I915_FORMAT_MOD_Y_TILED:
14342 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014343 if (INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014344 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14345 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014346 goto err;
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014347 }
Ben Widawsky2f075562017-03-24 14:29:48 -070014348 case DRM_FORMAT_MOD_LINEAR:
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014349 case I915_FORMAT_MOD_X_TILED:
14350 break;
14351 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014352 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14353 mode_cmd->modifier[0]);
Chris Wilson24dbf512017-02-15 10:59:18 +000014354 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014355 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014356
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014357 /*
14358 * gen2/3 display engine uses the fence if present,
14359 * so the tiling mode must match the fb modifier exactly.
14360 */
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014361 if (INTEL_GEN(dev_priv) < 4 &&
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014362 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014363 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014364 goto err;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014365 }
14366
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014367 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014368 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014369 if (mode_cmd->pitches[0] > pitch_limit) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014370 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
Ben Widawsky2f075562017-03-24 14:29:48 -070014371 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014372 "tiled" : "linear",
14373 mode_cmd->pitches[0], pitch_limit);
Chris Wilson24dbf512017-02-15 10:59:18 +000014374 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014375 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014376
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020014377 /*
14378 * If there's a fence, enforce that
14379 * the fb pitch and fence stride match.
14380 */
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014381 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14382 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14383 mode_cmd->pitches[0], stride);
Chris Wilson24dbf512017-02-15 10:59:18 +000014384 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014385 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014386
Ville Syrjälä57779d02012-10-31 17:50:14 +020014387 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014388 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014389 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014390 case DRM_FORMAT_RGB565:
14391 case DRM_FORMAT_XRGB8888:
14392 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014393 break;
14394 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014395 if (INTEL_GEN(dev_priv) > 3) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014396 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14397 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014398 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014399 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014400 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014401 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014402 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014403 INTEL_GEN(dev_priv) < 9) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014404 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14405 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014406 goto err;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014407 }
14408 break;
14409 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014410 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014411 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014412 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014413 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14414 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014415 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014416 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014417 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014418 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014419 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014420 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14421 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014422 goto err;
Damien Lespiau75312082015-05-15 19:06:01 +010014423 }
14424 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014425 case DRM_FORMAT_YUYV:
14426 case DRM_FORMAT_UYVY:
14427 case DRM_FORMAT_YVYU:
14428 case DRM_FORMAT_VYUY:
Ville Syrjäläab330812017-04-21 21:14:32 +030014429 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014430 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14431 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014432 goto err;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014433 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014434 break;
Chandra Kondurue44134f2018-05-12 03:03:15 +053014435 case DRM_FORMAT_NV12:
Chandra Kondurue44134f2018-05-12 03:03:15 +053014436 if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
Dhinakaran Pandiyanb45649f2018-08-24 13:38:56 -070014437 IS_BROXTON(dev_priv) || INTEL_GEN(dev_priv) >= 11) {
Chandra Kondurue44134f2018-05-12 03:03:15 +053014438 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14439 drm_get_format_name(mode_cmd->pixel_format,
14440 &format_name));
14441 goto err;
14442 }
14443 break;
Chris Wilson57cd6502010-08-08 12:34:44 +010014444 default:
Ville Syrjälä144cc1432017-03-07 21:42:10 +020014445 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14446 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014447 goto err;
Chris Wilson57cd6502010-08-08 12:34:44 +010014448 }
14449
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014450 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14451 if (mode_cmd->offsets[0] != 0)
Chris Wilson24dbf512017-02-15 10:59:18 +000014452 goto err;
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014453
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014454 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014455
Chandra Kondurue44134f2018-05-12 03:03:15 +053014456 if (fb->format->format == DRM_FORMAT_NV12 &&
14457 (fb->width < SKL_MIN_YUV_420_SRC_W ||
14458 fb->height < SKL_MIN_YUV_420_SRC_H ||
14459 (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
14460 DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
14461 return -EINVAL;
14462 }
14463
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014464 for (i = 0; i < fb->format->num_planes; i++) {
14465 u32 stride_alignment;
14466
14467 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
14468 DRM_DEBUG_KMS("bad plane %d handle\n", i);
Christophe JAILLET37875d62017-09-10 10:56:42 +020014469 goto err;
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014470 }
14471
14472 stride_alignment = intel_fb_stride_alignment(fb, i);
14473
14474 /*
14475 * Display WA #0531: skl,bxt,kbl,glk
14476 *
14477 * Render decompression and plane width > 3840
14478 * combined with horizontal panning requires the
14479 * plane stride to be a multiple of 4. We'll just
14480 * require the entire fb to accommodate that to avoid
14481 * potential runtime errors at plane configuration time.
14482 */
14483 if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
Dhinakaran Pandiyan63eaf9a2018-08-22 12:38:27 -070014484 is_ccs_modifier(fb->modifier))
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014485 stride_alignment *= 4;
14486
14487 if (fb->pitches[i] & (stride_alignment - 1)) {
14488 DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
14489 i, fb->pitches[i], stride_alignment);
14490 goto err;
14491 }
Ville Syrjäläd88c4af2017-03-07 21:42:06 +020014492
Daniel Stonea268bcd2018-05-18 15:30:08 +010014493 fb->obj[i] = &obj->base;
14494 }
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014495
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014496 ret = intel_fill_fb_info(dev_priv, fb);
Ville Syrjälä6687c902015-09-15 13:16:41 +030014497 if (ret)
Chris Wilson9aceb5c12017-03-01 15:41:27 +000014498 goto err;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014499
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070014500 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -080014501 if (ret) {
14502 DRM_ERROR("framebuffer init failed %d\n", ret);
Chris Wilson24dbf512017-02-15 10:59:18 +000014503 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -080014504 }
14505
Jesse Barnes79e53942008-11-07 14:24:08 -080014506 return 0;
Chris Wilson24dbf512017-02-15 10:59:18 +000014507
14508err:
Chris Wilsondd689282017-03-01 15:41:28 +000014509 i915_gem_object_lock(obj);
14510 obj->framebuffer_references--;
14511 i915_gem_object_unlock(obj);
Chris Wilson24dbf512017-02-15 10:59:18 +000014512 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014513}
14514
Jesse Barnes79e53942008-11-07 14:24:08 -080014515static struct drm_framebuffer *
14516intel_user_framebuffer_create(struct drm_device *dev,
14517 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014518 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014519{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014520 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014521 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014522 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014523
Chris Wilson03ac0642016-07-20 13:31:51 +010014524 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14525 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014526 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014527
Chris Wilson24dbf512017-02-15 10:59:18 +000014528 fb = intel_framebuffer_create(obj, &mode_cmd);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014529 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010014530 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014531
14532 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014533}
14534
Chris Wilson778e23a2016-12-05 14:29:39 +000014535static void intel_atomic_state_free(struct drm_atomic_state *state)
14536{
14537 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14538
14539 drm_atomic_state_default_release(state);
14540
14541 i915_sw_fence_fini(&intel_state->commit_ready);
14542
14543 kfree(state);
14544}
14545
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014546static enum drm_mode_status
14547intel_mode_valid(struct drm_device *dev,
14548 const struct drm_display_mode *mode)
14549{
Ville Syrjäläad77c532018-06-15 20:44:05 +030014550 struct drm_i915_private *dev_priv = to_i915(dev);
14551 int hdisplay_max, htotal_max;
14552 int vdisplay_max, vtotal_max;
14553
Ville Syrjäläe4dd27a2018-05-24 15:54:03 +030014554 /*
14555 * Can't reject DBLSCAN here because Xorg ddxen can add piles
14556 * of DBLSCAN modes to the output's mode list when they detect
14557 * the scaling mode property on the connector. And they don't
14558 * ask the kernel to validate those modes in any way until
14559 * modeset time at which point the client gets a protocol error.
14560 * So in order to not upset those clients we silently ignore the
14561 * DBLSCAN flag on such connectors. For other connectors we will
14562 * reject modes with the DBLSCAN flag in encoder->compute_config().
14563 * And we always reject DBLSCAN modes in connector->mode_valid()
14564 * as we never want such modes on the connector's mode list.
14565 */
14566
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014567 if (mode->vscan > 1)
14568 return MODE_NO_VSCAN;
14569
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014570 if (mode->flags & DRM_MODE_FLAG_HSKEW)
14571 return MODE_H_ILLEGAL;
14572
14573 if (mode->flags & (DRM_MODE_FLAG_CSYNC |
14574 DRM_MODE_FLAG_NCSYNC |
14575 DRM_MODE_FLAG_PCSYNC))
14576 return MODE_HSYNC;
14577
14578 if (mode->flags & (DRM_MODE_FLAG_BCAST |
14579 DRM_MODE_FLAG_PIXMUX |
14580 DRM_MODE_FLAG_CLKDIV2))
14581 return MODE_BAD;
14582
Ville Syrjäläad77c532018-06-15 20:44:05 +030014583 if (INTEL_GEN(dev_priv) >= 9 ||
14584 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
14585 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
14586 vdisplay_max = 4096;
14587 htotal_max = 8192;
14588 vtotal_max = 8192;
14589 } else if (INTEL_GEN(dev_priv) >= 3) {
14590 hdisplay_max = 4096;
14591 vdisplay_max = 4096;
14592 htotal_max = 8192;
14593 vtotal_max = 8192;
14594 } else {
14595 hdisplay_max = 2048;
14596 vdisplay_max = 2048;
14597 htotal_max = 4096;
14598 vtotal_max = 4096;
14599 }
14600
14601 if (mode->hdisplay > hdisplay_max ||
14602 mode->hsync_start > htotal_max ||
14603 mode->hsync_end > htotal_max ||
14604 mode->htotal > htotal_max)
14605 return MODE_H_ILLEGAL;
14606
14607 if (mode->vdisplay > vdisplay_max ||
14608 mode->vsync_start > vtotal_max ||
14609 mode->vsync_end > vtotal_max ||
14610 mode->vtotal > vtotal_max)
14611 return MODE_V_ILLEGAL;
14612
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014613 return MODE_OK;
14614}
14615
Jesse Barnes79e53942008-11-07 14:24:08 -080014616static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014617 .fb_create = intel_user_framebuffer_create,
Ville Syrjäläbbfb6ce2017-08-01 09:58:12 -070014618 .get_format_info = intel_get_format_info,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014619 .output_poll_changed = intel_fbdev_output_poll_changed,
Ville Syrjäläe995ca0b2017-11-14 20:32:58 +020014620 .mode_valid = intel_mode_valid,
Matt Roper5ee67f12015-01-21 16:35:44 -080014621 .atomic_check = intel_atomic_check,
14622 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014623 .atomic_state_alloc = intel_atomic_state_alloc,
14624 .atomic_state_clear = intel_atomic_state_clear,
Chris Wilson778e23a2016-12-05 14:29:39 +000014625 .atomic_state_free = intel_atomic_state_free,
Jesse Barnes79e53942008-11-07 14:24:08 -080014626};
14627
Imre Deak88212942016-03-16 13:38:53 +020014628/**
14629 * intel_init_display_hooks - initialize the display modesetting hooks
14630 * @dev_priv: device private
14631 */
14632void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014633{
Ville Syrjälä7ff89ca2017-02-07 20:33:05 +020014634 intel_init_cdclk_hooks(dev_priv);
14635
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +000014636 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014637 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014638 dev_priv->display.get_initial_plane_config =
14639 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014640 dev_priv->display.crtc_compute_clock =
14641 haswell_crtc_compute_clock;
14642 dev_priv->display.crtc_enable = haswell_crtc_enable;
14643 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014644 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014645 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014646 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014647 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014648 dev_priv->display.crtc_compute_clock =
14649 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014650 dev_priv->display.crtc_enable = haswell_crtc_enable;
14651 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014652 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014653 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014654 dev_priv->display.get_initial_plane_config =
Ville Syrjälä81894b22017-11-17 21:19:13 +020014655 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014656 dev_priv->display.crtc_compute_clock =
14657 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014658 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14659 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014660 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014661 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014662 dev_priv->display.get_initial_plane_config =
14663 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014664 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14665 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14666 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14667 } else if (IS_VALLEYVIEW(dev_priv)) {
14668 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14669 dev_priv->display.get_initial_plane_config =
14670 i9xx_get_initial_plane_config;
14671 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014672 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14673 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014674 } else if (IS_G4X(dev_priv)) {
14675 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14676 dev_priv->display.get_initial_plane_config =
14677 i9xx_get_initial_plane_config;
14678 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14679 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14680 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014681 } else if (IS_PINEVIEW(dev_priv)) {
14682 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14683 dev_priv->display.get_initial_plane_config =
14684 i9xx_get_initial_plane_config;
14685 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14686 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14687 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014688 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014689 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014690 dev_priv->display.get_initial_plane_config =
14691 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014692 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014693 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14694 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014695 } else {
14696 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14697 dev_priv->display.get_initial_plane_config =
14698 i9xx_get_initial_plane_config;
14699 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14700 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14701 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014702 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014703
Imre Deak88212942016-03-16 13:38:53 +020014704 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014705 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014706 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014707 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014708 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014709 /* FIXME: detect B0+ stepping and use auto training */
14710 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014711 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014712 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014713 }
14714
Rodrigo Vivibd30ca22017-09-26 14:13:46 -070014715 if (INTEL_GEN(dev_priv) >= 9)
Lyude27082492016-08-24 07:48:10 +020014716 dev_priv->display.update_crtcs = skl_update_crtcs;
14717 else
14718 dev_priv->display.update_crtcs = intel_update_crtcs;
Jesse Barnese70236a2009-09-21 10:42:27 -070014719}
14720
Jesse Barnesb690e962010-07-19 13:53:12 -070014721/*
Keith Packard435793d2011-07-12 14:56:22 -070014722 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14723 */
14724static void quirk_ssc_force_disable(struct drm_device *dev)
14725{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014726 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070014727 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014728 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014729}
14730
Carsten Emde4dca20e2012-03-15 15:56:26 +010014731/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014732 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14733 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014734 */
14735static void quirk_invert_brightness(struct drm_device *dev)
14736{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014737 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010014738 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014739 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014740}
14741
Scot Doyle9c72cc62014-07-03 23:27:50 +000014742/* Some VBT's incorrectly indicate no backlight is present */
14743static void quirk_backlight_present(struct drm_device *dev)
14744{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014745 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000014746 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14747 DRM_INFO("applying backlight present quirk\n");
14748}
14749
Manasi Navarec99a2592017-06-30 09:33:48 -070014750/* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
14751 * which is 300 ms greater than eDP spec T12 min.
14752 */
14753static void quirk_increase_t12_delay(struct drm_device *dev)
14754{
14755 struct drm_i915_private *dev_priv = to_i915(dev);
14756
14757 dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
14758 DRM_INFO("Applying T12 delay quirk\n");
14759}
14760
Clint Taylor90c3e212018-07-10 13:02:05 -070014761/*
14762 * GeminiLake NUC HDMI outputs require additional off time
14763 * this allows the onboard retimer to correctly sync to signal
14764 */
14765static void quirk_increase_ddi_disabled_time(struct drm_device *dev)
14766{
14767 struct drm_i915_private *dev_priv = to_i915(dev);
14768
14769 dev_priv->quirks |= QUIRK_INCREASE_DDI_DISABLED_TIME;
14770 DRM_INFO("Applying Increase DDI Disabled quirk\n");
14771}
14772
Jesse Barnesb690e962010-07-19 13:53:12 -070014773struct intel_quirk {
14774 int device;
14775 int subsystem_vendor;
14776 int subsystem_device;
14777 void (*hook)(struct drm_device *dev);
14778};
14779
Egbert Eich5f85f172012-10-14 15:46:38 +020014780/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14781struct intel_dmi_quirk {
14782 void (*hook)(struct drm_device *dev);
14783 const struct dmi_system_id (*dmi_id_list)[];
14784};
14785
14786static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14787{
14788 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14789 return 1;
14790}
14791
14792static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14793 {
14794 .dmi_id_list = &(const struct dmi_system_id[]) {
14795 {
14796 .callback = intel_dmi_reverse_brightness,
14797 .ident = "NCR Corporation",
14798 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14799 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14800 },
14801 },
14802 { } /* terminating entry */
14803 },
14804 .hook = quirk_invert_brightness,
14805 },
14806};
14807
Ben Widawskyc43b5632012-04-16 14:07:40 -070014808static struct intel_quirk intel_quirks[] = {
Keith Packard435793d2011-07-12 14:56:22 -070014809 /* Lenovo U160 cannot use SSC on LVDS */
14810 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014811
14812 /* Sony Vaio Y cannot use SSC on LVDS */
14813 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014814
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014815 /* Acer Aspire 5734Z must invert backlight brightness */
14816 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14817
14818 /* Acer/eMachines G725 */
14819 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14820
14821 /* Acer/eMachines e725 */
14822 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14823
14824 /* Acer/Packard Bell NCL20 */
14825 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14826
14827 /* Acer Aspire 4736Z */
14828 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014829
14830 /* Acer Aspire 5336 */
14831 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014832
14833 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14834 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014835
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014836 /* Acer C720 Chromebook (Core i3 4005U) */
14837 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14838
jens steinb2a96012014-10-28 20:25:53 +010014839 /* Apple Macbook 2,1 (Core 2 T7400) */
14840 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14841
Jani Nikula1b9448b02015-11-05 11:49:59 +020014842 /* Apple Macbook 4,1 */
14843 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14844
Scot Doyled4967d82014-07-03 23:27:52 +000014845 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14846 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014847
14848 /* HP Chromebook 14 (Celeron 2955U) */
14849 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014850
14851 /* Dell Chromebook 11 */
14852 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020014853
14854 /* Dell Chromebook 11 (2015 version) */
14855 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Manasi Navarec99a2592017-06-30 09:33:48 -070014856
14857 /* Toshiba Satellite P50-C-18C */
14858 { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
Clint Taylor90c3e212018-07-10 13:02:05 -070014859
14860 /* GeminiLake NUC */
14861 { 0x3185, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
14862 { 0x3184, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
14863 /* ASRock ITX*/
14864 { 0x3185, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
14865 { 0x3184, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
Jesse Barnesb690e962010-07-19 13:53:12 -070014866};
14867
14868static void intel_init_quirks(struct drm_device *dev)
14869{
14870 struct pci_dev *d = dev->pdev;
14871 int i;
14872
14873 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14874 struct intel_quirk *q = &intel_quirks[i];
14875
14876 if (d->device == q->device &&
14877 (d->subsystem_vendor == q->subsystem_vendor ||
14878 q->subsystem_vendor == PCI_ANY_ID) &&
14879 (d->subsystem_device == q->subsystem_device ||
14880 q->subsystem_device == PCI_ANY_ID))
14881 q->hook(dev);
14882 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014883 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14884 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14885 intel_dmi_quirks[i].hook(dev);
14886 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014887}
14888
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014889/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000014890static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014891{
David Weinehall52a05c32016-08-22 13:32:44 +030014892 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014893 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010014894 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014895
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014896 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030014897 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014898 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014899 sr1 = inb(VGA_SR_DATA);
14900 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030014901 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014902 udelay(300);
14903
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014904 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014905 POSTING_READ(vga_reg);
14906}
14907
Daniel Vetterf8175862012-04-10 15:50:11 +020014908void intel_modeset_init_hw(struct drm_device *dev)
14909{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014910 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014911
Ville Syrjälä4c75b942016-10-31 22:37:12 +020014912 intel_update_cdclk(dev_priv);
Ville Syrjäläcfddadc2017-10-24 12:52:16 +030014913 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +020014914 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
Daniel Vetterf8175862012-04-10 15:50:11 +020014915}
14916
Matt Roperd93c0372015-12-03 11:37:41 -080014917/*
14918 * Calculate what we think the watermarks should be for the state we've read
14919 * out of the hardware and then immediately program those watermarks so that
14920 * we ensure the hardware settings match our internal state.
14921 *
14922 * We can calculate what we think WM's should be by creating a duplicate of the
14923 * current state (which was constructed during hardware readout) and running it
14924 * through the atomic check code to calculate new watermark values in the
14925 * state object.
14926 */
14927static void sanitize_watermarks(struct drm_device *dev)
14928{
14929 struct drm_i915_private *dev_priv = to_i915(dev);
14930 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014931 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014932 struct drm_crtc *crtc;
14933 struct drm_crtc_state *cstate;
14934 struct drm_modeset_acquire_ctx ctx;
14935 int ret;
14936 int i;
14937
14938 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080014939 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080014940 return;
14941
14942 /*
14943 * We need to hold connection_mutex before calling duplicate_state so
14944 * that the connector loop is protected.
14945 */
14946 drm_modeset_acquire_init(&ctx, 0);
14947retry:
Matt Roper0cd12622016-01-12 07:13:37 -080014948 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080014949 if (ret == -EDEADLK) {
14950 drm_modeset_backoff(&ctx);
14951 goto retry;
14952 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080014953 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014954 }
14955
14956 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14957 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080014958 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080014959
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014960 intel_state = to_intel_atomic_state(state);
14961
Matt Ropered4a6a72016-02-23 17:20:13 -080014962 /*
14963 * Hardware readout is the only time we don't want to calculate
14964 * intermediate watermarks (since we don't trust the current
14965 * watermarks).
14966 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020014967 if (!HAS_GMCH_DISPLAY(dev_priv))
14968 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080014969
Matt Roperd93c0372015-12-03 11:37:41 -080014970 ret = intel_atomic_check(dev, state);
14971 if (ret) {
14972 /*
14973 * If we fail here, it means that the hardware appears to be
14974 * programmed in a way that shouldn't be possible, given our
14975 * understanding of watermark requirements. This might mean a
14976 * mistake in the hardware readout code or a mistake in the
14977 * watermark calculations for a given platform. Raise a WARN
14978 * so that this is noticeable.
14979 *
14980 * If this actually happens, we'll have to just leave the
14981 * BIOS-programmed watermarks untouched and hope for the best.
14982 */
14983 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014984 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080014985 }
14986
14987 /* Write calculated watermark values back */
Maarten Lankhorstaa5e9b42017-03-09 15:52:04 +010014988 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roperd93c0372015-12-03 11:37:41 -080014989 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14990
Matt Ropered4a6a72016-02-23 17:20:13 -080014991 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014992 dev_priv->display.optimize_watermarks(intel_state, cs);
Maarten Lankhorst556fe362017-11-10 12:34:53 +010014993
14994 to_intel_crtc_state(crtc->state)->wm = cs->wm;
Matt Roperd93c0372015-12-03 11:37:41 -080014995 }
14996
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020014997put_state:
Chris Wilson08536952016-10-14 13:18:18 +010014998 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080014999fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015000 drm_modeset_drop_locks(&ctx);
15001 drm_modeset_acquire_fini(&ctx);
15002}
15003
Chris Wilson58ecd9d2017-11-05 13:49:05 +000015004static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
15005{
15006 if (IS_GEN5(dev_priv)) {
15007 u32 fdi_pll_clk =
15008 I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
15009
15010 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
15011 } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
15012 dev_priv->fdi_pll_freq = 270000;
15013 } else {
15014 return;
15015 }
15016
15017 DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
15018}
15019
Azhar Shaikh516a49c2018-07-06 11:37:30 -070015020static int intel_initial_commit(struct drm_device *dev)
15021{
15022 struct drm_atomic_state *state = NULL;
15023 struct drm_modeset_acquire_ctx ctx;
15024 struct drm_crtc *crtc;
15025 struct drm_crtc_state *crtc_state;
15026 int ret = 0;
15027
15028 state = drm_atomic_state_alloc(dev);
15029 if (!state)
15030 return -ENOMEM;
15031
15032 drm_modeset_acquire_init(&ctx, 0);
15033
15034retry:
15035 state->acquire_ctx = &ctx;
15036
15037 drm_for_each_crtc(crtc, dev) {
15038 crtc_state = drm_atomic_get_crtc_state(state, crtc);
15039 if (IS_ERR(crtc_state)) {
15040 ret = PTR_ERR(crtc_state);
15041 goto out;
15042 }
15043
15044 if (crtc_state->active) {
15045 ret = drm_atomic_add_affected_planes(state, crtc);
15046 if (ret)
15047 goto out;
15048 }
15049 }
15050
15051 ret = drm_atomic_commit(state);
15052
15053out:
15054 if (ret == -EDEADLK) {
15055 drm_atomic_state_clear(state);
15056 drm_modeset_backoff(&ctx);
15057 goto retry;
15058 }
15059
15060 drm_atomic_state_put(state);
15061
15062 drm_modeset_drop_locks(&ctx);
15063 drm_modeset_acquire_fini(&ctx);
15064
15065 return ret;
15066}
15067
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015068int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080015069{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015070 struct drm_i915_private *dev_priv = to_i915(dev);
15071 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015072 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015073 struct intel_crtc *crtc;
Azhar Shaikh516a49c2018-07-06 11:37:30 -070015074 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015075
Ville Syrjälä757fffc2017-11-13 15:36:22 +020015076 dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
15077
Jesse Barnes79e53942008-11-07 14:24:08 -080015078 drm_mode_config_init(dev);
15079
15080 dev->mode_config.min_width = 0;
15081 dev->mode_config.min_height = 0;
15082
Dave Airlie019d96c2011-09-29 16:20:42 +010015083 dev->mode_config.preferred_depth = 24;
15084 dev->mode_config.prefer_shadow = 1;
15085
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015086 dev->mode_config.allow_fb_modifiers = true;
15087
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015088 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015089
Andrea Arcangeli400c19d2017-04-07 01:23:45 +020015090 init_llist_head(&dev_priv->atomic_helper.free_list);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015091 INIT_WORK(&dev_priv->atomic_helper.free_work,
Chris Wilsonba318c62017-02-02 20:47:41 +000015092 intel_atomic_helper_free_state_worker);
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015093
Jesse Barnesb690e962010-07-19 13:53:12 -070015094 intel_init_quirks(dev);
15095
Ville Syrjälä62d75df2016-10-31 22:37:25 +020015096 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015097
Lukas Wunner69f92f62015-07-15 13:57:35 +020015098 /*
15099 * There may be no VBT; and if the BIOS enabled SSC we can
15100 * just keep using it to avoid unnecessary flicker. Whereas if the
15101 * BIOS isn't using it, don't assume it will work even if the VBT
15102 * indicates as much.
15103 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015104 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020015105 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15106 DREF_SSC1_ENABLE);
15107
15108 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15109 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15110 bios_lvds_use_ssc ? "en" : "dis",
15111 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15112 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15113 }
15114 }
15115
Ville Syrjäläad77c532018-06-15 20:44:05 +030015116 /* maximum framebuffer dimensions */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015117 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015118 dev->mode_config.max_width = 2048;
15119 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015120 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015121 dev->mode_config.max_width = 4096;
15122 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015123 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015124 dev->mode_config.max_width = 8192;
15125 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015126 }
Damien Lespiau068be562014-03-28 14:17:49 +000015127
Jani Nikula2a307c22016-11-30 17:43:04 +020015128 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15129 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015130 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015131 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä98fac1d2018-06-15 20:44:04 +030015132 dev->mode_config.cursor_width = 64;
15133 dev->mode_config.cursor_height = 64;
Damien Lespiau068be562014-03-28 14:17:49 +000015134 } else {
Ville Syrjälä98fac1d2018-06-15 20:44:04 +030015135 dev->mode_config.cursor_width = 256;
15136 dev->mode_config.cursor_height = 256;
Damien Lespiau068be562014-03-28 14:17:49 +000015137 }
15138
Matthew Auld73ebd502017-12-11 15:18:20 +000015139 dev->mode_config.fb_base = ggtt->gmadr.start;
Jesse Barnes79e53942008-11-07 14:24:08 -080015140
Zhao Yakui28c97732009-10-09 11:39:41 +080015141 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015142 INTEL_INFO(dev_priv)->num_pipes,
15143 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015144
Damien Lespiau055e3932014-08-18 13:49:10 +010015145 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015146 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015147 if (ret) {
15148 drm_mode_config_cleanup(dev);
15149 return ret;
15150 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015151 }
15152
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015153 intel_shared_dpll_init(dev);
Chris Wilson58ecd9d2017-11-05 13:49:05 +000015154 intel_update_fdi_pll_freq(dev_priv);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015155
Ville Syrjälä5be6e332017-02-20 16:04:43 +020015156 intel_update_czclk(dev_priv);
15157 intel_modeset_init_hw(dev);
15158
Ville Syrjäläb2045352016-05-13 23:41:27 +030015159 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020015160 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030015161
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015162 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015163 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015164 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000015165
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015166 drm_modeset_lock_all(dev);
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015167 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015168 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015169
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015170 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015171 struct intel_initial_plane_config plane_config = {};
15172
Jesse Barnes46f297f2014-03-07 08:57:48 -080015173 if (!crtc->active)
15174 continue;
15175
Jesse Barnes46f297f2014-03-07 08:57:48 -080015176 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015177 * Note that reserving the BIOS fb up front prevents us
15178 * from stuffing other stolen allocations like the ring
15179 * on top. This prevents some ugliness at boot time, and
15180 * can even allow for smooth boot transitions if the BIOS
15181 * fb is large enough for the active pipe configuration.
15182 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015183 dev_priv->display.get_initial_plane_config(crtc,
15184 &plane_config);
15185
15186 /*
15187 * If the fb is shared between multiple heads, we'll
15188 * just get the first one.
15189 */
15190 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015191 }
Matt Roperd93c0372015-12-03 11:37:41 -080015192
15193 /*
15194 * Make sure hardware watermarks really match the state we read out.
15195 * Note that we need to do this after reconstructing the BIOS fb's
15196 * since the watermark calculation done here will use pstate->fb.
15197 */
Ville Syrjälä602ae832017-03-02 19:15:02 +020015198 if (!HAS_GMCH_DISPLAY(dev_priv))
15199 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015200
Azhar Shaikh516a49c2018-07-06 11:37:30 -070015201 /*
15202 * Force all active planes to recompute their states. So that on
15203 * mode_setcrtc after probe, all the intel_plane_state variables
15204 * are already calculated and there is no assert_plane warnings
15205 * during bootup.
15206 */
15207 ret = intel_initial_commit(dev);
15208 if (ret)
15209 DRM_DEBUG_KMS("Initial commit in probe failed.\n");
15210
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015211 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010015212}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015213
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015214void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15215{
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020015216 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015217 /* 640x480@60Hz, ~25175 kHz */
15218 struct dpll clock = {
15219 .m1 = 18,
15220 .m2 = 7,
15221 .p1 = 13,
15222 .p2 = 4,
15223 .n = 2,
15224 };
15225 u32 dpll, fp;
15226 int i;
15227
15228 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
15229
15230 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
15231 pipe_name(pipe), clock.vco, clock.dot);
15232
15233 fp = i9xx_dpll_compute_fp(&clock);
15234 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
15235 DPLL_VGA_MODE_DIS |
15236 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
15237 PLL_P2_DIVIDE_BY_4 |
15238 PLL_REF_INPUT_DREFCLK |
15239 DPLL_VCO_ENABLE;
15240
15241 I915_WRITE(FP0(pipe), fp);
15242 I915_WRITE(FP1(pipe), fp);
15243
15244 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
15245 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
15246 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
15247 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
15248 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
15249 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
15250 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
15251
15252 /*
15253 * Apparently we need to have VGA mode enabled prior to changing
15254 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
15255 * dividers, even though the register value does change.
15256 */
15257 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
15258 I915_WRITE(DPLL(pipe), dpll);
15259
15260 /* Wait for the clocks to stabilize. */
15261 POSTING_READ(DPLL(pipe));
15262 udelay(150);
15263
15264 /* The pixel multiplier can only be updated once the
15265 * DPLL is enabled and the clocks are stable.
15266 *
15267 * So write it again.
15268 */
15269 I915_WRITE(DPLL(pipe), dpll);
15270
15271 /* We do this three times for luck */
15272 for (i = 0; i < 3 ; i++) {
15273 I915_WRITE(DPLL(pipe), dpll);
15274 POSTING_READ(DPLL(pipe));
15275 udelay(150); /* wait for warmup */
15276 }
15277
15278 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
15279 POSTING_READ(PIPECONF(pipe));
Ville Syrjäläd5fb43c2017-11-29 17:37:31 +020015280
15281 intel_wait_for_pipe_scanline_moving(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015282}
15283
15284void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15285{
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015286 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15287
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015288 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
15289 pipe_name(pipe));
15290
Ville Syrjälä5816d9c2017-11-29 14:54:11 +020015291 WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
15292 WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
15293 WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
Ville Syrjäläb99b9ec2018-01-31 16:37:09 +020015294 WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
15295 WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015296
15297 I915_WRITE(PIPECONF(pipe), 0);
15298 POSTING_READ(PIPECONF(pipe));
15299
Ville Syrjälä8fedd642017-11-29 17:37:30 +020015300 intel_wait_for_pipe_scanline_stopped(crtc);
Ville Syrjälä2ee0da12017-06-01 17:36:16 +030015301
15302 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
15303 POSTING_READ(DPLL(pipe));
15304}
15305
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015306static void
15307intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
15308{
15309 struct intel_crtc *crtc;
Daniel Vetterfa555832012-10-10 23:14:00 +020015310
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015311 if (INTEL_GEN(dev_priv) >= 4)
15312 return;
Daniel Vetterfa555832012-10-10 23:14:00 +020015313
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015314 for_each_intel_crtc(&dev_priv->drm, crtc) {
15315 struct intel_plane *plane =
15316 to_intel_plane(crtc->base.primary);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015317 struct intel_crtc *plane_crtc;
15318 enum pipe pipe;
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015319
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015320 if (!plane->get_hw_state(plane, &pipe))
15321 continue;
15322
15323 if (pipe == crtc->pipe)
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015324 continue;
15325
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +030015326 DRM_DEBUG_KMS("[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
15327 plane->base.base.id, plane->base.name);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015328
15329 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15330 intel_plane_disable_noatomic(plane_crtc, plane);
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015331 }
Daniel Vetterfa555832012-10-10 23:14:00 +020015332}
15333
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015334static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15335{
15336 struct drm_device *dev = crtc->base.dev;
15337 struct intel_encoder *encoder;
15338
15339 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15340 return true;
15341
15342 return false;
15343}
15344
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015345static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15346{
15347 struct drm_device *dev = encoder->base.dev;
15348 struct intel_connector *connector;
15349
15350 for_each_connector_on_encoder(dev, &encoder->base, connector)
15351 return connector;
15352
15353 return NULL;
15354}
15355
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015356static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015357 enum pipe pch_transcoder)
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015358{
15359 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015360 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015361}
15362
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015363static void intel_sanitize_crtc(struct intel_crtc *crtc,
15364 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter24929352012-07-02 20:28:59 +020015365{
15366 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015367 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015368 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015369
Daniel Vetter24929352012-07-02 20:28:59 +020015370 /* Clear any frame start delays used for debugging left by the BIOS */
Ville Syrjälä738a8142017-11-15 22:04:42 +020015371 if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020015372 i915_reg_t reg = PIPECONF(cpu_transcoder);
15373
15374 I915_WRITE(reg,
15375 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15376 }
Daniel Vetter24929352012-07-02 20:28:59 +020015377
Ville Syrjäläd297e102014-08-06 14:50:01 +030015378 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015379 struct intel_plane *plane;
15380
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015381 /* Disable everything but the primary plane */
15382 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015383 const struct intel_plane_state *plane_state =
15384 to_intel_plane_state(plane->base.state);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015385
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015386 if (plane_state->base.visible &&
15387 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
15388 intel_plane_disable_noatomic(crtc, plane);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015389 }
Daniel Vetter96256042015-02-13 21:03:42 +010015390 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015391
Daniel Vetter24929352012-07-02 20:28:59 +020015392 /* Adjust the state of the output pipe according to whether we
15393 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015394 if (crtc->active && !intel_crtc_has_encoders(crtc))
Ville Syrjäläda1d0e22017-06-01 17:36:14 +030015395 intel_crtc_disable_noatomic(&crtc->base, ctx);
Daniel Vetter24929352012-07-02 20:28:59 +020015396
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010015397 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015398 /*
15399 * We start out with underrun reporting disabled to avoid races.
15400 * For correct bookkeeping mark this on active crtcs.
15401 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015402 * Also on gmch platforms we dont have any hardware bits to
15403 * disable the underrun reporting. Which means we need to start
15404 * out with underrun reporting disabled also on inactive pipes,
15405 * since otherwise we'll complain about the garbage we read when
15406 * e.g. coming up after runtime pm.
15407 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015408 * No protection against concurrent access is required - at
15409 * worst a fifo underrun happens which also sets this to false.
15410 */
15411 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015412 /*
15413 * We track the PCH trancoder underrun reporting state
15414 * within the crtc. With crtc for pipe A housing the underrun
15415 * reporting state for PCH transcoder A, crtc for pipe B housing
15416 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15417 * and marking underrun reporting as disabled for the non-existing
15418 * PCH transcoders B and C would prevent enabling the south
15419 * error interrupt (see cpt_can_enable_serr_int()).
15420 */
Ville Syrjäläecf837d92017-10-10 15:55:56 +030015421 if (has_pch_trancoder(dev_priv, crtc->pipe))
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030015422 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010015423 }
Daniel Vetter24929352012-07-02 20:28:59 +020015424}
15425
15426static void intel_sanitize_encoder(struct intel_encoder *encoder)
15427{
15428 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020015429
15430 /* We need to check both for a crtc link (meaning that the
15431 * encoder is active and trying to read from a pipe) and the
15432 * pipe itself being active. */
15433 bool has_active_crtc = encoder->base.crtc &&
15434 to_intel_crtc(encoder->base.crtc)->active;
15435
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020015436 connector = intel_encoder_find_connector(encoder);
15437 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015438 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15439 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015440 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015441
15442 /* Connector is active, but has no active pipe. This is
15443 * fallout from our resume register restoring. Disable
15444 * the encoder manually again. */
15445 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015446 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15447
Daniel Vetter24929352012-07-02 20:28:59 +020015448 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15449 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015450 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015451 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015452 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015453 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020015454 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015455 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015456
15457 /* Inconsistent output/port/pipe state happens presumably due to
15458 * a bug in one of the get_hw_state functions. Or someplace else
15459 * in our code, like the register restore mess on resume. Clamp
15460 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020015461
15462 connector->base.dpms = DRM_MODE_DPMS_OFF;
15463 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015464 }
Maarten Lankhorstd6cae4a2018-05-16 10:50:38 +020015465
15466 /* notify opregion of the sanitized encoder state */
15467 intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015468}
15469
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015470void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015471{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015472 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015473
Imre Deak04098752014-02-18 00:02:16 +020015474 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15475 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015476 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020015477 }
15478}
15479
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015480void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020015481{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015482 /* This function can be called both from intel_modeset_setup_hw_state or
15483 * at a very early point in our resume sequence, where the power well
15484 * structures are not yet restored. Since this function is at a very
15485 * paranoid "someone might have enabled VGA while we were not looking"
15486 * level, just check if the power well is enabled instead of trying to
15487 * follow the "don't touch the power well if we don't need it" policy
15488 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015489 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015490 return;
15491
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000015492 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020015493
15494 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015495}
15496
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015497/* FIXME read out full plane state for all planes */
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015498static void readout_plane_state(struct drm_i915_private *dev_priv)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015499{
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015500 struct intel_plane *plane;
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015501 struct intel_crtc *crtc;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015502
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015503 for_each_intel_plane(&dev_priv->drm, plane) {
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015504 struct intel_plane_state *plane_state =
15505 to_intel_plane_state(plane->base.state);
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015506 struct intel_crtc_state *crtc_state;
15507 enum pipe pipe = PIPE_A;
Ville Syrjäläeade6c82018-01-30 22:38:03 +020015508 bool visible;
15509
15510 visible = plane->get_hw_state(plane, &pipe);
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015511
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015512 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15513 crtc_state = to_intel_crtc_state(crtc->base.state);
15514
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015515 intel_set_plane_visible(crtc_state, plane_state, visible);
Ville Syrjälä7a4a2a42018-10-03 17:50:52 +030015516
15517 DRM_DEBUG_KMS("[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
15518 plane->base.base.id, plane->base.name,
15519 enableddisabled(visible), pipe_name(pipe));
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015520 }
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015521
15522 for_each_intel_crtc(&dev_priv->drm, crtc) {
15523 struct intel_crtc_state *crtc_state =
15524 to_intel_crtc_state(crtc->base.state);
15525
15526 fixup_active_planes(crtc_state);
15527 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015528}
15529
Daniel Vetter30e984d2013-06-05 13:34:17 +020015530static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015531{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015532 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015533 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015534 struct intel_crtc *crtc;
15535 struct intel_encoder *encoder;
15536 struct intel_connector *connector;
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015537 struct drm_connector_list_iter conn_iter;
Daniel Vetter53589012013-06-05 13:34:16 +020015538 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015539
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015540 dev_priv->active_crtcs = 0;
15541
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015542 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015543 struct intel_crtc_state *crtc_state =
15544 to_intel_crtc_state(crtc->base.state);
Daniel Vetter3b117c82013-04-17 20:15:07 +020015545
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015546 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015547 memset(crtc_state, 0, sizeof(*crtc_state));
15548 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015549
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015550 crtc_state->base.active = crtc_state->base.enable =
15551 dev_priv->display.get_pipe_config(crtc, crtc_state);
15552
15553 crtc->base.enabled = crtc_state->base.enable;
15554 crtc->active = crtc_state->base.active;
15555
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015556 if (crtc_state->base.active)
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015557 dev_priv->active_crtcs |= 1 << crtc->pipe;
15558
Ville Syrjälä78108b72016-05-27 20:59:19 +030015559 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15560 crtc->base.base.id, crtc->base.name,
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015561 enableddisabled(crtc_state->base.active));
Daniel Vetter24929352012-07-02 20:28:59 +020015562 }
15563
Ville Syrjälä62358aa2018-10-03 17:50:17 +030015564 readout_plane_state(dev_priv);
15565
Daniel Vetter53589012013-06-05 13:34:16 +020015566 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15567 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15568
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015569 pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
15570 &pll->state.hw_state);
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015571 pll->state.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015572 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015573 struct intel_crtc_state *crtc_state =
15574 to_intel_crtc_state(crtc->base.state);
15575
15576 if (crtc_state->base.active &&
15577 crtc_state->shared_dpll == pll)
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015578 pll->state.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015579 }
Ander Conselvan de Oliveira2c42e532016-12-29 17:22:09 +020015580 pll->active_mask = pll->state.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015581
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015582 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015583 pll->info->name, pll->state.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015584 }
15585
Damien Lespiaub2784e12014-08-05 11:29:37 +010015586 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015587 pipe = 0;
15588
15589 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015590 struct intel_crtc_state *crtc_state;
15591
Ville Syrjälä98187832016-10-31 22:37:10 +020015592 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015593 crtc_state = to_intel_crtc_state(crtc->base.state);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015594
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015595 encoder->base.crtc = &crtc->base;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015596 encoder->get_config(encoder, crtc_state);
Daniel Vetter24929352012-07-02 20:28:59 +020015597 } else {
15598 encoder->base.crtc = NULL;
15599 }
15600
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015601 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015602 encoder->base.base.id, encoder->base.name,
15603 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015604 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015605 }
15606
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015607 drm_connector_list_iter_begin(dev, &conn_iter);
15608 for_each_intel_connector_iter(connector, &conn_iter) {
Daniel Vetter24929352012-07-02 20:28:59 +020015609 if (connector->get_hw_state(connector)) {
15610 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015611
15612 encoder = connector->encoder;
15613 connector->base.encoder = &encoder->base;
15614
15615 if (encoder->base.crtc &&
15616 encoder->base.crtc->state->active) {
15617 /*
15618 * This has to be done during hardware readout
15619 * because anything calling .crtc_disable may
15620 * rely on the connector_mask being accurate.
15621 */
15622 encoder->base.crtc->state->connector_mask |=
Ville Syrjälä40560e22018-06-26 22:47:11 +030015623 drm_connector_mask(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015624 encoder->base.crtc->state->encoder_mask |=
Ville Syrjälä40560e22018-06-26 22:47:11 +030015625 drm_encoder_mask(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015626 }
15627
Daniel Vetter24929352012-07-02 20:28:59 +020015628 } else {
15629 connector->base.dpms = DRM_MODE_DPMS_OFF;
15630 connector->base.encoder = NULL;
15631 }
15632 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000015633 connector->base.base.id, connector->base.name,
15634 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020015635 }
Daniel Vetterf9e905c2017-03-01 10:52:25 +010015636 drm_connector_list_iter_end(&conn_iter);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015637
15638 for_each_intel_crtc(dev, crtc) {
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015639 struct intel_crtc_state *crtc_state =
15640 to_intel_crtc_state(crtc->base.state);
Ville Syrjäläd305e062017-08-30 21:57:03 +030015641 int min_cdclk = 0;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015642
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015643 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015644 if (crtc_state->base.active) {
15645 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
Ville Syrjäläbd4cd032018-04-26 19:30:15 +030015646 crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
15647 crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015648 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015649 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15650
15651 /*
15652 * The initial mode needs to be set in order to keep
15653 * the atomic core happy. It wants a valid mode if the
15654 * crtc's enabled, so we do the above call.
15655 *
Daniel Vetter7800fb62016-12-19 09:24:23 +010015656 * But we don't set all the derived state fully, hence
15657 * set a flag to indicate that a full recalculation is
15658 * needed on the next commit.
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015659 */
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015660 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015661
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +020015662 intel_crtc_compute_pixel_rate(crtc_state);
15663
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015664 if (dev_priv->display.modeset_calc_cdclk) {
Ville Syrjäläd305e062017-08-30 21:57:03 +030015665 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
Ville Syrjälä9c61de42017-07-10 22:33:47 +030015666 if (WARN_ON(min_cdclk < 0))
15667 min_cdclk = 0;
15668 }
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015669
Daniel Vetter5caa0fe2017-05-09 16:03:29 +020015670 drm_calc_timestamping_constants(&crtc->base,
15671 &crtc_state->base.adjusted_mode);
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015672 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015673 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015674
Ville Syrjäläd305e062017-08-30 21:57:03 +030015675 dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
Ville Syrjälä53e9bf52017-10-24 12:52:14 +030015676 dev_priv->min_voltage_level[crtc->pipe] =
15677 crtc_state->min_voltage_level;
Ville Syrjäläaca1ebf2016-12-20 17:39:02 +020015678
Ville Syrjäläa8cd6da2016-12-22 16:04:41 +020015679 intel_pipe_config_sanity_check(dev_priv, crtc_state);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015680 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015681}
15682
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015683static void
15684get_encoder_power_domains(struct drm_i915_private *dev_priv)
15685{
15686 struct intel_encoder *encoder;
15687
15688 for_each_intel_encoder(&dev_priv->drm, encoder) {
15689 u64 get_domains;
15690 enum intel_display_power_domain domain;
Imre Deak52528052018-06-21 21:44:49 +030015691 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015692
15693 if (!encoder->get_power_domains)
15694 continue;
15695
Imre Deak52528052018-06-21 21:44:49 +030015696 /*
Imre Deakb79ebe72018-07-05 15:26:54 +030015697 * MST-primary and inactive encoders don't have a crtc state
15698 * and neither of these require any power domain references.
Imre Deak52528052018-06-21 21:44:49 +030015699 */
Imre Deakb79ebe72018-07-05 15:26:54 +030015700 if (!encoder->base.crtc)
15701 continue;
Imre Deak52528052018-06-21 21:44:49 +030015702
Imre Deakb79ebe72018-07-05 15:26:54 +030015703 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
Imre Deak52528052018-06-21 21:44:49 +030015704 get_domains = encoder->get_power_domains(encoder, crtc_state);
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015705 for_each_power_domain(domain, get_domains)
15706 intel_display_power_get(dev_priv, domain);
15707 }
15708}
15709
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015710static void intel_early_display_was(struct drm_i915_private *dev_priv)
15711{
15712 /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
15713 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
15714 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
15715 DARBF_GATING_DIS);
15716
15717 if (IS_HASWELL(dev_priv)) {
15718 /*
15719 * WaRsPkgCStateDisplayPMReq:hsw
15720 * System hang if this isn't done before disabling all planes!
15721 */
15722 I915_WRITE(CHICKEN_PAR1_1,
15723 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
15724 }
15725}
15726
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015727/* Scan out the current hw modeset state,
15728 * and sanitizes it to the current state
15729 */
15730static void
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015731intel_modeset_setup_hw_state(struct drm_device *dev,
15732 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015733{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015734 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015735 struct intel_crtc *crtc;
15736 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015737 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015738
Imre Deak2cd9a682018-08-16 15:37:57 +030015739 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
15740
Rodrigo Vividf49ec82017-11-10 16:03:19 -080015741 intel_early_display_was(dev_priv);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015742 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015743
15744 /* HW state is read out, now we need to sanitize this mess. */
Ander Conselvan de Oliveira62b69562017-02-24 16:19:59 +020015745 get_encoder_power_domains(dev_priv);
15746
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015747 /*
15748 * intel_sanitize_plane_mapping() may need to do vblank
15749 * waits, so we need vblank interrupts restored beforehand.
15750 */
15751 for_each_intel_crtc(&dev_priv->drm, crtc) {
15752 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläb1e01592017-11-17 21:19:09 +020015753
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015754 if (crtc->active)
15755 drm_crtc_vblank_on(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015756 }
15757
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015758 intel_sanitize_plane_mapping(dev_priv);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015759
Ville Syrjälä68bc30d2018-10-03 17:49:51 +030015760 for_each_intel_encoder(dev, encoder)
15761 intel_sanitize_encoder(encoder);
15762
15763 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläaecd36b2017-06-01 17:36:13 +030015764 intel_sanitize_crtc(crtc, ctx);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015765 intel_dump_pipe_config(crtc, crtc->config,
15766 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015767 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015768
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015769 intel_modeset_update_connector_atomic_state(dev);
15770
Daniel Vetter35c95372013-07-17 06:55:04 +020015771 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15772 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15773
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015774 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015775 continue;
15776
Lucas De Marchi72f775f2018-03-20 15:06:34 -070015777 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
15778 pll->info->name);
Daniel Vetter35c95372013-07-17 06:55:04 +020015779
Lucas De Marchiee1398b2018-03-20 15:06:33 -070015780 pll->info->funcs->disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015781 pll->on = false;
15782 }
15783
Ville Syrjälä04548cb2017-04-21 21:14:29 +030015784 if (IS_G4X(dev_priv)) {
15785 g4x_wm_get_hw_state(dev);
15786 g4x_wm_sanitize(dev_priv);
15787 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015788 vlv_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015789 vlv_wm_sanitize(dev_priv);
Rodrigo Vivia029fa42017-08-09 13:52:48 -070015790 } else if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat30789992014-11-04 17:06:45 +000015791 skl_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015792 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015793 ilk_wm_get_hw_state(dev);
Ville Syrjälä602ae832017-03-02 19:15:02 +020015794 }
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015795
15796 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveirad8fc70b2017-02-09 11:31:21 +020015797 u64 put_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015798
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015799 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015800 if (WARN_ON(put_domains))
15801 modeset_put_power_domains(dev_priv, put_domains);
15802 }
Imre Deak2cd9a682018-08-16 15:37:57 +030015803
15804 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015805
15806 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015807}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015808
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015809void intel_display_resume(struct drm_device *dev)
15810{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015811 struct drm_i915_private *dev_priv = to_i915(dev);
15812 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15813 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015814 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015815
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015816 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030015817 if (state)
15818 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015819
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015820 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015821
Maarten Lankhorst73974892016-08-05 23:28:27 +030015822 while (1) {
15823 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15824 if (ret != -EDEADLK)
15825 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015826
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015827 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015828 }
15829
Maarten Lankhorst73974892016-08-05 23:28:27 +030015830 if (!ret)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +010015831 ret = __intel_display_resume(dev, state, &ctx);
Maarten Lankhorst73974892016-08-05 23:28:27 +030015832
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +053015833 intel_enable_ipc(dev_priv);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015834 drm_modeset_drop_locks(&ctx);
15835 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015836
Chris Wilson08536952016-10-14 13:18:18 +010015837 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015838 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson3c5e37f2017-01-15 12:58:25 +000015839 if (state)
15840 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015841}
15842
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010015843int intel_connector_register(struct drm_connector *connector)
15844{
15845 struct intel_connector *intel_connector = to_intel_connector(connector);
15846 int ret;
15847
15848 ret = intel_backlight_device_register(intel_connector);
15849 if (ret)
15850 goto err;
15851
15852 return 0;
15853
15854err:
15855 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015856}
15857
Chris Wilsonc191eca2016-06-17 11:40:33 +010015858void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020015859{
Chris Wilsone63d87c2016-06-17 11:40:34 +010015860 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015861
Chris Wilsone63d87c2016-06-17 11:40:34 +010015862 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015863 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015864}
15865
Manasi Navare886c6b82017-10-26 14:52:00 -070015866static void intel_hpd_poll_fini(struct drm_device *dev)
15867{
15868 struct intel_connector *connector;
15869 struct drm_connector_list_iter conn_iter;
15870
Chris Wilson448aa912017-11-28 11:01:47 +000015871 /* Kill all the work that may have been queued by hpd. */
Manasi Navare886c6b82017-10-26 14:52:00 -070015872 drm_connector_list_iter_begin(dev, &conn_iter);
15873 for_each_intel_connector_iter(connector, &conn_iter) {
15874 if (connector->modeset_retry_work.func)
15875 cancel_work_sync(&connector->modeset_retry_work);
Sean Paulee5e5e72018-01-08 14:55:39 -050015876 if (connector->hdcp_shim) {
15877 cancel_delayed_work_sync(&connector->hdcp_check_work);
15878 cancel_work_sync(&connector->hdcp_prop_work);
15879 }
Manasi Navare886c6b82017-10-26 14:52:00 -070015880 }
15881 drm_connector_list_iter_end(&conn_iter);
15882}
15883
Jesse Barnes79e53942008-11-07 14:24:08 -080015884void intel_modeset_cleanup(struct drm_device *dev)
15885{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015886 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070015887
Chris Wilson8bcf9f72018-07-10 10:44:20 +010015888 flush_workqueue(dev_priv->modeset_wq);
15889
Chris Wilsoneb955ee2017-01-23 21:29:39 +000015890 flush_work(&dev_priv->atomic_helper.free_work);
15891 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15892
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015893 /*
15894 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015895 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015896 * experience fancy races otherwise.
15897 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015898 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015899
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015900 /*
15901 * Due to the hpd irq storm handling the hotplug work can re-arm the
15902 * poll handlers. Hence disable polling after hpd handling is shut down.
15903 */
Manasi Navare886c6b82017-10-26 14:52:00 -070015904 intel_hpd_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015905
Daniel Vetter4f256d82017-07-15 00:46:55 +020015906 /* poll work can call into fbdev, hence clean that up afterwards */
15907 intel_fbdev_fini(dev_priv);
15908
Jesse Barnes723bfd72010-10-07 16:01:13 -070015909 intel_unregister_dsm_handler();
15910
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020015911 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015912
Chris Wilson1630fe72011-07-08 12:22:42 +010015913 /* flush any delayed tasks or pending work */
15914 flush_scheduled_work();
15915
Jesse Barnes79e53942008-11-07 14:24:08 -080015916 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015917
Chris Wilson1ee8da62016-05-12 12:43:23 +010015918 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015919
Tvrtko Ursulin40196442016-12-01 14:16:42 +000015920 intel_teardown_gmbus(dev_priv);
Ville Syrjälä757fffc2017-11-13 15:36:22 +020015921
15922 destroy_workqueue(dev_priv->modeset_wq);
Jesse Barnes79e53942008-11-07 14:24:08 -080015923}
15924
Chris Wilsondf0e9242010-09-09 16:20:55 +010015925void intel_connector_attach_encoder(struct intel_connector *connector,
15926 struct intel_encoder *encoder)
15927{
15928 connector->encoder = encoder;
Daniel Vettercde4c442018-07-09 10:40:07 +020015929 drm_connector_attach_encoder(&connector->base, &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015930}
Dave Airlie28d52042009-09-21 14:33:58 +100015931
15932/*
15933 * set vga decode state - true == enable VGA decode
15934 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015935int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100015936{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015937 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015938 u16 gmch_ctrl;
15939
Chris Wilson75fa0412014-02-07 18:37:02 -020015940 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15941 DRM_ERROR("failed to read control word\n");
15942 return -EIO;
15943 }
15944
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015945 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15946 return 0;
15947
Dave Airlie28d52042009-09-21 14:33:58 +100015948 if (state)
15949 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15950 else
15951 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015952
15953 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15954 DRM_ERROR("failed to write control word\n");
15955 return -EIO;
15956 }
15957
Dave Airlie28d52042009-09-21 14:33:58 +100015958 return 0;
15959}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015960
Chris Wilson98a2f412016-10-12 10:05:18 +010015961#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15962
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015963struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015964
15965 u32 power_well_driver;
15966
Chris Wilson63b66e52013-08-08 15:12:06 +020015967 int num_transcoders;
15968
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015969 struct intel_cursor_error_state {
15970 u32 control;
15971 u32 position;
15972 u32 base;
15973 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015974 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015975
15976 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015977 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015978 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015979 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015980 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015981
15982 struct intel_plane_error_state {
15983 u32 control;
15984 u32 stride;
15985 u32 size;
15986 u32 pos;
15987 u32 addr;
15988 u32 surface;
15989 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015990 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015991
15992 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015993 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015994 enum transcoder cpu_transcoder;
15995
15996 u32 conf;
15997
15998 u32 htotal;
15999 u32 hblank;
16000 u32 hsync;
16001 u32 vtotal;
16002 u32 vblank;
16003 u32 vsync;
16004 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016005};
16006
16007struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016008intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016009{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016010 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016011 int transcoders[] = {
16012 TRANSCODER_A,
16013 TRANSCODER_B,
16014 TRANSCODER_C,
16015 TRANSCODER_EDP,
16016 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016017 int i;
16018
Chris Wilsonc0336662016-05-06 15:40:21 +010016019 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016020 return NULL;
16021
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016022 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016023 if (error == NULL)
16024 return NULL;
16025
Chris Wilsonc0336662016-05-06 15:40:21 +010016026 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Imre Deak75e39682018-08-06 12:58:39 +030016027 error->power_well_driver = I915_READ(HSW_PWR_WELL_CTL2);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016028
Damien Lespiau055e3932014-08-18 13:49:10 +010016029 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016030 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016031 __intel_display_power_is_enabled(dev_priv,
16032 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016033 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016034 continue;
16035
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016036 error->cursor[i].control = I915_READ(CURCNTR(i));
16037 error->cursor[i].position = I915_READ(CURPOS(i));
16038 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016039
16040 error->plane[i].control = I915_READ(DSPCNTR(i));
16041 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016042 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016043 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016044 error->plane[i].pos = I915_READ(DSPPOS(i));
16045 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016046 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016047 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016048 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016049 error->plane[i].surface = I915_READ(DSPSURF(i));
16050 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16051 }
16052
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016053 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016054
Chris Wilsonc0336662016-05-06 15:40:21 +010016055 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030016056 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016057 }
16058
Jani Nikula4d1de972016-03-18 17:05:42 +020016059 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016060 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016061 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016062 error->num_transcoders++; /* Account for eDP. */
16063
16064 for (i = 0; i < error->num_transcoders; i++) {
16065 enum transcoder cpu_transcoder = transcoders[i];
16066
Imre Deakddf9c532013-11-27 22:02:02 +020016067 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016068 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016069 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016070 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016071 continue;
16072
Chris Wilson63b66e52013-08-08 15:12:06 +020016073 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16074
16075 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16076 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16077 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16078 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16079 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16080 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16081 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016082 }
16083
16084 return error;
16085}
16086
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016087#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16088
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016089void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016090intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016091 struct intel_display_error_state *error)
16092{
Chris Wilson5a4c6f12017-02-14 16:46:11 +000016093 struct drm_i915_private *dev_priv = m->i915;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016094 int i;
16095
Chris Wilson63b66e52013-08-08 15:12:06 +020016096 if (!error)
16097 return;
16098
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016099 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010016100 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016101 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016102 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016103 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016104 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016105 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016106 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016107 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016108 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016109
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016110 err_printf(m, "Plane [%d]:\n", i);
16111 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16112 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000016113 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016114 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16115 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016116 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010016117 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016118 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000016119 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016120 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16121 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016122 }
16123
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016124 err_printf(m, "Cursor [%d]:\n", i);
16125 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16126 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16127 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016128 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016129
16130 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016131 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016132 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016133 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016134 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016135 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16136 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16137 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16138 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16139 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16140 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16141 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16142 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016143}
Chris Wilson98a2f412016-10-12 10:05:18 +010016144
16145#endif