blob: b01faf50fe426f675827c1c7aa1e0c8806ee89ee [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"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Ville Syrjälä65edccc2016-10-31 22:37:01 +0200118static void skl_init_scalers(struct drm_i915_private *dev_priv,
119 struct intel_crtc *crtc,
120 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200124static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +0200127static int glk_calc_cdclk(int max_pixclk);
Imre Deak324513c2016-06-13 16:44:36 +0300128static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100129
Ma Lingd4906092009-03-18 20:13:27 +0800130struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300131 struct {
132 int min, max;
133 } dot, vco, n, m, m1, m2, p, p1;
134
135 struct {
136 int dot_limit;
137 int p2_slow, p2_fast;
138 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800139};
Jesse Barnes79e53942008-11-07 14:24:08 -0800140
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300141/* returns HPLL frequency in kHz */
142static int valleyview_get_vco(struct drm_i915_private *dev_priv)
143{
144 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
145
146 /* Obtain SKU information */
147 mutex_lock(&dev_priv->sb_lock);
148 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
149 CCK_FUSE_HPLL_FREQ_MASK;
150 mutex_unlock(&dev_priv->sb_lock);
151
152 return vco_freq[hpll_freq] * 1000;
153}
154
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200155int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
156 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300157{
158 u32 val;
159 int divider;
160
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300161 mutex_lock(&dev_priv->sb_lock);
162 val = vlv_cck_read(dev_priv, reg);
163 mutex_unlock(&dev_priv->sb_lock);
164
165 divider = val & CCK_FREQUENCY_VALUES;
166
167 WARN((val & CCK_FREQUENCY_STATUS) !=
168 (divider << CCK_FREQUENCY_STATUS_SHIFT),
169 "%s change in progress\n", name);
170
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200171 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
172}
173
174static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
175 const char *name, u32 reg)
176{
177 if (dev_priv->hpll_freq == 0)
178 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
179
180 return vlv_get_cck_clock(dev_priv, name, reg,
181 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300182}
183
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200184static int
185intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200186{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200187 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200188}
189
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200190static int
191intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300192{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300193 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200194 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
195 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200196}
197
198static int
199intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
200{
Jani Nikula79e50a42015-08-26 10:58:20 +0300201 uint32_t clkcfg;
202
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200203 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300204 clkcfg = I915_READ(CLKCFG);
205 switch (clkcfg & CLKCFG_FSB_MASK) {
206 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200217 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300218 /* these two are just a guess; one of them might be right */
219 case CLKCFG_FSB_1600:
220 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300222 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200223 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300224 }
225}
226
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300227void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200228{
229 if (HAS_PCH_SPLIT(dev_priv))
230 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
231 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
233 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
234 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
235 else
236 return; /* no rawclk on other platforms, or no need to know it */
237
238 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
239}
240
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300241static void intel_update_czclk(struct drm_i915_private *dev_priv)
242{
Wayne Boyer666a4532015-12-09 12:29:35 -0800243 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300244 return;
245
246 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
247 CCK_CZ_CLOCK_CONTROL);
248
249 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
250}
251
Chris Wilson021357a2010-09-07 20:54:59 +0100252static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200253intel_fdi_link_freq(struct drm_i915_private *dev_priv,
254 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100255{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200256 if (HAS_DDI(dev_priv))
257 return pipe_config->port_clock; /* SPLL */
258 else if (IS_GEN5(dev_priv))
259 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200260 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200261 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100262}
263
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300264static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400265 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200266 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200267 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700275};
276
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300277static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200278 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200279 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200280 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 2, .max = 33 },
286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 4, .p2_fast = 4 },
288};
289
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300290static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400291 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200292 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200293 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .m = { .min = 96, .max = 140 },
295 .m1 = { .min = 18, .max = 26 },
296 .m2 = { .min = 6, .max = 16 },
297 .p = { .min = 4, .max = 128 },
298 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700299 .p2 = { .dot_limit = 165000,
300 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700301};
Eric Anholt273e27c2011-03-30 13:01:10 -0700302
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300303static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400310 .p = { .min = 5, .max = 80 },
311 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .p2 = { .dot_limit = 200000,
313 .p2_slow = 10, .p2_fast = 5 },
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_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400317 .dot = { .min = 20000, .max = 400000 },
318 .vco = { .min = 1400000, .max = 2800000 },
319 .n = { .min = 1, .max = 6 },
320 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100321 .m1 = { .min = 8, .max = 18 },
322 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400323 .p = { .min = 7, .max = 98 },
324 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700325 .p2 = { .dot_limit = 112000,
326 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700327};
328
Eric Anholt273e27c2011-03-30 13:01:10 -0700329
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300330static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700331 .dot = { .min = 25000, .max = 270000 },
332 .vco = { .min = 1750000, .max = 3500000},
333 .n = { .min = 1, .max = 4 },
334 .m = { .min = 104, .max = 138 },
335 .m1 = { .min = 17, .max = 23 },
336 .m2 = { .min = 5, .max = 11 },
337 .p = { .min = 10, .max = 30 },
338 .p1 = { .min = 1, .max = 3},
339 .p2 = { .dot_limit = 270000,
340 .p2_slow = 10,
341 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800342 },
Keith Packarde4b36692009-06-05 19:22:17 -0700343};
344
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300345static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700346 .dot = { .min = 22000, .max = 400000 },
347 .vco = { .min = 1750000, .max = 3500000},
348 .n = { .min = 1, .max = 4 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 16, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 5, .max = 80 },
353 .p1 = { .min = 1, .max = 8},
354 .p2 = { .dot_limit = 165000,
355 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700356};
357
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300358static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700359 .dot = { .min = 20000, .max = 115000 },
360 .vco = { .min = 1750000, .max = 3500000 },
361 .n = { .min = 1, .max = 3 },
362 .m = { .min = 104, .max = 138 },
363 .m1 = { .min = 17, .max = 23 },
364 .m2 = { .min = 5, .max = 11 },
365 .p = { .min = 28, .max = 112 },
366 .p1 = { .min = 2, .max = 8 },
367 .p2 = { .dot_limit = 0,
368 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800369 },
Keith Packarde4b36692009-06-05 19:22:17 -0700370};
371
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300372static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700373 .dot = { .min = 80000, .max = 224000 },
374 .vco = { .min = 1750000, .max = 3500000 },
375 .n = { .min = 1, .max = 3 },
376 .m = { .min = 104, .max = 138 },
377 .m1 = { .min = 17, .max = 23 },
378 .m2 = { .min = 5, .max = 11 },
379 .p = { .min = 14, .max = 42 },
380 .p1 = { .min = 2, .max = 6 },
381 .p2 = { .dot_limit = 0,
382 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800383 },
Keith Packarde4b36692009-06-05 19:22:17 -0700384};
385
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300386static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400387 .dot = { .min = 20000, .max = 400000},
388 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700389 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400390 .n = { .min = 3, .max = 6 },
391 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700392 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 5, .max = 80 },
396 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700397 .p2 = { .dot_limit = 200000,
398 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700399};
400
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300401static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400402 .dot = { .min = 20000, .max = 400000 },
403 .vco = { .min = 1700000, .max = 3500000 },
404 .n = { .min = 3, .max = 6 },
405 .m = { .min = 2, .max = 256 },
406 .m1 = { .min = 0, .max = 0 },
407 .m2 = { .min = 0, .max = 254 },
408 .p = { .min = 7, .max = 112 },
409 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700410 .p2 = { .dot_limit = 112000,
411 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700412};
413
Eric Anholt273e27c2011-03-30 13:01:10 -0700414/* Ironlake / Sandybridge
415 *
416 * We calculate clock using (register_value + 2) for N/M1/M2, so here
417 * the range value for them is (actual_value - 2).
418 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300419static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 5 },
423 .m = { .min = 79, .max = 127 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 5, .max = 80 },
427 .p1 = { .min = 1, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700430};
431
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300432static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 118 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 28, .max = 112 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800443};
444
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300445static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700446 .dot = { .min = 25000, .max = 350000 },
447 .vco = { .min = 1760000, .max = 3510000 },
448 .n = { .min = 1, .max = 3 },
449 .m = { .min = 79, .max = 127 },
450 .m1 = { .min = 12, .max = 22 },
451 .m2 = { .min = 5, .max = 9 },
452 .p = { .min = 14, .max = 56 },
453 .p1 = { .min = 2, .max = 8 },
454 .p2 = { .dot_limit = 225000,
455 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800456};
457
Eric Anholt273e27c2011-03-30 13:01:10 -0700458/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300459static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 2 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800470};
471
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300472static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700473 .dot = { .min = 25000, .max = 350000 },
474 .vco = { .min = 1760000, .max = 3510000 },
475 .n = { .min = 1, .max = 3 },
476 .m = { .min = 79, .max = 126 },
477 .m1 = { .min = 12, .max = 22 },
478 .m2 = { .min = 5, .max = 9 },
479 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400480 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700481 .p2 = { .dot_limit = 225000,
482 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800483};
484
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300485static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300486 /*
487 * These are the data rate limits (measured in fast clocks)
488 * since those are the strictest limits we have. The fast
489 * clock and actual rate limits are more relaxed, so checking
490 * them would make no difference.
491 */
492 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200493 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700494 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700495 .m1 = { .min = 2, .max = 3 },
496 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300497 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300498 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700499};
500
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300501static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300502 /*
503 * These are the data rate limits (measured in fast clocks)
504 * since those are the strictest limits we have. The fast
505 * clock and actual rate limits are more relaxed, so checking
506 * them would make no difference.
507 */
508 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200509 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300510 .n = { .min = 1, .max = 1 },
511 .m1 = { .min = 2, .max = 2 },
512 .m2 = { .min = 24 << 22, .max = 175 << 22 },
513 .p1 = { .min = 2, .max = 4 },
514 .p2 = { .p2_slow = 1, .p2_fast = 14 },
515};
516
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300517static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200518 /* FIXME: find real dot limits */
519 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530520 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200521 .n = { .min = 1, .max = 1 },
522 .m1 = { .min = 2, .max = 2 },
523 /* FIXME: find real m2 limits */
524 .m2 = { .min = 2 << 22, .max = 255 << 22 },
525 .p1 = { .min = 2, .max = 4 },
526 .p2 = { .p2_slow = 1, .p2_fast = 20 },
527};
528
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200529static bool
530needs_modeset(struct drm_crtc_state *state)
531{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200532 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200533}
534
Imre Deakdccbea32015-06-22 23:35:51 +0300535/*
536 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
537 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
538 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
539 * The helpers' return value is the rate of the clock that is fed to the
540 * display engine's pipe which can be the above fast dot clock rate or a
541 * divided-down version of it.
542 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500543/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300544static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800545{
Shaohua Li21778322009-02-23 15:19:16 +0800546 clock->m = clock->m2 + 2;
547 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200548 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300549 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300550 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
551 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300552
553 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800554}
555
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200556static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
557{
558 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
559}
560
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300561static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800562{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200563 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800564 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200565 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300566 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300567 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
568 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300569
570 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800571}
572
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300573static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300574{
575 clock->m = clock->m1 * clock->m2;
576 clock->p = clock->p1 * clock->p2;
577 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300578 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300579 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
580 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300581
582 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300583}
584
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300585int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300586{
587 clock->m = clock->m1 * clock->m2;
588 clock->p = clock->p1 * clock->p2;
589 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300590 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300591 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
592 clock->n << 22);
593 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300594
595 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300596}
597
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800598#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800599/**
600 * Returns whether the given set of divisors are valid for a given refclk with
601 * the given connectors.
602 */
603
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100604static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300605 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300606 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800607{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300608 if (clock->n < limit->n.min || limit->n.max < clock->n)
609 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300616
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100617 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200618 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300619 if (clock->m1 <= clock->m2)
620 INTELPllInvalid("m1 <= m2\n");
621
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100622 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200623 !IS_GEN9_LP(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300624 if (clock->p < limit->p.min || limit->p.max < clock->p)
625 INTELPllInvalid("p out of range\n");
626 if (clock->m < limit->m.min || limit->m.max < clock->m)
627 INTELPllInvalid("m out of range\n");
628 }
629
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400631 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800637
638 return true;
639}
640
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300641static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300642i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300643 const struct intel_crtc_state *crtc_state,
644 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800645{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300646 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800647
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300648 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100650 * For LVDS just rely on its current settings for dual-channel.
651 * We haven't figured out how to reliably set up different
652 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100654 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300655 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300657 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 } else {
659 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300662 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300664}
665
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200666/*
667 * Returns a set of divisors for the desired target clock with the given
668 * refclk, or FALSE. The returned values represent the clock equation:
669 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
670 *
671 * Target and reference clocks are specified in kHz.
672 *
673 * If match_clock is provided, then best_clock P divider must match the P
674 * divider from @match_clock used for LVDS downclocking.
675 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300676static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300677i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300678 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300679 int target, int refclk, struct dpll *match_clock,
680 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681{
682 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300683 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300684 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800685
Akshay Joshi0206e352011-08-16 15:34:10 -0400686 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800687
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300688 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
689
Zhao Yakui42158662009-11-20 11:24:18 +0800690 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
691 clock.m1++) {
692 for (clock.m2 = limit->m2.min;
693 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200694 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800695 break;
696 for (clock.n = limit->n.min;
697 clock.n <= limit->n.max; clock.n++) {
698 for (clock.p1 = limit->p1.min;
699 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800700 int this_err;
701
Imre Deakdccbea32015-06-22 23:35:51 +0300702 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100703 if (!intel_PLL_is_valid(to_i915(dev),
704 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000705 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800707 if (match_clock &&
708 clock.p != match_clock->p)
709 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800710
711 this_err = abs(clock.dot - target);
712 if (this_err < err) {
713 *best_clock = clock;
714 err = this_err;
715 }
716 }
717 }
718 }
719 }
720
721 return (err != target);
722}
723
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200724/*
725 * Returns a set of divisors for the desired target clock with the given
726 * refclk, or FALSE. The returned values represent the clock equation:
727 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
728 *
729 * Target and reference clocks are specified in kHz.
730 *
731 * If match_clock is provided, then best_clock P divider must match the P
732 * divider from @match_clock used for LVDS downclocking.
733 */
Ma Lingd4906092009-03-18 20:13:27 +0800734static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300735pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200736 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300737 int target, int refclk, struct dpll *match_clock,
738 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200739{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300740 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300741 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200742 int err = target;
743
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200744 memset(best_clock, 0, sizeof(*best_clock));
745
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300746 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
747
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200748 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
749 clock.m1++) {
750 for (clock.m2 = limit->m2.min;
751 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200752 for (clock.n = limit->n.min;
753 clock.n <= limit->n.max; clock.n++) {
754 for (clock.p1 = limit->p1.min;
755 clock.p1 <= limit->p1.max; clock.p1++) {
756 int this_err;
757
Imre Deakdccbea32015-06-22 23:35:51 +0300758 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100759 if (!intel_PLL_is_valid(to_i915(dev),
760 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800761 &clock))
762 continue;
763 if (match_clock &&
764 clock.p != match_clock->p)
765 continue;
766
767 this_err = abs(clock.dot - target);
768 if (this_err < err) {
769 *best_clock = clock;
770 err = this_err;
771 }
772 }
773 }
774 }
775 }
776
777 return (err != target);
778}
779
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200780/*
781 * Returns a set of divisors for the desired target clock with the given
782 * refclk, or FALSE. The returned values represent the clock equation:
783 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200784 *
785 * Target and reference clocks are specified in kHz.
786 *
787 * If match_clock is provided, then best_clock P divider must match the P
788 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200789 */
Ma Lingd4906092009-03-18 20:13:27 +0800790static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300791g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200792 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300793 int target, int refclk, struct dpll *match_clock,
794 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800795{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300796 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300797 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800798 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300799 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400800 /* approximately equals target * 0.00585 */
801 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800802
803 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300804
805 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
806
Ma Lingd4906092009-03-18 20:13:27 +0800807 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200808 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800809 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200810 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800811 for (clock.m1 = limit->m1.max;
812 clock.m1 >= limit->m1.min; clock.m1--) {
813 for (clock.m2 = limit->m2.max;
814 clock.m2 >= limit->m2.min; clock.m2--) {
815 for (clock.p1 = limit->p1.max;
816 clock.p1 >= limit->p1.min; clock.p1--) {
817 int this_err;
818
Imre Deakdccbea32015-06-22 23:35:51 +0300819 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100820 if (!intel_PLL_is_valid(to_i915(dev),
821 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000822 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800823 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000824
825 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800826 if (this_err < err_most) {
827 *best_clock = clock;
828 err_most = this_err;
829 max_n = clock.n;
830 found = true;
831 }
832 }
833 }
834 }
835 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800836 return found;
837}
Ma Lingd4906092009-03-18 20:13:27 +0800838
Imre Deakd5dd62b2015-03-17 11:40:03 +0200839/*
840 * Check if the calculated PLL configuration is more optimal compared to the
841 * best configuration and error found so far. Return the calculated error.
842 */
843static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300844 const struct dpll *calculated_clock,
845 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200846 unsigned int best_error_ppm,
847 unsigned int *error_ppm)
848{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200849 /*
850 * For CHV ignore the error and consider only the P value.
851 * Prefer a bigger P value based on HW requirements.
852 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100853 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200854 *error_ppm = 0;
855
856 return calculated_clock->p > best_clock->p;
857 }
858
Imre Deak24be4e42015-03-17 11:40:04 +0200859 if (WARN_ON_ONCE(!target_freq))
860 return false;
861
Imre Deakd5dd62b2015-03-17 11:40:03 +0200862 *error_ppm = div_u64(1000000ULL *
863 abs(target_freq - calculated_clock->dot),
864 target_freq);
865 /*
866 * Prefer a better P value over a better (smaller) error if the error
867 * is small. Ensure this preference for future configurations too by
868 * setting the error to 0.
869 */
870 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
871 *error_ppm = 0;
872
873 return true;
874 }
875
876 return *error_ppm + 10 < best_error_ppm;
877}
878
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200879/*
880 * Returns a set of divisors for the desired target clock with the given
881 * refclk, or FALSE. The returned values represent the clock equation:
882 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
883 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800884static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300885vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200886 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300887 int target, int refclk, struct dpll *match_clock,
888 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700889{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200890 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300891 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300892 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300893 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300894 /* min update 19.2 MHz */
895 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300896 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700897
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300898 target *= 5; /* fast clock */
899
900 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700901
902 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300903 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300904 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300905 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300906 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300907 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700908 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300909 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200910 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300911
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300912 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
913 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300914
Imre Deakdccbea32015-06-22 23:35:51 +0300915 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300916
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100917 if (!intel_PLL_is_valid(to_i915(dev),
918 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300919 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300920 continue;
921
Imre Deakd5dd62b2015-03-17 11:40:03 +0200922 if (!vlv_PLL_is_optimal(dev, target,
923 &clock,
924 best_clock,
925 bestppm, &ppm))
926 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300927
Imre Deakd5dd62b2015-03-17 11:40:03 +0200928 *best_clock = clock;
929 bestppm = ppm;
930 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700931 }
932 }
933 }
934 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700935
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300936 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700938
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200939/*
940 * Returns a set of divisors for the desired target clock with the given
941 * refclk, or FALSE. The returned values represent the clock equation:
942 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
943 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300944static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300945chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200946 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300947 int target, int refclk, struct dpll *match_clock,
948 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300949{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200950 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300951 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200952 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300953 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300954 uint64_t m2;
955 int found = false;
956
957 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200958 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300959
960 /*
961 * Based on hardware doc, the n always set to 1, and m1 always
962 * set to 2. If requires to support 200Mhz refclk, we need to
963 * revisit this because n may not 1 anymore.
964 */
965 clock.n = 1, clock.m1 = 2;
966 target *= 5; /* fast clock */
967
968 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
969 for (clock.p2 = limit->p2.p2_fast;
970 clock.p2 >= limit->p2.p2_slow;
971 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200972 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300973
974 clock.p = clock.p1 * clock.p2;
975
976 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
977 clock.n) << 22, refclk * clock.m1);
978
979 if (m2 > INT_MAX/clock.m1)
980 continue;
981
982 clock.m2 = m2;
983
Imre Deakdccbea32015-06-22 23:35:51 +0300984 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300985
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100986 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300987 continue;
988
Imre Deak9ca3ba02015-03-17 11:40:05 +0200989 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
990 best_error_ppm, &error_ppm))
991 continue;
992
993 *best_clock = clock;
994 best_error_ppm = error_ppm;
995 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300996 }
997 }
998
999 return found;
1000}
1001
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001002bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001003 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001004{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001005 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001006 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001007
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001008 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001009 target_clock, refclk, NULL, best_clock);
1010}
1011
Ville Syrjälä525b9312016-10-31 22:37:02 +02001012bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001013{
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001014 /* Be paranoid as we can arrive here with only partial
1015 * state retrieved from the hardware during setup.
1016 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001017 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001018 * as Haswell has gained clock readout/fastboot support.
1019 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001020 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001021 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001022 *
1023 * FIXME: The intel_crtc->active here should be switched to
1024 * crtc->state->active once we have proper CRTC states wired up
1025 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001026 */
Ville Syrjälä525b9312016-10-31 22:37:02 +02001027 return crtc->active && crtc->base.primary->state->fb &&
1028 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001029}
1030
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001031enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1032 enum pipe pipe)
1033{
Ville Syrjälä98187832016-10-31 22:37:10 +02001034 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001035
Ville Syrjäläe2af48c2016-10-31 22:37:05 +02001036 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001037}
1038
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001039static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001040{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001041 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001042 u32 line1, line2;
1043 u32 line_mask;
1044
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001045 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001046 line_mask = DSL_LINEMASK_GEN2;
1047 else
1048 line_mask = DSL_LINEMASK_GEN3;
1049
1050 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001051 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001052 line2 = I915_READ(reg) & line_mask;
1053
1054 return line1 == line2;
1055}
1056
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057/*
1058 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001059 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001060 *
1061 * After disabling a pipe, we can't wait for vblank in the usual way,
1062 * spinning on the vblank interrupt status bit, since we won't actually
1063 * see an interrupt when the pipe is disabled.
1064 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 * On Gen4 and above:
1066 * wait for the pipe register state bit to turn off
1067 *
1068 * Otherwise:
1069 * wait for the display line value to settle (it usually
1070 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001071 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001072 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001073static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001074{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001076 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001077 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001078
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001079 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001080 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001081
Keith Packardab7ad7f2010-10-03 00:33:06 -07001082 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001083 if (intel_wait_for_register(dev_priv,
1084 reg, I965_PIPECONF_ACTIVE, 0,
1085 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001086 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001087 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001088 /* Wait for the display line to settle */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001089 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001090 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001091 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001092}
1093
Jesse Barnesb24e7172011-01-04 15:09:30 -08001094/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001095void assert_pll(struct drm_i915_private *dev_priv,
1096 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001098 u32 val;
1099 bool cur_state;
1100
Ville Syrjälä649636e2015-09-22 19:50:01 +03001101 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001102 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001103 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001104 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001105 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001106}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001107
Jani Nikula23538ef2013-08-27 15:12:22 +03001108/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001109void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001110{
1111 u32 val;
1112 bool cur_state;
1113
Ville Syrjäläa5805162015-05-26 20:42:30 +03001114 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001115 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001116 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001117
1118 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001119 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001120 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001121 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001122}
Jani Nikula23538ef2013-08-27 15:12:22 +03001123
Jesse Barnes040484a2011-01-03 12:14:26 -08001124static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1125 enum pipe pipe, bool state)
1126{
Jesse Barnes040484a2011-01-03 12:14:26 -08001127 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001128 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1129 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001130
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001131 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001132 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001133 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001134 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001135 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001136 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001137 cur_state = !!(val & FDI_TX_ENABLE);
1138 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001139 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001140 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001141 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001142}
1143#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1144#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1145
1146static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1147 enum pipe pipe, bool state)
1148{
Jesse Barnes040484a2011-01-03 12:14:26 -08001149 u32 val;
1150 bool cur_state;
1151
Ville Syrjälä649636e2015-09-22 19:50:01 +03001152 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001153 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001154 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001155 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001156 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
Jesse Barnes040484a2011-01-03 12:14:26 -08001164 u32 val;
1165
1166 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001167 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001168 return;
1169
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001170 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001171 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001172 return;
1173
Ville Syrjälä649636e2015-09-22 19:50:01 +03001174 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001175 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 -08001176}
1177
Daniel Vetter55607e82013-06-16 21:42:39 +02001178void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1179 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001180{
Jesse Barnes040484a2011-01-03 12:14:26 -08001181 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001182 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001183
Ville Syrjälä649636e2015-09-22 19:50:01 +03001184 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001185 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001186 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001187 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001188 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001189}
1190
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001191void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001192{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001193 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001194 u32 val;
1195 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001196 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001197
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001198 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001199 return;
1200
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001201 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001202 u32 port_sel;
1203
Imre Deak44cb7342016-08-10 14:07:29 +03001204 pp_reg = PP_CONTROL(0);
1205 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001206
1207 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1208 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1209 panel_pipe = PIPE_B;
1210 /* XXX: else fix for eDP */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001211 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001212 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001213 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001214 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001215 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001216 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001217 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1218 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001219 }
1220
1221 val = I915_READ(pp_reg);
1222 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001223 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001224 locked = false;
1225
Rob Clarke2c719b2014-12-15 13:56:32 -05001226 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001227 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001228 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001229}
1230
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001231static void assert_cursor(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
1233{
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001234 bool cur_state;
1235
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001236 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001237 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001238 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001239 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001240
Rob Clarke2c719b2014-12-15 13:56:32 -05001241 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001242 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001243 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001244}
1245#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1246#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1247
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001248void assert_pipe(struct drm_i915_private *dev_priv,
1249 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001250{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001251 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001252 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1253 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001254 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001255
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001256 /* if we need the pipe quirk it must be always on */
1257 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1258 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001259 state = true;
1260
Imre Deak4feed0e2016-02-12 18:55:14 +02001261 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1262 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001263 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001264 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001265
1266 intel_display_power_put(dev_priv, power_domain);
1267 } else {
1268 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001269 }
1270
Rob Clarke2c719b2014-12-15 13:56:32 -05001271 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001272 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001273 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001274}
1275
Chris Wilson931872f2012-01-16 23:01:13 +00001276static void assert_plane(struct drm_i915_private *dev_priv,
1277 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001278{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001279 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001280 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001281
Ville Syrjälä649636e2015-09-22 19:50:01 +03001282 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001283 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001284 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001285 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001286 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001287}
1288
Chris Wilson931872f2012-01-16 23:01:13 +00001289#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1290#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1291
Jesse Barnesb24e7172011-01-04 15:09:30 -08001292static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1293 enum pipe pipe)
1294{
Ville Syrjälä649636e2015-09-22 19:50:01 +03001295 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001296
Ville Syrjälä653e1022013-06-04 13:49:05 +03001297 /* Primary planes are fixed to pipes on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001298 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001299 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001300 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001301 "plane %c assertion failure, should be disabled but not\n",
1302 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001303 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001304 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001305
Jesse Barnesb24e7172011-01-04 15:09:30 -08001306 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001307 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001308 u32 val = I915_READ(DSPCNTR(i));
1309 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001310 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001311 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001312 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1313 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314 }
1315}
1316
Jesse Barnes19332d72013-03-28 09:55:38 -07001317static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1318 enum pipe pipe)
1319{
Ville Syrjälä649636e2015-09-22 19:50:01 +03001320 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001321
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001322 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001323 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001324 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001325 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001326 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1327 sprite, pipe_name(pipe));
1328 }
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001329 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001330 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä83c04a62016-11-22 18:02:00 +02001331 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001332 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001333 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001334 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001335 }
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001336 } else if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001337 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001338 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001339 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001340 plane_name(pipe), pipe_name(pipe));
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001341 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001342 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001343 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001344 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1345 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001346 }
1347}
1348
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001349static void assert_vblank_disabled(struct drm_crtc *crtc)
1350{
Rob Clarke2c719b2014-12-15 13:56:32 -05001351 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001352 drm_crtc_vblank_put(crtc);
1353}
1354
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001355void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1356 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001357{
Jesse Barnes92f25842011-01-04 15:09:34 -08001358 u32 val;
1359 bool enabled;
1360
Ville Syrjälä649636e2015-09-22 19:50:01 +03001361 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001362 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001363 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001364 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1365 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001366}
1367
Keith Packard4e634382011-08-06 10:39:45 -07001368static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001370{
1371 if ((val & DP_PORT_EN) == 0)
1372 return false;
1373
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001374 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001375 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001376 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1377 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001378 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001379 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1380 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001381 } else {
1382 if ((val & DP_PIPE_MASK) != (pipe << 30))
1383 return false;
1384 }
1385 return true;
1386}
1387
Keith Packard1519b992011-08-06 10:35:34 -07001388static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1389 enum pipe pipe, u32 val)
1390{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001391 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001392 return false;
1393
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001394 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001395 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001396 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001397 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001398 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1399 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001400 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001401 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001402 return false;
1403 }
1404 return true;
1405}
1406
1407static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1408 enum pipe pipe, u32 val)
1409{
1410 if ((val & LVDS_PORT_EN) == 0)
1411 return false;
1412
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001413 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001414 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1415 return false;
1416 } else {
1417 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1418 return false;
1419 }
1420 return true;
1421}
1422
1423static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1424 enum pipe pipe, u32 val)
1425{
1426 if ((val & ADPA_DAC_ENABLE) == 0)
1427 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001428 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001429 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1430 return false;
1431 } else {
1432 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1433 return false;
1434 }
1435 return true;
1436}
1437
Jesse Barnes291906f2011-02-02 12:28:03 -08001438static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001439 enum pipe pipe, i915_reg_t reg,
1440 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001441{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001442 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001443 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001444 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001445 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001446
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001447 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001448 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001449 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001450}
1451
1452static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001453 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001454{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001455 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001456 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001457 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001458 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001459
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001460 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001461 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001462 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001463}
1464
1465static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe)
1467{
Jesse Barnes291906f2011-02-02 12:28:03 -08001468 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001469
Keith Packardf0575e92011-07-25 22:12:43 -07001470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1472 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001473
Ville Syrjälä649636e2015-09-22 19:50:01 +03001474 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001475 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001476 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001477 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001478
Ville Syrjälä649636e2015-09-22 19:50:01 +03001479 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001480 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001481 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001482 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001483
Paulo Zanonie2debe92013-02-18 19:00:27 -03001484 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1485 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1486 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001487}
1488
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001489static void _vlv_enable_pll(struct intel_crtc *crtc,
1490 const struct intel_crtc_state *pipe_config)
1491{
1492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1493 enum pipe pipe = crtc->pipe;
1494
1495 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1496 POSTING_READ(DPLL(pipe));
1497 udelay(150);
1498
Chris Wilson2c30b432016-06-30 15:32:54 +01001499 if (intel_wait_for_register(dev_priv,
1500 DPLL(pipe),
1501 DPLL_LOCK_VLV,
1502 DPLL_LOCK_VLV,
1503 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001504 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1505}
1506
Ville Syrjäläd288f652014-10-28 13:20:22 +02001507static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001508 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001509{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001510 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001511 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001512
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001513 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001514
Daniel Vetter87442f72013-06-06 00:52:17 +02001515 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001516 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001517
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001518 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1519 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001520
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001521 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1522 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001523}
1524
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001525
1526static void _chv_enable_pll(struct intel_crtc *crtc,
1527 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001528{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001529 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001530 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001531 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001532 u32 tmp;
1533
Ville Syrjäläa5805162015-05-26 20:42:30 +03001534 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535
1536 /* Enable back the 10bit clock to display controller */
1537 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1538 tmp |= DPIO_DCLKP_EN;
1539 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1540
Ville Syrjälä54433e92015-05-26 20:42:31 +03001541 mutex_unlock(&dev_priv->sb_lock);
1542
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001543 /*
1544 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1545 */
1546 udelay(1);
1547
1548 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001549 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001550
1551 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001552 if (intel_wait_for_register(dev_priv,
1553 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1554 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001555 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001556}
1557
1558static void chv_enable_pll(struct intel_crtc *crtc,
1559 const struct intel_crtc_state *pipe_config)
1560{
1561 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1562 enum pipe pipe = crtc->pipe;
1563
1564 assert_pipe_disabled(dev_priv, pipe);
1565
1566 /* PLL is protected by panel, make sure we can write it */
1567 assert_panel_unlocked(dev_priv, pipe);
1568
1569 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1570 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001571
Ville Syrjäläc2317752016-03-15 16:39:56 +02001572 if (pipe != PIPE_A) {
1573 /*
1574 * WaPixelRepeatModeFixForC0:chv
1575 *
1576 * DPLLCMD is AWOL. Use chicken bits to propagate
1577 * the value from DPLLBMD to either pipe B or C.
1578 */
1579 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1580 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1581 I915_WRITE(CBR4_VLV, 0);
1582 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1583
1584 /*
1585 * DPLLB VGA mode also seems to cause problems.
1586 * We should always have it disabled.
1587 */
1588 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1589 } else {
1590 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1591 POSTING_READ(DPLL_MD(pipe));
1592 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001593}
1594
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001595static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001596{
1597 struct intel_crtc *crtc;
1598 int count = 0;
1599
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001600 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001601 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001602 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1603 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001604
1605 return count;
1606}
1607
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001608static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001609{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001611 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001612 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001613
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001614 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001615
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001616 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001617 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001618 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001619
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001620 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001621 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001622 /*
1623 * It appears to be important that we don't enable this
1624 * for the current pipe before otherwise configuring the
1625 * PLL. No idea how this should be handled if multiple
1626 * DVO outputs are enabled simultaneosly.
1627 */
1628 dpll |= DPLL_DVO_2X_MODE;
1629 I915_WRITE(DPLL(!crtc->pipe),
1630 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1631 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001632
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001633 /*
1634 * Apparently we need to have VGA mode enabled prior to changing
1635 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1636 * dividers, even though the register value does change.
1637 */
1638 I915_WRITE(reg, 0);
1639
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001640 I915_WRITE(reg, dpll);
1641
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001642 /* Wait for the clocks to stabilize. */
1643 POSTING_READ(reg);
1644 udelay(150);
1645
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001646 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001647 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001648 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001649 } else {
1650 /* The pixel multiplier can only be updated once the
1651 * DPLL is enabled and the clocks are stable.
1652 *
1653 * So write it again.
1654 */
1655 I915_WRITE(reg, dpll);
1656 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001657
1658 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001659 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001660 POSTING_READ(reg);
1661 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001662 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001663 POSTING_READ(reg);
1664 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001665 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001666 POSTING_READ(reg);
1667 udelay(150); /* wait for warmup */
1668}
1669
1670/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001671 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001672 * @dev_priv: i915 private structure
1673 * @pipe: pipe PLL to disable
1674 *
1675 * Disable the PLL for @pipe, making sure the pipe is off first.
1676 *
1677 * Note! This is for pre-ILK only.
1678 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001679static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001680{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001681 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001682 enum pipe pipe = crtc->pipe;
1683
1684 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001685 if (IS_I830(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001686 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00001687 !intel_num_dvo_pipes(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001688 I915_WRITE(DPLL(PIPE_B),
1689 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1690 I915_WRITE(DPLL(PIPE_A),
1691 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1692 }
1693
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001694 /* Don't disable pipe or pipe PLLs if needed */
1695 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1696 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001697 return;
1698
1699 /* Make sure the pipe isn't still relying on us */
1700 assert_pipe_disabled(dev_priv, pipe);
1701
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001702 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001703 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001704}
1705
Jesse Barnesf6071162013-10-01 10:41:38 -07001706static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1707{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001708 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001709
1710 /* Make sure the pipe isn't still relying on us */
1711 assert_pipe_disabled(dev_priv, pipe);
1712
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001713 val = DPLL_INTEGRATED_REF_CLK_VLV |
1714 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1715 if (pipe != PIPE_A)
1716 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1717
Jesse Barnesf6071162013-10-01 10:41:38 -07001718 I915_WRITE(DPLL(pipe), val);
1719 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001720}
1721
1722static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1723{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001724 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001725 u32 val;
1726
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001727 /* Make sure the pipe isn't still relying on us */
1728 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001729
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001730 val = DPLL_SSC_REF_CLK_CHV |
1731 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001732 if (pipe != PIPE_A)
1733 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001734
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001735 I915_WRITE(DPLL(pipe), val);
1736 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001737
Ville Syrjäläa5805162015-05-26 20:42:30 +03001738 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001739
1740 /* Disable 10bit clock to display controller */
1741 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1742 val &= ~DPIO_DCLKP_EN;
1743 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1744
Ville Syrjäläa5805162015-05-26 20:42:30 +03001745 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001746}
1747
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001748void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001749 struct intel_digital_port *dport,
1750 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001751{
1752 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001753 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001754
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001755 switch (dport->port) {
1756 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001757 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001758 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001759 break;
1760 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001761 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001762 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001763 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001764 break;
1765 case PORT_D:
1766 port_mask = DPLL_PORTD_READY_MASK;
1767 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001768 break;
1769 default:
1770 BUG();
1771 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001772
Chris Wilson370004d2016-06-30 15:32:56 +01001773 if (intel_wait_for_register(dev_priv,
1774 dpll_reg, port_mask, expected_mask,
1775 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001776 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1777 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001778}
1779
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001780static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1781 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001782{
Ville Syrjälä98187832016-10-31 22:37:10 +02001783 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1784 pipe);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001785 i915_reg_t reg;
1786 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001787
Jesse Barnes040484a2011-01-03 12:14:26 -08001788 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001789 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001790
1791 /* FDI must be feeding us bits for PCH ports */
1792 assert_fdi_tx_enabled(dev_priv, pipe);
1793 assert_fdi_rx_enabled(dev_priv, pipe);
1794
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001795 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001796 /* Workaround: Set the timing override bit before enabling the
1797 * pch transcoder. */
1798 reg = TRANS_CHICKEN2(pipe);
1799 val = I915_READ(reg);
1800 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1801 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001802 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001803
Daniel Vetterab9412b2013-05-03 11:49:46 +02001804 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001805 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001806 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001807
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001808 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001809 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001810 * Make the BPC in transcoder be consistent with
1811 * that in pipeconf reg. For HDMI we must use 8bpc
1812 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001813 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001814 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001815 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001816 val |= PIPECONF_8BPC;
1817 else
1818 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001819 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001820
1821 val &= ~TRANS_INTERLACE_MASK;
1822 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001823 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001824 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001825 val |= TRANS_LEGACY_INTERLACED_ILK;
1826 else
1827 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001828 else
1829 val |= TRANS_PROGRESSIVE;
1830
Jesse Barnes040484a2011-01-03 12:14:26 -08001831 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001832 if (intel_wait_for_register(dev_priv,
1833 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1834 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001835 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001836}
1837
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001838static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001839 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001840{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001841 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001842
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001843 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001844 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001845 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001846
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001847 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001848 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001849 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001850 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001851
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001852 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001853 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001854
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001855 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1856 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001857 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001858 else
1859 val |= TRANS_PROGRESSIVE;
1860
Daniel Vetterab9412b2013-05-03 11:49:46 +02001861 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001862 if (intel_wait_for_register(dev_priv,
1863 LPT_TRANSCONF,
1864 TRANS_STATE_ENABLE,
1865 TRANS_STATE_ENABLE,
1866 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001867 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001868}
1869
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001870static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1871 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001872{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001873 i915_reg_t reg;
1874 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001875
1876 /* FDI relies on the transcoder */
1877 assert_fdi_tx_disabled(dev_priv, pipe);
1878 assert_fdi_rx_disabled(dev_priv, pipe);
1879
Jesse Barnes291906f2011-02-02 12:28:03 -08001880 /* Ports must be off as well */
1881 assert_pch_ports_disabled(dev_priv, pipe);
1882
Daniel Vetterab9412b2013-05-03 11:49:46 +02001883 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001884 val = I915_READ(reg);
1885 val &= ~TRANS_ENABLE;
1886 I915_WRITE(reg, val);
1887 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001888 if (intel_wait_for_register(dev_priv,
1889 reg, TRANS_STATE_ENABLE, 0,
1890 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001891 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001892
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001893 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001894 /* Workaround: Clear the timing override chicken bit again. */
1895 reg = TRANS_CHICKEN2(pipe);
1896 val = I915_READ(reg);
1897 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1898 I915_WRITE(reg, val);
1899 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001900}
1901
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001902void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001904 u32 val;
1905
Daniel Vetterab9412b2013-05-03 11:49:46 +02001906 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001907 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001908 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001909 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001910 if (intel_wait_for_register(dev_priv,
1911 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1912 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001913 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001914
1915 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001916 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001917 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001918 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001919}
1920
Ville Syrjälä65f21302016-10-14 20:02:53 +03001921enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1922{
1923 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1924
1925 WARN_ON(!crtc->config->has_pch_encoder);
1926
1927 if (HAS_PCH_LPT(dev_priv))
1928 return TRANSCODER_A;
1929 else
1930 return (enum transcoder) crtc->pipe;
1931}
1932
Jesse Barnes92f25842011-01-04 15:09:34 -08001933/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001934 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001935 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001938 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001939 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001940static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941{
Paulo Zanoni03722642014-01-17 13:51:09 -02001942 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001943 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001944 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001945 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001946 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001947 u32 val;
1948
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001949 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1950
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001951 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001952 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001953 assert_sprites_disabled(dev_priv, pipe);
1954
Jesse Barnesb24e7172011-01-04 15:09:30 -08001955 /*
1956 * A pipe without a PLL won't actually be able to drive bits from
1957 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1958 * need the check.
1959 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001960 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001961 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001962 assert_dsi_pll_enabled(dev_priv);
1963 else
1964 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001965 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001966 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001967 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001968 assert_fdi_rx_pll_enabled(dev_priv,
1969 (enum pipe) intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001970 assert_fdi_tx_pll_enabled(dev_priv,
1971 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001972 }
1973 /* FIXME: assert CPU port conditions for SNB+ */
1974 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001975
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001976 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001977 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001978 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001979 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1980 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001981 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001982 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001983
1984 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001985 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001986
1987 /*
1988 * Until the pipe starts DSL will read as 0, which would cause
1989 * an apparent vblank timestamp jump, which messes up also the
1990 * frame count when it's derived from the timestamps. So let's
1991 * wait for the pipe to start properly before we call
1992 * drm_crtc_vblank_on()
1993 */
1994 if (dev->max_vblank_count == 0 &&
1995 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1996 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997}
1998
1999/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002000 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002001 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002003 * Disable the pipe of @crtc, making sure that various hardware
2004 * specific requirements are met, if applicable, e.g. plane
2005 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002006 *
2007 * Will wait until the pipe has shut down before returning.
2008 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002009static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002010{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002011 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002012 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002013 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002014 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015 u32 val;
2016
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002017 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2018
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019 /*
2020 * Make sure planes won't keep trying to pump pixels to us,
2021 * or we might hang the display.
2022 */
2023 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002024 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002025 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002027 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002029 if ((val & PIPECONF_ENABLE) == 0)
2030 return;
2031
Ville Syrjälä67adc642014-08-15 01:21:57 +03002032 /*
2033 * Double wide has implications for planes
2034 * so best keep it disabled when not needed.
2035 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002036 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002037 val &= ~PIPECONF_DOUBLE_WIDE;
2038
2039 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002040 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2041 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002042 val &= ~PIPECONF_ENABLE;
2043
2044 I915_WRITE(reg, val);
2045 if ((val & PIPECONF_ENABLE) == 0)
2046 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047}
2048
Ville Syrjälä832be822016-01-12 21:08:33 +02002049static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2050{
2051 return IS_GEN2(dev_priv) ? 2048 : 4096;
2052}
2053
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002054static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2055 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002056{
2057 switch (fb_modifier) {
2058 case DRM_FORMAT_MOD_NONE:
2059 return cpp;
2060 case I915_FORMAT_MOD_X_TILED:
2061 if (IS_GEN2(dev_priv))
2062 return 128;
2063 else
2064 return 512;
2065 case I915_FORMAT_MOD_Y_TILED:
2066 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2067 return 128;
2068 else
2069 return 512;
2070 case I915_FORMAT_MOD_Yf_TILED:
2071 switch (cpp) {
2072 case 1:
2073 return 64;
2074 case 2:
2075 case 4:
2076 return 128;
2077 case 8:
2078 case 16:
2079 return 256;
2080 default:
2081 MISSING_CASE(cpp);
2082 return cpp;
2083 }
2084 break;
2085 default:
2086 MISSING_CASE(fb_modifier);
2087 return cpp;
2088 }
2089}
2090
Ville Syrjälä832be822016-01-12 21:08:33 +02002091unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2092 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002093{
Ville Syrjälä832be822016-01-12 21:08:33 +02002094 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2095 return 1;
2096 else
2097 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002098 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002099}
2100
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002101/* Return the tile dimensions in pixel units */
2102static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2103 unsigned int *tile_width,
2104 unsigned int *tile_height,
2105 uint64_t fb_modifier,
2106 unsigned int cpp)
2107{
2108 unsigned int tile_width_bytes =
2109 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2110
2111 *tile_width = tile_width_bytes / cpp;
2112 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2113}
2114
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002115unsigned int
2116intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002117 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002118{
Ville Syrjälä832be822016-01-12 21:08:33 +02002119 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2120 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2121
2122 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002123}
2124
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002125unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2126{
2127 unsigned int size = 0;
2128 int i;
2129
2130 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2131 size += rot_info->plane[i].width * rot_info->plane[i].height;
2132
2133 return size;
2134}
2135
Daniel Vetter75c82a52015-10-14 16:51:04 +02002136static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002137intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2138 const struct drm_framebuffer *fb,
2139 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002140{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002141 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002142 *view = i915_ggtt_view_rotated;
2143 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2144 } else {
2145 *view = i915_ggtt_view_normal;
2146 }
2147}
2148
Ville Syrjälä603525d2016-01-12 21:08:37 +02002149static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002150{
2151 if (INTEL_INFO(dev_priv)->gen >= 9)
2152 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002153 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002154 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002155 return 128 * 1024;
2156 else if (INTEL_INFO(dev_priv)->gen >= 4)
2157 return 4 * 1024;
2158 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002159 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002160}
2161
Ville Syrjälä603525d2016-01-12 21:08:37 +02002162static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2163 uint64_t fb_modifier)
2164{
2165 switch (fb_modifier) {
2166 case DRM_FORMAT_MOD_NONE:
2167 return intel_linear_alignment(dev_priv);
2168 case I915_FORMAT_MOD_X_TILED:
2169 if (INTEL_INFO(dev_priv)->gen >= 9)
2170 return 256 * 1024;
2171 return 0;
2172 case I915_FORMAT_MOD_Y_TILED:
2173 case I915_FORMAT_MOD_Yf_TILED:
2174 return 1 * 1024 * 1024;
2175 default:
2176 MISSING_CASE(fb_modifier);
2177 return 0;
2178 }
2179}
2180
Chris Wilson058d88c2016-08-15 10:49:06 +01002181struct i915_vma *
2182intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002183{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002184 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002185 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002186 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002187 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002188 struct i915_vma *vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002189 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002190
Matt Roperebcdd392014-07-09 16:22:11 -07002191 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2192
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002193 alignment = intel_surf_alignment(dev_priv, fb->modifier);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002194
Ville Syrjälä3465c582016-02-15 22:54:43 +02002195 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002196
Chris Wilson693db182013-03-05 14:52:39 +00002197 /* Note that the w/a also requires 64 PTE of padding following the
2198 * bo. We currently fill all unused PTE with the shadow page and so
2199 * we should always have valid PTE following the scanout preventing
2200 * the VT-d warning.
2201 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002202 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002203 alignment = 256 * 1024;
2204
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002205 /*
2206 * Global gtt pte registers are special registers which actually forward
2207 * writes to a chunk of system memory. Which means that there is no risk
2208 * that the register values disappear as soon as we call
2209 * intel_runtime_pm_put(), so it is correct to wrap only the
2210 * pin/unpin/fence and not more.
2211 */
2212 intel_runtime_pm_get(dev_priv);
2213
Chris Wilson058d88c2016-08-15 10:49:06 +01002214 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
Chris Wilson49ef5292016-08-18 17:17:00 +01002215 if (IS_ERR(vma))
2216 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002217
Chris Wilson05a20d02016-08-18 17:16:55 +01002218 if (i915_vma_is_map_and_fenceable(vma)) {
Chris Wilson49ef5292016-08-18 17:17:00 +01002219 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2220 * fence, whereas 965+ only requires a fence if using
2221 * framebuffer compression. For simplicity, we always, when
2222 * possible, install a fence as the cost is not that onerous.
2223 *
2224 * If we fail to fence the tiled scanout, then either the
2225 * modeset will reject the change (which is highly unlikely as
2226 * the affected systems, all but one, do not have unmappable
2227 * space) or we will not be able to enable full powersaving
2228 * techniques (also likely not to apply due to various limits
2229 * FBC and the like impose on the size of the buffer, which
2230 * presumably we violated anyway with this unmappable buffer).
2231 * Anyway, it is presumably better to stumble onwards with
2232 * something and try to run the system in a "less than optimal"
2233 * mode that matches the user configuration.
2234 */
2235 if (i915_vma_get_fence(vma) == 0)
2236 i915_vma_pin_fence(vma);
Vivek Kasireddy98072162015-10-29 18:54:38 -07002237 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002238
Chris Wilson49ef5292016-08-18 17:17:00 +01002239err:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002240 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002241 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002242}
2243
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002244void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002245{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002246 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002247 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002248 struct i915_vma *vma;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002249
Matt Roperebcdd392014-07-09 16:22:11 -07002250 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2251
Ville Syrjälä3465c582016-02-15 22:54:43 +02002252 intel_fill_fb_ggtt_view(&view, fb, rotation);
Chris Wilson05a20d02016-08-18 17:16:55 +01002253 vma = i915_gem_object_to_ggtt(obj, &view);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002254
Chris Wilson49ef5292016-08-18 17:17:00 +01002255 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002256 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002257}
2258
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002259static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2260 unsigned int rotation)
2261{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002262 if (drm_rotation_90_or_270(rotation))
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002263 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2264 else
2265 return fb->pitches[plane];
2266}
2267
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002268/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002269 * Convert the x/y offsets into a linear offset.
2270 * Only valid with 0/180 degree rotation, which is fine since linear
2271 * offset is only used with linear buffers on pre-hsw and tiled buffers
2272 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2273 */
2274u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002275 const struct intel_plane_state *state,
2276 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002277{
Ville Syrjälä29490562016-01-20 18:02:50 +02002278 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002279 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2280 unsigned int pitch = fb->pitches[plane];
2281
2282 return y * pitch + x * cpp;
2283}
2284
2285/*
2286 * Add the x/y offsets derived from fb->offsets[] to the user
2287 * specified plane src x/y offsets. The resulting x/y offsets
2288 * specify the start of scanout from the beginning of the gtt mapping.
2289 */
2290void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002291 const struct intel_plane_state *state,
2292 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002293
2294{
Ville Syrjälä29490562016-01-20 18:02:50 +02002295 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2296 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002297
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002298 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002299 *x += intel_fb->rotated[plane].x;
2300 *y += intel_fb->rotated[plane].y;
2301 } else {
2302 *x += intel_fb->normal[plane].x;
2303 *y += intel_fb->normal[plane].y;
2304 }
2305}
2306
2307/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002308 * Input tile dimensions and pitch must already be
2309 * rotated to match x and y, and in pixel units.
2310 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002311static u32 _intel_adjust_tile_offset(int *x, int *y,
2312 unsigned int tile_width,
2313 unsigned int tile_height,
2314 unsigned int tile_size,
2315 unsigned int pitch_tiles,
2316 u32 old_offset,
2317 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002318{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002319 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002320 unsigned int tiles;
2321
2322 WARN_ON(old_offset & (tile_size - 1));
2323 WARN_ON(new_offset & (tile_size - 1));
2324 WARN_ON(new_offset > old_offset);
2325
2326 tiles = (old_offset - new_offset) / tile_size;
2327
2328 *y += tiles / pitch_tiles * tile_height;
2329 *x += tiles % pitch_tiles * tile_width;
2330
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002331 /* minimize x in case it got needlessly big */
2332 *y += *x / pitch_pixels * tile_height;
2333 *x %= pitch_pixels;
2334
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002335 return new_offset;
2336}
2337
2338/*
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002339 * Adjust the tile offset by moving the difference into
2340 * the x/y offsets.
2341 */
2342static u32 intel_adjust_tile_offset(int *x, int *y,
2343 const struct intel_plane_state *state, int plane,
2344 u32 old_offset, u32 new_offset)
2345{
2346 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2347 const struct drm_framebuffer *fb = state->base.fb;
2348 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2349 unsigned int rotation = state->base.rotation;
2350 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2351
2352 WARN_ON(new_offset > old_offset);
2353
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002354 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002355 unsigned int tile_size, tile_width, tile_height;
2356 unsigned int pitch_tiles;
2357
2358 tile_size = intel_tile_size(dev_priv);
2359 intel_tile_dims(dev_priv, &tile_width, &tile_height,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002360 fb->modifier, cpp);
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002361
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002362 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002363 pitch_tiles = pitch / tile_height;
2364 swap(tile_width, tile_height);
2365 } else {
2366 pitch_tiles = pitch / (tile_width * cpp);
2367 }
2368
2369 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2370 tile_size, pitch_tiles,
2371 old_offset, new_offset);
2372 } else {
2373 old_offset += *y * pitch + *x * cpp;
2374
2375 *y = (old_offset - new_offset) / pitch;
2376 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2377 }
2378
2379 return new_offset;
2380}
2381
2382/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002383 * Computes the linear offset to the base tile and adjusts
2384 * x, y. bytes per pixel is assumed to be a power-of-two.
2385 *
2386 * In the 90/270 rotated case, x and y are assumed
2387 * to be already rotated to match the rotated GTT view, and
2388 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002389 *
2390 * This function is used when computing the derived information
2391 * under intel_framebuffer, so using any of that information
2392 * here is not allowed. Anything under drm_framebuffer can be
2393 * used. This is why the user has to pass in the pitch since it
2394 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002395 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002396static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2397 int *x, int *y,
2398 const struct drm_framebuffer *fb, int plane,
2399 unsigned int pitch,
2400 unsigned int rotation,
2401 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002402{
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002403 uint64_t fb_modifier = fb->modifier;
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002404 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002405 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002406
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002407 if (alignment)
2408 alignment--;
2409
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002410 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002411 unsigned int tile_size, tile_width, tile_height;
2412 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002413
Ville Syrjäläd8433102016-01-12 21:08:35 +02002414 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002415 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2416 fb_modifier, cpp);
2417
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002418 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002419 pitch_tiles = pitch / tile_height;
2420 swap(tile_width, tile_height);
2421 } else {
2422 pitch_tiles = pitch / (tile_width * cpp);
2423 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002424
Ville Syrjäläd8433102016-01-12 21:08:35 +02002425 tile_rows = *y / tile_height;
2426 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002427
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002428 tiles = *x / tile_width;
2429 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002430
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002431 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2432 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002433
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002434 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2435 tile_size, pitch_tiles,
2436 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002437 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002438 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002439 offset_aligned = offset & ~alignment;
2440
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002441 *y = (offset & alignment) / pitch;
2442 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002443 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002444
2445 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002446}
2447
Ville Syrjälä6687c902015-09-15 13:16:41 +03002448u32 intel_compute_tile_offset(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002449 const struct intel_plane_state *state,
2450 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002451{
Ville Syrjälä29490562016-01-20 18:02:50 +02002452 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2453 const struct drm_framebuffer *fb = state->base.fb;
2454 unsigned int rotation = state->base.rotation;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002455 int pitch = intel_fb_pitch(fb, plane, rotation);
Ville Syrjälä8d970652016-01-28 16:30:28 +02002456 u32 alignment;
2457
2458 /* AUX_DIST needs only 4K alignment */
2459 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2460 alignment = 4096;
2461 else
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002462 alignment = intel_surf_alignment(dev_priv, fb->modifier);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002463
2464 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2465 rotation, alignment);
2466}
2467
2468/* Convert the fb->offset[] linear offset into x/y offsets */
2469static void intel_fb_offset_to_xy(int *x, int *y,
2470 const struct drm_framebuffer *fb, int plane)
2471{
2472 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2473 unsigned int pitch = fb->pitches[plane];
2474 u32 linear_offset = fb->offsets[plane];
2475
2476 *y = linear_offset / pitch;
2477 *x = linear_offset % pitch / cpp;
2478}
2479
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002480static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2481{
2482 switch (fb_modifier) {
2483 case I915_FORMAT_MOD_X_TILED:
2484 return I915_TILING_X;
2485 case I915_FORMAT_MOD_Y_TILED:
2486 return I915_TILING_Y;
2487 default:
2488 return I915_TILING_NONE;
2489 }
2490}
2491
Ville Syrjälä6687c902015-09-15 13:16:41 +03002492static int
2493intel_fill_fb_info(struct drm_i915_private *dev_priv,
2494 struct drm_framebuffer *fb)
2495{
2496 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2497 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2498 u32 gtt_offset_rotated = 0;
2499 unsigned int max_size = 0;
2500 uint32_t format = fb->pixel_format;
2501 int i, num_planes = drm_format_num_planes(format);
2502 unsigned int tile_size = intel_tile_size(dev_priv);
2503
2504 for (i = 0; i < num_planes; i++) {
2505 unsigned int width, height;
2506 unsigned int cpp, size;
2507 u32 offset;
2508 int x, y;
2509
2510 cpp = drm_format_plane_cpp(format, i);
2511 width = drm_format_plane_width(fb->width, format, i);
2512 height = drm_format_plane_height(fb->height, format, i);
2513
2514 intel_fb_offset_to_xy(&x, &y, fb, i);
2515
2516 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002517 * The fence (if used) is aligned to the start of the object
2518 * so having the framebuffer wrap around across the edge of the
2519 * fenced region doesn't really work. We have no API to configure
2520 * the fence start offset within the object (nor could we probably
2521 * on gen2/3). So it's just easier if we just require that the
2522 * fb layout agrees with the fence layout. We already check that the
2523 * fb stride matches the fence stride elsewhere.
2524 */
2525 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2526 (x + width) * cpp > fb->pitches[i]) {
2527 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2528 i, fb->offsets[i]);
2529 return -EINVAL;
2530 }
2531
2532 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002533 * First pixel of the framebuffer from
2534 * the start of the normal gtt mapping.
2535 */
2536 intel_fb->normal[i].x = x;
2537 intel_fb->normal[i].y = y;
2538
2539 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2540 fb, 0, fb->pitches[i],
Daniel Vettercc926382016-08-15 10:41:47 +02002541 DRM_ROTATE_0, tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002542 offset /= tile_size;
2543
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002544 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002545 unsigned int tile_width, tile_height;
2546 unsigned int pitch_tiles;
2547 struct drm_rect r;
2548
2549 intel_tile_dims(dev_priv, &tile_width, &tile_height,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002550 fb->modifier, cpp);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002551
2552 rot_info->plane[i].offset = offset;
2553 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2554 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2555 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2556
2557 intel_fb->rotated[i].pitch =
2558 rot_info->plane[i].height * tile_height;
2559
2560 /* how many tiles does this plane need */
2561 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2562 /*
2563 * If the plane isn't horizontally tile aligned,
2564 * we need one more tile.
2565 */
2566 if (x != 0)
2567 size++;
2568
2569 /* rotate the x/y offsets to match the GTT view */
2570 r.x1 = x;
2571 r.y1 = y;
2572 r.x2 = x + width;
2573 r.y2 = y + height;
2574 drm_rect_rotate(&r,
2575 rot_info->plane[i].width * tile_width,
2576 rot_info->plane[i].height * tile_height,
Daniel Vettercc926382016-08-15 10:41:47 +02002577 DRM_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002578 x = r.x1;
2579 y = r.y1;
2580
2581 /* rotate the tile dimensions to match the GTT view */
2582 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2583 swap(tile_width, tile_height);
2584
2585 /*
2586 * We only keep the x/y offsets, so push all of the
2587 * gtt offset into the x/y offsets.
2588 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002589 _intel_adjust_tile_offset(&x, &y, tile_size,
2590 tile_width, tile_height, pitch_tiles,
2591 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002592
2593 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2594
2595 /*
2596 * First pixel of the framebuffer from
2597 * the start of the rotated gtt mapping.
2598 */
2599 intel_fb->rotated[i].x = x;
2600 intel_fb->rotated[i].y = y;
2601 } else {
2602 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2603 x * cpp, tile_size);
2604 }
2605
2606 /* how many tiles in total needed in the bo */
2607 max_size = max(max_size, offset + size);
2608 }
2609
2610 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2611 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2612 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2613 return -EINVAL;
2614 }
2615
2616 return 0;
2617}
2618
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002619static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002620{
2621 switch (format) {
2622 case DISPPLANE_8BPP:
2623 return DRM_FORMAT_C8;
2624 case DISPPLANE_BGRX555:
2625 return DRM_FORMAT_XRGB1555;
2626 case DISPPLANE_BGRX565:
2627 return DRM_FORMAT_RGB565;
2628 default:
2629 case DISPPLANE_BGRX888:
2630 return DRM_FORMAT_XRGB8888;
2631 case DISPPLANE_RGBX888:
2632 return DRM_FORMAT_XBGR8888;
2633 case DISPPLANE_BGRX101010:
2634 return DRM_FORMAT_XRGB2101010;
2635 case DISPPLANE_RGBX101010:
2636 return DRM_FORMAT_XBGR2101010;
2637 }
2638}
2639
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002640static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2641{
2642 switch (format) {
2643 case PLANE_CTL_FORMAT_RGB_565:
2644 return DRM_FORMAT_RGB565;
2645 default:
2646 case PLANE_CTL_FORMAT_XRGB_8888:
2647 if (rgb_order) {
2648 if (alpha)
2649 return DRM_FORMAT_ABGR8888;
2650 else
2651 return DRM_FORMAT_XBGR8888;
2652 } else {
2653 if (alpha)
2654 return DRM_FORMAT_ARGB8888;
2655 else
2656 return DRM_FORMAT_XRGB8888;
2657 }
2658 case PLANE_CTL_FORMAT_XRGB_2101010:
2659 if (rgb_order)
2660 return DRM_FORMAT_XBGR2101010;
2661 else
2662 return DRM_FORMAT_XRGB2101010;
2663 }
2664}
2665
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002666static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002667intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2668 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002669{
2670 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002671 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002672 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002673 struct drm_i915_gem_object *obj = NULL;
2674 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002675 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002676 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2677 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2678 PAGE_SIZE);
2679
2680 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002681
Chris Wilsonff2652e2014-03-10 08:07:02 +00002682 if (plane_config->size == 0)
2683 return false;
2684
Paulo Zanoni3badb492015-09-23 12:52:23 -03002685 /* If the FB is too big, just don't use it since fbdev is not very
2686 * important and we should probably use that space with FBC or other
2687 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002688 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002689 return false;
2690
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002691 mutex_lock(&dev->struct_mutex);
2692
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00002693 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002694 base_aligned,
2695 base_aligned,
2696 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002697 if (!obj) {
2698 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002699 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002700 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002701
Chris Wilson3e510a82016-08-05 10:14:23 +01002702 if (plane_config->tiling == I915_TILING_X)
2703 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002704
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002705 mode_cmd.pixel_format = fb->pixel_format;
2706 mode_cmd.width = fb->width;
2707 mode_cmd.height = fb->height;
2708 mode_cmd.pitches[0] = fb->pitches[0];
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002709 mode_cmd.modifier[0] = fb->modifier;
Daniel Vetter18c52472015-02-10 17:16:09 +00002710 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002711
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002712 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002713 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002714 DRM_DEBUG_KMS("intel fb init failed\n");
2715 goto out_unref_obj;
2716 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002717
Jesse Barnes46f297f2014-03-07 08:57:48 -08002718 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002719
Daniel Vetterf6936e22015-03-26 12:17:05 +01002720 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002721 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002722
2723out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002724 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002725 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002726 return false;
2727}
2728
Daniel Vetter5a21b662016-05-24 17:13:53 +02002729/* Update plane->state->fb to match plane->fb after driver-internal updates */
2730static void
2731update_state_fb(struct drm_plane *plane)
2732{
2733 if (plane->fb == plane->state->fb)
2734 return;
2735
2736 if (plane->state->fb)
2737 drm_framebuffer_unreference(plane->state->fb);
2738 plane->state->fb = plane->fb;
2739 if (plane->state->fb)
2740 drm_framebuffer_reference(plane->state->fb);
2741}
2742
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002743static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002744intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2745 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002746{
2747 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002748 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002749 struct drm_crtc *c;
2750 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002751 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002752 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002753 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002754 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2755 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002756 struct intel_plane_state *intel_state =
2757 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002758 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002759
Damien Lespiau2d140302015-02-05 17:22:18 +00002760 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002761 return;
2762
Daniel Vetterf6936e22015-03-26 12:17:05 +01002763 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002764 fb = &plane_config->fb->base;
2765 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002766 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002767
Damien Lespiau2d140302015-02-05 17:22:18 +00002768 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002769
2770 /*
2771 * Failed to alloc the obj, check to see if we should share
2772 * an fb with another CRTC instead
2773 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002774 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002775 i = to_intel_crtc(c);
2776
2777 if (c == &intel_crtc->base)
2778 continue;
2779
Matt Roper2ff8fde2014-07-08 07:50:07 -07002780 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002781 continue;
2782
Daniel Vetter88595ac2015-03-26 12:42:24 +01002783 fb = c->primary->fb;
2784 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002785 continue;
2786
Daniel Vetter88595ac2015-03-26 12:42:24 +01002787 obj = intel_fb_obj(fb);
Chris Wilson058d88c2016-08-15 10:49:06 +01002788 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002789 drm_framebuffer_reference(fb);
2790 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002791 }
2792 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002793
Matt Roper200757f2015-12-03 11:37:36 -08002794 /*
2795 * We've failed to reconstruct the BIOS FB. Current display state
2796 * indicates that the primary plane is visible, but has a NULL FB,
2797 * which will lead to problems later if we don't fix it up. The
2798 * simplest solution is to just disable the primary plane now and
2799 * pretend the BIOS never had it enabled.
2800 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002801 to_intel_plane_state(plane_state)->base.visible = false;
Matt Roper200757f2015-12-03 11:37:36 -08002802 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002803 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002804 intel_plane->disable_plane(primary, &intel_crtc->base);
2805
Daniel Vetter88595ac2015-03-26 12:42:24 +01002806 return;
2807
2808valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002809 plane_state->src_x = 0;
2810 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002811 plane_state->src_w = fb->width << 16;
2812 plane_state->src_h = fb->height << 16;
2813
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002814 plane_state->crtc_x = 0;
2815 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002816 plane_state->crtc_w = fb->width;
2817 plane_state->crtc_h = fb->height;
2818
Rob Clark1638d302016-11-05 11:08:08 -04002819 intel_state->base.src = drm_plane_state_src(plane_state);
2820 intel_state->base.dst = drm_plane_state_dest(plane_state);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002821
Daniel Vetter88595ac2015-03-26 12:42:24 +01002822 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002823 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002824 dev_priv->preserve_bios_swizzle = true;
2825
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002826 drm_framebuffer_reference(fb);
2827 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002828 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002829 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002830 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2831 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002832}
2833
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002834static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2835 unsigned int rotation)
2836{
2837 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2838
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002839 switch (fb->modifier) {
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002840 case DRM_FORMAT_MOD_NONE:
2841 case I915_FORMAT_MOD_X_TILED:
2842 switch (cpp) {
2843 case 8:
2844 return 4096;
2845 case 4:
2846 case 2:
2847 case 1:
2848 return 8192;
2849 default:
2850 MISSING_CASE(cpp);
2851 break;
2852 }
2853 break;
2854 case I915_FORMAT_MOD_Y_TILED:
2855 case I915_FORMAT_MOD_Yf_TILED:
2856 switch (cpp) {
2857 case 8:
2858 return 2048;
2859 case 4:
2860 return 4096;
2861 case 2:
2862 case 1:
2863 return 8192;
2864 default:
2865 MISSING_CASE(cpp);
2866 break;
2867 }
2868 break;
2869 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002870 MISSING_CASE(fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002871 }
2872
2873 return 2048;
2874}
2875
2876static int skl_check_main_surface(struct intel_plane_state *plane_state)
2877{
2878 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2879 const struct drm_framebuffer *fb = plane_state->base.fb;
2880 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002881 int x = plane_state->base.src.x1 >> 16;
2882 int y = plane_state->base.src.y1 >> 16;
2883 int w = drm_rect_width(&plane_state->base.src) >> 16;
2884 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002885 int max_width = skl_max_plane_width(fb, 0, rotation);
2886 int max_height = 4096;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002887 u32 alignment, offset, aux_offset = plane_state->aux.offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002888
2889 if (w > max_width || h > max_height) {
2890 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2891 w, h, max_width, max_height);
2892 return -EINVAL;
2893 }
2894
2895 intel_add_fb_offsets(&x, &y, plane_state, 0);
2896 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2897
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002898 alignment = intel_surf_alignment(dev_priv, fb->modifier);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002899
2900 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002901 * AUX surface offset is specified as the distance from the
2902 * main surface offset, and it must be non-negative. Make
2903 * sure that is what we will get.
2904 */
2905 if (offset > aux_offset)
2906 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2907 offset, aux_offset & ~(alignment - 1));
2908
2909 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002910 * When using an X-tiled surface, the plane blows up
2911 * if the x offset + width exceed the stride.
2912 *
2913 * TODO: linear and Y-tiled seem fine, Yf untested,
2914 */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02002915 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002916 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2917
2918 while ((x + w) * cpp > fb->pitches[0]) {
2919 if (offset == 0) {
2920 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2921 return -EINVAL;
2922 }
2923
2924 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2925 offset, offset - alignment);
2926 }
2927 }
2928
2929 plane_state->main.offset = offset;
2930 plane_state->main.x = x;
2931 plane_state->main.y = y;
2932
2933 return 0;
2934}
2935
Ville Syrjälä8d970652016-01-28 16:30:28 +02002936static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2937{
2938 const struct drm_framebuffer *fb = plane_state->base.fb;
2939 unsigned int rotation = plane_state->base.rotation;
2940 int max_width = skl_max_plane_width(fb, 1, rotation);
2941 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02002942 int x = plane_state->base.src.x1 >> 17;
2943 int y = plane_state->base.src.y1 >> 17;
2944 int w = drm_rect_width(&plane_state->base.src) >> 17;
2945 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002946 u32 offset;
2947
2948 intel_add_fb_offsets(&x, &y, plane_state, 1);
2949 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2950
2951 /* FIXME not quite sure how/if these apply to the chroma plane */
2952 if (w > max_width || h > max_height) {
2953 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2954 w, h, max_width, max_height);
2955 return -EINVAL;
2956 }
2957
2958 plane_state->aux.offset = offset;
2959 plane_state->aux.x = x;
2960 plane_state->aux.y = y;
2961
2962 return 0;
2963}
2964
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002965int skl_check_plane_surface(struct intel_plane_state *plane_state)
2966{
2967 const struct drm_framebuffer *fb = plane_state->base.fb;
2968 unsigned int rotation = plane_state->base.rotation;
2969 int ret;
2970
2971 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002972 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02002973 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03002974 fb->width << 16, fb->height << 16,
2975 DRM_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002976
Ville Syrjälä8d970652016-01-28 16:30:28 +02002977 /*
2978 * Handle the AUX surface first since
2979 * the main surface setup depends on it.
2980 */
2981 if (fb->pixel_format == DRM_FORMAT_NV12) {
2982 ret = skl_check_nv12_aux_surface(plane_state);
2983 if (ret)
2984 return ret;
2985 } else {
2986 plane_state->aux.offset = ~0xfff;
2987 plane_state->aux.x = 0;
2988 plane_state->aux.y = 0;
2989 }
2990
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002991 ret = skl_check_main_surface(plane_state);
2992 if (ret)
2993 return ret;
2994
2995 return 0;
2996}
2997
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002998static void i9xx_update_primary_plane(struct drm_plane *primary,
2999 const struct intel_crtc_state *crtc_state,
3000 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07003001{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003002 struct drm_i915_private *dev_priv = to_i915(primary->dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3004 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes81255562010-08-02 12:07:50 -07003005 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003006 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07003007 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003008 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003009 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003010 int x = plane_state->base.src.x1 >> 16;
3011 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003012
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003013 dspcntr = DISPPLANE_GAMMA_ENABLE;
3014
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003015 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003016
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003017 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003018 if (intel_crtc->pipe == PIPE_B)
3019 dspcntr |= DISPPLANE_SEL_PIPE_B;
3020
3021 /* pipesrc and dspsize control the size that is scaled from,
3022 * which should always be the user's requested size.
3023 */
3024 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003025 ((crtc_state->pipe_src_h - 1) << 16) |
3026 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003027 I915_WRITE(DSPPOS(plane), 0);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01003028 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003029 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003030 ((crtc_state->pipe_src_h - 1) << 16) |
3031 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003032 I915_WRITE(PRIMPOS(plane), 0);
3033 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003034 }
3035
Ville Syrjälä57779d02012-10-31 17:50:14 +02003036 switch (fb->pixel_format) {
3037 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003038 dspcntr |= DISPPLANE_8BPP;
3039 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003040 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003041 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003042 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003043 case DRM_FORMAT_RGB565:
3044 dspcntr |= DISPPLANE_BGRX565;
3045 break;
3046 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003047 dspcntr |= DISPPLANE_BGRX888;
3048 break;
3049 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003050 dspcntr |= DISPPLANE_RGBX888;
3051 break;
3052 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003053 dspcntr |= DISPPLANE_BGRX101010;
3054 break;
3055 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003056 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003057 break;
3058 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003059 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07003060 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003061
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003062 if (INTEL_GEN(dev_priv) >= 4 &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003063 fb->modifier == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003064 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003065
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003066 if (rotation & DRM_ROTATE_180)
3067 dspcntr |= DISPPLANE_ROTATE_180;
3068
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003069 if (rotation & DRM_REFLECT_X)
3070 dspcntr |= DISPPLANE_MIRROR;
3071
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01003072 if (IS_G4X(dev_priv))
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003073 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3074
Ville Syrjälä29490562016-01-20 18:02:50 +02003075 intel_add_fb_offsets(&x, &y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003076
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003077 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetterc2c75132012-07-05 12:17:30 +02003078 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003079 intel_compute_tile_offset(&x, &y, plane_state, 0);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003080
Ville Syrjäläf22aa142016-11-14 18:53:58 +02003081 if (rotation & DRM_ROTATE_180) {
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003082 x += crtc_state->pipe_src_w - 1;
3083 y += crtc_state->pipe_src_h - 1;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +02003084 } else if (rotation & DRM_REFLECT_X) {
3085 x += crtc_state->pipe_src_w - 1;
Sonika Jindal48404c12014-08-22 14:06:04 +05303086 }
3087
Ville Syrjälä29490562016-01-20 18:02:50 +02003088 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003089
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003090 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä6687c902015-09-15 13:16:41 +03003091 intel_crtc->dspaddr_offset = linear_offset;
3092
Paulo Zanoni2db33662015-09-14 15:20:03 -03003093 intel_crtc->adjusted_x = x;
3094 intel_crtc->adjusted_y = y;
3095
Sonika Jindal48404c12014-08-22 14:06:04 +05303096 I915_WRITE(reg, dspcntr);
3097
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003098 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003099 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003100 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003101 intel_fb_gtt_offset(fb, rotation) +
3102 intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003103 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003104 I915_WRITE(DSPLINOFF(plane), linear_offset);
Ville Syrjäläbfb81042016-11-07 22:20:57 +02003105 } else {
3106 I915_WRITE(DSPADDR(plane),
3107 intel_fb_gtt_offset(fb, rotation) +
3108 intel_crtc->dspaddr_offset);
3109 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003110 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003111}
3112
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003113static void i9xx_disable_primary_plane(struct drm_plane *primary,
3114 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003115{
3116 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003117 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003119 int plane = intel_crtc->plane;
3120
3121 I915_WRITE(DSPCNTR(plane), 0);
3122 if (INTEL_INFO(dev_priv)->gen >= 4)
3123 I915_WRITE(DSPSURF(plane), 0);
3124 else
3125 I915_WRITE(DSPADDR(plane), 0);
3126 POSTING_READ(DSPCNTR(plane));
3127}
3128
3129static void ironlake_update_primary_plane(struct drm_plane *primary,
3130 const struct intel_crtc_state *crtc_state,
3131 const struct intel_plane_state *plane_state)
3132{
3133 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003134 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3136 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003137 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003138 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003139 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003140 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003141 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003142 int x = plane_state->base.src.x1 >> 16;
3143 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003144
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003145 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003146 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003147
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003148 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003149 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
3150
Ville Syrjälä57779d02012-10-31 17:50:14 +02003151 switch (fb->pixel_format) {
3152 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07003153 dspcntr |= DISPPLANE_8BPP;
3154 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003155 case DRM_FORMAT_RGB565:
3156 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003157 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003158 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003159 dspcntr |= DISPPLANE_BGRX888;
3160 break;
3161 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003162 dspcntr |= DISPPLANE_RGBX888;
3163 break;
3164 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003165 dspcntr |= DISPPLANE_BGRX101010;
3166 break;
3167 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003168 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003169 break;
3170 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003171 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07003172 }
3173
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003174 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003175 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003176
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003177 if (rotation & DRM_ROTATE_180)
3178 dspcntr |= DISPPLANE_ROTATE_180;
3179
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003180 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03003181 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003182
Ville Syrjälä29490562016-01-20 18:02:50 +02003183 intel_add_fb_offsets(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003184
Daniel Vetterc2c75132012-07-05 12:17:30 +02003185 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003186 intel_compute_tile_offset(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003187
Ville Syrjälädf0cd452016-11-14 18:53:59 +02003188 /* HSW+ does this automagically in hardware */
3189 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3190 rotation & DRM_ROTATE_180) {
3191 x += crtc_state->pipe_src_w - 1;
3192 y += crtc_state->pipe_src_h - 1;
Sonika Jindal48404c12014-08-22 14:06:04 +05303193 }
3194
Ville Syrjälä29490562016-01-20 18:02:50 +02003195 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003196
Paulo Zanoni2db33662015-09-14 15:20:03 -03003197 intel_crtc->adjusted_x = x;
3198 intel_crtc->adjusted_y = y;
3199
Sonika Jindal48404c12014-08-22 14:06:04 +05303200 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003201
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003202 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003203 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003204 intel_fb_gtt_offset(fb, rotation) +
3205 intel_crtc->dspaddr_offset);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003206 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00003207 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3208 } else {
3209 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3210 I915_WRITE(DSPLINOFF(plane), linear_offset);
3211 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07003212 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003213}
3214
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003215u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3216 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00003217{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003218 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
3219 return 64;
3220 } else {
3221 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00003222
Ville Syrjälä27ba3912016-02-15 22:54:40 +02003223 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00003224 }
3225}
3226
Ville Syrjälä6687c902015-09-15 13:16:41 +03003227u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3228 unsigned int rotation)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003229{
Ville Syrjälä6687c902015-09-15 13:16:41 +03003230 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetterce7f1722015-10-14 16:51:06 +02003231 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01003232 struct i915_vma *vma;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003233
Ville Syrjälä6687c902015-09-15 13:16:41 +03003234 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003235
Chris Wilson058d88c2016-08-15 10:49:06 +01003236 vma = i915_gem_object_to_ggtt(obj, &view);
3237 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3238 view.type))
3239 return -1;
3240
Chris Wilsonbde13eb2016-08-15 10:49:07 +01003241 return i915_ggtt_offset(vma);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003242}
3243
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003244static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3245{
3246 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003247 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003248
3249 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3250 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3251 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003252}
3253
Chandra Kondurua1b22782015-04-07 15:28:45 -07003254/*
3255 * This function detaches (aka. unbinds) unused scalers in hardware
3256 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003257static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003258{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003259 struct intel_crtc_scaler_state *scaler_state;
3260 int i;
3261
Chandra Kondurua1b22782015-04-07 15:28:45 -07003262 scaler_state = &intel_crtc->config->scaler_state;
3263
3264 /* loop through and disable scalers that aren't in use */
3265 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003266 if (!scaler_state->scalers[i].in_use)
3267 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003268 }
3269}
3270
Ville Syrjäläd2196772016-01-28 18:33:11 +02003271u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3272 unsigned int rotation)
3273{
3274 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3275 u32 stride = intel_fb_pitch(fb, plane, rotation);
3276
3277 /*
3278 * The stride is either expressed as a multiple of 64 bytes chunks for
3279 * linear buffers or in number of tiles for tiled buffers.
3280 */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003281 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjäläd2196772016-01-28 18:33:11 +02003282 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3283
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003284 stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
Ville Syrjäläd2196772016-01-28 18:33:11 +02003285 } else {
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003286 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
Ville Syrjäläd2196772016-01-28 18:33:11 +02003287 fb->pixel_format);
3288 }
3289
3290 return stride;
3291}
3292
Chandra Konduru6156a452015-04-27 13:48:39 -07003293u32 skl_plane_ctl_format(uint32_t pixel_format)
3294{
Chandra Konduru6156a452015-04-27 13:48:39 -07003295 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003296 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003297 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003298 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003299 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003300 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003301 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003302 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003303 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003304 /*
3305 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3306 * to be already pre-multiplied. We need to add a knob (or a different
3307 * DRM_FORMAT) for user-space to configure that.
3308 */
3309 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003310 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003311 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003312 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003313 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003314 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003315 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003316 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003317 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003318 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003319 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003320 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003321 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003322 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003323 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003324 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003325 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003326 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003327 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003328 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003329 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003330
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003331 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003332}
3333
3334u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3335{
Chandra Konduru6156a452015-04-27 13:48:39 -07003336 switch (fb_modifier) {
3337 case DRM_FORMAT_MOD_NONE:
3338 break;
3339 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003340 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003341 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003342 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003343 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003344 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003345 default:
3346 MISSING_CASE(fb_modifier);
3347 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003348
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003349 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003350}
3351
3352u32 skl_plane_ctl_rotation(unsigned int rotation)
3353{
Chandra Konduru6156a452015-04-27 13:48:39 -07003354 switch (rotation) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003355 case DRM_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003356 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303357 /*
3358 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3359 * while i915 HW rotation is clockwise, thats why this swapping.
3360 */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003361 case DRM_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303362 return PLANE_CTL_ROTATE_270;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003363 case DRM_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003364 return PLANE_CTL_ROTATE_180;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003365 case DRM_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303366 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003367 default:
3368 MISSING_CASE(rotation);
3369 }
3370
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003371 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003372}
3373
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003374static void skylake_update_primary_plane(struct drm_plane *plane,
3375 const struct intel_crtc_state *crtc_state,
3376 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003377{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003378 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003379 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3381 struct drm_framebuffer *fb = plane_state->base.fb;
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003382 enum plane_id plane_id = to_intel_plane(plane)->id;
3383 enum pipe pipe = to_intel_plane(plane)->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003384 u32 plane_ctl;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003385 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003386 u32 stride = skl_plane_stride(fb, 0, rotation);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003387 u32 surf_addr = plane_state->main.offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003388 int scaler_id = plane_state->scaler_id;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003389 int src_x = plane_state->main.x;
3390 int src_y = plane_state->main.y;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003391 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3392 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3393 int dst_x = plane_state->base.dst.x1;
3394 int dst_y = plane_state->base.dst.y1;
3395 int dst_w = drm_rect_width(&plane_state->base.dst);
3396 int dst_h = drm_rect_height(&plane_state->base.dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003397
3398 plane_ctl = PLANE_CTL_ENABLE |
3399 PLANE_CTL_PIPE_GAMMA_ENABLE |
3400 PLANE_CTL_PIPE_CSC_ENABLE;
3401
Chandra Konduru6156a452015-04-27 13:48:39 -07003402 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003403 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003404 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003405 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003406
Ville Syrjälä6687c902015-09-15 13:16:41 +03003407 /* Sizes are 0 based */
3408 src_w--;
3409 src_h--;
3410 dst_w--;
3411 dst_h--;
3412
Paulo Zanoni4c0b8a82016-08-19 19:03:23 -03003413 intel_crtc->dspaddr_offset = surf_addr;
3414
Ville Syrjälä6687c902015-09-15 13:16:41 +03003415 intel_crtc->adjusted_x = src_x;
3416 intel_crtc->adjusted_y = src_y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003417
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003418 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3419 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3420 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3421 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
Chandra Konduru6156a452015-04-27 13:48:39 -07003422
3423 if (scaler_id >= 0) {
3424 uint32_t ps_ctrl = 0;
3425
3426 WARN_ON(!dst_w || !dst_h);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003427 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
Chandra Konduru6156a452015-04-27 13:48:39 -07003428 crtc_state->scaler_state.scalers[scaler_id].mode;
3429 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3430 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3431 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3432 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003433 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003434 } else {
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003435 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
Chandra Konduru6156a452015-04-27 13:48:39 -07003436 }
3437
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003438 I915_WRITE(PLANE_SURF(pipe, plane_id),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003439 intel_fb_gtt_offset(fb, rotation) + surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003440
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003441 POSTING_READ(PLANE_SURF(pipe, plane_id));
Damien Lespiau70d21f02013-07-03 21:06:04 +01003442}
3443
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003444static void skylake_disable_primary_plane(struct drm_plane *primary,
3445 struct drm_crtc *crtc)
3446{
3447 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003448 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003449 enum plane_id plane_id = to_intel_plane(primary)->id;
3450 enum pipe pipe = to_intel_plane(primary)->pipe;
Lyude62e0fb82016-08-22 12:50:08 -04003451
Ville Syrjälä8e816bb2016-11-22 18:01:59 +02003452 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3453 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3454 POSTING_READ(PLANE_SURF(pipe, plane_id));
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003455}
3456
Jesse Barnes17638cd2011-06-24 12:19:23 -07003457/* Assume fb object is pinned & idle & fenced and just update base pointers */
3458static int
3459intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3460 int x, int y, enum mode_set_atomic state)
3461{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003462 /* Support for kgdboc is disabled, this needs a major rework. */
3463 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003464
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003465 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003466}
3467
Daniel Vetter5a21b662016-05-24 17:13:53 +02003468static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3469{
3470 struct intel_crtc *crtc;
3471
Chris Wilson91c8a322016-07-05 10:40:23 +01003472 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003473 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3474}
3475
Ville Syrjälä75147472014-11-24 18:28:11 +02003476static void intel_update_primary_planes(struct drm_device *dev)
3477{
Ville Syrjälä75147472014-11-24 18:28:11 +02003478 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003479
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003480 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003481 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003482 struct intel_plane_state *plane_state =
3483 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003484
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003485 if (plane_state->base.visible)
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003486 plane->update_plane(&plane->base,
3487 to_intel_crtc_state(crtc->state),
3488 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003489 }
3490}
3491
Maarten Lankhorst73974892016-08-05 23:28:27 +03003492static int
3493__intel_display_resume(struct drm_device *dev,
3494 struct drm_atomic_state *state)
3495{
3496 struct drm_crtc_state *crtc_state;
3497 struct drm_crtc *crtc;
3498 int i, ret;
3499
3500 intel_modeset_setup_hw_state(dev);
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +00003501 i915_redisable_vga(to_i915(dev));
Maarten Lankhorst73974892016-08-05 23:28:27 +03003502
3503 if (!state)
3504 return 0;
3505
3506 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3507 /*
3508 * Force recalculation even if we restore
3509 * current state. With fast modeset this may not result
3510 * in a modeset when the state is compatible.
3511 */
3512 crtc_state->mode_changed = true;
3513 }
3514
3515 /* ignore any reset values/BIOS leftovers in the WM registers */
3516 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3517
3518 ret = drm_atomic_commit(state);
3519
3520 WARN_ON(ret == -EDEADLK);
3521 return ret;
3522}
3523
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003524static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3525{
Ville Syrjäläae981042016-08-05 23:28:30 +03003526 return intel_has_gpu_reset(dev_priv) &&
3527 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003528}
3529
Chris Wilsonc0336662016-05-06 15:40:21 +01003530void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003531{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003532 struct drm_device *dev = &dev_priv->drm;
3533 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3534 struct drm_atomic_state *state;
3535 int ret;
3536
Maarten Lankhorst73974892016-08-05 23:28:27 +03003537 /*
3538 * Need mode_config.mutex so that we don't
3539 * trample ongoing ->detect() and whatnot.
3540 */
3541 mutex_lock(&dev->mode_config.mutex);
3542 drm_modeset_acquire_init(ctx, 0);
3543 while (1) {
3544 ret = drm_modeset_lock_all_ctx(dev, ctx);
3545 if (ret != -EDEADLK)
3546 break;
3547
3548 drm_modeset_backoff(ctx);
3549 }
3550
3551 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003552 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003553 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003554 return;
3555
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003556 /*
3557 * Disabling the crtcs gracefully seems nicer. Also the
3558 * g33 docs say we should at least disable all the planes.
3559 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003560 state = drm_atomic_helper_duplicate_state(dev, ctx);
3561 if (IS_ERR(state)) {
3562 ret = PTR_ERR(state);
3563 state = NULL;
3564 DRM_ERROR("Duplicating state failed with %i\n", ret);
3565 goto err;
3566 }
3567
3568 ret = drm_atomic_helper_disable_all(dev, ctx);
3569 if (ret) {
3570 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3571 goto err;
3572 }
3573
3574 dev_priv->modeset_restore_state = state;
3575 state->acquire_ctx = ctx;
3576 return;
3577
3578err:
Chris Wilson08536952016-10-14 13:18:18 +01003579 drm_atomic_state_put(state);
Ville Syrjälä75147472014-11-24 18:28:11 +02003580}
3581
Chris Wilsonc0336662016-05-06 15:40:21 +01003582void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003583{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003584 struct drm_device *dev = &dev_priv->drm;
3585 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3586 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3587 int ret;
3588
Daniel Vetter5a21b662016-05-24 17:13:53 +02003589 /*
3590 * Flips in the rings will be nuked by the reset,
3591 * so complete all pending flips so that user space
3592 * will get its events and not get stuck.
3593 */
3594 intel_complete_page_flips(dev_priv);
3595
Maarten Lankhorst73974892016-08-05 23:28:27 +03003596 dev_priv->modeset_restore_state = NULL;
3597
Ville Syrjälä75147472014-11-24 18:28:11 +02003598 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003599 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003600 if (!state) {
3601 /*
3602 * Flips in the rings have been nuked by the reset,
3603 * so update the base address of all primary
3604 * planes to the the last fb to make sure we're
3605 * showing the correct fb after a reset.
3606 *
3607 * FIXME: Atomic will make this obsolete since we won't schedule
3608 * CS-based flips (which might get lost in gpu resets) any more.
3609 */
3610 intel_update_primary_planes(dev);
3611 } else {
3612 ret = __intel_display_resume(dev, state);
3613 if (ret)
3614 DRM_ERROR("Restoring old state failed with %i\n", ret);
3615 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003616 } else {
3617 /*
3618 * The display has been reset as well,
3619 * so need a full re-initialization.
3620 */
3621 intel_runtime_pm_disable_interrupts(dev_priv);
3622 intel_runtime_pm_enable_interrupts(dev_priv);
3623
Imre Deak51f59202016-09-14 13:04:13 +03003624 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003625 intel_modeset_init_hw(dev);
3626
3627 spin_lock_irq(&dev_priv->irq_lock);
3628 if (dev_priv->display.hpd_irq_setup)
3629 dev_priv->display.hpd_irq_setup(dev_priv);
3630 spin_unlock_irq(&dev_priv->irq_lock);
3631
3632 ret = __intel_display_resume(dev, state);
3633 if (ret)
3634 DRM_ERROR("Restoring old state failed with %i\n", ret);
3635
3636 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003637 }
3638
Chris Wilson08536952016-10-14 13:18:18 +01003639 if (state)
3640 drm_atomic_state_put(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003641 drm_modeset_drop_locks(ctx);
3642 drm_modeset_acquire_fini(ctx);
3643 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003644}
3645
Chris Wilson8af29b02016-09-09 14:11:47 +01003646static bool abort_flip_on_reset(struct intel_crtc *crtc)
3647{
3648 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3649
3650 if (i915_reset_in_progress(error))
3651 return true;
3652
3653 if (crtc->reset_count != i915_reset_count(error))
3654 return true;
3655
3656 return false;
3657}
3658
Chris Wilson7d5e3792014-03-04 13:15:08 +00003659static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3660{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003661 struct drm_device *dev = crtc->dev;
3662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003663 bool pending;
3664
Chris Wilson8af29b02016-09-09 14:11:47 +01003665 if (abort_flip_on_reset(intel_crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +02003666 return false;
3667
3668 spin_lock_irq(&dev->event_lock);
3669 pending = to_intel_crtc(crtc)->flip_work != NULL;
3670 spin_unlock_irq(&dev->event_lock);
3671
3672 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003673}
3674
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003675static void intel_update_pipe_config(struct intel_crtc *crtc,
3676 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003677{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003678 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003679 struct intel_crtc_state *pipe_config =
3680 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003681
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003682 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3683 crtc->base.mode = crtc->base.state->mode;
3684
3685 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3686 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3687 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003688
3689 /*
3690 * Update pipe size and adjust fitter if needed: the reason for this is
3691 * that in compute_mode_changes we check the native mode (not the pfit
3692 * mode) to see if we can flip rather than do a full mode set. In the
3693 * fastboot case, we'll flip, but if we don't update the pipesrc and
3694 * pfit state, we'll end up with a big fb scanned out into the wrong
3695 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003696 */
3697
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003698 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003699 ((pipe_config->pipe_src_w - 1) << 16) |
3700 (pipe_config->pipe_src_h - 1));
3701
3702 /* on skylake this is done by detaching scalers */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00003703 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003704 skl_detach_scalers(crtc);
3705
3706 if (pipe_config->pch_pfit.enabled)
3707 skylake_pfit_enable(crtc);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003708 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003709 if (pipe_config->pch_pfit.enabled)
3710 ironlake_pfit_enable(crtc);
3711 else if (old_crtc_state->pch_pfit.enabled)
3712 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003713 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003714}
3715
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003716static void intel_fdi_normal_train(struct drm_crtc *crtc)
3717{
3718 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003719 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3721 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003722 i915_reg_t reg;
3723 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003724
3725 /* enable normal train */
3726 reg = FDI_TX_CTL(pipe);
3727 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003728 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003729 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3730 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003731 } else {
3732 temp &= ~FDI_LINK_TRAIN_NONE;
3733 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003734 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003735 I915_WRITE(reg, temp);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003739 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003740 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3741 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3742 } else {
3743 temp &= ~FDI_LINK_TRAIN_NONE;
3744 temp |= FDI_LINK_TRAIN_NONE;
3745 }
3746 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3747
3748 /* wait one idle pattern time */
3749 POSTING_READ(reg);
3750 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003751
3752 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003753 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003754 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3755 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003756}
3757
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003758/* The FDI link training functions for ILK/Ibexpeak. */
3759static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3760{
3761 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003762 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3764 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003765 i915_reg_t reg;
3766 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003767
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003768 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003769 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003770
Adam Jacksone1a44742010-06-25 15:32:14 -04003771 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3772 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003773 reg = FDI_RX_IMR(pipe);
3774 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003775 temp &= ~FDI_RX_SYMBOL_LOCK;
3776 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003777 I915_WRITE(reg, temp);
3778 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003779 udelay(150);
3780
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003781 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003782 reg = FDI_TX_CTL(pipe);
3783 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003784 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003785 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003786 temp &= ~FDI_LINK_TRAIN_NONE;
3787 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003788 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003789
Chris Wilson5eddb702010-09-11 13:48:45 +01003790 reg = FDI_RX_CTL(pipe);
3791 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003792 temp &= ~FDI_LINK_TRAIN_NONE;
3793 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003794 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3795
3796 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003797 udelay(150);
3798
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003799 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003800 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3801 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3802 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003803
Chris Wilson5eddb702010-09-11 13:48:45 +01003804 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003805 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003806 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003807 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3808
3809 if ((temp & FDI_RX_BIT_LOCK)) {
3810 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003811 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003812 break;
3813 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003814 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003815 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003816 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003817
3818 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003819 reg = FDI_TX_CTL(pipe);
3820 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003821 temp &= ~FDI_LINK_TRAIN_NONE;
3822 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003823 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003824
Chris Wilson5eddb702010-09-11 13:48:45 +01003825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003827 temp &= ~FDI_LINK_TRAIN_NONE;
3828 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003829 I915_WRITE(reg, temp);
3830
3831 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003832 udelay(150);
3833
Chris Wilson5eddb702010-09-11 13:48:45 +01003834 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003835 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003836 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003837 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3838
3839 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003840 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003841 DRM_DEBUG_KMS("FDI train 2 done.\n");
3842 break;
3843 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003844 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003845 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003846 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003847
3848 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003849
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003850}
3851
Akshay Joshi0206e352011-08-16 15:34:10 -04003852static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003853 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3854 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3855 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3856 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3857};
3858
3859/* The FDI link training functions for SNB/Cougarpoint. */
3860static void gen6_fdi_link_train(struct drm_crtc *crtc)
3861{
3862 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003863 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003864 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3865 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003866 i915_reg_t reg;
3867 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003868
Adam Jacksone1a44742010-06-25 15:32:14 -04003869 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3870 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003871 reg = FDI_RX_IMR(pipe);
3872 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003873 temp &= ~FDI_RX_SYMBOL_LOCK;
3874 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003875 I915_WRITE(reg, temp);
3876
3877 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003878 udelay(150);
3879
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003880 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003881 reg = FDI_TX_CTL(pipe);
3882 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003883 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003884 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003885 temp &= ~FDI_LINK_TRAIN_NONE;
3886 temp |= FDI_LINK_TRAIN_PATTERN_1;
3887 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3888 /* SNB-B */
3889 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003890 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003891
Daniel Vetterd74cf322012-10-26 10:58:13 +02003892 I915_WRITE(FDI_RX_MISC(pipe),
3893 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3894
Chris Wilson5eddb702010-09-11 13:48:45 +01003895 reg = FDI_RX_CTL(pipe);
3896 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003897 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003898 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3899 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3900 } else {
3901 temp &= ~FDI_LINK_TRAIN_NONE;
3902 temp |= FDI_LINK_TRAIN_PATTERN_1;
3903 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003904 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3905
3906 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003907 udelay(150);
3908
Akshay Joshi0206e352011-08-16 15:34:10 -04003909 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003910 reg = FDI_TX_CTL(pipe);
3911 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003912 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3913 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003914 I915_WRITE(reg, temp);
3915
3916 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003917 udelay(500);
3918
Sean Paulfa37d392012-03-02 12:53:39 -05003919 for (retry = 0; retry < 5; retry++) {
3920 reg = FDI_RX_IIR(pipe);
3921 temp = I915_READ(reg);
3922 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3923 if (temp & FDI_RX_BIT_LOCK) {
3924 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3925 DRM_DEBUG_KMS("FDI train 1 done.\n");
3926 break;
3927 }
3928 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003929 }
Sean Paulfa37d392012-03-02 12:53:39 -05003930 if (retry < 5)
3931 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003932 }
3933 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003934 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003935
3936 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003937 reg = FDI_TX_CTL(pipe);
3938 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003939 temp &= ~FDI_LINK_TRAIN_NONE;
3940 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003941 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003942 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3943 /* SNB-B */
3944 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3945 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003946 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003947
Chris Wilson5eddb702010-09-11 13:48:45 +01003948 reg = FDI_RX_CTL(pipe);
3949 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003950 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003951 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3952 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3953 } else {
3954 temp &= ~FDI_LINK_TRAIN_NONE;
3955 temp |= FDI_LINK_TRAIN_PATTERN_2;
3956 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003957 I915_WRITE(reg, temp);
3958
3959 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003960 udelay(150);
3961
Akshay Joshi0206e352011-08-16 15:34:10 -04003962 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003963 reg = FDI_TX_CTL(pipe);
3964 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003965 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3966 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003967 I915_WRITE(reg, temp);
3968
3969 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003970 udelay(500);
3971
Sean Paulfa37d392012-03-02 12:53:39 -05003972 for (retry = 0; retry < 5; retry++) {
3973 reg = FDI_RX_IIR(pipe);
3974 temp = I915_READ(reg);
3975 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3976 if (temp & FDI_RX_SYMBOL_LOCK) {
3977 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3978 DRM_DEBUG_KMS("FDI train 2 done.\n");
3979 break;
3980 }
3981 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003982 }
Sean Paulfa37d392012-03-02 12:53:39 -05003983 if (retry < 5)
3984 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003985 }
3986 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003987 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003988
3989 DRM_DEBUG_KMS("FDI train done.\n");
3990}
3991
Jesse Barnes357555c2011-04-28 15:09:55 -07003992/* Manual link training for Ivy Bridge A0 parts */
3993static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3994{
3995 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003996 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3998 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003999 i915_reg_t reg;
4000 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004001
4002 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4003 for train result */
4004 reg = FDI_RX_IMR(pipe);
4005 temp = I915_READ(reg);
4006 temp &= ~FDI_RX_SYMBOL_LOCK;
4007 temp &= ~FDI_RX_BIT_LOCK;
4008 I915_WRITE(reg, temp);
4009
4010 POSTING_READ(reg);
4011 udelay(150);
4012
Daniel Vetter01a415f2012-10-27 15:58:40 +02004013 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4014 I915_READ(FDI_RX_IIR(pipe)));
4015
Jesse Barnes139ccd32013-08-19 11:04:55 -07004016 /* Try each vswing and preemphasis setting twice before moving on */
4017 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4018 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004019 reg = FDI_TX_CTL(pipe);
4020 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004021 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4022 temp &= ~FDI_TX_ENABLE;
4023 I915_WRITE(reg, temp);
4024
4025 reg = FDI_RX_CTL(pipe);
4026 temp = I915_READ(reg);
4027 temp &= ~FDI_LINK_TRAIN_AUTO;
4028 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4029 temp &= ~FDI_RX_ENABLE;
4030 I915_WRITE(reg, temp);
4031
4032 /* enable CPU FDI TX and PCH FDI RX */
4033 reg = FDI_TX_CTL(pipe);
4034 temp = I915_READ(reg);
4035 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004036 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004037 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004038 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004039 temp |= snb_b_fdi_train_param[j/2];
4040 temp |= FDI_COMPOSITE_SYNC;
4041 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4042
4043 I915_WRITE(FDI_RX_MISC(pipe),
4044 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4045
4046 reg = FDI_RX_CTL(pipe);
4047 temp = I915_READ(reg);
4048 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4049 temp |= FDI_COMPOSITE_SYNC;
4050 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4051
4052 POSTING_READ(reg);
4053 udelay(1); /* should be 0.5us */
4054
4055 for (i = 0; i < 4; i++) {
4056 reg = FDI_RX_IIR(pipe);
4057 temp = I915_READ(reg);
4058 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4059
4060 if (temp & FDI_RX_BIT_LOCK ||
4061 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4062 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4063 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4064 i);
4065 break;
4066 }
4067 udelay(1); /* should be 0.5us */
4068 }
4069 if (i == 4) {
4070 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4071 continue;
4072 }
4073
4074 /* Train 2 */
4075 reg = FDI_TX_CTL(pipe);
4076 temp = I915_READ(reg);
4077 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4078 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4079 I915_WRITE(reg, temp);
4080
4081 reg = FDI_RX_CTL(pipe);
4082 temp = I915_READ(reg);
4083 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4084 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004085 I915_WRITE(reg, temp);
4086
4087 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004088 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004089
Jesse Barnes139ccd32013-08-19 11:04:55 -07004090 for (i = 0; i < 4; i++) {
4091 reg = FDI_RX_IIR(pipe);
4092 temp = I915_READ(reg);
4093 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004094
Jesse Barnes139ccd32013-08-19 11:04:55 -07004095 if (temp & FDI_RX_SYMBOL_LOCK ||
4096 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4097 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4098 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4099 i);
4100 goto train_done;
4101 }
4102 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004103 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004104 if (i == 4)
4105 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004106 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004107
Jesse Barnes139ccd32013-08-19 11:04:55 -07004108train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004109 DRM_DEBUG_KMS("FDI train done.\n");
4110}
4111
Daniel Vetter88cefb62012-08-12 19:27:14 +02004112static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004113{
Daniel Vetter88cefb62012-08-12 19:27:14 +02004114 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004115 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004116 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004117 i915_reg_t reg;
4118 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004119
Jesse Barnes0e23b992010-09-10 11:10:00 -07004120 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004121 reg = FDI_RX_CTL(pipe);
4122 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004123 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004124 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004125 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004126 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4127
4128 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004129 udelay(200);
4130
4131 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004132 temp = I915_READ(reg);
4133 I915_WRITE(reg, temp | FDI_PCDCLK);
4134
4135 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004136 udelay(200);
4137
Paulo Zanoni20749732012-11-23 15:30:38 -02004138 /* Enable CPU FDI TX PLL, always on for Ironlake */
4139 reg = FDI_TX_CTL(pipe);
4140 temp = I915_READ(reg);
4141 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4142 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004143
Paulo Zanoni20749732012-11-23 15:30:38 -02004144 POSTING_READ(reg);
4145 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004146 }
4147}
4148
Daniel Vetter88cefb62012-08-12 19:27:14 +02004149static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4150{
4151 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004152 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004153 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004154 i915_reg_t reg;
4155 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004156
4157 /* Switch from PCDclk to Rawclk */
4158 reg = FDI_RX_CTL(pipe);
4159 temp = I915_READ(reg);
4160 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4161
4162 /* Disable CPU FDI TX PLL */
4163 reg = FDI_TX_CTL(pipe);
4164 temp = I915_READ(reg);
4165 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4166
4167 POSTING_READ(reg);
4168 udelay(100);
4169
4170 reg = FDI_RX_CTL(pipe);
4171 temp = I915_READ(reg);
4172 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4173
4174 /* Wait for the clocks to turn off. */
4175 POSTING_READ(reg);
4176 udelay(100);
4177}
4178
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004179static void ironlake_fdi_disable(struct drm_crtc *crtc)
4180{
4181 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004182 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4184 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004185 i915_reg_t reg;
4186 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004187
4188 /* disable CPU FDI tx and PCH FDI rx */
4189 reg = FDI_TX_CTL(pipe);
4190 temp = I915_READ(reg);
4191 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4192 POSTING_READ(reg);
4193
4194 reg = FDI_RX_CTL(pipe);
4195 temp = I915_READ(reg);
4196 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004197 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004198 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4199
4200 POSTING_READ(reg);
4201 udelay(100);
4202
4203 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004204 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004205 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004206
4207 /* still set train pattern 1 */
4208 reg = FDI_TX_CTL(pipe);
4209 temp = I915_READ(reg);
4210 temp &= ~FDI_LINK_TRAIN_NONE;
4211 temp |= FDI_LINK_TRAIN_PATTERN_1;
4212 I915_WRITE(reg, temp);
4213
4214 reg = FDI_RX_CTL(pipe);
4215 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004216 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004217 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4218 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4219 } else {
4220 temp &= ~FDI_LINK_TRAIN_NONE;
4221 temp |= FDI_LINK_TRAIN_PATTERN_1;
4222 }
4223 /* BPC in FDI rx is consistent with that in PIPECONF */
4224 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004225 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004226 I915_WRITE(reg, temp);
4227
4228 POSTING_READ(reg);
4229 udelay(100);
4230}
4231
Chris Wilson49d73912016-11-29 09:50:08 +00004232bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004233{
4234 struct intel_crtc *crtc;
4235
4236 /* Note that we don't need to be called with mode_config.lock here
4237 * as our list of CRTC objects is static for the lifetime of the
4238 * device and so cannot disappear as we iterate. Similarly, we can
4239 * happily treat the predicates as racy, atomic checks as userspace
4240 * cannot claim and pin a new fb without at least acquring the
4241 * struct_mutex and so serialising with us.
4242 */
Chris Wilson49d73912016-11-29 09:50:08 +00004243 for_each_intel_crtc(&dev_priv->drm, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00004244 if (atomic_read(&crtc->unpin_work_count) == 0)
4245 continue;
4246
Daniel Vetter5a21b662016-05-24 17:13:53 +02004247 if (crtc->flip_work)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004248 intel_wait_for_vblank(dev_priv, crtc->pipe);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004249
4250 return true;
4251 }
4252
4253 return false;
4254}
4255
Daniel Vetter5a21b662016-05-24 17:13:53 +02004256static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004257{
4258 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004259 struct intel_flip_work *work = intel_crtc->flip_work;
4260
4261 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004262
4263 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07004264 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004265
4266 drm_crtc_vblank_put(&intel_crtc->base);
4267
Daniel Vetter5a21b662016-05-24 17:13:53 +02004268 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02004269 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004270
4271 trace_i915_flip_complete(intel_crtc->plane,
4272 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004273}
4274
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004275static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004276{
Chris Wilson0f911282012-04-17 10:05:38 +01004277 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004278 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004279 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004280
Daniel Vetter2c10d572012-12-20 21:24:07 +01004281 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004282
4283 ret = wait_event_interruptible_timeout(
4284 dev_priv->pending_flip_queue,
4285 !intel_crtc_has_pending_flip(crtc),
4286 60*HZ);
4287
4288 if (ret < 0)
4289 return ret;
4290
Daniel Vetter5a21b662016-05-24 17:13:53 +02004291 if (ret == 0) {
4292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4293 struct intel_flip_work *work;
4294
4295 spin_lock_irq(&dev->event_lock);
4296 work = intel_crtc->flip_work;
4297 if (work && !is_mmio_work(work)) {
4298 WARN_ONCE(1, "Removing stuck page flip\n");
4299 page_flip_completed(intel_crtc);
4300 }
4301 spin_unlock_irq(&dev->event_lock);
4302 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004303
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004304 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004305}
4306
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004307void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004308{
4309 u32 temp;
4310
4311 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4312
4313 mutex_lock(&dev_priv->sb_lock);
4314
4315 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4316 temp |= SBI_SSCCTL_DISABLE;
4317 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4318
4319 mutex_unlock(&dev_priv->sb_lock);
4320}
4321
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004322/* Program iCLKIP clock to the desired frequency */
4323static void lpt_program_iclkip(struct drm_crtc *crtc)
4324{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004325 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004326 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004327 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4328 u32 temp;
4329
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004330 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004331
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004332 /* The iCLK virtual clock root frequency is in MHz,
4333 * but the adjusted_mode->crtc_clock in in KHz. To get the
4334 * divisors, it is necessary to divide one by another, so we
4335 * convert the virtual clock precision to KHz here for higher
4336 * precision.
4337 */
4338 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004339 u32 iclk_virtual_root_freq = 172800 * 1000;
4340 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004341 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004342
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004343 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4344 clock << auxdiv);
4345 divsel = (desired_divisor / iclk_pi_range) - 2;
4346 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004347
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004348 /*
4349 * Near 20MHz is a corner case which is
4350 * out of range for the 7-bit divisor
4351 */
4352 if (divsel <= 0x7f)
4353 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004354 }
4355
4356 /* This should not happen with any sane values */
4357 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4358 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4359 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4360 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4361
4362 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 +03004363 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004364 auxdiv,
4365 divsel,
4366 phasedir,
4367 phaseinc);
4368
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004369 mutex_lock(&dev_priv->sb_lock);
4370
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004371 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004372 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004373 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4374 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4375 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4376 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4377 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4378 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004379 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004380
4381 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004382 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004383 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4384 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004385 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004386
4387 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004388 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004389 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004390 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004391
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004392 mutex_unlock(&dev_priv->sb_lock);
4393
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004394 /* Wait for initialization time */
4395 udelay(24);
4396
4397 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4398}
4399
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004400int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4401{
4402 u32 divsel, phaseinc, auxdiv;
4403 u32 iclk_virtual_root_freq = 172800 * 1000;
4404 u32 iclk_pi_range = 64;
4405 u32 desired_divisor;
4406 u32 temp;
4407
4408 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4409 return 0;
4410
4411 mutex_lock(&dev_priv->sb_lock);
4412
4413 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4414 if (temp & SBI_SSCCTL_DISABLE) {
4415 mutex_unlock(&dev_priv->sb_lock);
4416 return 0;
4417 }
4418
4419 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4420 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4421 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4422 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4423 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4424
4425 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4426 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4427 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4428
4429 mutex_unlock(&dev_priv->sb_lock);
4430
4431 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4432
4433 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4434 desired_divisor << auxdiv);
4435}
4436
Daniel Vetter275f01b22013-05-03 11:49:47 +02004437static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4438 enum pipe pch_transcoder)
4439{
4440 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004441 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004442 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004443
4444 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4445 I915_READ(HTOTAL(cpu_transcoder)));
4446 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4447 I915_READ(HBLANK(cpu_transcoder)));
4448 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4449 I915_READ(HSYNC(cpu_transcoder)));
4450
4451 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4452 I915_READ(VTOTAL(cpu_transcoder)));
4453 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4454 I915_READ(VBLANK(cpu_transcoder)));
4455 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4456 I915_READ(VSYNC(cpu_transcoder)));
4457 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4458 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4459}
4460
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004461static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004462{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004463 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004464 uint32_t temp;
4465
4466 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004467 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004468 return;
4469
4470 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4471 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4472
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004473 temp &= ~FDI_BC_BIFURCATION_SELECT;
4474 if (enable)
4475 temp |= FDI_BC_BIFURCATION_SELECT;
4476
4477 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004478 I915_WRITE(SOUTH_CHICKEN1, temp);
4479 POSTING_READ(SOUTH_CHICKEN1);
4480}
4481
4482static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4483{
4484 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004485
4486 switch (intel_crtc->pipe) {
4487 case PIPE_A:
4488 break;
4489 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004490 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004491 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004492 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004493 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004494
4495 break;
4496 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004497 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004498
4499 break;
4500 default:
4501 BUG();
4502 }
4503}
4504
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004505/* Return which DP Port should be selected for Transcoder DP control */
4506static enum port
4507intel_trans_dp_port_sel(struct drm_crtc *crtc)
4508{
4509 struct drm_device *dev = crtc->dev;
4510 struct intel_encoder *encoder;
4511
4512 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004513 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004514 encoder->type == INTEL_OUTPUT_EDP)
4515 return enc_to_dig_port(&encoder->base)->port;
4516 }
4517
4518 return -1;
4519}
4520
Jesse Barnesf67a5592011-01-05 10:31:48 -08004521/*
4522 * Enable PCH resources required for PCH ports:
4523 * - PCH PLLs
4524 * - FDI training & RX/TX
4525 * - update transcoder timings
4526 * - DP transcoding bits
4527 * - transcoder
4528 */
4529static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004530{
4531 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004532 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4534 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004535 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004536
Daniel Vetterab9412b2013-05-03 11:49:46 +02004537 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004538
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004539 if (IS_IVYBRIDGE(dev_priv))
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004540 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4541
Daniel Vettercd986ab2012-10-26 10:58:12 +02004542 /* Write the TU size bits before fdi link training, so that error
4543 * detection works. */
4544 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4545 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4546
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004547 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004548 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004549
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004550 /* We need to program the right clock selection before writing the pixel
4551 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004552 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004553 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004554
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004555 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004556 temp |= TRANS_DPLL_ENABLE(pipe);
4557 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004558 if (intel_crtc->config->shared_dpll ==
4559 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004560 temp |= sel;
4561 else
4562 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004563 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004564 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004565
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004566 /* XXX: pch pll's can be enabled any time before we enable the PCH
4567 * transcoder, and we actually should do this to not upset any PCH
4568 * transcoder that already use the clock when we share it.
4569 *
4570 * Note that enable_shared_dpll tries to do the right thing, but
4571 * get_shared_dpll unconditionally resets the pll - we need that to have
4572 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004573 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004574
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004575 /* set transcoder timing, panel must allow it */
4576 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004577 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004578
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004579 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004580
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004581 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004582 if (HAS_PCH_CPT(dev_priv) &&
4583 intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004584 const struct drm_display_mode *adjusted_mode =
4585 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004586 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004587 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004588 temp = I915_READ(reg);
4589 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004590 TRANS_DP_SYNC_MASK |
4591 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004592 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004593 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004594
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004595 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004596 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004597 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004598 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004599
4600 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004601 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004602 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004603 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004604 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004605 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004606 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004607 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004608 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004609 break;
4610 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004611 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004612 }
4613
Chris Wilson5eddb702010-09-11 13:48:45 +01004614 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004615 }
4616
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004617 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004618}
4619
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004620static void lpt_pch_enable(struct drm_crtc *crtc)
4621{
4622 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004623 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004625 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004626
Daniel Vetterab9412b2013-05-03 11:49:46 +02004627 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004628
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004629 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004630
Paulo Zanoni0540e482012-10-31 18:12:40 -02004631 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004632 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004633
Paulo Zanoni937bb612012-10-31 18:12:47 -02004634 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004635}
4636
Daniel Vettera1520312013-05-03 11:49:50 +02004637static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004638{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004639 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004640 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004641 u32 temp;
4642
4643 temp = I915_READ(dslreg);
4644 udelay(500);
4645 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004646 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004647 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004648 }
4649}
4650
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004651static int
4652skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4653 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4654 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004655{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004656 struct intel_crtc_scaler_state *scaler_state =
4657 &crtc_state->scaler_state;
4658 struct intel_crtc *intel_crtc =
4659 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004660 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004661
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004662 need_scaling = drm_rotation_90_or_270(rotation) ?
Chandra Konduru6156a452015-04-27 13:48:39 -07004663 (src_h != dst_w || src_w != dst_h):
4664 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004665
4666 /*
4667 * if plane is being disabled or scaler is no more required or force detach
4668 * - free scaler binded to this plane/crtc
4669 * - in order to do this, update crtc->scaler_usage
4670 *
4671 * Here scaler state in crtc_state is set free so that
4672 * scaler can be assigned to other user. Actual register
4673 * update to free the scaler is done in plane/panel-fit programming.
4674 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4675 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004676 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004677 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004678 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004679 scaler_state->scalers[*scaler_id].in_use = 0;
4680
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004681 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4682 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4683 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004684 scaler_state->scaler_users);
4685 *scaler_id = -1;
4686 }
4687 return 0;
4688 }
4689
4690 /* range checks */
4691 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4692 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4693
4694 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4695 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004696 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004697 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004698 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004699 return -EINVAL;
4700 }
4701
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004702 /* mark this plane as a scaler user in crtc_state */
4703 scaler_state->scaler_users |= (1 << scaler_user);
4704 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4705 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4706 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4707 scaler_state->scaler_users);
4708
4709 return 0;
4710}
4711
4712/**
4713 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4714 *
4715 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004716 *
4717 * Return
4718 * 0 - scaler_usage updated successfully
4719 * error - requested scaling cannot be supported or other error condition
4720 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004721int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004722{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004723 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004724
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004725 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004726 &state->scaler_state.scaler_id, DRM_ROTATE_0,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004727 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004728 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004729}
4730
4731/**
4732 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4733 *
4734 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004735 * @plane_state: atomic plane state to update
4736 *
4737 * Return
4738 * 0 - scaler_usage updated successfully
4739 * error - requested scaling cannot be supported or other error condition
4740 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004741static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4742 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004743{
4744
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004745 struct intel_plane *intel_plane =
4746 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004747 struct drm_framebuffer *fb = plane_state->base.fb;
4748 int ret;
4749
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004750 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004751
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004752 ret = skl_update_scaler(crtc_state, force_detach,
4753 drm_plane_index(&intel_plane->base),
4754 &plane_state->scaler_id,
4755 plane_state->base.rotation,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004756 drm_rect_width(&plane_state->base.src) >> 16,
4757 drm_rect_height(&plane_state->base.src) >> 16,
4758 drm_rect_width(&plane_state->base.dst),
4759 drm_rect_height(&plane_state->base.dst));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004760
4761 if (ret || plane_state->scaler_id < 0)
4762 return ret;
4763
Chandra Kondurua1b22782015-04-07 15:28:45 -07004764 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004765 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004766 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4767 intel_plane->base.base.id,
4768 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004769 return -EINVAL;
4770 }
4771
4772 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004773 switch (fb->pixel_format) {
4774 case DRM_FORMAT_RGB565:
4775 case DRM_FORMAT_XBGR8888:
4776 case DRM_FORMAT_XRGB8888:
4777 case DRM_FORMAT_ABGR8888:
4778 case DRM_FORMAT_ARGB8888:
4779 case DRM_FORMAT_XRGB2101010:
4780 case DRM_FORMAT_XBGR2101010:
4781 case DRM_FORMAT_YUYV:
4782 case DRM_FORMAT_YVYU:
4783 case DRM_FORMAT_UYVY:
4784 case DRM_FORMAT_VYUY:
4785 break;
4786 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004787 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4788 intel_plane->base.base.id, intel_plane->base.name,
4789 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004790 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004791 }
4792
Chandra Kondurua1b22782015-04-07 15:28:45 -07004793 return 0;
4794}
4795
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004796static void skylake_scaler_disable(struct intel_crtc *crtc)
4797{
4798 int i;
4799
4800 for (i = 0; i < crtc->num_scalers; i++)
4801 skl_detach_scaler(crtc, i);
4802}
4803
4804static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004805{
4806 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004807 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004808 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004809 struct intel_crtc_scaler_state *scaler_state =
4810 &crtc->config->scaler_state;
4811
4812 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4813
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004814 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004815 int id;
4816
4817 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4818 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4819 return;
4820 }
4821
4822 id = scaler_state->scaler_id;
4823 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4824 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4825 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4826 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4827
4828 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004829 }
4830}
4831
Jesse Barnesb074cec2013-04-25 12:55:02 -07004832static void ironlake_pfit_enable(struct intel_crtc *crtc)
4833{
4834 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004835 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004836 int pipe = crtc->pipe;
4837
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004838 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004839 /* Force use of hard-coded filter coefficients
4840 * as some pre-programmed values are broken,
4841 * e.g. x201.
4842 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004843 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07004844 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4845 PF_PIPE_SEL_IVB(pipe));
4846 else
4847 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004848 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4849 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004850 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004851}
4852
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004853void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004854{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004855 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004856 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004857
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004858 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004859 return;
4860
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004861 /*
4862 * We can only enable IPS after we enable a plane and wait for a vblank
4863 * This function is called from post_plane_update, which is run after
4864 * a vblank wait.
4865 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004866
Paulo Zanonid77e4532013-09-24 13:52:55 -03004867 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004868 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004869 mutex_lock(&dev_priv->rps.hw_lock);
4870 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4871 mutex_unlock(&dev_priv->rps.hw_lock);
4872 /* Quoting Art Runyan: "its not safe to expect any particular
4873 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004874 * mailbox." Moreover, the mailbox may return a bogus state,
4875 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004876 */
4877 } else {
4878 I915_WRITE(IPS_CTL, IPS_ENABLE);
4879 /* The bit only becomes 1 in the next vblank, so this wait here
4880 * is essentially intel_wait_for_vblank. If we don't have this
4881 * and don't wait for vblanks until the end of crtc_enable, then
4882 * the HW state readout code will complain that the expected
4883 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004884 if (intel_wait_for_register(dev_priv,
4885 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4886 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004887 DRM_ERROR("Timed out waiting for IPS enable\n");
4888 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004889}
4890
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004891void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004892{
4893 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004894 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004895
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004896 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004897 return;
4898
4899 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004900 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004901 mutex_lock(&dev_priv->rps.hw_lock);
4902 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4903 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004904 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004905 if (intel_wait_for_register(dev_priv,
4906 IPS_CTL, IPS_ENABLE, 0,
4907 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004908 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004909 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004910 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004911 POSTING_READ(IPS_CTL);
4912 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004913
4914 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004915 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004916}
4917
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004918static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004919{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004920 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004921 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004922 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004923
4924 mutex_lock(&dev->struct_mutex);
4925 dev_priv->mm.interruptible = false;
4926 (void) intel_overlay_switch_off(intel_crtc->overlay);
4927 dev_priv->mm.interruptible = true;
4928 mutex_unlock(&dev->struct_mutex);
4929 }
4930
4931 /* Let userspace switch the overlay on again. In most cases userspace
4932 * has to recompute where to put it anyway.
4933 */
4934}
4935
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004936/**
4937 * intel_post_enable_primary - Perform operations after enabling primary plane
4938 * @crtc: the CRTC whose primary plane was just enabled
4939 *
4940 * Performs potentially sleeping operations that must be done after the primary
4941 * plane is enabled, such as updating FBC and IPS. Note that this may be
4942 * called due to an explicit primary plane update, or due to an implicit
4943 * re-enable that is caused when a sprite plane is updated to no longer
4944 * completely hide the primary plane.
4945 */
4946static void
4947intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004948{
4949 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004950 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4952 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004953
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004954 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004955 * FIXME IPS should be fine as long as one plane is
4956 * enabled, but in practice it seems to have problems
4957 * when going from primary only to sprite only and vice
4958 * versa.
4959 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004960 hsw_enable_ips(intel_crtc);
4961
Daniel Vetterf99d7062014-06-19 16:01:59 +02004962 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004963 * Gen2 reports pipe underruns whenever all planes are disabled.
4964 * So don't enable underrun reporting before at least some planes
4965 * are enabled.
4966 * FIXME: Need to fix the logic to work when we turn off all planes
4967 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004968 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004969 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004970 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4971
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004972 /* Underruns don't always raise interrupts, so check manually. */
4973 intel_check_cpu_fifo_underruns(dev_priv);
4974 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004975}
4976
Ville Syrjälä2622a082016-03-09 19:07:26 +02004977/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004978static void
4979intel_pre_disable_primary(struct drm_crtc *crtc)
4980{
4981 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004982 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4984 int pipe = intel_crtc->pipe;
4985
4986 /*
4987 * Gen2 reports pipe underruns whenever all planes are disabled.
4988 * So diasble underrun reporting before all the planes get disabled.
4989 * FIXME: Need to fix the logic to work when we turn off all planes
4990 * but leave the pipe running.
4991 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004992 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4994
4995 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004996 * FIXME IPS should be fine as long as one plane is
4997 * enabled, but in practice it seems to have problems
4998 * when going from primary only to sprite only and vice
4999 * versa.
5000 */
5001 hsw_disable_ips(intel_crtc);
5002}
5003
5004/* FIXME get rid of this and use pre_plane_update */
5005static void
5006intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5007{
5008 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005009 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5011 int pipe = intel_crtc->pipe;
5012
5013 intel_pre_disable_primary(crtc);
5014
5015 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005016 * Vblank time updates from the shadow to live plane control register
5017 * are blocked if the memory self-refresh mode is active at that
5018 * moment. So to make sure the plane gets truly disabled, disable
5019 * first the self-refresh mode. The self-refresh enable bit in turn
5020 * will be checked/applied by the HW only at the next frame start
5021 * event which is after the vblank start event, so we need to have a
5022 * wait-for-vblank between disabling the plane and the pipe.
5023 */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005024 if (HAS_GMCH_DISPLAY(dev_priv)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005025 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03005026 dev_priv->wm.vlv.cxsr = false;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005027 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03005028 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005029}
5030
Daniel Vetter5a21b662016-05-24 17:13:53 +02005031static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5032{
5033 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5034 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5035 struct intel_crtc_state *pipe_config =
5036 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005037 struct drm_plane *primary = crtc->base.primary;
5038 struct drm_plane_state *old_pri_state =
5039 drm_atomic_get_existing_plane_state(old_state, primary);
5040
Chris Wilson5748b6a2016-08-04 16:32:38 +01005041 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005042
5043 crtc->wm.cxsr_allowed = true;
5044
5045 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005046 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005047
5048 if (old_pri_state) {
5049 struct intel_plane_state *primary_state =
5050 to_intel_plane_state(primary->state);
5051 struct intel_plane_state *old_primary_state =
5052 to_intel_plane_state(old_pri_state);
5053
5054 intel_fbc_post_update(crtc);
5055
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005056 if (primary_state->base.visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005057 (needs_modeset(&pipe_config->base) ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005058 !old_primary_state->base.visible))
Daniel Vetter5a21b662016-05-24 17:13:53 +02005059 intel_post_enable_primary(&crtc->base);
5060 }
5061}
5062
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005063static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005064{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005065 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005066 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005067 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01005068 struct intel_crtc_state *pipe_config =
5069 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005070 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5071 struct drm_plane *primary = crtc->base.primary;
5072 struct drm_plane_state *old_pri_state =
5073 drm_atomic_get_existing_plane_state(old_state, primary);
5074 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005075 struct intel_atomic_state *old_intel_state =
5076 to_intel_atomic_state(old_state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005077
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005078 if (old_pri_state) {
5079 struct intel_plane_state *primary_state =
5080 to_intel_plane_state(primary->state);
5081 struct intel_plane_state *old_primary_state =
5082 to_intel_plane_state(old_pri_state);
5083
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02005084 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01005085
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005086 if (old_primary_state->base.visible &&
5087 (modeset || !primary_state->base.visible))
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005088 intel_pre_disable_primary(&crtc->base);
5089 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005090
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005091 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03005092 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005093
Ville Syrjälä2622a082016-03-09 19:07:26 +02005094 /*
5095 * Vblank time updates from the shadow to live plane control register
5096 * are blocked if the memory self-refresh mode is active at that
5097 * moment. So to make sure the plane gets truly disabled, disable
5098 * first the self-refresh mode. The self-refresh enable bit in turn
5099 * will be checked/applied by the HW only at the next frame start
5100 * event which is after the vblank start event, so we need to have a
5101 * wait-for-vblank between disabling the plane and the pipe.
5102 */
5103 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005104 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005105 dev_priv->wm.vlv.cxsr = false;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005106 intel_wait_for_vblank(dev_priv, crtc->pipe);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005107 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005108 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005109
Matt Ropered4a6a72016-02-23 17:20:13 -08005110 /*
5111 * IVB workaround: must disable low power watermarks for at least
5112 * one frame before enabling scaling. LP watermarks can be re-enabled
5113 * when scaling is disabled.
5114 *
5115 * WaCxSRDisabledForSpriteScaling:ivb
5116 */
5117 if (pipe_config->disable_lp_wm) {
5118 ilk_disable_lp_wm(dev);
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005119 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005120 }
5121
5122 /*
5123 * If we're doing a modeset, we're done. No need to do any pre-vblank
5124 * watermark programming here.
5125 */
5126 if (needs_modeset(&pipe_config->base))
5127 return;
5128
5129 /*
5130 * For platforms that support atomic watermarks, program the
5131 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5132 * will be the intermediate values that are safe for both pre- and
5133 * post- vblank; when vblank happens, the 'active' values will be set
5134 * to the final 'target' values and we'll do this again to get the
5135 * optimal watermarks. For gen9+ platforms, the values we program here
5136 * will be the final target values which will get automatically latched
5137 * at vblank time; no further programming will be necessary.
5138 *
5139 * If a platform hasn't been transitioned to atomic watermarks yet,
5140 * we'll continue to update watermarks the old way, if flags tell
5141 * us to.
5142 */
5143 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005144 dev_priv->display.initial_watermarks(old_intel_state,
5145 pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005146 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005147 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005148}
5149
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005150static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005151{
5152 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005154 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005155 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005156
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005157 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005158
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005159 drm_for_each_plane_mask(p, dev, plane_mask)
5160 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005161
Daniel Vetterf99d7062014-06-19 16:01:59 +02005162 /*
5163 * FIXME: Once we grow proper nuclear flip support out of this we need
5164 * to compute the mask of flip planes precisely. For the time being
5165 * consider this a flip to a NULL plane.
5166 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01005167 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005168}
5169
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005170static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005171 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005172 struct drm_atomic_state *old_state)
5173{
5174 struct drm_connector_state *old_conn_state;
5175 struct drm_connector *conn;
5176 int i;
5177
5178 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5179 struct drm_connector_state *conn_state = conn->state;
5180 struct intel_encoder *encoder =
5181 to_intel_encoder(conn_state->best_encoder);
5182
5183 if (conn_state->crtc != crtc)
5184 continue;
5185
5186 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005187 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005188 }
5189}
5190
5191static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005192 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005193 struct drm_atomic_state *old_state)
5194{
5195 struct drm_connector_state *old_conn_state;
5196 struct drm_connector *conn;
5197 int i;
5198
5199 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5200 struct drm_connector_state *conn_state = conn->state;
5201 struct intel_encoder *encoder =
5202 to_intel_encoder(conn_state->best_encoder);
5203
5204 if (conn_state->crtc != crtc)
5205 continue;
5206
5207 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005208 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005209 }
5210}
5211
5212static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005213 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005214 struct drm_atomic_state *old_state)
5215{
5216 struct drm_connector_state *old_conn_state;
5217 struct drm_connector *conn;
5218 int i;
5219
5220 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5221 struct drm_connector_state *conn_state = conn->state;
5222 struct intel_encoder *encoder =
5223 to_intel_encoder(conn_state->best_encoder);
5224
5225 if (conn_state->crtc != crtc)
5226 continue;
5227
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005228 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005229 intel_opregion_notify_encoder(encoder, true);
5230 }
5231}
5232
5233static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005234 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005235 struct drm_atomic_state *old_state)
5236{
5237 struct drm_connector_state *old_conn_state;
5238 struct drm_connector *conn;
5239 int i;
5240
5241 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5242 struct intel_encoder *encoder =
5243 to_intel_encoder(old_conn_state->best_encoder);
5244
5245 if (old_conn_state->crtc != crtc)
5246 continue;
5247
5248 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005249 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005250 }
5251}
5252
5253static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005254 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005255 struct drm_atomic_state *old_state)
5256{
5257 struct drm_connector_state *old_conn_state;
5258 struct drm_connector *conn;
5259 int i;
5260
5261 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5262 struct intel_encoder *encoder =
5263 to_intel_encoder(old_conn_state->best_encoder);
5264
5265 if (old_conn_state->crtc != crtc)
5266 continue;
5267
5268 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005269 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005270 }
5271}
5272
5273static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005274 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005275 struct drm_atomic_state *old_state)
5276{
5277 struct drm_connector_state *old_conn_state;
5278 struct drm_connector *conn;
5279 int i;
5280
5281 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5282 struct intel_encoder *encoder =
5283 to_intel_encoder(old_conn_state->best_encoder);
5284
5285 if (old_conn_state->crtc != crtc)
5286 continue;
5287
5288 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005289 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005290 }
5291}
5292
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005293static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5294 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005295{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005296 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005297 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005298 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5300 int pipe = intel_crtc->pipe;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005301 struct intel_atomic_state *old_intel_state =
5302 to_intel_atomic_state(old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005303
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005304 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005305 return;
5306
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005307 /*
5308 * Sometimes spurious CPU pipe underruns happen during FDI
5309 * training, at least with VGA+HDMI cloning. Suppress them.
5310 *
5311 * On ILK we get an occasional spurious CPU pipe underruns
5312 * between eDP port A enable and vdd enable. Also PCH port
5313 * enable seems to result in the occasional CPU pipe underrun.
5314 *
5315 * Spurious PCH underruns also occur during PCH enabling.
5316 */
5317 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5318 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005319 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005320 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5321
5322 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005323 intel_prepare_shared_dpll(intel_crtc);
5324
Ville Syrjälä37a56502016-06-22 21:57:04 +03005325 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305326 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005327
5328 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005329 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005330
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005331 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005332 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005333 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005334 }
5335
5336 ironlake_set_pipeconf(crtc);
5337
Jesse Barnesf67a5592011-01-05 10:31:48 -08005338 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005339
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005340 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005341
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005342 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005343 /* Note: FDI PLL enabling _must_ be done before we enable the
5344 * cpu pipes, hence this is separate from all the other fdi/pch
5345 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005346 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005347 } else {
5348 assert_fdi_tx_disabled(dev_priv, pipe);
5349 assert_fdi_rx_disabled(dev_priv, pipe);
5350 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005351
Jesse Barnesb074cec2013-04-25 12:55:02 -07005352 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005353
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005354 /*
5355 * On ILK+ LUT must be loaded before the pipe is running but with
5356 * clocks enabled
5357 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005358 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005359
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005360 if (dev_priv->display.initial_watermarks != NULL)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005361 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005362 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005363
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005364 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005365 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005366
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005367 assert_vblank_disabled(crtc);
5368 drm_crtc_vblank_on(crtc);
5369
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005370 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005371
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005372 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005373 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005374
5375 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5376 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005377 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005378 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005379 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005380}
5381
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005382/* IPS only exists on ULT machines and is tied to pipe A. */
5383static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5384{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005385 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005386}
5387
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005388static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5389 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005390{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005391 struct drm_crtc *crtc = pipe_config->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005392 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005394 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005395 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005396 struct intel_atomic_state *old_intel_state =
5397 to_intel_atomic_state(old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005398
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005399 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005400 return;
5401
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005402 if (intel_crtc->config->has_pch_encoder)
5403 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5404 false);
5405
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005406 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005407
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005408 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005409 intel_enable_shared_dpll(intel_crtc);
5410
Ville Syrjälä37a56502016-06-22 21:57:04 +03005411 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305412 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005413
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005414 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005415 intel_set_pipe_timings(intel_crtc);
5416
Jani Nikulabc58be62016-03-18 17:05:39 +02005417 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005418
Jani Nikula4d1de972016-03-18 17:05:42 +02005419 if (cpu_transcoder != TRANSCODER_EDP &&
5420 !transcoder_is_dsi(cpu_transcoder)) {
5421 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005422 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005423 }
5424
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005425 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005426 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005427 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005428 }
5429
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005430 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005431 haswell_set_pipeconf(crtc);
5432
Jani Nikula391bf042016-03-18 17:05:40 +02005433 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005434
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005435 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005436
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005437 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005438
Daniel Vetter6b698512015-11-28 11:05:39 +01005439 if (intel_crtc->config->has_pch_encoder)
5440 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5441 else
5442 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5443
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005444 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005445
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005446 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005447 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005448
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005449 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305450 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005451
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005452 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005453 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005454 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005455 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005456
5457 /*
5458 * On ILK+ LUT must be loaded before the pipe is running but with
5459 * clocks enabled
5460 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005461 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005462
Paulo Zanoni1f544382012-10-24 11:32:00 -02005463 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005464 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305465 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005466
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005467 if (dev_priv->display.initial_watermarks != NULL)
Ville Syrjälä3125d392016-11-28 19:37:03 +02005468 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
Jani Nikula4d1de972016-03-18 17:05:42 +02005469
5470 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005471 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005472 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005473
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005474 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005475 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005476
Ville Syrjälä00370712016-11-14 19:44:06 +02005477 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Dave Airlie0e32b392014-05-02 14:02:48 +10005478 intel_ddi_set_vc_payload_alloc(crtc, true);
5479
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005480 assert_vblank_disabled(crtc);
5481 drm_crtc_vblank_on(crtc);
5482
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005483 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005484
Daniel Vetter6b698512015-11-28 11:05:39 +01005485 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005486 intel_wait_for_vblank(dev_priv, pipe);
5487 intel_wait_for_vblank(dev_priv, pipe);
Daniel Vetter6b698512015-11-28 11:05:39 +01005488 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005489 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5490 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005491 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005492
Paulo Zanonie4916942013-09-20 16:21:19 -03005493 /* If we change the relative order between pipe/planes enabling, we need
5494 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005495 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005496 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005497 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5498 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005499 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005500}
5501
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005502static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005503{
5504 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005505 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005506 int pipe = crtc->pipe;
5507
5508 /* To avoid upsetting the power well on haswell only disable the pfit if
5509 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005510 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005511 I915_WRITE(PF_CTL(pipe), 0);
5512 I915_WRITE(PF_WIN_POS(pipe), 0);
5513 I915_WRITE(PF_WIN_SZ(pipe), 0);
5514 }
5515}
5516
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005517static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5518 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005519{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005520 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005521 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005522 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5524 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005525
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005526 /*
5527 * Sometimes spurious CPU pipe underruns happen when the
5528 * pipe is already disabled, but FDI RX/TX is still enabled.
5529 * Happens at least with VGA+HDMI cloning. Suppress them.
5530 */
5531 if (intel_crtc->config->has_pch_encoder) {
5532 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005533 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005534 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005535
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005536 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005537
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005538 drm_crtc_vblank_off(crtc);
5539 assert_vblank_disabled(crtc);
5540
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005541 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005542
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005543 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005544
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005545 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005546 ironlake_fdi_disable(crtc);
5547
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005548 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005549
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005550 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005551 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005552
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005553 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005554 i915_reg_t reg;
5555 u32 temp;
5556
Daniel Vetterd925c592013-06-05 13:34:04 +02005557 /* disable TRANS_DP_CTL */
5558 reg = TRANS_DP_CTL(pipe);
5559 temp = I915_READ(reg);
5560 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5561 TRANS_DP_PORT_SEL_MASK);
5562 temp |= TRANS_DP_PORT_SEL_NONE;
5563 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005564
Daniel Vetterd925c592013-06-05 13:34:04 +02005565 /* disable DPLL_SEL */
5566 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005567 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005568 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005569 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005570
Daniel Vetterd925c592013-06-05 13:34:04 +02005571 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005572 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005573
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005574 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005575 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005576}
5577
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005578static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5579 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005580{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005581 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005582 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005584 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005585
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005586 if (intel_crtc->config->has_pch_encoder)
5587 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5588 false);
5589
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005590 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005591
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005592 drm_crtc_vblank_off(crtc);
5593 assert_vblank_disabled(crtc);
5594
Jani Nikula4d1de972016-03-18 17:05:42 +02005595 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005596 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005597 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005598
Ville Syrjälä00370712016-11-14 19:44:06 +02005599 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005600 intel_ddi_set_vc_payload_alloc(crtc, false);
5601
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005602 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305603 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005604
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00005605 if (INTEL_GEN(dev_priv) >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005606 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005607 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005608 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005609
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005610 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305611 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005612
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005613 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005614
Maarten Lankhorstb7076542016-08-23 16:18:08 +02005615 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005616 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5617 true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005618}
5619
Jesse Barnes2dd24552013-04-25 12:55:01 -07005620static void i9xx_pfit_enable(struct intel_crtc *crtc)
5621{
5622 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005623 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005624 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005625
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005626 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005627 return;
5628
Daniel Vetterc0b03412013-05-28 12:05:54 +02005629 /*
5630 * The panel fitter should only be adjusted whilst the pipe is disabled,
5631 * according to register description and PRM.
5632 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005633 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5634 assert_pipe_disabled(dev_priv, crtc->pipe);
5635
Jesse Barnesb074cec2013-04-25 12:55:02 -07005636 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5637 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005638
5639 /* Border color in case we don't scale up to the full screen. Black by
5640 * default, change to something else for debugging. */
5641 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005642}
5643
Dave Airlied05410f2014-06-05 13:22:59 +10005644static enum intel_display_power_domain port_to_power_domain(enum port port)
5645{
5646 switch (port) {
5647 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005648 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005649 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005650 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005651 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005652 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005653 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005654 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005655 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005656 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005657 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005658 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005659 return POWER_DOMAIN_PORT_OTHER;
5660 }
5661}
5662
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005663static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5664{
5665 switch (port) {
5666 case PORT_A:
5667 return POWER_DOMAIN_AUX_A;
5668 case PORT_B:
5669 return POWER_DOMAIN_AUX_B;
5670 case PORT_C:
5671 return POWER_DOMAIN_AUX_C;
5672 case PORT_D:
5673 return POWER_DOMAIN_AUX_D;
5674 case PORT_E:
5675 /* FIXME: Check VBT for actual wiring of PORT E */
5676 return POWER_DOMAIN_AUX_D;
5677 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005678 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005679 return POWER_DOMAIN_AUX_A;
5680 }
5681}
5682
Imre Deak319be8a2014-03-04 19:22:57 +02005683enum intel_display_power_domain
5684intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005685{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005686 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Imre Deak319be8a2014-03-04 19:22:57 +02005687 struct intel_digital_port *intel_dig_port;
5688
5689 switch (intel_encoder->type) {
5690 case INTEL_OUTPUT_UNKNOWN:
5691 /* Only DDI platforms should ever use this output type */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005692 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005693 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005694 case INTEL_OUTPUT_HDMI:
5695 case INTEL_OUTPUT_EDP:
5696 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005697 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005698 case INTEL_OUTPUT_DP_MST:
5699 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5700 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005701 case INTEL_OUTPUT_ANALOG:
5702 return POWER_DOMAIN_PORT_CRT;
5703 case INTEL_OUTPUT_DSI:
5704 return POWER_DOMAIN_PORT_DSI;
5705 default:
5706 return POWER_DOMAIN_PORT_OTHER;
5707 }
5708}
5709
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005710enum intel_display_power_domain
5711intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5712{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005713 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005714 struct intel_digital_port *intel_dig_port;
5715
5716 switch (intel_encoder->type) {
5717 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005718 case INTEL_OUTPUT_HDMI:
5719 /*
5720 * Only DDI platforms should ever use these output types.
5721 * We can get here after the HDMI detect code has already set
5722 * the type of the shared encoder. Since we can't be sure
5723 * what's the status of the given connectors, play safe and
5724 * run the DP detection too.
5725 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005726 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005727 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005728 case INTEL_OUTPUT_EDP:
5729 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5730 return port_to_aux_power_domain(intel_dig_port->port);
5731 case INTEL_OUTPUT_DP_MST:
5732 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5733 return port_to_aux_power_domain(intel_dig_port->port);
5734 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005735 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005736 return POWER_DOMAIN_AUX_A;
5737 }
5738}
5739
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005740static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5741 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005742{
5743 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005744 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5746 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005747 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005748 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005749
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005750 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005751 return 0;
5752
Imre Deak77d22dc2014-03-05 16:20:52 +02005753 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5754 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005755 if (crtc_state->pch_pfit.enabled ||
5756 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005757 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5758
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005759 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5760 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5761
Imre Deak319be8a2014-03-04 19:22:57 +02005762 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005763 }
Imre Deak319be8a2014-03-04 19:22:57 +02005764
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005765 if (crtc_state->shared_dpll)
5766 mask |= BIT(POWER_DOMAIN_PLLS);
5767
Imre Deak77d22dc2014-03-05 16:20:52 +02005768 return mask;
5769}
5770
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005771static unsigned long
5772modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5773 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005774{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005775 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5777 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005778 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005779
5780 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005781 intel_crtc->enabled_power_domains = new_domains =
5782 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005783
Daniel Vetter5a21b662016-05-24 17:13:53 +02005784 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005785
5786 for_each_power_domain(domain, domains)
5787 intel_display_power_get(dev_priv, domain);
5788
Daniel Vetter5a21b662016-05-24 17:13:53 +02005789 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005790}
5791
5792static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5793 unsigned long domains)
5794{
5795 enum intel_display_power_domain domain;
5796
5797 for_each_power_domain(domain, domains)
5798 intel_display_power_put(dev_priv, domain);
5799}
5800
Mika Kaholaadafdc62015-08-18 14:36:59 +03005801static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5802{
5803 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5804
Ander Conselvan de Oliveira09d09382016-12-02 10:23:55 +02005805 if (IS_GEMINILAKE(dev_priv))
5806 return 2 * max_cdclk_freq;
5807 else if (INTEL_INFO(dev_priv)->gen >= 9 ||
5808 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kaholaadafdc62015-08-18 14:36:59 +03005809 return max_cdclk_freq;
5810 else if (IS_CHERRYVIEW(dev_priv))
5811 return max_cdclk_freq*95/100;
5812 else if (INTEL_INFO(dev_priv)->gen < 4)
5813 return 2*max_cdclk_freq*90/100;
5814 else
5815 return max_cdclk_freq*90/100;
5816}
5817
Ville Syrjäläb2045352016-05-13 23:41:27 +03005818static int skl_calc_cdclk(int max_pixclk, int vco);
5819
Ville Syrjälä4c75b942016-10-31 22:37:12 +02005820static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005821{
Tvrtko Ursulin08537232016-10-13 11:03:02 +01005822 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005823 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005824 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005825
Ville Syrjäläb2045352016-05-13 23:41:27 +03005826 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005827 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005828
5829 /*
5830 * Use the lower (vco 8640) cdclk values as a
5831 * first guess. skl_calc_cdclk() will correct it
5832 * if the preferred vco is 8100 instead.
5833 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005834 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005835 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005836 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005837 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005838 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005839 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005840 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005841 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005842
5843 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02005844 } else if (IS_GEMINILAKE(dev_priv)) {
5845 dev_priv->max_cdclk_freq = 316800;
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01005846 } else if (IS_BROXTON(dev_priv)) {
Matt Roper281c1142016-04-05 14:37:19 -07005847 dev_priv->max_cdclk_freq = 624000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005848 } else if (IS_BROADWELL(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005849 /*
5850 * FIXME with extra cooling we can allow
5851 * 540 MHz for ULX and 675 Mhz for ULT.
5852 * How can we know if extra cooling is
5853 * available? PCI ID, VTB, something else?
5854 */
5855 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5856 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005857 else if (IS_BDW_ULX(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005858 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005859 else if (IS_BDW_ULT(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005860 dev_priv->max_cdclk_freq = 540000;
5861 else
5862 dev_priv->max_cdclk_freq = 675000;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005863 } else if (IS_CHERRYVIEW(dev_priv)) {
Mika Kahola0904dea2015-06-12 10:11:32 +03005864 dev_priv->max_cdclk_freq = 320000;
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01005865 } else if (IS_VALLEYVIEW(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005866 dev_priv->max_cdclk_freq = 400000;
5867 } else {
5868 /* otherwise assume cdclk is fixed */
5869 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5870 }
5871
Mika Kaholaadafdc62015-08-18 14:36:59 +03005872 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5873
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005874 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5875 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005876
5877 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5878 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005879}
5880
Ville Syrjälä4c75b942016-10-31 22:37:12 +02005881static void intel_update_cdclk(struct drm_i915_private *dev_priv)
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005882{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02005883 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005884
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005885 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005886 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5887 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5888 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005889 else
5890 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5891 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005892
5893 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005894 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5895 * Programmng [sic] note: bit[9:2] should be programmed to the number
5896 * of cdclk that generates 4MHz reference clock freq which is used to
5897 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005898 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005899 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005900 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005901}
5902
Ville Syrjälä92891e42016-05-11 22:44:45 +03005903/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5904static int skl_cdclk_decimal(int cdclk)
5905{
5906 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5907}
5908
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005909static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5910{
5911 int ratio;
5912
5913 if (cdclk == dev_priv->cdclk_pll.ref)
5914 return 0;
5915
5916 switch (cdclk) {
5917 default:
5918 MISSING_CASE(cdclk);
5919 case 144000:
5920 case 288000:
5921 case 384000:
5922 case 576000:
5923 ratio = 60;
5924 break;
5925 case 624000:
5926 ratio = 65;
5927 break;
5928 }
5929
5930 return dev_priv->cdclk_pll.ref * ratio;
5931}
5932
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02005933static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5934{
5935 int ratio;
5936
5937 if (cdclk == dev_priv->cdclk_pll.ref)
5938 return 0;
5939
5940 switch (cdclk) {
5941 default:
5942 MISSING_CASE(cdclk);
5943 case 79200:
5944 case 158400:
5945 case 316800:
5946 ratio = 33;
5947 break;
5948 }
5949
5950 return dev_priv->cdclk_pll.ref * ratio;
5951}
5952
Ville Syrjälä2b730012016-05-13 23:41:34 +03005953static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5954{
5955 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5956
5957 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005958 if (intel_wait_for_register(dev_priv,
5959 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5960 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005961 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005962
5963 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005964}
5965
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005966static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005967{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005968 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005969 u32 val;
5970
5971 val = I915_READ(BXT_DE_PLL_CTL);
5972 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005973 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005974 I915_WRITE(BXT_DE_PLL_CTL, val);
5975
5976 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5977
5978 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005979 if (intel_wait_for_register(dev_priv,
5980 BXT_DE_PLL_ENABLE,
5981 BXT_DE_PLL_LOCK,
5982 BXT_DE_PLL_LOCK,
5983 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005984 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005985
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005986 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005987}
5988
Imre Deak324513c2016-06-13 16:44:36 +03005989static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305990{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005991 u32 val, divider;
5992 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305993
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02005994 if (IS_GEMINILAKE(dev_priv))
5995 vco = glk_de_pll_vco(dev_priv, cdclk);
5996 else
5997 vco = bxt_de_pll_vco(dev_priv, cdclk);
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005998
5999 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
6000
6001 /* cdclk = vco / 2 / div{1,1.5,2,4} */
6002 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
6003 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306004 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306005 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006006 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306007 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306008 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006009 case 3:
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006010 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306011 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306012 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006013 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306014 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306015 break;
6016 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006017 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6018 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306019
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006020 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6021 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306022 }
6023
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306024 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006025 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306026 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6027 0x80000000);
6028 mutex_unlock(&dev_priv->rps.hw_lock);
6029
6030 if (ret) {
6031 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006032 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306033 return;
6034 }
6035
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006036 if (dev_priv->cdclk_pll.vco != 0 &&
6037 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03006038 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306039
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006040 if (dev_priv->cdclk_pll.vco != vco)
6041 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306042
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006043 val = divider | skl_cdclk_decimal(cdclk);
6044 /*
6045 * FIXME if only the cd2x divider needs changing, it could be done
6046 * without shutting off the pipe (if only one pipe is active).
6047 */
6048 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6049 /*
6050 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6051 * enable otherwise.
6052 */
6053 if (cdclk >= 500000)
6054 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6055 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306056
6057 mutex_lock(&dev_priv->rps.hw_lock);
6058 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006059 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306060 mutex_unlock(&dev_priv->rps.hw_lock);
6061
6062 if (ret) {
6063 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006064 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306065 return;
6066 }
6067
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006068 intel_update_cdclk(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306069}
6070
Imre Deakd66a2192016-05-24 15:38:33 +03006071static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306072{
Imre Deakd66a2192016-05-24 15:38:33 +03006073 u32 cdctl, expected;
6074
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006075 intel_update_cdclk(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306076
Imre Deakd66a2192016-05-24 15:38:33 +03006077 if (dev_priv->cdclk_pll.vco == 0 ||
6078 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6079 goto sanitize;
6080
6081 /* DPLL okay; verify the cdclock
6082 *
6083 * Some BIOS versions leave an incorrect decimal frequency value and
6084 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6085 * so sanitize this register.
6086 */
6087 cdctl = I915_READ(CDCLK_CTL);
6088 /*
6089 * Let's ignore the pipe field, since BIOS could have configured the
6090 * dividers both synching to an active pipe, or asynchronously
6091 * (PIPE_NONE).
6092 */
6093 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6094
6095 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6096 skl_cdclk_decimal(dev_priv->cdclk_freq);
6097 /*
6098 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6099 * enable otherwise.
6100 */
6101 if (dev_priv->cdclk_freq >= 500000)
6102 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6103
6104 if (cdctl == expected)
6105 /* All well; nothing to sanitize */
6106 return;
6107
6108sanitize:
6109 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6110
6111 /* force cdclk programming */
6112 dev_priv->cdclk_freq = 0;
6113
6114 /* force full PLL disable + enable */
6115 dev_priv->cdclk_pll.vco = -1;
6116}
6117
Imre Deak324513c2016-06-13 16:44:36 +03006118void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03006119{
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006120 int cdclk;
6121
Imre Deakd66a2192016-05-24 15:38:33 +03006122 bxt_sanitize_cdclk(dev_priv);
6123
6124 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03006125 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03006126
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306127 /*
6128 * FIXME:
6129 * - The initial CDCLK needs to be read from VBT.
6130 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306131 */
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006132 if (IS_GEMINILAKE(dev_priv))
6133 cdclk = glk_calc_cdclk(0);
6134 else
6135 cdclk = bxt_calc_cdclk(0);
6136
6137 bxt_set_cdclk(dev_priv, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306138}
6139
Imre Deak324513c2016-06-13 16:44:36 +03006140void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306141{
Imre Deak324513c2016-06-13 16:44:36 +03006142 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306143}
6144
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006145static int skl_calc_cdclk(int max_pixclk, int vco)
6146{
Ville Syrjälä63911d72016-05-13 23:41:32 +03006147 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006148 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006149 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006150 else if (max_pixclk > 432000)
6151 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006152 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006153 return 432000;
6154 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006155 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006156 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006157 if (max_pixclk > 540000)
6158 return 675000;
6159 else if (max_pixclk > 450000)
6160 return 540000;
6161 else if (max_pixclk > 337500)
6162 return 450000;
6163 else
6164 return 337500;
6165 }
6166}
6167
Ville Syrjäläea617912016-05-13 23:41:24 +03006168static void
6169skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006170{
Ville Syrjäläea617912016-05-13 23:41:24 +03006171 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006172
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006173 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03006174 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006175
Ville Syrjäläea617912016-05-13 23:41:24 +03006176 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03006177 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03006178 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006179
Imre Deak1c3f7702016-05-24 15:38:32 +03006180 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6181 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006182
Ville Syrjäläea617912016-05-13 23:41:24 +03006183 val = I915_READ(DPLL_CTRL1);
6184
Imre Deak1c3f7702016-05-24 15:38:32 +03006185 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6186 DPLL_CTRL1_SSC(SKL_DPLL0) |
6187 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6188 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6189 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006190
Ville Syrjäläea617912016-05-13 23:41:24 +03006191 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6192 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6193 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6194 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6195 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006196 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006197 break;
6198 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6199 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006200 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006201 break;
6202 default:
6203 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03006204 break;
6205 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006206}
6207
Ville Syrjäläb2045352016-05-13 23:41:27 +03006208void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6209{
6210 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6211
6212 dev_priv->skl_preferred_vco_freq = vco;
6213
6214 if (changed)
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006215 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006216}
6217
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006218static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03006219skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006220{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006221 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006222 u32 val;
6223
Ville Syrjälä63911d72016-05-13 23:41:32 +03006224 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006225
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006226 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006227 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006228 I915_WRITE(CDCLK_CTL, val);
6229 POSTING_READ(CDCLK_CTL);
6230
6231 /*
6232 * We always enable DPLL0 with the lowest link rate possible, but still
6233 * taking into account the VCO required to operate the eDP panel at the
6234 * desired frequency. The usual DP link rates operate with a VCO of
6235 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6236 * The modeset code is responsible for the selection of the exact link
6237 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006238 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006239 */
6240 val = I915_READ(DPLL_CTRL1);
6241
6242 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6243 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6244 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03006245 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006246 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6247 SKL_DPLL0);
6248 else
6249 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6250 SKL_DPLL0);
6251
6252 I915_WRITE(DPLL_CTRL1, val);
6253 POSTING_READ(DPLL_CTRL1);
6254
6255 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6256
Chris Wilsone24ca052016-06-30 15:33:05 +01006257 if (intel_wait_for_register(dev_priv,
6258 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6259 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006260 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006261
Ville Syrjälä63911d72016-05-13 23:41:32 +03006262 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03006263
6264 /* We'll want to keep using the current vco from now on. */
6265 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006266}
6267
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006268static void
6269skl_dpll0_disable(struct drm_i915_private *dev_priv)
6270{
6271 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01006272 if (intel_wait_for_register(dev_priv,
6273 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6274 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006275 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006276
Ville Syrjälä63911d72016-05-13 23:41:32 +03006277 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006278}
6279
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006280static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6281{
6282 int ret;
6283 u32 val;
6284
6285 /* inform PCU we want to change CDCLK */
6286 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6287 mutex_lock(&dev_priv->rps.hw_lock);
6288 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6289 mutex_unlock(&dev_priv->rps.hw_lock);
6290
6291 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6292}
6293
6294static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6295{
Ville Syrjälä848496e2016-07-13 16:32:03 +03006296 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006297}
6298
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006299static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006300{
6301 u32 freq_select, pcu_ack;
6302
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006303 WARN_ON((cdclk == 24000) != (vco == 0));
6304
Ville Syrjälä63911d72016-05-13 23:41:32 +03006305 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006306
6307 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6308 DRM_ERROR("failed to inform PCU about cdclk change\n");
6309 return;
6310 }
6311
6312 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006313 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006314 case 450000:
6315 case 432000:
6316 freq_select = CDCLK_FREQ_450_432;
6317 pcu_ack = 1;
6318 break;
6319 case 540000:
6320 freq_select = CDCLK_FREQ_540;
6321 pcu_ack = 2;
6322 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006323 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006324 case 337500:
6325 default:
6326 freq_select = CDCLK_FREQ_337_308;
6327 pcu_ack = 0;
6328 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006329 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006330 case 675000:
6331 freq_select = CDCLK_FREQ_675_617;
6332 pcu_ack = 3;
6333 break;
6334 }
6335
Ville Syrjälä63911d72016-05-13 23:41:32 +03006336 if (dev_priv->cdclk_pll.vco != 0 &&
6337 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006338 skl_dpll0_disable(dev_priv);
6339
Ville Syrjälä63911d72016-05-13 23:41:32 +03006340 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006341 skl_dpll0_enable(dev_priv, vco);
6342
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006343 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006344 POSTING_READ(CDCLK_CTL);
6345
6346 /* inform PCU of the change */
6347 mutex_lock(&dev_priv->rps.hw_lock);
6348 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6349 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01006350
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006351 intel_update_cdclk(dev_priv);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006352}
6353
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006354static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6355
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006356void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6357{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006358 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006359}
6360
6361void skl_init_cdclk(struct drm_i915_private *dev_priv)
6362{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006363 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006364
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006365 skl_sanitize_cdclk(dev_priv);
6366
Ville Syrjälä63911d72016-05-13 23:41:32 +03006367 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006368 /*
6369 * Use the current vco as our initial
6370 * guess as to what the preferred vco is.
6371 */
6372 if (dev_priv->skl_preferred_vco_freq == 0)
6373 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03006374 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006375 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006376 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006377
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006378 vco = dev_priv->skl_preferred_vco_freq;
6379 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03006380 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006381 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006382
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006383 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006384}
6385
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006386static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306387{
Ville Syrjälä09492492016-05-13 23:41:28 +03006388 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306389
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05306390 /*
6391 * check if the pre-os intialized the display
6392 * There is SWF18 scratchpad register defined which is set by the
6393 * pre-os which can be used by the OS drivers to check the status
6394 */
6395 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6396 goto sanitize;
6397
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006398 intel_update_cdclk(dev_priv);
Imre Deak1c3f7702016-05-24 15:38:32 +03006399 /* Is PLL enabled and locked ? */
6400 if (dev_priv->cdclk_pll.vco == 0 ||
6401 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6402 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006403
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306404 /* DPLL okay; verify the cdclock
6405 *
6406 * Noticed in some instances that the freq selection is correct but
6407 * decimal part is programmed wrong from BIOS where pre-os does not
6408 * enable display. Verify the same as well.
6409 */
Ville Syrjälä09492492016-05-13 23:41:28 +03006410 cdctl = I915_READ(CDCLK_CTL);
6411 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6412 skl_cdclk_decimal(dev_priv->cdclk_freq);
6413 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306414 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006415 return;
6416
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306417sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006418 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03006419
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006420 /* force cdclk programming */
6421 dev_priv->cdclk_freq = 0;
6422 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03006423 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306424}
6425
Jesse Barnes30a970c2013-11-04 13:48:12 -08006426/* Adjust CDclk dividers to allow high res or save power if possible */
6427static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6428{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006429 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006430 u32 val, cmd;
6431
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02006432 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
Vandana Kannan164dfd22014-11-24 13:37:41 +05306433 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02006434
Ville Syrjälädfcab172014-06-13 13:37:47 +03006435 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08006436 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03006437 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006438 cmd = 1;
6439 else
6440 cmd = 0;
6441
6442 mutex_lock(&dev_priv->rps.hw_lock);
6443 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6444 val &= ~DSPFREQGUAR_MASK;
6445 val |= (cmd << DSPFREQGUAR_SHIFT);
6446 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6447 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6448 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6449 50)) {
6450 DRM_ERROR("timed out waiting for CDclk change\n");
6451 }
6452 mutex_unlock(&dev_priv->rps.hw_lock);
6453
Ville Syrjälä54433e92015-05-26 20:42:31 +03006454 mutex_lock(&dev_priv->sb_lock);
6455
Ville Syrjälädfcab172014-06-13 13:37:47 +03006456 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006457 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006458
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006459 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006460
Jesse Barnes30a970c2013-11-04 13:48:12 -08006461 /* adjust cdclk divider */
6462 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03006463 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006464 val |= divider;
6465 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03006466
6467 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03006468 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03006469 50))
6470 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08006471 }
6472
Jesse Barnes30a970c2013-11-04 13:48:12 -08006473 /* adjust self-refresh exit latency value */
6474 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6475 val &= ~0x7f;
6476
6477 /*
6478 * For high bandwidth configs, we set a higher latency in the bunit
6479 * so that the core display fetch happens in time to avoid underruns.
6480 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03006481 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006482 val |= 4500 / 250; /* 4.5 usec */
6483 else
6484 val |= 3000 / 250; /* 3.0 usec */
6485 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03006486
Ville Syrjäläa5805162015-05-26 20:42:30 +03006487 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006488
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006489 intel_update_cdclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006490}
6491
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006492static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6493{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006494 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006495 u32 val, cmd;
6496
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02006497 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
Vandana Kannan164dfd22014-11-24 13:37:41 +05306498 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006499
6500 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006501 case 333333:
6502 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006503 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006504 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006505 break;
6506 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006507 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006508 return;
6509 }
6510
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006511 /*
6512 * Specs are full of misinformation, but testing on actual
6513 * hardware has shown that we just need to write the desired
6514 * CCK divider into the Punit register.
6515 */
6516 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6517
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006518 mutex_lock(&dev_priv->rps.hw_lock);
6519 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6520 val &= ~DSPFREQGUAR_MASK_CHV;
6521 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6522 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6523 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6524 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6525 50)) {
6526 DRM_ERROR("timed out waiting for CDclk change\n");
6527 }
6528 mutex_unlock(&dev_priv->rps.hw_lock);
6529
Ville Syrjälä4c75b942016-10-31 22:37:12 +02006530 intel_update_cdclk(dev_priv);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006531}
6532
Jesse Barnes30a970c2013-11-04 13:48:12 -08006533static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6534 int max_pixclk)
6535{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006536 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006537 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006538
Jesse Barnes30a970c2013-11-04 13:48:12 -08006539 /*
6540 * Really only a few cases to deal with, as only 4 CDclks are supported:
6541 * 200MHz
6542 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006543 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006544 * 400MHz (VLV only)
6545 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6546 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006547 *
6548 * We seem to get an unstable or solid color picture at 200MHz.
6549 * Not sure what's wrong. For now use 200MHz only when all pipes
6550 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006551 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006552 if (!IS_CHERRYVIEW(dev_priv) &&
6553 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006554 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006555 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006556 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006557 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006558 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006559 else
6560 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006561}
6562
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006563static int glk_calc_cdclk(int max_pixclk)
6564{
Ander Conselvan de Oliveira09d09382016-12-02 10:23:55 +02006565 if (max_pixclk > 2 * 158400)
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006566 return 316800;
Ander Conselvan de Oliveira09d09382016-12-02 10:23:55 +02006567 else if (max_pixclk > 2 * 79200)
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006568 return 158400;
6569 else
6570 return 79200;
6571}
6572
Imre Deak324513c2016-06-13 16:44:36 +03006573static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006574{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006575 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306576 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006577 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306578 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006579 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306580 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006581 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306582 return 288000;
6583 else
6584 return 144000;
6585}
6586
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006587/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006588static int intel_mode_max_pixclk(struct drm_device *dev,
6589 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006590{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006591 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01006592 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006593 struct drm_crtc *crtc;
6594 struct drm_crtc_state *crtc_state;
6595 unsigned max_pixclk = 0, i;
6596 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006597
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006598 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6599 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006600
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006601 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6602 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006603
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006604 if (crtc_state->enable)
6605 pixclk = crtc_state->adjusted_mode.crtc_clock;
6606
6607 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006608 }
6609
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006610 for_each_pipe(dev_priv, pipe)
6611 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6612
Jesse Barnes30a970c2013-11-04 13:48:12 -08006613 return max_pixclk;
6614}
6615
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006616static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006617{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006618 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006619 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006620 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006621 struct intel_atomic_state *intel_state =
6622 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006623
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006624 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006625 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306626
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006627 if (!intel_state->active_crtcs)
6628 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6629
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006630 return 0;
6631}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006632
Imre Deak324513c2016-06-13 16:44:36 +03006633static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006634{
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006635 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006636 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006637 struct intel_atomic_state *intel_state =
6638 to_intel_atomic_state(state);
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006639 int cdclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006640
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006641 if (IS_GEMINILAKE(dev_priv))
6642 cdclk = glk_calc_cdclk(max_pixclk);
6643 else
6644 cdclk = bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006645
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02006646 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
6647
6648 if (!intel_state->active_crtcs) {
6649 if (IS_GEMINILAKE(dev_priv))
6650 cdclk = glk_calc_cdclk(0);
6651 else
6652 cdclk = bxt_calc_cdclk(0);
6653
6654 intel_state->dev_cdclk = cdclk;
6655 }
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006656
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006657 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006658}
6659
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006660static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6661{
6662 unsigned int credits, default_credits;
6663
6664 if (IS_CHERRYVIEW(dev_priv))
6665 default_credits = PFI_CREDIT(12);
6666 else
6667 default_credits = PFI_CREDIT(8);
6668
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006669 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006670 /* CHV suggested value is 31 or 63 */
6671 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006672 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006673 else
6674 credits = PFI_CREDIT(15);
6675 } else {
6676 credits = default_credits;
6677 }
6678
6679 /*
6680 * WA - write default credits before re-programming
6681 * FIXME: should we also set the resend bit here?
6682 */
6683 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6684 default_credits);
6685
6686 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6687 credits | PFI_CREDIT_RESEND);
6688
6689 /*
6690 * FIXME is this guaranteed to clear
6691 * immediately or should we poll for it?
6692 */
6693 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6694}
6695
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006696static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006697{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006698 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006699 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006700 struct intel_atomic_state *old_intel_state =
6701 to_intel_atomic_state(old_state);
6702 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006703
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006704 /*
6705 * FIXME: We can end up here with all power domains off, yet
6706 * with a CDCLK frequency other than the minimum. To account
6707 * for this take the PIPE-A power domain, which covers the HW
6708 * blocks needed for the following programming. This can be
6709 * removed once it's guaranteed that we get here either with
6710 * the minimum CDCLK set, or the required power domains
6711 * enabled.
6712 */
6713 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006714
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006715 if (IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006716 cherryview_set_cdclk(dev, req_cdclk);
6717 else
6718 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006719
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006720 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006721
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006722 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006723}
6724
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006725static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6726 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006727{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006728 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006729 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006730 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006732 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006733
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006734 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006735 return;
6736
Ville Syrjälä37a56502016-06-22 21:57:04 +03006737 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306738 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006739
6740 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006741 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006742
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006743 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006744 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006745
6746 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6747 I915_WRITE(CHV_CANVAS(pipe), 0);
6748 }
6749
Daniel Vetter5b18e572014-04-24 23:55:06 +02006750 i9xx_set_pipeconf(intel_crtc);
6751
Jesse Barnes89b667f2013-04-18 14:51:36 -07006752 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006753
Daniel Vettera72e4c92014-09-30 10:56:47 +02006754 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006755
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006756 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006757
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006758 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006759 chv_prepare_pll(intel_crtc, intel_crtc->config);
6760 chv_enable_pll(intel_crtc, intel_crtc->config);
6761 } else {
6762 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6763 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006764 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006765
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006766 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006767
Jesse Barnes2dd24552013-04-25 12:55:01 -07006768 i9xx_pfit_enable(intel_crtc);
6769
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006770 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006771
Ville Syrjälä432081b2016-10-31 22:37:03 +02006772 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006773 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006774
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006775 assert_vblank_disabled(crtc);
6776 drm_crtc_vblank_on(crtc);
6777
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006778 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006779}
6780
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006781static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6782{
6783 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006784 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006785
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006786 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6787 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006788}
6789
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006790static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6791 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006792{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006793 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006794 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006795 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006797 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006798
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006799 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006800 return;
6801
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006802 i9xx_set_pll_dividers(intel_crtc);
6803
Ville Syrjälä37a56502016-06-22 21:57:04 +03006804 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306805 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006806
6807 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006808 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006809
Daniel Vetter5b18e572014-04-24 23:55:06 +02006810 i9xx_set_pipeconf(intel_crtc);
6811
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006812 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006813
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006814 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006815 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006816
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006817 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006818
Daniel Vetterf6736a12013-06-05 13:34:30 +02006819 i9xx_enable_pll(intel_crtc);
6820
Jesse Barnes2dd24552013-04-25 12:55:01 -07006821 i9xx_pfit_enable(intel_crtc);
6822
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006823 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006824
Ville Syrjälä432081b2016-10-31 22:37:03 +02006825 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006826 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006827
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006828 assert_vblank_disabled(crtc);
6829 drm_crtc_vblank_on(crtc);
6830
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006831 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006832}
6833
Daniel Vetter87476d62013-04-11 16:29:06 +02006834static void i9xx_pfit_disable(struct intel_crtc *crtc)
6835{
6836 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006837 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006838
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006839 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006840 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006841
6842 assert_pipe_disabled(dev_priv, crtc->pipe);
6843
Daniel Vetter328d8e82013-05-08 10:36:31 +02006844 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6845 I915_READ(PFIT_CONTROL));
6846 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006847}
6848
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006849static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6850 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006851{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006852 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006853 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006854 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6856 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006857
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006858 /*
6859 * On gen2 planes are double buffered but the pipe isn't, so we must
6860 * wait for planes to fully turn off before disabling the pipe.
6861 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006862 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02006863 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006864
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006865 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006866
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006867 drm_crtc_vblank_off(crtc);
6868 assert_vblank_disabled(crtc);
6869
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006870 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006871
Daniel Vetter87476d62013-04-11 16:29:06 +02006872 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006873
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006874 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006875
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006876 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006877 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006878 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006879 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006880 vlv_disable_pll(dev_priv, pipe);
6881 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006882 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006883 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006884
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006885 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006886
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006887 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006888 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006889}
6890
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006891static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006892{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006893 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006895 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006896 enum intel_display_power_domain domain;
6897 unsigned long domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006898 struct drm_atomic_state *state;
6899 struct intel_crtc_state *crtc_state;
6900 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006901
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006902 if (!intel_crtc->active)
6903 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006904
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006905 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006906 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006907
Ville Syrjälä2622a082016-03-09 19:07:26 +02006908 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006909
6910 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006911 to_intel_plane_state(crtc->primary->state)->base.visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006912 }
6913
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006914 state = drm_atomic_state_alloc(crtc->dev);
6915 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6916
6917 /* Everything's already locked, -EDEADLK can't happen. */
6918 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6919 ret = drm_atomic_add_affected_connectors(state, crtc);
6920
6921 WARN_ON(IS_ERR(crtc_state) || ret);
6922
6923 dev_priv->display.crtc_disable(crtc_state, state);
6924
Chris Wilson08536952016-10-14 13:18:18 +01006925 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006926
Ville Syrjälä78108b72016-05-27 20:59:19 +03006927 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6928 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006929
6930 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6931 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006932 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006933 crtc->enabled = false;
6934 crtc->state->connector_mask = 0;
6935 crtc->state->encoder_mask = 0;
6936
6937 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6938 encoder->base.crtc = NULL;
6939
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006940 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02006941 intel_update_watermarks(intel_crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006942 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006943
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006944 domains = intel_crtc->enabled_power_domains;
6945 for_each_power_domain(domain, domains)
6946 intel_display_power_put(dev_priv, domain);
6947 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006948
6949 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6950 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006951}
6952
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006953/*
6954 * turn all crtc's off, but do not adjust state
6955 * This has to be paired with a call to intel_modeset_setup_hw_state.
6956 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006957int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006958{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006959 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006960 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006961 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006962
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006963 state = drm_atomic_helper_suspend(dev);
6964 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006965 if (ret)
6966 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006967 else
6968 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006969 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006970}
6971
Chris Wilsonea5b2132010-08-04 13:50:23 +01006972void intel_encoder_destroy(struct drm_encoder *encoder)
6973{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006974 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006975
Chris Wilsonea5b2132010-08-04 13:50:23 +01006976 drm_encoder_cleanup(encoder);
6977 kfree(intel_encoder);
6978}
6979
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006980/* Cross check the actual hw state with our own modeset state tracking (and it's
6981 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006982static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006983{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006984 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006985
6986 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6987 connector->base.base.id,
6988 connector->base.name);
6989
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006990 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006991 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006992 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006993
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006994 I915_STATE_WARN(!crtc,
6995 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006996
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006997 if (!crtc)
6998 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006999
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02007000 I915_STATE_WARN(!crtc->state->active,
7001 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02007002
Maarten Lankhorste85376c2015-08-27 13:13:31 +02007003 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02007004 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02007005
Maarten Lankhorste85376c2015-08-27 13:13:31 +02007006 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02007007 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10007008
Maarten Lankhorste85376c2015-08-27 13:13:31 +02007009 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02007010 "attached encoder crtc differs from connector crtc\n");
7011 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02007012 I915_STATE_WARN(crtc && crtc->state->active,
7013 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02007014 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02007015 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02007016 }
7017}
7018
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03007019int intel_connector_init(struct intel_connector *connector)
7020{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01007021 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03007022
Maarten Lankhorst5350a032016-01-04 12:53:15 +01007023 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03007024 return -ENOMEM;
7025
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03007026 return 0;
7027}
7028
7029struct intel_connector *intel_connector_alloc(void)
7030{
7031 struct intel_connector *connector;
7032
7033 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7034 if (!connector)
7035 return NULL;
7036
7037 if (intel_connector_init(connector) < 0) {
7038 kfree(connector);
7039 return NULL;
7040 }
7041
7042 return connector;
7043}
7044
Daniel Vetterf0947c32012-07-02 13:10:34 +02007045/* Simple connector->get_hw_state implementation for encoders that support only
7046 * one connector and no cloning and hence the encoder state determines the state
7047 * of the connector. */
7048bool intel_connector_get_hw_state(struct intel_connector *connector)
7049{
Daniel Vetter24929352012-07-02 20:28:59 +02007050 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02007051 struct intel_encoder *encoder = connector->encoder;
7052
7053 return encoder->get_hw_state(encoder, &pipe);
7054}
7055
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007056static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007057{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007058 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7059 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007060
7061 return 0;
7062}
7063
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007064static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007065 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007066{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007067 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007068 struct drm_atomic_state *state = pipe_config->base.state;
7069 struct intel_crtc *other_crtc;
7070 struct intel_crtc_state *other_crtc_state;
7071
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007072 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7073 pipe_name(pipe), pipe_config->fdi_lanes);
7074 if (pipe_config->fdi_lanes > 4) {
7075 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7076 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007077 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007078 }
7079
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007080 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007081 if (pipe_config->fdi_lanes > 2) {
7082 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7083 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007084 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007085 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007086 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007087 }
7088 }
7089
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00007090 if (INTEL_INFO(dev_priv)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007091 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007092
7093 /* Ivybridge 3 pipe is really complicated */
7094 switch (pipe) {
7095 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007096 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007097 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007098 if (pipe_config->fdi_lanes <= 2)
7099 return 0;
7100
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007101 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007102 other_crtc_state =
7103 intel_atomic_get_crtc_state(state, other_crtc);
7104 if (IS_ERR(other_crtc_state))
7105 return PTR_ERR(other_crtc_state);
7106
7107 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007108 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7109 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007110 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007111 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007112 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007113 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02007114 if (pipe_config->fdi_lanes > 2) {
7115 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7116 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007117 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02007118 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007119
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007120 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007121 other_crtc_state =
7122 intel_atomic_get_crtc_state(state, other_crtc);
7123 if (IS_ERR(other_crtc_state))
7124 return PTR_ERR(other_crtc_state);
7125
7126 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007127 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007128 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007129 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007130 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007131 default:
7132 BUG();
7133 }
7134}
7135
Daniel Vettere29c22c2013-02-21 00:00:16 +01007136#define RETRY 1
7137static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007138 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02007139{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007140 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007141 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007142 int lane, link_bw, fdi_dotclock, ret;
7143 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007144
Daniel Vettere29c22c2013-02-21 00:00:16 +01007145retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02007146 /* FDI is a binary signal running at ~2.7GHz, encoding
7147 * each output octet as 10 bits. The actual frequency
7148 * is stored as a divider into a 100MHz clock, and the
7149 * mode pixel clock is stored in units of 1KHz.
7150 * Hence the bw of each lane in terms of the mode signal
7151 * is:
7152 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02007153 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007154
Damien Lespiau241bfc32013-09-25 16:45:37 +01007155 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007156
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007157 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007158 pipe_config->pipe_bpp);
7159
7160 pipe_config->fdi_lanes = lane;
7161
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007162 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007163 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007164
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02007165 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007166 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01007167 pipe_config->pipe_bpp -= 2*3;
7168 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7169 pipe_config->pipe_bpp);
7170 needs_recompute = true;
7171 pipe_config->bw_constrained = true;
7172
7173 goto retry;
7174 }
7175
7176 if (needs_recompute)
7177 return RETRY;
7178
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007179 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007180}
7181
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007182static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7183 struct intel_crtc_state *pipe_config)
7184{
7185 if (pipe_config->pipe_bpp > 24)
7186 return false;
7187
7188 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007189 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007190 return true;
7191
7192 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03007193 * We compare against max which means we must take
7194 * the increased cdclk requirement into account when
7195 * calculating the new cdclk.
7196 *
7197 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007198 */
7199 return ilk_pipe_pixel_rate(pipe_config) <=
7200 dev_priv->max_cdclk_freq * 95 / 100;
7201}
7202
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007203static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007204 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007205{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007206 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007207 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007208
Jani Nikulad330a952014-01-21 11:24:25 +02007209 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007210 hsw_crtc_supports_ips(crtc) &&
7211 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007212}
7213
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007214static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7215{
7216 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7217
7218 /* GDG double wide on either pipe, otherwise pipe A only */
7219 return INTEL_INFO(dev_priv)->gen < 4 &&
7220 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7221}
7222
Daniel Vettera43f6e02013-06-07 23:10:32 +02007223static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007224 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007225{
Daniel Vettera43f6e02013-06-07 23:10:32 +02007226 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007227 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007228 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03007229 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01007230
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007231 if (INTEL_GEN(dev_priv) < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007232 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007233
7234 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007235 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007236 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007237 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007238 if (intel_crtc_supports_double_wide(crtc) &&
7239 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007240 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007241 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007242 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03007243 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007244
Ville Syrjäläf3261152016-05-24 21:34:18 +03007245 if (adjusted_mode->crtc_clock > clock_limit) {
7246 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7247 adjusted_mode->crtc_clock, clock_limit,
7248 yesno(pipe_config->double_wide));
7249 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007250 }
Chris Wilson89749352010-09-12 18:25:19 +01007251
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007252 /*
7253 * Pipe horizontal size must be even in:
7254 * - DVO ganged mode
7255 * - LVDS dual channel mode
7256 * - Double wide pipe
7257 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007258 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007259 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7260 pipe_config->pipe_src_w &= ~1;
7261
Damien Lespiau8693a822013-05-03 18:48:11 +01007262 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7263 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03007264 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007265 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03007266 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01007267 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03007268
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007269 if (HAS_IPS(dev_priv))
Daniel Vettera43f6e02013-06-07 23:10:32 +02007270 hsw_compute_ips_config(crtc, pipe_config);
7271
Daniel Vetter877d48d2013-04-19 11:24:43 +02007272 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02007273 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007274
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02007275 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007276}
7277
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007278static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä1652d192015-03-31 14:12:01 +03007279{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007280 u32 cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007281
Ville Syrjäläea617912016-05-13 23:41:24 +03007282 skl_dpll0_update(dev_priv);
7283
Ville Syrjälä63911d72016-05-13 23:41:32 +03007284 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007285 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007286
Ville Syrjäläea617912016-05-13 23:41:24 +03007287 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007288
Ville Syrjälä63911d72016-05-13 23:41:32 +03007289 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007290 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7291 case CDCLK_FREQ_450_432:
7292 return 432000;
7293 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007294 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03007295 case CDCLK_FREQ_540:
7296 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007297 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007298 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007299 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007300 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007301 }
7302 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007303 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7304 case CDCLK_FREQ_450_432:
7305 return 450000;
7306 case CDCLK_FREQ_337_308:
7307 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03007308 case CDCLK_FREQ_540:
7309 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007310 case CDCLK_FREQ_675_617:
7311 return 675000;
7312 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007313 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007314 }
7315 }
7316
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007317 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007318}
7319
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007320static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7321{
7322 u32 val;
7323
7324 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03007325 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007326
7327 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03007328 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007329 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007330
Imre Deak1c3f7702016-05-24 15:38:32 +03007331 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7332 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007333
7334 val = I915_READ(BXT_DE_PLL_CTL);
7335 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7336 dev_priv->cdclk_pll.ref;
7337}
7338
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007339static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007340{
Ville Syrjäläf5986242016-05-13 23:41:37 +03007341 u32 divider;
7342 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007343
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007344 bxt_de_pll_update(dev_priv);
7345
Ville Syrjäläf5986242016-05-13 23:41:37 +03007346 vco = dev_priv->cdclk_pll.vco;
7347 if (vco == 0)
7348 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007349
Ville Syrjäläf5986242016-05-13 23:41:37 +03007350 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007351
Ville Syrjäläf5986242016-05-13 23:41:37 +03007352 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007353 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007354 div = 2;
7355 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007356 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +02007357 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
Ville Syrjäläf5986242016-05-13 23:41:37 +03007358 div = 3;
7359 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007360 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007361 div = 4;
7362 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007363 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007364 div = 8;
7365 break;
7366 default:
7367 MISSING_CASE(divider);
7368 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007369 }
7370
Ville Syrjäläf5986242016-05-13 23:41:37 +03007371 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007372}
7373
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007374static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä1652d192015-03-31 14:12:01 +03007375{
Ville Syrjälä1652d192015-03-31 14:12:01 +03007376 uint32_t lcpll = I915_READ(LCPLL_CTL);
7377 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7378
7379 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7380 return 800000;
7381 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7382 return 450000;
7383 else if (freq == LCPLL_CLK_FREQ_450)
7384 return 450000;
7385 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7386 return 540000;
7387 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7388 return 337500;
7389 else
7390 return 675000;
7391}
7392
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007393static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä1652d192015-03-31 14:12:01 +03007394{
Ville Syrjälä1652d192015-03-31 14:12:01 +03007395 uint32_t lcpll = I915_READ(LCPLL_CTL);
7396 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7397
7398 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7399 return 800000;
7400 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7401 return 450000;
7402 else if (freq == LCPLL_CLK_FREQ_450)
7403 return 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007404 else if (IS_HSW_ULT(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +03007405 return 337500;
7406 else
7407 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007408}
7409
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007410static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007411{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007412 return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007413 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007414}
7415
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007416static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjäläb37a6432015-03-31 14:11:54 +03007417{
7418 return 450000;
7419}
7420
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007421static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -08007422{
Jesse Barnese70236a2009-09-21 10:42:27 -07007423 return 400000;
7424}
Jesse Barnes79e53942008-11-07 14:24:08 -08007425
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007426static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007427{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007428 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007429}
Jesse Barnes79e53942008-11-07 14:24:08 -08007430
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007431static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007432{
7433 return 200000;
7434}
Jesse Barnes79e53942008-11-07 14:24:08 -08007435
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007436static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007437{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007438 struct pci_dev *pdev = dev_priv->drm.pdev;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007439 u16 gcfgc = 0;
7440
David Weinehall52a05c32016-08-22 13:32:44 +03007441 pci_read_config_word(pdev, GCFGC, &gcfgc);
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007442
7443 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7444 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007445 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007446 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007447 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007448 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007449 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007450 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7451 return 200000;
7452 default:
7453 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7454 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007455 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007456 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007457 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007458 }
7459}
7460
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007461static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007462{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007463 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007464 u16 gcfgc = 0;
7465
David Weinehall52a05c32016-08-22 13:32:44 +03007466 pci_read_config_word(pdev, GCFGC, &gcfgc);
Jesse Barnese70236a2009-09-21 10:42:27 -07007467
7468 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03007469 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007470 else {
7471 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7472 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007473 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007474 default:
7475 case GC_DISPLAY_CLOCK_190_200_MHZ:
7476 return 190000;
7477 }
7478 }
7479}
Jesse Barnes79e53942008-11-07 14:24:08 -08007480
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007481static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007482{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007483 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007484}
7485
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007486static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007487{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007488 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007489 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007490
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007491 /*
7492 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7493 * encoding is different :(
7494 * FIXME is this the right way to detect 852GM/852GMV?
7495 */
David Weinehall52a05c32016-08-22 13:32:44 +03007496 if (pdev->revision == 0x1)
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007497 return 133333;
7498
David Weinehall52a05c32016-08-22 13:32:44 +03007499 pci_bus_read_config_word(pdev->bus,
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007500 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7501
Jesse Barnese70236a2009-09-21 10:42:27 -07007502 /* Assume that the hardware is in the high speed state. This
7503 * should be the default.
7504 */
7505 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7506 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007507 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007508 case GC_CLOCK_100_200:
7509 return 200000;
7510 case GC_CLOCK_166_250:
7511 return 250000;
7512 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007513 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007514 case GC_CLOCK_133_266:
7515 case GC_CLOCK_133_266_2:
7516 case GC_CLOCK_166_266:
7517 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007518 }
7519
7520 /* Shouldn't happen */
7521 return 0;
7522}
7523
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007524static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -07007525{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007526 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007527}
7528
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007529static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007530{
Ville Syrjälä34edce22015-05-22 11:22:33 +03007531 static const unsigned int blb_vco[8] = {
7532 [0] = 3200000,
7533 [1] = 4000000,
7534 [2] = 5333333,
7535 [3] = 4800000,
7536 [4] = 6400000,
7537 };
7538 static const unsigned int pnv_vco[8] = {
7539 [0] = 3200000,
7540 [1] = 4000000,
7541 [2] = 5333333,
7542 [3] = 4800000,
7543 [4] = 2666667,
7544 };
7545 static const unsigned int cl_vco[8] = {
7546 [0] = 3200000,
7547 [1] = 4000000,
7548 [2] = 5333333,
7549 [3] = 6400000,
7550 [4] = 3333333,
7551 [5] = 3566667,
7552 [6] = 4266667,
7553 };
7554 static const unsigned int elk_vco[8] = {
7555 [0] = 3200000,
7556 [1] = 4000000,
7557 [2] = 5333333,
7558 [3] = 4800000,
7559 };
7560 static const unsigned int ctg_vco[8] = {
7561 [0] = 3200000,
7562 [1] = 4000000,
7563 [2] = 5333333,
7564 [3] = 6400000,
7565 [4] = 2666667,
7566 [5] = 4266667,
7567 };
7568 const unsigned int *vco_table;
7569 unsigned int vco;
7570 uint8_t tmp = 0;
7571
7572 /* FIXME other chipsets? */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007573 if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007574 vco_table = ctg_vco;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007575 else if (IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007576 vco_table = elk_vco;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007577 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007578 vco_table = cl_vco;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007579 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007580 vco_table = pnv_vco;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007581 else if (IS_G33(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007582 vco_table = blb_vco;
7583 else
7584 return 0;
7585
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007586 tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007587
7588 vco = vco_table[tmp & 0x7];
7589 if (vco == 0)
7590 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7591 else
7592 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7593
7594 return vco;
7595}
7596
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007597static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007598{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007599 struct pci_dev *pdev = dev_priv->drm.pdev;
7600 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007601 uint16_t tmp = 0;
7602
David Weinehall52a05c32016-08-22 13:32:44 +03007603 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007604
7605 cdclk_sel = (tmp >> 12) & 0x1;
7606
7607 switch (vco) {
7608 case 2666667:
7609 case 4000000:
7610 case 5333333:
7611 return cdclk_sel ? 333333 : 222222;
7612 case 3200000:
7613 return cdclk_sel ? 320000 : 228571;
7614 default:
7615 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7616 return 222222;
7617 }
7618}
7619
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007620static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007621{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007622 struct pci_dev *pdev = dev_priv->drm.pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007623 static const uint8_t div_3200[] = { 16, 10, 8 };
7624 static const uint8_t div_4000[] = { 20, 12, 10 };
7625 static const uint8_t div_5333[] = { 24, 16, 14 };
7626 const uint8_t *div_table;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007627 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007628 uint16_t tmp = 0;
7629
David Weinehall52a05c32016-08-22 13:32:44 +03007630 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007631
7632 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7633
7634 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7635 goto fail;
7636
7637 switch (vco) {
7638 case 3200000:
7639 div_table = div_3200;
7640 break;
7641 case 4000000:
7642 div_table = div_4000;
7643 break;
7644 case 5333333:
7645 div_table = div_5333;
7646 break;
7647 default:
7648 goto fail;
7649 }
7650
7651 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7652
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007653fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007654 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7655 return 200000;
7656}
7657
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007658static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
Ville Syrjälä34edce22015-05-22 11:22:33 +03007659{
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007660 struct pci_dev *pdev = dev_priv->drm.pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007661 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7662 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7663 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7664 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7665 const uint8_t *div_table;
Ville Syrjälä1353c4f2016-10-31 22:37:13 +02007666 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007667 uint16_t tmp = 0;
7668
David Weinehall52a05c32016-08-22 13:32:44 +03007669 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007670
7671 cdclk_sel = (tmp >> 4) & 0x7;
7672
7673 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7674 goto fail;
7675
7676 switch (vco) {
7677 case 3200000:
7678 div_table = div_3200;
7679 break;
7680 case 4000000:
7681 div_table = div_4000;
7682 break;
7683 case 4800000:
7684 div_table = div_4800;
7685 break;
7686 case 5333333:
7687 div_table = div_5333;
7688 break;
7689 default:
7690 goto fail;
7691 }
7692
7693 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7694
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007695fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007696 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7697 return 190476;
7698}
7699
Zhenyu Wang2c072452009-06-05 15:38:42 +08007700static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007701intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007702{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007703 while (*num > DATA_LINK_M_N_MASK ||
7704 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007705 *num >>= 1;
7706 *den >>= 1;
7707 }
7708}
7709
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007710static void compute_m_n(unsigned int m, unsigned int n,
7711 uint32_t *ret_m, uint32_t *ret_n)
7712{
7713 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7714 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7715 intel_reduce_m_n_ratio(ret_m, ret_n);
7716}
7717
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007718void
7719intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7720 int pixel_clock, int link_clock,
7721 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007722{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007723 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007724
7725 compute_m_n(bits_per_pixel * pixel_clock,
7726 link_clock * nlanes * 8,
7727 &m_n->gmch_m, &m_n->gmch_n);
7728
7729 compute_m_n(pixel_clock, link_clock,
7730 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007731}
7732
Chris Wilsona7615032011-01-12 17:04:08 +00007733static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7734{
Jani Nikulad330a952014-01-21 11:24:25 +02007735 if (i915.panel_use_ssc >= 0)
7736 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007737 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007738 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007739}
7740
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007741static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007742{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007743 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007744}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007745
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007746static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7747{
7748 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007749}
7750
Daniel Vetterf47709a2013-03-28 10:42:02 +01007751static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007752 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007753 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007754{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007755 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007756 u32 fp, fp2 = 0;
7757
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007758 if (IS_PINEVIEW(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007759 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007760 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007761 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007762 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007763 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007764 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007765 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007766 }
7767
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007768 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007769
Daniel Vetterf47709a2013-03-28 10:42:02 +01007770 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007771 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007772 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007773 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007774 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007775 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007776 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007777 }
7778}
7779
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007780static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7781 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007782{
7783 u32 reg_val;
7784
7785 /*
7786 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7787 * and set it to a reasonable value instead.
7788 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007789 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007790 reg_val &= 0xffffff00;
7791 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007792 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007793
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007794 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007795 reg_val &= 0x8cffffff;
7796 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007797 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007798
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007799 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007800 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007801 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007802
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007803 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007804 reg_val &= 0x00ffffff;
7805 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007806 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007807}
7808
Daniel Vetterb5518422013-05-03 11:49:48 +02007809static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7810 struct intel_link_m_n *m_n)
7811{
7812 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007813 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007814 int pipe = crtc->pipe;
7815
Daniel Vettere3b95f12013-05-03 11:49:49 +02007816 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7817 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7818 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7819 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007820}
7821
7822static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007823 struct intel_link_m_n *m_n,
7824 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007825{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007826 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007827 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007828 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007829
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00007830 if (INTEL_GEN(dev_priv) >= 5) {
Daniel Vetterb5518422013-05-03 11:49:48 +02007831 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7832 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7833 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7834 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007835 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7836 * for gen < 8) and if DRRS is supported (to make sure the
7837 * registers are not unnecessarily accessed).
7838 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007839 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7840 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007841 I915_WRITE(PIPE_DATA_M2(transcoder),
7842 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7843 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7844 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7845 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7846 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007847 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007848 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7849 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7850 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7851 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007852 }
7853}
7854
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307855void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007856{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307857 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7858
7859 if (m_n == M1_N1) {
7860 dp_m_n = &crtc->config->dp_m_n;
7861 dp_m2_n2 = &crtc->config->dp_m2_n2;
7862 } else if (m_n == M2_N2) {
7863
7864 /*
7865 * M2_N2 registers are not supported. Hence m2_n2 divider value
7866 * needs to be programmed into M1_N1.
7867 */
7868 dp_m_n = &crtc->config->dp_m2_n2;
7869 } else {
7870 DRM_ERROR("Unsupported divider value\n");
7871 return;
7872 }
7873
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007874 if (crtc->config->has_pch_encoder)
7875 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007876 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307877 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007878}
7879
Daniel Vetter251ac862015-06-18 10:30:24 +02007880static void vlv_compute_dpll(struct intel_crtc *crtc,
7881 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007882{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007883 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007884 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007885 if (crtc->pipe != PIPE_A)
7886 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007887
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007888 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007889 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007890 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7891 DPLL_EXT_BUFFER_ENABLE_VLV;
7892
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007893 pipe_config->dpll_hw_state.dpll_md =
7894 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7895}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007896
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007897static void chv_compute_dpll(struct intel_crtc *crtc,
7898 struct intel_crtc_state *pipe_config)
7899{
7900 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007901 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007902 if (crtc->pipe != PIPE_A)
7903 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7904
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007905 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007906 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007907 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7908
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007909 pipe_config->dpll_hw_state.dpll_md =
7910 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007911}
7912
Ville Syrjäläd288f652014-10-28 13:20:22 +02007913static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007914 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007915{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007916 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007917 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007918 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007919 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007920 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007921 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007922
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007923 /* Enable Refclk */
7924 I915_WRITE(DPLL(pipe),
7925 pipe_config->dpll_hw_state.dpll &
7926 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7927
7928 /* No need to actually set up the DPLL with DSI */
7929 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7930 return;
7931
Ville Syrjäläa5805162015-05-26 20:42:30 +03007932 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007933
Ville Syrjäläd288f652014-10-28 13:20:22 +02007934 bestn = pipe_config->dpll.n;
7935 bestm1 = pipe_config->dpll.m1;
7936 bestm2 = pipe_config->dpll.m2;
7937 bestp1 = pipe_config->dpll.p1;
7938 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007939
Jesse Barnes89b667f2013-04-18 14:51:36 -07007940 /* See eDP HDMI DPIO driver vbios notes doc */
7941
7942 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007943 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007944 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007945
7946 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007947 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007948
7949 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007950 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007951 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007953
7954 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007955 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007956
7957 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007958 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7959 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7960 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007961 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007962
7963 /*
7964 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7965 * but we don't support that).
7966 * Note: don't use the DAC post divider as it seems unstable.
7967 */
7968 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007970
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007971 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007973
Jesse Barnes89b667f2013-04-18 14:51:36 -07007974 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007975 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007976 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7977 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007978 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007979 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007980 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007981 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007982 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007983
Ville Syrjälä37a56502016-06-22 21:57:04 +03007984 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007985 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007986 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007988 0x0df40000);
7989 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007990 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007991 0x0df70000);
7992 } else { /* HDMI or VGA */
7993 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007994 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007995 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007996 0x0df70000);
7997 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007998 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007999 0x0df40000);
8000 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07008001
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008002 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07008003 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03008004 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07008005 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008006 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07008007
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008008 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03008009 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07008010}
8011
Ville Syrjäläd288f652014-10-28 13:20:22 +02008012static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008013 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03008014{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008015 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008016 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03008017 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008018 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308019 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008020 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308021 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308022 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008023
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03008024 /* Enable Refclk and SSC */
8025 I915_WRITE(DPLL(pipe),
8026 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8027
8028 /* No need to actually set up the DPLL with DSI */
8029 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8030 return;
8031
Ville Syrjäläd288f652014-10-28 13:20:22 +02008032 bestn = pipe_config->dpll.n;
8033 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8034 bestm1 = pipe_config->dpll.m1;
8035 bestm2 = pipe_config->dpll.m2 >> 22;
8036 bestp1 = pipe_config->dpll.p1;
8037 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308038 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308039 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308040 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008041
Ville Syrjäläa5805162015-05-26 20:42:30 +03008042 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008043
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008044 /* p1 and p2 divider */
8045 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8046 5 << DPIO_CHV_S1_DIV_SHIFT |
8047 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8048 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8049 1 << DPIO_CHV_K_DIV_SHIFT);
8050
8051 /* Feedback post-divider - m2 */
8052 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8053
8054 /* Feedback refclk divider - n and m1 */
8055 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8056 DPIO_CHV_M1_DIV_BY_2 |
8057 1 << DPIO_CHV_N_DIV_SHIFT);
8058
8059 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03008060 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008061
8062 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308063 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8064 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8065 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8066 if (bestm2_frac)
8067 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8068 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008069
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05308070 /* Program digital lock detect threshold */
8071 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8072 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8073 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8074 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8075 if (!bestm2_frac)
8076 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8077 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8078
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008079 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308080 if (vco == 5400000) {
8081 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8082 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8083 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8084 tribuf_calcntr = 0x9;
8085 } else if (vco <= 6200000) {
8086 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8087 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8088 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8089 tribuf_calcntr = 0x9;
8090 } else if (vco <= 6480000) {
8091 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8092 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8093 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8094 tribuf_calcntr = 0x8;
8095 } else {
8096 /* Not supported. Apply the same limits as in the max case */
8097 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8098 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8099 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8100 tribuf_calcntr = 0;
8101 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008102 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8103
Ville Syrjälä968040b2015-03-11 22:52:08 +02008104 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308105 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8106 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8107 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8108
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008109 /* AFC Recal */
8110 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8111 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8112 DPIO_AFC_RECAL);
8113
Ville Syrjäläa5805162015-05-26 20:42:30 +03008114 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008115}
8116
Ville Syrjäläd288f652014-10-28 13:20:22 +02008117/**
8118 * vlv_force_pll_on - forcibly enable just the PLL
8119 * @dev_priv: i915 private structure
8120 * @pipe: pipe PLL to enable
8121 * @dpll: PLL configuration
8122 *
8123 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8124 * in cases where we need the PLL enabled even when @pipe is not going to
8125 * be enabled.
8126 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008127int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008128 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02008129{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02008130 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008131 struct intel_crtc_state *pipe_config;
8132
8133 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8134 if (!pipe_config)
8135 return -ENOMEM;
8136
8137 pipe_config->base.crtc = &crtc->base;
8138 pipe_config->pixel_multiplier = 1;
8139 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008140
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008141 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008142 chv_compute_dpll(crtc, pipe_config);
8143 chv_prepare_pll(crtc, pipe_config);
8144 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008145 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008146 vlv_compute_dpll(crtc, pipe_config);
8147 vlv_prepare_pll(crtc, pipe_config);
8148 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008149 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008150
8151 kfree(pipe_config);
8152
8153 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008154}
8155
8156/**
8157 * vlv_force_pll_off - forcibly disable just the PLL
8158 * @dev_priv: i915 private structure
8159 * @pipe: pipe PLL to disable
8160 *
8161 * Disable the PLL for @pipe. To be used in cases where we need
8162 * the PLL enabled even when @pipe is not going to be enabled.
8163 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008164void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02008165{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008166 if (IS_CHERRYVIEW(dev_priv))
8167 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008168 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008169 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008170}
8171
Daniel Vetter251ac862015-06-18 10:30:24 +02008172static void i9xx_compute_dpll(struct intel_crtc *crtc,
8173 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008174 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008175{
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008176 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008177 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008178 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008179
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008180 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308181
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008182 dpll = DPLL_VGA_MODE_DIS;
8183
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008184 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008185 dpll |= DPLLB_MODE_LVDS;
8186 else
8187 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01008188
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008189 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008190 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02008191 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008192 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02008193
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008194 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8195 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008196 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008197
Ville Syrjälä37a56502016-06-22 21:57:04 +03008198 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008199 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008200
8201 /* compute bitmask from p1 value */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008202 if (IS_PINEVIEW(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008203 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8204 else {
8205 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008206 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008207 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8208 }
8209 switch (clock->p2) {
8210 case 5:
8211 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8212 break;
8213 case 7:
8214 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8215 break;
8216 case 10:
8217 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8218 break;
8219 case 14:
8220 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8221 break;
8222 }
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008223 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008224 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8225
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008226 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008227 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008228 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008229 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008230 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8231 else
8232 dpll |= PLL_REF_INPUT_DREFCLK;
8233
8234 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008235 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008236
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008237 if (INTEL_GEN(dev_priv) >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008238 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008239 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008240 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008241 }
8242}
8243
Daniel Vetter251ac862015-06-18 10:30:24 +02008244static void i8xx_compute_dpll(struct intel_crtc *crtc,
8245 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008246 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008247{
Daniel Vetterf47709a2013-03-28 10:42:02 +01008248 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008249 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008250 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008251 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008252
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008253 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308254
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008255 dpll = DPLL_VGA_MODE_DIS;
8256
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008257 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008258 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8259 } else {
8260 if (clock->p1 == 2)
8261 dpll |= PLL_P1_DIVIDE_BY_TWO;
8262 else
8263 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8264 if (clock->p2 == 4)
8265 dpll |= PLL_P2_DIVIDE_BY_4;
8266 }
8267
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008268 if (!IS_I830(dev_priv) &&
8269 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008270 dpll |= DPLL_DVO_2X_MODE;
8271
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008272 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008273 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008274 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8275 else
8276 dpll |= PLL_REF_INPUT_DREFCLK;
8277
8278 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008279 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008280}
8281
Daniel Vetter8a654f32013-06-01 17:16:22 +02008282static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008283{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008284 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008285 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008286 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03008287 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008288 uint32_t crtc_vtotal, crtc_vblank_end;
8289 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008290
8291 /* We need to be careful not to changed the adjusted mode, for otherwise
8292 * the hw state checker will get angry at the mismatch. */
8293 crtc_vtotal = adjusted_mode->crtc_vtotal;
8294 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008295
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008296 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008297 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008298 crtc_vtotal -= 1;
8299 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008300
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008301 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008302 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8303 else
8304 vsyncshift = adjusted_mode->crtc_hsync_start -
8305 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008306 if (vsyncshift < 0)
8307 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008308 }
8309
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008310 if (INTEL_GEN(dev_priv) > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008311 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008312
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008313 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008314 (adjusted_mode->crtc_hdisplay - 1) |
8315 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008316 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008317 (adjusted_mode->crtc_hblank_start - 1) |
8318 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008319 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008320 (adjusted_mode->crtc_hsync_start - 1) |
8321 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8322
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008323 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008324 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008325 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008326 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008327 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008328 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008329 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008330 (adjusted_mode->crtc_vsync_start - 1) |
8331 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8332
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008333 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8334 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8335 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8336 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008337 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008338 (pipe == PIPE_B || pipe == PIPE_C))
8339 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8340
Jani Nikulabc58be62016-03-18 17:05:39 +02008341}
8342
8343static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8344{
8345 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008346 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008347 enum pipe pipe = intel_crtc->pipe;
8348
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008349 /* pipesrc controls the size that is scaled from, which should
8350 * always be the user's requested size.
8351 */
8352 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008353 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8354 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008355}
8356
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008357static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008358 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008359{
8360 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008361 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008362 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8363 uint32_t tmp;
8364
8365 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008366 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8367 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008368 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008369 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8370 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008371 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008372 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8373 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008374
8375 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008376 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8377 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008378 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008379 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8380 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008381 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008382 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8383 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008384
8385 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008386 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8387 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8388 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008389 }
Jani Nikulabc58be62016-03-18 17:05:39 +02008390}
8391
8392static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8393 struct intel_crtc_state *pipe_config)
8394{
8395 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008396 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008397 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008398
8399 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008400 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8401 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8402
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008403 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8404 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008405}
8406
Daniel Vetterf6a83282014-02-11 15:28:57 -08008407void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008408 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03008409{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008410 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8411 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8412 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8413 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008414
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008415 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8416 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8417 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8418 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008419
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008420 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008421 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03008422
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008423 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8424 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008425
8426 mode->hsync = drm_mode_hsync(mode);
8427 mode->vrefresh = drm_mode_vrefresh(mode);
8428 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03008429}
8430
Daniel Vetter84b046f2013-02-19 18:48:54 +01008431static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8432{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008433 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01008434 uint32_t pipeconf;
8435
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008436 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008437
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03008438 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8439 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8440 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02008441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008442 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008443 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008444
Daniel Vetterff9ce462013-04-24 14:57:17 +02008445 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008446 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8447 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008448 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008449 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02008450 pipeconf |= PIPECONF_DITHER_EN |
8451 PIPECONF_DITHER_TYPE_SP;
8452
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008453 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008454 case 18:
8455 pipeconf |= PIPECONF_6BPC;
8456 break;
8457 case 24:
8458 pipeconf |= PIPECONF_8BPC;
8459 break;
8460 case 30:
8461 pipeconf |= PIPECONF_10BPC;
8462 break;
8463 default:
8464 /* Case prevented by intel_choose_pipe_bpp_dither. */
8465 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01008466 }
8467 }
8468
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +00008469 if (HAS_PIPE_CXSR(dev_priv)) {
Daniel Vetter84b046f2013-02-19 18:48:54 +01008470 if (intel_crtc->lowfreq_avail) {
8471 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8472 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8473 } else {
8474 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01008475 }
8476 }
8477
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008478 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008479 if (INTEL_GEN(dev_priv) < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008480 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008481 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8482 else
8483 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8484 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01008485 pipeconf |= PIPECONF_PROGRESSIVE;
8486
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008487 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008488 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008489 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008490
Daniel Vetter84b046f2013-02-19 18:48:54 +01008491 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8492 POSTING_READ(PIPECONF(intel_crtc->pipe));
8493}
8494
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008495static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8496 struct intel_crtc_state *crtc_state)
8497{
8498 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008499 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008500 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008501 int refclk = 48000;
8502
8503 memset(&crtc_state->dpll_hw_state, 0,
8504 sizeof(crtc_state->dpll_hw_state));
8505
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008506 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008507 if (intel_panel_use_ssc(dev_priv)) {
8508 refclk = dev_priv->vbt.lvds_ssc_freq;
8509 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8510 }
8511
8512 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008513 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008514 limit = &intel_limits_i8xx_dvo;
8515 } else {
8516 limit = &intel_limits_i8xx_dac;
8517 }
8518
8519 if (!crtc_state->clock_set &&
8520 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8521 refclk, NULL, &crtc_state->dpll)) {
8522 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8523 return -EINVAL;
8524 }
8525
8526 i8xx_compute_dpll(crtc, crtc_state, NULL);
8527
8528 return 0;
8529}
8530
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008531static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8532 struct intel_crtc_state *crtc_state)
8533{
8534 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008535 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008536 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008537 int refclk = 96000;
8538
8539 memset(&crtc_state->dpll_hw_state, 0,
8540 sizeof(crtc_state->dpll_hw_state));
8541
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008542 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008543 if (intel_panel_use_ssc(dev_priv)) {
8544 refclk = dev_priv->vbt.lvds_ssc_freq;
8545 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8546 }
8547
8548 if (intel_is_dual_link_lvds(dev))
8549 limit = &intel_limits_g4x_dual_channel_lvds;
8550 else
8551 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008552 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8553 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008554 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008555 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008556 limit = &intel_limits_g4x_sdvo;
8557 } else {
8558 /* The option is for other outputs */
8559 limit = &intel_limits_i9xx_sdvo;
8560 }
8561
8562 if (!crtc_state->clock_set &&
8563 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8564 refclk, NULL, &crtc_state->dpll)) {
8565 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8566 return -EINVAL;
8567 }
8568
8569 i9xx_compute_dpll(crtc, crtc_state, NULL);
8570
8571 return 0;
8572}
8573
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008574static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8575 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008576{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008577 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008578 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008579 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008580 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008581
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008582 memset(&crtc_state->dpll_hw_state, 0,
8583 sizeof(crtc_state->dpll_hw_state));
8584
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008585 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008586 if (intel_panel_use_ssc(dev_priv)) {
8587 refclk = dev_priv->vbt.lvds_ssc_freq;
8588 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8589 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008590
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008591 limit = &intel_limits_pineview_lvds;
8592 } else {
8593 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008594 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008595
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008596 if (!crtc_state->clock_set &&
8597 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8598 refclk, NULL, &crtc_state->dpll)) {
8599 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8600 return -EINVAL;
8601 }
8602
8603 i9xx_compute_dpll(crtc, crtc_state, NULL);
8604
8605 return 0;
8606}
8607
8608static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8609 struct intel_crtc_state *crtc_state)
8610{
8611 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008612 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008613 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008614 int refclk = 96000;
8615
8616 memset(&crtc_state->dpll_hw_state, 0,
8617 sizeof(crtc_state->dpll_hw_state));
8618
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008619 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008620 if (intel_panel_use_ssc(dev_priv)) {
8621 refclk = dev_priv->vbt.lvds_ssc_freq;
8622 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008623 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008624
8625 limit = &intel_limits_i9xx_lvds;
8626 } else {
8627 limit = &intel_limits_i9xx_sdvo;
8628 }
8629
8630 if (!crtc_state->clock_set &&
8631 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8632 refclk, NULL, &crtc_state->dpll)) {
8633 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8634 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008635 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008636
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008637 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008638
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008639 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008640}
8641
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008642static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8643 struct intel_crtc_state *crtc_state)
8644{
8645 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008646 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008647
8648 memset(&crtc_state->dpll_hw_state, 0,
8649 sizeof(crtc_state->dpll_hw_state));
8650
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008651 if (!crtc_state->clock_set &&
8652 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8653 refclk, NULL, &crtc_state->dpll)) {
8654 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8655 return -EINVAL;
8656 }
8657
8658 chv_compute_dpll(crtc, crtc_state);
8659
8660 return 0;
8661}
8662
8663static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8664 struct intel_crtc_state *crtc_state)
8665{
8666 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008667 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008668
8669 memset(&crtc_state->dpll_hw_state, 0,
8670 sizeof(crtc_state->dpll_hw_state));
8671
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008672 if (!crtc_state->clock_set &&
8673 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8674 refclk, NULL, &crtc_state->dpll)) {
8675 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8676 return -EINVAL;
8677 }
8678
8679 vlv_compute_dpll(crtc, crtc_state);
8680
8681 return 0;
8682}
8683
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008684static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008685 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008686{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008687 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008688 uint32_t tmp;
8689
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008690 if (INTEL_GEN(dev_priv) <= 3 &&
8691 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008692 return;
8693
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008694 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008695 if (!(tmp & PFIT_ENABLE))
8696 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008697
Daniel Vetter06922822013-07-11 13:35:40 +02008698 /* Check whether the pfit is attached to our pipe. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008699 if (INTEL_GEN(dev_priv) < 4) {
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008700 if (crtc->pipe != PIPE_B)
8701 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008702 } else {
8703 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8704 return;
8705 }
8706
Daniel Vetter06922822013-07-11 13:35:40 +02008707 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008708 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008709}
8710
Jesse Barnesacbec812013-09-20 11:29:32 -07008711static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008712 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008713{
8714 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008715 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008716 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008717 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008718 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008719 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008720
Ville Syrjäläb5219732016-03-15 16:40:01 +02008721 /* In case of DSI, DPLL will not be used */
8722 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308723 return;
8724
Ville Syrjäläa5805162015-05-26 20:42:30 +03008725 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008726 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008727 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008728
8729 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8730 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8731 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8732 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8733 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8734
Imre Deakdccbea32015-06-22 23:35:51 +03008735 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008736}
8737
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008738static void
8739i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8740 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008741{
8742 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008743 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008744 u32 val, base, offset;
8745 int pipe = crtc->pipe, plane = crtc->plane;
8746 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008747 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008748 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008749 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008750
Damien Lespiau42a7b082015-02-05 19:35:13 +00008751 val = I915_READ(DSPCNTR(plane));
8752 if (!(val & DISPLAY_PLANE_ENABLE))
8753 return;
8754
Damien Lespiaud9806c92015-01-21 14:07:19 +00008755 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008756 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008757 DRM_DEBUG_KMS("failed to alloc fb\n");
8758 return;
8759 }
8760
Damien Lespiau1b842c82015-01-21 13:50:54 +00008761 fb = &intel_fb->base;
8762
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008763 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00008764 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008765 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008766 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00008767 }
8768 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008769
8770 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008771 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008772 fb->pixel_format = fourcc;
8773 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008774
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008775 if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008776 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008777 offset = I915_READ(DSPTILEOFF(plane));
8778 else
8779 offset = I915_READ(DSPLINOFF(plane));
8780 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8781 } else {
8782 base = I915_READ(DSPADDR(plane));
8783 }
8784 plane_config->base = base;
8785
8786 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008787 fb->width = ((val >> 16) & 0xfff) + 1;
8788 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008789
8790 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008791 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008792
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008793 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008794 fb->pixel_format,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02008795 fb->modifier);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008796
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008797 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008798
Damien Lespiau2844a922015-01-20 12:51:48 +00008799 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8800 pipe_name(pipe), plane, fb->width, fb->height,
8801 fb->bits_per_pixel, base, fb->pitches[0],
8802 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008803
Damien Lespiau2d140302015-02-05 17:22:18 +00008804 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008805}
8806
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008807static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008808 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008809{
8810 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008811 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008812 int pipe = pipe_config->cpu_transcoder;
8813 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008814 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008815 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008816 int refclk = 100000;
8817
Ville Syrjäläb5219732016-03-15 16:40:01 +02008818 /* In case of DSI, DPLL will not be used */
8819 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8820 return;
8821
Ville Syrjäläa5805162015-05-26 20:42:30 +03008822 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008823 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8824 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8825 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8826 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008827 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008828 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008829
8830 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008831 clock.m2 = (pll_dw0 & 0xff) << 22;
8832 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8833 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008834 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8835 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8836 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8837
Imre Deakdccbea32015-06-22 23:35:51 +03008838 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008839}
8840
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008841static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008842 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008843{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008844 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +02008845 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008846 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008847 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008848
Imre Deak17290502016-02-12 18:55:11 +02008849 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8850 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008851 return false;
8852
Daniel Vettere143a212013-07-04 12:01:15 +02008853 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008854 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008855
Imre Deak17290502016-02-12 18:55:11 +02008856 ret = false;
8857
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008858 tmp = I915_READ(PIPECONF(crtc->pipe));
8859 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008860 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008861
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008862 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8863 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008864 switch (tmp & PIPECONF_BPC_MASK) {
8865 case PIPECONF_6BPC:
8866 pipe_config->pipe_bpp = 18;
8867 break;
8868 case PIPECONF_8BPC:
8869 pipe_config->pipe_bpp = 24;
8870 break;
8871 case PIPECONF_10BPC:
8872 pipe_config->pipe_bpp = 30;
8873 break;
8874 default:
8875 break;
8876 }
8877 }
8878
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008879 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008880 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008881 pipe_config->limited_color_range = true;
8882
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008883 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä282740f2013-09-04 18:30:03 +03008884 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8885
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008886 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008887 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008888
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008889 i9xx_get_pfit_config(crtc, pipe_config);
8890
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00008891 if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008892 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008893 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02008894 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8895 else
8896 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008897 pipe_config->pixel_multiplier =
8898 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8899 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008900 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008901 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8902 IS_G33(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02008903 tmp = I915_READ(DPLL(crtc->pipe));
8904 pipe_config->pixel_multiplier =
8905 ((tmp & SDVO_MULTIPLIER_MASK)
8906 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8907 } else {
8908 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8909 * port and will be fixed up in the encoder->get_config
8910 * function. */
8911 pipe_config->pixel_multiplier = 1;
8912 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008913 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008914 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008915 /*
8916 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8917 * on 830. Filter it out here so that we don't
8918 * report errors due to that.
8919 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008920 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008921 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8922
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008923 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8924 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008925 } else {
8926 /* Mask out read-only status bits. */
8927 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8928 DPLL_PORTC_READY_MASK |
8929 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008930 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008931
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008932 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008933 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01008934 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07008935 vlv_crtc_clock_get(crtc, pipe_config);
8936 else
8937 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008938
Ville Syrjälä0f646142015-08-26 19:39:18 +03008939 /*
8940 * Normally the dotclock is filled in by the encoder .get_config()
8941 * but in case the pipe is enabled w/o any ports we need a sane
8942 * default.
8943 */
8944 pipe_config->base.adjusted_mode.crtc_clock =
8945 pipe_config->port_clock / pipe_config->pixel_multiplier;
8946
Imre Deak17290502016-02-12 18:55:11 +02008947 ret = true;
8948
8949out:
8950 intel_display_power_put(dev_priv, power_domain);
8951
8952 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008953}
8954
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008955static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008956{
Jesse Barnes13d83a62011-08-03 12:59:20 -07008957 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008958 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008959 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008960 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008961 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008962 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008963 bool has_ck505 = false;
8964 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008965 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008966
8967 /* We need to take the global config into account */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02008968 for_each_intel_encoder(&dev_priv->drm, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008969 switch (encoder->type) {
8970 case INTEL_OUTPUT_LVDS:
8971 has_panel = true;
8972 has_lvds = true;
8973 break;
8974 case INTEL_OUTPUT_EDP:
8975 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008976 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008977 has_cpu_edp = true;
8978 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008979 default:
8980 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008981 }
8982 }
8983
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008984 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008985 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008986 can_ssc = has_ck505;
8987 } else {
8988 has_ck505 = false;
8989 can_ssc = true;
8990 }
8991
Lyude1c1a24d2016-06-14 11:04:09 -04008992 /* Check if any DPLLs are using the SSC source */
8993 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8994 u32 temp = I915_READ(PCH_DPLL(i));
8995
8996 if (!(temp & DPLL_VCO_ENABLE))
8997 continue;
8998
8999 if ((temp & PLL_REF_INPUT_MASK) ==
9000 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9001 using_ssc_source = true;
9002 break;
9003 }
9004 }
9005
9006 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9007 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009008
9009 /* Ironlake: try to setup display ref clock before DPLL
9010 * enabling. This is only under driver's control after
9011 * PCH B stepping, previous chipset stepping should be
9012 * ignoring this setting.
9013 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009014 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009015
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009016 /* As we must carefully and slowly disable/enable each source in turn,
9017 * compute the final state we want first and check if we need to
9018 * make any changes at all.
9019 */
9020 final = val;
9021 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07009022 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009023 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07009024 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009025 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9026
Daniel Vetter8c07eb62016-06-09 18:39:07 +02009027 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009028 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02009029 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009030
Keith Packard199e5d72011-09-22 12:01:57 -07009031 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009032 final |= DREF_SSC_SOURCE_ENABLE;
9033
9034 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9035 final |= DREF_SSC1_ENABLE;
9036
9037 if (has_cpu_edp) {
9038 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9039 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9040 else
9041 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9042 } else
9043 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04009044 } else if (using_ssc_source) {
9045 final |= DREF_SSC_SOURCE_ENABLE;
9046 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009047 }
9048
9049 if (final == val)
9050 return;
9051
9052 /* Always enable nonspread source */
9053 val &= ~DREF_NONSPREAD_SOURCE_MASK;
9054
9055 if (has_ck505)
9056 val |= DREF_NONSPREAD_CK505_ENABLE;
9057 else
9058 val |= DREF_NONSPREAD_SOURCE_ENABLE;
9059
9060 if (has_panel) {
9061 val &= ~DREF_SSC_SOURCE_MASK;
9062 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009063
Keith Packard199e5d72011-09-22 12:01:57 -07009064 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07009065 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009066 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009067 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02009068 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009069 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009070
9071 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009072 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009073 POSTING_READ(PCH_DREF_CONTROL);
9074 udelay(200);
9075
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009076 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009077
9078 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07009079 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07009080 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009081 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009082 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02009083 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009084 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07009085 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009086 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009087
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009088 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009089 POSTING_READ(PCH_DREF_CONTROL);
9090 udelay(200);
9091 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04009092 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009093
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009094 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07009095
9096 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009097 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009098
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009099 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009100 POSTING_READ(PCH_DREF_CONTROL);
9101 udelay(200);
9102
Lyude1c1a24d2016-06-14 11:04:09 -04009103 if (!using_ssc_source) {
9104 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009105
Lyude1c1a24d2016-06-14 11:04:09 -04009106 /* Turn off the SSC source */
9107 val &= ~DREF_SSC_SOURCE_MASK;
9108 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009109
Lyude1c1a24d2016-06-14 11:04:09 -04009110 /* Turn off SSC1 */
9111 val &= ~DREF_SSC1_ENABLE;
9112
9113 I915_WRITE(PCH_DREF_CONTROL, val);
9114 POSTING_READ(PCH_DREF_CONTROL);
9115 udelay(200);
9116 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07009117 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009118
9119 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009120}
9121
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009122static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02009123{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009124 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009125
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009126 tmp = I915_READ(SOUTH_CHICKEN2);
9127 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9128 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009129
Imre Deakcf3598c2016-06-28 13:37:31 +03009130 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9131 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009132 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02009133
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009134 tmp = I915_READ(SOUTH_CHICKEN2);
9135 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9136 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009137
Imre Deakcf3598c2016-06-28 13:37:31 +03009138 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9139 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009140 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009141}
9142
9143/* WaMPhyProgramming:hsw */
9144static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9145{
9146 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009147
9148 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9149 tmp &= ~(0xFF << 24);
9150 tmp |= (0x12 << 24);
9151 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9152
Paulo Zanonidde86e22012-12-01 12:04:25 -02009153 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9154 tmp |= (1 << 11);
9155 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9156
9157 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9158 tmp |= (1 << 11);
9159 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9160
Paulo Zanonidde86e22012-12-01 12:04:25 -02009161 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9162 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9163 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9164
9165 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9166 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9167 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9168
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009169 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9170 tmp &= ~(7 << 13);
9171 tmp |= (5 << 13);
9172 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009173
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009174 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9175 tmp &= ~(7 << 13);
9176 tmp |= (5 << 13);
9177 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009178
9179 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9180 tmp &= ~0xFF;
9181 tmp |= 0x1C;
9182 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9183
9184 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9185 tmp &= ~0xFF;
9186 tmp |= 0x1C;
9187 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9188
9189 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9190 tmp &= ~(0xFF << 16);
9191 tmp |= (0x1C << 16);
9192 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9193
9194 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9195 tmp &= ~(0xFF << 16);
9196 tmp |= (0x1C << 16);
9197 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9198
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009199 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9200 tmp |= (1 << 27);
9201 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009202
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009203 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9204 tmp |= (1 << 27);
9205 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009206
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009207 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9208 tmp &= ~(0xF << 28);
9209 tmp |= (4 << 28);
9210 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009211
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009212 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9213 tmp &= ~(0xF << 28);
9214 tmp |= (4 << 28);
9215 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009216}
9217
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009218/* Implements 3 different sequences from BSpec chapter "Display iCLK
9219 * Programming" based on the parameters passed:
9220 * - Sequence to enable CLKOUT_DP
9221 * - Sequence to enable CLKOUT_DP without spread
9222 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9223 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009224static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9225 bool with_spread, bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009226{
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009227 uint32_t reg, tmp;
9228
9229 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9230 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009231 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9232 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009233 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009234
Ville Syrjäläa5805162015-05-26 20:42:30 +03009235 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009236
9237 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9238 tmp &= ~SBI_SSCCTL_DISABLE;
9239 tmp |= SBI_SSCCTL_PATHALT;
9240 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9241
9242 udelay(24);
9243
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009244 if (with_spread) {
9245 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9246 tmp &= ~SBI_SSCCTL_PATHALT;
9247 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009248
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009249 if (with_fdi) {
9250 lpt_reset_fdi_mphy(dev_priv);
9251 lpt_program_fdi_mphy(dev_priv);
9252 }
9253 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02009254
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009255 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009256 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9257 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9258 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01009259
Ville Syrjäläa5805162015-05-26 20:42:30 +03009260 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009261}
9262
Paulo Zanoni47701c32013-07-23 11:19:25 -03009263/* Sequence to disable CLKOUT_DP */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009264static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
Paulo Zanoni47701c32013-07-23 11:19:25 -03009265{
Paulo Zanoni47701c32013-07-23 11:19:25 -03009266 uint32_t reg, tmp;
9267
Ville Syrjäläa5805162015-05-26 20:42:30 +03009268 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009269
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009270 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03009271 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9272 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9273 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9274
9275 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9276 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9277 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9278 tmp |= SBI_SSCCTL_PATHALT;
9279 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9280 udelay(32);
9281 }
9282 tmp |= SBI_SSCCTL_DISABLE;
9283 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9284 }
9285
Ville Syrjäläa5805162015-05-26 20:42:30 +03009286 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009287}
9288
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009289#define BEND_IDX(steps) ((50 + (steps)) / 5)
9290
9291static const uint16_t sscdivintphase[] = {
9292 [BEND_IDX( 50)] = 0x3B23,
9293 [BEND_IDX( 45)] = 0x3B23,
9294 [BEND_IDX( 40)] = 0x3C23,
9295 [BEND_IDX( 35)] = 0x3C23,
9296 [BEND_IDX( 30)] = 0x3D23,
9297 [BEND_IDX( 25)] = 0x3D23,
9298 [BEND_IDX( 20)] = 0x3E23,
9299 [BEND_IDX( 15)] = 0x3E23,
9300 [BEND_IDX( 10)] = 0x3F23,
9301 [BEND_IDX( 5)] = 0x3F23,
9302 [BEND_IDX( 0)] = 0x0025,
9303 [BEND_IDX( -5)] = 0x0025,
9304 [BEND_IDX(-10)] = 0x0125,
9305 [BEND_IDX(-15)] = 0x0125,
9306 [BEND_IDX(-20)] = 0x0225,
9307 [BEND_IDX(-25)] = 0x0225,
9308 [BEND_IDX(-30)] = 0x0325,
9309 [BEND_IDX(-35)] = 0x0325,
9310 [BEND_IDX(-40)] = 0x0425,
9311 [BEND_IDX(-45)] = 0x0425,
9312 [BEND_IDX(-50)] = 0x0525,
9313};
9314
9315/*
9316 * Bend CLKOUT_DP
9317 * steps -50 to 50 inclusive, in steps of 5
9318 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9319 * change in clock period = -(steps / 10) * 5.787 ps
9320 */
9321static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9322{
9323 uint32_t tmp;
9324 int idx = BEND_IDX(steps);
9325
9326 if (WARN_ON(steps % 5 != 0))
9327 return;
9328
9329 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9330 return;
9331
9332 mutex_lock(&dev_priv->sb_lock);
9333
9334 if (steps % 10 != 0)
9335 tmp = 0xAAAAAAAB;
9336 else
9337 tmp = 0x00000000;
9338 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9339
9340 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9341 tmp &= 0xffff0000;
9342 tmp |= sscdivintphase[idx];
9343 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9344
9345 mutex_unlock(&dev_priv->sb_lock);
9346}
9347
9348#undef BEND_IDX
9349
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009350static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009351{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009352 struct intel_encoder *encoder;
9353 bool has_vga = false;
9354
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009355 for_each_intel_encoder(&dev_priv->drm, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009356 switch (encoder->type) {
9357 case INTEL_OUTPUT_ANALOG:
9358 has_vga = true;
9359 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02009360 default:
9361 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009362 }
9363 }
9364
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009365 if (has_vga) {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009366 lpt_bend_clkout_dp(dev_priv, 0);
9367 lpt_enable_clkout_dp(dev_priv, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009368 } else {
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009369 lpt_disable_clkout_dp(dev_priv);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009370 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009371}
9372
Paulo Zanonidde86e22012-12-01 12:04:25 -02009373/*
9374 * Initialize reference clocks when the driver loads
9375 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009376void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02009377{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009378 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009379 ironlake_init_pch_refclk(dev_priv);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009380 else if (HAS_PCH_LPT(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02009381 lpt_init_pch_refclk(dev_priv);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009382}
9383
Daniel Vetter6ff93602013-04-19 11:24:36 +02009384static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03009385{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009386 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03009387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9388 int pipe = intel_crtc->pipe;
9389 uint32_t val;
9390
Daniel Vetter78114072013-06-13 00:54:57 +02009391 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03009392
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009393 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03009394 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009395 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009396 break;
9397 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009398 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009399 break;
9400 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009401 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009402 break;
9403 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009404 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009405 break;
9406 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03009407 /* Case prevented by intel_choose_pipe_bpp_dither. */
9408 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03009409 }
9410
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009411 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03009412 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9413
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009414 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03009415 val |= PIPECONF_INTERLACED_ILK;
9416 else
9417 val |= PIPECONF_PROGRESSIVE;
9418
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009419 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009420 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009421
Paulo Zanonic8203562012-09-12 10:06:29 -03009422 I915_WRITE(PIPECONF(pipe), val);
9423 POSTING_READ(PIPECONF(pipe));
9424}
9425
Daniel Vetter6ff93602013-04-19 11:24:36 +02009426static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009427{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009428 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009430 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02009431 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009432
Jani Nikula391bf042016-03-18 17:05:40 +02009433 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009434 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9435
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009436 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009437 val |= PIPECONF_INTERLACED_ILK;
9438 else
9439 val |= PIPECONF_PROGRESSIVE;
9440
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009441 I915_WRITE(PIPECONF(cpu_transcoder), val);
9442 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02009443}
9444
Jani Nikula391bf042016-03-18 17:05:40 +02009445static void haswell_set_pipemisc(struct drm_crtc *crtc)
9446{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009447 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02009448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9449
9450 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9451 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009452
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009453 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009454 case 18:
9455 val |= PIPEMISC_DITHER_6_BPC;
9456 break;
9457 case 24:
9458 val |= PIPEMISC_DITHER_8_BPC;
9459 break;
9460 case 30:
9461 val |= PIPEMISC_DITHER_10_BPC;
9462 break;
9463 case 36:
9464 val |= PIPEMISC_DITHER_12_BPC;
9465 break;
9466 default:
9467 /* Case prevented by pipe_config_set_bpp. */
9468 BUG();
9469 }
9470
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009471 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009472 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9473
Jani Nikula391bf042016-03-18 17:05:40 +02009474 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009475 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009476}
9477
Paulo Zanonid4b19312012-11-29 11:29:32 -02009478int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9479{
9480 /*
9481 * Account for spread spectrum to avoid
9482 * oversubscribing the link. Max center spread
9483 * is 2.5%; use 5% for safety's sake.
9484 */
9485 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02009486 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02009487}
9488
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009489static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02009490{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009491 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03009492}
9493
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009494static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9495 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009496 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009497{
9498 struct drm_crtc *crtc = &intel_crtc->base;
9499 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009500 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009501 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009502 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08009503
Chris Wilsonc1858122010-12-03 21:35:48 +00009504 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009505 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009506 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07009507 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009508 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009509 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009510 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009511 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009512 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009513
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009514 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00009515
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009516 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
9517 fp |= FP_CB_TUNE;
9518
9519 if (reduced_clock) {
9520 fp2 = i9xx_dpll_compute_fp(reduced_clock);
9521
9522 if (reduced_clock->m < factor * reduced_clock->n)
9523 fp2 |= FP_CB_TUNE;
9524 } else {
9525 fp2 = fp;
9526 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009527
Chris Wilson5eddb702010-09-11 13:48:45 +01009528 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009529
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009530 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07009531 dpll |= DPLLB_MODE_LVDS;
9532 else
9533 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009534
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009535 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009536 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009537
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009538 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9539 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009540 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009541
Ville Syrjälä37a56502016-06-22 21:57:04 +03009542 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009543 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009544
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03009545 /*
9546 * The high speed IO clock is only really required for
9547 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9548 * possible to share the DPLL between CRT and HDMI. Enabling
9549 * the clock needlessly does no real harm, except use up a
9550 * bit of power potentially.
9551 *
9552 * We'll limit this to IVB with 3 pipes, since it has only two
9553 * DPLLs and so DPLL sharing is the only way to get three pipes
9554 * driving PCH ports at the same time. On SNB we could do this,
9555 * and potentially avoid enabling the second DPLL, but it's not
9556 * clear if it''s a win or loss power wise. No point in doing
9557 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9558 */
9559 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9560 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9561 dpll |= DPLL_SDVO_HIGH_SPEED;
9562
Eric Anholta07d6782011-03-30 13:01:08 -07009563 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009564 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009565 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009566 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009567
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009568 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009569 case 5:
9570 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9571 break;
9572 case 7:
9573 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9574 break;
9575 case 10:
9576 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9577 break;
9578 case 14:
9579 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9580 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009581 }
9582
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009583 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9584 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009585 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009586 else
9587 dpll |= PLL_REF_INPUT_DREFCLK;
9588
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009589 dpll |= DPLL_VCO_ENABLE;
9590
9591 crtc_state->dpll_hw_state.dpll = dpll;
9592 crtc_state->dpll_hw_state.fp0 = fp;
9593 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009594}
9595
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009596static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9597 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009598{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009599 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009600 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009601 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009602 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009603 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009604 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009605 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009606
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009607 memset(&crtc_state->dpll_hw_state, 0,
9608 sizeof(crtc_state->dpll_hw_state));
9609
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009610 crtc->lowfreq_avail = false;
9611
9612 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9613 if (!crtc_state->has_pch_encoder)
9614 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009615
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009616 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009617 if (intel_panel_use_ssc(dev_priv)) {
9618 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9619 dev_priv->vbt.lvds_ssc_freq);
9620 refclk = dev_priv->vbt.lvds_ssc_freq;
9621 }
9622
9623 if (intel_is_dual_link_lvds(dev)) {
9624 if (refclk == 100000)
9625 limit = &intel_limits_ironlake_dual_lvds_100m;
9626 else
9627 limit = &intel_limits_ironlake_dual_lvds;
9628 } else {
9629 if (refclk == 100000)
9630 limit = &intel_limits_ironlake_single_lvds_100m;
9631 else
9632 limit = &intel_limits_ironlake_single_lvds;
9633 }
9634 } else {
9635 limit = &intel_limits_ironlake_dac;
9636 }
9637
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009638 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009639 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9640 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009641 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9642 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009643 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009644
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009645 ironlake_compute_dpll(crtc, crtc_state,
9646 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009647
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009648 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9649 if (pll == NULL) {
9650 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9651 pipe_name(crtc->pipe));
9652 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009653 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009654
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009655 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009656 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009657 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009658
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009659 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009660}
9661
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009662static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9663 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009664{
9665 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009666 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009667 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009668
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009669 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9670 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9671 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9672 & ~TU_SIZE_MASK;
9673 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9674 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9675 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9676}
9677
9678static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9679 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009680 struct intel_link_m_n *m_n,
9681 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009682{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009683 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009684 enum pipe pipe = crtc->pipe;
9685
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009686 if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009687 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9688 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9689 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9690 & ~TU_SIZE_MASK;
9691 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9692 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9693 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009694 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9695 * gen < 8) and if DRRS is supported (to make sure the
9696 * registers are not unnecessarily read).
9697 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009698 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009699 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009700 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9701 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9702 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9703 & ~TU_SIZE_MASK;
9704 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9705 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9706 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9707 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009708 } else {
9709 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9710 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9711 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9712 & ~TU_SIZE_MASK;
9713 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9714 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9715 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9716 }
9717}
9718
9719void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009720 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009721{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009722 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009723 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9724 else
9725 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009726 &pipe_config->dp_m_n,
9727 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009728}
9729
Daniel Vetter72419202013-04-04 13:28:53 +02009730static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009731 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009732{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009733 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009734 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009735}
9736
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009737static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009738 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009739{
9740 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009741 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009742 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9743 uint32_t ps_ctrl = 0;
9744 int id = -1;
9745 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009746
Chandra Kondurua1b22782015-04-07 15:28:45 -07009747 /* find scaler attached to this pipe */
9748 for (i = 0; i < crtc->num_scalers; i++) {
9749 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9750 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9751 id = i;
9752 pipe_config->pch_pfit.enabled = true;
9753 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9754 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9755 break;
9756 }
9757 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009758
Chandra Kondurua1b22782015-04-07 15:28:45 -07009759 scaler_state->scaler_id = id;
9760 if (id >= 0) {
9761 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9762 } else {
9763 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009764 }
9765}
9766
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009767static void
9768skylake_get_initial_plane_config(struct intel_crtc *crtc,
9769 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009770{
9771 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009772 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009773 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009774 int pipe = crtc->pipe;
9775 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009776 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009777 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009778 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009779
Damien Lespiaud9806c92015-01-21 14:07:19 +00009780 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009781 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009782 DRM_DEBUG_KMS("failed to alloc fb\n");
9783 return;
9784 }
9785
Damien Lespiau1b842c82015-01-21 13:50:54 +00009786 fb = &intel_fb->base;
9787
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009788 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009789 if (!(val & PLANE_CTL_ENABLE))
9790 goto error;
9791
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009792 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9793 fourcc = skl_format_to_fourcc(pixel_format,
9794 val & PLANE_CTL_ORDER_RGBX,
9795 val & PLANE_CTL_ALPHA_MASK);
9796 fb->pixel_format = fourcc;
9797 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9798
Damien Lespiau40f46282015-02-27 11:15:21 +00009799 tiling = val & PLANE_CTL_TILED_MASK;
9800 switch (tiling) {
9801 case PLANE_CTL_TILED_LINEAR:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009802 fb->modifier = DRM_FORMAT_MOD_NONE;
Damien Lespiau40f46282015-02-27 11:15:21 +00009803 break;
9804 case PLANE_CTL_TILED_X:
9805 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009806 fb->modifier = I915_FORMAT_MOD_X_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00009807 break;
9808 case PLANE_CTL_TILED_Y:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009809 fb->modifier = I915_FORMAT_MOD_Y_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00009810 break;
9811 case PLANE_CTL_TILED_YF:
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009812 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
Damien Lespiau40f46282015-02-27 11:15:21 +00009813 break;
9814 default:
9815 MISSING_CASE(tiling);
9816 goto error;
9817 }
9818
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009819 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9820 plane_config->base = base;
9821
9822 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9823
9824 val = I915_READ(PLANE_SIZE(pipe, 0));
9825 fb->height = ((val >> 16) & 0xfff) + 1;
9826 fb->width = ((val >> 0) & 0x1fff) + 1;
9827
9828 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009829 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
Damien Lespiau40f46282015-02-27 11:15:21 +00009830 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009831 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9832
9833 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009834 fb->pixel_format,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009835 fb->modifier);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009836
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009837 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009838
9839 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9840 pipe_name(pipe), fb->width, fb->height,
9841 fb->bits_per_pixel, base, fb->pitches[0],
9842 plane_config->size);
9843
Damien Lespiau2d140302015-02-05 17:22:18 +00009844 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009845 return;
9846
9847error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01009848 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009849}
9850
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009851static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009852 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009853{
9854 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009855 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009856 uint32_t tmp;
9857
9858 tmp = I915_READ(PF_CTL(crtc->pipe));
9859
9860 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009861 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009862 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9863 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009864
9865 /* We currently do not free assignements of panel fitters on
9866 * ivb/hsw (since we don't use the higher upscaling modes which
9867 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009868 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009869 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9870 PF_PIPE_SEL_IVB(crtc->pipe));
9871 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009872 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009873}
9874
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009875static void
9876ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9877 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009878{
9879 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009880 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009881 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009882 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009883 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009884 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009885 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009886 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009887
Damien Lespiau42a7b082015-02-05 19:35:13 +00009888 val = I915_READ(DSPCNTR(pipe));
9889 if (!(val & DISPLAY_PLANE_ENABLE))
9890 return;
9891
Damien Lespiaud9806c92015-01-21 14:07:19 +00009892 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009893 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009894 DRM_DEBUG_KMS("failed to alloc fb\n");
9895 return;
9896 }
9897
Damien Lespiau1b842c82015-01-21 13:50:54 +00009898 fb = &intel_fb->base;
9899
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +00009900 if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter18c52472015-02-10 17:16:09 +00009901 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009902 plane_config->tiling = I915_TILING_X;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009903 fb->modifier = I915_FORMAT_MOD_X_TILED;
Daniel Vetter18c52472015-02-10 17:16:09 +00009904 }
9905 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009906
9907 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009908 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009909 fb->pixel_format = fourcc;
9910 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009911
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009912 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01009913 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009914 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009915 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009916 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009917 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009918 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009919 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009920 }
9921 plane_config->base = base;
9922
9923 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009924 fb->width = ((val >> 16) & 0xfff) + 1;
9925 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009926
9927 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009928 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009929
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009930 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009931 fb->pixel_format,
Ville Syrjäläbae781b2016-11-16 13:33:16 +02009932 fb->modifier);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009933
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009934 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009935
Damien Lespiau2844a922015-01-20 12:51:48 +00009936 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9937 pipe_name(pipe), fb->width, fb->height,
9938 fb->bits_per_pixel, base, fb->pitches[0],
9939 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009940
Damien Lespiau2d140302015-02-05 17:22:18 +00009941 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009942}
9943
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009944static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009945 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009946{
9947 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009948 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009949 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009950 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009951 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009952
Imre Deak17290502016-02-12 18:55:11 +02009953 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9954 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009955 return false;
9956
Daniel Vettere143a212013-07-04 12:01:15 +02009957 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009958 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009959
Imre Deak17290502016-02-12 18:55:11 +02009960 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009961 tmp = I915_READ(PIPECONF(crtc->pipe));
9962 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009963 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009964
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009965 switch (tmp & PIPECONF_BPC_MASK) {
9966 case PIPECONF_6BPC:
9967 pipe_config->pipe_bpp = 18;
9968 break;
9969 case PIPECONF_8BPC:
9970 pipe_config->pipe_bpp = 24;
9971 break;
9972 case PIPECONF_10BPC:
9973 pipe_config->pipe_bpp = 30;
9974 break;
9975 case PIPECONF_12BPC:
9976 pipe_config->pipe_bpp = 36;
9977 break;
9978 default:
9979 break;
9980 }
9981
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009982 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9983 pipe_config->limited_color_range = true;
9984
Daniel Vetterab9412b2013-05-03 11:49:46 +02009985 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009986 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009987 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009988
Daniel Vetter88adfff2013-03-28 10:42:01 +01009989 pipe_config->has_pch_encoder = true;
9990
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009991 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9992 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9993 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009994
9995 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009996
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009997 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009998 /*
9999 * The pipe->pch transcoder and pch transcoder->pll
10000 * mapping is fixed.
10001 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010002 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010003 } else {
10004 tmp = I915_READ(PCH_DPLL_SEL);
10005 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010006 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010007 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010008 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010009 }
Daniel Vetter66e985c2013-06-05 13:34:20 +020010010
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010011 pipe_config->shared_dpll =
10012 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10013 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +020010014
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010015 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10016 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010017
10018 tmp = pipe_config->dpll_hw_state.dpll;
10019 pipe_config->pixel_multiplier =
10020 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10021 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010022
10023 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010024 } else {
10025 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010026 }
10027
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010028 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +020010029 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010030
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010031 ironlake_get_pfit_config(crtc, pipe_config);
10032
Imre Deak17290502016-02-12 18:55:11 +020010033 ret = true;
10034
10035out:
10036 intel_display_power_put(dev_priv, power_domain);
10037
10038 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010039}
10040
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010041static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10042{
Chris Wilson91c8a322016-07-05 10:40:23 +010010043 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010044 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010045
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010046 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -050010047 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010048 pipe_name(crtc->pipe));
10049
Rob Clarke2c719b2014-12-15 13:56:32 -050010050 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10051 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +030010052 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10053 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +030010054 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010055 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010056 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010057 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -050010058 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -030010059 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010060 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010061 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010062 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010063 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010064 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010065
Paulo Zanoni9926ada2014-04-01 19:39:47 -030010066 /*
10067 * In theory we can still leave IRQs enabled, as long as only the HPD
10068 * interrupts remain enabled. We used to check for that, but since it's
10069 * gen-specific and since we only disable LCPLL after we fully disable
10070 * the interrupts, the check below should be enough.
10071 */
Rob Clarke2c719b2014-12-15 13:56:32 -050010072 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010073}
10074
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010075static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10076{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010077 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010078 return I915_READ(D_COMP_HSW);
10079 else
10080 return I915_READ(D_COMP_BDW);
10081}
10082
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010083static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10084{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010085 if (IS_HASWELL(dev_priv)) {
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010086 mutex_lock(&dev_priv->rps.hw_lock);
10087 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10088 val))
Chris Wilson79cf2192016-08-24 11:16:07 +010010089 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010090 mutex_unlock(&dev_priv->rps.hw_lock);
10091 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010092 I915_WRITE(D_COMP_BDW, val);
10093 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010094 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010095}
10096
10097/*
10098 * This function implements pieces of two sequences from BSpec:
10099 * - Sequence for display software to disable LCPLL
10100 * - Sequence for display software to allow package C8+
10101 * The steps implemented here are just the steps that actually touch the LCPLL
10102 * register. Callers should take care of disabling all the display engine
10103 * functions, doing the mode unset, fixing interrupts, etc.
10104 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010105static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10106 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010107{
10108 uint32_t val;
10109
10110 assert_can_disable_lcpll(dev_priv);
10111
10112 val = I915_READ(LCPLL_CTL);
10113
10114 if (switch_to_fclk) {
10115 val |= LCPLL_CD_SOURCE_FCLK;
10116 I915_WRITE(LCPLL_CTL, val);
10117
Imre Deakf53dd632016-06-28 13:37:32 +030010118 if (wait_for_us(I915_READ(LCPLL_CTL) &
10119 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010120 DRM_ERROR("Switching to FCLK failed\n");
10121
10122 val = I915_READ(LCPLL_CTL);
10123 }
10124
10125 val |= LCPLL_PLL_DISABLE;
10126 I915_WRITE(LCPLL_CTL, val);
10127 POSTING_READ(LCPLL_CTL);
10128
Chris Wilson24d84412016-06-30 15:33:07 +010010129 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010130 DRM_ERROR("LCPLL still locked\n");
10131
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010132 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010133 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010134 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010135 ndelay(100);
10136
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010137 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10138 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010139 DRM_ERROR("D_COMP RCOMP still in progress\n");
10140
10141 if (allow_power_down) {
10142 val = I915_READ(LCPLL_CTL);
10143 val |= LCPLL_POWER_DOWN_ALLOW;
10144 I915_WRITE(LCPLL_CTL, val);
10145 POSTING_READ(LCPLL_CTL);
10146 }
10147}
10148
10149/*
10150 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10151 * source.
10152 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010153static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010154{
10155 uint32_t val;
10156
10157 val = I915_READ(LCPLL_CTL);
10158
10159 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10160 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10161 return;
10162
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010163 /*
10164 * Make sure we're not on PC8 state before disabling PC8, otherwise
10165 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010166 */
Mika Kuoppala59bad942015-01-16 11:34:40 +020010167 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -030010168
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010169 if (val & LCPLL_POWER_DOWN_ALLOW) {
10170 val &= ~LCPLL_POWER_DOWN_ALLOW;
10171 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +020010172 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010173 }
10174
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010175 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010176 val |= D_COMP_COMP_FORCE;
10177 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010178 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010179
10180 val = I915_READ(LCPLL_CTL);
10181 val &= ~LCPLL_PLL_DISABLE;
10182 I915_WRITE(LCPLL_CTL, val);
10183
Chris Wilson93220c02016-06-30 15:33:08 +010010184 if (intel_wait_for_register(dev_priv,
10185 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10186 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010187 DRM_ERROR("LCPLL not locked yet\n");
10188
10189 if (val & LCPLL_CD_SOURCE_FCLK) {
10190 val = I915_READ(LCPLL_CTL);
10191 val &= ~LCPLL_CD_SOURCE_FCLK;
10192 I915_WRITE(LCPLL_CTL, val);
10193
Imre Deakf53dd632016-06-28 13:37:32 +030010194 if (wait_for_us((I915_READ(LCPLL_CTL) &
10195 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010196 DRM_ERROR("Switching back to LCPLL failed\n");
10197 }
Paulo Zanoni215733f2013-08-19 13:18:07 -030010198
Mika Kuoppala59bad942015-01-16 11:34:40 +020010199 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjälä4c75b942016-10-31 22:37:12 +020010200 intel_update_cdclk(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010201}
10202
Paulo Zanoni765dab672014-03-07 20:08:18 -030010203/*
10204 * Package states C8 and deeper are really deep PC states that can only be
10205 * reached when all the devices on the system allow it, so even if the graphics
10206 * device allows PC8+, it doesn't mean the system will actually get to these
10207 * states. Our driver only allows PC8+ when going into runtime PM.
10208 *
10209 * The requirements for PC8+ are that all the outputs are disabled, the power
10210 * well is disabled and most interrupts are disabled, and these are also
10211 * requirements for runtime PM. When these conditions are met, we manually do
10212 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10213 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10214 * hang the machine.
10215 *
10216 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10217 * the state of some registers, so when we come back from PC8+ we need to
10218 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10219 * need to take care of the registers kept by RC6. Notice that this happens even
10220 * if we don't put the device in PCI D3 state (which is what currently happens
10221 * because of the runtime PM support).
10222 *
10223 * For more, read "Display Sequences for Package C8" on the hardware
10224 * documentation.
10225 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010226void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010227{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010228 uint32_t val;
10229
Paulo Zanonic67a4702013-08-19 13:18:09 -030010230 DRM_DEBUG_KMS("Enabling package C8+\n");
10231
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010232 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010233 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10234 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10235 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10236 }
10237
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020010238 lpt_disable_clkout_dp(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010239 hsw_disable_lcpll(dev_priv, true, true);
10240}
10241
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010242void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010243{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010244 uint32_t val;
10245
Paulo Zanonic67a4702013-08-19 13:18:09 -030010246 DRM_DEBUG_KMS("Disabling package C8+\n");
10247
10248 hsw_restore_lcpll(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020010249 lpt_init_pch_refclk(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010250
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010251 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010252 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10253 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10254 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10255 }
Paulo Zanonic67a4702013-08-19 13:18:09 -030010256}
10257
Imre Deak324513c2016-06-13 16:44:36 +030010258static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010259{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030010260 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010261 struct intel_atomic_state *old_intel_state =
10262 to_intel_atomic_state(old_state);
10263 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010264
Imre Deak324513c2016-06-13 16:44:36 +030010265 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010266}
10267
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010268static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
10269 int pixel_rate)
10270{
Dhinakaran Pandiyan9c754022016-11-02 13:13:21 -070010271 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
10272
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010273 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Dhinakaran Pandiyan9c754022016-11-02 13:13:21 -070010274 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010275 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10276
10277 /* BSpec says "Do not use DisplayPort with CDCLK less than
10278 * 432 MHz, audio enabled, port width x4, and link rate
10279 * HBR2 (5.4 GHz), or else there may be audio corruption or
10280 * screen corruption."
10281 */
10282 if (intel_crtc_has_dp_encoder(crtc_state) &&
10283 crtc_state->has_audio &&
10284 crtc_state->port_clock >= 540000 &&
10285 crtc_state->lane_count == 4)
10286 pixel_rate = max(432000, pixel_rate);
10287
10288 return pixel_rate;
10289}
10290
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010291/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010292static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010293{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010294 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010010295 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010296 struct drm_crtc *crtc;
10297 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010298 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010299 unsigned max_pixel_rate = 0, i;
10300 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010301
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010302 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10303 sizeof(intel_state->min_pixclk));
10304
10305 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010306 int pixel_rate;
10307
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010308 crtc_state = to_intel_crtc_state(cstate);
10309 if (!crtc_state->base.enable) {
10310 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010311 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010312 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010313
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010314 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010315
Dhinakaran Pandiyan9c754022016-11-02 13:13:21 -070010316 if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
Dhinakaran Pandiyanb30ce9e2016-11-01 11:47:59 -070010317 pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
10318 pixel_rate);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010319
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010320 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010321 }
10322
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010323 for_each_pipe(dev_priv, pipe)
10324 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10325
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010326 return max_pixel_rate;
10327}
10328
10329static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10330{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010331 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010332 uint32_t val, data;
10333 int ret;
10334
10335 if (WARN((I915_READ(LCPLL_CTL) &
10336 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10337 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10338 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10339 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10340 "trying to change cdclk frequency with cdclk not enabled\n"))
10341 return;
10342
10343 mutex_lock(&dev_priv->rps.hw_lock);
10344 ret = sandybridge_pcode_write(dev_priv,
10345 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10346 mutex_unlock(&dev_priv->rps.hw_lock);
10347 if (ret) {
10348 DRM_ERROR("failed to inform pcode about cdclk change\n");
10349 return;
10350 }
10351
10352 val = I915_READ(LCPLL_CTL);
10353 val |= LCPLL_CD_SOURCE_FCLK;
10354 I915_WRITE(LCPLL_CTL, val);
10355
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010356 if (wait_for_us(I915_READ(LCPLL_CTL) &
10357 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010358 DRM_ERROR("Switching to FCLK failed\n");
10359
10360 val = I915_READ(LCPLL_CTL);
10361 val &= ~LCPLL_CLK_FREQ_MASK;
10362
10363 switch (cdclk) {
10364 case 450000:
10365 val |= LCPLL_CLK_FREQ_450;
10366 data = 0;
10367 break;
10368 case 540000:
10369 val |= LCPLL_CLK_FREQ_54O_BDW;
10370 data = 1;
10371 break;
10372 case 337500:
10373 val |= LCPLL_CLK_FREQ_337_5_BDW;
10374 data = 2;
10375 break;
10376 case 675000:
10377 val |= LCPLL_CLK_FREQ_675_BDW;
10378 data = 3;
10379 break;
10380 default:
10381 WARN(1, "invalid cdclk frequency\n");
10382 return;
10383 }
10384
10385 I915_WRITE(LCPLL_CTL, val);
10386
10387 val = I915_READ(LCPLL_CTL);
10388 val &= ~LCPLL_CD_SOURCE_FCLK;
10389 I915_WRITE(LCPLL_CTL, val);
10390
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010391 if (wait_for_us((I915_READ(LCPLL_CTL) &
10392 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010393 DRM_ERROR("Switching back to LCPLL failed\n");
10394
10395 mutex_lock(&dev_priv->rps.hw_lock);
10396 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10397 mutex_unlock(&dev_priv->rps.hw_lock);
10398
Ville Syrjälä7f1052a2016-04-26 19:46:32 +030010399 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10400
Ville Syrjälä4c75b942016-10-31 22:37:12 +020010401 intel_update_cdclk(dev_priv);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010402
10403 WARN(cdclk != dev_priv->cdclk_freq,
10404 "cdclk requested %d kHz but got %d kHz\n",
10405 cdclk, dev_priv->cdclk_freq);
10406}
10407
Ville Syrjälä587c7912016-05-11 22:44:41 +030010408static int broadwell_calc_cdclk(int max_pixclk)
10409{
10410 if (max_pixclk > 540000)
10411 return 675000;
10412 else if (max_pixclk > 450000)
10413 return 540000;
10414 else if (max_pixclk > 337500)
10415 return 450000;
10416 else
10417 return 337500;
10418}
10419
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010420static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010421{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010422 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010423 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010424 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010425 int cdclk;
10426
10427 /*
10428 * FIXME should also account for plane ratio
10429 * once 64bpp pixel formats are supported.
10430 */
Ville Syrjälä587c7912016-05-11 22:44:41 +030010431 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010432
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010433 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +010010434 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10435 cdclk, dev_priv->max_cdclk_freq);
10436 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010437 }
10438
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010439 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10440 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +030010441 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010442
10443 return 0;
10444}
10445
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010446static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010447{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010448 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010449 struct intel_atomic_state *old_intel_state =
10450 to_intel_atomic_state(old_state);
10451 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010452
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010453 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010454}
10455
Clint Taylorc89e39f2016-05-13 23:41:21 +030010456static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10457{
10458 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10459 struct drm_i915_private *dev_priv = to_i915(state->dev);
10460 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010461 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010462 int cdclk;
10463
10464 /*
10465 * FIXME should also account for plane ratio
10466 * once 64bpp pixel formats are supported.
10467 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010468 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010469
10470 /*
10471 * FIXME move the cdclk caclulation to
10472 * compute_config() so we can fail gracegully.
10473 */
10474 if (cdclk > dev_priv->max_cdclk_freq) {
10475 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10476 cdclk, dev_priv->max_cdclk_freq);
10477 cdclk = dev_priv->max_cdclk_freq;
10478 }
10479
10480 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10481 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010482 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010483
10484 return 0;
10485}
10486
10487static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10488{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010489 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10490 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10491 unsigned int req_cdclk = intel_state->dev_cdclk;
10492 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010493
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010494 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010495}
10496
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +020010497static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10498 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010499{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010500 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +020010501 if (!intel_ddi_pll_select(crtc, crtc_state))
10502 return -EINVAL;
10503 }
Daniel Vetter716c2e52014-06-25 22:02:02 +030010504
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +030010505 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +020010506
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +020010507 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010508}
10509
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010510static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10511 enum port port,
10512 struct intel_crtc_state *pipe_config)
10513{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010514 enum intel_dpll_id id;
10515
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010516 switch (port) {
10517 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +020010518 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010519 break;
10520 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +020010521 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010522 break;
10523 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +020010524 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010525 break;
10526 default:
10527 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010528 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010529 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010530
10531 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010532}
10533
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010534static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10535 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010536 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010537{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010538 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +020010539 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010540
10541 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010542 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010543
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010544 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010545 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010546
10547 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010548}
10549
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010550static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10551 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010552 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010553{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010554 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010555 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010556
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010557 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010558 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010559 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010560 break;
10561 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010562 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010563 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010564 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010565 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010566 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010567 case PORT_CLK_SEL_LCPLL_810:
10568 id = DPLL_ID_LCPLL_810;
10569 break;
10570 case PORT_CLK_SEL_LCPLL_1350:
10571 id = DPLL_ID_LCPLL_1350;
10572 break;
10573 case PORT_CLK_SEL_LCPLL_2700:
10574 id = DPLL_ID_LCPLL_2700;
10575 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010576 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010577 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010578 /* fall through */
10579 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010580 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010581 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010582
10583 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010584}
10585
Jani Nikulacf304292016-03-18 17:05:41 +020010586static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10587 struct intel_crtc_state *pipe_config,
10588 unsigned long *power_domain_mask)
10589{
10590 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010591 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +020010592 enum intel_display_power_domain power_domain;
10593 u32 tmp;
10594
Imre Deakd9a7bc62016-05-12 16:18:50 +030010595 /*
10596 * The pipe->transcoder mapping is fixed with the exception of the eDP
10597 * transcoder handled below.
10598 */
Jani Nikulacf304292016-03-18 17:05:41 +020010599 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10600
10601 /*
10602 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10603 * consistency and less surprising code; it's in always on power).
10604 */
10605 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10606 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10607 enum pipe trans_edp_pipe;
10608 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10609 default:
10610 WARN(1, "unknown pipe linked to edp transcoder\n");
10611 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10612 case TRANS_DDI_EDP_INPUT_A_ON:
10613 trans_edp_pipe = PIPE_A;
10614 break;
10615 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10616 trans_edp_pipe = PIPE_B;
10617 break;
10618 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10619 trans_edp_pipe = PIPE_C;
10620 break;
10621 }
10622
10623 if (trans_edp_pipe == crtc->pipe)
10624 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10625 }
10626
10627 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10628 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10629 return false;
10630 *power_domain_mask |= BIT(power_domain);
10631
10632 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10633
10634 return tmp & PIPECONF_ENABLE;
10635}
10636
Jani Nikula4d1de972016-03-18 17:05:42 +020010637static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10638 struct intel_crtc_state *pipe_config,
10639 unsigned long *power_domain_mask)
10640{
10641 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010642 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010643 enum intel_display_power_domain power_domain;
10644 enum port port;
10645 enum transcoder cpu_transcoder;
10646 u32 tmp;
10647
Jani Nikula4d1de972016-03-18 17:05:42 +020010648 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10649 if (port == PORT_A)
10650 cpu_transcoder = TRANSCODER_DSI_A;
10651 else
10652 cpu_transcoder = TRANSCODER_DSI_C;
10653
10654 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10655 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10656 continue;
10657 *power_domain_mask |= BIT(power_domain);
10658
Imre Deakdb18b6a2016-03-24 12:41:40 +020010659 /*
10660 * The PLL needs to be enabled with a valid divider
10661 * configuration, otherwise accessing DSI registers will hang
10662 * the machine. See BSpec North Display Engine
10663 * registers/MIPI[BXT]. We can break out here early, since we
10664 * need the same DSI PLL to be enabled for both DSI ports.
10665 */
10666 if (!intel_dsi_pll_is_enabled(dev_priv))
10667 break;
10668
Jani Nikula4d1de972016-03-18 17:05:42 +020010669 /* XXX: this works for video mode only */
10670 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10671 if (!(tmp & DPI_ENABLE))
10672 continue;
10673
10674 tmp = I915_READ(MIPI_CTRL(port));
10675 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10676 continue;
10677
10678 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010679 break;
10680 }
10681
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010682 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010683}
10684
Daniel Vetter26804af2014-06-25 22:01:55 +030010685static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010686 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010687{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010688 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010689 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010690 enum port port;
10691 uint32_t tmp;
10692
10693 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10694
10695 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10696
Tvrtko Ursulin08537232016-10-13 11:03:02 +010010697 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010698 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020010699 else if (IS_GEN9_LP(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010700 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010701 else
10702 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010703
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010704 pll = pipe_config->shared_dpll;
10705 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010706 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10707 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010708 }
10709
Daniel Vetter26804af2014-06-25 22:01:55 +030010710 /*
10711 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10712 * DDI E. So just check whether this pipe is wired to DDI E and whether
10713 * the PCH transcoder is on.
10714 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010715 if (INTEL_GEN(dev_priv) < 9 &&
Damien Lespiauca370452013-12-03 13:56:24 +000010716 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010717 pipe_config->has_pch_encoder = true;
10718
10719 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10720 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10721 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10722
10723 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10724 }
10725}
10726
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010727static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010728 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010729{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010730 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Imre Deak17290502016-02-12 18:55:11 +020010731 enum intel_display_power_domain power_domain;
10732 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010733 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010734
Imre Deak17290502016-02-12 18:55:11 +020010735 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10736 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010737 return false;
Imre Deak17290502016-02-12 18:55:11 +020010738 power_domain_mask = BIT(power_domain);
10739
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010740 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010741
Jani Nikulacf304292016-03-18 17:05:41 +020010742 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010743
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020010744 if (IS_GEN9_LP(dev_priv) &&
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010745 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10746 WARN_ON(active);
10747 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010748 }
10749
Jani Nikulacf304292016-03-18 17:05:41 +020010750 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010751 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010752
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010753 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010754 haswell_get_ddi_port_state(crtc, pipe_config);
10755 intel_get_pipe_timings(crtc, pipe_config);
10756 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010757
Jani Nikulabc58be62016-03-18 17:05:39 +020010758 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010759
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010760 pipe_config->gamma_mode =
10761 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10762
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010763 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä65edccc2016-10-31 22:37:01 +020010764 skl_init_scalers(dev_priv, crtc, pipe_config);
Chandra Kondurua1b22782015-04-07 15:28:45 -070010765
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010766 pipe_config->scaler_state.scaler_id = -1;
10767 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10768 }
10769
Imre Deak17290502016-02-12 18:55:11 +020010770 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10771 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10772 power_domain_mask |= BIT(power_domain);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000010773 if (INTEL_GEN(dev_priv) >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010774 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010775 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010776 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010777 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010778
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010779 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080010780 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10781 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010782
Jani Nikula4d1de972016-03-18 17:05:42 +020010783 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10784 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010785 pipe_config->pixel_multiplier =
10786 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10787 } else {
10788 pipe_config->pixel_multiplier = 1;
10789 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010790
Imre Deak17290502016-02-12 18:55:11 +020010791out:
10792 for_each_power_domain(power_domain, power_domain_mask)
10793 intel_display_power_put(dev_priv, power_domain);
10794
Jani Nikulacf304292016-03-18 17:05:41 +020010795 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010796}
10797
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010798static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10799 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010800{
10801 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010802 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010804 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010805
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010806 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010807 unsigned int width = plane_state->base.crtc_w;
10808 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010809 unsigned int stride = roundup_pow_of_two(width) * 4;
10810
10811 switch (stride) {
10812 default:
10813 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10814 width, stride);
10815 stride = 256;
10816 /* fallthrough */
10817 case 256:
10818 case 512:
10819 case 1024:
10820 case 2048:
10821 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010822 }
10823
Ville Syrjälädc41c152014-08-13 11:57:05 +030010824 cntl |= CURSOR_ENABLE |
10825 CURSOR_GAMMA_ENABLE |
10826 CURSOR_FORMAT_ARGB |
10827 CURSOR_STRIDE(stride);
10828
10829 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010830 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010831
Ville Syrjälädc41c152014-08-13 11:57:05 +030010832 if (intel_crtc->cursor_cntl != 0 &&
10833 (intel_crtc->cursor_base != base ||
10834 intel_crtc->cursor_size != size ||
10835 intel_crtc->cursor_cntl != cntl)) {
10836 /* On these chipsets we can only modify the base/size/stride
10837 * whilst the cursor is disabled.
10838 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010839 I915_WRITE(CURCNTR(PIPE_A), 0);
10840 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010841 intel_crtc->cursor_cntl = 0;
10842 }
10843
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010844 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010845 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010846 intel_crtc->cursor_base = base;
10847 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010848
10849 if (intel_crtc->cursor_size != size) {
10850 I915_WRITE(CURSIZE, size);
10851 intel_crtc->cursor_size = size;
10852 }
10853
Chris Wilson4b0e3332014-05-30 16:35:26 +030010854 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010855 I915_WRITE(CURCNTR(PIPE_A), cntl);
10856 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010857 intel_crtc->cursor_cntl = cntl;
10858 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010859}
10860
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010861static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10862 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010863{
10864 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010865 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10867 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010868 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010869
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010870 if (plane_state && plane_state->base.visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010871 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010872 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010873 case 64:
10874 cntl |= CURSOR_MODE_64_ARGB_AX;
10875 break;
10876 case 128:
10877 cntl |= CURSOR_MODE_128_ARGB_AX;
10878 break;
10879 case 256:
10880 cntl |= CURSOR_MODE_256_ARGB_AX;
10881 break;
10882 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010883 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010884 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010885 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010886 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010887
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010888 if (HAS_DDI(dev_priv))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010889 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010890
Ville Syrjäläf22aa142016-11-14 18:53:58 +020010891 if (plane_state->base.rotation & DRM_ROTATE_180)
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010892 cntl |= CURSOR_ROTATE_180;
10893 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010894
Chris Wilson4b0e3332014-05-30 16:35:26 +030010895 if (intel_crtc->cursor_cntl != cntl) {
10896 I915_WRITE(CURCNTR(pipe), cntl);
10897 POSTING_READ(CURCNTR(pipe));
10898 intel_crtc->cursor_cntl = cntl;
10899 }
10900
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010901 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010902 I915_WRITE(CURBASE(pipe), base);
10903 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010904
10905 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010906}
10907
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010908/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010909static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010910 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010911{
10912 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010913 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10915 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010916 u32 base = intel_crtc->cursor_addr;
10917 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010918
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010919 if (plane_state) {
10920 int x = plane_state->base.crtc_x;
10921 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010922
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010923 if (x < 0) {
10924 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10925 x = -x;
10926 }
10927 pos |= x << CURSOR_X_SHIFT;
10928
10929 if (y < 0) {
10930 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10931 y = -y;
10932 }
10933 pos |= y << CURSOR_Y_SHIFT;
10934
10935 /* ILK+ do this automagically */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010010936 if (HAS_GMCH_DISPLAY(dev_priv) &&
Ville Syrjäläf22aa142016-11-14 18:53:58 +020010937 plane_state->base.rotation & DRM_ROTATE_180) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010938 base += (plane_state->base.crtc_h *
10939 plane_state->base.crtc_w - 1) * 4;
10940 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010941 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010942
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010943 I915_WRITE(CURPOS(pipe), pos);
10944
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010945 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010946 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010947 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010948 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010949}
10950
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010951static bool cursor_size_ok(struct drm_i915_private *dev_priv,
Ville Syrjälädc41c152014-08-13 11:57:05 +030010952 uint32_t width, uint32_t height)
10953{
10954 if (width == 0 || height == 0)
10955 return false;
10956
10957 /*
10958 * 845g/865g are special in that they are only limited by
10959 * the width of their cursors, the height is arbitrary up to
10960 * the precision of the register. Everything else requires
10961 * square cursors, limited to a few power-of-two sizes.
10962 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010963 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjälädc41c152014-08-13 11:57:05 +030010964 if ((width & 63) != 0)
10965 return false;
10966
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010967 if (width > (IS_845G(dev_priv) ? 64 : 512))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010968 return false;
10969
10970 if (height > 1023)
10971 return false;
10972 } else {
10973 switch (width | height) {
10974 case 256:
10975 case 128:
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010976 if (IS_GEN2(dev_priv))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010977 return false;
10978 case 64:
10979 break;
10980 default:
10981 return false;
10982 }
10983 }
10984
10985 return true;
10986}
10987
Jesse Barnes79e53942008-11-07 14:24:08 -080010988/* VESA 640x480x72Hz mode to set on the pipe */
10989static struct drm_display_mode load_detect_mode = {
10990 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10991 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10992};
10993
Daniel Vettera8bb6812014-02-10 18:00:39 +010010994struct drm_framebuffer *
10995__intel_framebuffer_create(struct drm_device *dev,
10996 struct drm_mode_fb_cmd2 *mode_cmd,
10997 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010998{
10999 struct intel_framebuffer *intel_fb;
11000 int ret;
11001
11002 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011003 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010011004 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010011005
11006 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020011007 if (ret)
11008 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010011009
11010 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020011011
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011012err:
11013 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020011014 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010011015}
11016
Daniel Vetterb5ea6422014-03-02 21:18:00 +010011017static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010011018intel_framebuffer_create(struct drm_device *dev,
11019 struct drm_mode_fb_cmd2 *mode_cmd,
11020 struct drm_i915_gem_object *obj)
11021{
11022 struct drm_framebuffer *fb;
11023 int ret;
11024
11025 ret = i915_mutex_lock_interruptible(dev);
11026 if (ret)
11027 return ERR_PTR(ret);
11028 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11029 mutex_unlock(&dev->struct_mutex);
11030
11031 return fb;
11032}
11033
Chris Wilsond2dff872011-04-19 08:36:26 +010011034static u32
11035intel_framebuffer_pitch_for_width(int width, int bpp)
11036{
11037 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11038 return ALIGN(pitch, 64);
11039}
11040
11041static u32
11042intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11043{
11044 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020011045 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010011046}
11047
11048static struct drm_framebuffer *
11049intel_framebuffer_create_for_mode(struct drm_device *dev,
11050 struct drm_display_mode *mode,
11051 int depth, int bpp)
11052{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011053 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010011054 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000011055 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010011056
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +000011057 obj = i915_gem_object_create(to_i915(dev),
Chris Wilsond2dff872011-04-19 08:36:26 +010011058 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010011059 if (IS_ERR(obj))
11060 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011061
11062 mode_cmd.width = mode->hdisplay;
11063 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011064 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11065 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000011066 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010011067
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011068 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11069 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010011070 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011071
11072 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010011073}
11074
11075static struct drm_framebuffer *
11076mode_fits_in_fbdev(struct drm_device *dev,
11077 struct drm_display_mode *mode)
11078{
Daniel Vetter06957262015-08-10 13:34:08 +020011079#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010011080 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010011081 struct drm_i915_gem_object *obj;
11082 struct drm_framebuffer *fb;
11083
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011084 if (!dev_priv->fbdev)
11085 return NULL;
11086
11087 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010011088 return NULL;
11089
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011090 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011091 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011092
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011093 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011094 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11095 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010011096 return NULL;
11097
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011098 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010011099 return NULL;
11100
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011101 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010011102 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020011103#else
11104 return NULL;
11105#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010011106}
11107
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011108static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11109 struct drm_crtc *crtc,
11110 struct drm_display_mode *mode,
11111 struct drm_framebuffer *fb,
11112 int x, int y)
11113{
11114 struct drm_plane_state *plane_state;
11115 int hdisplay, vdisplay;
11116 int ret;
11117
11118 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11119 if (IS_ERR(plane_state))
11120 return PTR_ERR(plane_state);
11121
11122 if (mode)
11123 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11124 else
11125 hdisplay = vdisplay = 0;
11126
11127 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11128 if (ret)
11129 return ret;
11130 drm_atomic_set_fb_for_plane(plane_state, fb);
11131 plane_state->crtc_x = 0;
11132 plane_state->crtc_y = 0;
11133 plane_state->crtc_w = hdisplay;
11134 plane_state->crtc_h = vdisplay;
11135 plane_state->src_x = x << 16;
11136 plane_state->src_y = y << 16;
11137 plane_state->src_w = hdisplay << 16;
11138 plane_state->src_h = vdisplay << 16;
11139
11140 return 0;
11141}
11142
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011143bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010011144 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050011145 struct intel_load_detect_pipe *old,
11146 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011147{
11148 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011149 struct intel_encoder *intel_encoder =
11150 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080011151 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011152 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080011153 struct drm_crtc *crtc = NULL;
11154 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020011155 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94352cf2012-07-05 22:51:56 +020011156 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050011157 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011158 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011159 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011160 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050011161 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011162
Chris Wilsond2dff872011-04-19 08:36:26 +010011163 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011164 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011165 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011166
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011167 old->restore_state = NULL;
11168
Rob Clark51fd3712013-11-19 12:10:12 -050011169retry:
11170 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11171 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011172 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011173
Jesse Barnes79e53942008-11-07 14:24:08 -080011174 /*
11175 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010011176 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011177 * - if the connector already has an assigned crtc, use it (but make
11178 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010011179 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011180 * - try to find the first unused crtc that can drive this connector,
11181 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080011182 */
11183
11184 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011185 if (connector->state->crtc) {
11186 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010011187
Rob Clark51fd3712013-11-19 12:10:12 -050011188 ret = drm_modeset_lock(&crtc->mutex, ctx);
11189 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011190 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010011191
11192 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011193 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080011194 }
11195
11196 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011197 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080011198 i++;
11199 if (!(encoder->possible_crtcs & (1 << i)))
11200 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011201
11202 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11203 if (ret)
11204 goto fail;
11205
11206 if (possible_crtc->state->enable) {
11207 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030011208 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011209 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030011210
11211 crtc = possible_crtc;
11212 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080011213 }
11214
11215 /*
11216 * If we didn't find an unused CRTC, don't use any.
11217 */
11218 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010011219 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011220 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011221 }
11222
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011223found:
11224 intel_crtc = to_intel_crtc(crtc);
11225
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010011226 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11227 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011228 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011229
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011230 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011231 restore_state = drm_atomic_state_alloc(dev);
11232 if (!state || !restore_state) {
11233 ret = -ENOMEM;
11234 goto fail;
11235 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011236
11237 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011238 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011239
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011240 connector_state = drm_atomic_get_connector_state(state, connector);
11241 if (IS_ERR(connector_state)) {
11242 ret = PTR_ERR(connector_state);
11243 goto fail;
11244 }
11245
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011246 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11247 if (ret)
11248 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011249
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011250 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11251 if (IS_ERR(crtc_state)) {
11252 ret = PTR_ERR(crtc_state);
11253 goto fail;
11254 }
11255
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020011256 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011257
Chris Wilson64927112011-04-20 07:25:26 +010011258 if (!mode)
11259 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080011260
Chris Wilsond2dff872011-04-19 08:36:26 +010011261 /* We need a framebuffer large enough to accommodate all accesses
11262 * that the plane may generate whilst we perform load detection.
11263 * We can not rely on the fbcon either being present (we get called
11264 * during its initialisation to detect all boot displays, or it may
11265 * not even exist) or that it is large enough to satisfy the
11266 * requested mode.
11267 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020011268 fb = mode_fits_in_fbdev(dev, mode);
11269 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011270 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011271 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010011272 } else
11273 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011274 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011275 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011276 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011277 }
Chris Wilsond2dff872011-04-19 08:36:26 +010011278
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011279 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11280 if (ret)
11281 goto fail;
11282
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011283 drm_framebuffer_unreference(fb);
11284
11285 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11286 if (ret)
11287 goto fail;
11288
11289 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11290 if (!ret)
11291 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11292 if (!ret)
11293 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11294 if (ret) {
11295 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11296 goto fail;
11297 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030011298
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010011299 ret = drm_atomic_commit(state);
11300 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010011301 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011302 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011303 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011304
11305 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010011306
Jesse Barnes79e53942008-11-07 14:24:08 -080011307 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020011308 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010011309 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011310
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011311fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010011312 if (state) {
11313 drm_atomic_state_put(state);
11314 state = NULL;
11315 }
11316 if (restore_state) {
11317 drm_atomic_state_put(restore_state);
11318 restore_state = NULL;
11319 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011320
Rob Clark51fd3712013-11-19 12:10:12 -050011321 if (ret == -EDEADLK) {
11322 drm_modeset_backoff(ctx);
11323 goto retry;
11324 }
11325
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011326 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011327}
11328
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011329void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020011330 struct intel_load_detect_pipe *old,
11331 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011332{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011333 struct intel_encoder *intel_encoder =
11334 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010011335 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011336 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011337 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011338
Chris Wilsond2dff872011-04-19 08:36:26 +010011339 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011340 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011341 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011342
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011343 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010011344 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011345
11346 ret = drm_atomic_commit(state);
Chris Wilson08536952016-10-14 13:18:18 +010011347 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011348 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010011349 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080011350}
11351
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011352static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011353 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011354{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011355 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011356 u32 dpll = pipe_config->dpll_hw_state.dpll;
11357
11358 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020011359 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010011360 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011361 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011362 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011363 return 96000;
11364 else
11365 return 48000;
11366}
11367
Jesse Barnes79e53942008-11-07 14:24:08 -080011368/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011369static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011370 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080011371{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011372 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011373 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011374 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030011375 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080011376 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030011377 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030011378 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011379 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080011380
11381 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030011382 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011383 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030011384 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011385
11386 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020011387 if (IS_PINEVIEW(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011388 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11389 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080011390 } else {
11391 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11392 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11393 }
11394
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011395 if (!IS_GEN2(dev_priv)) {
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020011396 if (IS_PINEVIEW(dev_priv))
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011397 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11398 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080011399 else
11400 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080011401 DPLL_FPA01_P1_POST_DIV_SHIFT);
11402
11403 switch (dpll & DPLL_MODE_MASK) {
11404 case DPLLB_MODE_DAC_SERIAL:
11405 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11406 5 : 10;
11407 break;
11408 case DPLLB_MODE_LVDS:
11409 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11410 7 : 14;
11411 break;
11412 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080011413 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080011414 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011415 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080011416 }
11417
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +020011418 if (IS_PINEVIEW(dev_priv))
Imre Deakdccbea32015-06-22 23:35:51 +030011419 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011420 else
Imre Deakdccbea32015-06-22 23:35:51 +030011421 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011422 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010011423 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011424 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080011425
11426 if (is_lvds) {
11427 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11428 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011429
11430 if (lvds & LVDS_CLKB_POWER_UP)
11431 clock.p2 = 7;
11432 else
11433 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080011434 } else {
11435 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11436 clock.p1 = 2;
11437 else {
11438 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11439 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11440 }
11441 if (dpll & PLL_P2_DIVIDE_BY_4)
11442 clock.p2 = 4;
11443 else
11444 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080011445 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011446
Imre Deakdccbea32015-06-22 23:35:51 +030011447 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011448 }
11449
Ville Syrjälä18442d02013-09-13 16:00:08 +030011450 /*
11451 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010011452 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030011453 * encoder's get_config() function.
11454 */
Imre Deakdccbea32015-06-22 23:35:51 +030011455 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011456}
11457
Ville Syrjälä6878da02013-09-13 15:59:11 +030011458int intel_dotclock_calculate(int link_freq,
11459 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011460{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011461 /*
11462 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011463 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011464 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011465 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011466 *
11467 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011468 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080011469 */
11470
Ville Syrjälä6878da02013-09-13 15:59:11 +030011471 if (!m_n->link_n)
11472 return 0;
11473
11474 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11475}
11476
Ville Syrjälä18442d02013-09-13 16:00:08 +030011477static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011478 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030011479{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011480 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011481
11482 /* read out port_clock from the DPLL */
11483 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030011484
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011485 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011486 * In case there is an active pipe without active ports,
11487 * we may need some idea for the dotclock anyway.
11488 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011489 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011490 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011491 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011492 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080011493}
11494
11495/** Returns the currently programmed mode of the given pipe. */
11496struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11497 struct drm_crtc *crtc)
11498{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011499 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011501 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011502 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011503 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020011504 int htot = I915_READ(HTOTAL(cpu_transcoder));
11505 int hsync = I915_READ(HSYNC(cpu_transcoder));
11506 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11507 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030011508 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080011509
11510 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11511 if (!mode)
11512 return NULL;
11513
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011514 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11515 if (!pipe_config) {
11516 kfree(mode);
11517 return NULL;
11518 }
11519
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011520 /*
11521 * Construct a pipe_config sufficient for getting the clock info
11522 * back out of crtc_clock_get.
11523 *
11524 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11525 * to use a real value here instead.
11526 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011527 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11528 pipe_config->pixel_multiplier = 1;
11529 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11530 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11531 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11532 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011533
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011534 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080011535 mode->hdisplay = (htot & 0xffff) + 1;
11536 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11537 mode->hsync_start = (hsync & 0xffff) + 1;
11538 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11539 mode->vdisplay = (vtot & 0xffff) + 1;
11540 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11541 mode->vsync_start = (vsync & 0xffff) + 1;
11542 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11543
11544 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080011545
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011546 kfree(pipe_config);
11547
Jesse Barnes79e53942008-11-07 14:24:08 -080011548 return mode;
11549}
11550
11551static void intel_crtc_destroy(struct drm_crtc *crtc)
11552{
11553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011554 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011555 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011556
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011557 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011558 work = intel_crtc->flip_work;
11559 intel_crtc->flip_work = NULL;
11560 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011561
Daniel Vetter5a21b662016-05-24 17:13:53 +020011562 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011563 cancel_work_sync(&work->mmio_work);
11564 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011565 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011566 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011567
11568 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011569
Jesse Barnes79e53942008-11-07 14:24:08 -080011570 kfree(intel_crtc);
11571}
11572
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011573static void intel_unpin_work_fn(struct work_struct *__work)
11574{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011575 struct intel_flip_work *work =
11576 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011577 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11578 struct drm_device *dev = crtc->base.dev;
11579 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011580
Daniel Vetter5a21b662016-05-24 17:13:53 +020011581 if (is_mmio_work(work))
11582 flush_work(&work->mmio_work);
11583
11584 mutex_lock(&dev->struct_mutex);
11585 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010011586 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011587 mutex_unlock(&dev->struct_mutex);
11588
Chris Wilsone8a261e2016-07-20 13:31:49 +010011589 i915_gem_request_put(work->flip_queued_req);
11590
Chris Wilson5748b6a2016-08-04 16:32:38 +010011591 intel_frontbuffer_flip_complete(to_i915(dev),
11592 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011593 intel_fbc_post_update(crtc);
11594 drm_framebuffer_unreference(work->old_fb);
11595
11596 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11597 atomic_dec(&crtc->unpin_work_count);
11598
11599 kfree(work);
11600}
11601
11602/* Is 'a' after or equal to 'b'? */
11603static bool g4x_flip_count_after_eq(u32 a, u32 b)
11604{
11605 return !((a - b) & 0x80000000);
11606}
11607
11608static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11609 struct intel_flip_work *work)
11610{
11611 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011612 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011613
Chris Wilson8af29b02016-09-09 14:11:47 +010011614 if (abort_flip_on_reset(crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011615 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011616
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011617 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011618 * The relevant registers doen't exist on pre-ctg.
11619 * As the flip done interrupt doesn't trigger for mmio
11620 * flips on gmch platforms, a flip count check isn't
11621 * really needed there. But since ctg has the registers,
11622 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011623 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011624 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011625 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011626
Daniel Vetter5a21b662016-05-24 17:13:53 +020011627 /*
11628 * BDW signals flip done immediately if the plane
11629 * is disabled, even if the plane enable is already
11630 * armed to occur at the next vblank :(
11631 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011632
Daniel Vetter5a21b662016-05-24 17:13:53 +020011633 /*
11634 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11635 * used the same base address. In that case the mmio flip might
11636 * have completed, but the CS hasn't even executed the flip yet.
11637 *
11638 * A flip count check isn't enough as the CS might have updated
11639 * the base address just after start of vblank, but before we
11640 * managed to process the interrupt. This means we'd complete the
11641 * CS flip too soon.
11642 *
11643 * Combining both checks should get us a good enough result. It may
11644 * still happen that the CS flip has been executed, but has not
11645 * yet actually completed. But in case the base address is the same
11646 * anyway, we don't really care.
11647 */
11648 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11649 crtc->flip_work->gtt_offset &&
11650 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11651 crtc->flip_work->flip_count);
11652}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011653
Daniel Vetter5a21b662016-05-24 17:13:53 +020011654static bool
11655__pageflip_finished_mmio(struct intel_crtc *crtc,
11656 struct intel_flip_work *work)
11657{
11658 /*
11659 * MMIO work completes when vblank is different from
11660 * flip_queued_vblank.
11661 *
11662 * Reset counter value doesn't matter, this is handled by
11663 * i915_wait_request finishing early, so no need to handle
11664 * reset here.
11665 */
11666 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011667}
11668
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011669
11670static bool pageflip_finished(struct intel_crtc *crtc,
11671 struct intel_flip_work *work)
11672{
11673 if (!atomic_read(&work->pending))
11674 return false;
11675
11676 smp_rmb();
11677
Daniel Vetter5a21b662016-05-24 17:13:53 +020011678 if (is_mmio_work(work))
11679 return __pageflip_finished_mmio(crtc, work);
11680 else
11681 return __pageflip_finished_cs(crtc, work);
11682}
11683
11684void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11685{
Chris Wilson91c8a322016-07-05 10:40:23 +010011686 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020011687 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011688 struct intel_flip_work *work;
11689 unsigned long flags;
11690
11691 /* Ignore early vblank irqs */
11692 if (!crtc)
11693 return;
11694
Daniel Vetterf3260382014-09-15 14:55:23 +020011695 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011696 * This is called both by irq handlers and the reset code (to complete
11697 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011698 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011699 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011700 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011701
11702 if (work != NULL &&
11703 !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011704 pageflip_finished(crtc, work))
11705 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011706
11707 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011708}
11709
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011710void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011711{
Chris Wilson91c8a322016-07-05 10:40:23 +010011712 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020011713 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011714 struct intel_flip_work *work;
11715 unsigned long flags;
11716
11717 /* Ignore early vblank irqs */
11718 if (!crtc)
11719 return;
11720
11721 /*
11722 * This is called both by irq handlers and the reset code (to complete
11723 * lost pageflips) so needs the full irqsave spinlocks.
11724 */
11725 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011726 work = crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011727
Daniel Vetter5a21b662016-05-24 17:13:53 +020011728 if (work != NULL &&
11729 is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011730 pageflip_finished(crtc, work))
11731 page_flip_completed(crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011732
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011733 spin_unlock_irqrestore(&dev->event_lock, flags);
11734}
11735
Daniel Vetter5a21b662016-05-24 17:13:53 +020011736static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11737 struct intel_flip_work *work)
11738{
11739 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11740
11741 /* Ensure that the work item is consistent when activating it ... */
11742 smp_mb__before_atomic();
11743 atomic_set(&work->pending, 1);
11744}
11745
11746static int intel_gen2_queue_flip(struct drm_device *dev,
11747 struct drm_crtc *crtc,
11748 struct drm_framebuffer *fb,
11749 struct drm_i915_gem_object *obj,
11750 struct drm_i915_gem_request *req,
11751 uint32_t flags)
11752{
Chris Wilson7e37f882016-08-02 22:50:21 +010011753 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11755 u32 flip_mask;
11756 int ret;
11757
11758 ret = intel_ring_begin(req, 6);
11759 if (ret)
11760 return ret;
11761
11762 /* Can't queue multiple flips, so wait for the previous
11763 * one to finish before executing the next.
11764 */
11765 if (intel_crtc->plane)
11766 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11767 else
11768 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011769 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11770 intel_ring_emit(ring, MI_NOOP);
11771 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011772 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011773 intel_ring_emit(ring, fb->pitches[0]);
11774 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11775 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011776
11777 return 0;
11778}
11779
11780static int intel_gen3_queue_flip(struct drm_device *dev,
11781 struct drm_crtc *crtc,
11782 struct drm_framebuffer *fb,
11783 struct drm_i915_gem_object *obj,
11784 struct drm_i915_gem_request *req,
11785 uint32_t flags)
11786{
Chris Wilson7e37f882016-08-02 22:50:21 +010011787 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11789 u32 flip_mask;
11790 int ret;
11791
11792 ret = intel_ring_begin(req, 6);
11793 if (ret)
11794 return ret;
11795
11796 if (intel_crtc->plane)
11797 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11798 else
11799 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011800 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11801 intel_ring_emit(ring, MI_NOOP);
11802 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011803 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011804 intel_ring_emit(ring, fb->pitches[0]);
11805 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11806 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011807
11808 return 0;
11809}
11810
11811static int intel_gen4_queue_flip(struct drm_device *dev,
11812 struct drm_crtc *crtc,
11813 struct drm_framebuffer *fb,
11814 struct drm_i915_gem_object *obj,
11815 struct drm_i915_gem_request *req,
11816 uint32_t flags)
11817{
Chris Wilson7e37f882016-08-02 22:50:21 +010011818 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011819 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11821 uint32_t pf, pipesrc;
11822 int ret;
11823
11824 ret = intel_ring_begin(req, 4);
11825 if (ret)
11826 return ret;
11827
11828 /* i965+ uses the linear or tiled offsets from the
11829 * Display Registers (which do not change across a page-flip)
11830 * so we need only reprogram the base address.
11831 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011832 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011833 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011834 intel_ring_emit(ring, fb->pitches[0]);
11835 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Ville Syrjäläbae781b2016-11-16 13:33:16 +020011836 intel_fb_modifier_to_tiling(fb->modifier));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011837
11838 /* XXX Enabling the panel-fitter across page-flip is so far
11839 * untested on non-native modes, so ignore it for now.
11840 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11841 */
11842 pf = 0;
11843 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011844 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011845
11846 return 0;
11847}
11848
11849static int intel_gen6_queue_flip(struct drm_device *dev,
11850 struct drm_crtc *crtc,
11851 struct drm_framebuffer *fb,
11852 struct drm_i915_gem_object *obj,
11853 struct drm_i915_gem_request *req,
11854 uint32_t flags)
11855{
Chris Wilson7e37f882016-08-02 22:50:21 +010011856 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011857 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11859 uint32_t pf, pipesrc;
11860 int ret;
11861
11862 ret = intel_ring_begin(req, 4);
11863 if (ret)
11864 return ret;
11865
Chris Wilsonb5321f32016-08-02 22:50:18 +010011866 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011867 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011868 intel_ring_emit(ring, fb->pitches[0] |
Ville Syrjäläbae781b2016-11-16 13:33:16 +020011869 intel_fb_modifier_to_tiling(fb->modifier));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011870 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011871
11872 /* Contrary to the suggestions in the documentation,
11873 * "Enable Panel Fitter" does not seem to be required when page
11874 * flipping with a non-native mode, and worse causes a normal
11875 * modeset to fail.
11876 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11877 */
11878 pf = 0;
11879 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011880 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011881
11882 return 0;
11883}
11884
11885static int intel_gen7_queue_flip(struct drm_device *dev,
11886 struct drm_crtc *crtc,
11887 struct drm_framebuffer *fb,
11888 struct drm_i915_gem_object *obj,
11889 struct drm_i915_gem_request *req,
11890 uint32_t flags)
11891{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011892 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson7e37f882016-08-02 22:50:21 +010011893 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11895 uint32_t plane_bit = 0;
11896 int len, ret;
11897
11898 switch (intel_crtc->plane) {
11899 case PLANE_A:
11900 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11901 break;
11902 case PLANE_B:
11903 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11904 break;
11905 case PLANE_C:
11906 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11907 break;
11908 default:
11909 WARN_ONCE(1, "unknown plane in flip command\n");
11910 return -ENODEV;
11911 }
11912
11913 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011914 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011915 len += 6;
11916 /*
11917 * On Gen 8, SRM is now taking an extra dword to accommodate
11918 * 48bits addresses, and we need a NOOP for the batch size to
11919 * stay even.
11920 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011921 if (IS_GEN8(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011922 len += 2;
11923 }
11924
11925 /*
11926 * BSpec MI_DISPLAY_FLIP for IVB:
11927 * "The full packet must be contained within the same cache line."
11928 *
11929 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11930 * cacheline, if we ever start emitting more commands before
11931 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11932 * then do the cacheline alignment, and finally emit the
11933 * MI_DISPLAY_FLIP.
11934 */
11935 ret = intel_ring_cacheline_align(req);
11936 if (ret)
11937 return ret;
11938
11939 ret = intel_ring_begin(req, len);
11940 if (ret)
11941 return ret;
11942
11943 /* Unmask the flip-done completion message. Note that the bspec says that
11944 * we should do this for both the BCS and RCS, and that we must not unmask
11945 * more than one flip event at any time (or ensure that one flip message
11946 * can be sent by waiting for flip-done prior to queueing new flips).
11947 * Experimentation says that BCS works despite DERRMR masking all
11948 * flip-done completion events and that unmasking all planes at once
11949 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11950 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11951 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011952 if (req->engine->id == RCS) {
11953 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11954 intel_ring_emit_reg(ring, DERRMR);
11955 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011956 DERRMR_PIPEB_PRI_FLIP_DONE |
11957 DERRMR_PIPEC_PRI_FLIP_DONE));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011958 if (IS_GEN8(dev_priv))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011959 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011960 MI_SRM_LRM_GLOBAL_GTT);
11961 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011962 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011963 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011964 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonbde13eb2016-08-15 10:49:07 +010011965 intel_ring_emit(ring,
11966 i915_ggtt_offset(req->engine->scratch) + 256);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011967 if (IS_GEN8(dev_priv)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011968 intel_ring_emit(ring, 0);
11969 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011970 }
11971 }
11972
Chris Wilsonb5321f32016-08-02 22:50:18 +010011973 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011974 intel_ring_emit(ring, fb->pitches[0] |
Ville Syrjäläbae781b2016-11-16 13:33:16 +020011975 intel_fb_modifier_to_tiling(fb->modifier));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011976 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11977 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011978
11979 return 0;
11980}
11981
11982static bool use_mmio_flip(struct intel_engine_cs *engine,
11983 struct drm_i915_gem_object *obj)
11984{
11985 /*
11986 * This is not being used for older platforms, because
11987 * non-availability of flip done interrupt forces us to use
11988 * CS flips. Older platforms derive flip done using some clever
11989 * tricks involving the flip_pending status bits and vblank irqs.
11990 * So using MMIO flips there would disrupt this mechanism.
11991 */
11992
11993 if (engine == NULL)
11994 return true;
11995
11996 if (INTEL_GEN(engine->i915) < 5)
11997 return false;
11998
11999 if (i915.use_mmio_flip < 0)
12000 return false;
12001 else if (i915.use_mmio_flip > 0)
12002 return true;
12003 else if (i915.enable_execlists)
12004 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010012005
Chris Wilsond07f0e52016-10-28 13:58:44 +010012006 return engine != i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012007}
12008
12009static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
12010 unsigned int rotation,
12011 struct intel_flip_work *work)
12012{
12013 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012014 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012015 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
12016 const enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +020012017 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012018
12019 ctl = I915_READ(PLANE_CTL(pipe, 0));
12020 ctl &= ~PLANE_CTL_TILED_MASK;
Ville Syrjäläbae781b2016-11-16 13:33:16 +020012021 switch (fb->modifier) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012022 case DRM_FORMAT_MOD_NONE:
12023 break;
12024 case I915_FORMAT_MOD_X_TILED:
12025 ctl |= PLANE_CTL_TILED_X;
12026 break;
12027 case I915_FORMAT_MOD_Y_TILED:
12028 ctl |= PLANE_CTL_TILED_Y;
12029 break;
12030 case I915_FORMAT_MOD_Yf_TILED:
12031 ctl |= PLANE_CTL_TILED_YF;
12032 break;
12033 default:
Ville Syrjäläbae781b2016-11-16 13:33:16 +020012034 MISSING_CASE(fb->modifier);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012035 }
12036
12037 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020012038 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12039 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12040 */
12041 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12042 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12043
12044 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12045 POSTING_READ(PLANE_SURF(pipe, 0));
12046}
12047
12048static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12049 struct intel_flip_work *work)
12050{
12051 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012052 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012053 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012054 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12055 u32 dspcntr;
12056
12057 dspcntr = I915_READ(reg);
12058
Ville Syrjäläbae781b2016-11-16 13:33:16 +020012059 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012060 dspcntr |= DISPPLANE_TILED;
12061 else
12062 dspcntr &= ~DISPPLANE_TILED;
12063
12064 I915_WRITE(reg, dspcntr);
12065
12066 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12067 POSTING_READ(DSPSURF(intel_crtc->plane));
12068}
12069
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012070static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000012071{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012072 struct intel_flip_work *work =
12073 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012074 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12076 struct intel_framebuffer *intel_fb =
12077 to_intel_framebuffer(crtc->base.primary->fb);
12078 struct drm_i915_gem_object *obj = intel_fb->obj;
12079
Chris Wilsond07f0e52016-10-28 13:58:44 +010012080 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012081
12082 intel_pipe_update_start(crtc);
12083
12084 if (INTEL_GEN(dev_priv) >= 9)
12085 skl_do_mmio_flip(crtc, work->rotation, work);
12086 else
12087 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12088 ilk_do_mmio_flip(crtc, work);
12089
12090 intel_pipe_update_end(crtc, work);
12091}
12092
12093static int intel_default_queue_flip(struct drm_device *dev,
12094 struct drm_crtc *crtc,
12095 struct drm_framebuffer *fb,
12096 struct drm_i915_gem_object *obj,
12097 struct drm_i915_gem_request *req,
12098 uint32_t flags)
12099{
12100 return -ENODEV;
12101}
12102
12103static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12104 struct intel_crtc *intel_crtc,
12105 struct intel_flip_work *work)
12106{
12107 u32 addr, vblank;
12108
12109 if (!atomic_read(&work->pending))
12110 return false;
12111
12112 smp_rmb();
12113
12114 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12115 if (work->flip_ready_vblank == 0) {
12116 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010012117 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012118 return false;
12119
12120 work->flip_ready_vblank = vblank;
12121 }
12122
12123 if (vblank - work->flip_ready_vblank < 3)
12124 return false;
12125
12126 /* Potential stall - if we see that the flip has happened,
12127 * assume a missed interrupt. */
12128 if (INTEL_GEN(dev_priv) >= 4)
12129 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12130 else
12131 addr = I915_READ(DSPADDR(intel_crtc->plane));
12132
12133 /* There is a potential issue here with a false positive after a flip
12134 * to the same address. We could address this by checking for a
12135 * non-incrementing frame counter.
12136 */
12137 return addr == work->gtt_offset;
12138}
12139
12140void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12141{
Chris Wilson91c8a322016-07-05 10:40:23 +010012142 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020012143 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012144 struct intel_flip_work *work;
12145
12146 WARN_ON(!in_interrupt());
12147
12148 if (crtc == NULL)
12149 return;
12150
12151 spin_lock(&dev->event_lock);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012152 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012153
12154 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012155 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012156 WARN_ONCE(1,
12157 "Kicking stuck page flip: queued at %d, now %d\n",
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012158 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
12159 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012160 work = NULL;
12161 }
12162
12163 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012164 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012165 intel_queue_rps_boost_for_request(work->flip_queued_req);
12166 spin_unlock(&dev->event_lock);
12167}
12168
12169static int intel_crtc_page_flip(struct drm_crtc *crtc,
12170 struct drm_framebuffer *fb,
12171 struct drm_pending_vblank_event *event,
12172 uint32_t page_flip_flags)
12173{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012174 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012175 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012176 struct drm_framebuffer *old_fb = crtc->primary->fb;
12177 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12179 struct drm_plane *primary = crtc->primary;
12180 enum pipe pipe = intel_crtc->pipe;
12181 struct intel_flip_work *work;
12182 struct intel_engine_cs *engine;
12183 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010012184 struct drm_i915_gem_request *request;
Chris Wilson058d88c2016-08-15 10:49:06 +010012185 struct i915_vma *vma;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012186 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053012187
Daniel Vetter5a21b662016-05-24 17:13:53 +020012188 /*
12189 * drm_mode_page_flip_ioctl() should already catch this, but double
12190 * check to be safe. In the future we may enable pageflipping from
12191 * a disabled primary plane.
12192 */
12193 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12194 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012195
Daniel Vetter5a21b662016-05-24 17:13:53 +020012196 /* Can't change pixel format via MI display flips. */
12197 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12198 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012199
Daniel Vetter5a21b662016-05-24 17:13:53 +020012200 /*
12201 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12202 * Note that pitch changes could also affect these register.
12203 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012204 if (INTEL_GEN(dev_priv) > 3 &&
Daniel Vetter5a21b662016-05-24 17:13:53 +020012205 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12206 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12207 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012208
Daniel Vetter5a21b662016-05-24 17:13:53 +020012209 if (i915_terminally_wedged(&dev_priv->gpu_error))
12210 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012211
Daniel Vetter5a21b662016-05-24 17:13:53 +020012212 work = kzalloc(sizeof(*work), GFP_KERNEL);
12213 if (work == NULL)
12214 return -ENOMEM;
12215
12216 work->event = event;
12217 work->crtc = crtc;
12218 work->old_fb = old_fb;
12219 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053012220
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012221 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012222 if (ret)
12223 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012224
Daniel Vetter5a21b662016-05-24 17:13:53 +020012225 /* We borrow the event spin lock for protecting flip_work */
12226 spin_lock_irq(&dev->event_lock);
12227 if (intel_crtc->flip_work) {
12228 /* Before declaring the flip queue wedged, check if
12229 * the hardware completed the operation behind our backs.
12230 */
12231 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12232 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12233 page_flip_completed(intel_crtc);
12234 } else {
12235 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12236 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012237
Daniel Vetter5a21b662016-05-24 17:13:53 +020012238 drm_crtc_vblank_put(crtc);
12239 kfree(work);
12240 return -EBUSY;
12241 }
12242 }
12243 intel_crtc->flip_work = work;
12244 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080012245
Daniel Vetter5a21b662016-05-24 17:13:53 +020012246 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12247 flush_workqueue(dev_priv->wq);
12248
12249 /* Reference the objects for the scheduled work. */
12250 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012251
12252 crtc->primary->fb = fb;
12253 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020012254
Chris Wilson25dc5562016-07-20 13:31:52 +010012255 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012256
12257 ret = i915_mutex_lock_interruptible(dev);
12258 if (ret)
12259 goto cleanup;
12260
Chris Wilson8af29b02016-09-09 14:11:47 +010012261 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12262 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012263 ret = -EIO;
Matthew Auldddbb2712016-11-28 10:36:48 +000012264 goto unlock;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012265 }
12266
12267 atomic_inc(&intel_crtc->unpin_work_count);
12268
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012269 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012270 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12271
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010012272 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012273 engine = dev_priv->engine[BCS];
Ville Syrjäläbae781b2016-11-16 13:33:16 +020012274 if (fb->modifier != old_fb->modifier)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012275 /* vlv: DISPLAY_FLIP fails to change tiling */
12276 engine = NULL;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012277 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012278 engine = dev_priv->engine[BCS];
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012279 } else if (INTEL_GEN(dev_priv) >= 7) {
Chris Wilsond07f0e52016-10-28 13:58:44 +010012280 engine = i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012281 if (engine == NULL || engine->id != RCS)
Akash Goel3b3f1652016-10-13 22:44:48 +053012282 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012283 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +053012284 engine = dev_priv->engine[RCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012285 }
12286
12287 mmio_flip = use_mmio_flip(engine, obj);
12288
Chris Wilson058d88c2016-08-15 10:49:06 +010012289 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12290 if (IS_ERR(vma)) {
12291 ret = PTR_ERR(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012292 goto cleanup_pending;
Chris Wilson058d88c2016-08-15 10:49:06 +010012293 }
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012294
Ville Syrjälä6687c902015-09-15 13:16:41 +030012295 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012296 work->gtt_offset += intel_crtc->dspaddr_offset;
12297 work->rotation = crtc->primary->state->rotation;
12298
Paulo Zanoni1f0613162016-08-17 16:41:44 -030012299 /*
12300 * There's the potential that the next frame will not be compatible with
12301 * FBC, so we want to call pre_update() before the actual page flip.
12302 * The problem is that pre_update() caches some information about the fb
12303 * object, so we want to do this only after the object is pinned. Let's
12304 * be on the safe side and do this immediately before scheduling the
12305 * flip.
12306 */
12307 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12308 to_intel_plane_state(primary->state));
12309
Daniel Vetter5a21b662016-05-24 17:13:53 +020012310 if (mmio_flip) {
12311 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
Imre Deak6277c8d2016-09-20 14:58:19 +030012312 queue_work(system_unbound_wq, &work->mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012313 } else {
Chris Wilson8e637172016-08-02 22:50:26 +010012314 request = i915_gem_request_alloc(engine, engine->last_context);
12315 if (IS_ERR(request)) {
12316 ret = PTR_ERR(request);
12317 goto cleanup_unpin;
12318 }
12319
Chris Wilsona2bc4692016-09-09 14:11:56 +010012320 ret = i915_gem_request_await_object(request, obj, false);
Chris Wilson8e637172016-08-02 22:50:26 +010012321 if (ret)
12322 goto cleanup_request;
12323
Daniel Vetter5a21b662016-05-24 17:13:53 +020012324 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12325 page_flip_flags);
12326 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010012327 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012328
12329 intel_mark_page_flip_active(intel_crtc, work);
12330
Chris Wilson8e637172016-08-02 22:50:26 +010012331 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012332 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012333 }
12334
Chris Wilson92117f02016-11-28 14:36:48 +000012335 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012336 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12337 to_intel_plane(primary)->frontbuffer_bit);
12338 mutex_unlock(&dev->struct_mutex);
12339
Chris Wilson5748b6a2016-08-04 16:32:38 +010012340 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020012341 to_intel_plane(primary)->frontbuffer_bit);
12342
12343 trace_i915_flip_request(intel_crtc->plane, obj);
12344
12345 return 0;
12346
Chris Wilson8e637172016-08-02 22:50:26 +010012347cleanup_request:
12348 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012349cleanup_unpin:
12350 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12351cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020012352 atomic_dec(&intel_crtc->unpin_work_count);
Matthew Auldddbb2712016-11-28 10:36:48 +000012353unlock:
Daniel Vetter5a21b662016-05-24 17:13:53 +020012354 mutex_unlock(&dev->struct_mutex);
12355cleanup:
12356 crtc->primary->fb = old_fb;
12357 update_state_fb(crtc->primary);
12358
Chris Wilsonf0cd5182016-10-28 13:58:43 +010012359 i915_gem_object_put(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012360 drm_framebuffer_unreference(work->old_fb);
12361
12362 spin_lock_irq(&dev->event_lock);
12363 intel_crtc->flip_work = NULL;
12364 spin_unlock_irq(&dev->event_lock);
12365
12366 drm_crtc_vblank_put(crtc);
12367free_work:
12368 kfree(work);
12369
12370 if (ret == -EIO) {
12371 struct drm_atomic_state *state;
12372 struct drm_plane_state *plane_state;
12373
12374out_hang:
12375 state = drm_atomic_state_alloc(dev);
12376 if (!state)
12377 return -ENOMEM;
12378 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12379
12380retry:
12381 plane_state = drm_atomic_get_plane_state(state, primary);
12382 ret = PTR_ERR_OR_ZERO(plane_state);
12383 if (!ret) {
12384 drm_atomic_set_fb_for_plane(plane_state, fb);
12385
12386 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12387 if (!ret)
12388 ret = drm_atomic_commit(state);
12389 }
12390
12391 if (ret == -EDEADLK) {
12392 drm_modeset_backoff(state->acquire_ctx);
12393 drm_atomic_state_clear(state);
12394 goto retry;
12395 }
12396
Chris Wilson08536952016-10-14 13:18:18 +010012397 drm_atomic_state_put(state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012398
12399 if (ret == 0 && event) {
12400 spin_lock_irq(&dev->event_lock);
12401 drm_crtc_send_vblank_event(crtc, event);
12402 spin_unlock_irq(&dev->event_lock);
12403 }
12404 }
12405 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012406}
12407
Daniel Vetter5a21b662016-05-24 17:13:53 +020012408
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012409/**
12410 * intel_wm_need_update - Check whether watermarks need updating
12411 * @plane: drm plane
12412 * @state: new plane state
12413 *
12414 * Check current plane state versus the new one to determine whether
12415 * watermarks need to be recalculated.
12416 *
12417 * Returns true or false.
12418 */
12419static bool intel_wm_need_update(struct drm_plane *plane,
12420 struct drm_plane_state *state)
12421{
Matt Roperd21fbe82015-09-24 15:53:12 -070012422 struct intel_plane_state *new = to_intel_plane_state(state);
12423 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12424
12425 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012426 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012427 return true;
12428
12429 if (!cur->base.fb || !new->base.fb)
12430 return false;
12431
Ville Syrjäläbae781b2016-11-16 13:33:16 +020012432 if (cur->base.fb->modifier != new->base.fb->modifier ||
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012433 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012434 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12435 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12436 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12437 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012438 return true;
12439
12440 return false;
12441}
12442
Matt Roperd21fbe82015-09-24 15:53:12 -070012443static bool needs_scaling(struct intel_plane_state *state)
12444{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012445 int src_w = drm_rect_width(&state->base.src) >> 16;
12446 int src_h = drm_rect_height(&state->base.src) >> 16;
12447 int dst_w = drm_rect_width(&state->base.dst);
12448 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070012449
12450 return (src_w != dst_w || src_h != dst_h);
12451}
12452
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012453int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12454 struct drm_plane_state *plane_state)
12455{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012456 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012457 struct drm_crtc *crtc = crtc_state->crtc;
12458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12459 struct drm_plane *plane = plane_state->plane;
12460 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080012461 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012462 struct intel_plane_state *old_plane_state =
12463 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012464 bool mode_changed = needs_modeset(crtc_state);
12465 bool was_crtc_enabled = crtc->state->active;
12466 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012467 bool turn_off, turn_on, visible, was_visible;
12468 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030012469 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012470
Tvrtko Ursulin55b8f2a2016-10-14 09:17:22 +010012471 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012472 ret = skl_update_scaler_plane(
12473 to_intel_crtc_state(crtc_state),
12474 to_intel_plane_state(plane_state));
12475 if (ret)
12476 return ret;
12477 }
12478
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012479 was_visible = old_plane_state->base.visible;
12480 visible = to_intel_plane_state(plane_state)->base.visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012481
12482 if (!was_crtc_enabled && WARN_ON(was_visible))
12483 was_visible = false;
12484
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012485 /*
12486 * Visibility is calculated as if the crtc was on, but
12487 * after scaler setup everything depends on it being off
12488 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030012489 *
12490 * FIXME this is wrong for watermarks. Watermarks should also
12491 * be computed as if the pipe would be active. Perhaps move
12492 * per-plane wm computation to the .check_plane() hook, and
12493 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012494 */
12495 if (!is_crtc_enabled)
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012496 to_intel_plane_state(plane_state)->base.visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012497
12498 if (!was_visible && !visible)
12499 return 0;
12500
Maarten Lankhorste8861672016-02-24 11:24:26 +010012501 if (fb != old_plane_state->base.fb)
12502 pipe_config->fb_changed = true;
12503
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012504 turn_off = was_visible && (!visible || mode_changed);
12505 turn_on = visible && (!was_visible || mode_changed);
12506
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012507 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030012508 intel_crtc->base.base.id,
12509 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012510 plane->base.id, plane->name,
12511 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012512
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012513 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12514 plane->base.id, plane->name,
12515 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012516 turn_off, turn_on, mode_changed);
12517
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012518 if (turn_on) {
12519 pipe_config->update_wm_pre = true;
12520
12521 /* must disable cxsr around plane enable/disable */
12522 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12523 pipe_config->disable_cxsr = true;
12524 } else if (turn_off) {
12525 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012526
Ville Syrjälä852eb002015-06-24 22:00:07 +030012527 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012528 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012529 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012530 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012531 /* FIXME bollocks */
12532 pipe_config->update_wm_pre = true;
12533 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012534 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012535
Matt Ropered4a6a72016-02-23 17:20:13 -080012536 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012537 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012538 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012539 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12540
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012541 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012542 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012543
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012544 /*
12545 * WaCxSRDisabledForSpriteScaling:ivb
12546 *
12547 * cstate->update_wm was already set above, so this flag will
12548 * take effect when we commit and program watermarks.
12549 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012550 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012551 needs_scaling(to_intel_plane_state(plane_state)) &&
12552 !needs_scaling(old_plane_state))
12553 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012554
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012555 return 0;
12556}
12557
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012558static bool encoders_cloneable(const struct intel_encoder *a,
12559 const struct intel_encoder *b)
12560{
12561 /* masks could be asymmetric, so check both ways */
12562 return a == b || (a->cloneable & (1 << b->type) &&
12563 b->cloneable & (1 << a->type));
12564}
12565
12566static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12567 struct intel_crtc *crtc,
12568 struct intel_encoder *encoder)
12569{
12570 struct intel_encoder *source_encoder;
12571 struct drm_connector *connector;
12572 struct drm_connector_state *connector_state;
12573 int i;
12574
12575 for_each_connector_in_state(state, connector, connector_state, i) {
12576 if (connector_state->crtc != &crtc->base)
12577 continue;
12578
12579 source_encoder =
12580 to_intel_encoder(connector_state->best_encoder);
12581 if (!encoders_cloneable(encoder, source_encoder))
12582 return false;
12583 }
12584
12585 return true;
12586}
12587
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012588static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12589 struct drm_crtc_state *crtc_state)
12590{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012591 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012592 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012593 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012594 struct intel_crtc_state *pipe_config =
12595 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012596 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012597 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012598 bool mode_changed = needs_modeset(crtc_state);
12599
Ville Syrjälä852eb002015-06-24 22:00:07 +030012600 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012601 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012602
Maarten Lankhorstad421372015-06-15 12:33:42 +020012603 if (mode_changed && crtc_state->enable &&
12604 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012605 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012606 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12607 pipe_config);
12608 if (ret)
12609 return ret;
12610 }
12611
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012612 if (crtc_state->color_mgmt_changed) {
12613 ret = intel_color_check(crtc, crtc_state);
12614 if (ret)
12615 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012616
12617 /*
12618 * Changing color management on Intel hardware is
12619 * handled as part of planes update.
12620 */
12621 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012622 }
12623
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012624 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012625 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012626 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012627 if (ret) {
12628 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012629 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012630 }
12631 }
12632
12633 if (dev_priv->display.compute_intermediate_wm &&
12634 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12635 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12636 return 0;
12637
12638 /*
12639 * Calculate 'intermediate' watermarks that satisfy both the
12640 * old state and the new state. We can program these
12641 * immediately.
12642 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012643 ret = dev_priv->display.compute_intermediate_wm(dev,
Matt Ropered4a6a72016-02-23 17:20:13 -080012644 intel_crtc,
12645 pipe_config);
12646 if (ret) {
12647 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12648 return ret;
12649 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012650 } else if (dev_priv->display.compute_intermediate_wm) {
12651 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12652 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012653 }
12654
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000012655 if (INTEL_GEN(dev_priv) >= 9) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012656 if (mode_changed)
12657 ret = skl_update_scaler_crtc(pipe_config);
12658
12659 if (!ret)
12660 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12661 pipe_config);
12662 }
12663
12664 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012665}
12666
Jani Nikula65b38e02015-04-13 11:26:56 +030012667static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012668 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012669 .atomic_begin = intel_begin_crtc_commit,
12670 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012671 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012672};
12673
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012674static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12675{
12676 struct intel_connector *connector;
12677
12678 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012679 if (connector->base.state->crtc)
12680 drm_connector_unreference(&connector->base);
12681
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012682 if (connector->base.encoder) {
12683 connector->base.state->best_encoder =
12684 connector->base.encoder;
12685 connector->base.state->crtc =
12686 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012687
12688 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012689 } else {
12690 connector->base.state->best_encoder = NULL;
12691 connector->base.state->crtc = NULL;
12692 }
12693 }
12694}
12695
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012696static void
Robin Schroereba905b2014-05-18 02:24:50 +020012697connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012698 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012699{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012700 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012701 int bpp = pipe_config->pipe_bpp;
12702
12703 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012704 connector->base.base.id,
12705 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012706
12707 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012708 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012709 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012710 bpp, info->bpc * 3);
12711 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012712 }
12713
Mario Kleiner196f9542016-07-06 12:05:45 +020012714 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012715 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020012716 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12717 bpp);
12718 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012719 }
12720}
12721
12722static int
12723compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012724 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012725{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012726 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012727 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012728 struct drm_connector *connector;
12729 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012730 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012731
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012732 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12733 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012734 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012735 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012736 bpp = 12*3;
12737 else
12738 bpp = 8*3;
12739
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012740
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012741 pipe_config->pipe_bpp = bpp;
12742
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012743 state = pipe_config->base.state;
12744
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012745 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012746 for_each_connector_in_state(state, connector, connector_state, i) {
12747 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012748 continue;
12749
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012750 connected_sink_compute_bpp(to_intel_connector(connector),
12751 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012752 }
12753
12754 return bpp;
12755}
12756
Daniel Vetter644db712013-09-19 14:53:58 +020012757static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12758{
12759 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12760 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012761 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012762 mode->crtc_hdisplay, mode->crtc_hsync_start,
12763 mode->crtc_hsync_end, mode->crtc_htotal,
12764 mode->crtc_vdisplay, mode->crtc_vsync_start,
12765 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12766}
12767
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012768static inline void
12769intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012770 unsigned int lane_count, struct intel_link_m_n *m_n)
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012771{
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012772 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12773 id, lane_count,
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012774 m_n->gmch_m, m_n->gmch_n,
12775 m_n->link_m, m_n->link_n, m_n->tu);
12776}
12777
Daniel Vetterc0b03412013-05-28 12:05:54 +020012778static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012779 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012780 const char *context)
12781{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012782 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012783 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012784 struct drm_plane *plane;
12785 struct intel_plane *intel_plane;
12786 struct intel_plane_state *state;
12787 struct drm_framebuffer *fb;
12788
Tvrtko Ursulin66766e42016-11-17 12:30:10 +000012789 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
12790 crtc->base.base.id, crtc->base.name, context);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012791
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012792 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
12793 transcoder_name(pipe_config->cpu_transcoder),
Daniel Vetterc0b03412013-05-28 12:05:54 +020012794 pipe_config->pipe_bpp, pipe_config->dither);
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012795
12796 if (pipe_config->has_pch_encoder)
12797 intel_dump_m_n_config(pipe_config, "fdi",
12798 pipe_config->fdi_lanes,
12799 &pipe_config->fdi_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012800
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012801 if (intel_crtc_has_dp_encoder(pipe_config)) {
Tvrtko Ursulina4309652016-11-17 12:30:09 +000012802 intel_dump_m_n_config(pipe_config, "dp m_n",
12803 pipe_config->lane_count, &pipe_config->dp_m_n);
Tvrtko Ursulind806e682016-11-17 15:44:09 +000012804 if (pipe_config->has_drrs)
12805 intel_dump_m_n_config(pipe_config, "dp m2_n2",
12806 pipe_config->lane_count,
12807 &pipe_config->dp_m2_n2);
Tvrtko Ursulinf6982332016-11-17 12:30:08 +000012808 }
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012809
Daniel Vetter55072d12014-11-20 16:10:28 +010012810 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012811 pipe_config->has_audio, pipe_config->has_infoframe);
Daniel Vetter55072d12014-11-20 16:10:28 +010012812
Daniel Vetterc0b03412013-05-28 12:05:54 +020012813 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012814 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012815 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012816 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12817 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012818 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
12819 pipe_config->port_clock,
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012820 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000012821
12822 if (INTEL_GEN(dev_priv) >= 9)
12823 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12824 crtc->num_scalers,
12825 pipe_config->scaler_state.scaler_users,
12826 pipe_config->scaler_state.scaler_id);
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000012827
12828 if (HAS_GMCH_DISPLAY(dev_priv))
12829 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12830 pipe_config->gmch_pfit.control,
12831 pipe_config->gmch_pfit.pgm_ratios,
12832 pipe_config->gmch_pfit.lvds_border_bits);
12833 else
12834 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
12835 pipe_config->pch_pfit.pos,
12836 pipe_config->pch_pfit.size,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000012837 enableddisabled(pipe_config->pch_pfit.enabled));
Tvrtko Ursulina74f8372016-11-17 12:30:13 +000012838
Tvrtko Ursulin2c894292016-11-17 12:30:11 +000012839 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
12840 pipe_config->ips_enabled, pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012841
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020012842 if (IS_GEN9_LP(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012843 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012844 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012845 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012846 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012847 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012848 pipe_config->dpll_hw_state.pll0,
12849 pipe_config->dpll_hw_state.pll1,
12850 pipe_config->dpll_hw_state.pll2,
12851 pipe_config->dpll_hw_state.pll3,
12852 pipe_config->dpll_hw_state.pll6,
12853 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012854 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012855 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012856 pipe_config->dpll_hw_state.pcsdw12);
Tvrtko Ursulin08537232016-10-13 11:03:02 +010012857 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012858 DRM_DEBUG_KMS("dpll_hw_state: "
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012859 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012860 pipe_config->dpll_hw_state.ctrl1,
12861 pipe_config->dpll_hw_state.cfgcr1,
12862 pipe_config->dpll_hw_state.cfgcr2);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012863 } else if (HAS_DDI(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012864 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012865 pipe_config->dpll_hw_state.wrpll,
12866 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012867 } else {
12868 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12869 "fp0: 0x%x, fp1: 0x%x\n",
12870 pipe_config->dpll_hw_state.dpll,
12871 pipe_config->dpll_hw_state.dpll_md,
12872 pipe_config->dpll_hw_state.fp0,
12873 pipe_config->dpll_hw_state.fp1);
12874 }
12875
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012876 DRM_DEBUG_KMS("planes on this crtc\n");
12877 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000012878 struct drm_format_name_buf format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012879 intel_plane = to_intel_plane(plane);
12880 if (intel_plane->pipe != crtc->pipe)
12881 continue;
12882
12883 state = to_intel_plane_state(plane->state);
12884 fb = state->base.fb;
12885 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012886 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12887 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012888 continue;
12889 }
12890
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000012891 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
12892 plane->base.id, plane->name,
Eric Engestromb3c11ac2016-11-12 01:12:56 +000012893 fb->base.id, fb->width, fb->height,
12894 drm_get_format_name(fb->pixel_format, &format_name));
Tvrtko Ursulindd2f6162016-11-17 12:30:12 +000012895 if (INTEL_GEN(dev_priv) >= 9)
12896 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12897 state->scaler_id,
12898 state->base.src.x1 >> 16,
12899 state->base.src.y1 >> 16,
12900 drm_rect_width(&state->base.src) >> 16,
12901 drm_rect_height(&state->base.src) >> 16,
12902 state->base.dst.x1, state->base.dst.y1,
12903 drm_rect_width(&state->base.dst),
12904 drm_rect_height(&state->base.dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012905 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012906}
12907
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012908static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012909{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012910 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012911 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012912 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012913 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012914
12915 /*
12916 * Walk the connector list instead of the encoder
12917 * list to detect the problem on ddi platforms
12918 * where there's just one encoder per digital port.
12919 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012920 drm_for_each_connector(connector, dev) {
12921 struct drm_connector_state *connector_state;
12922 struct intel_encoder *encoder;
12923
12924 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12925 if (!connector_state)
12926 connector_state = connector->state;
12927
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012928 if (!connector_state->best_encoder)
12929 continue;
12930
12931 encoder = to_intel_encoder(connector_state->best_encoder);
12932
12933 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012934
12935 switch (encoder->type) {
12936 unsigned int port_mask;
12937 case INTEL_OUTPUT_UNKNOWN:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012938 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012939 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012940 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012941 case INTEL_OUTPUT_HDMI:
12942 case INTEL_OUTPUT_EDP:
12943 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12944
12945 /* the same port mustn't appear more than once */
12946 if (used_ports & port_mask)
12947 return false;
12948
12949 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012950 break;
12951 case INTEL_OUTPUT_DP_MST:
12952 used_mst_ports |=
12953 1 << enc_to_mst(&encoder->base)->primary->port;
12954 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012955 default:
12956 break;
12957 }
12958 }
12959
Ville Syrjälä477321e2016-07-28 17:50:40 +030012960 /* can't mix MST and SST/HDMI on the same port */
12961 if (used_ports & used_mst_ports)
12962 return false;
12963
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012964 return true;
12965}
12966
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012967static void
12968clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12969{
12970 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012971 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012972 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012973 struct intel_shared_dpll *shared_dpll;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012974 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012975
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012976 /* FIXME: before the switch to atomic started, a new pipe_config was
12977 * kzalloc'd. Code that depends on any field being zero should be
12978 * fixed, so that the crtc_state can be safely duplicated. For now,
12979 * only fields that are know to not cause problems are preserved. */
12980
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012981 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012982 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012983 shared_dpll = crtc_state->shared_dpll;
12984 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012985 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012986
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012987 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012988
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012989 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012990 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012991 crtc_state->shared_dpll = shared_dpll;
12992 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012993 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012994}
12995
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012996static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012997intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012998 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012999{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013000 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020013001 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030013002 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020013003 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020013004 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020013005 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010013006 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020013007
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013008 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020013009
Daniel Vettere143a212013-07-04 12:01:15 +020013010 pipe_config->cpu_transcoder =
13011 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013012
Imre Deak2960bc92013-07-30 13:36:32 +030013013 /*
13014 * Sanitize sync polarity flags based on requested ones. If neither
13015 * positive or negative polarity is requested, treat this as meaning
13016 * negative polarity.
13017 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013018 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030013019 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013020 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030013021
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013022 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030013023 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013024 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030013025
Daniel Vetterd328c9d2015-04-10 16:22:37 +020013026 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13027 pipe_config);
13028 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010013029 goto fail;
13030
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030013031 /*
13032 * Determine the real pipe dimensions. Note that stereo modes can
13033 * increase the actual pipe size due to the frame doubling and
13034 * insertion of additional space for blanks between the frame. This
13035 * is stored in the crtc timings. We use the requested mode to do this
13036 * computation to clearly distinguish it from the adjusted mode, which
13037 * can be changed by the connectors in the below retry loop.
13038 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013039 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080013040 &pipe_config->pipe_src_w,
13041 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030013042
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013043 for_each_connector_in_state(state, connector, connector_state, i) {
13044 if (connector_state->crtc != crtc)
13045 continue;
13046
13047 encoder = to_intel_encoder(connector_state->best_encoder);
13048
Ville Syrjäläe25148d2016-06-22 21:57:09 +030013049 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13050 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13051 goto fail;
13052 }
13053
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013054 /*
13055 * Determine output_types before calling the .compute_config()
13056 * hooks so that the hooks can use this information safely.
13057 */
13058 pipe_config->output_types |= 1 << encoder->type;
13059 }
13060
Daniel Vettere29c22c2013-02-21 00:00:16 +010013061encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020013062 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020013063 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020013064 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013065
Daniel Vetter135c81b2013-07-21 21:37:09 +020013066 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013067 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13068 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020013069
Daniel Vetter7758a112012-07-08 19:40:39 +020013070 /* Pass our mode to the connectors and the CRTC to give them a chance to
13071 * adjust it according to limitations or connector properties, and also
13072 * a chance to reject the mode entirely.
13073 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030013074 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020013075 if (connector_state->crtc != crtc)
13076 continue;
13077
13078 encoder = to_intel_encoder(connector_state->best_encoder);
13079
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020013080 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020013081 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020013082 goto fail;
13083 }
13084 }
13085
Daniel Vetterff9a6752013-06-01 17:16:21 +020013086 /* Set default port clock if not overwritten by the encoder. Needs to be
13087 * done afterwards in case the encoder adjusts the mode. */
13088 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013089 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010013090 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013091
Daniel Vettera43f6e02013-06-07 23:10:32 +020013092 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010013093 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020013094 DRM_DEBUG_KMS("CRTC fixup failed\n");
13095 goto fail;
13096 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010013097
13098 if (ret == RETRY) {
13099 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13100 ret = -EINVAL;
13101 goto fail;
13102 }
13103
13104 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13105 retry = false;
13106 goto encoder_retry;
13107 }
13108
Daniel Vettere8fa4272015-08-12 11:43:34 +020013109 /* Dithering seems to not pass-through bits correctly when it should, so
13110 * only enable it on 6bpc panels. */
13111 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020013112 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020013113 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010013114
Daniel Vetter7758a112012-07-08 19:40:39 +020013115fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030013116 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020013117}
13118
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013119static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013120intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013121{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013122 struct drm_crtc *crtc;
13123 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020013124 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020013125
Ville Syrjälä76688512014-01-10 11:28:06 +020013126 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020013127 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020013128 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020013129
13130 /* Update hwmode for vblank functions */
13131 if (crtc->state->active)
13132 crtc->hwmode = crtc->state->adjusted_mode;
13133 else
13134 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020013135
13136 /*
13137 * Update legacy state to satisfy fbc code. This can
13138 * be removed when fbc uses the atomic state.
13139 */
13140 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13141 struct drm_plane_state *plane_state = crtc->primary->state;
13142
13143 crtc->primary->fb = plane_state->fb;
13144 crtc->x = plane_state->src_x >> 16;
13145 crtc->y = plane_state->src_y >> 16;
13146 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013147 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013148}
13149
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013150static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013151{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013152 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013153
13154 if (clock1 == clock2)
13155 return true;
13156
13157 if (!clock1 || !clock2)
13158 return false;
13159
13160 diff = abs(clock1 - clock2);
13161
13162 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13163 return true;
13164
13165 return false;
13166}
13167
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013168static bool
13169intel_compare_m_n(unsigned int m, unsigned int n,
13170 unsigned int m2, unsigned int n2,
13171 bool exact)
13172{
13173 if (m == m2 && n == n2)
13174 return true;
13175
13176 if (exact || !m || !n || !m2 || !n2)
13177 return false;
13178
13179 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13180
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013181 if (n > n2) {
13182 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013183 m2 <<= 1;
13184 n2 <<= 1;
13185 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013186 } else if (n < n2) {
13187 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013188 m <<= 1;
13189 n <<= 1;
13190 }
13191 }
13192
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013193 if (n != n2)
13194 return false;
13195
13196 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013197}
13198
13199static bool
13200intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13201 struct intel_link_m_n *m2_n2,
13202 bool adjust)
13203{
13204 if (m_n->tu == m2_n2->tu &&
13205 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13206 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13207 intel_compare_m_n(m_n->link_m, m_n->link_n,
13208 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13209 if (adjust)
13210 *m2_n2 = *m_n;
13211
13212 return true;
13213 }
13214
13215 return false;
13216}
13217
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013218static bool
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013219intel_pipe_config_compare(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013220 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013221 struct intel_crtc_state *pipe_config,
13222 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013223{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013224 bool ret = true;
13225
13226#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13227 do { \
13228 if (!adjust) \
13229 DRM_ERROR(fmt, ##__VA_ARGS__); \
13230 else \
13231 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13232 } while (0)
13233
Daniel Vetter66e985c2013-06-05 13:34:20 +020013234#define PIPE_CONF_CHECK_X(name) \
13235 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013236 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013237 "(expected 0x%08x, found 0x%08x)\n", \
13238 current_config->name, \
13239 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013240 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013241 }
13242
Daniel Vetter08a24032013-04-19 11:25:34 +020013243#define PIPE_CONF_CHECK_I(name) \
13244 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013245 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020013246 "(expected %i, found %i)\n", \
13247 current_config->name, \
13248 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013249 ret = false; \
13250 }
13251
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013252#define PIPE_CONF_CHECK_P(name) \
13253 if (current_config->name != pipe_config->name) { \
13254 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13255 "(expected %p, found %p)\n", \
13256 current_config->name, \
13257 pipe_config->name); \
13258 ret = false; \
13259 }
13260
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013261#define PIPE_CONF_CHECK_M_N(name) \
13262 if (!intel_compare_link_m_n(&current_config->name, \
13263 &pipe_config->name,\
13264 adjust)) { \
13265 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13266 "(expected tu %i gmch %i/%i link %i/%i, " \
13267 "found tu %i, gmch %i/%i link %i/%i)\n", \
13268 current_config->name.tu, \
13269 current_config->name.gmch_m, \
13270 current_config->name.gmch_n, \
13271 current_config->name.link_m, \
13272 current_config->name.link_n, \
13273 pipe_config->name.tu, \
13274 pipe_config->name.gmch_m, \
13275 pipe_config->name.gmch_n, \
13276 pipe_config->name.link_m, \
13277 pipe_config->name.link_n); \
13278 ret = false; \
13279 }
13280
Daniel Vetter55c561a2016-03-30 11:34:36 +020013281/* This is required for BDW+ where there is only one set of registers for
13282 * switching between high and low RR.
13283 * This macro can be used whenever a comparison has to be made between one
13284 * hw state and multiple sw state variables.
13285 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013286#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13287 if (!intel_compare_link_m_n(&current_config->name, \
13288 &pipe_config->name, adjust) && \
13289 !intel_compare_link_m_n(&current_config->alt_name, \
13290 &pipe_config->name, adjust)) { \
13291 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13292 "(expected tu %i gmch %i/%i link %i/%i, " \
13293 "or tu %i gmch %i/%i link %i/%i, " \
13294 "found tu %i, gmch %i/%i link %i/%i)\n", \
13295 current_config->name.tu, \
13296 current_config->name.gmch_m, \
13297 current_config->name.gmch_n, \
13298 current_config->name.link_m, \
13299 current_config->name.link_n, \
13300 current_config->alt_name.tu, \
13301 current_config->alt_name.gmch_m, \
13302 current_config->alt_name.gmch_n, \
13303 current_config->alt_name.link_m, \
13304 current_config->alt_name.link_n, \
13305 pipe_config->name.tu, \
13306 pipe_config->name.gmch_m, \
13307 pipe_config->name.gmch_n, \
13308 pipe_config->name.link_m, \
13309 pipe_config->name.link_n); \
13310 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010013311 }
13312
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013313#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13314 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013315 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013316 "(expected %i, found %i)\n", \
13317 current_config->name & (mask), \
13318 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013319 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013320 }
13321
Ville Syrjälä5e550652013-09-06 23:29:07 +030013322#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13323 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013324 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013325 "(expected %i, found %i)\n", \
13326 current_config->name, \
13327 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013328 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013329 }
13330
Daniel Vetterbb760062013-06-06 14:55:52 +020013331#define PIPE_CONF_QUIRK(quirk) \
13332 ((current_config->quirks | pipe_config->quirks) & (quirk))
13333
Daniel Vettereccb1402013-05-22 00:50:22 +020013334 PIPE_CONF_CHECK_I(cpu_transcoder);
13335
Daniel Vetter08a24032013-04-19 11:25:34 +020013336 PIPE_CONF_CHECK_I(has_pch_encoder);
13337 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013338 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020013339
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030013340 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030013341 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013342
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013343 if (INTEL_GEN(dev_priv) < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013344 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013345
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013346 if (current_config->has_drrs)
13347 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13348 } else
13349 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030013350
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013351 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020013352
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013353 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13354 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13355 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13356 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13357 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13358 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013359
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013360 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13361 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13362 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13363 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13364 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13365 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013366
Daniel Vetterc93f54c2013-06-27 19:47:19 +020013367 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020013368 PIPE_CONF_CHECK_I(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013369 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013370 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020013371 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080013372 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020013373
Daniel Vetter9ed109a2014-04-24 23:54:52 +020013374 PIPE_CONF_CHECK_I(has_audio);
13375
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013376 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013377 DRM_MODE_FLAG_INTERLACE);
13378
Daniel Vetterbb760062013-06-06 14:55:52 +020013379 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013380 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013381 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013382 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013383 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013384 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013385 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013386 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013387 DRM_MODE_FLAG_NVSYNC);
13388 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013389
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013390 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020013391 /* pfit ratios are autocomputed by the hw on gen4+ */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013392 if (INTEL_GEN(dev_priv) < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020013393 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013394 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020013395
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013396 if (!adjust) {
13397 PIPE_CONF_CHECK_I(pipe_src_w);
13398 PIPE_CONF_CHECK_I(pipe_src_h);
13399
13400 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13401 if (current_config->pch_pfit.enabled) {
13402 PIPE_CONF_CHECK_X(pch_pfit.pos);
13403 PIPE_CONF_CHECK_X(pch_pfit.size);
13404 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020013405
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020013406 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13407 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070013408
Jesse Barnese59150d2014-01-07 13:30:45 -080013409 /* BDW+ don't expose a synchronous way to read the state */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013410 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080013411 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030013412
Ville Syrjälä282740f2013-09-04 18:30:03 +030013413 PIPE_CONF_CHECK_I(double_wide);
13414
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013415 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013416 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020013417 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013418 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13419 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030013420 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010013421 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000013422 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13423 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13424 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020013425
Ville Syrjälä47eacba2016-04-12 22:14:35 +030013426 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13427 PIPE_CONF_CHECK_X(dsi_pll.div);
13428
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010013429 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030013430 PIPE_CONF_CHECK_I(pipe_bpp);
13431
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013432 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080013433 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030013434
Daniel Vetter66e985c2013-06-05 13:34:20 +020013435#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020013436#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013437#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013438#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030013439#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020013440#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013441#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020013442
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013443 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013444}
13445
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013446static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13447 const struct intel_crtc_state *pipe_config)
13448{
13449 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020013450 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013451 &pipe_config->fdi_m_n);
13452 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13453
13454 /*
13455 * FDI already provided one idea for the dotclock.
13456 * Yell if the encoder disagrees.
13457 */
13458 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13459 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13460 fdi_dotclock, dotclock);
13461 }
13462}
13463
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013464static void verify_wm_state(struct drm_crtc *crtc,
13465 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000013466{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013467 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000013468 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013469 struct skl_pipe_wm hw_wm, *sw_wm;
13470 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13471 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013472 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13473 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013474 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000013475
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013476 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000013477 return;
13478
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013479 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020013480 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013481
Damien Lespiau08db6652014-11-04 17:06:52 +000013482 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13483 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13484
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013485 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070013486 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013487 hw_plane_wm = &hw_wm.planes[plane];
13488 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000013489
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013490 /* Watermarks */
13491 for (level = 0; level <= max_level; level++) {
13492 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13493 &sw_plane_wm->wm[level]))
13494 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000013495
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013496 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",
13497 pipe_name(pipe), plane + 1, level,
13498 sw_plane_wm->wm[level].plane_en,
13499 sw_plane_wm->wm[level].plane_res_b,
13500 sw_plane_wm->wm[level].plane_res_l,
13501 hw_plane_wm->wm[level].plane_en,
13502 hw_plane_wm->wm[level].plane_res_b,
13503 hw_plane_wm->wm[level].plane_res_l);
13504 }
13505
13506 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13507 &sw_plane_wm->trans_wm)) {
13508 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",
13509 pipe_name(pipe), plane + 1,
13510 sw_plane_wm->trans_wm.plane_en,
13511 sw_plane_wm->trans_wm.plane_res_b,
13512 sw_plane_wm->trans_wm.plane_res_l,
13513 hw_plane_wm->trans_wm.plane_en,
13514 hw_plane_wm->trans_wm.plane_res_b,
13515 hw_plane_wm->trans_wm.plane_res_l);
13516 }
13517
13518 /* DDB */
13519 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13520 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13521
13522 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013523 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 -040013524 pipe_name(pipe), plane + 1,
13525 sw_ddb_entry->start, sw_ddb_entry->end,
13526 hw_ddb_entry->start, hw_ddb_entry->end);
13527 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013528 }
13529
Lyude27082492016-08-24 07:48:10 +020013530 /*
13531 * cursor
13532 * If the cursor plane isn't active, we may not have updated it's ddb
13533 * allocation. In that case since the ddb allocation will be updated
13534 * once the plane becomes visible, we can skip this check
13535 */
13536 if (intel_crtc->cursor_addr) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013537 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13538 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013539
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013540 /* Watermarks */
13541 for (level = 0; level <= max_level; level++) {
13542 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13543 &sw_plane_wm->wm[level]))
13544 continue;
13545
13546 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",
13547 pipe_name(pipe), level,
13548 sw_plane_wm->wm[level].plane_en,
13549 sw_plane_wm->wm[level].plane_res_b,
13550 sw_plane_wm->wm[level].plane_res_l,
13551 hw_plane_wm->wm[level].plane_en,
13552 hw_plane_wm->wm[level].plane_res_b,
13553 hw_plane_wm->wm[level].plane_res_l);
13554 }
13555
13556 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13557 &sw_plane_wm->trans_wm)) {
13558 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13559 pipe_name(pipe),
13560 sw_plane_wm->trans_wm.plane_en,
13561 sw_plane_wm->trans_wm.plane_res_b,
13562 sw_plane_wm->trans_wm.plane_res_l,
13563 hw_plane_wm->trans_wm.plane_en,
13564 hw_plane_wm->trans_wm.plane_res_b,
13565 hw_plane_wm->trans_wm.plane_res_l);
13566 }
13567
13568 /* DDB */
13569 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13570 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
13571
13572 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013573 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020013574 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013575 sw_ddb_entry->start, sw_ddb_entry->end,
13576 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020013577 }
Damien Lespiau08db6652014-11-04 17:06:52 +000013578 }
13579}
13580
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013581static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013582verify_connector_state(struct drm_device *dev,
13583 struct drm_atomic_state *state,
13584 struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013585{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013586 struct drm_connector *connector;
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013587 struct drm_connector_state *old_conn_state;
13588 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013589
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013590 for_each_connector_in_state(state, connector, old_conn_state, i) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013591 struct drm_encoder *encoder = connector->encoder;
13592 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013593
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013594 if (state->crtc != crtc)
13595 continue;
13596
Daniel Vetter5a21b662016-05-24 17:13:53 +020013597 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013598
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013599 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013600 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013601 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013602}
13603
13604static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013605verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013606{
13607 struct intel_encoder *encoder;
13608 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013609
Damien Lespiaub2784e12014-08-05 11:29:37 +010013610 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013611 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013612 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013613
13614 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13615 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013616 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013617
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013618 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013619 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013620 continue;
13621 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013622
13623 I915_STATE_WARN(connector->base.state->crtc !=
13624 encoder->base.crtc,
13625 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013626 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013627
Rob Clarke2c719b2014-12-15 13:56:32 -050013628 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013629 "encoder's enabled state mismatch "
13630 "(expected %i, found %i)\n",
13631 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013632
13633 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013634 bool active;
13635
13636 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013637 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013638 "encoder detached but still enabled on pipe %c.\n",
13639 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013640 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013641 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013642}
13643
13644static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013645verify_crtc_state(struct drm_crtc *crtc,
13646 struct drm_crtc_state *old_crtc_state,
13647 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013648{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013649 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013650 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013651 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13653 struct intel_crtc_state *pipe_config, *sw_config;
13654 struct drm_atomic_state *old_state;
13655 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013656
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013657 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013658 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013659 pipe_config = to_intel_crtc_state(old_crtc_state);
13660 memset(pipe_config, 0, sizeof(*pipe_config));
13661 pipe_config->base.crtc = crtc;
13662 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013663
Ville Syrjälä78108b72016-05-27 20:59:19 +030013664 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013665
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013666 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013667
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013668 /* hw state is inconsistent with the pipe quirk */
13669 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13670 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13671 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013672
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013673 I915_STATE_WARN(new_crtc_state->active != active,
13674 "crtc active state doesn't match with hw state "
13675 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013676
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013677 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13678 "transitional active state does not match atomic hw state "
13679 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013680
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013681 for_each_encoder_on_crtc(dev, crtc, encoder) {
13682 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013683
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013684 active = encoder->get_hw_state(encoder, &pipe);
13685 I915_STATE_WARN(active != new_crtc_state->active,
13686 "[ENCODER:%i] active %i with crtc active %i\n",
13687 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013688
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013689 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13690 "Encoder connected to wrong pipe %c\n",
13691 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013692
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013693 if (active) {
13694 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013695 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013696 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013697 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013698
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013699 if (!new_crtc_state->active)
13700 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013701
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013702 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013703
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013704 sw_config = to_intel_crtc_state(crtc->state);
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000013705 if (!intel_pipe_config_compare(dev_priv, sw_config,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013706 pipe_config, false)) {
13707 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13708 intel_dump_pipe_config(intel_crtc, pipe_config,
13709 "[hw state]");
13710 intel_dump_pipe_config(intel_crtc, sw_config,
13711 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013712 }
13713}
13714
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013715static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013716verify_single_dpll_state(struct drm_i915_private *dev_priv,
13717 struct intel_shared_dpll *pll,
13718 struct drm_crtc *crtc,
13719 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013720{
13721 struct intel_dpll_hw_state dpll_hw_state;
13722 unsigned crtc_mask;
13723 bool active;
13724
13725 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13726
13727 DRM_DEBUG_KMS("%s\n", pll->name);
13728
13729 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13730
13731 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13732 I915_STATE_WARN(!pll->on && pll->active_mask,
13733 "pll in active use but not on in sw tracking\n");
13734 I915_STATE_WARN(pll->on && !pll->active_mask,
13735 "pll is on but not used by any active crtc\n");
13736 I915_STATE_WARN(pll->on != active,
13737 "pll on state mismatch (expected %i, found %i)\n",
13738 pll->on, active);
13739 }
13740
13741 if (!crtc) {
13742 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13743 "more active pll users than references: %x vs %x\n",
13744 pll->active_mask, pll->config.crtc_mask);
13745
13746 return;
13747 }
13748
13749 crtc_mask = 1 << drm_crtc_index(crtc);
13750
13751 if (new_state->active)
13752 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13753 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13754 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13755 else
13756 I915_STATE_WARN(pll->active_mask & crtc_mask,
13757 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13758 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13759
13760 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13761 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13762 crtc_mask, pll->config.crtc_mask);
13763
13764 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13765 &dpll_hw_state,
13766 sizeof(dpll_hw_state)),
13767 "pll hw state mismatch\n");
13768}
13769
13770static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013771verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13772 struct drm_crtc_state *old_crtc_state,
13773 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013774{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013775 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013776 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13777 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13778
13779 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013780 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013781
13782 if (old_state->shared_dpll &&
13783 old_state->shared_dpll != new_state->shared_dpll) {
13784 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13785 struct intel_shared_dpll *pll = old_state->shared_dpll;
13786
13787 I915_STATE_WARN(pll->active_mask & crtc_mask,
13788 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13789 pipe_name(drm_crtc_index(crtc)));
13790 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13791 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13792 pipe_name(drm_crtc_index(crtc)));
13793 }
13794}
13795
13796static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013797intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013798 struct drm_atomic_state *state,
13799 struct drm_crtc_state *old_state,
13800 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013801{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013802 if (!needs_modeset(new_state) &&
13803 !to_intel_crtc_state(new_state)->update_pipe)
13804 return;
13805
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013806 verify_wm_state(crtc, new_state);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013807 verify_connector_state(crtc->dev, state, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013808 verify_crtc_state(crtc, old_state, new_state);
13809 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013810}
13811
13812static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013813verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013814{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013815 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013816 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013817
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013818 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013819 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013820}
Daniel Vetter53589012013-06-05 13:34:16 +020013821
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013822static void
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013823intel_modeset_verify_disabled(struct drm_device *dev,
13824 struct drm_atomic_state *state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013825{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013826 verify_encoder_state(dev);
Maarten Lankhorst677100c2016-11-08 13:55:41 +010013827 verify_connector_state(dev, state, NULL);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013828 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020013829}
13830
Ville Syrjälä80715b22014-05-15 20:23:23 +030013831static void update_scanline_offset(struct intel_crtc *crtc)
13832{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013833 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013834
13835 /*
13836 * The scanline counter increments at the leading edge of hsync.
13837 *
13838 * On most platforms it starts counting from vtotal-1 on the
13839 * first active line. That means the scanline counter value is
13840 * always one less than what we would expect. Ie. just after
13841 * start of vblank, which also occurs at start of hsync (on the
13842 * last active line), the scanline counter will read vblank_start-1.
13843 *
13844 * On gen2 the scanline counter starts counting from 1 instead
13845 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13846 * to keep the value positive), instead of adding one.
13847 *
13848 * On HSW+ the behaviour of the scanline counter depends on the output
13849 * type. For DP ports it behaves like most other platforms, but on HDMI
13850 * there's an extra 1 line difference. So we need to add two instead of
13851 * one to the value.
13852 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013853 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013854 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013855 int vtotal;
13856
Ville Syrjälä124abe02015-09-08 13:40:45 +030013857 vtotal = adjusted_mode->crtc_vtotal;
13858 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013859 vtotal /= 2;
13860
13861 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013862 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013863 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013864 crtc->scanline_offset = 2;
13865 } else
13866 crtc->scanline_offset = 1;
13867}
13868
Maarten Lankhorstad421372015-06-15 12:33:42 +020013869static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013870{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013871 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013872 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013873 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013874 struct drm_crtc *crtc;
13875 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013876 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013877
13878 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013879 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013880
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013881 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013883 struct intel_shared_dpll *old_dpll =
13884 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013885
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013886 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013887 continue;
13888
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013889 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013890
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013891 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013892 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013893
Maarten Lankhorstad421372015-06-15 12:33:42 +020013894 if (!shared_dpll)
13895 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13896
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013897 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013898 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013899}
13900
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013901/*
13902 * This implements the workaround described in the "notes" section of the mode
13903 * set sequence documentation. When going from no pipes or single pipe to
13904 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13905 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13906 */
13907static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13908{
13909 struct drm_crtc_state *crtc_state;
13910 struct intel_crtc *intel_crtc;
13911 struct drm_crtc *crtc;
13912 struct intel_crtc_state *first_crtc_state = NULL;
13913 struct intel_crtc_state *other_crtc_state = NULL;
13914 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13915 int i;
13916
13917 /* look at all crtc's that are going to be enabled in during modeset */
13918 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13919 intel_crtc = to_intel_crtc(crtc);
13920
13921 if (!crtc_state->active || !needs_modeset(crtc_state))
13922 continue;
13923
13924 if (first_crtc_state) {
13925 other_crtc_state = to_intel_crtc_state(crtc_state);
13926 break;
13927 } else {
13928 first_crtc_state = to_intel_crtc_state(crtc_state);
13929 first_pipe = intel_crtc->pipe;
13930 }
13931 }
13932
13933 /* No workaround needed? */
13934 if (!first_crtc_state)
13935 return 0;
13936
13937 /* w/a possibly needed, check how many crtc's are already enabled. */
13938 for_each_intel_crtc(state->dev, intel_crtc) {
13939 struct intel_crtc_state *pipe_config;
13940
13941 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13942 if (IS_ERR(pipe_config))
13943 return PTR_ERR(pipe_config);
13944
13945 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13946
13947 if (!pipe_config->base.active ||
13948 needs_modeset(&pipe_config->base))
13949 continue;
13950
13951 /* 2 or more enabled crtcs means no need for w/a */
13952 if (enabled_pipe != INVALID_PIPE)
13953 return 0;
13954
13955 enabled_pipe = intel_crtc->pipe;
13956 }
13957
13958 if (enabled_pipe != INVALID_PIPE)
13959 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13960 else if (other_crtc_state)
13961 other_crtc_state->hsw_workaround_pipe = first_pipe;
13962
13963 return 0;
13964}
13965
Ville Syrjälä8d965612016-11-14 18:35:10 +020013966static int intel_lock_all_pipes(struct drm_atomic_state *state)
13967{
13968 struct drm_crtc *crtc;
13969
13970 /* Add all pipes to the state */
13971 for_each_crtc(state->dev, crtc) {
13972 struct drm_crtc_state *crtc_state;
13973
13974 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13975 if (IS_ERR(crtc_state))
13976 return PTR_ERR(crtc_state);
13977 }
13978
13979 return 0;
13980}
13981
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013982static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13983{
13984 struct drm_crtc *crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013985
Ville Syrjälä8d965612016-11-14 18:35:10 +020013986 /*
13987 * Add all pipes to the state, and force
13988 * a modeset on all the active ones.
13989 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013990 for_each_crtc(state->dev, crtc) {
Ville Syrjälä9780aad2016-11-14 18:35:11 +020013991 struct drm_crtc_state *crtc_state;
13992 int ret;
13993
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013994 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13995 if (IS_ERR(crtc_state))
13996 return PTR_ERR(crtc_state);
13997
13998 if (!crtc_state->active || needs_modeset(crtc_state))
13999 continue;
14000
14001 crtc_state->mode_changed = true;
14002
14003 ret = drm_atomic_add_affected_connectors(state, crtc);
14004 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020014005 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014006
14007 ret = drm_atomic_add_affected_planes(state, crtc);
14008 if (ret)
Ville Syrjälä9780aad2016-11-14 18:35:11 +020014009 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014010 }
14011
Ville Syrjälä9780aad2016-11-14 18:35:11 +020014012 return 0;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014013}
14014
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014015static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014016{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014017 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014018 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014019 struct drm_crtc *crtc;
14020 struct drm_crtc_state *crtc_state;
14021 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014022
Maarten Lankhorstb3592832015-06-15 12:33:38 +020014023 if (!check_digital_port_conflicts(state)) {
14024 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
14025 return -EINVAL;
14026 }
14027
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014028 intel_state->modeset = true;
14029 intel_state->active_crtcs = dev_priv->active_crtcs;
14030
14031 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14032 if (crtc_state->active)
14033 intel_state->active_crtcs |= 1 << i;
14034 else
14035 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070014036
14037 if (crtc_state->active != crtc->state->active)
14038 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014039 }
14040
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014041 /*
14042 * See if the config requires any additional preparation, e.g.
14043 * to adjust global state with pipes off. We need to do this
14044 * here so we can get the modeset_pipe updated config for the new
14045 * mode set on this crtc. For other crtcs we need to use the
14046 * adjusted_mode bits in the crtc directly.
14047 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014048 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030014049 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030014050 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030014051 if (!intel_state->cdclk_pll_vco)
14052 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014053
Clint Taylorc89e39f2016-05-13 23:41:21 +030014054 ret = dev_priv->display.modeset_calc_cdclk(state);
14055 if (ret < 0)
14056 return ret;
14057
Ville Syrjälä8d965612016-11-14 18:35:10 +020014058 /*
14059 * Writes to dev_priv->atomic_cdclk_freq must protected by
14060 * holding all the crtc locks, even if we don't end up
14061 * touching the hardware
14062 */
14063 if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
14064 ret = intel_lock_all_pipes(state);
14065 if (ret < 0)
14066 return ret;
14067 }
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014068
Ville Syrjälä8d965612016-11-14 18:35:10 +020014069 /* All pipes must be switched off while we change the cdclk. */
14070 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
14071 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
14072 ret = intel_modeset_all_pipes(state);
14073 if (ret < 0)
14074 return ret;
14075 }
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010014076
14077 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14078 intel_state->cdclk, intel_state->dev_cdclk);
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020014079 } else {
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014080 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020014081 }
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014082
Maarten Lankhorstad421372015-06-15 12:33:42 +020014083 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014084
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014085 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020014086 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020014087
Maarten Lankhorstad421372015-06-15 12:33:42 +020014088 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014089}
14090
Matt Roperaa363132015-09-24 15:53:18 -070014091/*
14092 * Handle calculation of various watermark data at the end of the atomic check
14093 * phase. The code here should be run after the per-crtc and per-plane 'check'
14094 * handlers to ensure that all derived state has been updated.
14095 */
Matt Roper55994c22016-05-12 07:06:08 -070014096static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070014097{
14098 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070014099 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070014100
14101 /* Is there platform-specific watermark information to calculate? */
14102 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070014103 return dev_priv->display.compute_global_watermarks(state);
14104
14105 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070014106}
14107
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014108/**
14109 * intel_atomic_check - validate state object
14110 * @dev: drm device
14111 * @state: state to validate
14112 */
14113static int intel_atomic_check(struct drm_device *dev,
14114 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014115{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014116 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070014117 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014118 struct drm_crtc *crtc;
14119 struct drm_crtc_state *crtc_state;
14120 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014121 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014122
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014123 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014124 if (ret)
14125 return ret;
14126
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014127 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014128 struct intel_crtc_state *pipe_config =
14129 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014130
14131 /* Catch I915_MODE_FLAG_INHERITED */
14132 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14133 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014134
Daniel Vetter26495482015-07-15 14:15:52 +020014135 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014136 continue;
14137
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014138 if (!crtc_state->enable) {
14139 any_ms = true;
14140 continue;
14141 }
14142
Daniel Vetter26495482015-07-15 14:15:52 +020014143 /* FIXME: For only active_changed we shouldn't need to do any
14144 * state recomputation at all. */
14145
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014146 ret = drm_atomic_add_affected_connectors(state, crtc);
14147 if (ret)
14148 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020014149
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014150 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014151 if (ret) {
14152 intel_dump_pipe_config(to_intel_crtc(crtc),
14153 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014154 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014155 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014156
Jani Nikula73831232015-11-19 10:26:30 +020014157 if (i915.fastboot &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000014158 intel_pipe_config_compare(dev_priv,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014159 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014160 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020014161 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014162 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020014163 }
14164
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014165 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020014166 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014167
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014168 ret = drm_atomic_add_affected_planes(state, crtc);
14169 if (ret)
14170 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014171
Daniel Vetter26495482015-07-15 14:15:52 +020014172 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14173 needs_modeset(crtc_state) ?
14174 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014175 }
14176
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014177 if (any_ms) {
14178 ret = intel_modeset_checks(state);
14179
14180 if (ret)
14181 return ret;
Ville Syrjäläe0ca7a62016-11-14 18:35:09 +020014182 } else {
14183 intel_state->cdclk = dev_priv->atomic_cdclk_freq;
14184 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014185
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014186 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070014187 if (ret)
14188 return ret;
14189
Paulo Zanonif51be2e2016-01-19 11:35:50 -020014190 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070014191 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014192}
14193
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014194static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010014195 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014196{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014197 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014198 struct drm_crtc_state *crtc_state;
14199 struct drm_crtc *crtc;
14200 int i, ret;
14201
Daniel Vetter5a21b662016-05-24 17:13:53 +020014202 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14203 if (state->legacy_cursor_update)
14204 continue;
14205
14206 ret = intel_crtc_wait_for_pending_flips(crtc);
14207 if (ret)
14208 return ret;
14209
14210 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14211 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014212 }
14213
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014214 ret = mutex_lock_interruptible(&dev->struct_mutex);
14215 if (ret)
14216 return ret;
14217
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014218 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010014219 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014220
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014221 return ret;
14222}
14223
Maarten Lankhorsta2991412016-05-17 15:07:48 +020014224u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14225{
14226 struct drm_device *dev = crtc->base.dev;
14227
14228 if (!dev->max_vblank_count)
14229 return drm_accurate_vblank_count(&crtc->base);
14230
14231 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14232}
14233
Daniel Vetter5a21b662016-05-24 17:13:53 +020014234static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14235 struct drm_i915_private *dev_priv,
14236 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010014237{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014238 unsigned last_vblank_count[I915_MAX_PIPES];
14239 enum pipe pipe;
14240 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014241
Daniel Vetter5a21b662016-05-24 17:13:53 +020014242 if (!crtc_mask)
14243 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014244
Daniel Vetter5a21b662016-05-24 17:13:53 +020014245 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020014246 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14247 pipe);
Maarten Lankhorste8861672016-02-24 11:24:26 +010014248
Daniel Vetter5a21b662016-05-24 17:13:53 +020014249 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010014250 continue;
14251
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014252 ret = drm_crtc_vblank_get(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014253 if (WARN_ON(ret != 0)) {
14254 crtc_mask &= ~(1 << pipe);
14255 continue;
14256 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014257
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014258 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014259 }
14260
14261 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020014262 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14263 pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014264 long lret;
14265
14266 if (!((1 << pipe) & crtc_mask))
14267 continue;
14268
14269 lret = wait_event_timeout(dev->vblank[pipe].queue,
14270 last_vblank_count[pipe] !=
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014271 drm_crtc_vblank_count(&crtc->base),
Daniel Vetter5a21b662016-05-24 17:13:53 +020014272 msecs_to_jiffies(50));
14273
14274 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
14275
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014276 drm_crtc_vblank_put(&crtc->base);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014277 }
14278}
14279
Daniel Vetter5a21b662016-05-24 17:13:53 +020014280static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014281{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014282 /* fb updated, need to unpin old fb */
14283 if (crtc_state->fb_changed)
14284 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014285
Daniel Vetter5a21b662016-05-24 17:13:53 +020014286 /* wm changes, need vblank before final wm's */
14287 if (crtc_state->update_wm_post)
14288 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014289
Daniel Vetter5a21b662016-05-24 17:13:53 +020014290 /*
14291 * cxsr is re-enabled after vblank.
14292 * This is already handled by crtc_state->update_wm_post,
14293 * but added for clarity.
14294 */
14295 if (crtc_state->disable_cxsr)
14296 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014297
Daniel Vetter5a21b662016-05-24 17:13:53 +020014298 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014299}
14300
Lyude896e5bb2016-08-24 07:48:09 +020014301static void intel_update_crtc(struct drm_crtc *crtc,
14302 struct drm_atomic_state *state,
14303 struct drm_crtc_state *old_crtc_state,
14304 unsigned int *crtc_vblank_mask)
14305{
14306 struct drm_device *dev = crtc->dev;
14307 struct drm_i915_private *dev_priv = to_i915(dev);
14308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14309 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14310 bool modeset = needs_modeset(crtc->state);
14311
14312 if (modeset) {
14313 update_scanline_offset(intel_crtc);
14314 dev_priv->display.crtc_enable(pipe_config, state);
14315 } else {
14316 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14317 }
14318
14319 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14320 intel_fbc_enable(
14321 intel_crtc, pipe_config,
14322 to_intel_plane_state(crtc->primary->state));
14323 }
14324
14325 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14326
14327 if (needs_vblank_wait(pipe_config))
14328 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14329}
14330
14331static void intel_update_crtcs(struct drm_atomic_state *state,
14332 unsigned int *crtc_vblank_mask)
14333{
14334 struct drm_crtc *crtc;
14335 struct drm_crtc_state *old_crtc_state;
14336 int i;
14337
14338 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14339 if (!crtc->state->active)
14340 continue;
14341
14342 intel_update_crtc(crtc, state, old_crtc_state,
14343 crtc_vblank_mask);
14344 }
14345}
14346
Lyude27082492016-08-24 07:48:10 +020014347static void skl_update_crtcs(struct drm_atomic_state *state,
14348 unsigned int *crtc_vblank_mask)
14349{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020014350 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020014351 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14352 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040014353 struct intel_crtc *intel_crtc;
Lyude27082492016-08-24 07:48:10 +020014354 struct drm_crtc_state *old_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040014355 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020014356 unsigned int updated = 0;
14357 bool progress;
14358 enum pipe pipe;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014359 int i;
14360
14361 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
14362
14363 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
14364 /* ignore allocations for crtc's that have been turned off. */
14365 if (crtc->state->active)
14366 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020014367
14368 /*
14369 * Whenever the number of active pipes changes, we need to make sure we
14370 * update the pipes in the right order so that their ddb allocations
14371 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14372 * cause pipe underruns and other bad stuff.
14373 */
14374 do {
Lyude27082492016-08-24 07:48:10 +020014375 progress = false;
14376
14377 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14378 bool vbl_wait = false;
14379 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040014380
14381 intel_crtc = to_intel_crtc(crtc);
14382 cstate = to_intel_crtc_state(crtc->state);
14383 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020014384
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014385 if (updated & cmask || !cstate->base.active)
Lyude27082492016-08-24 07:48:10 +020014386 continue;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014387
14388 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
Lyude27082492016-08-24 07:48:10 +020014389 continue;
14390
14391 updated |= cmask;
Maarten Lankhorst5eff5032016-11-08 13:55:35 +010014392 entries[i] = &cstate->wm.skl.ddb;
Lyude27082492016-08-24 07:48:10 +020014393
14394 /*
14395 * If this is an already active pipe, it's DDB changed,
14396 * and this isn't the last pipe that needs updating
14397 * then we need to wait for a vblank to pass for the
14398 * new ddb allocation to take effect.
14399 */
Lyudece0ba282016-09-15 10:46:35 -040014400 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
Maarten Lankhorst512b5522016-11-08 13:55:34 +010014401 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
Lyude27082492016-08-24 07:48:10 +020014402 !crtc->state->active_changed &&
14403 intel_state->wm_results.dirty_pipes != updated)
14404 vbl_wait = true;
14405
14406 intel_update_crtc(crtc, state, old_crtc_state,
14407 crtc_vblank_mask);
14408
14409 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020014410 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020014411
14412 progress = true;
14413 }
14414 } while (progress);
14415}
14416
Daniel Vetter94f05022016-06-14 18:01:00 +020014417static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014418{
Daniel Vetter94f05022016-06-14 18:01:00 +020014419 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014420 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014421 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014422 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014423 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014424 struct intel_crtc_state *intel_cstate;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014425 bool hw_check = intel_state->modeset;
14426 unsigned long put_domains[I915_MAX_PIPES] = {};
14427 unsigned crtc_vblank_mask = 0;
Chris Wilsone95433c2016-10-28 13:58:27 +010014428 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020014429
Daniel Vetterea0000f2016-06-13 16:13:46 +020014430 drm_atomic_helper_wait_for_dependencies(state);
14431
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010014432 if (intel_state->modeset)
Daniel Vetter5a21b662016-05-24 17:13:53 +020014433 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014434
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014435 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14437
Daniel Vetter5a21b662016-05-24 17:13:53 +020014438 if (needs_modeset(crtc->state) ||
14439 to_intel_crtc_state(crtc->state)->update_pipe) {
14440 hw_check = true;
14441
14442 put_domains[to_intel_crtc(crtc)->pipe] =
14443 modeset_get_crtc_power_domains(crtc,
14444 to_intel_crtc_state(crtc->state));
14445 }
14446
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014447 if (!needs_modeset(crtc->state))
14448 continue;
14449
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014450 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010014451
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014452 if (old_crtc_state->active) {
14453 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorst4a806552016-08-09 17:04:01 +020014454 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014455 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020014456 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014457 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020014458
14459 /*
14460 * Underruns don't always raise
14461 * interrupts, so check manually.
14462 */
14463 intel_check_cpu_fifo_underruns(dev_priv);
14464 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010014465
Maarten Lankhorste62929b2016-11-08 13:55:33 +010014466 if (!crtc->state->active) {
14467 /*
14468 * Make sure we don't call initial_watermarks
14469 * for ILK-style watermark updates.
14470 */
14471 if (dev_priv->display.atomic_update_watermarks)
14472 dev_priv->display.initial_watermarks(intel_state,
14473 to_intel_crtc_state(crtc->state));
14474 else
14475 intel_update_watermarks(intel_crtc);
14476 }
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014477 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010014478 }
Daniel Vetter7758a112012-07-08 19:40:39 +020014479
Daniel Vetterea9d7582012-07-10 10:42:52 +020014480 /* Only after disabling all output pipelines that will be changed can we
14481 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014482 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020014483
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014484 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014485 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014486
14487 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030014488 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030014489 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014490 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010014491
Lyude656d1b82016-08-17 15:55:54 -040014492 /*
14493 * SKL workaround: bspec recommends we disable the SAGV when we
14494 * have more then one pipe enabled
14495 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030014496 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014497 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014498
Maarten Lankhorst677100c2016-11-08 13:55:41 +010014499 intel_modeset_verify_disabled(dev, state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014500 }
Daniel Vetter47fab732012-10-26 10:58:18 +020014501
Lyude896e5bb2016-08-24 07:48:09 +020014502 /* Complete the events for pipes that have now been disabled */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014503 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020014504 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014505
Daniel Vetter1f7528c2016-06-13 16:13:45 +020014506 /* Complete events for now disable pipes here. */
14507 if (modeset && !crtc->state->active && crtc->state->event) {
14508 spin_lock_irq(&dev->event_lock);
14509 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14510 spin_unlock_irq(&dev->event_lock);
14511
14512 crtc->state->event = NULL;
14513 }
Matt Ropered4a6a72016-02-23 17:20:13 -080014514 }
14515
Lyude896e5bb2016-08-24 07:48:09 +020014516 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14517 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14518
Daniel Vetter94f05022016-06-14 18:01:00 +020014519 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14520 * already, but still need the state for the delayed optimization. To
14521 * fix this:
14522 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14523 * - schedule that vblank worker _before_ calling hw_done
14524 * - at the start of commit_tail, cancel it _synchrously
14525 * - switch over to the vblank wait helper in the core after that since
14526 * we don't need out special handling any more.
14527 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020014528 if (!state->legacy_cursor_update)
14529 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14530
14531 /*
14532 * Now that the vblank has passed, we can go ahead and program the
14533 * optimal watermarks on platforms that need two-step watermark
14534 * programming.
14535 *
14536 * TODO: Move this (and other cleanup) to an async worker eventually.
14537 */
14538 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14539 intel_cstate = to_intel_crtc_state(crtc->state);
14540
14541 if (dev_priv->display.optimize_watermarks)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014542 dev_priv->display.optimize_watermarks(intel_state,
14543 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014544 }
14545
14546 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14547 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14548
14549 if (put_domains[i])
14550 modeset_put_power_domains(dev_priv, put_domains[i]);
14551
Maarten Lankhorst677100c2016-11-08 13:55:41 +010014552 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014553 }
14554
Paulo Zanoni56feca92016-09-22 18:00:28 -030014555 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014556 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014557
Daniel Vetter94f05022016-06-14 18:01:00 +020014558 drm_atomic_helper_commit_hw_done(state);
14559
Daniel Vetter5a21b662016-05-24 17:13:53 +020014560 if (intel_state->modeset)
14561 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14562
14563 mutex_lock(&dev->struct_mutex);
14564 drm_atomic_helper_cleanup_planes(dev, state);
14565 mutex_unlock(&dev->struct_mutex);
14566
Daniel Vetterea0000f2016-06-13 16:13:46 +020014567 drm_atomic_helper_commit_cleanup_done(state);
14568
Chris Wilson08536952016-10-14 13:18:18 +010014569 drm_atomic_state_put(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080014570
Mika Kuoppala75714942015-12-16 09:26:48 +020014571 /* As one of the primary mmio accessors, KMS has a high likelihood
14572 * of triggering bugs in unclaimed access. After we finish
14573 * modesetting, see if an error has been flagged, and if so
14574 * enable debugging for the next modeset - and hope we catch
14575 * the culprit.
14576 *
14577 * XXX note that we assume display power is on at this point.
14578 * This might hold true now but we need to add pm helper to check
14579 * unclaimed only when the hardware is on, as atomic commits
14580 * can happen also when the device is completely off.
14581 */
14582 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020014583}
14584
14585static void intel_atomic_commit_work(struct work_struct *work)
14586{
Chris Wilsonc004a902016-10-28 13:58:45 +010014587 struct drm_atomic_state *state =
14588 container_of(work, struct drm_atomic_state, commit_work);
14589
Daniel Vetter94f05022016-06-14 18:01:00 +020014590 intel_atomic_commit_tail(state);
14591}
14592
Chris Wilsonc004a902016-10-28 13:58:45 +010014593static int __i915_sw_fence_call
14594intel_atomic_commit_ready(struct i915_sw_fence *fence,
14595 enum i915_sw_fence_notify notify)
14596{
14597 struct intel_atomic_state *state =
14598 container_of(fence, struct intel_atomic_state, commit_ready);
14599
14600 switch (notify) {
14601 case FENCE_COMPLETE:
14602 if (state->base.commit_work.func)
14603 queue_work(system_unbound_wq, &state->base.commit_work);
14604 break;
14605
14606 case FENCE_FREE:
14607 drm_atomic_state_put(&state->base);
14608 break;
14609 }
14610
14611 return NOTIFY_DONE;
14612}
14613
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014614static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14615{
14616 struct drm_plane_state *old_plane_state;
14617 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014618 int i;
14619
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010014620 for_each_plane_in_state(state, plane, old_plane_state, i)
14621 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14622 intel_fb_obj(plane->state->fb),
14623 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014624}
14625
Daniel Vetter94f05022016-06-14 18:01:00 +020014626/**
14627 * intel_atomic_commit - commit validated state object
14628 * @dev: DRM device
14629 * @state: the top-level driver state object
14630 * @nonblock: nonblocking commit
14631 *
14632 * This function commits a top-level state object that has been validated
14633 * with drm_atomic_helper_check().
14634 *
Daniel Vetter94f05022016-06-14 18:01:00 +020014635 * RETURNS
14636 * Zero for success or -errno.
14637 */
14638static int intel_atomic_commit(struct drm_device *dev,
14639 struct drm_atomic_state *state,
14640 bool nonblock)
14641{
14642 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014643 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020014644 int ret = 0;
14645
Daniel Vetter94f05022016-06-14 18:01:00 +020014646 ret = drm_atomic_helper_setup_commit(state, nonblock);
14647 if (ret)
14648 return ret;
14649
Chris Wilsonc004a902016-10-28 13:58:45 +010014650 drm_atomic_state_get(state);
14651 i915_sw_fence_init(&intel_state->commit_ready,
14652 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014653
Chris Wilsond07f0e52016-10-28 13:58:44 +010014654 ret = intel_atomic_prepare_commit(dev, state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014655 if (ret) {
14656 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Chris Wilsonc004a902016-10-28 13:58:45 +010014657 i915_sw_fence_commit(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014658 return ret;
14659 }
14660
14661 drm_atomic_helper_swap_state(state, true);
14662 dev_priv->wm.distrust_bios_wm = false;
Daniel Vetter94f05022016-06-14 18:01:00 +020014663 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014664 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014665
Maarten Lankhorstc3b32652016-11-08 13:55:40 +010014666 if (intel_state->modeset) {
14667 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14668 sizeof(intel_state->min_pixclk));
14669 dev_priv->active_crtcs = intel_state->active_crtcs;
14670 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
14671 }
14672
Chris Wilson08536952016-10-14 13:18:18 +010014673 drm_atomic_state_get(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010014674 INIT_WORK(&state->commit_work,
14675 nonblock ? intel_atomic_commit_work : NULL);
14676
14677 i915_sw_fence_commit(&intel_state->commit_ready);
14678 if (!nonblock) {
14679 i915_sw_fence_wait(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014680 intel_atomic_commit_tail(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010014681 }
Mika Kuoppala75714942015-12-16 09:26:48 +020014682
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014683 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020014684}
14685
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014686void intel_crtc_restore_mode(struct drm_crtc *crtc)
14687{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014688 struct drm_device *dev = crtc->dev;
14689 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014690 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014691 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014692
14693 state = drm_atomic_state_alloc(dev);
14694 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030014695 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14696 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014697 return;
14698 }
14699
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014700 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014701
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014702retry:
14703 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14704 ret = PTR_ERR_OR_ZERO(crtc_state);
14705 if (!ret) {
14706 if (!crtc_state->active)
14707 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014708
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014709 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014710 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014711 }
14712
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014713 if (ret == -EDEADLK) {
14714 drm_atomic_state_clear(state);
14715 drm_modeset_backoff(state->acquire_ctx);
14716 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030014717 }
14718
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014719out:
Chris Wilson08536952016-10-14 13:18:18 +010014720 drm_atomic_state_put(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014721}
14722
Bob Paauwea8784872016-07-15 14:59:02 +010014723/*
14724 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14725 * drm_atomic_helper_legacy_gamma_set() directly.
14726 */
14727static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14728 u16 *red, u16 *green, u16 *blue,
14729 uint32_t size)
14730{
14731 struct drm_device *dev = crtc->dev;
14732 struct drm_mode_config *config = &dev->mode_config;
14733 struct drm_crtc_state *state;
14734 int ret;
14735
14736 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14737 if (ret)
14738 return ret;
14739
14740 /*
14741 * Make sure we update the legacy properties so this works when
14742 * atomic is not enabled.
14743 */
14744
14745 state = crtc->state;
14746
14747 drm_object_property_set_value(&crtc->base,
14748 config->degamma_lut_property,
14749 (state->degamma_lut) ?
14750 state->degamma_lut->base.id : 0);
14751
14752 drm_object_property_set_value(&crtc->base,
14753 config->ctm_property,
14754 (state->ctm) ?
14755 state->ctm->base.id : 0);
14756
14757 drm_object_property_set_value(&crtc->base,
14758 config->gamma_lut_property,
14759 (state->gamma_lut) ?
14760 state->gamma_lut->base.id : 0);
14761
14762 return 0;
14763}
14764
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014765static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010014766 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014767 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014768 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014769 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014770 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014771 .atomic_duplicate_state = intel_crtc_duplicate_state,
14772 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014773};
14774
Matt Roper6beb8c232014-12-01 15:40:14 -080014775/**
14776 * intel_prepare_plane_fb - Prepare fb for usage on plane
14777 * @plane: drm plane to prepare for
14778 * @fb: framebuffer to prepare for presentation
14779 *
14780 * Prepares a framebuffer for usage on a display plane. Generally this
14781 * involves pinning the underlying object and updating the frontbuffer tracking
14782 * bits. Some older platforms need special physical address handling for
14783 * cursor planes.
14784 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014785 * Must be called with struct_mutex held.
14786 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014787 * Returns 0 on success, negative error code on failure.
14788 */
14789int
14790intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014791 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014792{
Chris Wilsonc004a902016-10-28 13:58:45 +010014793 struct intel_atomic_state *intel_state =
14794 to_intel_atomic_state(new_state->state);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014795 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014796 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014797 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014798 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010014799 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014800
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014801 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014802 return 0;
14803
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014804 if (old_obj) {
14805 struct drm_crtc_state *crtc_state =
Chris Wilsonc004a902016-10-28 13:58:45 +010014806 drm_atomic_get_existing_crtc_state(new_state->state,
14807 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014808
14809 /* Big Hammer, we also need to ensure that any pending
14810 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14811 * current scanout is retired before unpinning the old
14812 * framebuffer. Note that we rely on userspace rendering
14813 * into the buffer attached to the pipe they are waiting
14814 * on. If not, userspace generates a GPU hang with IPEHR
14815 * point to the MI_WAIT_FOR_EVENT.
14816 *
14817 * This should only fail upon a hung GPU, in which case we
14818 * can safely continue.
14819 */
Chris Wilsonc004a902016-10-28 13:58:45 +010014820 if (needs_modeset(crtc_state)) {
14821 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14822 old_obj->resv, NULL,
14823 false, 0,
14824 GFP_KERNEL);
14825 if (ret < 0)
14826 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014827 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014828 }
14829
Chris Wilsonc004a902016-10-28 13:58:45 +010014830 if (new_state->fence) { /* explicit fencing */
14831 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14832 new_state->fence,
14833 I915_FENCE_TIMEOUT,
14834 GFP_KERNEL);
14835 if (ret < 0)
14836 return ret;
14837 }
14838
Chris Wilsonc37efb92016-06-17 08:28:47 +010014839 if (!obj)
14840 return 0;
14841
Chris Wilsonc004a902016-10-28 13:58:45 +010014842 if (!new_state->fence) { /* implicit fencing */
14843 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14844 obj->resv, NULL,
14845 false, I915_FENCE_TIMEOUT,
14846 GFP_KERNEL);
14847 if (ret < 0)
14848 return ret;
Chris Wilson6b5e90f2016-11-14 20:41:05 +000014849
14850 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
Chris Wilsonc004a902016-10-28 13:58:45 +010014851 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020014852
Chris Wilsonc37efb92016-06-17 08:28:47 +010014853 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014854 INTEL_INFO(dev_priv)->cursor_needs_physical) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014855 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
Matt Roper6beb8c232014-12-01 15:40:14 -080014856 ret = i915_gem_object_attach_phys(obj, align);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014857 if (ret) {
Matt Roper6beb8c232014-12-01 15:40:14 -080014858 DRM_DEBUG_KMS("failed to attach phys object\n");
Chris Wilsond07f0e52016-10-28 13:58:44 +010014859 return ret;
14860 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014861 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +010014862 struct i915_vma *vma;
14863
14864 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014865 if (IS_ERR(vma)) {
14866 DRM_DEBUG_KMS("failed to pin object\n");
14867 return PTR_ERR(vma);
14868 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014869 }
14870
Chris Wilsond07f0e52016-10-28 13:58:44 +010014871 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080014872}
14873
Matt Roper38f3ce32014-12-02 07:45:25 -080014874/**
14875 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14876 * @plane: drm plane to clean up for
14877 * @fb: old framebuffer that was on plane
14878 *
14879 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014880 *
14881 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014882 */
14883void
14884intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014885 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014886{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014887 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014888 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014889 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14890 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014891
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014892 old_intel_state = to_intel_plane_state(old_state);
14893
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014894 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014895 return;
14896
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014897 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000014898 !INTEL_INFO(dev_priv)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014899 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Matt Roper465c1202014-05-29 08:06:54 -070014900}
14901
Chandra Konduru6156a452015-04-27 13:48:39 -070014902int
14903skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14904{
14905 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014906 int crtc_clock, cdclk;
14907
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014908 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014909 return DRM_PLANE_HELPER_NO_SCALING;
14910
Chandra Konduru6156a452015-04-27 13:48:39 -070014911 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014912 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014913
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014914 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014915 return DRM_PLANE_HELPER_NO_SCALING;
14916
14917 /*
14918 * skl max scale is lower of:
14919 * close to 3 but not 3, -1 is for that purpose
14920 * or
14921 * cdclk/crtc_clock
14922 */
14923 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14924
14925 return max_scale;
14926}
14927
Matt Roper465c1202014-05-29 08:06:54 -070014928static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014929intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014930 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014931 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014932{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014933 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080014934 struct drm_crtc *crtc = state->base.crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070014935 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014936 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14937 bool can_position = false;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014938 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014939
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014940 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014941 /* use scaler when colorkey is not required */
14942 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14943 min_scale = 1;
14944 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14945 }
Sonika Jindald8106362015-04-10 14:37:28 +053014946 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014947 }
Sonika Jindald8106362015-04-10 14:37:28 +053014948
Daniel Vettercc926382016-08-15 10:41:47 +020014949 ret = drm_plane_helper_check_state(&state->base,
14950 &state->clip,
14951 min_scale, max_scale,
14952 can_position, true);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014953 if (ret)
14954 return ret;
14955
Daniel Vettercc926382016-08-15 10:41:47 +020014956 if (!state->base.fb)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014957 return 0;
14958
14959 if (INTEL_GEN(dev_priv) >= 9) {
14960 ret = skl_check_plane_surface(state);
14961 if (ret)
14962 return ret;
14963 }
14964
14965 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070014966}
14967
Daniel Vetter5a21b662016-05-24 17:13:53 +020014968static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14969 struct drm_crtc_state *old_crtc_state)
14970{
14971 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040014972 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Lyudeb707aa52016-09-15 10:56:06 -040014974 struct intel_crtc_state *intel_cstate =
14975 to_intel_crtc_state(crtc->state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014976 struct intel_crtc_state *old_intel_cstate =
Daniel Vetter5a21b662016-05-24 17:13:53 +020014977 to_intel_crtc_state(old_crtc_state);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014978 struct intel_atomic_state *old_intel_state =
14979 to_intel_atomic_state(old_crtc_state->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014980 bool modeset = needs_modeset(crtc->state);
14981
14982 /* Perform vblank evasion around commit operation */
14983 intel_pipe_update_start(intel_crtc);
14984
14985 if (modeset)
Maarten Lankhorste62929b2016-11-08 13:55:33 +010014986 goto out;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014987
14988 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14989 intel_color_set_csc(crtc->state);
14990 intel_color_load_luts(crtc->state);
14991 }
14992
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014993 if (intel_cstate->update_pipe)
14994 intel_update_pipe_config(intel_crtc, old_intel_cstate);
14995 else if (INTEL_GEN(dev_priv) >= 9)
Daniel Vetter5a21b662016-05-24 17:13:53 +020014996 skl_detach_scalers(intel_crtc);
Lyude62e0fb82016-08-22 12:50:08 -040014997
Maarten Lankhorste62929b2016-11-08 13:55:33 +010014998out:
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010014999 if (dev_priv->display.atomic_update_watermarks)
15000 dev_priv->display.atomic_update_watermarks(old_intel_state,
15001 intel_cstate);
Daniel Vetter5a21b662016-05-24 17:13:53 +020015002}
15003
15004static void intel_finish_crtc_commit(struct drm_crtc *crtc,
15005 struct drm_crtc_state *old_crtc_state)
15006{
15007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15008
15009 intel_pipe_update_end(intel_crtc, NULL);
15010}
15011
Matt Ropercf4c7c12014-12-04 10:27:42 -080015012/**
Matt Roper4a3b8762014-12-23 10:41:51 -080015013 * intel_plane_destroy - destroy a plane
15014 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080015015 *
Matt Roper4a3b8762014-12-23 10:41:51 -080015016 * Common destruction function for all types of planes (primary, cursor,
15017 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080015018 */
Matt Roper4a3b8762014-12-23 10:41:51 -080015019void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070015020{
Matt Roper465c1202014-05-29 08:06:54 -070015021 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030015022 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070015023}
15024
Matt Roper65a3fea2015-01-21 16:35:42 -080015025const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070015026 .update_plane = drm_atomic_helper_update_plane,
15027 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070015028 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080015029 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080015030 .atomic_get_property = intel_plane_atomic_get_property,
15031 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080015032 .atomic_duplicate_state = intel_plane_duplicate_state,
15033 .atomic_destroy_state = intel_plane_destroy_state,
Matt Roper465c1202014-05-29 08:06:54 -070015034};
15035
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015036static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020015037intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070015038{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015039 struct intel_plane *primary = NULL;
15040 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070015041 const uint32_t *intel_primary_formats;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015042 unsigned int supported_rotations;
Thierry Reding45e37432015-08-12 16:54:28 +020015043 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015044 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070015045
15046 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015047 if (!primary) {
15048 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015049 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015050 }
Matt Roper465c1202014-05-29 08:06:54 -070015051
Matt Roper8e7d6882015-01-21 16:35:41 -080015052 state = intel_create_plane_state(&primary->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015053 if (!state) {
15054 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015055 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015056 }
15057
Matt Roper8e7d6882015-01-21 16:35:41 -080015058 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080015059
Matt Roper465c1202014-05-29 08:06:54 -070015060 primary->can_scale = false;
15061 primary->max_downscale = 1;
Ville Syrjälä580503c2016-10-31 22:37:00 +020015062 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru6156a452015-04-27 13:48:39 -070015063 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070015064 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070015065 }
Matt Roper465c1202014-05-29 08:06:54 -070015066 primary->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020015067 /*
15068 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
15069 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
15070 */
15071 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
15072 primary->plane = (enum plane) !pipe;
15073 else
15074 primary->plane = (enum plane) pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020015075 primary->id = PLANE_PRIMARY;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030015076 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080015077 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070015078
Ville Syrjälä580503c2016-10-31 22:37:00 +020015079 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015080 intel_primary_formats = skl_primary_formats;
15081 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015082
15083 primary->update_plane = skylake_update_primary_plane;
15084 primary->disable_plane = skylake_disable_primary_plane;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015085 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015086 intel_primary_formats = i965_primary_formats;
15087 num_formats = ARRAY_SIZE(i965_primary_formats);
15088
15089 primary->update_plane = ironlake_update_primary_plane;
15090 primary->disable_plane = i9xx_disable_primary_plane;
Ville Syrjälä580503c2016-10-31 22:37:00 +020015091 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010015092 intel_primary_formats = i965_primary_formats;
15093 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015094
15095 primary->update_plane = i9xx_update_primary_plane;
15096 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015097 } else {
15098 intel_primary_formats = i8xx_primary_formats;
15099 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015100
15101 primary->update_plane = i9xx_update_primary_plane;
15102 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070015103 }
15104
Ville Syrjälä580503c2016-10-31 22:37:00 +020015105 if (INTEL_GEN(dev_priv) >= 9)
15106 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15107 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015108 intel_primary_formats, num_formats,
15109 DRM_PLANE_TYPE_PRIMARY,
15110 "plane 1%c", pipe_name(pipe));
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015111 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä580503c2016-10-31 22:37:00 +020015112 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15113 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015114 intel_primary_formats, num_formats,
15115 DRM_PLANE_TYPE_PRIMARY,
15116 "primary %c", pipe_name(pipe));
15117 else
Ville Syrjälä580503c2016-10-31 22:37:00 +020015118 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15119 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015120 intel_primary_formats, num_formats,
15121 DRM_PLANE_TYPE_PRIMARY,
15122 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015123 if (ret)
15124 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053015125
Dave Airlie5481e272016-10-25 16:36:13 +100015126 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015127 supported_rotations =
15128 DRM_ROTATE_0 | DRM_ROTATE_90 |
15129 DRM_ROTATE_180 | DRM_ROTATE_270;
Ville Syrjälä4ea7be22016-11-14 18:54:00 +020015130 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
15131 supported_rotations =
15132 DRM_ROTATE_0 | DRM_ROTATE_180 |
15133 DRM_REFLECT_X;
Dave Airlie5481e272016-10-25 16:36:13 +100015134 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015135 supported_rotations =
15136 DRM_ROTATE_0 | DRM_ROTATE_180;
15137 } else {
15138 supported_rotations = DRM_ROTATE_0;
15139 }
15140
Dave Airlie5481e272016-10-25 16:36:13 +100015141 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015142 drm_plane_create_rotation_property(&primary->base,
15143 DRM_ROTATE_0,
15144 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053015145
Matt Roperea2c67b2014-12-23 10:41:52 -080015146 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15147
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015148 return primary;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015149
15150fail:
15151 kfree(state);
15152 kfree(primary);
15153
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015154 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070015155}
15156
Matt Roper3d7d6512014-06-10 08:28:13 -070015157static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030015158intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020015159 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030015160 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070015161{
Matt Roper2b875c22014-12-01 15:40:13 -080015162 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015163 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015164 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015165 unsigned stride;
15166 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015167
Ville Syrjäläf8856a42016-07-26 19:07:00 +030015168 ret = drm_plane_helper_check_state(&state->base,
15169 &state->clip,
15170 DRM_PLANE_HELPER_NO_SCALING,
15171 DRM_PLANE_HELPER_NO_SCALING,
15172 true, true);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015173 if (ret)
15174 return ret;
15175
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015176 /* if we want to turn off the cursor ignore width and height */
15177 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015178 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015179
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015180 /* Check for which cursor types we support */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015181 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15182 state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080015183 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15184 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015185 return -EINVAL;
15186 }
15187
Matt Roperea2c67b2014-12-23 10:41:52 -080015188 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15189 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015190 DRM_DEBUG_KMS("buffer is too small\n");
15191 return -ENOMEM;
15192 }
15193
Ville Syrjäläbae781b2016-11-16 13:33:16 +020015194 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015195 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015196 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015197 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015198
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015199 /*
15200 * There's something wrong with the cursor on CHV pipe C.
15201 * If it straddles the left edge of the screen then
15202 * moving it away from the edge or disabling it often
15203 * results in a pipe underrun, and often that can lead to
15204 * dead pipe (constant underrun reported, and it scans
15205 * out just a solid color). To recover from that, the
15206 * display power well must be turned off and on again.
15207 * Refuse the put the cursor into that compromised position.
15208 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015209 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
Ville Syrjälä936e71e2016-07-26 19:06:59 +030015210 state->base.visible && state->base.crtc_x < 0) {
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015211 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15212 return -EINVAL;
15213 }
15214
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015215 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015216}
15217
Matt Roperf4a2cf22014-12-01 15:40:12 -080015218static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015219intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020015220 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015221{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010015222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15223
15224 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015225 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015226}
15227
15228static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015229intel_update_cursor_plane(struct drm_plane *plane,
15230 const struct intel_crtc_state *crtc_state,
15231 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030015232{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015233 struct drm_crtc *crtc = crtc_state->base.crtc;
15234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015235 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080015236 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080015237 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070015238
Matt Roperf4a2cf22014-12-01 15:40:12 -080015239 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080015240 addr = 0;
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000015241 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
Chris Wilson058d88c2016-08-15 10:49:06 +010015242 addr = i915_gem_object_ggtt_offset(obj, NULL);
Matt Roperf4a2cf22014-12-01 15:40:12 -080015243 else
Gustavo Padovana912f122014-12-01 15:40:10 -080015244 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080015245
Gustavo Padovana912f122014-12-01 15:40:10 -080015246 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015247 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015248}
Gustavo Padovan852e7872014-09-05 17:22:31 -030015249
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015250static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020015251intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070015252{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015253 struct intel_plane *cursor = NULL;
15254 struct intel_plane_state *state = NULL;
15255 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070015256
15257 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015258 if (!cursor) {
15259 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015260 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015261 }
Matt Roper3d7d6512014-06-10 08:28:13 -070015262
Matt Roper8e7d6882015-01-21 16:35:41 -080015263 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015264 if (!state) {
15265 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015266 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015267 }
15268
Matt Roper8e7d6882015-01-21 16:35:41 -080015269 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080015270
Matt Roper3d7d6512014-06-10 08:28:13 -070015271 cursor->can_scale = false;
15272 cursor->max_downscale = 1;
15273 cursor->pipe = pipe;
15274 cursor->plane = pipe;
Ville Syrjäläb14e5842016-11-22 18:01:56 +020015275 cursor->id = PLANE_CURSOR;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030015276 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080015277 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015278 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015279 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070015280
Ville Syrjälä580503c2016-10-31 22:37:00 +020015281 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
15282 0, &intel_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015283 intel_cursor_formats,
15284 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015285 DRM_PLANE_TYPE_CURSOR,
15286 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015287 if (ret)
15288 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015289
Dave Airlie5481e272016-10-25 16:36:13 +100015290 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015291 drm_plane_create_rotation_property(&cursor->base,
15292 DRM_ROTATE_0,
15293 DRM_ROTATE_0 |
15294 DRM_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015295
Ville Syrjälä580503c2016-10-31 22:37:00 +020015296 if (INTEL_GEN(dev_priv) >= 9)
Chandra Konduruaf99ced2015-05-11 14:35:47 -070015297 state->scaler_id = -1;
15298
Matt Roperea2c67b2014-12-23 10:41:52 -080015299 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15300
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015301 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015302
15303fail:
15304 kfree(state);
15305 kfree(cursor);
15306
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015307 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070015308}
15309
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015310static void skl_init_scalers(struct drm_i915_private *dev_priv,
15311 struct intel_crtc *crtc,
15312 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015313{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015314 struct intel_crtc_scaler_state *scaler_state =
15315 &crtc_state->scaler_state;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015316 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015317
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015318 for (i = 0; i < crtc->num_scalers; i++) {
15319 struct intel_scaler *scaler = &scaler_state->scalers[i];
15320
15321 scaler->in_use = 0;
15322 scaler->mode = PS_SCALER_MODE_DYN;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015323 }
15324
15325 scaler_state->scaler_id = -1;
15326}
15327
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015328static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080015329{
15330 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015331 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015332 struct intel_plane *primary = NULL;
15333 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015334 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015335
Daniel Vetter955382f2013-09-19 14:05:45 +020015336 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015337 if (!intel_crtc)
15338 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080015339
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015340 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015341 if (!crtc_state) {
15342 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015343 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015344 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030015345 intel_crtc->config = crtc_state;
15346 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080015347 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015348
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015349 /* initialize shared scalers */
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015350 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015351 if (pipe == PIPE_C)
15352 intel_crtc->num_scalers = 1;
15353 else
15354 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15355
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015356 skl_init_scalers(dev_priv, intel_crtc, crtc_state);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015357 }
15358
Ville Syrjälä580503c2016-10-31 22:37:00 +020015359 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015360 if (IS_ERR(primary)) {
15361 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070015362 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015363 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020015364 intel_crtc->plane_ids_mask |= BIT(primary->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070015365
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015366 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015367 struct intel_plane *plane;
15368
Ville Syrjälä580503c2016-10-31 22:37:00 +020015369 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020015370 if (IS_ERR(plane)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015371 ret = PTR_ERR(plane);
15372 goto fail;
15373 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020015374 intel_crtc->plane_ids_mask |= BIT(plane->id);
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015375 }
15376
Ville Syrjälä580503c2016-10-31 22:37:00 +020015377 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläd2b2cbc2016-11-07 22:20:56 +020015378 if (IS_ERR(cursor)) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015379 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070015380 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015381 }
Ville Syrjäläd97d7b42016-11-22 18:01:57 +020015382 intel_crtc->plane_ids_mask |= BIT(cursor->id);
Matt Roper3d7d6512014-06-10 08:28:13 -070015383
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020015384 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015385 &primary->base, &cursor->base,
15386 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030015387 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070015388 if (ret)
15389 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080015390
Jesse Barnes80824002009-09-10 15:28:06 -070015391 intel_crtc->pipe = pipe;
Ville Syrjäläe3c566d2016-11-08 16:47:11 +020015392 intel_crtc->plane = primary->plane;
Jesse Barnes80824002009-09-10 15:28:06 -070015393
Chris Wilson4b0e3332014-05-30 16:35:26 +030015394 intel_crtc->cursor_base = ~0;
15395 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015396 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030015397
Ville Syrjälä852eb002015-06-24 22:00:07 +030015398 intel_crtc->wm.cxsr_allowed = true;
15399
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080015400 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15401 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015402 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
15403 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080015404
Jesse Barnes79e53942008-11-07 14:24:08 -080015405 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020015406
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000015407 intel_color_init(&intel_crtc->base);
15408
Daniel Vetter87b6b102014-05-15 15:33:46 +020015409 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015410
15411 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070015412
15413fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015414 /*
15415 * drm_mode_config_cleanup() will free up any
15416 * crtcs/planes already initialized.
15417 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015418 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015419 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015420
15421 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015422}
15423
Jesse Barnes752aa882013-10-31 18:55:49 +020015424enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15425{
15426 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015427 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020015428
Rob Clark51fd3712013-11-19 12:10:12 -050015429 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020015430
Ville Syrjäläd3babd32014-11-07 11:16:01 +020015431 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020015432 return INVALID_PIPE;
15433
15434 return to_intel_crtc(encoder->crtc)->pipe;
15435}
15436
Carl Worth08d7b3d2009-04-29 14:43:54 -070015437int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000015438 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070015439{
Carl Worth08d7b3d2009-04-29 14:43:54 -070015440 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040015441 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020015442 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015443
Rob Clark7707e652014-07-17 23:30:04 -040015444 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010015445 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030015446 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015447
Rob Clark7707e652014-07-17 23:30:04 -040015448 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020015449 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015450
Daniel Vetterc05422d2009-08-11 16:05:30 +020015451 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015452}
15453
Daniel Vetter66a92782012-07-12 20:08:18 +020015454static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080015455{
Daniel Vetter66a92782012-07-12 20:08:18 +020015456 struct drm_device *dev = encoder->base.dev;
15457 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080015458 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080015459 int entry = 0;
15460
Damien Lespiaub2784e12014-08-05 11:29:37 +010015461 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020015462 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020015463 index_mask |= (1 << entry);
15464
Jesse Barnes79e53942008-11-07 14:24:08 -080015465 entry++;
15466 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010015467
Jesse Barnes79e53942008-11-07 14:24:08 -080015468 return index_mask;
15469}
15470
Ville Syrjälä646d5772016-10-31 22:37:14 +020015471static bool has_edp_a(struct drm_i915_private *dev_priv)
Chris Wilson4d302442010-12-14 19:21:29 +000015472{
Ville Syrjälä646d5772016-10-31 22:37:14 +020015473 if (!IS_MOBILE(dev_priv))
Chris Wilson4d302442010-12-14 19:21:29 +000015474 return false;
15475
15476 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15477 return false;
15478
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015479 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000015480 return false;
15481
15482 return true;
15483}
15484
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015485static bool intel_crt_present(struct drm_i915_private *dev_priv)
Jesse Barnes84b4e042014-06-25 08:24:29 -070015486{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015487 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau884497e2013-12-03 13:56:23 +000015488 return false;
15489
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015490 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015491 return false;
15492
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015493 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015494 return false;
15495
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015496 if (HAS_PCH_LPT_H(dev_priv) &&
15497 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020015498 return false;
15499
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020015500 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015501 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 +020015502 return false;
15503
Ville Syrjäläe4abb732015-12-01 23:31:33 +020015504 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070015505 return false;
15506
15507 return true;
15508}
15509
Imre Deak8090ba82016-08-10 14:07:33 +030015510void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15511{
15512 int pps_num;
15513 int pps_idx;
15514
15515 if (HAS_DDI(dev_priv))
15516 return;
15517 /*
15518 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15519 * everywhere where registers can be write protected.
15520 */
15521 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15522 pps_num = 2;
15523 else
15524 pps_num = 1;
15525
15526 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15527 u32 val = I915_READ(PP_CONTROL(pps_idx));
15528
15529 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15530 I915_WRITE(PP_CONTROL(pps_idx), val);
15531 }
15532}
15533
Imre Deak44cb7342016-08-10 14:07:29 +030015534static void intel_pps_init(struct drm_i915_private *dev_priv)
15535{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020015536 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
Imre Deak44cb7342016-08-10 14:07:29 +030015537 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15538 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15539 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15540 else
15541 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030015542
15543 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030015544}
15545
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015546static void intel_setup_outputs(struct drm_i915_private *dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -080015547{
Chris Wilson4ef69c72010-09-09 15:14:28 +010015548 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015549 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080015550
Imre Deak44cb7342016-08-10 14:07:29 +030015551 intel_pps_init(dev_priv);
15552
Imre Deak97a824e12016-06-21 11:51:47 +030015553 /*
15554 * intel_edp_init_connector() depends on this completing first, to
15555 * prevent the registeration of both eDP and LVDS and the incorrect
15556 * sharing of the PPS.
15557 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015558 intel_lvds_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015559
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015560 if (intel_crt_present(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015561 intel_crt_init(dev_priv);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015562
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +020015563 if (IS_GEN9_LP(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053015564 /*
15565 * FIXME: Broxton doesn't support port detection via the
15566 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15567 * detect the ports.
15568 */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015569 intel_ddi_init(dev_priv, PORT_A);
15570 intel_ddi_init(dev_priv, PORT_B);
15571 intel_ddi_init(dev_priv, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020015572
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015573 intel_dsi_init(dev_priv);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015574 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015575 int found;
15576
Jesse Barnesde31fac2015-03-06 15:53:32 -080015577 /*
15578 * Haswell uses DDI functions to detect digital outputs.
15579 * On SKL pre-D0 the strap isn't connected, so we assume
15580 * it's there.
15581 */
Ville Syrjälä77179402015-09-18 20:03:35 +030015582 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080015583 /* WaIgnoreDDIAStrap: skl */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015584 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015585 intel_ddi_init(dev_priv, PORT_A);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015586
15587 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15588 * register */
15589 found = I915_READ(SFUSE_STRAP);
15590
15591 if (found & SFUSE_STRAP_DDIB_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015592 intel_ddi_init(dev_priv, PORT_B);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015593 if (found & SFUSE_STRAP_DDIC_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015594 intel_ddi_init(dev_priv, PORT_C);
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015595 if (found & SFUSE_STRAP_DDID_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015596 intel_ddi_init(dev_priv, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015597 /*
15598 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15599 */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015600 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015601 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15602 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15603 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015604 intel_ddi_init(dev_priv, PORT_E);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015605
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015606 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015607 int found;
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000015608 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020015609
Ville Syrjälä646d5772016-10-31 22:37:14 +020015610 if (has_edp_a(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015611 intel_dp_init(dev_priv, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015612
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015613 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080015614 /* PCH SDVOB multiplex with HDMIB */
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015615 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015616 if (!found)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015617 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015618 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015619 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015620 }
15621
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015622 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015623 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015624
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015625 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015626 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015627
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015628 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015629 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015630
Daniel Vetter270b3042012-10-27 15:52:05 +020015631 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015632 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015633 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015634 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010015635
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015636 /*
15637 * The DP_DETECTED bit is the latched state of the DDC
15638 * SDA pin at boot. However since eDP doesn't require DDC
15639 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15640 * eDP ports may have been muxed to an alternate function.
15641 * Thus we can't rely on the DP_DETECTED bit alone to detect
15642 * eDP ports. Consult the VBT as well as DP_DETECTED to
15643 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030015644 *
15645 * Sadly the straps seem to be missing sometimes even for HDMI
15646 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15647 * and VBT for the presence of the port. Additionally we can't
15648 * trust the port type the VBT declares as we've seen at least
15649 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015650 */
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000015651 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015652 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15653 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015654 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015655 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015656 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030015657
Tvrtko Ursulindd11bc12016-11-16 08:55:41 +000015658 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015659 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15660 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015661 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015662 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015663 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053015664
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015665 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015666 /*
15667 * eDP not supported on port D,
15668 * so no need to worry about it
15669 */
15670 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15671 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015672 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015673 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015674 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030015675 }
15676
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015677 intel_dsi_init(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015678 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015679 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080015680
Paulo Zanonie2debe92013-02-18 19:00:27 -030015681 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015682 DRM_DEBUG_KMS("probing SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015683 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015684 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015685 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015686 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015687 }
Ma Ling27185ae2009-08-24 13:50:23 +080015688
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015689 if (!found && IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015690 intel_dp_init(dev_priv, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080015691 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015692
15693 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015694
Paulo Zanonie2debe92013-02-18 19:00:27 -030015695 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015696 DRM_DEBUG_KMS("probing SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015697 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015698 }
Ma Ling27185ae2009-08-24 13:50:23 +080015699
Paulo Zanonie2debe92013-02-18 19:00:27 -030015700 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015701
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015702 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015703 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015704 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015705 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015706 if (IS_G4X(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015707 intel_dp_init(dev_priv, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080015708 }
Ma Ling27185ae2009-08-24 13:50:23 +080015709
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015710 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015711 intel_dp_init(dev_priv, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015712 } else if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015713 intel_dvo_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015714
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +000015715 if (SUPPORTS_TV(dev_priv))
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015716 intel_tv_init(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080015717
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015718 intel_psr_init(dev_priv);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070015719
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015720 for_each_intel_encoder(&dev_priv->drm, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010015721 encoder->base.possible_crtcs = encoder->crtc_mask;
15722 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020015723 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080015724 }
Chris Wilson47356eb2011-01-11 17:06:04 +000015725
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015726 intel_init_pch_refclk(dev_priv);
Daniel Vetter270b3042012-10-27 15:52:05 +020015727
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020015728 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
Jesse Barnes79e53942008-11-07 14:24:08 -080015729}
15730
15731static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15732{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015733 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080015734 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080015735
Daniel Vetteref2d6332014-02-10 18:00:38 +010015736 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015737 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010015738 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010015739 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015740 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015741 kfree(intel_fb);
15742}
15743
15744static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000015745 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080015746 unsigned int *handle)
15747{
15748 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000015749 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080015750
Chris Wilsoncc917ab2015-10-13 14:22:26 +010015751 if (obj->userptr.mm) {
15752 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15753 return -EINVAL;
15754 }
15755
Chris Wilson05394f32010-11-08 19:18:58 +000015756 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080015757}
15758
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015759static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15760 struct drm_file *file,
15761 unsigned flags, unsigned color,
15762 struct drm_clip_rect *clips,
15763 unsigned num_clips)
15764{
15765 struct drm_device *dev = fb->dev;
15766 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15767 struct drm_i915_gem_object *obj = intel_fb->obj;
15768
15769 mutex_lock(&dev->struct_mutex);
Chris Wilsona6a7cc42016-11-18 21:17:46 +000015770 if (obj->pin_display && obj->cache_dirty)
15771 i915_gem_clflush_object(obj, true);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030015772 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015773 mutex_unlock(&dev->struct_mutex);
15774
15775 return 0;
15776}
15777
Jesse Barnes79e53942008-11-07 14:24:08 -080015778static const struct drm_framebuffer_funcs intel_fb_funcs = {
15779 .destroy = intel_user_framebuffer_destroy,
15780 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015781 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080015782};
15783
Damien Lespiaub3218032015-02-27 11:15:18 +000015784static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015785u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15786 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000015787{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015788 u32 gen = INTEL_INFO(dev_priv)->gen;
Damien Lespiaub3218032015-02-27 11:15:18 +000015789
15790 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020015791 int cpp = drm_format_plane_cpp(pixel_format, 0);
15792
Damien Lespiaub3218032015-02-27 11:15:18 +000015793 /* "The stride in bytes must not exceed the of the size of 8K
15794 * pixels and 32K bytes."
15795 */
Ville Syrjäläac484962016-01-20 21:05:26 +020015796 return min(8192 * cpp, 32768);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015797 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15798 !IS_CHERRYVIEW(dev_priv)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015799 return 32*1024;
15800 } else if (gen >= 4) {
15801 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15802 return 16*1024;
15803 else
15804 return 32*1024;
15805 } else if (gen >= 3) {
15806 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15807 return 8*1024;
15808 else
15809 return 16*1024;
15810 } else {
15811 /* XXX DSPC is limited to 4k tiled */
15812 return 8*1024;
15813 }
15814}
15815
Daniel Vetterb5ea6422014-03-02 21:18:00 +010015816static int intel_framebuffer_init(struct drm_device *dev,
15817 struct intel_framebuffer *intel_fb,
15818 struct drm_mode_fb_cmd2 *mode_cmd,
15819 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015820{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015821 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015822 unsigned int tiling = i915_gem_object_get_tiling(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080015823 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015824 u32 pitch_limit, stride_alignment;
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015825 struct drm_format_name_buf format_name;
Jesse Barnes79e53942008-11-07 14:24:08 -080015826
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015827 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15828
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015829 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015830 /*
15831 * If there's a fence, enforce that
15832 * the fb modifier and tiling mode match.
15833 */
15834 if (tiling != I915_TILING_NONE &&
15835 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015836 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15837 return -EINVAL;
15838 }
15839 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015840 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015841 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015842 } else if (tiling == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015843 DRM_DEBUG("No Y tiling for legacy addfb\n");
15844 return -EINVAL;
15845 }
15846 }
15847
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015848 /* Passed in modifier sanity checking. */
15849 switch (mode_cmd->modifier[0]) {
15850 case I915_FORMAT_MOD_Y_TILED:
15851 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015852 if (INTEL_GEN(dev_priv) < 9) {
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015853 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15854 mode_cmd->modifier[0]);
15855 return -EINVAL;
15856 }
15857 case DRM_FORMAT_MOD_NONE:
15858 case I915_FORMAT_MOD_X_TILED:
15859 break;
15860 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015861 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15862 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015863 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015864 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015865
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015866 /*
15867 * gen2/3 display engine uses the fence if present,
15868 * so the tiling mode must match the fb modifier exactly.
15869 */
15870 if (INTEL_INFO(dev_priv)->gen < 4 &&
15871 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15872 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15873 return -EINVAL;
15874 }
15875
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015876 stride_alignment = intel_fb_stride_alignment(dev_priv,
15877 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015878 mode_cmd->pixel_format);
15879 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15880 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15881 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015882 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015883 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015884
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015885 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015886 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015887 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015888 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15889 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015890 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015891 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015892 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015893 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015894
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015895 /*
15896 * If there's a fence, enforce that
15897 * the fb pitch and fence stride match.
15898 */
15899 if (tiling != I915_TILING_NONE &&
Chris Wilson3e510a82016-08-05 10:14:23 +010015900 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015901 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilson3e510a82016-08-05 10:14:23 +010015902 mode_cmd->pitches[0],
15903 i915_gem_object_get_stride(obj));
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015904 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015905 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015906
Ville Syrjälä57779d02012-10-31 17:50:14 +020015907 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015908 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015909 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015910 case DRM_FORMAT_RGB565:
15911 case DRM_FORMAT_XRGB8888:
15912 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015913 break;
15914 case DRM_FORMAT_XRGB1555:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015915 if (INTEL_GEN(dev_priv) > 3) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015916 DRM_DEBUG("unsupported pixel format: %s\n",
15917 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015918 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015919 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015920 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015921 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015922 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015923 INTEL_GEN(dev_priv) < 9) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015924 DRM_DEBUG("unsupported pixel format: %s\n",
15925 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015926 return -EINVAL;
15927 }
15928 break;
15929 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015930 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015931 case DRM_FORMAT_XBGR2101010:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015932 if (INTEL_GEN(dev_priv) < 4) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015933 DRM_DEBUG("unsupported pixel format: %s\n",
15934 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015935 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015936 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015937 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015938 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015939 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015940 DRM_DEBUG("unsupported pixel format: %s\n",
15941 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Damien Lespiau75312082015-05-15 19:06:01 +010015942 return -EINVAL;
15943 }
15944 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015945 case DRM_FORMAT_YUYV:
15946 case DRM_FORMAT_UYVY:
15947 case DRM_FORMAT_YVYU:
15948 case DRM_FORMAT_VYUY:
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000015949 if (INTEL_GEN(dev_priv) < 5) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015950 DRM_DEBUG("unsupported pixel format: %s\n",
15951 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015952 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015953 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015954 break;
15955 default:
Eric Engestromb3c11ac2016-11-12 01:12:56 +000015956 DRM_DEBUG("unsupported pixel format: %s\n",
15957 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Chris Wilson57cd6502010-08-08 12:34:44 +010015958 return -EINVAL;
15959 }
15960
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015961 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15962 if (mode_cmd->offsets[0] != 0)
15963 return -EINVAL;
15964
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015965 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15966 intel_fb->obj = obj;
15967
Ville Syrjälä6687c902015-09-15 13:16:41 +030015968 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15969 if (ret)
15970 return ret;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015971
Jesse Barnes79e53942008-11-07 14:24:08 -080015972 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15973 if (ret) {
15974 DRM_ERROR("framebuffer init failed %d\n", ret);
15975 return ret;
15976 }
15977
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015978 intel_fb->obj->framebuffer_references++;
15979
Jesse Barnes79e53942008-11-07 14:24:08 -080015980 return 0;
15981}
15982
Jesse Barnes79e53942008-11-07 14:24:08 -080015983static struct drm_framebuffer *
15984intel_user_framebuffer_create(struct drm_device *dev,
15985 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020015986 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015987{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015988 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015989 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015990 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015991
Chris Wilson03ac0642016-07-20 13:31:51 +010015992 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15993 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015994 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015995
Daniel Vetter92907cb2015-11-23 09:04:05 +010015996 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015997 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010015998 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015999
16000 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080016001}
16002
Jesse Barnes79e53942008-11-07 14:24:08 -080016003static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080016004 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020016005 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080016006 .atomic_check = intel_atomic_check,
16007 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020016008 .atomic_state_alloc = intel_atomic_state_alloc,
16009 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080016010};
16011
Imre Deak88212942016-03-16 13:38:53 +020016012/**
16013 * intel_init_display_hooks - initialize the display modesetting hooks
16014 * @dev_priv: device private
16015 */
16016void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070016017{
Imre Deak88212942016-03-16 13:38:53 +020016018 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000016019 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000016020 dev_priv->display.get_initial_plane_config =
16021 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000016022 dev_priv->display.crtc_compute_clock =
16023 haswell_crtc_compute_clock;
16024 dev_priv->display.crtc_enable = haswell_crtc_enable;
16025 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020016026 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010016027 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000016028 dev_priv->display.get_initial_plane_config =
16029 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020016030 dev_priv->display.crtc_compute_clock =
16031 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020016032 dev_priv->display.crtc_enable = haswell_crtc_enable;
16033 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020016034 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010016035 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000016036 dev_priv->display.get_initial_plane_config =
16037 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020016038 dev_priv->display.crtc_compute_clock =
16039 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020016040 dev_priv->display.crtc_enable = ironlake_crtc_enable;
16041 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020016042 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070016043 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000016044 dev_priv->display.get_initial_plane_config =
16045 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020016046 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
16047 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16048 dev_priv->display.crtc_disable = i9xx_crtc_disable;
16049 } else if (IS_VALLEYVIEW(dev_priv)) {
16050 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16051 dev_priv->display.get_initial_plane_config =
16052 i9xx_get_initial_plane_config;
16053 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070016054 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16055 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020016056 } else if (IS_G4X(dev_priv)) {
16057 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16058 dev_priv->display.get_initial_plane_config =
16059 i9xx_get_initial_plane_config;
16060 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
16061 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16062 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020016063 } else if (IS_PINEVIEW(dev_priv)) {
16064 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16065 dev_priv->display.get_initial_plane_config =
16066 i9xx_get_initial_plane_config;
16067 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
16068 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16069 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020016070 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010016071 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000016072 dev_priv->display.get_initial_plane_config =
16073 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020016074 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020016075 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16076 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020016077 } else {
16078 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16079 dev_priv->display.get_initial_plane_config =
16080 i9xx_get_initial_plane_config;
16081 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
16082 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16083 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070016084 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016085
Jesse Barnese70236a2009-09-21 10:42:27 -070016086 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020016087 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016088 dev_priv->display.get_display_clock_speed =
16089 skylake_get_display_clock_speed;
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +020016090 else if (IS_GEN9_LP(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070016091 dev_priv->display.get_display_clock_speed =
16092 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016093 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016094 dev_priv->display.get_display_clock_speed =
16095 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016096 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016097 dev_priv->display.get_display_clock_speed =
16098 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016099 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070016100 dev_priv->display.get_display_clock_speed =
16101 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016102 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030016103 dev_priv->display.get_display_clock_speed =
16104 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016105 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
16106 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016107 dev_priv->display.get_display_clock_speed =
16108 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016109 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016110 dev_priv->display.get_display_clock_speed =
16111 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016112 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016113 dev_priv->display.get_display_clock_speed =
16114 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016115 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016116 dev_priv->display.get_display_clock_speed =
16117 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016118 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016119 dev_priv->display.get_display_clock_speed =
16120 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016121 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016122 dev_priv->display.get_display_clock_speed =
16123 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016124 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016125 dev_priv->display.get_display_clock_speed =
16126 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016127 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016128 dev_priv->display.get_display_clock_speed =
16129 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016130 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016131 dev_priv->display.get_display_clock_speed =
16132 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016133 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016134 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030016135 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016136 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020016137 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070016138 dev_priv->display.get_display_clock_speed =
16139 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016140 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016141
Imre Deak88212942016-03-16 13:38:53 +020016142 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016143 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016144 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016145 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016146 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016147 /* FIXME: detect B0+ stepping and use auto training */
16148 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016149 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016150 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030016151 }
16152
16153 if (IS_BROADWELL(dev_priv)) {
16154 dev_priv->display.modeset_commit_cdclk =
16155 broadwell_modeset_commit_cdclk;
16156 dev_priv->display.modeset_calc_cdclk =
16157 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020016158 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016159 dev_priv->display.modeset_commit_cdclk =
16160 valleyview_modeset_commit_cdclk;
16161 dev_priv->display.modeset_calc_cdclk =
16162 valleyview_modeset_calc_cdclk;
Ander Conselvan de Oliveira89b3c3c2016-12-02 10:23:54 +020016163 } else if (IS_GEN9_LP(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016164 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016165 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016166 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016167 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030016168 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16169 dev_priv->display.modeset_commit_cdclk =
16170 skl_modeset_commit_cdclk;
16171 dev_priv->display.modeset_calc_cdclk =
16172 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070016173 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020016174
Lyude27082492016-08-24 07:48:10 +020016175 if (dev_priv->info.gen >= 9)
16176 dev_priv->display.update_crtcs = skl_update_crtcs;
16177 else
16178 dev_priv->display.update_crtcs = intel_update_crtcs;
16179
Daniel Vetter5a21b662016-05-24 17:13:53 +020016180 switch (INTEL_INFO(dev_priv)->gen) {
16181 case 2:
16182 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16183 break;
16184
16185 case 3:
16186 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16187 break;
16188
16189 case 4:
16190 case 5:
16191 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16192 break;
16193
16194 case 6:
16195 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16196 break;
16197 case 7:
16198 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16199 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16200 break;
16201 case 9:
16202 /* Drop through - unsupported since execlist only. */
16203 default:
16204 /* Default just returns -ENODEV to indicate unsupported */
16205 dev_priv->display.queue_flip = intel_default_queue_flip;
16206 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016207}
16208
Jesse Barnesb690e962010-07-19 13:53:12 -070016209/*
16210 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16211 * resume, or other times. This quirk makes sure that's the case for
16212 * affected systems.
16213 */
Akshay Joshi0206e352011-08-16 15:34:10 -040016214static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070016215{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016216 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070016217
16218 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016219 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016220}
16221
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016222static void quirk_pipeb_force(struct drm_device *dev)
16223{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016224 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016225
16226 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16227 DRM_INFO("applying pipe b force quirk\n");
16228}
16229
Keith Packard435793d2011-07-12 14:56:22 -070016230/*
16231 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16232 */
16233static void quirk_ssc_force_disable(struct drm_device *dev)
16234{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016235 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070016236 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016237 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070016238}
16239
Carsten Emde4dca20e2012-03-15 15:56:26 +010016240/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010016241 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16242 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010016243 */
16244static void quirk_invert_brightness(struct drm_device *dev)
16245{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016246 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010016247 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016248 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016249}
16250
Scot Doyle9c72cc62014-07-03 23:27:50 +000016251/* Some VBT's incorrectly indicate no backlight is present */
16252static void quirk_backlight_present(struct drm_device *dev)
16253{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016254 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000016255 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16256 DRM_INFO("applying backlight present quirk\n");
16257}
16258
Jesse Barnesb690e962010-07-19 13:53:12 -070016259struct intel_quirk {
16260 int device;
16261 int subsystem_vendor;
16262 int subsystem_device;
16263 void (*hook)(struct drm_device *dev);
16264};
16265
Egbert Eich5f85f172012-10-14 15:46:38 +020016266/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16267struct intel_dmi_quirk {
16268 void (*hook)(struct drm_device *dev);
16269 const struct dmi_system_id (*dmi_id_list)[];
16270};
16271
16272static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16273{
16274 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16275 return 1;
16276}
16277
16278static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16279 {
16280 .dmi_id_list = &(const struct dmi_system_id[]) {
16281 {
16282 .callback = intel_dmi_reverse_brightness,
16283 .ident = "NCR Corporation",
16284 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16285 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16286 },
16287 },
16288 { } /* terminating entry */
16289 },
16290 .hook = quirk_invert_brightness,
16291 },
16292};
16293
Ben Widawskyc43b5632012-04-16 14:07:40 -070016294static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070016295 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16296 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16297
Jesse Barnesb690e962010-07-19 13:53:12 -070016298 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16299 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16300
Ville Syrjälä5f080c02014-08-15 01:22:06 +030016301 /* 830 needs to leave pipe A & dpll A up */
16302 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16303
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016304 /* 830 needs to leave pipe B & dpll B up */
16305 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16306
Keith Packard435793d2011-07-12 14:56:22 -070016307 /* Lenovo U160 cannot use SSC on LVDS */
16308 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020016309
16310 /* Sony Vaio Y cannot use SSC on LVDS */
16311 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010016312
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010016313 /* Acer Aspire 5734Z must invert backlight brightness */
16314 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16315
16316 /* Acer/eMachines G725 */
16317 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16318
16319 /* Acer/eMachines e725 */
16320 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16321
16322 /* Acer/Packard Bell NCL20 */
16323 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16324
16325 /* Acer Aspire 4736Z */
16326 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020016327
16328 /* Acer Aspire 5336 */
16329 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000016330
16331 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16332 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000016333
Scot Doyledfb3d47b2014-08-21 16:08:02 +000016334 /* Acer C720 Chromebook (Core i3 4005U) */
16335 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16336
jens steinb2a96012014-10-28 20:25:53 +010016337 /* Apple Macbook 2,1 (Core 2 T7400) */
16338 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16339
Jani Nikula1b9448b02015-11-05 11:49:59 +020016340 /* Apple Macbook 4,1 */
16341 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16342
Scot Doyled4967d82014-07-03 23:27:52 +000016343 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16344 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000016345
16346 /* HP Chromebook 14 (Celeron 2955U) */
16347 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020016348
16349 /* Dell Chromebook 11 */
16350 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020016351
16352 /* Dell Chromebook 11 (2015 version) */
16353 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070016354};
16355
16356static void intel_init_quirks(struct drm_device *dev)
16357{
16358 struct pci_dev *d = dev->pdev;
16359 int i;
16360
16361 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16362 struct intel_quirk *q = &intel_quirks[i];
16363
16364 if (d->device == q->device &&
16365 (d->subsystem_vendor == q->subsystem_vendor ||
16366 q->subsystem_vendor == PCI_ANY_ID) &&
16367 (d->subsystem_device == q->subsystem_device ||
16368 q->subsystem_device == PCI_ANY_ID))
16369 q->hook(dev);
16370 }
Egbert Eich5f85f172012-10-14 15:46:38 +020016371 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16372 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16373 intel_dmi_quirks[i].hook(dev);
16374 }
Jesse Barnesb690e962010-07-19 13:53:12 -070016375}
16376
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016377/* Disable the VGA plane that we never use */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016378static void i915_disable_vga(struct drm_i915_private *dev_priv)
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016379{
David Weinehall52a05c32016-08-22 13:32:44 +030016380 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016381 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016382 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016383
Ville Syrjälä2b37c612014-01-22 21:32:38 +020016384 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030016385 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070016386 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016387 sr1 = inb(VGA_SR_DATA);
16388 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030016389 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016390 udelay(300);
16391
Ville Syrjälä01f5a622014-12-16 18:38:37 +020016392 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016393 POSTING_READ(vga_reg);
16394}
16395
Daniel Vetterf8175862012-04-10 15:50:11 +020016396void intel_modeset_init_hw(struct drm_device *dev)
16397{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016398 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016399
Ville Syrjälä4c75b942016-10-31 22:37:12 +020016400 intel_update_cdclk(dev_priv);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016401
16402 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16403
Ville Syrjälä46f16e62016-10-31 22:37:22 +020016404 intel_init_clock_gating(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020016405}
16406
Matt Roperd93c0372015-12-03 11:37:41 -080016407/*
16408 * Calculate what we think the watermarks should be for the state we've read
16409 * out of the hardware and then immediately program those watermarks so that
16410 * we ensure the hardware settings match our internal state.
16411 *
16412 * We can calculate what we think WM's should be by creating a duplicate of the
16413 * current state (which was constructed during hardware readout) and running it
16414 * through the atomic check code to calculate new watermark values in the
16415 * state object.
16416 */
16417static void sanitize_watermarks(struct drm_device *dev)
16418{
16419 struct drm_i915_private *dev_priv = to_i915(dev);
16420 struct drm_atomic_state *state;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016421 struct intel_atomic_state *intel_state;
Matt Roperd93c0372015-12-03 11:37:41 -080016422 struct drm_crtc *crtc;
16423 struct drm_crtc_state *cstate;
16424 struct drm_modeset_acquire_ctx ctx;
16425 int ret;
16426 int i;
16427
16428 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080016429 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080016430 return;
16431
16432 /*
16433 * We need to hold connection_mutex before calling duplicate_state so
16434 * that the connector loop is protected.
16435 */
16436 drm_modeset_acquire_init(&ctx, 0);
16437retry:
Matt Roper0cd12622016-01-12 07:13:37 -080016438 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080016439 if (ret == -EDEADLK) {
16440 drm_modeset_backoff(&ctx);
16441 goto retry;
16442 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080016443 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016444 }
16445
16446 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16447 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080016448 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016449
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016450 intel_state = to_intel_atomic_state(state);
16451
Matt Ropered4a6a72016-02-23 17:20:13 -080016452 /*
16453 * Hardware readout is the only time we don't want to calculate
16454 * intermediate watermarks (since we don't trust the current
16455 * watermarks).
16456 */
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016457 intel_state->skip_intermediate_wm = true;
Matt Ropered4a6a72016-02-23 17:20:13 -080016458
Matt Roperd93c0372015-12-03 11:37:41 -080016459 ret = intel_atomic_check(dev, state);
16460 if (ret) {
16461 /*
16462 * If we fail here, it means that the hardware appears to be
16463 * programmed in a way that shouldn't be possible, given our
16464 * understanding of watermark requirements. This might mean a
16465 * mistake in the hardware readout code or a mistake in the
16466 * watermark calculations for a given platform. Raise a WARN
16467 * so that this is noticeable.
16468 *
16469 * If this actually happens, we'll have to just leave the
16470 * BIOS-programmed watermarks untouched and hope for the best.
16471 */
16472 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016473 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080016474 }
16475
16476 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080016477 for_each_crtc_in_state(state, crtc, cstate, i) {
16478 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16479
Matt Ropered4a6a72016-02-23 17:20:13 -080016480 cs->wm.need_postvbl_update = true;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +010016481 dev_priv->display.optimize_watermarks(intel_state, cs);
Matt Roperd93c0372015-12-03 11:37:41 -080016482 }
16483
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016484put_state:
Chris Wilson08536952016-10-14 13:18:18 +010016485 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080016486fail:
Matt Roperd93c0372015-12-03 11:37:41 -080016487 drm_modeset_drop_locks(&ctx);
16488 drm_modeset_acquire_fini(&ctx);
16489}
16490
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016491int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080016492{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016493 struct drm_i915_private *dev_priv = to_i915(dev);
16494 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000016495 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080016496 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080016497
16498 drm_mode_config_init(dev);
16499
16500 dev->mode_config.min_width = 0;
16501 dev->mode_config.min_height = 0;
16502
Dave Airlie019d96c2011-09-29 16:20:42 +010016503 dev->mode_config.preferred_depth = 24;
16504 dev->mode_config.prefer_shadow = 1;
16505
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000016506 dev->mode_config.allow_fb_modifiers = true;
16507
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020016508 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080016509
Jesse Barnesb690e962010-07-19 13:53:12 -070016510 intel_init_quirks(dev);
16511
Ville Syrjälä62d75df2016-10-31 22:37:25 +020016512 intel_init_pm(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030016513
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016514 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016515 return 0;
Ben Widawskye3c74752013-04-05 13:12:39 -070016516
Lukas Wunner69f92f62015-07-15 13:57:35 +020016517 /*
16518 * There may be no VBT; and if the BIOS enabled SSC we can
16519 * just keep using it to avoid unnecessary flicker. Whereas if the
16520 * BIOS isn't using it, don't assume it will work even if the VBT
16521 * indicates as much.
16522 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010016523 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020016524 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16525 DREF_SSC1_ENABLE);
16526
16527 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16528 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16529 bios_lvds_use_ssc ? "en" : "dis",
16530 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16531 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16532 }
16533 }
16534
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016535 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016536 dev->mode_config.max_width = 2048;
16537 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016538 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070016539 dev->mode_config.max_width = 4096;
16540 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080016541 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016542 dev->mode_config.max_width = 8192;
16543 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080016544 }
Damien Lespiau068be562014-03-28 14:17:49 +000016545
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010016546 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16547 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030016548 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016549 } else if (IS_GEN2(dev_priv)) {
Damien Lespiau068be562014-03-28 14:17:49 +000016550 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16551 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16552 } else {
16553 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16554 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16555 }
16556
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016557 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080016558
Zhao Yakui28c97732009-10-09 11:39:41 +080016559 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016560 INTEL_INFO(dev_priv)->num_pipes,
16561 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080016562
Damien Lespiau055e3932014-08-18 13:49:10 +010016563 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016564 int ret;
16565
Ville Syrjälä5ab0d852016-10-31 22:37:11 +020016566 ret = intel_crtc_init(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016567 if (ret) {
16568 drm_mode_config_cleanup(dev);
16569 return ret;
16570 }
Jesse Barnes79e53942008-11-07 14:24:08 -080016571 }
16572
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030016573 intel_update_czclk(dev_priv);
Ville Syrjälä4c75b942016-10-31 22:37:12 +020016574 intel_update_cdclk(dev_priv);
Ville Syrjälä6a259b12016-11-29 16:13:57 +020016575 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030016576
Daniel Vettere72f9fb2013-06-05 13:34:06 +020016577 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010016578
Ville Syrjäläb2045352016-05-13 23:41:27 +030016579 if (dev_priv->max_cdclk_freq == 0)
Ville Syrjälä4c75b942016-10-31 22:37:12 +020016580 intel_update_max_cdclk(dev_priv);
Ville Syrjäläb2045352016-05-13 23:41:27 +030016581
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016582 /* Just disable it once at startup */
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016583 i915_disable_vga(dev_priv);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +020016584 intel_setup_outputs(dev_priv);
Chris Wilson11be49e2012-11-15 11:32:20 +000016585
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016586 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016587 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016588 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016589
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016590 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016591 struct intel_initial_plane_config plane_config = {};
16592
Jesse Barnes46f297f2014-03-07 08:57:48 -080016593 if (!crtc->active)
16594 continue;
16595
Jesse Barnes46f297f2014-03-07 08:57:48 -080016596 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080016597 * Note that reserving the BIOS fb up front prevents us
16598 * from stuffing other stolen allocations like the ring
16599 * on top. This prevents some ugliness at boot time, and
16600 * can even allow for smooth boot transitions if the BIOS
16601 * fb is large enough for the active pipe configuration.
16602 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016603 dev_priv->display.get_initial_plane_config(crtc,
16604 &plane_config);
16605
16606 /*
16607 * If the fb is shared between multiple heads, we'll
16608 * just get the first one.
16609 */
16610 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016611 }
Matt Roperd93c0372015-12-03 11:37:41 -080016612
16613 /*
16614 * Make sure hardware watermarks really match the state we read out.
16615 * Note that we need to do this after reconstructing the BIOS fb's
16616 * since the watermark calculation done here will use pstate->fb.
16617 */
16618 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016619
16620 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010016621}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080016622
Daniel Vetter7fad7982012-07-04 17:51:47 +020016623static void intel_enable_pipe_a(struct drm_device *dev)
16624{
16625 struct intel_connector *connector;
16626 struct drm_connector *crt = NULL;
16627 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016628 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020016629
16630 /* We can't just switch on the pipe A, we need to set things up with a
16631 * proper mode and output configuration. As a gross hack, enable pipe A
16632 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016633 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020016634 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16635 crt = &connector->base;
16636 break;
16637 }
16638 }
16639
16640 if (!crt)
16641 return;
16642
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016643 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020016644 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020016645}
16646
Daniel Vetterfa555832012-10-10 23:14:00 +020016647static bool
16648intel_check_plane_mapping(struct intel_crtc *crtc)
16649{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016650 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030016651 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020016652
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000016653 if (INTEL_INFO(dev_priv)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020016654 return true;
16655
Ville Syrjälä649636e2015-09-22 19:50:01 +030016656 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020016657
16658 if ((val & DISPLAY_PLANE_ENABLE) &&
16659 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16660 return false;
16661
16662 return true;
16663}
16664
Ville Syrjälä02e93c32015-08-26 19:39:19 +030016665static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16666{
16667 struct drm_device *dev = crtc->base.dev;
16668 struct intel_encoder *encoder;
16669
16670 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16671 return true;
16672
16673 return false;
16674}
16675
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016676static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16677{
16678 struct drm_device *dev = encoder->base.dev;
16679 struct intel_connector *connector;
16680
16681 for_each_connector_on_encoder(dev, &encoder->base, connector)
16682 return connector;
16683
16684 return NULL;
16685}
16686
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016687static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16688 enum transcoder pch_transcoder)
16689{
16690 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16691 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16692}
16693
Daniel Vetter24929352012-07-02 20:28:59 +020016694static void intel_sanitize_crtc(struct intel_crtc *crtc)
16695{
16696 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010016697 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020016698 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020016699
Daniel Vetter24929352012-07-02 20:28:59 +020016700 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020016701 if (!transcoder_is_dsi(cpu_transcoder)) {
16702 i915_reg_t reg = PIPECONF(cpu_transcoder);
16703
16704 I915_WRITE(reg,
16705 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16706 }
Daniel Vetter24929352012-07-02 20:28:59 +020016707
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016708 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010016709 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030016710 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016711 struct intel_plane *plane;
16712
Daniel Vetter96256042015-02-13 21:03:42 +010016713 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016714
16715 /* Disable everything but the primary plane */
16716 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16717 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16718 continue;
16719
16720 plane->disable_plane(&plane->base, &crtc->base);
16721 }
Daniel Vetter96256042015-02-13 21:03:42 +010016722 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016723
Daniel Vetter24929352012-07-02 20:28:59 +020016724 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020016725 * disable the crtc (and hence change the state) if it is wrong. Note
16726 * that gen4+ has a fixed plane -> pipe mapping. */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000016727 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020016728 bool plane;
16729
Ville Syrjälä78108b72016-05-27 20:59:19 +030016730 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16731 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016732
16733 /* Pipe has the wrong plane attached and the plane is active.
16734 * Temporarily change the plane mapping and disable everything
16735 * ... */
16736 plane = crtc->plane;
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016737 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020016738 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016739 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016740 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020016741 }
Daniel Vetter24929352012-07-02 20:28:59 +020016742
Daniel Vetter7fad7982012-07-04 17:51:47 +020016743 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16744 crtc->pipe == PIPE_A && !crtc->active) {
16745 /* BIOS forgot to enable pipe A, this mostly happens after
16746 * resume. Force-enable the pipe to fix this, the update_dpms
16747 * call below we restore the pipe to the right state, but leave
16748 * the required bits on. */
16749 intel_enable_pipe_a(dev);
16750 }
16751
Daniel Vetter24929352012-07-02 20:28:59 +020016752 /* Adjust the state of the output pipe according to whether we
16753 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010016754 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016755 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016756
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010016757 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010016758 /*
16759 * We start out with underrun reporting disabled to avoid races.
16760 * For correct bookkeeping mark this on active crtcs.
16761 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020016762 * Also on gmch platforms we dont have any hardware bits to
16763 * disable the underrun reporting. Which means we need to start
16764 * out with underrun reporting disabled also on inactive pipes,
16765 * since otherwise we'll complain about the garbage we read when
16766 * e.g. coming up after runtime pm.
16767 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010016768 * No protection against concurrent access is required - at
16769 * worst a fifo underrun happens which also sets this to false.
16770 */
16771 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016772 /*
16773 * We track the PCH trancoder underrun reporting state
16774 * within the crtc. With crtc for pipe A housing the underrun
16775 * reporting state for PCH transcoder A, crtc for pipe B housing
16776 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16777 * and marking underrun reporting as disabled for the non-existing
16778 * PCH transcoders B and C would prevent enabling the south
16779 * error interrupt (see cpt_can_enable_serr_int()).
16780 */
16781 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16782 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010016783 }
Daniel Vetter24929352012-07-02 20:28:59 +020016784}
16785
16786static void intel_sanitize_encoder(struct intel_encoder *encoder)
16787{
16788 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020016789
16790 /* We need to check both for a crtc link (meaning that the
16791 * encoder is active and trying to read from a pipe) and the
16792 * pipe itself being active. */
16793 bool has_active_crtc = encoder->base.crtc &&
16794 to_intel_crtc(encoder->base.crtc)->active;
16795
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016796 connector = intel_encoder_find_connector(encoder);
16797 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020016798 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16799 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016800 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016801
16802 /* Connector is active, but has no active pipe. This is
16803 * fallout from our resume register restoring. Disable
16804 * the encoder manually again. */
16805 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016806 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16807
Daniel Vetter24929352012-07-02 20:28:59 +020016808 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16809 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016810 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016811 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030016812 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016813 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020016814 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020016815 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016816
16817 /* Inconsistent output/port/pipe state happens presumably due to
16818 * a bug in one of the get_hw_state functions. Or someplace else
16819 * in our code, like the register restore mess on resume. Clamp
16820 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016821
16822 connector->base.dpms = DRM_MODE_DPMS_OFF;
16823 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016824 }
16825 /* Enabled encoders without active connectors will be fixed in
16826 * the crtc fixup. */
16827}
16828
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016829void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016830{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016831 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016832
Imre Deak04098752014-02-18 00:02:16 +020016833 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16834 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016835 i915_disable_vga(dev_priv);
Imre Deak04098752014-02-18 00:02:16 +020016836 }
16837}
16838
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016839void i915_redisable_vga(struct drm_i915_private *dev_priv)
Imre Deak04098752014-02-18 00:02:16 +020016840{
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016841 /* This function can be called both from intel_modeset_setup_hw_state or
16842 * at a very early point in our resume sequence, where the power well
16843 * structures are not yet restored. Since this function is at a very
16844 * paranoid "someone might have enabled VGA while we were not looking"
16845 * level, just check if the power well is enabled instead of trying to
16846 * follow the "don't touch the power well if we don't need it" policy
16847 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016848 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016849 return;
16850
Tvrtko Ursulin29b74b72016-11-16 08:55:39 +000016851 i915_redisable_vga_power_on(dev_priv);
Imre Deak6392f842016-02-12 18:55:13 +020016852
16853 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016854}
16855
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016856static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016857{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016858 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016859
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016860 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016861}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016862
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016863/* FIXME read out full plane state for all planes */
16864static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016865{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016866 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016867 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016868 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016869
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016870 plane_state->base.visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016871 primary_get_hw_state(to_intel_plane(primary));
16872
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016873 if (plane_state->base.visible)
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016874 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016875}
16876
Daniel Vetter30e984d2013-06-05 13:34:17 +020016877static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016878{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016879 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016880 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016881 struct intel_crtc *crtc;
16882 struct intel_encoder *encoder;
16883 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016884 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016885
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016886 dev_priv->active_crtcs = 0;
16887
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016888 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016889 struct intel_crtc_state *crtc_state = crtc->config;
16890 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016891
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016892 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016893 memset(crtc_state, 0, sizeof(*crtc_state));
16894 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016895
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016896 crtc_state->base.active = crtc_state->base.enable =
16897 dev_priv->display.get_pipe_config(crtc, crtc_state);
16898
16899 crtc->base.enabled = crtc_state->base.enable;
16900 crtc->active = crtc_state->base.active;
16901
16902 if (crtc_state->base.active) {
16903 dev_priv->active_crtcs |= 1 << crtc->pipe;
16904
Clint Taylorc89e39f2016-05-13 23:41:21 +030016905 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016906 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016907 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016908 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16909 else
16910 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016911
16912 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16913 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16914 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016915 }
16916
16917 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016918
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016919 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016920
Ville Syrjälä78108b72016-05-27 20:59:19 +030016921 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16922 crtc->base.base.id, crtc->base.name,
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000016923 enableddisabled(crtc->active));
Daniel Vetter24929352012-07-02 20:28:59 +020016924 }
16925
Daniel Vetter53589012013-06-05 13:34:16 +020016926 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16927 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16928
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016929 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16930 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016931 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016932 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016933 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016934 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016935 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016936 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016937
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016938 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016939 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016940 }
16941
Damien Lespiaub2784e12014-08-05 11:29:37 +010016942 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016943 pipe = 0;
16944
16945 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjälä98187832016-10-31 22:37:10 +020016946 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020016947
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016948 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016949 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016950 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016951 } else {
16952 encoder->base.crtc = NULL;
16953 }
16954
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016955 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000016956 encoder->base.base.id, encoder->base.name,
16957 enableddisabled(encoder->base.crtc),
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016958 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016959 }
16960
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016961 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016962 if (connector->get_hw_state(connector)) {
16963 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016964
16965 encoder = connector->encoder;
16966 connector->base.encoder = &encoder->base;
16967
16968 if (encoder->base.crtc &&
16969 encoder->base.crtc->state->active) {
16970 /*
16971 * This has to be done during hardware readout
16972 * because anything calling .crtc_disable may
16973 * rely on the connector_mask being accurate.
16974 */
16975 encoder->base.crtc->state->connector_mask |=
16976 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016977 encoder->base.crtc->state->encoder_mask |=
16978 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016979 }
16980
Daniel Vetter24929352012-07-02 20:28:59 +020016981 } else {
16982 connector->base.dpms = DRM_MODE_DPMS_OFF;
16983 connector->base.encoder = NULL;
16984 }
16985 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +000016986 connector->base.base.id, connector->base.name,
16987 enableddisabled(connector->base.encoder));
Daniel Vetter24929352012-07-02 20:28:59 +020016988 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016989
16990 for_each_intel_crtc(dev, crtc) {
16991 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16992
16993 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16994 if (crtc->base.state->active) {
16995 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16996 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16997 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16998
16999 /*
17000 * The initial mode needs to be set in order to keep
17001 * the atomic core happy. It wants a valid mode if the
17002 * crtc's enabled, so we do the above call.
17003 *
17004 * At this point some state updated by the connectors
17005 * in their ->detect() callback has not run yet, so
17006 * no recalculation can be done yet.
17007 *
17008 * Even if we could do a recalculation and modeset
17009 * right now it would cause a double modeset if
17010 * fbdev or userspace chooses a different initial mode.
17011 *
17012 * If that happens, someone indicated they wanted a
17013 * mode change, which means it's safe to do a full
17014 * recalculation.
17015 */
17016 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030017017
17018 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
17019 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030017020 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020017021
17022 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030017023 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020017024}
17025
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017026/* Scan out the current hw modeset state,
17027 * and sanitizes it to the current state
17028 */
17029static void
17030intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020017031{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017032 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020017033 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020017034 struct intel_crtc *crtc;
17035 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020017036 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020017037
17038 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020017039
17040 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010017041 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020017042 intel_sanitize_encoder(encoder);
17043 }
17044
Damien Lespiau055e3932014-08-18 13:49:10 +010017045 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020017046 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020017047
Daniel Vetter24929352012-07-02 20:28:59 +020017048 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020017049 intel_dump_pipe_config(crtc, crtc->config,
17050 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020017051 }
Daniel Vetter9a935852012-07-05 22:34:27 +020017052
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020017053 intel_modeset_update_connector_atomic_state(dev);
17054
Daniel Vetter35c95372013-07-17 06:55:04 +020017055 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
17056 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
17057
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010017058 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020017059 continue;
17060
17061 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
17062
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020017063 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020017064 pll->on = false;
17065 }
17066
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010017067 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030017068 vlv_wm_get_hw_state(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010017069 else if (IS_GEN9(dev_priv))
Pradeep Bhat30789992014-11-04 17:06:45 +000017070 skl_wm_get_hw_state(dev);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010017071 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030017072 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020017073
17074 for_each_intel_crtc(dev, crtc) {
17075 unsigned long put_domains;
17076
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010017077 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020017078 if (WARN_ON(put_domains))
17079 modeset_put_power_domains(dev_priv, put_domains);
17080 }
17081 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020017082
17083 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017084}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030017085
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017086void intel_display_resume(struct drm_device *dev)
17087{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017088 struct drm_i915_private *dev_priv = to_i915(dev);
17089 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17090 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017091 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020017092
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017093 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030017094 if (state)
17095 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017096
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010017097 /*
17098 * This is a cludge because with real atomic modeset mode_config.mutex
17099 * won't be taken. Unfortunately some probed state like
17100 * audio_codec_enable is still protected by mode_config.mutex, so lock
17101 * it here for now.
17102 */
17103 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017104 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017105
Maarten Lankhorst73974892016-08-05 23:28:27 +030017106 while (1) {
17107 ret = drm_modeset_lock_all_ctx(dev, &ctx);
17108 if (ret != -EDEADLK)
17109 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017110
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017111 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017112 }
17113
Maarten Lankhorst73974892016-08-05 23:28:27 +030017114 if (!ret)
17115 ret = __intel_display_resume(dev, state);
17116
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017117 drm_modeset_drop_locks(&ctx);
17118 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010017119 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017120
Chris Wilson08536952016-10-14 13:18:18 +010017121 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017122 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010017123 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010017124}
17125
17126void intel_modeset_gem_init(struct drm_device *dev)
17127{
Chris Wilsondc979972016-05-10 14:10:04 +010017128 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080017129 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070017130 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080017131
Chris Wilsondc979972016-05-10 14:10:04 +010017132 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017133
Chris Wilson1833b132012-05-09 11:56:28 +010017134 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020017135
Chris Wilson1ee8da62016-05-12 12:43:23 +010017136 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080017137
17138 /*
17139 * Make sure any fbs we allocated at startup are properly
17140 * pinned & fenced. When we do the allocation it's too early
17141 * for this.
17142 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010017143 for_each_crtc(dev, c) {
Chris Wilson058d88c2016-08-15 10:49:06 +010017144 struct i915_vma *vma;
17145
Matt Roper2ff8fde2014-07-08 07:50:07 -070017146 obj = intel_fb_obj(c->primary->fb);
17147 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080017148 continue;
17149
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017150 mutex_lock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017151 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +020017152 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017153 mutex_unlock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017154 if (IS_ERR(vma)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080017155 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17156 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100017157 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020017158 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017159 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020017160 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017161 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080017162 }
17163 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017164}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020017165
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017166int intel_connector_register(struct drm_connector *connector)
17167{
17168 struct intel_connector *intel_connector = to_intel_connector(connector);
17169 int ret;
17170
17171 ret = intel_backlight_device_register(intel_connector);
17172 if (ret)
17173 goto err;
17174
17175 return 0;
17176
17177err:
17178 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080017179}
17180
Chris Wilsonc191eca2016-06-17 11:40:33 +010017181void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020017182{
Chris Wilsone63d87c2016-06-17 11:40:34 +010017183 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017184
Chris Wilsone63d87c2016-06-17 11:40:34 +010017185 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017186 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017187}
17188
Jesse Barnes79e53942008-11-07 14:24:08 -080017189void intel_modeset_cleanup(struct drm_device *dev)
17190{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017191 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070017192
Chris Wilsondc979972016-05-10 14:10:04 +010017193 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020017194
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017195 /*
17196 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020017197 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017198 * experience fancy races otherwise.
17199 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020017200 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070017201
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017202 /*
17203 * Due to the hpd irq storm handling the hotplug work can re-arm the
17204 * poll handlers. Hence disable polling after hpd handling is shut down.
17205 */
Keith Packardf87ea762010-10-03 19:36:26 -070017206 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017207
Jesse Barnes723bfd72010-10-07 16:01:13 -070017208 intel_unregister_dsm_handler();
17209
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020017210 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050017211
Chris Wilson1630fe72011-07-08 12:22:42 +010017212 /* flush any delayed tasks or pending work */
17213 flush_scheduled_work();
17214
Jesse Barnes79e53942008-11-07 14:24:08 -080017215 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010017216
Chris Wilson1ee8da62016-05-12 12:43:23 +010017217 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017218
Chris Wilsondc979972016-05-10 14:10:04 +010017219 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010017220
Tvrtko Ursulin40196442016-12-01 14:16:42 +000017221 intel_teardown_gmbus(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -080017222}
17223
Chris Wilsondf0e9242010-09-09 16:20:55 +010017224void intel_connector_attach_encoder(struct intel_connector *connector,
17225 struct intel_encoder *encoder)
17226{
17227 connector->encoder = encoder;
17228 drm_mode_connector_attach_encoder(&connector->base,
17229 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080017230}
Dave Airlie28d52042009-09-21 14:33:58 +100017231
17232/*
17233 * set vga decode state - true == enable VGA decode
17234 */
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000017235int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
Dave Airlie28d52042009-09-21 14:33:58 +100017236{
Tvrtko Ursulin6315b5d2016-11-16 12:32:42 +000017237 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100017238 u16 gmch_ctrl;
17239
Chris Wilson75fa0412014-02-07 18:37:02 -020017240 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17241 DRM_ERROR("failed to read control word\n");
17242 return -EIO;
17243 }
17244
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020017245 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17246 return 0;
17247
Dave Airlie28d52042009-09-21 14:33:58 +100017248 if (state)
17249 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17250 else
17251 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020017252
17253 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17254 DRM_ERROR("failed to write control word\n");
17255 return -EIO;
17256 }
17257
Dave Airlie28d52042009-09-21 14:33:58 +100017258 return 0;
17259}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017260
Chris Wilson98a2f412016-10-12 10:05:18 +010017261#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17262
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017263struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017264
17265 u32 power_well_driver;
17266
Chris Wilson63b66e52013-08-08 15:12:06 +020017267 int num_transcoders;
17268
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017269 struct intel_cursor_error_state {
17270 u32 control;
17271 u32 position;
17272 u32 base;
17273 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010017274 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017275
17276 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017277 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017278 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030017279 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010017280 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017281
17282 struct intel_plane_error_state {
17283 u32 control;
17284 u32 stride;
17285 u32 size;
17286 u32 pos;
17287 u32 addr;
17288 u32 surface;
17289 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010017290 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020017291
17292 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017293 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020017294 enum transcoder cpu_transcoder;
17295
17296 u32 conf;
17297
17298 u32 htotal;
17299 u32 hblank;
17300 u32 hsync;
17301 u32 vtotal;
17302 u32 vblank;
17303 u32 vsync;
17304 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017305};
17306
17307struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010017308intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017309{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017310 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020017311 int transcoders[] = {
17312 TRANSCODER_A,
17313 TRANSCODER_B,
17314 TRANSCODER_C,
17315 TRANSCODER_EDP,
17316 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017317 int i;
17318
Chris Wilsonc0336662016-05-06 15:40:21 +010017319 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020017320 return NULL;
17321
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017322 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017323 if (error == NULL)
17324 return NULL;
17325
Chris Wilsonc0336662016-05-06 15:40:21 +010017326 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017327 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17328
Damien Lespiau055e3932014-08-18 13:49:10 +010017329 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020017330 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017331 __intel_display_power_is_enabled(dev_priv,
17332 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020017333 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017334 continue;
17335
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030017336 error->cursor[i].control = I915_READ(CURCNTR(i));
17337 error->cursor[i].position = I915_READ(CURPOS(i));
17338 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017339
17340 error->plane[i].control = I915_READ(DSPCNTR(i));
17341 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017342 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030017343 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017344 error->plane[i].pos = I915_READ(DSPPOS(i));
17345 }
Chris Wilsonc0336662016-05-06 15:40:21 +010017346 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030017347 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017348 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017349 error->plane[i].surface = I915_READ(DSPSURF(i));
17350 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17351 }
17352
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017353 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030017354
Chris Wilsonc0336662016-05-06 15:40:21 +010017355 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030017356 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020017357 }
17358
Jani Nikula4d1de972016-03-18 17:05:42 +020017359 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010017360 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030017361 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020017362 error->num_transcoders++; /* Account for eDP. */
17363
17364 for (i = 0; i < error->num_transcoders; i++) {
17365 enum transcoder cpu_transcoder = transcoders[i];
17366
Imre Deakddf9c532013-11-27 22:02:02 +020017367 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017368 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020017369 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017370 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017371 continue;
17372
Chris Wilson63b66e52013-08-08 15:12:06 +020017373 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17374
17375 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17376 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17377 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17378 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17379 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17380 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17381 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017382 }
17383
17384 return error;
17385}
17386
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017387#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17388
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017389void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017390intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000017391 struct drm_i915_private *dev_priv,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017392 struct intel_display_error_state *error)
17393{
17394 int i;
17395
Chris Wilson63b66e52013-08-08 15:12:06 +020017396 if (!error)
17397 return;
17398
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +000017399 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010017400 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017401 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017402 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010017403 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017404 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020017405 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017406 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017407 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030017408 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017409
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017410 err_printf(m, "Plane [%d]:\n", i);
17411 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17412 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000017413 if (INTEL_GEN(dev_priv) <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017414 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17415 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017416 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010017417 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017418 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Tvrtko Ursulin5f56d5f2016-11-16 08:55:37 +000017419 if (INTEL_GEN(dev_priv) >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017420 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17421 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017422 }
17423
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017424 err_printf(m, "Cursor [%d]:\n", i);
17425 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17426 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17427 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017428 }
Chris Wilson63b66e52013-08-08 15:12:06 +020017429
17430 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020017431 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020017432 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017433 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017434 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020017435 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17436 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17437 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17438 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17439 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17440 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17441 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17442 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017443}
Chris Wilson98a2f412016-10-12 10:05:18 +010017444
17445#endif