blob: 7c61ab4e5c642074ada5e5236844a2221c6de1b4 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010037#include "intel_frontbuffer.h"
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039#include "i915_drv.h"
Chris Wilsonc37efb92016-06-17 08:28:47 +010040#include "i915_gem_dmabuf.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020041#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070042#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080043#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080044#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010045#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070047#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080049#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080050#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080051
Daniel Vetter5a21b662016-05-24 17:13:53 +020052static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
Matt Roper465c1202014-05-29 08:06:54 -070057/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010058static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010059 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010062 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070063};
64
65/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010066static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010067 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070070 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010071 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
79 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010080 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070081 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070083 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053084 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070088};
89
Matt Roper3d7d6512014-06-10 08:28:13 -070090/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
Jesse Barnesf1f644d2013-06-27 00:39:25 +030095static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020096 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030097static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020098 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030099
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800100static int intel_framebuffer_init(struct drm_device *dev,
101 struct intel_framebuffer *ifb,
102 struct drm_mode_fb_cmd2 *mode_cmd,
103 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200104static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
105static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200106static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200107static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700108 struct intel_link_m_n *m_n,
109 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200110static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200111static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200112static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200113static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200114 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200115static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200116 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200117static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
118static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700119static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_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);
Imre Deak324513c2016-06-13 16:44:36 +0300127static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100128
Ma Lingd4906092009-03-18 20:13:27 +0800129struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800138};
Jesse Barnes79e53942008-11-07 14:24:08 -0800139
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300156{
157 u32 val;
158 int divider;
159
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300181}
182
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200185{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200187}
188
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300191{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300192 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200195}
196
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
199{
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 uint32_t clkcfg;
201
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200206 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300207 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200212 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300213 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300215 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200216 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200220 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300221 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200222 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300223 }
224}
225
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300226void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
Wayne Boyer666a4532015-12-09 12:29:35 -0800242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
Chris Wilson021357a2010-09-07 20:54:59 +0100251static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100254{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200259 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200260 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100261}
262
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300263static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400264 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200265 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200266 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700274};
275
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300276static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200277 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200278 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200279 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300289static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200291 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200292 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
Eric Anholt273e27c2011-03-30 13:01:10 -0700301
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300302static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700313};
314
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300315static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700326};
327
Eric Anholt273e27c2011-03-30 13:01:10 -0700328
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300329static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800341 },
Keith Packarde4b36692009-06-05 19:22:17 -0700342};
343
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300344static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700355};
356
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800368 },
Keith Packarde4b36692009-06-05 19:22:17 -0700369};
370
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800382 },
Keith Packarde4b36692009-06-05 19:22:17 -0700383};
384
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300385static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700388 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700391 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700398};
399
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300400static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700411};
412
Eric Anholt273e27c2011-03-30 13:01:10 -0700413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300418static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700429};
430
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300431static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442};
443
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800455};
456
Eric Anholt273e27c2011-03-30 13:01:10 -0700457/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300458static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400466 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469};
470
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400479 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800482};
483
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300484static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200492 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300496 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700498};
499
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300500static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200508 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300516static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530519 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200531 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200532}
533
Imre Deakdccbea32015-06-22 23:35:51 +0300534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500542/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800544{
Shaohua Li21778322009-02-23 15:19:16 +0800545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200547 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300548 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300551
552 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800553}
554
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800561{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200562 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800563 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300565 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300568
569 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800570}
571
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300577 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300580
581 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300582}
583
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300584int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300589 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300593
594 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300595}
596
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
Chris Wilson1b894b52010-12-14 20:04:54 +0000603static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300604 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400612 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400614 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300615
Wayne Boyer666a4532015-12-09 12:29:35 -0800616 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
617 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
Wayne Boyer666a4532015-12-09 12:29:35 -0800621 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
Jesse Barnes79e53942008-11-07 14:24:08 -0800628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635
636 return true;
637}
638
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300639static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300640i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300641 const struct intel_crtc_state *crtc_state,
642 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300644 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800645
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300646 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100648 * For LVDS just rely on its current settings for dual-channel.
649 * We haven't figured out how to reliably set up different
650 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100652 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300653 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300655 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 } else {
657 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300658 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300662}
663
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200664/*
665 * Returns a set of divisors for the desired target clock with the given
666 * refclk, or FALSE. The returned values represent the clock equation:
667 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
668 *
669 * Target and reference clocks are specified in kHz.
670 *
671 * If match_clock is provided, then best_clock P divider must match the P
672 * divider from @match_clock used for LVDS downclocking.
673 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300674static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300675i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300676 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300677 int target, int refclk, struct dpll *match_clock,
678 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300679{
680 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300681 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300682 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800683
Akshay Joshi0206e352011-08-16 15:34:10 -0400684 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800685
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
Zhao Yakui42158662009-11-20 11:24:18 +0800688 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
689 clock.m1++) {
690 for (clock.m2 = limit->m2.min;
691 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200692 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800693 break;
694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 int this_err;
699
Imre Deakdccbea32015-06-22 23:35:51 +0300700 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000701 if (!intel_PLL_is_valid(dev, limit,
702 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800703 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800704 if (match_clock &&
705 clock.p != match_clock->p)
706 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800707
708 this_err = abs(clock.dot - target);
709 if (this_err < err) {
710 *best_clock = clock;
711 err = this_err;
712 }
713 }
714 }
715 }
716 }
717
718 return (err != target);
719}
720
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200721/*
722 * Returns a set of divisors for the desired target clock with the given
723 * refclk, or FALSE. The returned values represent the clock equation:
724 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
725 *
726 * Target and reference clocks are specified in kHz.
727 *
728 * If match_clock is provided, then best_clock P divider must match the P
729 * divider from @match_clock used for LVDS downclocking.
730 */
Ma Lingd4906092009-03-18 20:13:27 +0800731static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300732pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200733 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300734 int target, int refclk, struct dpll *match_clock,
735 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200736{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300737 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300738 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200739 int err = target;
740
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200741 memset(best_clock, 0, sizeof(*best_clock));
742
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300743 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
744
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200745 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
746 clock.m1++) {
747 for (clock.m2 = limit->m2.min;
748 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200749 for (clock.n = limit->n.min;
750 clock.n <= limit->n.max; clock.n++) {
751 for (clock.p1 = limit->p1.min;
752 clock.p1 <= limit->p1.max; clock.p1++) {
753 int this_err;
754
Imre Deakdccbea32015-06-22 23:35:51 +0300755 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800756 if (!intel_PLL_is_valid(dev, limit,
757 &clock))
758 continue;
759 if (match_clock &&
760 clock.p != match_clock->p)
761 continue;
762
763 this_err = abs(clock.dot - target);
764 if (this_err < err) {
765 *best_clock = clock;
766 err = this_err;
767 }
768 }
769 }
770 }
771 }
772
773 return (err != target);
774}
775
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200776/*
777 * Returns a set of divisors for the desired target clock with the given
778 * refclk, or FALSE. The returned values represent the clock equation:
779 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200780 *
781 * Target and reference clocks are specified in kHz.
782 *
783 * If match_clock is provided, then best_clock P divider must match the P
784 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200785 */
Ma Lingd4906092009-03-18 20:13:27 +0800786static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300787g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200788 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300789 int target, int refclk, struct dpll *match_clock,
790 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800791{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300792 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300793 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800794 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300795 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400796 /* approximately equals target * 0.00585 */
797 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800798
799 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300800
801 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
802
Ma Lingd4906092009-03-18 20:13:27 +0800803 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200804 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800805 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200806 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800807 for (clock.m1 = limit->m1.max;
808 clock.m1 >= limit->m1.min; clock.m1--) {
809 for (clock.m2 = limit->m2.max;
810 clock.m2 >= limit->m2.min; clock.m2--) {
811 for (clock.p1 = limit->p1.max;
812 clock.p1 >= limit->p1.min; clock.p1--) {
813 int this_err;
814
Imre Deakdccbea32015-06-22 23:35:51 +0300815 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000816 if (!intel_PLL_is_valid(dev, limit,
817 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800818 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000819
820 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800821 if (this_err < err_most) {
822 *best_clock = clock;
823 err_most = this_err;
824 max_n = clock.n;
825 found = true;
826 }
827 }
828 }
829 }
830 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800831 return found;
832}
Ma Lingd4906092009-03-18 20:13:27 +0800833
Imre Deakd5dd62b2015-03-17 11:40:03 +0200834/*
835 * Check if the calculated PLL configuration is more optimal compared to the
836 * best configuration and error found so far. Return the calculated error.
837 */
838static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300839 const struct dpll *calculated_clock,
840 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200841 unsigned int best_error_ppm,
842 unsigned int *error_ppm)
843{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200844 /*
845 * For CHV ignore the error and consider only the P value.
846 * Prefer a bigger P value based on HW requirements.
847 */
848 if (IS_CHERRYVIEW(dev)) {
849 *error_ppm = 0;
850
851 return calculated_clock->p > best_clock->p;
852 }
853
Imre Deak24be4e42015-03-17 11:40:04 +0200854 if (WARN_ON_ONCE(!target_freq))
855 return false;
856
Imre Deakd5dd62b2015-03-17 11:40:03 +0200857 *error_ppm = div_u64(1000000ULL *
858 abs(target_freq - calculated_clock->dot),
859 target_freq);
860 /*
861 * Prefer a better P value over a better (smaller) error if the error
862 * is small. Ensure this preference for future configurations too by
863 * setting the error to 0.
864 */
865 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
866 *error_ppm = 0;
867
868 return true;
869 }
870
871 return *error_ppm + 10 < best_error_ppm;
872}
873
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200874/*
875 * Returns a set of divisors for the desired target clock with the given
876 * refclk, or FALSE. The returned values represent the clock equation:
877 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
878 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800879static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300880vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200881 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300882 int target, int refclk, struct dpll *match_clock,
883 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700884{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200885 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300886 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300887 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300888 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300889 /* min update 19.2 MHz */
890 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300891 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700892
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300893 target *= 5; /* fast clock */
894
895 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700896
897 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300898 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300899 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300900 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300901 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300902 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700903 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300904 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200905 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300906
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300907 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
908 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300909
Imre Deakdccbea32015-06-22 23:35:51 +0300910 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300911
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300912 if (!intel_PLL_is_valid(dev, limit,
913 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300914 continue;
915
Imre Deakd5dd62b2015-03-17 11:40:03 +0200916 if (!vlv_PLL_is_optimal(dev, target,
917 &clock,
918 best_clock,
919 bestppm, &ppm))
920 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300921
Imre Deakd5dd62b2015-03-17 11:40:03 +0200922 *best_clock = clock;
923 bestppm = ppm;
924 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700925 }
926 }
927 }
928 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300930 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700931}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700932
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200933/*
934 * Returns a set of divisors for the desired target clock with the given
935 * refclk, or FALSE. The returned values represent the clock equation:
936 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
937 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300938static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300939chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200940 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300941 int target, int refclk, struct dpll *match_clock,
942 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300943{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300945 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200946 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300947 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300948 uint64_t m2;
949 int found = false;
950
951 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200952 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300953
954 /*
955 * Based on hardware doc, the n always set to 1, and m1 always
956 * set to 2. If requires to support 200Mhz refclk, we need to
957 * revisit this because n may not 1 anymore.
958 */
959 clock.n = 1, clock.m1 = 2;
960 target *= 5; /* fast clock */
961
962 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
963 for (clock.p2 = limit->p2.p2_fast;
964 clock.p2 >= limit->p2.p2_slow;
965 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200966 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300967
968 clock.p = clock.p1 * clock.p2;
969
970 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
971 clock.n) << 22, refclk * clock.m1);
972
973 if (m2 > INT_MAX/clock.m1)
974 continue;
975
976 clock.m2 = m2;
977
Imre Deakdccbea32015-06-22 23:35:51 +0300978 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300979
980 if (!intel_PLL_is_valid(dev, limit, &clock))
981 continue;
982
Imre Deak9ca3ba02015-03-17 11:40:05 +0200983 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
984 best_error_ppm, &error_ppm))
985 continue;
986
987 *best_clock = clock;
988 best_error_ppm = error_ppm;
989 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300990 }
991 }
992
993 return found;
994}
995
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200996bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300997 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200998{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200999 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001000 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001001
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001002 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001003 target_clock, refclk, NULL, best_clock);
1004}
1005
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001006bool intel_crtc_active(struct drm_crtc *crtc)
1007{
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
1010 /* Be paranoid as we can arrive here with only partial
1011 * state retrieved from the hardware during setup.
1012 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001013 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001014 * as Haswell has gained clock readout/fastboot support.
1015 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001016 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001017 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001018 *
1019 * FIXME: The intel_crtc->active here should be switched to
1020 * crtc->state->active once we have proper CRTC states wired up
1021 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001022 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001023 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001024 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001025}
1026
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001027enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1028 enum pipe pipe)
1029{
1030 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1032
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001033 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001034}
1035
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001036static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1037{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001038 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001039 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001040 u32 line1, line2;
1041 u32 line_mask;
1042
1043 if (IS_GEN2(dev))
1044 line_mask = DSL_LINEMASK_GEN2;
1045 else
1046 line_mask = DSL_LINEMASK_GEN3;
1047
1048 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001049 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001050 line2 = I915_READ(reg) & line_mask;
1051
1052 return line1 == line2;
1053}
1054
Keith Packardab7ad7f2010-10-03 00:33:06 -07001055/*
1056 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001057 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001058 *
1059 * After disabling a pipe, we can't wait for vblank in the usual way,
1060 * spinning on the vblank interrupt status bit, since we won't actually
1061 * see an interrupt when the pipe is disabled.
1062 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001063 * On Gen4 and above:
1064 * wait for the pipe register state bit to turn off
1065 *
1066 * Otherwise:
1067 * wait for the display line value to settle (it usually
1068 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001069 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001070 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001071static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001072{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001073 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001074 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001075 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001076 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001077
Keith Packardab7ad7f2010-10-03 00:33:06 -07001078 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001079 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001080
Keith Packardab7ad7f2010-10-03 00:33:06 -07001081 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001082 if (intel_wait_for_register(dev_priv,
1083 reg, I965_PIPECONF_ACTIVE, 0,
1084 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001085 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001086 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001087 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001088 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001089 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001090 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001091}
1092
Jesse Barnesb24e7172011-01-04 15:09:30 -08001093/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001094void assert_pll(struct drm_i915_private *dev_priv,
1095 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001096{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097 u32 val;
1098 bool cur_state;
1099
Ville Syrjälä649636e2015-09-22 19:50:01 +03001100 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001101 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001102 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001104 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001106
Jani Nikula23538ef2013-08-27 15:12:22 +03001107/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001108void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001109{
1110 u32 val;
1111 bool cur_state;
1112
Ville Syrjäläa5805162015-05-26 20:42:30 +03001113 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001114 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001115 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001116
1117 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001118 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001119 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001120 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001121}
Jani Nikula23538ef2013-08-27 15:12:22 +03001122
Jesse Barnes040484a2011-01-03 12:14:26 -08001123static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
1125{
Jesse Barnes040484a2011-01-03 12:14:26 -08001126 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1128 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001129
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001130 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001131 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001132 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001133 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001134 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001135 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001136 cur_state = !!(val & FDI_TX_ENABLE);
1137 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001138 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001139 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001140 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001141}
1142#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1143#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1144
1145static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1146 enum pipe pipe, bool state)
1147{
Jesse Barnes040484a2011-01-03 12:14:26 -08001148 u32 val;
1149 bool cur_state;
1150
Ville Syrjälä649636e2015-09-22 19:50:01 +03001151 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001152 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001153 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001154 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001155 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001156}
1157#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1158#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1159
1160static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1161 enum pipe pipe)
1162{
Jesse Barnes040484a2011-01-03 12:14:26 -08001163 u32 val;
1164
1165 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001166 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001167 return;
1168
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001169 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001170 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001171 return;
1172
Ville Syrjälä649636e2015-09-22 19:50:01 +03001173 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001174 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 -08001175}
1176
Daniel Vetter55607e82013-06-16 21:42:39 +02001177void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1178 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001179{
Jesse Barnes040484a2011-01-03 12:14:26 -08001180 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001181 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001182
Ville Syrjälä649636e2015-09-22 19:50:01 +03001183 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001184 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001185 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001186 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001187 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001188}
1189
Daniel Vetterb680c372014-09-19 18:27:27 +02001190void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1191 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001192{
Chris Wilson91c8a322016-07-05 10:40:23 +01001193 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001194 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001195 u32 val;
1196 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001197 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001198
Jani Nikulabedd4db2014-08-22 15:04:13 +03001199 if (WARN_ON(HAS_DDI(dev)))
1200 return;
1201
1202 if (HAS_PCH_SPLIT(dev)) {
1203 u32 port_sel;
1204
Imre Deak44cb7342016-08-10 14:07:29 +03001205 pp_reg = PP_CONTROL(0);
1206 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001207
1208 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1209 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001212 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001213 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001214 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001215 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001216 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001217 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001218 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1219 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001220 }
1221
1222 val = I915_READ(pp_reg);
1223 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001224 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001225 locked = false;
1226
Rob Clarke2c719b2014-12-15 13:56:32 -05001227 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001228 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001229 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001230}
1231
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001232static void assert_cursor(struct drm_i915_private *dev_priv,
1233 enum pipe pipe, bool state)
1234{
Chris Wilson91c8a322016-07-05 10:40:23 +01001235 struct drm_device *dev = &dev_priv->drm;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001236 bool cur_state;
1237
Paulo Zanonid9d82082014-02-27 16:30:56 -03001238 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001239 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001240 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001241 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001242
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001244 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001245 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001246}
1247#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1248#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1249
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001250void assert_pipe(struct drm_i915_private *dev_priv,
1251 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001252{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001253 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001254 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1255 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001256 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001257
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001258 /* if we need the pipe quirk it must be always on */
1259 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1260 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001261 state = true;
1262
Imre Deak4feed0e2016-02-12 18:55:14 +02001263 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1264 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001265 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001266 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001267
1268 intel_display_power_put(dev_priv, power_domain);
1269 } else {
1270 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001271 }
1272
Rob Clarke2c719b2014-12-15 13:56:32 -05001273 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001274 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001275 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001276}
1277
Chris Wilson931872f2012-01-16 23:01:13 +00001278static void assert_plane(struct drm_i915_private *dev_priv,
1279 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001281 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001282 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001283
Ville Syrjälä649636e2015-09-22 19:50:01 +03001284 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001285 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001286 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001287 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001288 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001289}
1290
Chris Wilson931872f2012-01-16 23:01:13 +00001291#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1292#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1293
Jesse Barnesb24e7172011-01-04 15:09:30 -08001294static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
1296{
Chris Wilson91c8a322016-07-05 10:40:23 +01001297 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001298 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001299
Ville Syrjälä653e1022013-06-04 13:49:05 +03001300 /* Primary planes are fixed to pipes on gen4+ */
1301 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001302 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001303 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001304 "plane %c assertion failure, should be disabled but not\n",
1305 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001306 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001307 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001308
Jesse Barnesb24e7172011-01-04 15:09:30 -08001309 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001310 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001311 u32 val = I915_READ(DSPCNTR(i));
1312 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001314 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001315 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1316 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001317 }
1318}
1319
Jesse Barnes19332d72013-03-28 09:55:38 -07001320static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1321 enum pipe pipe)
1322{
Chris Wilson91c8a322016-07-05 10:40:23 +01001323 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001324 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001325
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001326 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001327 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001328 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001329 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001330 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1331 sprite, pipe_name(pipe));
1332 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001333 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001334 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001335 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001336 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001337 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001338 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001339 }
1340 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001341 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001342 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001344 plane_name(pipe), pipe_name(pipe));
1345 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001346 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001347 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001348 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1349 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001350 }
1351}
1352
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001353static void assert_vblank_disabled(struct drm_crtc *crtc)
1354{
Rob Clarke2c719b2014-12-15 13:56:32 -05001355 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001356 drm_crtc_vblank_put(crtc);
1357}
1358
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001359void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001361{
Jesse Barnes92f25842011-01-04 15:09:34 -08001362 u32 val;
1363 bool enabled;
1364
Ville Syrjälä649636e2015-09-22 19:50:01 +03001365 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001366 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001367 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001368 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1369 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001370}
1371
Keith Packard4e634382011-08-06 10:39:45 -07001372static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001374{
1375 if ((val & DP_PORT_EN) == 0)
1376 return false;
1377
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001378 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001379 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001380 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1381 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001382 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001383 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1384 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001385 } else {
1386 if ((val & DP_PIPE_MASK) != (pipe << 30))
1387 return false;
1388 }
1389 return true;
1390}
1391
Keith Packard1519b992011-08-06 10:35:34 -07001392static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe, u32 val)
1394{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001395 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001396 return false;
1397
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001398 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001399 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001400 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001401 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001402 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1403 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001404 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001405 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001406 return false;
1407 }
1408 return true;
1409}
1410
1411static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe, u32 val)
1413{
1414 if ((val & LVDS_PORT_EN) == 0)
1415 return false;
1416
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001417 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001418 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1419 return false;
1420 } else {
1421 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1422 return false;
1423 }
1424 return true;
1425}
1426
1427static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1428 enum pipe pipe, u32 val)
1429{
1430 if ((val & ADPA_DAC_ENABLE) == 0)
1431 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001432 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001433 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1434 return false;
1435 } else {
1436 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1437 return false;
1438 }
1439 return true;
1440}
1441
Jesse Barnes291906f2011-02-02 12:28:03 -08001442static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001443 enum pipe pipe, i915_reg_t reg,
1444 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001445{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001446 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001447 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001448 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001449 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001450
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001451 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001452 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001453 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001454}
1455
1456static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001457 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001458{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001459 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001460 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001461 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001462 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001463
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001464 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001465 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001466 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001467}
1468
1469static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe)
1471{
Jesse Barnes291906f2011-02-02 12:28:03 -08001472 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001473
Keith Packardf0575e92011-07-25 22:12:43 -07001474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1476 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001477
Ville Syrjälä649636e2015-09-22 19:50:01 +03001478 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001479 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001480 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001481 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001482
Ville Syrjälä649636e2015-09-22 19:50:01 +03001483 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001484 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001485 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001486 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001487
Paulo Zanonie2debe92013-02-18 19:00:27 -03001488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1490 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001491}
1492
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001493static void _vlv_enable_pll(struct intel_crtc *crtc,
1494 const struct intel_crtc_state *pipe_config)
1495{
1496 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1497 enum pipe pipe = crtc->pipe;
1498
1499 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1500 POSTING_READ(DPLL(pipe));
1501 udelay(150);
1502
Chris Wilson2c30b432016-06-30 15:32:54 +01001503 if (intel_wait_for_register(dev_priv,
1504 DPLL(pipe),
1505 DPLL_LOCK_VLV,
1506 DPLL_LOCK_VLV,
1507 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001508 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1509}
1510
Ville Syrjäläd288f652014-10-28 13:20:22 +02001511static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001512 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001513{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001514 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001515 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001516
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001517 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001518
Daniel Vetter87442f72013-06-06 00:52:17 +02001519 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001520 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001521
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001522 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1523 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001524
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001525 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1526 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001527}
1528
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001529
1530static void _chv_enable_pll(struct intel_crtc *crtc,
1531 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001532{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001533 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001534 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001536 u32 tmp;
1537
Ville Syrjäläa5805162015-05-26 20:42:30 +03001538 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001539
1540 /* Enable back the 10bit clock to display controller */
1541 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1542 tmp |= DPIO_DCLKP_EN;
1543 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1544
Ville Syrjälä54433e92015-05-26 20:42:31 +03001545 mutex_unlock(&dev_priv->sb_lock);
1546
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001547 /*
1548 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1549 */
1550 udelay(1);
1551
1552 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001553 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001554
1555 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001556 if (intel_wait_for_register(dev_priv,
1557 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1558 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001559 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001560}
1561
1562static void chv_enable_pll(struct intel_crtc *crtc,
1563 const struct intel_crtc_state *pipe_config)
1564{
1565 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1566 enum pipe pipe = crtc->pipe;
1567
1568 assert_pipe_disabled(dev_priv, pipe);
1569
1570 /* PLL is protected by panel, make sure we can write it */
1571 assert_panel_unlocked(dev_priv, pipe);
1572
1573 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1574 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001575
Ville Syrjäläc2317752016-03-15 16:39:56 +02001576 if (pipe != PIPE_A) {
1577 /*
1578 * WaPixelRepeatModeFixForC0:chv
1579 *
1580 * DPLLCMD is AWOL. Use chicken bits to propagate
1581 * the value from DPLLBMD to either pipe B or C.
1582 */
1583 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1584 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1585 I915_WRITE(CBR4_VLV, 0);
1586 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1587
1588 /*
1589 * DPLLB VGA mode also seems to cause problems.
1590 * We should always have it disabled.
1591 */
1592 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1593 } else {
1594 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1595 POSTING_READ(DPLL_MD(pipe));
1596 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001597}
1598
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001599static int intel_num_dvo_pipes(struct drm_device *dev)
1600{
1601 struct intel_crtc *crtc;
1602 int count = 0;
1603
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001604 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001605 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001606 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1607 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001608
1609 return count;
1610}
1611
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001612static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001613{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001614 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001615 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001616 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001617 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001618
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001619 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001620
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001621 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001622 if (IS_MOBILE(dev) && !IS_I830(dev))
1623 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001624
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001625 /* Enable DVO 2x clock on both PLLs if necessary */
1626 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1627 /*
1628 * It appears to be important that we don't enable this
1629 * for the current pipe before otherwise configuring the
1630 * PLL. No idea how this should be handled if multiple
1631 * DVO outputs are enabled simultaneosly.
1632 */
1633 dpll |= DPLL_DVO_2X_MODE;
1634 I915_WRITE(DPLL(!crtc->pipe),
1635 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1636 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001637
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001638 /*
1639 * Apparently we need to have VGA mode enabled prior to changing
1640 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1641 * dividers, even though the register value does change.
1642 */
1643 I915_WRITE(reg, 0);
1644
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001645 I915_WRITE(reg, dpll);
1646
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001647 /* Wait for the clocks to stabilize. */
1648 POSTING_READ(reg);
1649 udelay(150);
1650
1651 if (INTEL_INFO(dev)->gen >= 4) {
1652 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001653 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001654 } else {
1655 /* The pixel multiplier can only be updated once the
1656 * DPLL is enabled and the clocks are stable.
1657 *
1658 * So write it again.
1659 */
1660 I915_WRITE(reg, dpll);
1661 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001662
1663 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001664 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001665 POSTING_READ(reg);
1666 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001667 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001668 POSTING_READ(reg);
1669 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001670 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001671 POSTING_READ(reg);
1672 udelay(150); /* wait for warmup */
1673}
1674
1675/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001676 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001677 * @dev_priv: i915 private structure
1678 * @pipe: pipe PLL to disable
1679 *
1680 * Disable the PLL for @pipe, making sure the pipe is off first.
1681 *
1682 * Note! This is for pre-ILK only.
1683 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001684static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001685{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001686 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001687 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001688 enum pipe pipe = crtc->pipe;
1689
1690 /* Disable DVO 2x clock on both PLLs if necessary */
1691 if (IS_I830(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001692 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001693 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001694 I915_WRITE(DPLL(PIPE_B),
1695 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1696 I915_WRITE(DPLL(PIPE_A),
1697 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1698 }
1699
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001700 /* Don't disable pipe or pipe PLLs if needed */
1701 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1702 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001703 return;
1704
1705 /* Make sure the pipe isn't still relying on us */
1706 assert_pipe_disabled(dev_priv, pipe);
1707
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001708 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001709 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001710}
1711
Jesse Barnesf6071162013-10-01 10:41:38 -07001712static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1713{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001714 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001715
1716 /* Make sure the pipe isn't still relying on us */
1717 assert_pipe_disabled(dev_priv, pipe);
1718
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001719 val = DPLL_INTEGRATED_REF_CLK_VLV |
1720 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1721 if (pipe != PIPE_A)
1722 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1723
Jesse Barnesf6071162013-10-01 10:41:38 -07001724 I915_WRITE(DPLL(pipe), val);
1725 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001726}
1727
1728static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001730 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001731 u32 val;
1732
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001733 /* Make sure the pipe isn't still relying on us */
1734 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001735
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001736 val = DPLL_SSC_REF_CLK_CHV |
1737 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001738 if (pipe != PIPE_A)
1739 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001740
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001741 I915_WRITE(DPLL(pipe), val);
1742 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001743
Ville Syrjäläa5805162015-05-26 20:42:30 +03001744 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001745
1746 /* Disable 10bit clock to display controller */
1747 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1748 val &= ~DPIO_DCLKP_EN;
1749 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1750
Ville Syrjäläa5805162015-05-26 20:42:30 +03001751 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001752}
1753
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001754void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001755 struct intel_digital_port *dport,
1756 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001757{
1758 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001759 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001760
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001761 switch (dport->port) {
1762 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001763 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001764 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001765 break;
1766 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001767 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001768 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001769 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001770 break;
1771 case PORT_D:
1772 port_mask = DPLL_PORTD_READY_MASK;
1773 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001774 break;
1775 default:
1776 BUG();
1777 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001778
Chris Wilson370004d2016-06-30 15:32:56 +01001779 if (intel_wait_for_register(dev_priv,
1780 dpll_reg, port_mask, expected_mask,
1781 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001782 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1783 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001784}
1785
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001786static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1787 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001788{
Chris Wilson91c8a322016-07-05 10:40:23 +01001789 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001790 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001792 i915_reg_t reg;
1793 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001794
Jesse Barnes040484a2011-01-03 12:14:26 -08001795 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001796 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001797
1798 /* FDI must be feeding us bits for PCH ports */
1799 assert_fdi_tx_enabled(dev_priv, pipe);
1800 assert_fdi_rx_enabled(dev_priv, pipe);
1801
Daniel Vetter23670b322012-11-01 09:15:30 +01001802 if (HAS_PCH_CPT(dev)) {
1803 /* Workaround: Set the timing override bit before enabling the
1804 * pch transcoder. */
1805 reg = TRANS_CHICKEN2(pipe);
1806 val = I915_READ(reg);
1807 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1808 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001809 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001810
Daniel Vetterab9412b2013-05-03 11:49:46 +02001811 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001812 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001813 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001814
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001815 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001816 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001817 * Make the BPC in transcoder be consistent with
1818 * that in pipeconf reg. For HDMI we must use 8bpc
1819 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001820 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001821 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001822 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001823 val |= PIPECONF_8BPC;
1824 else
1825 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001826 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001827
1828 val &= ~TRANS_INTERLACE_MASK;
1829 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001830 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001831 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001832 val |= TRANS_LEGACY_INTERLACED_ILK;
1833 else
1834 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001835 else
1836 val |= TRANS_PROGRESSIVE;
1837
Jesse Barnes040484a2011-01-03 12:14:26 -08001838 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001839 if (intel_wait_for_register(dev_priv,
1840 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1841 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001842 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001843}
1844
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001845static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001846 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001847{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001849
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001850 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001851 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001852 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001853
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001854 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001856 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001858
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001859 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001860 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001861
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001862 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1863 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001864 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001865 else
1866 val |= TRANS_PROGRESSIVE;
1867
Daniel Vetterab9412b2013-05-03 11:49:46 +02001868 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001869 if (intel_wait_for_register(dev_priv,
1870 LPT_TRANSCONF,
1871 TRANS_STATE_ENABLE,
1872 TRANS_STATE_ENABLE,
1873 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001874 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001875}
1876
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001877static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001879{
Chris Wilson91c8a322016-07-05 10:40:23 +01001880 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001881 i915_reg_t reg;
1882 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001883
1884 /* FDI relies on the transcoder */
1885 assert_fdi_tx_disabled(dev_priv, pipe);
1886 assert_fdi_rx_disabled(dev_priv, pipe);
1887
Jesse Barnes291906f2011-02-02 12:28:03 -08001888 /* Ports must be off as well */
1889 assert_pch_ports_disabled(dev_priv, pipe);
1890
Daniel Vetterab9412b2013-05-03 11:49:46 +02001891 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001892 val = I915_READ(reg);
1893 val &= ~TRANS_ENABLE;
1894 I915_WRITE(reg, val);
1895 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001896 if (intel_wait_for_register(dev_priv,
1897 reg, TRANS_STATE_ENABLE, 0,
1898 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001899 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001900
Ville Syrjäläc4656132015-10-29 21:25:56 +02001901 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001902 /* Workaround: Clear the timing override chicken bit again. */
1903 reg = TRANS_CHICKEN2(pipe);
1904 val = I915_READ(reg);
1905 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1906 I915_WRITE(reg, val);
1907 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001908}
1909
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001910static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001912 u32 val;
1913
Daniel Vetterab9412b2013-05-03 11:49:46 +02001914 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001915 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001916 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001917 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001918 if (intel_wait_for_register(dev_priv,
1919 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1920 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001921 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001922
1923 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001924 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001925 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001926 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001927}
1928
1929/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001930 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001931 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001932 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001933 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001934 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001935 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001936static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001937{
Paulo Zanoni03722642014-01-17 13:51:09 -02001938 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001939 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001940 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001941 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001942 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001943 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001944 u32 val;
1945
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001946 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1947
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001948 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001949 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001950 assert_sprites_disabled(dev_priv, pipe);
1951
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001952 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001953 pch_transcoder = TRANSCODER_A;
1954 else
1955 pch_transcoder = pipe;
1956
Jesse Barnesb24e7172011-01-04 15:09:30 -08001957 /*
1958 * A pipe without a PLL won't actually be able to drive bits from
1959 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1960 * need the check.
1961 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001962 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001963 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001964 assert_dsi_pll_enabled(dev_priv);
1965 else
1966 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001967 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001968 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001969 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001970 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001971 assert_fdi_tx_pll_enabled(dev_priv,
1972 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001973 }
1974 /* FIXME: assert CPU port conditions for SNB+ */
1975 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001976
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001977 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001978 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001979 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001980 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1981 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001982 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001983 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001984
1985 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001986 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001987
1988 /*
1989 * Until the pipe starts DSL will read as 0, which would cause
1990 * an apparent vblank timestamp jump, which messes up also the
1991 * frame count when it's derived from the timestamps. So let's
1992 * wait for the pipe to start properly before we call
1993 * drm_crtc_vblank_on()
1994 */
1995 if (dev->max_vblank_count == 0 &&
1996 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1997 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001998}
1999
2000/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002001 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002002 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002003 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002004 * Disable the pipe of @crtc, making sure that various hardware
2005 * specific requirements are met, if applicable, e.g. plane
2006 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002007 *
2008 * Will wait until the pipe has shut down before returning.
2009 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002010static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002011{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002012 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002013 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002014 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002015 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002016 u32 val;
2017
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002018 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2019
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020 /*
2021 * Make sure planes won't keep trying to pump pixels to us,
2022 * or we might hang the display.
2023 */
2024 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002025 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002026 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002028 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002029 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002030 if ((val & PIPECONF_ENABLE) == 0)
2031 return;
2032
Ville Syrjälä67adc642014-08-15 01:21:57 +03002033 /*
2034 * Double wide has implications for planes
2035 * so best keep it disabled when not needed.
2036 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002037 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002038 val &= ~PIPECONF_DOUBLE_WIDE;
2039
2040 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002041 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2042 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002043 val &= ~PIPECONF_ENABLE;
2044
2045 I915_WRITE(reg, val);
2046 if ((val & PIPECONF_ENABLE) == 0)
2047 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002048}
2049
Ville Syrjälä832be822016-01-12 21:08:33 +02002050static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2051{
2052 return IS_GEN2(dev_priv) ? 2048 : 4096;
2053}
2054
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002055static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2056 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002057{
2058 switch (fb_modifier) {
2059 case DRM_FORMAT_MOD_NONE:
2060 return cpp;
2061 case I915_FORMAT_MOD_X_TILED:
2062 if (IS_GEN2(dev_priv))
2063 return 128;
2064 else
2065 return 512;
2066 case I915_FORMAT_MOD_Y_TILED:
2067 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2068 return 128;
2069 else
2070 return 512;
2071 case I915_FORMAT_MOD_Yf_TILED:
2072 switch (cpp) {
2073 case 1:
2074 return 64;
2075 case 2:
2076 case 4:
2077 return 128;
2078 case 8:
2079 case 16:
2080 return 256;
2081 default:
2082 MISSING_CASE(cpp);
2083 return cpp;
2084 }
2085 break;
2086 default:
2087 MISSING_CASE(fb_modifier);
2088 return cpp;
2089 }
2090}
2091
Ville Syrjälä832be822016-01-12 21:08:33 +02002092unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2093 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002094{
Ville Syrjälä832be822016-01-12 21:08:33 +02002095 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2096 return 1;
2097 else
2098 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002099 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002100}
2101
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002102/* Return the tile dimensions in pixel units */
2103static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2104 unsigned int *tile_width,
2105 unsigned int *tile_height,
2106 uint64_t fb_modifier,
2107 unsigned int cpp)
2108{
2109 unsigned int tile_width_bytes =
2110 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2111
2112 *tile_width = tile_width_bytes / cpp;
2113 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2114}
2115
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002116unsigned int
2117intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002118 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002119{
Ville Syrjälä832be822016-01-12 21:08:33 +02002120 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2121 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2122
2123 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002124}
2125
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002126unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127{
2128 unsigned int size = 0;
2129 int i;
2130
2131 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132 size += rot_info->plane[i].width * rot_info->plane[i].height;
2133
2134 return size;
2135}
2136
Daniel Vetter75c82a52015-10-14 16:51:04 +02002137static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002138intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2139 const struct drm_framebuffer *fb,
2140 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002141{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002142 if (intel_rotation_90_or_270(rotation)) {
2143 *view = i915_ggtt_view_rotated;
2144 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2145 } else {
2146 *view = i915_ggtt_view_normal;
2147 }
2148}
2149
Ville Syrjälä603525d2016-01-12 21:08:37 +02002150static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002151{
2152 if (INTEL_INFO(dev_priv)->gen >= 9)
2153 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002154 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002155 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002156 return 128 * 1024;
2157 else if (INTEL_INFO(dev_priv)->gen >= 4)
2158 return 4 * 1024;
2159 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002160 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002161}
2162
Ville Syrjälä603525d2016-01-12 21:08:37 +02002163static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2164 uint64_t fb_modifier)
2165{
2166 switch (fb_modifier) {
2167 case DRM_FORMAT_MOD_NONE:
2168 return intel_linear_alignment(dev_priv);
2169 case I915_FORMAT_MOD_X_TILED:
2170 if (INTEL_INFO(dev_priv)->gen >= 9)
2171 return 256 * 1024;
2172 return 0;
2173 case I915_FORMAT_MOD_Y_TILED:
2174 case I915_FORMAT_MOD_Yf_TILED:
2175 return 1 * 1024 * 1024;
2176 default:
2177 MISSING_CASE(fb_modifier);
2178 return 0;
2179 }
2180}
2181
Chris Wilson127bd2a2010-07-23 23:32:05 +01002182int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002183intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2184 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002185{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002186 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002187 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002188 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002189 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002190 u32 alignment;
2191 int ret;
2192
Matt Roperebcdd392014-07-09 16:22:11 -07002193 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2194
Ville Syrjälä603525d2016-01-12 21:08:37 +02002195 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002196
Ville Syrjälä3465c582016-02-15 22:54:43 +02002197 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002198
Chris Wilson693db182013-03-05 14:52:39 +00002199 /* Note that the w/a also requires 64 PTE of padding following the
2200 * bo. We currently fill all unused PTE with the shadow page and so
2201 * we should always have valid PTE following the scanout preventing
2202 * the VT-d warning.
2203 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002204 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002205 alignment = 256 * 1024;
2206
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002207 /*
2208 * Global gtt pte registers are special registers which actually forward
2209 * writes to a chunk of system memory. Which means that there is no risk
2210 * that the register values disappear as soon as we call
2211 * intel_runtime_pm_put(), so it is correct to wrap only the
2212 * pin/unpin/fence and not more.
2213 */
2214 intel_runtime_pm_get(dev_priv);
2215
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002216 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2217 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002218 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002219 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002220
2221 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2222 * fence, whereas 965+ only requires a fence if using
2223 * framebuffer compression. For simplicity, we always install
2224 * a fence as the cost is not that onerous.
2225 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002226 if (view.type == I915_GGTT_VIEW_NORMAL) {
2227 ret = i915_gem_object_get_fence(obj);
2228 if (ret == -EDEADLK) {
2229 /*
2230 * -EDEADLK means there are no free fences
2231 * no pending flips.
2232 *
2233 * This is propagated to atomic, but it uses
2234 * -EDEADLK to force a locking recovery, so
2235 * change the returned error to -EBUSY.
2236 */
2237 ret = -EBUSY;
2238 goto err_unpin;
2239 } else if (ret)
2240 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002241
Vivek Kasireddy98072162015-10-29 18:54:38 -07002242 i915_gem_object_pin_fence(obj);
2243 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002244
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002245 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002246 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002247
2248err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002249 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002250err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002251 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002252 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002253}
2254
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002255void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002256{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002257 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002258 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002259
Matt Roperebcdd392014-07-09 16:22:11 -07002260 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2261
Ville Syrjälä3465c582016-02-15 22:54:43 +02002262 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002263
Vivek Kasireddy98072162015-10-29 18:54:38 -07002264 if (view.type == I915_GGTT_VIEW_NORMAL)
2265 i915_gem_object_unpin_fence(obj);
2266
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002267 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002268}
2269
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002270/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002271 * Convert the x/y offsets into a linear offset.
2272 * Only valid with 0/180 degree rotation, which is fine since linear
2273 * offset is only used with linear buffers on pre-hsw and tiled buffers
2274 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2275 */
2276u32 intel_fb_xy_to_linear(int x, int y,
2277 const struct drm_framebuffer *fb, int plane)
2278{
2279 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,
2291 const struct drm_framebuffer *fb, int plane,
2292 unsigned int rotation)
2293
2294{
2295 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2296
2297 if (intel_rotation_90_or_270(rotation)) {
2298 *x += intel_fb->rotated[plane].x;
2299 *y += intel_fb->rotated[plane].y;
2300 } else {
2301 *x += intel_fb->normal[plane].x;
2302 *y += intel_fb->normal[plane].y;
2303 }
2304}
2305
2306/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002307 * Adjust the tile offset by moving the difference into
2308 * the x/y offsets.
2309 *
2310 * Input tile dimensions and pitch must already be
2311 * rotated to match x and y, and in pixel units.
2312 */
2313static u32 intel_adjust_tile_offset(int *x, int *y,
2314 unsigned int tile_width,
2315 unsigned int tile_height,
2316 unsigned int tile_size,
2317 unsigned int pitch_tiles,
2318 u32 old_offset,
2319 u32 new_offset)
2320{
2321 unsigned int tiles;
2322
2323 WARN_ON(old_offset & (tile_size - 1));
2324 WARN_ON(new_offset & (tile_size - 1));
2325 WARN_ON(new_offset > old_offset);
2326
2327 tiles = (old_offset - new_offset) / tile_size;
2328
2329 *y += tiles / pitch_tiles * tile_height;
2330 *x += tiles % pitch_tiles * tile_width;
2331
2332 return new_offset;
2333}
2334
2335/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002336 * Computes the linear offset to the base tile and adjusts
2337 * x, y. bytes per pixel is assumed to be a power-of-two.
2338 *
2339 * In the 90/270 rotated case, x and y are assumed
2340 * to be already rotated to match the rotated GTT view, and
2341 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002342 *
2343 * This function is used when computing the derived information
2344 * under intel_framebuffer, so using any of that information
2345 * here is not allowed. Anything under drm_framebuffer can be
2346 * used. This is why the user has to pass in the pitch since it
2347 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002348 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002349static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2350 int *x, int *y,
2351 const struct drm_framebuffer *fb, int plane,
2352 unsigned int pitch,
2353 unsigned int rotation,
2354 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002355{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002356 uint64_t fb_modifier = fb->modifier[plane];
2357 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002358 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002359
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002360 if (alignment)
2361 alignment--;
2362
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002363 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002364 unsigned int tile_size, tile_width, tile_height;
2365 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002366
Ville Syrjäläd8433102016-01-12 21:08:35 +02002367 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002368 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2369 fb_modifier, cpp);
2370
2371 if (intel_rotation_90_or_270(rotation)) {
2372 pitch_tiles = pitch / tile_height;
2373 swap(tile_width, tile_height);
2374 } else {
2375 pitch_tiles = pitch / (tile_width * cpp);
2376 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002377
Ville Syrjäläd8433102016-01-12 21:08:35 +02002378 tile_rows = *y / tile_height;
2379 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002380
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002381 tiles = *x / tile_width;
2382 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002383
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002384 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2385 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002386
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002387 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2388 tile_size, pitch_tiles,
2389 offset, offset_aligned);
2390 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002391 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002392 offset_aligned = offset & ~alignment;
2393
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002394 *y = (offset & alignment) / pitch;
2395 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002396 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002397
2398 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002399}
2400
Ville Syrjälä6687c902015-09-15 13:16:41 +03002401u32 intel_compute_tile_offset(int *x, int *y,
2402 const struct drm_framebuffer *fb, int plane,
2403 unsigned int pitch,
2404 unsigned int rotation)
2405{
2406 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2407 u32 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
2408
2409 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2410 rotation, alignment);
2411}
2412
2413/* Convert the fb->offset[] linear offset into x/y offsets */
2414static void intel_fb_offset_to_xy(int *x, int *y,
2415 const struct drm_framebuffer *fb, int plane)
2416{
2417 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2418 unsigned int pitch = fb->pitches[plane];
2419 u32 linear_offset = fb->offsets[plane];
2420
2421 *y = linear_offset / pitch;
2422 *x = linear_offset % pitch / cpp;
2423}
2424
2425static int
2426intel_fill_fb_info(struct drm_i915_private *dev_priv,
2427 struct drm_framebuffer *fb)
2428{
2429 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2430 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2431 u32 gtt_offset_rotated = 0;
2432 unsigned int max_size = 0;
2433 uint32_t format = fb->pixel_format;
2434 int i, num_planes = drm_format_num_planes(format);
2435 unsigned int tile_size = intel_tile_size(dev_priv);
2436
2437 for (i = 0; i < num_planes; i++) {
2438 unsigned int width, height;
2439 unsigned int cpp, size;
2440 u32 offset;
2441 int x, y;
2442
2443 cpp = drm_format_plane_cpp(format, i);
2444 width = drm_format_plane_width(fb->width, format, i);
2445 height = drm_format_plane_height(fb->height, format, i);
2446
2447 intel_fb_offset_to_xy(&x, &y, fb, i);
2448
2449 /*
2450 * First pixel of the framebuffer from
2451 * the start of the normal gtt mapping.
2452 */
2453 intel_fb->normal[i].x = x;
2454 intel_fb->normal[i].y = y;
2455
2456 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2457 fb, 0, fb->pitches[i],
2458 BIT(DRM_ROTATE_0), tile_size);
2459 offset /= tile_size;
2460
2461 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2462 unsigned int tile_width, tile_height;
2463 unsigned int pitch_tiles;
2464 struct drm_rect r;
2465
2466 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2467 fb->modifier[i], cpp);
2468
2469 rot_info->plane[i].offset = offset;
2470 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2471 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2472 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2473
2474 intel_fb->rotated[i].pitch =
2475 rot_info->plane[i].height * tile_height;
2476
2477 /* how many tiles does this plane need */
2478 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2479 /*
2480 * If the plane isn't horizontally tile aligned,
2481 * we need one more tile.
2482 */
2483 if (x != 0)
2484 size++;
2485
2486 /* rotate the x/y offsets to match the GTT view */
2487 r.x1 = x;
2488 r.y1 = y;
2489 r.x2 = x + width;
2490 r.y2 = y + height;
2491 drm_rect_rotate(&r,
2492 rot_info->plane[i].width * tile_width,
2493 rot_info->plane[i].height * tile_height,
2494 BIT(DRM_ROTATE_270));
2495 x = r.x1;
2496 y = r.y1;
2497
2498 /* rotate the tile dimensions to match the GTT view */
2499 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2500 swap(tile_width, tile_height);
2501
2502 /*
2503 * We only keep the x/y offsets, so push all of the
2504 * gtt offset into the x/y offsets.
2505 */
2506 intel_adjust_tile_offset(&x, &y, tile_size,
2507 tile_width, tile_height, pitch_tiles,
2508 gtt_offset_rotated * tile_size, 0);
2509
2510 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2511
2512 /*
2513 * First pixel of the framebuffer from
2514 * the start of the rotated gtt mapping.
2515 */
2516 intel_fb->rotated[i].x = x;
2517 intel_fb->rotated[i].y = y;
2518 } else {
2519 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2520 x * cpp, tile_size);
2521 }
2522
2523 /* how many tiles in total needed in the bo */
2524 max_size = max(max_size, offset + size);
2525 }
2526
2527 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2528 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2529 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2530 return -EINVAL;
2531 }
2532
2533 return 0;
2534}
2535
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002536static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002537{
2538 switch (format) {
2539 case DISPPLANE_8BPP:
2540 return DRM_FORMAT_C8;
2541 case DISPPLANE_BGRX555:
2542 return DRM_FORMAT_XRGB1555;
2543 case DISPPLANE_BGRX565:
2544 return DRM_FORMAT_RGB565;
2545 default:
2546 case DISPPLANE_BGRX888:
2547 return DRM_FORMAT_XRGB8888;
2548 case DISPPLANE_RGBX888:
2549 return DRM_FORMAT_XBGR8888;
2550 case DISPPLANE_BGRX101010:
2551 return DRM_FORMAT_XRGB2101010;
2552 case DISPPLANE_RGBX101010:
2553 return DRM_FORMAT_XBGR2101010;
2554 }
2555}
2556
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002557static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2558{
2559 switch (format) {
2560 case PLANE_CTL_FORMAT_RGB_565:
2561 return DRM_FORMAT_RGB565;
2562 default:
2563 case PLANE_CTL_FORMAT_XRGB_8888:
2564 if (rgb_order) {
2565 if (alpha)
2566 return DRM_FORMAT_ABGR8888;
2567 else
2568 return DRM_FORMAT_XBGR8888;
2569 } else {
2570 if (alpha)
2571 return DRM_FORMAT_ARGB8888;
2572 else
2573 return DRM_FORMAT_XRGB8888;
2574 }
2575 case PLANE_CTL_FORMAT_XRGB_2101010:
2576 if (rgb_order)
2577 return DRM_FORMAT_XBGR2101010;
2578 else
2579 return DRM_FORMAT_XRGB2101010;
2580 }
2581}
2582
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002583static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002584intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2585 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002586{
2587 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002588 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002589 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002590 struct drm_i915_gem_object *obj = NULL;
2591 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002592 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002593 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2594 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2595 PAGE_SIZE);
2596
2597 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002598
Chris Wilsonff2652e2014-03-10 08:07:02 +00002599 if (plane_config->size == 0)
2600 return false;
2601
Paulo Zanoni3badb492015-09-23 12:52:23 -03002602 /* If the FB is too big, just don't use it since fbdev is not very
2603 * important and we should probably use that space with FBC or other
2604 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002605 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002606 return false;
2607
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002608 mutex_lock(&dev->struct_mutex);
2609
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002610 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2611 base_aligned,
2612 base_aligned,
2613 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002614 if (!obj) {
2615 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002616 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002617 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002618
Chris Wilson3e510a82016-08-05 10:14:23 +01002619 if (plane_config->tiling == I915_TILING_X)
2620 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002621
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002622 mode_cmd.pixel_format = fb->pixel_format;
2623 mode_cmd.width = fb->width;
2624 mode_cmd.height = fb->height;
2625 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002626 mode_cmd.modifier[0] = fb->modifier[0];
2627 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002628
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002629 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002630 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002631 DRM_DEBUG_KMS("intel fb init failed\n");
2632 goto out_unref_obj;
2633 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002634
Jesse Barnes46f297f2014-03-07 08:57:48 -08002635 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002636
Daniel Vetterf6936e22015-03-26 12:17:05 +01002637 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002638 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002639
2640out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002641 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002642 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002643 return false;
2644}
2645
Daniel Vetter5a21b662016-05-24 17:13:53 +02002646/* Update plane->state->fb to match plane->fb after driver-internal updates */
2647static void
2648update_state_fb(struct drm_plane *plane)
2649{
2650 if (plane->fb == plane->state->fb)
2651 return;
2652
2653 if (plane->state->fb)
2654 drm_framebuffer_unreference(plane->state->fb);
2655 plane->state->fb = plane->fb;
2656 if (plane->state->fb)
2657 drm_framebuffer_reference(plane->state->fb);
2658}
2659
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002660static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002661intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2662 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002663{
2664 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002665 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002666 struct drm_crtc *c;
2667 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002668 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002669 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002670 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002671 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2672 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002673 struct intel_plane_state *intel_state =
2674 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002675 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002676
Damien Lespiau2d140302015-02-05 17:22:18 +00002677 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002678 return;
2679
Daniel Vetterf6936e22015-03-26 12:17:05 +01002680 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002681 fb = &plane_config->fb->base;
2682 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002683 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002684
Damien Lespiau2d140302015-02-05 17:22:18 +00002685 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002686
2687 /*
2688 * Failed to alloc the obj, check to see if we should share
2689 * an fb with another CRTC instead
2690 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002691 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002692 i = to_intel_crtc(c);
2693
2694 if (c == &intel_crtc->base)
2695 continue;
2696
Matt Roper2ff8fde2014-07-08 07:50:07 -07002697 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002698 continue;
2699
Daniel Vetter88595ac2015-03-26 12:42:24 +01002700 fb = c->primary->fb;
2701 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002702 continue;
2703
Daniel Vetter88595ac2015-03-26 12:42:24 +01002704 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002705 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002706 drm_framebuffer_reference(fb);
2707 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002708 }
2709 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002710
Matt Roper200757f2015-12-03 11:37:36 -08002711 /*
2712 * We've failed to reconstruct the BIOS FB. Current display state
2713 * indicates that the primary plane is visible, but has a NULL FB,
2714 * which will lead to problems later if we don't fix it up. The
2715 * simplest solution is to just disable the primary plane now and
2716 * pretend the BIOS never had it enabled.
2717 */
2718 to_intel_plane_state(plane_state)->visible = false;
2719 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002720 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002721 intel_plane->disable_plane(primary, &intel_crtc->base);
2722
Daniel Vetter88595ac2015-03-26 12:42:24 +01002723 return;
2724
2725valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002726 plane_state->src_x = 0;
2727 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002728 plane_state->src_w = fb->width << 16;
2729 plane_state->src_h = fb->height << 16;
2730
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002731 plane_state->crtc_x = 0;
2732 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002733 plane_state->crtc_w = fb->width;
2734 plane_state->crtc_h = fb->height;
2735
Matt Roper0a8d8a82015-12-03 11:37:38 -08002736 intel_state->src.x1 = plane_state->src_x;
2737 intel_state->src.y1 = plane_state->src_y;
2738 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2739 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2740 intel_state->dst.x1 = plane_state->crtc_x;
2741 intel_state->dst.y1 = plane_state->crtc_y;
2742 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2743 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2744
Daniel Vetter88595ac2015-03-26 12:42:24 +01002745 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002746 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002747 dev_priv->preserve_bios_swizzle = true;
2748
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002749 drm_framebuffer_reference(fb);
2750 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002751 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002752 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002753 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2754 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002755}
2756
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002757static void i9xx_update_primary_plane(struct drm_plane *primary,
2758 const struct intel_crtc_state *crtc_state,
2759 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002760{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002761 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002762 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2764 struct drm_framebuffer *fb = plane_state->base.fb;
2765 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002766 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002767 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002768 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002769 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002770 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002771 int x = plane_state->src.x1 >> 16;
2772 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002773
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002774 dspcntr = DISPPLANE_GAMMA_ENABLE;
2775
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002776 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002777
2778 if (INTEL_INFO(dev)->gen < 4) {
2779 if (intel_crtc->pipe == PIPE_B)
2780 dspcntr |= DISPPLANE_SEL_PIPE_B;
2781
2782 /* pipesrc and dspsize control the size that is scaled from,
2783 * which should always be the user's requested size.
2784 */
2785 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002786 ((crtc_state->pipe_src_h - 1) << 16) |
2787 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002788 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002789 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2790 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002791 ((crtc_state->pipe_src_h - 1) << 16) |
2792 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002793 I915_WRITE(PRIMPOS(plane), 0);
2794 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002795 }
2796
Ville Syrjälä57779d02012-10-31 17:50:14 +02002797 switch (fb->pixel_format) {
2798 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002799 dspcntr |= DISPPLANE_8BPP;
2800 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002801 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002803 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002804 case DRM_FORMAT_RGB565:
2805 dspcntr |= DISPPLANE_BGRX565;
2806 break;
2807 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 dspcntr |= DISPPLANE_BGRX888;
2809 break;
2810 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 dspcntr |= DISPPLANE_RGBX888;
2812 break;
2813 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 dspcntr |= DISPPLANE_BGRX101010;
2815 break;
2816 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002817 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002818 break;
2819 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002820 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002821 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822
Chris Wilson3e510a82016-08-05 10:14:23 +01002823 if (INTEL_INFO(dev)->gen >= 4 && i915_gem_object_is_tiled(obj))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002824 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002825
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002826 if (IS_G4X(dev))
2827 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2828
Ville Syrjälä6687c902015-09-15 13:16:41 +03002829 intel_add_fb_offsets(&x, &y, fb, 0, rotation);
Jesse Barnes81255562010-08-02 12:07:50 -07002830
Ville Syrjälä6687c902015-09-15 13:16:41 +03002831 if (INTEL_INFO(dev)->gen >= 4)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002832 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002833 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002834 fb->pitches[0], rotation);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002835
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002836 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302837 dspcntr |= DISPPLANE_ROTATE_180;
2838
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002839 x += (crtc_state->pipe_src_w - 1);
2840 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302841 }
2842
Ville Syrjälä6687c902015-09-15 13:16:41 +03002843 linear_offset = intel_fb_xy_to_linear(x, y, fb, 0);
2844
2845 if (INTEL_INFO(dev)->gen < 4)
2846 intel_crtc->dspaddr_offset = linear_offset;
2847
Paulo Zanoni2db33662015-09-14 15:20:03 -03002848 intel_crtc->adjusted_x = x;
2849 intel_crtc->adjusted_y = y;
2850
Sonika Jindal48404c12014-08-22 14:06:04 +05302851 I915_WRITE(reg, dspcntr);
2852
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002853 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002854 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002855 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03002856 intel_fb_gtt_offset(fb, rotation) +
2857 intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002858 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002859 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002860 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002861 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002862 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002863}
2864
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002865static void i9xx_disable_primary_plane(struct drm_plane *primary,
2866 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002867{
2868 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002869 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002871 int plane = intel_crtc->plane;
2872
2873 I915_WRITE(DSPCNTR(plane), 0);
2874 if (INTEL_INFO(dev_priv)->gen >= 4)
2875 I915_WRITE(DSPSURF(plane), 0);
2876 else
2877 I915_WRITE(DSPADDR(plane), 0);
2878 POSTING_READ(DSPCNTR(plane));
2879}
2880
2881static void ironlake_update_primary_plane(struct drm_plane *primary,
2882 const struct intel_crtc_state *crtc_state,
2883 const struct intel_plane_state *plane_state)
2884{
2885 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002886 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2888 struct drm_framebuffer *fb = plane_state->base.fb;
2889 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002890 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002891 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002892 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002893 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002894 unsigned int rotation = plane_state->base.rotation;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002895 int x = plane_state->src.x1 >> 16;
2896 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002897
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002898 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002899 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002900
2901 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2902 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2903
Ville Syrjälä57779d02012-10-31 17:50:14 +02002904 switch (fb->pixel_format) {
2905 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002906 dspcntr |= DISPPLANE_8BPP;
2907 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002908 case DRM_FORMAT_RGB565:
2909 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002910 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002911 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002912 dspcntr |= DISPPLANE_BGRX888;
2913 break;
2914 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002915 dspcntr |= DISPPLANE_RGBX888;
2916 break;
2917 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002918 dspcntr |= DISPPLANE_BGRX101010;
2919 break;
2920 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002921 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002922 break;
2923 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002924 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002925 }
2926
Chris Wilson3e510a82016-08-05 10:14:23 +01002927 if (i915_gem_object_is_tiled(obj))
Jesse Barnes17638cd2011-06-24 12:19:23 -07002928 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002929
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002930 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002931 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002932
Ville Syrjälä6687c902015-09-15 13:16:41 +03002933 intel_add_fb_offsets(&x, &y, fb, 0, rotation);
2934
Daniel Vetterc2c75132012-07-05 12:17:30 +02002935 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002936 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002937 fb->pitches[0], rotation);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002938
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002939 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302940 dspcntr |= DISPPLANE_ROTATE_180;
2941
2942 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002943 x += (crtc_state->pipe_src_w - 1);
2944 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302945 }
2946 }
2947
Ville Syrjälä6687c902015-09-15 13:16:41 +03002948 linear_offset = intel_fb_xy_to_linear(x, y, fb, 0);
2949
Paulo Zanoni2db33662015-09-14 15:20:03 -03002950 intel_crtc->adjusted_x = x;
2951 intel_crtc->adjusted_y = y;
2952
Sonika Jindal48404c12014-08-22 14:06:04 +05302953 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002954
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002955 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002956 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03002957 intel_fb_gtt_offset(fb, rotation) +
2958 intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002959 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002960 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2961 } else {
2962 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2963 I915_WRITE(DSPLINOFF(plane), linear_offset);
2964 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002965 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002966}
2967
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002968u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2969 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002970{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002971 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2972 return 64;
2973 } else {
2974 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002975
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002976 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002977 }
2978}
2979
Ville Syrjälä6687c902015-09-15 13:16:41 +03002980u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
2981 unsigned int rotation)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002982{
Ville Syrjälä6687c902015-09-15 13:16:41 +03002983 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetterce7f1722015-10-14 16:51:06 +02002984 struct i915_ggtt_view view;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002985 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002986
Ville Syrjälä6687c902015-09-15 13:16:41 +03002987 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002988
Ville Syrjälä6687c902015-09-15 13:16:41 +03002989 offset = i915_gem_obj_ggtt_offset_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002990
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002991 WARN_ON(upper_32_bits(offset));
2992
2993 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002994}
2995
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002996static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2997{
2998 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002999 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003000
3001 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3002 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3003 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003004}
3005
Chandra Kondurua1b22782015-04-07 15:28:45 -07003006/*
3007 * This function detaches (aka. unbinds) unused scalers in hardware
3008 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003009static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003010{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003011 struct intel_crtc_scaler_state *scaler_state;
3012 int i;
3013
Chandra Kondurua1b22782015-04-07 15:28:45 -07003014 scaler_state = &intel_crtc->config->scaler_state;
3015
3016 /* loop through and disable scalers that aren't in use */
3017 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003018 if (!scaler_state->scalers[i].in_use)
3019 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003020 }
3021}
3022
Chandra Konduru6156a452015-04-27 13:48:39 -07003023u32 skl_plane_ctl_format(uint32_t pixel_format)
3024{
Chandra Konduru6156a452015-04-27 13:48:39 -07003025 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003026 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003027 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003028 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003029 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003030 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003031 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003032 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003033 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 /*
3035 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3036 * to be already pre-multiplied. We need to add a knob (or a different
3037 * DRM_FORMAT) for user-space to configure that.
3038 */
3039 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003040 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003041 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003042 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003043 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003044 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003045 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003046 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003047 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003048 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003049 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003050 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003051 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003052 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003053 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003054 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003055 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003056 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003058 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003059 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003060
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003061 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003062}
3063
3064u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3065{
Chandra Konduru6156a452015-04-27 13:48:39 -07003066 switch (fb_modifier) {
3067 case DRM_FORMAT_MOD_NONE:
3068 break;
3069 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003070 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003071 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003072 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003073 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003074 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003075 default:
3076 MISSING_CASE(fb_modifier);
3077 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003078
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003079 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003080}
3081
3082u32 skl_plane_ctl_rotation(unsigned int rotation)
3083{
Chandra Konduru6156a452015-04-27 13:48:39 -07003084 switch (rotation) {
3085 case BIT(DRM_ROTATE_0):
3086 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303087 /*
3088 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3089 * while i915 HW rotation is clockwise, thats why this swapping.
3090 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003091 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303092 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003093 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003094 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003095 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303096 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003097 default:
3098 MISSING_CASE(rotation);
3099 }
3100
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003101 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003102}
3103
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003104static void skylake_update_primary_plane(struct drm_plane *plane,
3105 const struct intel_crtc_state *crtc_state,
3106 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003107{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003108 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003109 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3111 struct drm_framebuffer *fb = plane_state->base.fb;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003112 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303113 u32 plane_ctl, stride_div, stride;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003114 unsigned int rotation = plane_state->base.rotation;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003115 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003116 int scaler_id = plane_state->scaler_id;
3117 int src_x = plane_state->src.x1 >> 16;
3118 int src_y = plane_state->src.y1 >> 16;
3119 int src_w = drm_rect_width(&plane_state->src) >> 16;
3120 int src_h = drm_rect_height(&plane_state->src) >> 16;
3121 int dst_x = plane_state->dst.x1;
3122 int dst_y = plane_state->dst.y1;
3123 int dst_w = drm_rect_width(&plane_state->dst);
3124 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003125
3126 plane_ctl = PLANE_CTL_ENABLE |
3127 PLANE_CTL_PIPE_GAMMA_ENABLE |
3128 PLANE_CTL_PIPE_CSC_ENABLE;
3129
Chandra Konduru6156a452015-04-27 13:48:39 -07003130 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3131 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003132 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003133 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003134
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303135 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03003136 struct drm_rect r = {
3137 .x1 = src_x,
3138 .x2 = src_x + src_w,
3139 .y1 = src_y,
3140 .y2 = src_y + src_h,
3141 };
Ville Syrjälä832be822016-01-12 21:08:33 +02003142 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003143 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Ville Syrjälä832be822016-01-12 21:08:33 +02003144
Ville Syrjälä6687c902015-09-15 13:16:41 +03003145 /* Rotate src coordinates to match rotated GTT view */
3146 drm_rect_rotate(&r, fb->width, fb->height, BIT(DRM_ROTATE_270));
3147
3148 src_x = r.x1;
3149 src_y = r.y1;
3150 src_w = drm_rect_width(&r);
3151 src_h = drm_rect_height(&r);
3152
3153 stride_div = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3154 stride = intel_fb->rotated[0].pitch;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303155 } else {
Ville Syrjälä6687c902015-09-15 13:16:41 +03003156 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3157 fb->pixel_format);
3158 stride = fb->pitches[0];
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303159 }
Damien Lespiaub3218032015-02-27 11:15:18 +00003160
Ville Syrjälä6687c902015-09-15 13:16:41 +03003161 intel_add_fb_offsets(&src_x, &src_y, fb, 0, rotation);
3162 surf_addr = intel_compute_tile_offset(&src_x, &src_y, fb, 0,
3163 stride, rotation);
3164
3165 /* Sizes are 0 based */
3166 src_w--;
3167 src_h--;
3168 dst_w--;
3169 dst_h--;
3170
3171 intel_crtc->adjusted_x = src_x;
3172 intel_crtc->adjusted_y = src_y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003173
Damien Lespiau70d21f02013-07-03 21:06:04 +01003174 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003175 I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
3176 I915_WRITE(PLANE_STRIDE(pipe, 0), stride / stride_div);
3177 I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
Chandra Konduru6156a452015-04-27 13:48:39 -07003178
3179 if (scaler_id >= 0) {
3180 uint32_t ps_ctrl = 0;
3181
3182 WARN_ON(!dst_w || !dst_h);
3183 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3184 crtc_state->scaler_state.scalers[scaler_id].mode;
3185 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3186 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3187 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3188 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3189 I915_WRITE(PLANE_POS(pipe, 0), 0);
3190 } else {
3191 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3192 }
3193
Ville Syrjälä6687c902015-09-15 13:16:41 +03003194 I915_WRITE(PLANE_SURF(pipe, 0),
3195 intel_fb_gtt_offset(fb, rotation) + surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003196
3197 POSTING_READ(PLANE_SURF(pipe, 0));
3198}
3199
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003200static void skylake_disable_primary_plane(struct drm_plane *primary,
3201 struct drm_crtc *crtc)
3202{
3203 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003204 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003205 int pipe = to_intel_crtc(crtc)->pipe;
3206
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003207 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3208 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3209 POSTING_READ(PLANE_SURF(pipe, 0));
3210}
3211
Jesse Barnes17638cd2011-06-24 12:19:23 -07003212/* Assume fb object is pinned & idle & fenced and just update base pointers */
3213static int
3214intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3215 int x, int y, enum mode_set_atomic state)
3216{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003217 /* Support for kgdboc is disabled, this needs a major rework. */
3218 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003219
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003220 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003221}
3222
Daniel Vetter5a21b662016-05-24 17:13:53 +02003223static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3224{
3225 struct intel_crtc *crtc;
3226
Chris Wilson91c8a322016-07-05 10:40:23 +01003227 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003228 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3229}
3230
Ville Syrjälä75147472014-11-24 18:28:11 +02003231static void intel_update_primary_planes(struct drm_device *dev)
3232{
Ville Syrjälä75147472014-11-24 18:28:11 +02003233 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003234
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003235 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003236 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003237 struct intel_plane_state *plane_state =
3238 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003239
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003240 if (plane_state->visible)
3241 plane->update_plane(&plane->base,
3242 to_intel_crtc_state(crtc->state),
3243 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003244 }
3245}
3246
Maarten Lankhorst73974892016-08-05 23:28:27 +03003247static int
3248__intel_display_resume(struct drm_device *dev,
3249 struct drm_atomic_state *state)
3250{
3251 struct drm_crtc_state *crtc_state;
3252 struct drm_crtc *crtc;
3253 int i, ret;
3254
3255 intel_modeset_setup_hw_state(dev);
3256 i915_redisable_vga(dev);
3257
3258 if (!state)
3259 return 0;
3260
3261 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3262 /*
3263 * Force recalculation even if we restore
3264 * current state. With fast modeset this may not result
3265 * in a modeset when the state is compatible.
3266 */
3267 crtc_state->mode_changed = true;
3268 }
3269
3270 /* ignore any reset values/BIOS leftovers in the WM registers */
3271 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3272
3273 ret = drm_atomic_commit(state);
3274
3275 WARN_ON(ret == -EDEADLK);
3276 return ret;
3277}
3278
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003279static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3280{
Ville Syrjäläae981042016-08-05 23:28:30 +03003281 return intel_has_gpu_reset(dev_priv) &&
3282 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003283}
3284
Chris Wilsonc0336662016-05-06 15:40:21 +01003285void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003286{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003287 struct drm_device *dev = &dev_priv->drm;
3288 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3289 struct drm_atomic_state *state;
3290 int ret;
3291
Maarten Lankhorst73974892016-08-05 23:28:27 +03003292 /*
3293 * Need mode_config.mutex so that we don't
3294 * trample ongoing ->detect() and whatnot.
3295 */
3296 mutex_lock(&dev->mode_config.mutex);
3297 drm_modeset_acquire_init(ctx, 0);
3298 while (1) {
3299 ret = drm_modeset_lock_all_ctx(dev, ctx);
3300 if (ret != -EDEADLK)
3301 break;
3302
3303 drm_modeset_backoff(ctx);
3304 }
3305
3306 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003307 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003308 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003309 return;
3310
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003311 /*
3312 * Disabling the crtcs gracefully seems nicer. Also the
3313 * g33 docs say we should at least disable all the planes.
3314 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003315 state = drm_atomic_helper_duplicate_state(dev, ctx);
3316 if (IS_ERR(state)) {
3317 ret = PTR_ERR(state);
3318 state = NULL;
3319 DRM_ERROR("Duplicating state failed with %i\n", ret);
3320 goto err;
3321 }
3322
3323 ret = drm_atomic_helper_disable_all(dev, ctx);
3324 if (ret) {
3325 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3326 goto err;
3327 }
3328
3329 dev_priv->modeset_restore_state = state;
3330 state->acquire_ctx = ctx;
3331 return;
3332
3333err:
3334 drm_atomic_state_free(state);
Ville Syrjälä75147472014-11-24 18:28:11 +02003335}
3336
Chris Wilsonc0336662016-05-06 15:40:21 +01003337void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003338{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003339 struct drm_device *dev = &dev_priv->drm;
3340 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3341 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3342 int ret;
3343
Daniel Vetter5a21b662016-05-24 17:13:53 +02003344 /*
3345 * Flips in the rings will be nuked by the reset,
3346 * so complete all pending flips so that user space
3347 * will get its events and not get stuck.
3348 */
3349 intel_complete_page_flips(dev_priv);
3350
Maarten Lankhorst73974892016-08-05 23:28:27 +03003351 dev_priv->modeset_restore_state = NULL;
3352
Ville Syrjälä75147472014-11-24 18:28:11 +02003353 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003354 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003355 if (!state) {
3356 /*
3357 * Flips in the rings have been nuked by the reset,
3358 * so update the base address of all primary
3359 * planes to the the last fb to make sure we're
3360 * showing the correct fb after a reset.
3361 *
3362 * FIXME: Atomic will make this obsolete since we won't schedule
3363 * CS-based flips (which might get lost in gpu resets) any more.
3364 */
3365 intel_update_primary_planes(dev);
3366 } else {
3367 ret = __intel_display_resume(dev, state);
3368 if (ret)
3369 DRM_ERROR("Restoring old state failed with %i\n", ret);
3370 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003371 } else {
3372 /*
3373 * The display has been reset as well,
3374 * so need a full re-initialization.
3375 */
3376 intel_runtime_pm_disable_interrupts(dev_priv);
3377 intel_runtime_pm_enable_interrupts(dev_priv);
3378
3379 intel_modeset_init_hw(dev);
3380
3381 spin_lock_irq(&dev_priv->irq_lock);
3382 if (dev_priv->display.hpd_irq_setup)
3383 dev_priv->display.hpd_irq_setup(dev_priv);
3384 spin_unlock_irq(&dev_priv->irq_lock);
3385
3386 ret = __intel_display_resume(dev, state);
3387 if (ret)
3388 DRM_ERROR("Restoring old state failed with %i\n", ret);
3389
3390 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003391 }
3392
Maarten Lankhorst73974892016-08-05 23:28:27 +03003393 drm_modeset_drop_locks(ctx);
3394 drm_modeset_acquire_fini(ctx);
3395 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003396}
3397
Chris Wilson7d5e3792014-03-04 13:15:08 +00003398static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3399{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003400 struct drm_device *dev = crtc->dev;
3401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3402 unsigned reset_counter;
3403 bool pending;
3404
3405 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3406 if (intel_crtc->reset_counter != reset_counter)
3407 return false;
3408
3409 spin_lock_irq(&dev->event_lock);
3410 pending = to_intel_crtc(crtc)->flip_work != NULL;
3411 spin_unlock_irq(&dev->event_lock);
3412
3413 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003414}
3415
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003416static void intel_update_pipe_config(struct intel_crtc *crtc,
3417 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003418{
3419 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003420 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003421 struct intel_crtc_state *pipe_config =
3422 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003423
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003424 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3425 crtc->base.mode = crtc->base.state->mode;
3426
3427 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3428 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3429 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003430
3431 /*
3432 * Update pipe size and adjust fitter if needed: the reason for this is
3433 * that in compute_mode_changes we check the native mode (not the pfit
3434 * mode) to see if we can flip rather than do a full mode set. In the
3435 * fastboot case, we'll flip, but if we don't update the pipesrc and
3436 * pfit state, we'll end up with a big fb scanned out into the wrong
3437 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003438 */
3439
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003440 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003441 ((pipe_config->pipe_src_w - 1) << 16) |
3442 (pipe_config->pipe_src_h - 1));
3443
3444 /* on skylake this is done by detaching scalers */
3445 if (INTEL_INFO(dev)->gen >= 9) {
3446 skl_detach_scalers(crtc);
3447
3448 if (pipe_config->pch_pfit.enabled)
3449 skylake_pfit_enable(crtc);
3450 } else if (HAS_PCH_SPLIT(dev)) {
3451 if (pipe_config->pch_pfit.enabled)
3452 ironlake_pfit_enable(crtc);
3453 else if (old_crtc_state->pch_pfit.enabled)
3454 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003455 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003456}
3457
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003458static void intel_fdi_normal_train(struct drm_crtc *crtc)
3459{
3460 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003461 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3463 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003464 i915_reg_t reg;
3465 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003466
3467 /* enable normal train */
3468 reg = FDI_TX_CTL(pipe);
3469 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003470 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003471 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3472 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003473 } else {
3474 temp &= ~FDI_LINK_TRAIN_NONE;
3475 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003476 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003477 I915_WRITE(reg, temp);
3478
3479 reg = FDI_RX_CTL(pipe);
3480 temp = I915_READ(reg);
3481 if (HAS_PCH_CPT(dev)) {
3482 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3483 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3484 } else {
3485 temp &= ~FDI_LINK_TRAIN_NONE;
3486 temp |= FDI_LINK_TRAIN_NONE;
3487 }
3488 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3489
3490 /* wait one idle pattern time */
3491 POSTING_READ(reg);
3492 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003493
3494 /* IVB wants error correction enabled */
3495 if (IS_IVYBRIDGE(dev))
3496 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3497 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003498}
3499
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500/* The FDI link training functions for ILK/Ibexpeak. */
3501static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3502{
3503 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003504 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3506 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003507 i915_reg_t reg;
3508 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003510 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003511 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003512
Adam Jacksone1a44742010-06-25 15:32:14 -04003513 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3514 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003515 reg = FDI_RX_IMR(pipe);
3516 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003517 temp &= ~FDI_RX_SYMBOL_LOCK;
3518 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 I915_WRITE(reg, temp);
3520 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003521 udelay(150);
3522
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003523 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 reg = FDI_TX_CTL(pipe);
3525 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003526 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003527 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 temp &= ~FDI_LINK_TRAIN_NONE;
3529 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 reg = FDI_RX_CTL(pipe);
3533 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 temp &= ~FDI_LINK_TRAIN_NONE;
3535 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3537
3538 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 udelay(150);
3540
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003541 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003542 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3543 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3544 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003545
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003547 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003548 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3550
3551 if ((temp & FDI_RX_BIT_LOCK)) {
3552 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003553 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003554 break;
3555 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003557 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003558 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559
3560 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 reg = FDI_TX_CTL(pipe);
3562 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563 temp &= ~FDI_LINK_TRAIN_NONE;
3564 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003565 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003566
Chris Wilson5eddb702010-09-11 13:48:45 +01003567 reg = FDI_RX_CTL(pipe);
3568 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003569 temp &= ~FDI_LINK_TRAIN_NONE;
3570 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003571 I915_WRITE(reg, temp);
3572
3573 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 udelay(150);
3575
Chris Wilson5eddb702010-09-11 13:48:45 +01003576 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003577 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003578 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3580
3581 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003582 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003583 DRM_DEBUG_KMS("FDI train 2 done.\n");
3584 break;
3585 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003587 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003588 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003589
3590 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003591
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592}
3593
Akshay Joshi0206e352011-08-16 15:34:10 -04003594static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003595 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3596 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3597 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3598 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3599};
3600
3601/* The FDI link training functions for SNB/Cougarpoint. */
3602static void gen6_fdi_link_train(struct drm_crtc *crtc)
3603{
3604 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003605 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3607 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003608 i915_reg_t reg;
3609 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003610
Adam Jacksone1a44742010-06-25 15:32:14 -04003611 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3612 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003613 reg = FDI_RX_IMR(pipe);
3614 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003615 temp &= ~FDI_RX_SYMBOL_LOCK;
3616 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003617 I915_WRITE(reg, temp);
3618
3619 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003620 udelay(150);
3621
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003622 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003623 reg = FDI_TX_CTL(pipe);
3624 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003625 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003626 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627 temp &= ~FDI_LINK_TRAIN_NONE;
3628 temp |= FDI_LINK_TRAIN_PATTERN_1;
3629 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3630 /* SNB-B */
3631 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003632 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003633
Daniel Vetterd74cf322012-10-26 10:58:13 +02003634 I915_WRITE(FDI_RX_MISC(pipe),
3635 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3636
Chris Wilson5eddb702010-09-11 13:48:45 +01003637 reg = FDI_RX_CTL(pipe);
3638 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003639 if (HAS_PCH_CPT(dev)) {
3640 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3641 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3642 } else {
3643 temp &= ~FDI_LINK_TRAIN_NONE;
3644 temp |= FDI_LINK_TRAIN_PATTERN_1;
3645 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003646 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3647
3648 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003649 udelay(150);
3650
Akshay Joshi0206e352011-08-16 15:34:10 -04003651 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003652 reg = FDI_TX_CTL(pipe);
3653 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003654 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3655 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003656 I915_WRITE(reg, temp);
3657
3658 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003659 udelay(500);
3660
Sean Paulfa37d392012-03-02 12:53:39 -05003661 for (retry = 0; retry < 5; retry++) {
3662 reg = FDI_RX_IIR(pipe);
3663 temp = I915_READ(reg);
3664 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3665 if (temp & FDI_RX_BIT_LOCK) {
3666 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3667 DRM_DEBUG_KMS("FDI train 1 done.\n");
3668 break;
3669 }
3670 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003671 }
Sean Paulfa37d392012-03-02 12:53:39 -05003672 if (retry < 5)
3673 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003674 }
3675 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003676 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003677
3678 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003679 reg = FDI_TX_CTL(pipe);
3680 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003681 temp &= ~FDI_LINK_TRAIN_NONE;
3682 temp |= FDI_LINK_TRAIN_PATTERN_2;
3683 if (IS_GEN6(dev)) {
3684 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3685 /* SNB-B */
3686 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3687 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003688 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003689
Chris Wilson5eddb702010-09-11 13:48:45 +01003690 reg = FDI_RX_CTL(pipe);
3691 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003692 if (HAS_PCH_CPT(dev)) {
3693 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3694 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3695 } else {
3696 temp &= ~FDI_LINK_TRAIN_NONE;
3697 temp |= FDI_LINK_TRAIN_PATTERN_2;
3698 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003699 I915_WRITE(reg, temp);
3700
3701 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003702 udelay(150);
3703
Akshay Joshi0206e352011-08-16 15:34:10 -04003704 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003705 reg = FDI_TX_CTL(pipe);
3706 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003707 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3708 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003709 I915_WRITE(reg, temp);
3710
3711 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003712 udelay(500);
3713
Sean Paulfa37d392012-03-02 12:53:39 -05003714 for (retry = 0; retry < 5; retry++) {
3715 reg = FDI_RX_IIR(pipe);
3716 temp = I915_READ(reg);
3717 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3718 if (temp & FDI_RX_SYMBOL_LOCK) {
3719 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3720 DRM_DEBUG_KMS("FDI train 2 done.\n");
3721 break;
3722 }
3723 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003724 }
Sean Paulfa37d392012-03-02 12:53:39 -05003725 if (retry < 5)
3726 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003727 }
3728 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003729 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003730
3731 DRM_DEBUG_KMS("FDI train done.\n");
3732}
3733
Jesse Barnes357555c2011-04-28 15:09:55 -07003734/* Manual link training for Ivy Bridge A0 parts */
3735static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003738 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3740 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003741 i915_reg_t reg;
3742 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003743
3744 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3745 for train result */
3746 reg = FDI_RX_IMR(pipe);
3747 temp = I915_READ(reg);
3748 temp &= ~FDI_RX_SYMBOL_LOCK;
3749 temp &= ~FDI_RX_BIT_LOCK;
3750 I915_WRITE(reg, temp);
3751
3752 POSTING_READ(reg);
3753 udelay(150);
3754
Daniel Vetter01a415f2012-10-27 15:58:40 +02003755 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3756 I915_READ(FDI_RX_IIR(pipe)));
3757
Jesse Barnes139ccd32013-08-19 11:04:55 -07003758 /* Try each vswing and preemphasis setting twice before moving on */
3759 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3760 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003761 reg = FDI_TX_CTL(pipe);
3762 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003763 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3764 temp &= ~FDI_TX_ENABLE;
3765 I915_WRITE(reg, temp);
3766
3767 reg = FDI_RX_CTL(pipe);
3768 temp = I915_READ(reg);
3769 temp &= ~FDI_LINK_TRAIN_AUTO;
3770 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3771 temp &= ~FDI_RX_ENABLE;
3772 I915_WRITE(reg, temp);
3773
3774 /* enable CPU FDI TX and PCH FDI RX */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003778 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003779 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003780 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003781 temp |= snb_b_fdi_train_param[j/2];
3782 temp |= FDI_COMPOSITE_SYNC;
3783 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3784
3785 I915_WRITE(FDI_RX_MISC(pipe),
3786 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3787
3788 reg = FDI_RX_CTL(pipe);
3789 temp = I915_READ(reg);
3790 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3791 temp |= FDI_COMPOSITE_SYNC;
3792 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3793
3794 POSTING_READ(reg);
3795 udelay(1); /* should be 0.5us */
3796
3797 for (i = 0; i < 4; i++) {
3798 reg = FDI_RX_IIR(pipe);
3799 temp = I915_READ(reg);
3800 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3801
3802 if (temp & FDI_RX_BIT_LOCK ||
3803 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3804 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3805 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3806 i);
3807 break;
3808 }
3809 udelay(1); /* should be 0.5us */
3810 }
3811 if (i == 4) {
3812 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3813 continue;
3814 }
3815
3816 /* Train 2 */
3817 reg = FDI_TX_CTL(pipe);
3818 temp = I915_READ(reg);
3819 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3820 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3821 I915_WRITE(reg, temp);
3822
3823 reg = FDI_RX_CTL(pipe);
3824 temp = I915_READ(reg);
3825 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3826 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003827 I915_WRITE(reg, temp);
3828
3829 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003830 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003831
Jesse Barnes139ccd32013-08-19 11:04:55 -07003832 for (i = 0; i < 4; i++) {
3833 reg = FDI_RX_IIR(pipe);
3834 temp = I915_READ(reg);
3835 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003836
Jesse Barnes139ccd32013-08-19 11:04:55 -07003837 if (temp & FDI_RX_SYMBOL_LOCK ||
3838 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3839 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3840 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3841 i);
3842 goto train_done;
3843 }
3844 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003845 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003846 if (i == 4)
3847 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003848 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003849
Jesse Barnes139ccd32013-08-19 11:04:55 -07003850train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003851 DRM_DEBUG_KMS("FDI train done.\n");
3852}
3853
Daniel Vetter88cefb62012-08-12 19:27:14 +02003854static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003855{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003856 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003857 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003858 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003859 i915_reg_t reg;
3860 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003861
Jesse Barnes0e23b992010-09-10 11:10:00 -07003862 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003863 reg = FDI_RX_CTL(pipe);
3864 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003865 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003866 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003867 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003868 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3869
3870 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003871 udelay(200);
3872
3873 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003874 temp = I915_READ(reg);
3875 I915_WRITE(reg, temp | FDI_PCDCLK);
3876
3877 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003878 udelay(200);
3879
Paulo Zanoni20749732012-11-23 15:30:38 -02003880 /* Enable CPU FDI TX PLL, always on for Ironlake */
3881 reg = FDI_TX_CTL(pipe);
3882 temp = I915_READ(reg);
3883 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3884 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003885
Paulo Zanoni20749732012-11-23 15:30:38 -02003886 POSTING_READ(reg);
3887 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003888 }
3889}
3890
Daniel Vetter88cefb62012-08-12 19:27:14 +02003891static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3892{
3893 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003894 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02003895 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003896 i915_reg_t reg;
3897 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003898
3899 /* Switch from PCDclk to Rawclk */
3900 reg = FDI_RX_CTL(pipe);
3901 temp = I915_READ(reg);
3902 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3903
3904 /* Disable CPU FDI TX PLL */
3905 reg = FDI_TX_CTL(pipe);
3906 temp = I915_READ(reg);
3907 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3908
3909 POSTING_READ(reg);
3910 udelay(100);
3911
3912 reg = FDI_RX_CTL(pipe);
3913 temp = I915_READ(reg);
3914 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3915
3916 /* Wait for the clocks to turn off. */
3917 POSTING_READ(reg);
3918 udelay(100);
3919}
3920
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003921static void ironlake_fdi_disable(struct drm_crtc *crtc)
3922{
3923 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003924 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3926 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003927 i915_reg_t reg;
3928 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003929
3930 /* disable CPU FDI tx and PCH FDI rx */
3931 reg = FDI_TX_CTL(pipe);
3932 temp = I915_READ(reg);
3933 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3934 POSTING_READ(reg);
3935
3936 reg = FDI_RX_CTL(pipe);
3937 temp = I915_READ(reg);
3938 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003939 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003940 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3941
3942 POSTING_READ(reg);
3943 udelay(100);
3944
3945 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003946 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003947 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003948
3949 /* still set train pattern 1 */
3950 reg = FDI_TX_CTL(pipe);
3951 temp = I915_READ(reg);
3952 temp &= ~FDI_LINK_TRAIN_NONE;
3953 temp |= FDI_LINK_TRAIN_PATTERN_1;
3954 I915_WRITE(reg, temp);
3955
3956 reg = FDI_RX_CTL(pipe);
3957 temp = I915_READ(reg);
3958 if (HAS_PCH_CPT(dev)) {
3959 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3960 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3961 } else {
3962 temp &= ~FDI_LINK_TRAIN_NONE;
3963 temp |= FDI_LINK_TRAIN_PATTERN_1;
3964 }
3965 /* BPC in FDI rx is consistent with that in PIPECONF */
3966 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003967 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003968 I915_WRITE(reg, temp);
3969
3970 POSTING_READ(reg);
3971 udelay(100);
3972}
3973
Chris Wilson5dce5b932014-01-20 10:17:36 +00003974bool intel_has_pending_fb_unpin(struct drm_device *dev)
3975{
3976 struct intel_crtc *crtc;
3977
3978 /* Note that we don't need to be called with mode_config.lock here
3979 * as our list of CRTC objects is static for the lifetime of the
3980 * device and so cannot disappear as we iterate. Similarly, we can
3981 * happily treat the predicates as racy, atomic checks as userspace
3982 * cannot claim and pin a new fb without at least acquring the
3983 * struct_mutex and so serialising with us.
3984 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003985 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003986 if (atomic_read(&crtc->unpin_work_count) == 0)
3987 continue;
3988
Daniel Vetter5a21b662016-05-24 17:13:53 +02003989 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003990 intel_wait_for_vblank(dev, crtc->pipe);
3991
3992 return true;
3993 }
3994
3995 return false;
3996}
3997
Daniel Vetter5a21b662016-05-24 17:13:53 +02003998static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003999{
4000 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004001 struct intel_flip_work *work = intel_crtc->flip_work;
4002
4003 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004004
4005 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07004006 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004007
4008 drm_crtc_vblank_put(&intel_crtc->base);
4009
Daniel Vetter5a21b662016-05-24 17:13:53 +02004010 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02004011 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004012
4013 trace_i915_flip_complete(intel_crtc->plane,
4014 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004015}
4016
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004017static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004018{
Chris Wilson0f911282012-04-17 10:05:38 +01004019 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004020 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004021 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004022
Daniel Vetter2c10d572012-12-20 21:24:07 +01004023 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004024
4025 ret = wait_event_interruptible_timeout(
4026 dev_priv->pending_flip_queue,
4027 !intel_crtc_has_pending_flip(crtc),
4028 60*HZ);
4029
4030 if (ret < 0)
4031 return ret;
4032
Daniel Vetter5a21b662016-05-24 17:13:53 +02004033 if (ret == 0) {
4034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4035 struct intel_flip_work *work;
4036
4037 spin_lock_irq(&dev->event_lock);
4038 work = intel_crtc->flip_work;
4039 if (work && !is_mmio_work(work)) {
4040 WARN_ONCE(1, "Removing stuck page flip\n");
4041 page_flip_completed(intel_crtc);
4042 }
4043 spin_unlock_irq(&dev->event_lock);
4044 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004045
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004046 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004047}
4048
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004049static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
4050{
4051 u32 temp;
4052
4053 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4054
4055 mutex_lock(&dev_priv->sb_lock);
4056
4057 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4058 temp |= SBI_SSCCTL_DISABLE;
4059 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4060
4061 mutex_unlock(&dev_priv->sb_lock);
4062}
4063
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004064/* Program iCLKIP clock to the desired frequency */
4065static void lpt_program_iclkip(struct drm_crtc *crtc)
4066{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004067 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004068 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004069 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4070 u32 temp;
4071
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004072 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004073
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004074 /* The iCLK virtual clock root frequency is in MHz,
4075 * but the adjusted_mode->crtc_clock in in KHz. To get the
4076 * divisors, it is necessary to divide one by another, so we
4077 * convert the virtual clock precision to KHz here for higher
4078 * precision.
4079 */
4080 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004081 u32 iclk_virtual_root_freq = 172800 * 1000;
4082 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004083 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004084
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004085 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4086 clock << auxdiv);
4087 divsel = (desired_divisor / iclk_pi_range) - 2;
4088 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004089
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004090 /*
4091 * Near 20MHz is a corner case which is
4092 * out of range for the 7-bit divisor
4093 */
4094 if (divsel <= 0x7f)
4095 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004096 }
4097
4098 /* This should not happen with any sane values */
4099 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4100 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4101 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4102 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4103
4104 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 +03004105 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004106 auxdiv,
4107 divsel,
4108 phasedir,
4109 phaseinc);
4110
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004111 mutex_lock(&dev_priv->sb_lock);
4112
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004113 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004114 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004115 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4116 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4117 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4118 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4119 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4120 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004121 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004122
4123 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004124 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004125 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4126 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004127 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004128
4129 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004130 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004131 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004132 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004133
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004134 mutex_unlock(&dev_priv->sb_lock);
4135
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004136 /* Wait for initialization time */
4137 udelay(24);
4138
4139 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4140}
4141
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004142int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4143{
4144 u32 divsel, phaseinc, auxdiv;
4145 u32 iclk_virtual_root_freq = 172800 * 1000;
4146 u32 iclk_pi_range = 64;
4147 u32 desired_divisor;
4148 u32 temp;
4149
4150 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4151 return 0;
4152
4153 mutex_lock(&dev_priv->sb_lock);
4154
4155 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4156 if (temp & SBI_SSCCTL_DISABLE) {
4157 mutex_unlock(&dev_priv->sb_lock);
4158 return 0;
4159 }
4160
4161 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4162 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4163 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4164 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4165 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4166
4167 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4168 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4169 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4170
4171 mutex_unlock(&dev_priv->sb_lock);
4172
4173 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4174
4175 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4176 desired_divisor << auxdiv);
4177}
4178
Daniel Vetter275f01b22013-05-03 11:49:47 +02004179static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4180 enum pipe pch_transcoder)
4181{
4182 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004183 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004184 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004185
4186 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4187 I915_READ(HTOTAL(cpu_transcoder)));
4188 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4189 I915_READ(HBLANK(cpu_transcoder)));
4190 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4191 I915_READ(HSYNC(cpu_transcoder)));
4192
4193 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4194 I915_READ(VTOTAL(cpu_transcoder)));
4195 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4196 I915_READ(VBLANK(cpu_transcoder)));
4197 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4198 I915_READ(VSYNC(cpu_transcoder)));
4199 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4200 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4201}
4202
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004203static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004204{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004205 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004206 uint32_t temp;
4207
4208 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004209 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004210 return;
4211
4212 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4213 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4214
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004215 temp &= ~FDI_BC_BIFURCATION_SELECT;
4216 if (enable)
4217 temp |= FDI_BC_BIFURCATION_SELECT;
4218
4219 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004220 I915_WRITE(SOUTH_CHICKEN1, temp);
4221 POSTING_READ(SOUTH_CHICKEN1);
4222}
4223
4224static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4225{
4226 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004227
4228 switch (intel_crtc->pipe) {
4229 case PIPE_A:
4230 break;
4231 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004232 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004233 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004234 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004235 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004236
4237 break;
4238 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004239 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004240
4241 break;
4242 default:
4243 BUG();
4244 }
4245}
4246
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004247/* Return which DP Port should be selected for Transcoder DP control */
4248static enum port
4249intel_trans_dp_port_sel(struct drm_crtc *crtc)
4250{
4251 struct drm_device *dev = crtc->dev;
4252 struct intel_encoder *encoder;
4253
4254 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004255 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004256 encoder->type == INTEL_OUTPUT_EDP)
4257 return enc_to_dig_port(&encoder->base)->port;
4258 }
4259
4260 return -1;
4261}
4262
Jesse Barnesf67a5592011-01-05 10:31:48 -08004263/*
4264 * Enable PCH resources required for PCH ports:
4265 * - PCH PLLs
4266 * - FDI training & RX/TX
4267 * - update transcoder timings
4268 * - DP transcoding bits
4269 * - transcoder
4270 */
4271static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004272{
4273 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004274 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4276 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004277 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004278
Daniel Vetterab9412b2013-05-03 11:49:46 +02004279 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004280
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004281 if (IS_IVYBRIDGE(dev))
4282 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4283
Daniel Vettercd986ab2012-10-26 10:58:12 +02004284 /* Write the TU size bits before fdi link training, so that error
4285 * detection works. */
4286 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4287 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4288
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004289 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004290 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004291
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004292 /* We need to program the right clock selection before writing the pixel
4293 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004294 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004295 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004296
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004297 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004298 temp |= TRANS_DPLL_ENABLE(pipe);
4299 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004300 if (intel_crtc->config->shared_dpll ==
4301 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004302 temp |= sel;
4303 else
4304 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004305 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004306 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004307
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004308 /* XXX: pch pll's can be enabled any time before we enable the PCH
4309 * transcoder, and we actually should do this to not upset any PCH
4310 * transcoder that already use the clock when we share it.
4311 *
4312 * Note that enable_shared_dpll tries to do the right thing, but
4313 * get_shared_dpll unconditionally resets the pll - we need that to have
4314 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004315 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004316
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004317 /* set transcoder timing, panel must allow it */
4318 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004319 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004320
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004321 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004322
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004323 /* For PCH DP, enable TRANS_DP_CTL */
Ville Syrjälä37a56502016-06-22 21:57:04 +03004324 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004325 const struct drm_display_mode *adjusted_mode =
4326 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004327 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004328 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004329 temp = I915_READ(reg);
4330 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004331 TRANS_DP_SYNC_MASK |
4332 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004333 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004334 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004335
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004336 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004337 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004338 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004339 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004340
4341 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004342 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004343 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004344 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004345 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004346 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004347 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004348 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004349 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004350 break;
4351 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004352 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004353 }
4354
Chris Wilson5eddb702010-09-11 13:48:45 +01004355 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004356 }
4357
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004358 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004359}
4360
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004361static void lpt_pch_enable(struct drm_crtc *crtc)
4362{
4363 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004364 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004366 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004367
Daniel Vetterab9412b2013-05-03 11:49:46 +02004368 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004369
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004370 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004371
Paulo Zanoni0540e482012-10-31 18:12:40 -02004372 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004373 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004374
Paulo Zanoni937bb612012-10-31 18:12:47 -02004375 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004376}
4377
Daniel Vettera1520312013-05-03 11:49:50 +02004378static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004379{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004380 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004381 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004382 u32 temp;
4383
4384 temp = I915_READ(dslreg);
4385 udelay(500);
4386 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004387 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004388 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004389 }
4390}
4391
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004392static int
4393skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4394 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4395 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004396{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004397 struct intel_crtc_scaler_state *scaler_state =
4398 &crtc_state->scaler_state;
4399 struct intel_crtc *intel_crtc =
4400 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004401 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004402
4403 need_scaling = intel_rotation_90_or_270(rotation) ?
4404 (src_h != dst_w || src_w != dst_h):
4405 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004406
4407 /*
4408 * if plane is being disabled or scaler is no more required or force detach
4409 * - free scaler binded to this plane/crtc
4410 * - in order to do this, update crtc->scaler_usage
4411 *
4412 * Here scaler state in crtc_state is set free so that
4413 * scaler can be assigned to other user. Actual register
4414 * update to free the scaler is done in plane/panel-fit programming.
4415 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4416 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004417 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004418 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004419 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004420 scaler_state->scalers[*scaler_id].in_use = 0;
4421
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004422 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4423 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4424 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004425 scaler_state->scaler_users);
4426 *scaler_id = -1;
4427 }
4428 return 0;
4429 }
4430
4431 /* range checks */
4432 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4433 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4434
4435 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4436 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004437 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004438 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004439 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004440 return -EINVAL;
4441 }
4442
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004443 /* mark this plane as a scaler user in crtc_state */
4444 scaler_state->scaler_users |= (1 << scaler_user);
4445 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4446 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4447 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4448 scaler_state->scaler_users);
4449
4450 return 0;
4451}
4452
4453/**
4454 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4455 *
4456 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004457 *
4458 * Return
4459 * 0 - scaler_usage updated successfully
4460 * error - requested scaling cannot be supported or other error condition
4461 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004462int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004463{
4464 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004465 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004466
Ville Syrjälä78108b72016-05-27 20:59:19 +03004467 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4468 intel_crtc->base.base.id, intel_crtc->base.name,
4469 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004470
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004471 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004472 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004473 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004474 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004475}
4476
4477/**
4478 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4479 *
4480 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004481 * @plane_state: atomic plane state to update
4482 *
4483 * Return
4484 * 0 - scaler_usage updated successfully
4485 * error - requested scaling cannot be supported or other error condition
4486 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004487static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4488 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004489{
4490
4491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004492 struct intel_plane *intel_plane =
4493 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004494 struct drm_framebuffer *fb = plane_state->base.fb;
4495 int ret;
4496
4497 bool force_detach = !fb || !plane_state->visible;
4498
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004499 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4500 intel_plane->base.base.id, intel_plane->base.name,
4501 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004502
4503 ret = skl_update_scaler(crtc_state, force_detach,
4504 drm_plane_index(&intel_plane->base),
4505 &plane_state->scaler_id,
4506 plane_state->base.rotation,
4507 drm_rect_width(&plane_state->src) >> 16,
4508 drm_rect_height(&plane_state->src) >> 16,
4509 drm_rect_width(&plane_state->dst),
4510 drm_rect_height(&plane_state->dst));
4511
4512 if (ret || plane_state->scaler_id < 0)
4513 return ret;
4514
Chandra Kondurua1b22782015-04-07 15:28:45 -07004515 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004516 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004517 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4518 intel_plane->base.base.id,
4519 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004520 return -EINVAL;
4521 }
4522
4523 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004524 switch (fb->pixel_format) {
4525 case DRM_FORMAT_RGB565:
4526 case DRM_FORMAT_XBGR8888:
4527 case DRM_FORMAT_XRGB8888:
4528 case DRM_FORMAT_ABGR8888:
4529 case DRM_FORMAT_ARGB8888:
4530 case DRM_FORMAT_XRGB2101010:
4531 case DRM_FORMAT_XBGR2101010:
4532 case DRM_FORMAT_YUYV:
4533 case DRM_FORMAT_YVYU:
4534 case DRM_FORMAT_UYVY:
4535 case DRM_FORMAT_VYUY:
4536 break;
4537 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004538 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4539 intel_plane->base.base.id, intel_plane->base.name,
4540 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004541 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004542 }
4543
Chandra Kondurua1b22782015-04-07 15:28:45 -07004544 return 0;
4545}
4546
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004547static void skylake_scaler_disable(struct intel_crtc *crtc)
4548{
4549 int i;
4550
4551 for (i = 0; i < crtc->num_scalers; i++)
4552 skl_detach_scaler(crtc, i);
4553}
4554
4555static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004556{
4557 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004558 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004559 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004560 struct intel_crtc_scaler_state *scaler_state =
4561 &crtc->config->scaler_state;
4562
4563 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4564
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004565 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004566 int id;
4567
4568 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4569 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4570 return;
4571 }
4572
4573 id = scaler_state->scaler_id;
4574 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4575 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4576 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4577 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4578
4579 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004580 }
4581}
4582
Jesse Barnesb074cec2013-04-25 12:55:02 -07004583static void ironlake_pfit_enable(struct intel_crtc *crtc)
4584{
4585 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004586 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004587 int pipe = crtc->pipe;
4588
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004589 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004590 /* Force use of hard-coded filter coefficients
4591 * as some pre-programmed values are broken,
4592 * e.g. x201.
4593 */
4594 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4595 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4596 PF_PIPE_SEL_IVB(pipe));
4597 else
4598 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004599 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4600 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004601 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004602}
4603
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004604void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004605{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004606 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004607 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004608
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004609 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004610 return;
4611
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004612 /*
4613 * We can only enable IPS after we enable a plane and wait for a vblank
4614 * This function is called from post_plane_update, which is run after
4615 * a vblank wait.
4616 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004617
Paulo Zanonid77e4532013-09-24 13:52:55 -03004618 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004619 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004620 mutex_lock(&dev_priv->rps.hw_lock);
4621 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4622 mutex_unlock(&dev_priv->rps.hw_lock);
4623 /* Quoting Art Runyan: "its not safe to expect any particular
4624 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004625 * mailbox." Moreover, the mailbox may return a bogus state,
4626 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004627 */
4628 } else {
4629 I915_WRITE(IPS_CTL, IPS_ENABLE);
4630 /* The bit only becomes 1 in the next vblank, so this wait here
4631 * is essentially intel_wait_for_vblank. If we don't have this
4632 * and don't wait for vblanks until the end of crtc_enable, then
4633 * the HW state readout code will complain that the expected
4634 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004635 if (intel_wait_for_register(dev_priv,
4636 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4637 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004638 DRM_ERROR("Timed out waiting for IPS enable\n");
4639 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004640}
4641
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004642void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004643{
4644 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004645 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004646
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004647 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004648 return;
4649
4650 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004651 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004652 mutex_lock(&dev_priv->rps.hw_lock);
4653 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4654 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004655 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004656 if (intel_wait_for_register(dev_priv,
4657 IPS_CTL, IPS_ENABLE, 0,
4658 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004659 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004660 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004661 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004662 POSTING_READ(IPS_CTL);
4663 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004664
4665 /* We need to wait for a vblank before we can disable the plane. */
4666 intel_wait_for_vblank(dev, crtc->pipe);
4667}
4668
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004669static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004670{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004671 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004672 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004673 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004674
4675 mutex_lock(&dev->struct_mutex);
4676 dev_priv->mm.interruptible = false;
4677 (void) intel_overlay_switch_off(intel_crtc->overlay);
4678 dev_priv->mm.interruptible = true;
4679 mutex_unlock(&dev->struct_mutex);
4680 }
4681
4682 /* Let userspace switch the overlay on again. In most cases userspace
4683 * has to recompute where to put it anyway.
4684 */
4685}
4686
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004687/**
4688 * intel_post_enable_primary - Perform operations after enabling primary plane
4689 * @crtc: the CRTC whose primary plane was just enabled
4690 *
4691 * Performs potentially sleeping operations that must be done after the primary
4692 * plane is enabled, such as updating FBC and IPS. Note that this may be
4693 * called due to an explicit primary plane update, or due to an implicit
4694 * re-enable that is caused when a sprite plane is updated to no longer
4695 * completely hide the primary plane.
4696 */
4697static void
4698intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004699{
4700 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004701 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4703 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004704
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004705 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004706 * FIXME IPS should be fine as long as one plane is
4707 * enabled, but in practice it seems to have problems
4708 * when going from primary only to sprite only and vice
4709 * versa.
4710 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004711 hsw_enable_ips(intel_crtc);
4712
Daniel Vetterf99d7062014-06-19 16:01:59 +02004713 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004714 * Gen2 reports pipe underruns whenever all planes are disabled.
4715 * So don't enable underrun reporting before at least some planes
4716 * are enabled.
4717 * FIXME: Need to fix the logic to work when we turn off all planes
4718 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004719 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004720 if (IS_GEN2(dev))
4721 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4722
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004723 /* Underruns don't always raise interrupts, so check manually. */
4724 intel_check_cpu_fifo_underruns(dev_priv);
4725 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004726}
4727
Ville Syrjälä2622a082016-03-09 19:07:26 +02004728/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004729static void
4730intel_pre_disable_primary(struct drm_crtc *crtc)
4731{
4732 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004733 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4735 int pipe = intel_crtc->pipe;
4736
4737 /*
4738 * Gen2 reports pipe underruns whenever all planes are disabled.
4739 * So diasble underrun reporting before all the planes get disabled.
4740 * FIXME: Need to fix the logic to work when we turn off all planes
4741 * but leave the pipe running.
4742 */
4743 if (IS_GEN2(dev))
4744 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4745
4746 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004747 * FIXME IPS should be fine as long as one plane is
4748 * enabled, but in practice it seems to have problems
4749 * when going from primary only to sprite only and vice
4750 * versa.
4751 */
4752 hsw_disable_ips(intel_crtc);
4753}
4754
4755/* FIXME get rid of this and use pre_plane_update */
4756static void
4757intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4758{
4759 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004760 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4762 int pipe = intel_crtc->pipe;
4763
4764 intel_pre_disable_primary(crtc);
4765
4766 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004767 * Vblank time updates from the shadow to live plane control register
4768 * are blocked if the memory self-refresh mode is active at that
4769 * moment. So to make sure the plane gets truly disabled, disable
4770 * first the self-refresh mode. The self-refresh enable bit in turn
4771 * will be checked/applied by the HW only at the next frame start
4772 * event which is after the vblank start event, so we need to have a
4773 * wait-for-vblank between disabling the plane and the pipe.
4774 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004775 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004776 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004777 dev_priv->wm.vlv.cxsr = false;
4778 intel_wait_for_vblank(dev, pipe);
4779 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004780}
4781
Daniel Vetter5a21b662016-05-24 17:13:53 +02004782static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4783{
4784 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4785 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4786 struct intel_crtc_state *pipe_config =
4787 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004788 struct drm_plane *primary = crtc->base.primary;
4789 struct drm_plane_state *old_pri_state =
4790 drm_atomic_get_existing_plane_state(old_state, primary);
4791
Chris Wilson5748b6a2016-08-04 16:32:38 +01004792 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004793
4794 crtc->wm.cxsr_allowed = true;
4795
4796 if (pipe_config->update_wm_post && pipe_config->base.active)
4797 intel_update_watermarks(&crtc->base);
4798
4799 if (old_pri_state) {
4800 struct intel_plane_state *primary_state =
4801 to_intel_plane_state(primary->state);
4802 struct intel_plane_state *old_primary_state =
4803 to_intel_plane_state(old_pri_state);
4804
4805 intel_fbc_post_update(crtc);
4806
4807 if (primary_state->visible &&
4808 (needs_modeset(&pipe_config->base) ||
4809 !old_primary_state->visible))
4810 intel_post_enable_primary(&crtc->base);
4811 }
4812}
4813
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004814static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004815{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004816 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004817 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004818 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004819 struct intel_crtc_state *pipe_config =
4820 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004821 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4822 struct drm_plane *primary = crtc->base.primary;
4823 struct drm_plane_state *old_pri_state =
4824 drm_atomic_get_existing_plane_state(old_state, primary);
4825 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004826
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004827 if (old_pri_state) {
4828 struct intel_plane_state *primary_state =
4829 to_intel_plane_state(primary->state);
4830 struct intel_plane_state *old_primary_state =
4831 to_intel_plane_state(old_pri_state);
4832
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004833 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004834
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004835 if (old_primary_state->visible &&
4836 (modeset || !primary_state->visible))
4837 intel_pre_disable_primary(&crtc->base);
4838 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004839
David Weinehalla4015f92016-05-19 15:50:36 +03004840 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004841 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004842
Ville Syrjälä2622a082016-03-09 19:07:26 +02004843 /*
4844 * Vblank time updates from the shadow to live plane control register
4845 * are blocked if the memory self-refresh mode is active at that
4846 * moment. So to make sure the plane gets truly disabled, disable
4847 * first the self-refresh mode. The self-refresh enable bit in turn
4848 * will be checked/applied by the HW only at the next frame start
4849 * event which is after the vblank start event, so we need to have a
4850 * wait-for-vblank between disabling the plane and the pipe.
4851 */
4852 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004853 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004854 dev_priv->wm.vlv.cxsr = false;
4855 intel_wait_for_vblank(dev, crtc->pipe);
4856 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004857 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004858
Matt Ropered4a6a72016-02-23 17:20:13 -08004859 /*
4860 * IVB workaround: must disable low power watermarks for at least
4861 * one frame before enabling scaling. LP watermarks can be re-enabled
4862 * when scaling is disabled.
4863 *
4864 * WaCxSRDisabledForSpriteScaling:ivb
4865 */
4866 if (pipe_config->disable_lp_wm) {
4867 ilk_disable_lp_wm(dev);
4868 intel_wait_for_vblank(dev, crtc->pipe);
4869 }
4870
4871 /*
4872 * If we're doing a modeset, we're done. No need to do any pre-vblank
4873 * watermark programming here.
4874 */
4875 if (needs_modeset(&pipe_config->base))
4876 return;
4877
4878 /*
4879 * For platforms that support atomic watermarks, program the
4880 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4881 * will be the intermediate values that are safe for both pre- and
4882 * post- vblank; when vblank happens, the 'active' values will be set
4883 * to the final 'target' values and we'll do this again to get the
4884 * optimal watermarks. For gen9+ platforms, the values we program here
4885 * will be the final target values which will get automatically latched
4886 * at vblank time; no further programming will be necessary.
4887 *
4888 * If a platform hasn't been transitioned to atomic watermarks yet,
4889 * we'll continue to update watermarks the old way, if flags tell
4890 * us to.
4891 */
4892 if (dev_priv->display.initial_watermarks != NULL)
4893 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004894 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004895 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004896}
4897
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004898static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004899{
4900 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004902 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004903 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004904
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004905 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004906
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004907 drm_for_each_plane_mask(p, dev, plane_mask)
4908 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004909
Daniel Vetterf99d7062014-06-19 16:01:59 +02004910 /*
4911 * FIXME: Once we grow proper nuclear flip support out of this we need
4912 * to compute the mask of flip planes precisely. For the time being
4913 * consider this a flip to a NULL plane.
4914 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01004915 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004916}
4917
Jesse Barnesf67a5592011-01-05 10:31:48 -08004918static void ironlake_crtc_enable(struct drm_crtc *crtc)
4919{
4920 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004921 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004923 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004924 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004925 struct intel_crtc_state *pipe_config =
4926 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004927
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004928 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004929 return;
4930
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004931 /*
4932 * Sometimes spurious CPU pipe underruns happen during FDI
4933 * training, at least with VGA+HDMI cloning. Suppress them.
4934 *
4935 * On ILK we get an occasional spurious CPU pipe underruns
4936 * between eDP port A enable and vdd enable. Also PCH port
4937 * enable seems to result in the occasional CPU pipe underrun.
4938 *
4939 * Spurious PCH underruns also occur during PCH enabling.
4940 */
4941 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4942 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004943 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004944 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4945
4946 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004947 intel_prepare_shared_dpll(intel_crtc);
4948
Ville Syrjälä37a56502016-06-22 21:57:04 +03004949 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304950 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004951
4952 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004953 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004954
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004955 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004956 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004957 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004958 }
4959
4960 ironlake_set_pipeconf(crtc);
4961
Jesse Barnesf67a5592011-01-05 10:31:48 -08004962 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004963
Daniel Vetterf6736a12013-06-05 13:34:30 +02004964 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004965 if (encoder->pre_enable)
4966 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004967
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004968 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004969 /* Note: FDI PLL enabling _must_ be done before we enable the
4970 * cpu pipes, hence this is separate from all the other fdi/pch
4971 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004972 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004973 } else {
4974 assert_fdi_tx_disabled(dev_priv, pipe);
4975 assert_fdi_rx_disabled(dev_priv, pipe);
4976 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004977
Jesse Barnesb074cec2013-04-25 12:55:02 -07004978 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004979
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004980 /*
4981 * On ILK+ LUT must be loaded before the pipe is running but with
4982 * clocks enabled
4983 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004984 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004985
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004986 if (dev_priv->display.initial_watermarks != NULL)
4987 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004988 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004989
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004990 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004991 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004992
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004993 assert_vblank_disabled(crtc);
4994 drm_crtc_vblank_on(crtc);
4995
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004996 for_each_encoder_on_crtc(dev, crtc, encoder)
4997 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004998
4999 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02005000 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005001
5002 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5003 if (intel_crtc->config->has_pch_encoder)
5004 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005005 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005006 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007}
5008
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005009/* IPS only exists on ULT machines and is tied to pipe A. */
5010static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5011{
Damien Lespiauf5adf942013-06-24 18:29:34 +01005012 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005013}
5014
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005015static void haswell_crtc_enable(struct drm_crtc *crtc)
5016{
5017 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005018 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5020 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005021 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005022 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005023 struct intel_crtc_state *pipe_config =
5024 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005026 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005027 return;
5028
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005029 if (intel_crtc->config->has_pch_encoder)
5030 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5031 false);
5032
Imre Deak95a7a2a2016-06-13 16:44:35 +03005033 for_each_encoder_on_crtc(dev, crtc, encoder)
5034 if (encoder->pre_pll_enable)
5035 encoder->pre_pll_enable(encoder);
5036
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005037 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005038 intel_enable_shared_dpll(intel_crtc);
5039
Ville Syrjälä37a56502016-06-22 21:57:04 +03005040 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305041 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005042
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005043 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005044 intel_set_pipe_timings(intel_crtc);
5045
Jani Nikulabc58be62016-03-18 17:05:39 +02005046 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005047
Jani Nikula4d1de972016-03-18 17:05:42 +02005048 if (cpu_transcoder != TRANSCODER_EDP &&
5049 !transcoder_is_dsi(cpu_transcoder)) {
5050 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005051 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005052 }
5053
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005054 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005055 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005056 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005057 }
5058
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005059 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005060 haswell_set_pipeconf(crtc);
5061
Jani Nikula391bf042016-03-18 17:05:40 +02005062 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005063
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005064 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005065
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005066 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005067
Daniel Vetter6b698512015-11-28 11:05:39 +01005068 if (intel_crtc->config->has_pch_encoder)
5069 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5070 else
5071 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5072
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305073 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005074 if (encoder->pre_enable)
5075 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305076 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005077
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005078 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005079 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005080
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005081 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305082 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005083
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005084 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005085 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005086 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005087 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005088
5089 /*
5090 * On ILK+ LUT must be loaded before the pipe is running but with
5091 * clocks enabled
5092 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005093 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005094
Paulo Zanoni1f544382012-10-24 11:32:00 -02005095 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005096 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305097 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005098
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005099 if (dev_priv->display.initial_watermarks != NULL)
5100 dev_priv->display.initial_watermarks(pipe_config);
5101 else
5102 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02005103
5104 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005105 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005106 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005107
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005108 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005109 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005110
Jani Nikulaa65347b2015-11-27 12:21:46 +02005111 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005112 intel_ddi_set_vc_payload_alloc(crtc, true);
5113
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005114 assert_vblank_disabled(crtc);
5115 drm_crtc_vblank_on(crtc);
5116
Jani Nikula8807e552013-08-30 19:40:32 +03005117 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005118 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005119 intel_opregion_notify_encoder(encoder, true);
5120 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005121
Daniel Vetter6b698512015-11-28 11:05:39 +01005122 if (intel_crtc->config->has_pch_encoder) {
5123 intel_wait_for_vblank(dev, pipe);
5124 intel_wait_for_vblank(dev, pipe);
5125 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005126 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5127 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005128 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005129
Paulo Zanonie4916942013-09-20 16:21:19 -03005130 /* If we change the relative order between pipe/planes enabling, we need
5131 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005132 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5133 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5134 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5135 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5136 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005137}
5138
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005139static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005140{
5141 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005142 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005143 int pipe = crtc->pipe;
5144
5145 /* To avoid upsetting the power well on haswell only disable the pfit if
5146 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005147 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005148 I915_WRITE(PF_CTL(pipe), 0);
5149 I915_WRITE(PF_WIN_POS(pipe), 0);
5150 I915_WRITE(PF_WIN_SZ(pipe), 0);
5151 }
5152}
5153
Jesse Barnes6be4a602010-09-10 10:26:01 -07005154static void ironlake_crtc_disable(struct drm_crtc *crtc)
5155{
5156 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005157 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005159 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005160 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005161
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005162 /*
5163 * Sometimes spurious CPU pipe underruns happen when the
5164 * pipe is already disabled, but FDI RX/TX is still enabled.
5165 * Happens at least with VGA+HDMI cloning. Suppress them.
5166 */
5167 if (intel_crtc->config->has_pch_encoder) {
5168 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005169 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005170 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005171
Daniel Vetterea9d7582012-07-10 10:42:52 +02005172 for_each_encoder_on_crtc(dev, crtc, encoder)
5173 encoder->disable(encoder);
5174
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005175 drm_crtc_vblank_off(crtc);
5176 assert_vblank_disabled(crtc);
5177
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005178 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005179
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005180 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005181
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005182 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005183 ironlake_fdi_disable(crtc);
5184
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005185 for_each_encoder_on_crtc(dev, crtc, encoder)
5186 if (encoder->post_disable)
5187 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005188
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005189 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005190 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005191
Daniel Vetterd925c592013-06-05 13:34:04 +02005192 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005193 i915_reg_t reg;
5194 u32 temp;
5195
Daniel Vetterd925c592013-06-05 13:34:04 +02005196 /* disable TRANS_DP_CTL */
5197 reg = TRANS_DP_CTL(pipe);
5198 temp = I915_READ(reg);
5199 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5200 TRANS_DP_PORT_SEL_MASK);
5201 temp |= TRANS_DP_PORT_SEL_NONE;
5202 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005203
Daniel Vetterd925c592013-06-05 13:34:04 +02005204 /* disable DPLL_SEL */
5205 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005206 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005207 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005208 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005209
Daniel Vetterd925c592013-06-05 13:34:04 +02005210 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005211 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005212
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005213 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005214 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005215}
5216
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005217static void haswell_crtc_disable(struct drm_crtc *crtc)
5218{
5219 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005220 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5222 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005223 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005224
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005225 if (intel_crtc->config->has_pch_encoder)
5226 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5227 false);
5228
Jani Nikula8807e552013-08-30 19:40:32 +03005229 for_each_encoder_on_crtc(dev, crtc, encoder) {
5230 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005231 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005232 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005233
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005234 drm_crtc_vblank_off(crtc);
5235 assert_vblank_disabled(crtc);
5236
Jani Nikula4d1de972016-03-18 17:05:42 +02005237 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005238 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005239 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005240
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005241 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005242 intel_ddi_set_vc_payload_alloc(crtc, false);
5243
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005244 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305245 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005246
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005247 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005248 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005249 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005250 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005251
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005252 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305253 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005254
Imre Deak97b040a2014-06-25 22:01:50 +03005255 for_each_encoder_on_crtc(dev, crtc, encoder)
5256 if (encoder->post_disable)
5257 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005258
Ville Syrjälä92966a32015-12-08 16:05:48 +02005259 if (intel_crtc->config->has_pch_encoder) {
5260 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005261 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005262 intel_ddi_fdi_disable(crtc);
5263
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005264 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5265 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005266 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005267}
5268
Jesse Barnes2dd24552013-04-25 12:55:01 -07005269static void i9xx_pfit_enable(struct intel_crtc *crtc)
5270{
5271 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005272 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005273 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005274
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005275 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005276 return;
5277
Daniel Vetterc0b03412013-05-28 12:05:54 +02005278 /*
5279 * The panel fitter should only be adjusted whilst the pipe is disabled,
5280 * according to register description and PRM.
5281 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005282 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5283 assert_pipe_disabled(dev_priv, crtc->pipe);
5284
Jesse Barnesb074cec2013-04-25 12:55:02 -07005285 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5286 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005287
5288 /* Border color in case we don't scale up to the full screen. Black by
5289 * default, change to something else for debugging. */
5290 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005291}
5292
Dave Airlied05410f2014-06-05 13:22:59 +10005293static enum intel_display_power_domain port_to_power_domain(enum port port)
5294{
5295 switch (port) {
5296 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005297 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005298 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005299 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005300 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005301 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005302 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005303 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005304 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005305 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005306 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005307 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005308 return POWER_DOMAIN_PORT_OTHER;
5309 }
5310}
5311
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005312static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5313{
5314 switch (port) {
5315 case PORT_A:
5316 return POWER_DOMAIN_AUX_A;
5317 case PORT_B:
5318 return POWER_DOMAIN_AUX_B;
5319 case PORT_C:
5320 return POWER_DOMAIN_AUX_C;
5321 case PORT_D:
5322 return POWER_DOMAIN_AUX_D;
5323 case PORT_E:
5324 /* FIXME: Check VBT for actual wiring of PORT E */
5325 return POWER_DOMAIN_AUX_D;
5326 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005327 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005328 return POWER_DOMAIN_AUX_A;
5329 }
5330}
5331
Imre Deak319be8a2014-03-04 19:22:57 +02005332enum intel_display_power_domain
5333intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005334{
Imre Deak319be8a2014-03-04 19:22:57 +02005335 struct drm_device *dev = intel_encoder->base.dev;
5336 struct intel_digital_port *intel_dig_port;
5337
5338 switch (intel_encoder->type) {
5339 case INTEL_OUTPUT_UNKNOWN:
5340 /* Only DDI platforms should ever use this output type */
5341 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005342 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005343 case INTEL_OUTPUT_HDMI:
5344 case INTEL_OUTPUT_EDP:
5345 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005346 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005347 case INTEL_OUTPUT_DP_MST:
5348 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5349 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005350 case INTEL_OUTPUT_ANALOG:
5351 return POWER_DOMAIN_PORT_CRT;
5352 case INTEL_OUTPUT_DSI:
5353 return POWER_DOMAIN_PORT_DSI;
5354 default:
5355 return POWER_DOMAIN_PORT_OTHER;
5356 }
5357}
5358
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005359enum intel_display_power_domain
5360intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5361{
5362 struct drm_device *dev = intel_encoder->base.dev;
5363 struct intel_digital_port *intel_dig_port;
5364
5365 switch (intel_encoder->type) {
5366 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005367 case INTEL_OUTPUT_HDMI:
5368 /*
5369 * Only DDI platforms should ever use these output types.
5370 * We can get here after the HDMI detect code has already set
5371 * the type of the shared encoder. Since we can't be sure
5372 * what's the status of the given connectors, play safe and
5373 * run the DP detection too.
5374 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005375 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005376 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005377 case INTEL_OUTPUT_EDP:
5378 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5379 return port_to_aux_power_domain(intel_dig_port->port);
5380 case INTEL_OUTPUT_DP_MST:
5381 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5382 return port_to_aux_power_domain(intel_dig_port->port);
5383 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005384 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005385 return POWER_DOMAIN_AUX_A;
5386 }
5387}
5388
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005389static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5390 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005391{
5392 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005393 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5395 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005396 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005397 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005398
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005399 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005400 return 0;
5401
Imre Deak77d22dc2014-03-05 16:20:52 +02005402 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5403 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005404 if (crtc_state->pch_pfit.enabled ||
5405 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005406 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5407
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005408 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5409 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5410
Imre Deak319be8a2014-03-04 19:22:57 +02005411 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005412 }
Imre Deak319be8a2014-03-04 19:22:57 +02005413
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005414 if (crtc_state->shared_dpll)
5415 mask |= BIT(POWER_DOMAIN_PLLS);
5416
Imre Deak77d22dc2014-03-05 16:20:52 +02005417 return mask;
5418}
5419
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005420static unsigned long
5421modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5422 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005423{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005424 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5426 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005427 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005428
5429 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005430 intel_crtc->enabled_power_domains = new_domains =
5431 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005432
Daniel Vetter5a21b662016-05-24 17:13:53 +02005433 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005434
5435 for_each_power_domain(domain, domains)
5436 intel_display_power_get(dev_priv, domain);
5437
Daniel Vetter5a21b662016-05-24 17:13:53 +02005438 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005439}
5440
5441static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5442 unsigned long domains)
5443{
5444 enum intel_display_power_domain domain;
5445
5446 for_each_power_domain(domain, domains)
5447 intel_display_power_put(dev_priv, domain);
5448}
5449
Mika Kaholaadafdc62015-08-18 14:36:59 +03005450static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5451{
5452 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5453
5454 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5455 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5456 return max_cdclk_freq;
5457 else if (IS_CHERRYVIEW(dev_priv))
5458 return max_cdclk_freq*95/100;
5459 else if (INTEL_INFO(dev_priv)->gen < 4)
5460 return 2*max_cdclk_freq*90/100;
5461 else
5462 return max_cdclk_freq*90/100;
5463}
5464
Ville Syrjäläb2045352016-05-13 23:41:27 +03005465static int skl_calc_cdclk(int max_pixclk, int vco);
5466
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005467static void intel_update_max_cdclk(struct drm_device *dev)
5468{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005469 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005470
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005471 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005472 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005473 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005474
Ville Syrjäläb2045352016-05-13 23:41:27 +03005475 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005476 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005477
5478 /*
5479 * Use the lower (vco 8640) cdclk values as a
5480 * first guess. skl_calc_cdclk() will correct it
5481 * if the preferred vco is 8100 instead.
5482 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005483 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005484 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005485 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005486 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005487 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005488 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005489 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005490 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005491
5492 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005493 } else if (IS_BROXTON(dev)) {
5494 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005495 } else if (IS_BROADWELL(dev)) {
5496 /*
5497 * FIXME with extra cooling we can allow
5498 * 540 MHz for ULX and 675 Mhz for ULT.
5499 * How can we know if extra cooling is
5500 * available? PCI ID, VTB, something else?
5501 */
5502 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5503 dev_priv->max_cdclk_freq = 450000;
5504 else if (IS_BDW_ULX(dev))
5505 dev_priv->max_cdclk_freq = 450000;
5506 else if (IS_BDW_ULT(dev))
5507 dev_priv->max_cdclk_freq = 540000;
5508 else
5509 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005510 } else if (IS_CHERRYVIEW(dev)) {
5511 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005512 } else if (IS_VALLEYVIEW(dev)) {
5513 dev_priv->max_cdclk_freq = 400000;
5514 } else {
5515 /* otherwise assume cdclk is fixed */
5516 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5517 }
5518
Mika Kaholaadafdc62015-08-18 14:36:59 +03005519 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5520
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005521 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5522 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005523
5524 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5525 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005526}
5527
5528static void intel_update_cdclk(struct drm_device *dev)
5529{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005530 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005531
5532 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005533
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005534 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005535 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5536 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5537 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005538 else
5539 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5540 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005541
5542 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005543 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5544 * Programmng [sic] note: bit[9:2] should be programmed to the number
5545 * of cdclk that generates 4MHz reference clock freq which is used to
5546 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005547 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005548 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005549 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005550}
5551
Ville Syrjälä92891e42016-05-11 22:44:45 +03005552/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5553static int skl_cdclk_decimal(int cdclk)
5554{
5555 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5556}
5557
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005558static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5559{
5560 int ratio;
5561
5562 if (cdclk == dev_priv->cdclk_pll.ref)
5563 return 0;
5564
5565 switch (cdclk) {
5566 default:
5567 MISSING_CASE(cdclk);
5568 case 144000:
5569 case 288000:
5570 case 384000:
5571 case 576000:
5572 ratio = 60;
5573 break;
5574 case 624000:
5575 ratio = 65;
5576 break;
5577 }
5578
5579 return dev_priv->cdclk_pll.ref * ratio;
5580}
5581
Ville Syrjälä2b730012016-05-13 23:41:34 +03005582static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5583{
5584 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5585
5586 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005587 if (intel_wait_for_register(dev_priv,
5588 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5589 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005590 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005591
5592 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005593}
5594
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005595static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005596{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005597 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005598 u32 val;
5599
5600 val = I915_READ(BXT_DE_PLL_CTL);
5601 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005602 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005603 I915_WRITE(BXT_DE_PLL_CTL, val);
5604
5605 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5606
5607 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005608 if (intel_wait_for_register(dev_priv,
5609 BXT_DE_PLL_ENABLE,
5610 BXT_DE_PLL_LOCK,
5611 BXT_DE_PLL_LOCK,
5612 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005613 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005614
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005615 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005616}
5617
Imre Deak324513c2016-06-13 16:44:36 +03005618static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305619{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005620 u32 val, divider;
5621 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305622
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005623 vco = bxt_de_pll_vco(dev_priv, cdclk);
5624
5625 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5626
5627 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5628 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5629 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305630 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305631 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005632 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305633 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305634 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005635 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305636 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305637 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005638 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305639 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305640 break;
5641 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005642 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5643 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305644
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005645 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5646 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305647 }
5648
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305649 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005650 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305651 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5652 0x80000000);
5653 mutex_unlock(&dev_priv->rps.hw_lock);
5654
5655 if (ret) {
5656 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005657 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305658 return;
5659 }
5660
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005661 if (dev_priv->cdclk_pll.vco != 0 &&
5662 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005663 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305664
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005665 if (dev_priv->cdclk_pll.vco != vco)
5666 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305667
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005668 val = divider | skl_cdclk_decimal(cdclk);
5669 /*
5670 * FIXME if only the cd2x divider needs changing, it could be done
5671 * without shutting off the pipe (if only one pipe is active).
5672 */
5673 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5674 /*
5675 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5676 * enable otherwise.
5677 */
5678 if (cdclk >= 500000)
5679 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5680 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305681
5682 mutex_lock(&dev_priv->rps.hw_lock);
5683 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005684 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305685 mutex_unlock(&dev_priv->rps.hw_lock);
5686
5687 if (ret) {
5688 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005689 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305690 return;
5691 }
5692
Chris Wilson91c8a322016-07-05 10:40:23 +01005693 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305694}
5695
Imre Deakd66a2192016-05-24 15:38:33 +03005696static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305697{
Imre Deakd66a2192016-05-24 15:38:33 +03005698 u32 cdctl, expected;
5699
Chris Wilson91c8a322016-07-05 10:40:23 +01005700 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305701
Imre Deakd66a2192016-05-24 15:38:33 +03005702 if (dev_priv->cdclk_pll.vco == 0 ||
5703 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5704 goto sanitize;
5705
5706 /* DPLL okay; verify the cdclock
5707 *
5708 * Some BIOS versions leave an incorrect decimal frequency value and
5709 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5710 * so sanitize this register.
5711 */
5712 cdctl = I915_READ(CDCLK_CTL);
5713 /*
5714 * Let's ignore the pipe field, since BIOS could have configured the
5715 * dividers both synching to an active pipe, or asynchronously
5716 * (PIPE_NONE).
5717 */
5718 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5719
5720 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5721 skl_cdclk_decimal(dev_priv->cdclk_freq);
5722 /*
5723 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5724 * enable otherwise.
5725 */
5726 if (dev_priv->cdclk_freq >= 500000)
5727 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5728
5729 if (cdctl == expected)
5730 /* All well; nothing to sanitize */
5731 return;
5732
5733sanitize:
5734 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5735
5736 /* force cdclk programming */
5737 dev_priv->cdclk_freq = 0;
5738
5739 /* force full PLL disable + enable */
5740 dev_priv->cdclk_pll.vco = -1;
5741}
5742
Imre Deak324513c2016-06-13 16:44:36 +03005743void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005744{
5745 bxt_sanitize_cdclk(dev_priv);
5746
5747 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005748 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005749
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305750 /*
5751 * FIXME:
5752 * - The initial CDCLK needs to be read from VBT.
5753 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305754 */
Imre Deak324513c2016-06-13 16:44:36 +03005755 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305756}
5757
Imre Deak324513c2016-06-13 16:44:36 +03005758void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305759{
Imre Deak324513c2016-06-13 16:44:36 +03005760 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305761}
5762
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005763static int skl_calc_cdclk(int max_pixclk, int vco)
5764{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005765 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005766 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005767 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005768 else if (max_pixclk > 432000)
5769 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005770 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005771 return 432000;
5772 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005773 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005774 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005775 if (max_pixclk > 540000)
5776 return 675000;
5777 else if (max_pixclk > 450000)
5778 return 540000;
5779 else if (max_pixclk > 337500)
5780 return 450000;
5781 else
5782 return 337500;
5783 }
5784}
5785
Ville Syrjäläea617912016-05-13 23:41:24 +03005786static void
5787skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005788{
Ville Syrjäläea617912016-05-13 23:41:24 +03005789 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005790
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005791 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005792 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005793
Ville Syrjäläea617912016-05-13 23:41:24 +03005794 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005795 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005796 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005797
Imre Deak1c3f7702016-05-24 15:38:32 +03005798 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5799 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005800
Ville Syrjäläea617912016-05-13 23:41:24 +03005801 val = I915_READ(DPLL_CTRL1);
5802
Imre Deak1c3f7702016-05-24 15:38:32 +03005803 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5804 DPLL_CTRL1_SSC(SKL_DPLL0) |
5805 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5806 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5807 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005808
Ville Syrjäläea617912016-05-13 23:41:24 +03005809 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5810 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5811 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5812 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5813 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005814 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005815 break;
5816 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5817 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005818 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005819 break;
5820 default:
5821 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005822 break;
5823 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005824}
5825
Ville Syrjäläb2045352016-05-13 23:41:27 +03005826void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5827{
5828 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5829
5830 dev_priv->skl_preferred_vco_freq = vco;
5831
5832 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01005833 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005834}
5835
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005836static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005837skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005838{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005839 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005840 u32 val;
5841
Ville Syrjälä63911d72016-05-13 23:41:32 +03005842 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005843
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005844 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005845 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005846 I915_WRITE(CDCLK_CTL, val);
5847 POSTING_READ(CDCLK_CTL);
5848
5849 /*
5850 * We always enable DPLL0 with the lowest link rate possible, but still
5851 * taking into account the VCO required to operate the eDP panel at the
5852 * desired frequency. The usual DP link rates operate with a VCO of
5853 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5854 * The modeset code is responsible for the selection of the exact link
5855 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005856 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005857 */
5858 val = I915_READ(DPLL_CTRL1);
5859
5860 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5861 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5862 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005863 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005864 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5865 SKL_DPLL0);
5866 else
5867 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5868 SKL_DPLL0);
5869
5870 I915_WRITE(DPLL_CTRL1, val);
5871 POSTING_READ(DPLL_CTRL1);
5872
5873 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5874
Chris Wilsone24ca052016-06-30 15:33:05 +01005875 if (intel_wait_for_register(dev_priv,
5876 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5877 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005878 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005879
Ville Syrjälä63911d72016-05-13 23:41:32 +03005880 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005881
5882 /* We'll want to keep using the current vco from now on. */
5883 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005884}
5885
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005886static void
5887skl_dpll0_disable(struct drm_i915_private *dev_priv)
5888{
5889 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01005890 if (intel_wait_for_register(dev_priv,
5891 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5892 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005893 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005894
Ville Syrjälä63911d72016-05-13 23:41:32 +03005895 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005896}
5897
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005898static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5899{
5900 int ret;
5901 u32 val;
5902
5903 /* inform PCU we want to change CDCLK */
5904 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5905 mutex_lock(&dev_priv->rps.hw_lock);
5906 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5907 mutex_unlock(&dev_priv->rps.hw_lock);
5908
5909 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5910}
5911
5912static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5913{
Ville Syrjälä848496e2016-07-13 16:32:03 +03005914 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005915}
5916
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005917static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005918{
Chris Wilson91c8a322016-07-05 10:40:23 +01005919 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005920 u32 freq_select, pcu_ack;
5921
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005922 WARN_ON((cdclk == 24000) != (vco == 0));
5923
Ville Syrjälä63911d72016-05-13 23:41:32 +03005924 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005925
5926 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5927 DRM_ERROR("failed to inform PCU about cdclk change\n");
5928 return;
5929 }
5930
5931 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005932 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005933 case 450000:
5934 case 432000:
5935 freq_select = CDCLK_FREQ_450_432;
5936 pcu_ack = 1;
5937 break;
5938 case 540000:
5939 freq_select = CDCLK_FREQ_540;
5940 pcu_ack = 2;
5941 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005942 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005943 case 337500:
5944 default:
5945 freq_select = CDCLK_FREQ_337_308;
5946 pcu_ack = 0;
5947 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005948 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005949 case 675000:
5950 freq_select = CDCLK_FREQ_675_617;
5951 pcu_ack = 3;
5952 break;
5953 }
5954
Ville Syrjälä63911d72016-05-13 23:41:32 +03005955 if (dev_priv->cdclk_pll.vco != 0 &&
5956 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005957 skl_dpll0_disable(dev_priv);
5958
Ville Syrjälä63911d72016-05-13 23:41:32 +03005959 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005960 skl_dpll0_enable(dev_priv, vco);
5961
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005962 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005963 POSTING_READ(CDCLK_CTL);
5964
5965 /* inform PCU of the change */
5966 mutex_lock(&dev_priv->rps.hw_lock);
5967 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5968 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005969
5970 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005971}
5972
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005973static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5974
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005975void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5976{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005977 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005978}
5979
5980void skl_init_cdclk(struct drm_i915_private *dev_priv)
5981{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005982 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005983
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005984 skl_sanitize_cdclk(dev_priv);
5985
Ville Syrjälä63911d72016-05-13 23:41:32 +03005986 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005987 /*
5988 * Use the current vco as our initial
5989 * guess as to what the preferred vco is.
5990 */
5991 if (dev_priv->skl_preferred_vco_freq == 0)
5992 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005993 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005994 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005995 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005996
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005997 vco = dev_priv->skl_preferred_vco_freq;
5998 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005999 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006000 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006001
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006002 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006003}
6004
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006005static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306006{
Ville Syrjälä09492492016-05-13 23:41:28 +03006007 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306008
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05306009 /*
6010 * check if the pre-os intialized the display
6011 * There is SWF18 scratchpad register defined which is set by the
6012 * pre-os which can be used by the OS drivers to check the status
6013 */
6014 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6015 goto sanitize;
6016
Chris Wilson91c8a322016-07-05 10:40:23 +01006017 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03006018 /* Is PLL enabled and locked ? */
6019 if (dev_priv->cdclk_pll.vco == 0 ||
6020 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6021 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006022
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306023 /* DPLL okay; verify the cdclock
6024 *
6025 * Noticed in some instances that the freq selection is correct but
6026 * decimal part is programmed wrong from BIOS where pre-os does not
6027 * enable display. Verify the same as well.
6028 */
Ville Syrjälä09492492016-05-13 23:41:28 +03006029 cdctl = I915_READ(CDCLK_CTL);
6030 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6031 skl_cdclk_decimal(dev_priv->cdclk_freq);
6032 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306033 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006034 return;
6035
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306036sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006037 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03006038
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006039 /* force cdclk programming */
6040 dev_priv->cdclk_freq = 0;
6041 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03006042 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306043}
6044
Jesse Barnes30a970c2013-11-04 13:48:12 -08006045/* Adjust CDclk dividers to allow high res or save power if possible */
6046static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6047{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006048 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006049 u32 val, cmd;
6050
Vandana Kannan164dfd22014-11-24 13:37:41 +05306051 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6052 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02006053
Ville Syrjälädfcab172014-06-13 13:37:47 +03006054 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08006055 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03006056 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006057 cmd = 1;
6058 else
6059 cmd = 0;
6060
6061 mutex_lock(&dev_priv->rps.hw_lock);
6062 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6063 val &= ~DSPFREQGUAR_MASK;
6064 val |= (cmd << DSPFREQGUAR_SHIFT);
6065 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6066 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6067 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6068 50)) {
6069 DRM_ERROR("timed out waiting for CDclk change\n");
6070 }
6071 mutex_unlock(&dev_priv->rps.hw_lock);
6072
Ville Syrjälä54433e92015-05-26 20:42:31 +03006073 mutex_lock(&dev_priv->sb_lock);
6074
Ville Syrjälädfcab172014-06-13 13:37:47 +03006075 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006076 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006077
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006078 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006079
Jesse Barnes30a970c2013-11-04 13:48:12 -08006080 /* adjust cdclk divider */
6081 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03006082 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006083 val |= divider;
6084 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03006085
6086 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03006087 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03006088 50))
6089 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08006090 }
6091
Jesse Barnes30a970c2013-11-04 13:48:12 -08006092 /* adjust self-refresh exit latency value */
6093 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6094 val &= ~0x7f;
6095
6096 /*
6097 * For high bandwidth configs, we set a higher latency in the bunit
6098 * so that the core display fetch happens in time to avoid underruns.
6099 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03006100 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006101 val |= 4500 / 250; /* 4.5 usec */
6102 else
6103 val |= 3000 / 250; /* 3.0 usec */
6104 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03006105
Ville Syrjäläa5805162015-05-26 20:42:30 +03006106 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006107
Ville Syrjäläb6283052015-06-03 15:45:07 +03006108 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006109}
6110
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006111static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6112{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006113 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006114 u32 val, cmd;
6115
Vandana Kannan164dfd22014-11-24 13:37:41 +05306116 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6117 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006118
6119 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006120 case 333333:
6121 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006122 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006123 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006124 break;
6125 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006126 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006127 return;
6128 }
6129
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006130 /*
6131 * Specs are full of misinformation, but testing on actual
6132 * hardware has shown that we just need to write the desired
6133 * CCK divider into the Punit register.
6134 */
6135 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6136
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006137 mutex_lock(&dev_priv->rps.hw_lock);
6138 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6139 val &= ~DSPFREQGUAR_MASK_CHV;
6140 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6141 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6142 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6143 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6144 50)) {
6145 DRM_ERROR("timed out waiting for CDclk change\n");
6146 }
6147 mutex_unlock(&dev_priv->rps.hw_lock);
6148
Ville Syrjäläb6283052015-06-03 15:45:07 +03006149 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006150}
6151
Jesse Barnes30a970c2013-11-04 13:48:12 -08006152static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6153 int max_pixclk)
6154{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006155 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006156 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006157
Jesse Barnes30a970c2013-11-04 13:48:12 -08006158 /*
6159 * Really only a few cases to deal with, as only 4 CDclks are supported:
6160 * 200MHz
6161 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006162 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006163 * 400MHz (VLV only)
6164 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6165 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006166 *
6167 * We seem to get an unstable or solid color picture at 200MHz.
6168 * Not sure what's wrong. For now use 200MHz only when all pipes
6169 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006170 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006171 if (!IS_CHERRYVIEW(dev_priv) &&
6172 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006173 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006174 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006175 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006176 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006177 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006178 else
6179 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006180}
6181
Imre Deak324513c2016-06-13 16:44:36 +03006182static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006183{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006184 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306185 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006186 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306187 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006188 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306189 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006190 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306191 return 288000;
6192 else
6193 return 144000;
6194}
6195
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006196/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006197static int intel_mode_max_pixclk(struct drm_device *dev,
6198 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006199{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006200 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01006201 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006202 struct drm_crtc *crtc;
6203 struct drm_crtc_state *crtc_state;
6204 unsigned max_pixclk = 0, i;
6205 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006206
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006207 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6208 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006209
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006210 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6211 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006212
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006213 if (crtc_state->enable)
6214 pixclk = crtc_state->adjusted_mode.crtc_clock;
6215
6216 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006217 }
6218
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006219 for_each_pipe(dev_priv, pipe)
6220 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6221
Jesse Barnes30a970c2013-11-04 13:48:12 -08006222 return max_pixclk;
6223}
6224
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006225static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006226{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006227 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006228 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006229 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006230 struct intel_atomic_state *intel_state =
6231 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006232
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006233 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006234 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306235
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006236 if (!intel_state->active_crtcs)
6237 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6238
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006239 return 0;
6240}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006241
Imre Deak324513c2016-06-13 16:44:36 +03006242static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006243{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006244 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006245 struct intel_atomic_state *intel_state =
6246 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006247
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006248 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006249 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006250
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006251 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006252 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006253
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006254 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006255}
6256
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006257static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6258{
6259 unsigned int credits, default_credits;
6260
6261 if (IS_CHERRYVIEW(dev_priv))
6262 default_credits = PFI_CREDIT(12);
6263 else
6264 default_credits = PFI_CREDIT(8);
6265
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006266 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006267 /* CHV suggested value is 31 or 63 */
6268 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006269 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006270 else
6271 credits = PFI_CREDIT(15);
6272 } else {
6273 credits = default_credits;
6274 }
6275
6276 /*
6277 * WA - write default credits before re-programming
6278 * FIXME: should we also set the resend bit here?
6279 */
6280 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6281 default_credits);
6282
6283 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6284 credits | PFI_CREDIT_RESEND);
6285
6286 /*
6287 * FIXME is this guaranteed to clear
6288 * immediately or should we poll for it?
6289 */
6290 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6291}
6292
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006293static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006294{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006295 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006296 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006297 struct intel_atomic_state *old_intel_state =
6298 to_intel_atomic_state(old_state);
6299 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006300
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006301 /*
6302 * FIXME: We can end up here with all power domains off, yet
6303 * with a CDCLK frequency other than the minimum. To account
6304 * for this take the PIPE-A power domain, which covers the HW
6305 * blocks needed for the following programming. This can be
6306 * removed once it's guaranteed that we get here either with
6307 * the minimum CDCLK set, or the required power domains
6308 * enabled.
6309 */
6310 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006311
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006312 if (IS_CHERRYVIEW(dev))
6313 cherryview_set_cdclk(dev, req_cdclk);
6314 else
6315 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006316
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006317 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006318
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006319 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006320}
6321
Jesse Barnes89b667f2013-04-18 14:51:36 -07006322static void valleyview_crtc_enable(struct drm_crtc *crtc)
6323{
6324 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006325 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6327 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006328 struct intel_crtc_state *pipe_config =
6329 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006330 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006331
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006332 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006333 return;
6334
Ville Syrjälä37a56502016-06-22 21:57:04 +03006335 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306336 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006337
6338 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006339 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006340
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006341 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006342 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006343
6344 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6345 I915_WRITE(CHV_CANVAS(pipe), 0);
6346 }
6347
Daniel Vetter5b18e572014-04-24 23:55:06 +02006348 i9xx_set_pipeconf(intel_crtc);
6349
Jesse Barnes89b667f2013-04-18 14:51:36 -07006350 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006351
Daniel Vettera72e4c92014-09-30 10:56:47 +02006352 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006353
Jesse Barnes89b667f2013-04-18 14:51:36 -07006354 for_each_encoder_on_crtc(dev, crtc, encoder)
6355 if (encoder->pre_pll_enable)
6356 encoder->pre_pll_enable(encoder);
6357
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006358 if (IS_CHERRYVIEW(dev)) {
6359 chv_prepare_pll(intel_crtc, intel_crtc->config);
6360 chv_enable_pll(intel_crtc, intel_crtc->config);
6361 } else {
6362 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6363 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006364 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006365
6366 for_each_encoder_on_crtc(dev, crtc, encoder)
6367 if (encoder->pre_enable)
6368 encoder->pre_enable(encoder);
6369
Jesse Barnes2dd24552013-04-25 12:55:01 -07006370 i9xx_pfit_enable(intel_crtc);
6371
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006372 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006373
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006374 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006375 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006376
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006377 assert_vblank_disabled(crtc);
6378 drm_crtc_vblank_on(crtc);
6379
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006380 for_each_encoder_on_crtc(dev, crtc, encoder)
6381 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006382}
6383
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006384static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6385{
6386 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006387 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006388
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006389 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6390 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006391}
6392
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006393static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006394{
6395 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006396 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006398 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006399 struct intel_crtc_state *pipe_config =
6400 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006401 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006402
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006403 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006404 return;
6405
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006406 i9xx_set_pll_dividers(intel_crtc);
6407
Ville Syrjälä37a56502016-06-22 21:57:04 +03006408 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306409 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006410
6411 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006412 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006413
Daniel Vetter5b18e572014-04-24 23:55:06 +02006414 i9xx_set_pipeconf(intel_crtc);
6415
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006416 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006417
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006418 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006419 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006420
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006421 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006422 if (encoder->pre_enable)
6423 encoder->pre_enable(encoder);
6424
Daniel Vetterf6736a12013-06-05 13:34:30 +02006425 i9xx_enable_pll(intel_crtc);
6426
Jesse Barnes2dd24552013-04-25 12:55:01 -07006427 i9xx_pfit_enable(intel_crtc);
6428
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006429 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006430
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006431 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006432 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006433
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006434 assert_vblank_disabled(crtc);
6435 drm_crtc_vblank_on(crtc);
6436
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006437 for_each_encoder_on_crtc(dev, crtc, encoder)
6438 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006439}
6440
Daniel Vetter87476d62013-04-11 16:29:06 +02006441static void i9xx_pfit_disable(struct intel_crtc *crtc)
6442{
6443 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006444 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006445
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006446 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006447 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006448
6449 assert_pipe_disabled(dev_priv, crtc->pipe);
6450
Daniel Vetter328d8e82013-05-08 10:36:31 +02006451 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6452 I915_READ(PFIT_CONTROL));
6453 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006454}
6455
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006456static void i9xx_crtc_disable(struct drm_crtc *crtc)
6457{
6458 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006459 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006461 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006462 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006463
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006464 /*
6465 * On gen2 planes are double buffered but the pipe isn't, so we must
6466 * wait for planes to fully turn off before disabling the pipe.
6467 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006468 if (IS_GEN2(dev))
6469 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006470
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006471 for_each_encoder_on_crtc(dev, crtc, encoder)
6472 encoder->disable(encoder);
6473
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006474 drm_crtc_vblank_off(crtc);
6475 assert_vblank_disabled(crtc);
6476
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006477 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006478
Daniel Vetter87476d62013-04-11 16:29:06 +02006479 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006480
Jesse Barnes89b667f2013-04-18 14:51:36 -07006481 for_each_encoder_on_crtc(dev, crtc, encoder)
6482 if (encoder->post_disable)
6483 encoder->post_disable(encoder);
6484
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006485 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006486 if (IS_CHERRYVIEW(dev))
6487 chv_disable_pll(dev_priv, pipe);
6488 else if (IS_VALLEYVIEW(dev))
6489 vlv_disable_pll(dev_priv, pipe);
6490 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006491 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006492 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006493
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006494 for_each_encoder_on_crtc(dev, crtc, encoder)
6495 if (encoder->post_pll_disable)
6496 encoder->post_pll_disable(encoder);
6497
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006498 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006499 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006500}
6501
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006502static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006503{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006504 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006505 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006506 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006507 enum intel_display_power_domain domain;
6508 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006509
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006510 if (!intel_crtc->active)
6511 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006512
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006513 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006514 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006515
Ville Syrjälä2622a082016-03-09 19:07:26 +02006516 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006517
6518 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6519 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006520 }
6521
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006522 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006523
Ville Syrjälä78108b72016-05-27 20:59:19 +03006524 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6525 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006526
6527 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6528 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006529 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006530 crtc->enabled = false;
6531 crtc->state->connector_mask = 0;
6532 crtc->state->encoder_mask = 0;
6533
6534 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6535 encoder->base.crtc = NULL;
6536
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006537 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006538 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006539 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006540
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006541 domains = intel_crtc->enabled_power_domains;
6542 for_each_power_domain(domain, domains)
6543 intel_display_power_put(dev_priv, domain);
6544 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006545
6546 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6547 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006548}
6549
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006550/*
6551 * turn all crtc's off, but do not adjust state
6552 * This has to be paired with a call to intel_modeset_setup_hw_state.
6553 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006554int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006555{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006556 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006557 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006558 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006559
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006560 state = drm_atomic_helper_suspend(dev);
6561 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006562 if (ret)
6563 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006564 else
6565 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006566 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006567}
6568
Chris Wilsonea5b2132010-08-04 13:50:23 +01006569void intel_encoder_destroy(struct drm_encoder *encoder)
6570{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006571 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006572
Chris Wilsonea5b2132010-08-04 13:50:23 +01006573 drm_encoder_cleanup(encoder);
6574 kfree(intel_encoder);
6575}
6576
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006577/* Cross check the actual hw state with our own modeset state tracking (and it's
6578 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006579static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006580{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006581 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006582
6583 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6584 connector->base.base.id,
6585 connector->base.name);
6586
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006587 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006588 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006589 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006590
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006591 I915_STATE_WARN(!crtc,
6592 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006593
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006594 if (!crtc)
6595 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006596
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006597 I915_STATE_WARN(!crtc->state->active,
6598 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006599
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006600 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006601 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006602
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006603 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006604 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006605
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006606 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006607 "attached encoder crtc differs from connector crtc\n");
6608 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006609 I915_STATE_WARN(crtc && crtc->state->active,
6610 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006611 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006612 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006613 }
6614}
6615
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006616int intel_connector_init(struct intel_connector *connector)
6617{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006618 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006619
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006620 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006621 return -ENOMEM;
6622
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006623 return 0;
6624}
6625
6626struct intel_connector *intel_connector_alloc(void)
6627{
6628 struct intel_connector *connector;
6629
6630 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6631 if (!connector)
6632 return NULL;
6633
6634 if (intel_connector_init(connector) < 0) {
6635 kfree(connector);
6636 return NULL;
6637 }
6638
6639 return connector;
6640}
6641
Daniel Vetterf0947c32012-07-02 13:10:34 +02006642/* Simple connector->get_hw_state implementation for encoders that support only
6643 * one connector and no cloning and hence the encoder state determines the state
6644 * of the connector. */
6645bool intel_connector_get_hw_state(struct intel_connector *connector)
6646{
Daniel Vetter24929352012-07-02 20:28:59 +02006647 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006648 struct intel_encoder *encoder = connector->encoder;
6649
6650 return encoder->get_hw_state(encoder, &pipe);
6651}
6652
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006653static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006654{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006655 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6656 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006657
6658 return 0;
6659}
6660
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006661static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006662 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006663{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006664 struct drm_atomic_state *state = pipe_config->base.state;
6665 struct intel_crtc *other_crtc;
6666 struct intel_crtc_state *other_crtc_state;
6667
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006668 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6669 pipe_name(pipe), pipe_config->fdi_lanes);
6670 if (pipe_config->fdi_lanes > 4) {
6671 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6672 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006673 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006674 }
6675
Paulo Zanonibafb6552013-11-02 21:07:44 -07006676 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006677 if (pipe_config->fdi_lanes > 2) {
6678 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6679 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006680 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006681 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006682 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006683 }
6684 }
6685
6686 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006687 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006688
6689 /* Ivybridge 3 pipe is really complicated */
6690 switch (pipe) {
6691 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006692 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006693 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006694 if (pipe_config->fdi_lanes <= 2)
6695 return 0;
6696
6697 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6698 other_crtc_state =
6699 intel_atomic_get_crtc_state(state, other_crtc);
6700 if (IS_ERR(other_crtc_state))
6701 return PTR_ERR(other_crtc_state);
6702
6703 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006704 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6705 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006706 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006707 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006708 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006709 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006710 if (pipe_config->fdi_lanes > 2) {
6711 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6712 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006713 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006714 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006715
6716 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6717 other_crtc_state =
6718 intel_atomic_get_crtc_state(state, other_crtc);
6719 if (IS_ERR(other_crtc_state))
6720 return PTR_ERR(other_crtc_state);
6721
6722 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006723 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006724 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006725 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006726 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006727 default:
6728 BUG();
6729 }
6730}
6731
Daniel Vettere29c22c2013-02-21 00:00:16 +01006732#define RETRY 1
6733static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006734 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006735{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006736 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006737 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006738 int lane, link_bw, fdi_dotclock, ret;
6739 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006740
Daniel Vettere29c22c2013-02-21 00:00:16 +01006741retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006742 /* FDI is a binary signal running at ~2.7GHz, encoding
6743 * each output octet as 10 bits. The actual frequency
6744 * is stored as a divider into a 100MHz clock, and the
6745 * mode pixel clock is stored in units of 1KHz.
6746 * Hence the bw of each lane in terms of the mode signal
6747 * is:
6748 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006749 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006750
Damien Lespiau241bfc32013-09-25 16:45:37 +01006751 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006752
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006753 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006754 pipe_config->pipe_bpp);
6755
6756 pipe_config->fdi_lanes = lane;
6757
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006758 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006759 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006760
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006761 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006762 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006763 pipe_config->pipe_bpp -= 2*3;
6764 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6765 pipe_config->pipe_bpp);
6766 needs_recompute = true;
6767 pipe_config->bw_constrained = true;
6768
6769 goto retry;
6770 }
6771
6772 if (needs_recompute)
6773 return RETRY;
6774
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006775 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006776}
6777
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006778static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6779 struct intel_crtc_state *pipe_config)
6780{
6781 if (pipe_config->pipe_bpp > 24)
6782 return false;
6783
6784 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006785 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006786 return true;
6787
6788 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006789 * We compare against max which means we must take
6790 * the increased cdclk requirement into account when
6791 * calculating the new cdclk.
6792 *
6793 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006794 */
6795 return ilk_pipe_pixel_rate(pipe_config) <=
6796 dev_priv->max_cdclk_freq * 95 / 100;
6797}
6798
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006799static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006800 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006801{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006802 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006803 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006804
Jani Nikulad330a952014-01-21 11:24:25 +02006805 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006806 hsw_crtc_supports_ips(crtc) &&
6807 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006808}
6809
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006810static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6811{
6812 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6813
6814 /* GDG double wide on either pipe, otherwise pipe A only */
6815 return INTEL_INFO(dev_priv)->gen < 4 &&
6816 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6817}
6818
Daniel Vettera43f6e02013-06-07 23:10:32 +02006819static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006820 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006821{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006822 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006823 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006824 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006825 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006826
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006827 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006828 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006829
6830 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006831 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006832 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006833 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006834 if (intel_crtc_supports_double_wide(crtc) &&
6835 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006836 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006837 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006838 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006839 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006840
Ville Syrjäläf3261152016-05-24 21:34:18 +03006841 if (adjusted_mode->crtc_clock > clock_limit) {
6842 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6843 adjusted_mode->crtc_clock, clock_limit,
6844 yesno(pipe_config->double_wide));
6845 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006846 }
Chris Wilson89749352010-09-12 18:25:19 +01006847
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006848 /*
6849 * Pipe horizontal size must be even in:
6850 * - DVO ganged mode
6851 * - LVDS dual channel mode
6852 * - Double wide pipe
6853 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006854 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006855 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6856 pipe_config->pipe_src_w &= ~1;
6857
Damien Lespiau8693a822013-05-03 18:48:11 +01006858 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6859 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006860 */
6861 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006862 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006863 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006864
Damien Lespiauf5adf942013-06-24 18:29:34 +01006865 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006866 hsw_compute_ips_config(crtc, pipe_config);
6867
Daniel Vetter877d48d2013-04-19 11:24:43 +02006868 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006869 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006870
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006871 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006872}
6873
Ville Syrjälä1652d192015-03-31 14:12:01 +03006874static int skylake_get_display_clock_speed(struct drm_device *dev)
6875{
6876 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006877 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006878
Ville Syrjäläea617912016-05-13 23:41:24 +03006879 skl_dpll0_update(dev_priv);
6880
Ville Syrjälä63911d72016-05-13 23:41:32 +03006881 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006882 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006883
Ville Syrjäläea617912016-05-13 23:41:24 +03006884 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006885
Ville Syrjälä63911d72016-05-13 23:41:32 +03006886 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006887 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6888 case CDCLK_FREQ_450_432:
6889 return 432000;
6890 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006891 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006892 case CDCLK_FREQ_540:
6893 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006894 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006895 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006896 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006897 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006898 }
6899 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006900 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6901 case CDCLK_FREQ_450_432:
6902 return 450000;
6903 case CDCLK_FREQ_337_308:
6904 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006905 case CDCLK_FREQ_540:
6906 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006907 case CDCLK_FREQ_675_617:
6908 return 675000;
6909 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006910 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006911 }
6912 }
6913
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006914 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006915}
6916
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006917static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6918{
6919 u32 val;
6920
6921 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006922 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006923
6924 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006925 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006926 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006927
Imre Deak1c3f7702016-05-24 15:38:32 +03006928 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6929 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006930
6931 val = I915_READ(BXT_DE_PLL_CTL);
6932 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6933 dev_priv->cdclk_pll.ref;
6934}
6935
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006936static int broxton_get_display_clock_speed(struct drm_device *dev)
6937{
6938 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006939 u32 divider;
6940 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006941
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006942 bxt_de_pll_update(dev_priv);
6943
Ville Syrjäläf5986242016-05-13 23:41:37 +03006944 vco = dev_priv->cdclk_pll.vco;
6945 if (vco == 0)
6946 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006947
Ville Syrjäläf5986242016-05-13 23:41:37 +03006948 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006949
Ville Syrjäläf5986242016-05-13 23:41:37 +03006950 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006951 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006952 div = 2;
6953 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006954 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006955 div = 3;
6956 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006957 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006958 div = 4;
6959 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006960 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006961 div = 8;
6962 break;
6963 default:
6964 MISSING_CASE(divider);
6965 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006966 }
6967
Ville Syrjäläf5986242016-05-13 23:41:37 +03006968 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006969}
6970
Ville Syrjälä1652d192015-03-31 14:12:01 +03006971static int broadwell_get_display_clock_speed(struct drm_device *dev)
6972{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006973 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006974 uint32_t lcpll = I915_READ(LCPLL_CTL);
6975 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6976
6977 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6978 return 800000;
6979 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6980 return 450000;
6981 else if (freq == LCPLL_CLK_FREQ_450)
6982 return 450000;
6983 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6984 return 540000;
6985 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6986 return 337500;
6987 else
6988 return 675000;
6989}
6990
6991static int haswell_get_display_clock_speed(struct drm_device *dev)
6992{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006993 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006994 uint32_t lcpll = I915_READ(LCPLL_CTL);
6995 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6996
6997 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6998 return 800000;
6999 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7000 return 450000;
7001 else if (freq == LCPLL_CLK_FREQ_450)
7002 return 450000;
7003 else if (IS_HSW_ULT(dev))
7004 return 337500;
7005 else
7006 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007007}
7008
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007009static int valleyview_get_display_clock_speed(struct drm_device *dev)
7010{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007011 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
7012 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007013}
7014
Ville Syrjäläb37a6432015-03-31 14:11:54 +03007015static int ilk_get_display_clock_speed(struct drm_device *dev)
7016{
7017 return 450000;
7018}
7019
Jesse Barnese70236a2009-09-21 10:42:27 -07007020static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08007021{
Jesse Barnese70236a2009-09-21 10:42:27 -07007022 return 400000;
7023}
Jesse Barnes79e53942008-11-07 14:24:08 -08007024
Jesse Barnese70236a2009-09-21 10:42:27 -07007025static int i915_get_display_clock_speed(struct drm_device *dev)
7026{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007027 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007028}
Jesse Barnes79e53942008-11-07 14:24:08 -08007029
Jesse Barnese70236a2009-09-21 10:42:27 -07007030static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
7031{
7032 return 200000;
7033}
Jesse Barnes79e53942008-11-07 14:24:08 -08007034
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007035static int pnv_get_display_clock_speed(struct drm_device *dev)
7036{
7037 u16 gcfgc = 0;
7038
7039 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
7040
7041 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7042 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007043 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007044 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007045 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007046 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007047 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007048 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7049 return 200000;
7050 default:
7051 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7052 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007053 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007054 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007055 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007056 }
7057}
7058
Jesse Barnese70236a2009-09-21 10:42:27 -07007059static int i915gm_get_display_clock_speed(struct drm_device *dev)
7060{
7061 u16 gcfgc = 0;
7062
7063 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
7064
7065 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03007066 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007067 else {
7068 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7069 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007070 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007071 default:
7072 case GC_DISPLAY_CLOCK_190_200_MHZ:
7073 return 190000;
7074 }
7075 }
7076}
Jesse Barnes79e53942008-11-07 14:24:08 -08007077
Jesse Barnese70236a2009-09-21 10:42:27 -07007078static int i865_get_display_clock_speed(struct drm_device *dev)
7079{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007080 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007081}
7082
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007083static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07007084{
7085 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007086
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007087 /*
7088 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7089 * encoding is different :(
7090 * FIXME is this the right way to detect 852GM/852GMV?
7091 */
7092 if (dev->pdev->revision == 0x1)
7093 return 133333;
7094
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007095 pci_bus_read_config_word(dev->pdev->bus,
7096 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7097
Jesse Barnese70236a2009-09-21 10:42:27 -07007098 /* Assume that the hardware is in the high speed state. This
7099 * should be the default.
7100 */
7101 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7102 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007103 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007104 case GC_CLOCK_100_200:
7105 return 200000;
7106 case GC_CLOCK_166_250:
7107 return 250000;
7108 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007109 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007110 case GC_CLOCK_133_266:
7111 case GC_CLOCK_133_266_2:
7112 case GC_CLOCK_166_266:
7113 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007114 }
7115
7116 /* Shouldn't happen */
7117 return 0;
7118}
7119
7120static int i830_get_display_clock_speed(struct drm_device *dev)
7121{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007122 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007123}
7124
Ville Syrjälä34edce22015-05-22 11:22:33 +03007125static unsigned int intel_hpll_vco(struct drm_device *dev)
7126{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007127 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007128 static const unsigned int blb_vco[8] = {
7129 [0] = 3200000,
7130 [1] = 4000000,
7131 [2] = 5333333,
7132 [3] = 4800000,
7133 [4] = 6400000,
7134 };
7135 static const unsigned int pnv_vco[8] = {
7136 [0] = 3200000,
7137 [1] = 4000000,
7138 [2] = 5333333,
7139 [3] = 4800000,
7140 [4] = 2666667,
7141 };
7142 static const unsigned int cl_vco[8] = {
7143 [0] = 3200000,
7144 [1] = 4000000,
7145 [2] = 5333333,
7146 [3] = 6400000,
7147 [4] = 3333333,
7148 [5] = 3566667,
7149 [6] = 4266667,
7150 };
7151 static const unsigned int elk_vco[8] = {
7152 [0] = 3200000,
7153 [1] = 4000000,
7154 [2] = 5333333,
7155 [3] = 4800000,
7156 };
7157 static const unsigned int ctg_vco[8] = {
7158 [0] = 3200000,
7159 [1] = 4000000,
7160 [2] = 5333333,
7161 [3] = 6400000,
7162 [4] = 2666667,
7163 [5] = 4266667,
7164 };
7165 const unsigned int *vco_table;
7166 unsigned int vco;
7167 uint8_t tmp = 0;
7168
7169 /* FIXME other chipsets? */
7170 if (IS_GM45(dev))
7171 vco_table = ctg_vco;
7172 else if (IS_G4X(dev))
7173 vco_table = elk_vco;
7174 else if (IS_CRESTLINE(dev))
7175 vco_table = cl_vco;
7176 else if (IS_PINEVIEW(dev))
7177 vco_table = pnv_vco;
7178 else if (IS_G33(dev))
7179 vco_table = blb_vco;
7180 else
7181 return 0;
7182
7183 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7184
7185 vco = vco_table[tmp & 0x7];
7186 if (vco == 0)
7187 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7188 else
7189 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7190
7191 return vco;
7192}
7193
7194static int gm45_get_display_clock_speed(struct drm_device *dev)
7195{
7196 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7197 uint16_t tmp = 0;
7198
7199 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7200
7201 cdclk_sel = (tmp >> 12) & 0x1;
7202
7203 switch (vco) {
7204 case 2666667:
7205 case 4000000:
7206 case 5333333:
7207 return cdclk_sel ? 333333 : 222222;
7208 case 3200000:
7209 return cdclk_sel ? 320000 : 228571;
7210 default:
7211 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7212 return 222222;
7213 }
7214}
7215
7216static int i965gm_get_display_clock_speed(struct drm_device *dev)
7217{
7218 static const uint8_t div_3200[] = { 16, 10, 8 };
7219 static const uint8_t div_4000[] = { 20, 12, 10 };
7220 static const uint8_t div_5333[] = { 24, 16, 14 };
7221 const uint8_t *div_table;
7222 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7223 uint16_t tmp = 0;
7224
7225 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7226
7227 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7228
7229 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7230 goto fail;
7231
7232 switch (vco) {
7233 case 3200000:
7234 div_table = div_3200;
7235 break;
7236 case 4000000:
7237 div_table = div_4000;
7238 break;
7239 case 5333333:
7240 div_table = div_5333;
7241 break;
7242 default:
7243 goto fail;
7244 }
7245
7246 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7247
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007248fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007249 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7250 return 200000;
7251}
7252
7253static int g33_get_display_clock_speed(struct drm_device *dev)
7254{
7255 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7256 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7257 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7258 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7259 const uint8_t *div_table;
7260 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7261 uint16_t tmp = 0;
7262
7263 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7264
7265 cdclk_sel = (tmp >> 4) & 0x7;
7266
7267 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7268 goto fail;
7269
7270 switch (vco) {
7271 case 3200000:
7272 div_table = div_3200;
7273 break;
7274 case 4000000:
7275 div_table = div_4000;
7276 break;
7277 case 4800000:
7278 div_table = div_4800;
7279 break;
7280 case 5333333:
7281 div_table = div_5333;
7282 break;
7283 default:
7284 goto fail;
7285 }
7286
7287 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7288
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007289fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007290 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7291 return 190476;
7292}
7293
Zhenyu Wang2c072452009-06-05 15:38:42 +08007294static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007295intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007296{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007297 while (*num > DATA_LINK_M_N_MASK ||
7298 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007299 *num >>= 1;
7300 *den >>= 1;
7301 }
7302}
7303
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007304static void compute_m_n(unsigned int m, unsigned int n,
7305 uint32_t *ret_m, uint32_t *ret_n)
7306{
7307 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7308 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7309 intel_reduce_m_n_ratio(ret_m, ret_n);
7310}
7311
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007312void
7313intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7314 int pixel_clock, int link_clock,
7315 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007316{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007317 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007318
7319 compute_m_n(bits_per_pixel * pixel_clock,
7320 link_clock * nlanes * 8,
7321 &m_n->gmch_m, &m_n->gmch_n);
7322
7323 compute_m_n(pixel_clock, link_clock,
7324 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007325}
7326
Chris Wilsona7615032011-01-12 17:04:08 +00007327static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7328{
Jani Nikulad330a952014-01-21 11:24:25 +02007329 if (i915.panel_use_ssc >= 0)
7330 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007331 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007332 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007333}
7334
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007335static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007336{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007337 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007338}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007339
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007340static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7341{
7342 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007343}
7344
Daniel Vetterf47709a2013-03-28 10:42:02 +01007345static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007346 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007347 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007348{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007349 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007350 u32 fp, fp2 = 0;
7351
7352 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007353 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007354 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007355 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007356 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007357 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007358 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007359 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007360 }
7361
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007362 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007363
Daniel Vetterf47709a2013-03-28 10:42:02 +01007364 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007365 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007366 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007367 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007368 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007369 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007370 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007371 }
7372}
7373
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007374static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7375 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007376{
7377 u32 reg_val;
7378
7379 /*
7380 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7381 * and set it to a reasonable value instead.
7382 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007383 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007384 reg_val &= 0xffffff00;
7385 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007386 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007387
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007388 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007389 reg_val &= 0x8cffffff;
7390 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007391 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007392
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007393 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007394 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007395 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007396
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007397 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007398 reg_val &= 0x00ffffff;
7399 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007400 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007401}
7402
Daniel Vetterb5518422013-05-03 11:49:48 +02007403static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7404 struct intel_link_m_n *m_n)
7405{
7406 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007407 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007408 int pipe = crtc->pipe;
7409
Daniel Vettere3b95f12013-05-03 11:49:49 +02007410 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7411 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7412 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7413 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007414}
7415
7416static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007417 struct intel_link_m_n *m_n,
7418 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007419{
7420 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007421 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007422 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007423 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007424
7425 if (INTEL_INFO(dev)->gen >= 5) {
7426 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7427 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7428 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7429 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007430 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7431 * for gen < 8) and if DRRS is supported (to make sure the
7432 * registers are not unnecessarily accessed).
7433 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307434 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007435 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007436 I915_WRITE(PIPE_DATA_M2(transcoder),
7437 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7438 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7439 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7440 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7441 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007442 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007443 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7444 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7445 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7446 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007447 }
7448}
7449
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307450void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007451{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307452 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7453
7454 if (m_n == M1_N1) {
7455 dp_m_n = &crtc->config->dp_m_n;
7456 dp_m2_n2 = &crtc->config->dp_m2_n2;
7457 } else if (m_n == M2_N2) {
7458
7459 /*
7460 * M2_N2 registers are not supported. Hence m2_n2 divider value
7461 * needs to be programmed into M1_N1.
7462 */
7463 dp_m_n = &crtc->config->dp_m2_n2;
7464 } else {
7465 DRM_ERROR("Unsupported divider value\n");
7466 return;
7467 }
7468
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007469 if (crtc->config->has_pch_encoder)
7470 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007471 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307472 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007473}
7474
Daniel Vetter251ac862015-06-18 10:30:24 +02007475static void vlv_compute_dpll(struct intel_crtc *crtc,
7476 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007477{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007478 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007479 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007480 if (crtc->pipe != PIPE_A)
7481 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007482
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007483 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007484 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007485 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7486 DPLL_EXT_BUFFER_ENABLE_VLV;
7487
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007488 pipe_config->dpll_hw_state.dpll_md =
7489 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7490}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007491
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007492static void chv_compute_dpll(struct intel_crtc *crtc,
7493 struct intel_crtc_state *pipe_config)
7494{
7495 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007496 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007497 if (crtc->pipe != PIPE_A)
7498 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7499
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007500 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007501 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007502 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7503
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007504 pipe_config->dpll_hw_state.dpll_md =
7505 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007506}
7507
Ville Syrjäläd288f652014-10-28 13:20:22 +02007508static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007509 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007510{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007511 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007512 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007513 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007514 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007515 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007516 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007517
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007518 /* Enable Refclk */
7519 I915_WRITE(DPLL(pipe),
7520 pipe_config->dpll_hw_state.dpll &
7521 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7522
7523 /* No need to actually set up the DPLL with DSI */
7524 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7525 return;
7526
Ville Syrjäläa5805162015-05-26 20:42:30 +03007527 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007528
Ville Syrjäläd288f652014-10-28 13:20:22 +02007529 bestn = pipe_config->dpll.n;
7530 bestm1 = pipe_config->dpll.m1;
7531 bestm2 = pipe_config->dpll.m2;
7532 bestp1 = pipe_config->dpll.p1;
7533 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007534
Jesse Barnes89b667f2013-04-18 14:51:36 -07007535 /* See eDP HDMI DPIO driver vbios notes doc */
7536
7537 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007538 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007539 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007540
7541 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007542 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007543
7544 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007545 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007546 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007547 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007548
7549 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007550 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007551
7552 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007553 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7554 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7555 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007556 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007557
7558 /*
7559 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7560 * but we don't support that).
7561 * Note: don't use the DAC post divider as it seems unstable.
7562 */
7563 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007564 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007565
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007566 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007567 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007568
Jesse Barnes89b667f2013-04-18 14:51:36 -07007569 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007570 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007571 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7572 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007573 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007574 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007575 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007576 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007577 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007578
Ville Syrjälä37a56502016-06-22 21:57:04 +03007579 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007580 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007581 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007582 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007583 0x0df40000);
7584 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007585 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007586 0x0df70000);
7587 } else { /* HDMI or VGA */
7588 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007589 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007590 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007591 0x0df70000);
7592 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007593 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007594 0x0df40000);
7595 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007596
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007597 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007598 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007599 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007600 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007601 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007602
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007603 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007604 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007605}
7606
Ville Syrjäläd288f652014-10-28 13:20:22 +02007607static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007608 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007609{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007610 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007611 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007612 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007613 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307614 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007615 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307616 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307617 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007618
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007619 /* Enable Refclk and SSC */
7620 I915_WRITE(DPLL(pipe),
7621 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7622
7623 /* No need to actually set up the DPLL with DSI */
7624 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7625 return;
7626
Ville Syrjäläd288f652014-10-28 13:20:22 +02007627 bestn = pipe_config->dpll.n;
7628 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7629 bestm1 = pipe_config->dpll.m1;
7630 bestm2 = pipe_config->dpll.m2 >> 22;
7631 bestp1 = pipe_config->dpll.p1;
7632 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307633 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307634 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307635 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007636
Ville Syrjäläa5805162015-05-26 20:42:30 +03007637 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007638
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007639 /* p1 and p2 divider */
7640 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7641 5 << DPIO_CHV_S1_DIV_SHIFT |
7642 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7643 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7644 1 << DPIO_CHV_K_DIV_SHIFT);
7645
7646 /* Feedback post-divider - m2 */
7647 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7648
7649 /* Feedback refclk divider - n and m1 */
7650 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7651 DPIO_CHV_M1_DIV_BY_2 |
7652 1 << DPIO_CHV_N_DIV_SHIFT);
7653
7654 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007655 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007656
7657 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307658 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7659 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7660 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7661 if (bestm2_frac)
7662 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7663 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007664
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307665 /* Program digital lock detect threshold */
7666 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7667 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7668 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7669 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7670 if (!bestm2_frac)
7671 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7672 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7673
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007674 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307675 if (vco == 5400000) {
7676 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7677 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7678 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7679 tribuf_calcntr = 0x9;
7680 } else if (vco <= 6200000) {
7681 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7682 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7683 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7684 tribuf_calcntr = 0x9;
7685 } else if (vco <= 6480000) {
7686 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7687 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7688 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7689 tribuf_calcntr = 0x8;
7690 } else {
7691 /* Not supported. Apply the same limits as in the max case */
7692 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7693 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7694 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7695 tribuf_calcntr = 0;
7696 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007697 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7698
Ville Syrjälä968040b2015-03-11 22:52:08 +02007699 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307700 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7701 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7702 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7703
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007704 /* AFC Recal */
7705 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7706 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7707 DPIO_AFC_RECAL);
7708
Ville Syrjäläa5805162015-05-26 20:42:30 +03007709 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007710}
7711
Ville Syrjäläd288f652014-10-28 13:20:22 +02007712/**
7713 * vlv_force_pll_on - forcibly enable just the PLL
7714 * @dev_priv: i915 private structure
7715 * @pipe: pipe PLL to enable
7716 * @dpll: PLL configuration
7717 *
7718 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7719 * in cases where we need the PLL enabled even when @pipe is not going to
7720 * be enabled.
7721 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007722int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7723 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007724{
7725 struct intel_crtc *crtc =
7726 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007727 struct intel_crtc_state *pipe_config;
7728
7729 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7730 if (!pipe_config)
7731 return -ENOMEM;
7732
7733 pipe_config->base.crtc = &crtc->base;
7734 pipe_config->pixel_multiplier = 1;
7735 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007736
7737 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007738 chv_compute_dpll(crtc, pipe_config);
7739 chv_prepare_pll(crtc, pipe_config);
7740 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007741 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007742 vlv_compute_dpll(crtc, pipe_config);
7743 vlv_prepare_pll(crtc, pipe_config);
7744 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007745 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007746
7747 kfree(pipe_config);
7748
7749 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007750}
7751
7752/**
7753 * vlv_force_pll_off - forcibly disable just the PLL
7754 * @dev_priv: i915 private structure
7755 * @pipe: pipe PLL to disable
7756 *
7757 * Disable the PLL for @pipe. To be used in cases where we need
7758 * the PLL enabled even when @pipe is not going to be enabled.
7759 */
7760void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7761{
7762 if (IS_CHERRYVIEW(dev))
7763 chv_disable_pll(to_i915(dev), pipe);
7764 else
7765 vlv_disable_pll(to_i915(dev), pipe);
7766}
7767
Daniel Vetter251ac862015-06-18 10:30:24 +02007768static void i9xx_compute_dpll(struct intel_crtc *crtc,
7769 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007770 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007771{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007772 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007773 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007774 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007775 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007776
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007777 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307778
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007779 dpll = DPLL_VGA_MODE_DIS;
7780
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007781 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007782 dpll |= DPLLB_MODE_LVDS;
7783 else
7784 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007785
Daniel Vetteref1b4602013-06-01 17:17:04 +02007786 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007787 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007788 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007789 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007790
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007791 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7792 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007793 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007794
Ville Syrjälä37a56502016-06-22 21:57:04 +03007795 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007796 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007797
7798 /* compute bitmask from p1 value */
7799 if (IS_PINEVIEW(dev))
7800 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7801 else {
7802 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7803 if (IS_G4X(dev) && reduced_clock)
7804 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7805 }
7806 switch (clock->p2) {
7807 case 5:
7808 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7809 break;
7810 case 7:
7811 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7812 break;
7813 case 10:
7814 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7815 break;
7816 case 14:
7817 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7818 break;
7819 }
7820 if (INTEL_INFO(dev)->gen >= 4)
7821 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7822
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007823 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007824 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007825 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007826 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007827 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7828 else
7829 dpll |= PLL_REF_INPUT_DREFCLK;
7830
7831 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007832 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007833
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007834 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007835 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007836 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007837 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007838 }
7839}
7840
Daniel Vetter251ac862015-06-18 10:30:24 +02007841static void i8xx_compute_dpll(struct intel_crtc *crtc,
7842 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007843 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007844{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007845 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007846 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007847 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007848 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007849
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007850 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307851
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007852 dpll = DPLL_VGA_MODE_DIS;
7853
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007854 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007855 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7856 } else {
7857 if (clock->p1 == 2)
7858 dpll |= PLL_P1_DIVIDE_BY_TWO;
7859 else
7860 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7861 if (clock->p2 == 4)
7862 dpll |= PLL_P2_DIVIDE_BY_4;
7863 }
7864
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007865 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007866 dpll |= DPLL_DVO_2X_MODE;
7867
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007868 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007869 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007870 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7871 else
7872 dpll |= PLL_REF_INPUT_DREFCLK;
7873
7874 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007875 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007876}
7877
Daniel Vetter8a654f32013-06-01 17:16:22 +02007878static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007879{
7880 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007881 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007882 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007883 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007884 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007885 uint32_t crtc_vtotal, crtc_vblank_end;
7886 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007887
7888 /* We need to be careful not to changed the adjusted mode, for otherwise
7889 * the hw state checker will get angry at the mismatch. */
7890 crtc_vtotal = adjusted_mode->crtc_vtotal;
7891 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007892
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007893 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007894 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007895 crtc_vtotal -= 1;
7896 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007897
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007898 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007899 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7900 else
7901 vsyncshift = adjusted_mode->crtc_hsync_start -
7902 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007903 if (vsyncshift < 0)
7904 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007905 }
7906
7907 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007908 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007909
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007910 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007911 (adjusted_mode->crtc_hdisplay - 1) |
7912 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007913 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007914 (adjusted_mode->crtc_hblank_start - 1) |
7915 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007916 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007917 (adjusted_mode->crtc_hsync_start - 1) |
7918 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7919
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007920 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007921 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007922 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007923 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007924 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007925 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007926 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007927 (adjusted_mode->crtc_vsync_start - 1) |
7928 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7929
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007930 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7931 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7932 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7933 * bits. */
7934 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7935 (pipe == PIPE_B || pipe == PIPE_C))
7936 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7937
Jani Nikulabc58be62016-03-18 17:05:39 +02007938}
7939
7940static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7941{
7942 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007943 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007944 enum pipe pipe = intel_crtc->pipe;
7945
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007946 /* pipesrc controls the size that is scaled from, which should
7947 * always be the user's requested size.
7948 */
7949 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007950 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7951 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007952}
7953
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007954static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007955 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007956{
7957 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007958 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007959 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7960 uint32_t tmp;
7961
7962 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007963 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7964 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007965 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007966 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7967 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007968 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007969 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7970 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007971
7972 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007973 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7974 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007975 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007976 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7977 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007978 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007979 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7980 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007981
7982 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007983 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7984 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7985 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007986 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007987}
7988
7989static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7990 struct intel_crtc_state *pipe_config)
7991{
7992 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007993 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007994 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007995
7996 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007997 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7998 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7999
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008000 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8001 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008002}
8003
Daniel Vetterf6a83282014-02-11 15:28:57 -08008004void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008005 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03008006{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008007 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8008 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8009 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8010 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008011
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008012 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8013 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8014 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8015 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008016
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008017 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008018 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03008019
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008020 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8021 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008022
8023 mode->hsync = drm_mode_hsync(mode);
8024 mode->vrefresh = drm_mode_vrefresh(mode);
8025 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03008026}
8027
Daniel Vetter84b046f2013-02-19 18:48:54 +01008028static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8029{
8030 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008031 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01008032 uint32_t pipeconf;
8033
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008034 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008035
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03008036 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8037 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8038 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02008039
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008040 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008041 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008042
Daniel Vetterff9ce462013-04-24 14:57:17 +02008043 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08008044 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008045 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008046 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02008047 pipeconf |= PIPECONF_DITHER_EN |
8048 PIPECONF_DITHER_TYPE_SP;
8049
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008050 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008051 case 18:
8052 pipeconf |= PIPECONF_6BPC;
8053 break;
8054 case 24:
8055 pipeconf |= PIPECONF_8BPC;
8056 break;
8057 case 30:
8058 pipeconf |= PIPECONF_10BPC;
8059 break;
8060 default:
8061 /* Case prevented by intel_choose_pipe_bpp_dither. */
8062 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01008063 }
8064 }
8065
8066 if (HAS_PIPE_CXSR(dev)) {
8067 if (intel_crtc->lowfreq_avail) {
8068 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8069 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8070 } else {
8071 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01008072 }
8073 }
8074
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008075 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008076 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008077 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008078 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8079 else
8080 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8081 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01008082 pipeconf |= PIPECONF_PROGRESSIVE;
8083
Wayne Boyer666a4532015-12-09 12:29:35 -08008084 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8085 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008086 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008087
Daniel Vetter84b046f2013-02-19 18:48:54 +01008088 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8089 POSTING_READ(PIPECONF(intel_crtc->pipe));
8090}
8091
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008092static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8093 struct intel_crtc_state *crtc_state)
8094{
8095 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008096 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008097 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008098 int refclk = 48000;
8099
8100 memset(&crtc_state->dpll_hw_state, 0,
8101 sizeof(crtc_state->dpll_hw_state));
8102
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008103 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008104 if (intel_panel_use_ssc(dev_priv)) {
8105 refclk = dev_priv->vbt.lvds_ssc_freq;
8106 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8107 }
8108
8109 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008110 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008111 limit = &intel_limits_i8xx_dvo;
8112 } else {
8113 limit = &intel_limits_i8xx_dac;
8114 }
8115
8116 if (!crtc_state->clock_set &&
8117 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8118 refclk, NULL, &crtc_state->dpll)) {
8119 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8120 return -EINVAL;
8121 }
8122
8123 i8xx_compute_dpll(crtc, crtc_state, NULL);
8124
8125 return 0;
8126}
8127
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008128static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8129 struct intel_crtc_state *crtc_state)
8130{
8131 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008132 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008133 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008134 int refclk = 96000;
8135
8136 memset(&crtc_state->dpll_hw_state, 0,
8137 sizeof(crtc_state->dpll_hw_state));
8138
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008139 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008140 if (intel_panel_use_ssc(dev_priv)) {
8141 refclk = dev_priv->vbt.lvds_ssc_freq;
8142 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8143 }
8144
8145 if (intel_is_dual_link_lvds(dev))
8146 limit = &intel_limits_g4x_dual_channel_lvds;
8147 else
8148 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008149 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8150 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008151 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008152 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008153 limit = &intel_limits_g4x_sdvo;
8154 } else {
8155 /* The option is for other outputs */
8156 limit = &intel_limits_i9xx_sdvo;
8157 }
8158
8159 if (!crtc_state->clock_set &&
8160 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8161 refclk, NULL, &crtc_state->dpll)) {
8162 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8163 return -EINVAL;
8164 }
8165
8166 i9xx_compute_dpll(crtc, crtc_state, NULL);
8167
8168 return 0;
8169}
8170
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008171static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8172 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008173{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008174 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008175 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008176 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008177 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008178
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008179 memset(&crtc_state->dpll_hw_state, 0,
8180 sizeof(crtc_state->dpll_hw_state));
8181
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008182 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008183 if (intel_panel_use_ssc(dev_priv)) {
8184 refclk = dev_priv->vbt.lvds_ssc_freq;
8185 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8186 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008187
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008188 limit = &intel_limits_pineview_lvds;
8189 } else {
8190 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008191 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008192
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008193 if (!crtc_state->clock_set &&
8194 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8195 refclk, NULL, &crtc_state->dpll)) {
8196 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8197 return -EINVAL;
8198 }
8199
8200 i9xx_compute_dpll(crtc, crtc_state, NULL);
8201
8202 return 0;
8203}
8204
8205static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8206 struct intel_crtc_state *crtc_state)
8207{
8208 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008209 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008210 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008211 int refclk = 96000;
8212
8213 memset(&crtc_state->dpll_hw_state, 0,
8214 sizeof(crtc_state->dpll_hw_state));
8215
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008216 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008217 if (intel_panel_use_ssc(dev_priv)) {
8218 refclk = dev_priv->vbt.lvds_ssc_freq;
8219 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008220 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008221
8222 limit = &intel_limits_i9xx_lvds;
8223 } else {
8224 limit = &intel_limits_i9xx_sdvo;
8225 }
8226
8227 if (!crtc_state->clock_set &&
8228 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8229 refclk, NULL, &crtc_state->dpll)) {
8230 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8231 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008232 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008233
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008234 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008235
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008236 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008237}
8238
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008239static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8240 struct intel_crtc_state *crtc_state)
8241{
8242 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008243 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008244
8245 memset(&crtc_state->dpll_hw_state, 0,
8246 sizeof(crtc_state->dpll_hw_state));
8247
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008248 if (!crtc_state->clock_set &&
8249 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8250 refclk, NULL, &crtc_state->dpll)) {
8251 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8252 return -EINVAL;
8253 }
8254
8255 chv_compute_dpll(crtc, crtc_state);
8256
8257 return 0;
8258}
8259
8260static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8261 struct intel_crtc_state *crtc_state)
8262{
8263 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008264 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008265
8266 memset(&crtc_state->dpll_hw_state, 0,
8267 sizeof(crtc_state->dpll_hw_state));
8268
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008269 if (!crtc_state->clock_set &&
8270 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8271 refclk, NULL, &crtc_state->dpll)) {
8272 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8273 return -EINVAL;
8274 }
8275
8276 vlv_compute_dpll(crtc, crtc_state);
8277
8278 return 0;
8279}
8280
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008281static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008282 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008283{
8284 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008285 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008286 uint32_t tmp;
8287
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008288 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8289 return;
8290
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008291 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008292 if (!(tmp & PFIT_ENABLE))
8293 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008294
Daniel Vetter06922822013-07-11 13:35:40 +02008295 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008296 if (INTEL_INFO(dev)->gen < 4) {
8297 if (crtc->pipe != PIPE_B)
8298 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008299 } else {
8300 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8301 return;
8302 }
8303
Daniel Vetter06922822013-07-11 13:35:40 +02008304 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008305 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008306}
8307
Jesse Barnesacbec812013-09-20 11:29:32 -07008308static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008309 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008310{
8311 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008312 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008313 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008314 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008315 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008316 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008317
Ville Syrjäläb5219732016-03-15 16:40:01 +02008318 /* In case of DSI, DPLL will not be used */
8319 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308320 return;
8321
Ville Syrjäläa5805162015-05-26 20:42:30 +03008322 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008323 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008324 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008325
8326 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8327 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8328 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8329 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8330 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8331
Imre Deakdccbea32015-06-22 23:35:51 +03008332 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008333}
8334
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008335static void
8336i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8337 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008338{
8339 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008340 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008341 u32 val, base, offset;
8342 int pipe = crtc->pipe, plane = crtc->plane;
8343 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008344 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008345 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008346 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008347
Damien Lespiau42a7b082015-02-05 19:35:13 +00008348 val = I915_READ(DSPCNTR(plane));
8349 if (!(val & DISPLAY_PLANE_ENABLE))
8350 return;
8351
Damien Lespiaud9806c92015-01-21 14:07:19 +00008352 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008353 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008354 DRM_DEBUG_KMS("failed to alloc fb\n");
8355 return;
8356 }
8357
Damien Lespiau1b842c82015-01-21 13:50:54 +00008358 fb = &intel_fb->base;
8359
Daniel Vetter18c52472015-02-10 17:16:09 +00008360 if (INTEL_INFO(dev)->gen >= 4) {
8361 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008362 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008363 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8364 }
8365 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008366
8367 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008368 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008369 fb->pixel_format = fourcc;
8370 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008371
8372 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008373 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008374 offset = I915_READ(DSPTILEOFF(plane));
8375 else
8376 offset = I915_READ(DSPLINOFF(plane));
8377 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8378 } else {
8379 base = I915_READ(DSPADDR(plane));
8380 }
8381 plane_config->base = base;
8382
8383 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008384 fb->width = ((val >> 16) & 0xfff) + 1;
8385 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008386
8387 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008388 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008389
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008390 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008391 fb->pixel_format,
8392 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008393
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008394 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008395
Damien Lespiau2844a922015-01-20 12:51:48 +00008396 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8397 pipe_name(pipe), plane, fb->width, fb->height,
8398 fb->bits_per_pixel, base, fb->pitches[0],
8399 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008400
Damien Lespiau2d140302015-02-05 17:22:18 +00008401 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008402}
8403
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008404static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008405 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008406{
8407 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008408 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008409 int pipe = pipe_config->cpu_transcoder;
8410 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008411 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008412 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008413 int refclk = 100000;
8414
Ville Syrjäläb5219732016-03-15 16:40:01 +02008415 /* In case of DSI, DPLL will not be used */
8416 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8417 return;
8418
Ville Syrjäläa5805162015-05-26 20:42:30 +03008419 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008420 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8421 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8422 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8423 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008424 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008425 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008426
8427 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008428 clock.m2 = (pll_dw0 & 0xff) << 22;
8429 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8430 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008431 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8432 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8433 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8434
Imre Deakdccbea32015-06-22 23:35:51 +03008435 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008436}
8437
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008438static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008439 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008440{
8441 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008442 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008443 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008444 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008445 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008446
Imre Deak17290502016-02-12 18:55:11 +02008447 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8448 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008449 return false;
8450
Daniel Vettere143a212013-07-04 12:01:15 +02008451 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008452 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008453
Imre Deak17290502016-02-12 18:55:11 +02008454 ret = false;
8455
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008456 tmp = I915_READ(PIPECONF(crtc->pipe));
8457 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008458 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008459
Wayne Boyer666a4532015-12-09 12:29:35 -08008460 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008461 switch (tmp & PIPECONF_BPC_MASK) {
8462 case PIPECONF_6BPC:
8463 pipe_config->pipe_bpp = 18;
8464 break;
8465 case PIPECONF_8BPC:
8466 pipe_config->pipe_bpp = 24;
8467 break;
8468 case PIPECONF_10BPC:
8469 pipe_config->pipe_bpp = 30;
8470 break;
8471 default:
8472 break;
8473 }
8474 }
8475
Wayne Boyer666a4532015-12-09 12:29:35 -08008476 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8477 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008478 pipe_config->limited_color_range = true;
8479
Ville Syrjälä282740f2013-09-04 18:30:03 +03008480 if (INTEL_INFO(dev)->gen < 4)
8481 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8482
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008483 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008484 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008485
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008486 i9xx_get_pfit_config(crtc, pipe_config);
8487
Daniel Vetter6c49f242013-06-06 12:45:25 +02008488 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008489 /* No way to read it out on pipes B and C */
8490 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8491 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8492 else
8493 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008494 pipe_config->pixel_multiplier =
8495 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8496 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008497 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008498 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8499 tmp = I915_READ(DPLL(crtc->pipe));
8500 pipe_config->pixel_multiplier =
8501 ((tmp & SDVO_MULTIPLIER_MASK)
8502 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8503 } else {
8504 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8505 * port and will be fixed up in the encoder->get_config
8506 * function. */
8507 pipe_config->pixel_multiplier = 1;
8508 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008509 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008510 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008511 /*
8512 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8513 * on 830. Filter it out here so that we don't
8514 * report errors due to that.
8515 */
8516 if (IS_I830(dev))
8517 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8518
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008519 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8520 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008521 } else {
8522 /* Mask out read-only status bits. */
8523 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8524 DPLL_PORTC_READY_MASK |
8525 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008526 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008527
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008528 if (IS_CHERRYVIEW(dev))
8529 chv_crtc_clock_get(crtc, pipe_config);
8530 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008531 vlv_crtc_clock_get(crtc, pipe_config);
8532 else
8533 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008534
Ville Syrjälä0f646142015-08-26 19:39:18 +03008535 /*
8536 * Normally the dotclock is filled in by the encoder .get_config()
8537 * but in case the pipe is enabled w/o any ports we need a sane
8538 * default.
8539 */
8540 pipe_config->base.adjusted_mode.crtc_clock =
8541 pipe_config->port_clock / pipe_config->pixel_multiplier;
8542
Imre Deak17290502016-02-12 18:55:11 +02008543 ret = true;
8544
8545out:
8546 intel_display_power_put(dev_priv, power_domain);
8547
8548 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008549}
8550
Paulo Zanonidde86e22012-12-01 12:04:25 -02008551static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008552{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008553 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008554 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008555 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008556 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008557 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008558 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008559 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008560 bool has_ck505 = false;
8561 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008562 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008563
8564 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008565 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008566 switch (encoder->type) {
8567 case INTEL_OUTPUT_LVDS:
8568 has_panel = true;
8569 has_lvds = true;
8570 break;
8571 case INTEL_OUTPUT_EDP:
8572 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008573 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008574 has_cpu_edp = true;
8575 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008576 default:
8577 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008578 }
8579 }
8580
Keith Packard99eb6a02011-09-26 14:29:12 -07008581 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008582 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008583 can_ssc = has_ck505;
8584 } else {
8585 has_ck505 = false;
8586 can_ssc = true;
8587 }
8588
Lyude1c1a24d2016-06-14 11:04:09 -04008589 /* Check if any DPLLs are using the SSC source */
8590 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8591 u32 temp = I915_READ(PCH_DPLL(i));
8592
8593 if (!(temp & DPLL_VCO_ENABLE))
8594 continue;
8595
8596 if ((temp & PLL_REF_INPUT_MASK) ==
8597 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8598 using_ssc_source = true;
8599 break;
8600 }
8601 }
8602
8603 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8604 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008605
8606 /* Ironlake: try to setup display ref clock before DPLL
8607 * enabling. This is only under driver's control after
8608 * PCH B stepping, previous chipset stepping should be
8609 * ignoring this setting.
8610 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008611 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008612
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008613 /* As we must carefully and slowly disable/enable each source in turn,
8614 * compute the final state we want first and check if we need to
8615 * make any changes at all.
8616 */
8617 final = val;
8618 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008619 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008620 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008621 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008622 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8623
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008624 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008625 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008626 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008627
Keith Packard199e5d72011-09-22 12:01:57 -07008628 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008629 final |= DREF_SSC_SOURCE_ENABLE;
8630
8631 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8632 final |= DREF_SSC1_ENABLE;
8633
8634 if (has_cpu_edp) {
8635 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8636 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8637 else
8638 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8639 } else
8640 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008641 } else if (using_ssc_source) {
8642 final |= DREF_SSC_SOURCE_ENABLE;
8643 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008644 }
8645
8646 if (final == val)
8647 return;
8648
8649 /* Always enable nonspread source */
8650 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8651
8652 if (has_ck505)
8653 val |= DREF_NONSPREAD_CK505_ENABLE;
8654 else
8655 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8656
8657 if (has_panel) {
8658 val &= ~DREF_SSC_SOURCE_MASK;
8659 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008660
Keith Packard199e5d72011-09-22 12:01:57 -07008661 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008662 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008663 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008664 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008665 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008666 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008667
8668 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008669 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008670 POSTING_READ(PCH_DREF_CONTROL);
8671 udelay(200);
8672
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008673 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008674
8675 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008676 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008677 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008678 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008679 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008680 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008681 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008682 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008683 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008684
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008685 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008686 POSTING_READ(PCH_DREF_CONTROL);
8687 udelay(200);
8688 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008689 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008690
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008691 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008692
8693 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008694 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008695
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008696 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008697 POSTING_READ(PCH_DREF_CONTROL);
8698 udelay(200);
8699
Lyude1c1a24d2016-06-14 11:04:09 -04008700 if (!using_ssc_source) {
8701 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008702
Lyude1c1a24d2016-06-14 11:04:09 -04008703 /* Turn off the SSC source */
8704 val &= ~DREF_SSC_SOURCE_MASK;
8705 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008706
Lyude1c1a24d2016-06-14 11:04:09 -04008707 /* Turn off SSC1 */
8708 val &= ~DREF_SSC1_ENABLE;
8709
8710 I915_WRITE(PCH_DREF_CONTROL, val);
8711 POSTING_READ(PCH_DREF_CONTROL);
8712 udelay(200);
8713 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008714 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008715
8716 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008717}
8718
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008719static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008720{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008721 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008722
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008723 tmp = I915_READ(SOUTH_CHICKEN2);
8724 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8725 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008726
Imre Deakcf3598c2016-06-28 13:37:31 +03008727 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8728 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008729 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008730
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008731 tmp = I915_READ(SOUTH_CHICKEN2);
8732 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8733 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008734
Imre Deakcf3598c2016-06-28 13:37:31 +03008735 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8736 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008737 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008738}
8739
8740/* WaMPhyProgramming:hsw */
8741static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8742{
8743 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008744
8745 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8746 tmp &= ~(0xFF << 24);
8747 tmp |= (0x12 << 24);
8748 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8749
Paulo Zanonidde86e22012-12-01 12:04:25 -02008750 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8751 tmp |= (1 << 11);
8752 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8753
8754 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8755 tmp |= (1 << 11);
8756 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8757
Paulo Zanonidde86e22012-12-01 12:04:25 -02008758 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8759 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8760 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8761
8762 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8763 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8764 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8765
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008766 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8767 tmp &= ~(7 << 13);
8768 tmp |= (5 << 13);
8769 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008770
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008771 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8772 tmp &= ~(7 << 13);
8773 tmp |= (5 << 13);
8774 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008775
8776 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8777 tmp &= ~0xFF;
8778 tmp |= 0x1C;
8779 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8780
8781 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8782 tmp &= ~0xFF;
8783 tmp |= 0x1C;
8784 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8785
8786 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8787 tmp &= ~(0xFF << 16);
8788 tmp |= (0x1C << 16);
8789 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8790
8791 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8792 tmp &= ~(0xFF << 16);
8793 tmp |= (0x1C << 16);
8794 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8795
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008796 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8797 tmp |= (1 << 27);
8798 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008799
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008800 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8801 tmp |= (1 << 27);
8802 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008803
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008804 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8805 tmp &= ~(0xF << 28);
8806 tmp |= (4 << 28);
8807 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008808
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008809 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8810 tmp &= ~(0xF << 28);
8811 tmp |= (4 << 28);
8812 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008813}
8814
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008815/* Implements 3 different sequences from BSpec chapter "Display iCLK
8816 * Programming" based on the parameters passed:
8817 * - Sequence to enable CLKOUT_DP
8818 * - Sequence to enable CLKOUT_DP without spread
8819 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8820 */
8821static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8822 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008823{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008824 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008825 uint32_t reg, tmp;
8826
8827 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8828 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008829 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008830 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008831
Ville Syrjäläa5805162015-05-26 20:42:30 +03008832 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008833
8834 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8835 tmp &= ~SBI_SSCCTL_DISABLE;
8836 tmp |= SBI_SSCCTL_PATHALT;
8837 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8838
8839 udelay(24);
8840
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008841 if (with_spread) {
8842 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8843 tmp &= ~SBI_SSCCTL_PATHALT;
8844 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008845
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008846 if (with_fdi) {
8847 lpt_reset_fdi_mphy(dev_priv);
8848 lpt_program_fdi_mphy(dev_priv);
8849 }
8850 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008851
Ville Syrjäläc2699522015-08-27 23:55:59 +03008852 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008853 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8854 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8855 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008856
Ville Syrjäläa5805162015-05-26 20:42:30 +03008857 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008858}
8859
Paulo Zanoni47701c32013-07-23 11:19:25 -03008860/* Sequence to disable CLKOUT_DP */
8861static void lpt_disable_clkout_dp(struct drm_device *dev)
8862{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008863 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008864 uint32_t reg, tmp;
8865
Ville Syrjäläa5805162015-05-26 20:42:30 +03008866 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008867
Ville Syrjäläc2699522015-08-27 23:55:59 +03008868 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008869 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8870 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8871 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8872
8873 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8874 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8875 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8876 tmp |= SBI_SSCCTL_PATHALT;
8877 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8878 udelay(32);
8879 }
8880 tmp |= SBI_SSCCTL_DISABLE;
8881 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8882 }
8883
Ville Syrjäläa5805162015-05-26 20:42:30 +03008884 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008885}
8886
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008887#define BEND_IDX(steps) ((50 + (steps)) / 5)
8888
8889static const uint16_t sscdivintphase[] = {
8890 [BEND_IDX( 50)] = 0x3B23,
8891 [BEND_IDX( 45)] = 0x3B23,
8892 [BEND_IDX( 40)] = 0x3C23,
8893 [BEND_IDX( 35)] = 0x3C23,
8894 [BEND_IDX( 30)] = 0x3D23,
8895 [BEND_IDX( 25)] = 0x3D23,
8896 [BEND_IDX( 20)] = 0x3E23,
8897 [BEND_IDX( 15)] = 0x3E23,
8898 [BEND_IDX( 10)] = 0x3F23,
8899 [BEND_IDX( 5)] = 0x3F23,
8900 [BEND_IDX( 0)] = 0x0025,
8901 [BEND_IDX( -5)] = 0x0025,
8902 [BEND_IDX(-10)] = 0x0125,
8903 [BEND_IDX(-15)] = 0x0125,
8904 [BEND_IDX(-20)] = 0x0225,
8905 [BEND_IDX(-25)] = 0x0225,
8906 [BEND_IDX(-30)] = 0x0325,
8907 [BEND_IDX(-35)] = 0x0325,
8908 [BEND_IDX(-40)] = 0x0425,
8909 [BEND_IDX(-45)] = 0x0425,
8910 [BEND_IDX(-50)] = 0x0525,
8911};
8912
8913/*
8914 * Bend CLKOUT_DP
8915 * steps -50 to 50 inclusive, in steps of 5
8916 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8917 * change in clock period = -(steps / 10) * 5.787 ps
8918 */
8919static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8920{
8921 uint32_t tmp;
8922 int idx = BEND_IDX(steps);
8923
8924 if (WARN_ON(steps % 5 != 0))
8925 return;
8926
8927 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8928 return;
8929
8930 mutex_lock(&dev_priv->sb_lock);
8931
8932 if (steps % 10 != 0)
8933 tmp = 0xAAAAAAAB;
8934 else
8935 tmp = 0x00000000;
8936 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8937
8938 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8939 tmp &= 0xffff0000;
8940 tmp |= sscdivintphase[idx];
8941 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8942
8943 mutex_unlock(&dev_priv->sb_lock);
8944}
8945
8946#undef BEND_IDX
8947
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008948static void lpt_init_pch_refclk(struct drm_device *dev)
8949{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008950 struct intel_encoder *encoder;
8951 bool has_vga = false;
8952
Damien Lespiaub2784e12014-08-05 11:29:37 +01008953 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008954 switch (encoder->type) {
8955 case INTEL_OUTPUT_ANALOG:
8956 has_vga = true;
8957 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008958 default:
8959 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008960 }
8961 }
8962
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008963 if (has_vga) {
8964 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008965 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008966 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008967 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008968 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008969}
8970
Paulo Zanonidde86e22012-12-01 12:04:25 -02008971/*
8972 * Initialize reference clocks when the driver loads
8973 */
8974void intel_init_pch_refclk(struct drm_device *dev)
8975{
8976 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8977 ironlake_init_pch_refclk(dev);
8978 else if (HAS_PCH_LPT(dev))
8979 lpt_init_pch_refclk(dev);
8980}
8981
Daniel Vetter6ff93602013-04-19 11:24:36 +02008982static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008983{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008984 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8986 int pipe = intel_crtc->pipe;
8987 uint32_t val;
8988
Daniel Vetter78114072013-06-13 00:54:57 +02008989 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008990
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008991 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008992 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008993 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008994 break;
8995 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008996 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008997 break;
8998 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008999 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009000 break;
9001 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009002 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009003 break;
9004 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03009005 /* Case prevented by intel_choose_pipe_bpp_dither. */
9006 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03009007 }
9008
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009009 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03009010 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9011
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009012 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03009013 val |= PIPECONF_INTERLACED_ILK;
9014 else
9015 val |= PIPECONF_PROGRESSIVE;
9016
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009017 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009018 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009019
Paulo Zanonic8203562012-09-12 10:06:29 -03009020 I915_WRITE(PIPECONF(pipe), val);
9021 POSTING_READ(PIPECONF(pipe));
9022}
9023
Daniel Vetter6ff93602013-04-19 11:24:36 +02009024static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009025{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009026 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009028 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02009029 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009030
Jani Nikula391bf042016-03-18 17:05:40 +02009031 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009032 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9033
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009034 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009035 val |= PIPECONF_INTERLACED_ILK;
9036 else
9037 val |= PIPECONF_PROGRESSIVE;
9038
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009039 I915_WRITE(PIPECONF(cpu_transcoder), val);
9040 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02009041}
9042
Jani Nikula391bf042016-03-18 17:05:40 +02009043static void haswell_set_pipemisc(struct drm_crtc *crtc)
9044{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009045 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02009046 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9047
9048 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9049 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009050
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009051 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009052 case 18:
9053 val |= PIPEMISC_DITHER_6_BPC;
9054 break;
9055 case 24:
9056 val |= PIPEMISC_DITHER_8_BPC;
9057 break;
9058 case 30:
9059 val |= PIPEMISC_DITHER_10_BPC;
9060 break;
9061 case 36:
9062 val |= PIPEMISC_DITHER_12_BPC;
9063 break;
9064 default:
9065 /* Case prevented by pipe_config_set_bpp. */
9066 BUG();
9067 }
9068
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009069 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009070 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9071
Jani Nikula391bf042016-03-18 17:05:40 +02009072 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009073 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009074}
9075
Paulo Zanonid4b19312012-11-29 11:29:32 -02009076int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9077{
9078 /*
9079 * Account for spread spectrum to avoid
9080 * oversubscribing the link. Max center spread
9081 * is 2.5%; use 5% for safety's sake.
9082 */
9083 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02009084 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02009085}
9086
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009087static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02009088{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009089 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03009090}
9091
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009092static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9093 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009094 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009095{
9096 struct drm_crtc *crtc = &intel_crtc->base;
9097 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009098 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009099 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009100 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08009101
Chris Wilsonc1858122010-12-03 21:35:48 +00009102 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009103 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009104 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07009105 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009106 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02009107 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009108 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009109 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009110 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009111
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009112 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00009113
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009114 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
9115 fp |= FP_CB_TUNE;
9116
9117 if (reduced_clock) {
9118 fp2 = i9xx_dpll_compute_fp(reduced_clock);
9119
9120 if (reduced_clock->m < factor * reduced_clock->n)
9121 fp2 |= FP_CB_TUNE;
9122 } else {
9123 fp2 = fp;
9124 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009125
Chris Wilson5eddb702010-09-11 13:48:45 +01009126 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009127
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009128 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07009129 dpll |= DPLLB_MODE_LVDS;
9130 else
9131 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009132
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009133 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009134 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009135
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009136 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9137 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009138 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009139
Ville Syrjälä37a56502016-06-22 21:57:04 +03009140 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009141 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009142
Eric Anholta07d6782011-03-30 13:01:08 -07009143 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009144 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009145 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009146 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009147
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009148 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009149 case 5:
9150 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9151 break;
9152 case 7:
9153 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9154 break;
9155 case 10:
9156 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9157 break;
9158 case 14:
9159 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9160 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009161 }
9162
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009163 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9164 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009165 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009166 else
9167 dpll |= PLL_REF_INPUT_DREFCLK;
9168
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009169 dpll |= DPLL_VCO_ENABLE;
9170
9171 crtc_state->dpll_hw_state.dpll = dpll;
9172 crtc_state->dpll_hw_state.fp0 = fp;
9173 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009174}
9175
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009176static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9177 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009178{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009179 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009180 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009181 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009182 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009183 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009184 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009185 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009186
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009187 memset(&crtc_state->dpll_hw_state, 0,
9188 sizeof(crtc_state->dpll_hw_state));
9189
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009190 crtc->lowfreq_avail = false;
9191
9192 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9193 if (!crtc_state->has_pch_encoder)
9194 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009195
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009196 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009197 if (intel_panel_use_ssc(dev_priv)) {
9198 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9199 dev_priv->vbt.lvds_ssc_freq);
9200 refclk = dev_priv->vbt.lvds_ssc_freq;
9201 }
9202
9203 if (intel_is_dual_link_lvds(dev)) {
9204 if (refclk == 100000)
9205 limit = &intel_limits_ironlake_dual_lvds_100m;
9206 else
9207 limit = &intel_limits_ironlake_dual_lvds;
9208 } else {
9209 if (refclk == 100000)
9210 limit = &intel_limits_ironlake_single_lvds_100m;
9211 else
9212 limit = &intel_limits_ironlake_single_lvds;
9213 }
9214 } else {
9215 limit = &intel_limits_ironlake_dac;
9216 }
9217
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009218 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009219 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9220 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009221 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9222 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009223 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009224
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009225 ironlake_compute_dpll(crtc, crtc_state,
9226 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009227
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009228 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9229 if (pll == NULL) {
9230 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9231 pipe_name(crtc->pipe));
9232 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009233 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009234
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009235 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009236 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009237 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009238
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009239 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009240}
9241
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009242static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9243 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009244{
9245 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009246 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009247 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009248
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009249 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9250 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9251 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9252 & ~TU_SIZE_MASK;
9253 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9254 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9255 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9256}
9257
9258static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9259 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009260 struct intel_link_m_n *m_n,
9261 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009262{
9263 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009264 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009265 enum pipe pipe = crtc->pipe;
9266
9267 if (INTEL_INFO(dev)->gen >= 5) {
9268 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9269 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9270 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9271 & ~TU_SIZE_MASK;
9272 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9273 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9274 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009275 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9276 * gen < 8) and if DRRS is supported (to make sure the
9277 * registers are not unnecessarily read).
9278 */
9279 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009280 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009281 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9282 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9283 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9284 & ~TU_SIZE_MASK;
9285 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9286 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9287 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9288 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009289 } else {
9290 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9291 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9292 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9293 & ~TU_SIZE_MASK;
9294 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9295 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9296 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9297 }
9298}
9299
9300void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009301 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009302{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009303 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009304 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9305 else
9306 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009307 &pipe_config->dp_m_n,
9308 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009309}
9310
Daniel Vetter72419202013-04-04 13:28:53 +02009311static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009312 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009313{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009314 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009315 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009316}
9317
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009318static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009319 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009320{
9321 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009322 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009323 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9324 uint32_t ps_ctrl = 0;
9325 int id = -1;
9326 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009327
Chandra Kondurua1b22782015-04-07 15:28:45 -07009328 /* find scaler attached to this pipe */
9329 for (i = 0; i < crtc->num_scalers; i++) {
9330 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9331 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9332 id = i;
9333 pipe_config->pch_pfit.enabled = true;
9334 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9335 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9336 break;
9337 }
9338 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009339
Chandra Kondurua1b22782015-04-07 15:28:45 -07009340 scaler_state->scaler_id = id;
9341 if (id >= 0) {
9342 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9343 } else {
9344 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009345 }
9346}
9347
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009348static void
9349skylake_get_initial_plane_config(struct intel_crtc *crtc,
9350 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009351{
9352 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009353 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009354 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009355 int pipe = crtc->pipe;
9356 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009357 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009358 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009359 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009360
Damien Lespiaud9806c92015-01-21 14:07:19 +00009361 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009362 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009363 DRM_DEBUG_KMS("failed to alloc fb\n");
9364 return;
9365 }
9366
Damien Lespiau1b842c82015-01-21 13:50:54 +00009367 fb = &intel_fb->base;
9368
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009369 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009370 if (!(val & PLANE_CTL_ENABLE))
9371 goto error;
9372
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009373 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9374 fourcc = skl_format_to_fourcc(pixel_format,
9375 val & PLANE_CTL_ORDER_RGBX,
9376 val & PLANE_CTL_ALPHA_MASK);
9377 fb->pixel_format = fourcc;
9378 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9379
Damien Lespiau40f46282015-02-27 11:15:21 +00009380 tiling = val & PLANE_CTL_TILED_MASK;
9381 switch (tiling) {
9382 case PLANE_CTL_TILED_LINEAR:
9383 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9384 break;
9385 case PLANE_CTL_TILED_X:
9386 plane_config->tiling = I915_TILING_X;
9387 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9388 break;
9389 case PLANE_CTL_TILED_Y:
9390 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9391 break;
9392 case PLANE_CTL_TILED_YF:
9393 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9394 break;
9395 default:
9396 MISSING_CASE(tiling);
9397 goto error;
9398 }
9399
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009400 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9401 plane_config->base = base;
9402
9403 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9404
9405 val = I915_READ(PLANE_SIZE(pipe, 0));
9406 fb->height = ((val >> 16) & 0xfff) + 1;
9407 fb->width = ((val >> 0) & 0x1fff) + 1;
9408
9409 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009410 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009411 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009412 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9413
9414 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009415 fb->pixel_format,
9416 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009417
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009418 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009419
9420 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9421 pipe_name(pipe), fb->width, fb->height,
9422 fb->bits_per_pixel, base, fb->pitches[0],
9423 plane_config->size);
9424
Damien Lespiau2d140302015-02-05 17:22:18 +00009425 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009426 return;
9427
9428error:
9429 kfree(fb);
9430}
9431
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009432static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009433 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009434{
9435 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009436 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009437 uint32_t tmp;
9438
9439 tmp = I915_READ(PF_CTL(crtc->pipe));
9440
9441 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009442 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009443 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9444 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009445
9446 /* We currently do not free assignements of panel fitters on
9447 * ivb/hsw (since we don't use the higher upscaling modes which
9448 * differentiates them) so just WARN about this case for now. */
9449 if (IS_GEN7(dev)) {
9450 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9451 PF_PIPE_SEL_IVB(crtc->pipe));
9452 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009453 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009454}
9455
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009456static void
9457ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9458 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009459{
9460 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009461 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009462 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009463 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009464 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009465 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009466 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009467 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009468
Damien Lespiau42a7b082015-02-05 19:35:13 +00009469 val = I915_READ(DSPCNTR(pipe));
9470 if (!(val & DISPLAY_PLANE_ENABLE))
9471 return;
9472
Damien Lespiaud9806c92015-01-21 14:07:19 +00009473 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009474 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009475 DRM_DEBUG_KMS("failed to alloc fb\n");
9476 return;
9477 }
9478
Damien Lespiau1b842c82015-01-21 13:50:54 +00009479 fb = &intel_fb->base;
9480
Daniel Vetter18c52472015-02-10 17:16:09 +00009481 if (INTEL_INFO(dev)->gen >= 4) {
9482 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009483 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009484 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9485 }
9486 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009487
9488 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009489 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009490 fb->pixel_format = fourcc;
9491 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009492
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009493 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009494 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009495 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009496 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009497 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009498 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009499 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009500 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009501 }
9502 plane_config->base = base;
9503
9504 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009505 fb->width = ((val >> 16) & 0xfff) + 1;
9506 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009507
9508 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009509 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009510
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009511 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009512 fb->pixel_format,
9513 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009514
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009515 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009516
Damien Lespiau2844a922015-01-20 12:51:48 +00009517 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9518 pipe_name(pipe), fb->width, fb->height,
9519 fb->bits_per_pixel, base, fb->pitches[0],
9520 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009521
Damien Lespiau2d140302015-02-05 17:22:18 +00009522 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009523}
9524
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009525static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009526 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009527{
9528 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009529 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009530 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009531 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009532 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009533
Imre Deak17290502016-02-12 18:55:11 +02009534 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9535 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009536 return false;
9537
Daniel Vettere143a212013-07-04 12:01:15 +02009538 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009539 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009540
Imre Deak17290502016-02-12 18:55:11 +02009541 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009542 tmp = I915_READ(PIPECONF(crtc->pipe));
9543 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009544 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009545
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009546 switch (tmp & PIPECONF_BPC_MASK) {
9547 case PIPECONF_6BPC:
9548 pipe_config->pipe_bpp = 18;
9549 break;
9550 case PIPECONF_8BPC:
9551 pipe_config->pipe_bpp = 24;
9552 break;
9553 case PIPECONF_10BPC:
9554 pipe_config->pipe_bpp = 30;
9555 break;
9556 case PIPECONF_12BPC:
9557 pipe_config->pipe_bpp = 36;
9558 break;
9559 default:
9560 break;
9561 }
9562
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009563 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9564 pipe_config->limited_color_range = true;
9565
Daniel Vetterab9412b2013-05-03 11:49:46 +02009566 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009567 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009568 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009569
Daniel Vetter88adfff2013-03-28 10:42:01 +01009570 pipe_config->has_pch_encoder = true;
9571
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009572 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9573 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9574 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009575
9576 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009577
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009578 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009579 /*
9580 * The pipe->pch transcoder and pch transcoder->pll
9581 * mapping is fixed.
9582 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009583 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009584 } else {
9585 tmp = I915_READ(PCH_DPLL_SEL);
9586 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009587 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009588 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009589 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009590 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009591
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009592 pipe_config->shared_dpll =
9593 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9594 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009595
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009596 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9597 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009598
9599 tmp = pipe_config->dpll_hw_state.dpll;
9600 pipe_config->pixel_multiplier =
9601 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9602 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009603
9604 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009605 } else {
9606 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009607 }
9608
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009609 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009610 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009611
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009612 ironlake_get_pfit_config(crtc, pipe_config);
9613
Imre Deak17290502016-02-12 18:55:11 +02009614 ret = true;
9615
9616out:
9617 intel_display_power_put(dev_priv, power_domain);
9618
9619 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009620}
9621
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009622static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9623{
Chris Wilson91c8a322016-07-05 10:40:23 +01009624 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009625 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009626
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009627 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009628 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009629 pipe_name(crtc->pipe));
9630
Rob Clarke2c719b2014-12-15 13:56:32 -05009631 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9632 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009633 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9634 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +03009635 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009636 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009637 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009638 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009639 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009640 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009641 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009642 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009643 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009644 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009645 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009646
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009647 /*
9648 * In theory we can still leave IRQs enabled, as long as only the HPD
9649 * interrupts remain enabled. We used to check for that, but since it's
9650 * gen-specific and since we only disable LCPLL after we fully disable
9651 * the interrupts, the check below should be enough.
9652 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009653 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009654}
9655
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009656static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9657{
Chris Wilson91c8a322016-07-05 10:40:23 +01009658 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009659
9660 if (IS_HASWELL(dev))
9661 return I915_READ(D_COMP_HSW);
9662 else
9663 return I915_READ(D_COMP_BDW);
9664}
9665
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009666static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9667{
Chris Wilson91c8a322016-07-05 10:40:23 +01009668 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009669
9670 if (IS_HASWELL(dev)) {
9671 mutex_lock(&dev_priv->rps.hw_lock);
9672 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9673 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009674 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009675 mutex_unlock(&dev_priv->rps.hw_lock);
9676 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009677 I915_WRITE(D_COMP_BDW, val);
9678 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009679 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009680}
9681
9682/*
9683 * This function implements pieces of two sequences from BSpec:
9684 * - Sequence for display software to disable LCPLL
9685 * - Sequence for display software to allow package C8+
9686 * The steps implemented here are just the steps that actually touch the LCPLL
9687 * register. Callers should take care of disabling all the display engine
9688 * functions, doing the mode unset, fixing interrupts, etc.
9689 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009690static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9691 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009692{
9693 uint32_t val;
9694
9695 assert_can_disable_lcpll(dev_priv);
9696
9697 val = I915_READ(LCPLL_CTL);
9698
9699 if (switch_to_fclk) {
9700 val |= LCPLL_CD_SOURCE_FCLK;
9701 I915_WRITE(LCPLL_CTL, val);
9702
Imre Deakf53dd632016-06-28 13:37:32 +03009703 if (wait_for_us(I915_READ(LCPLL_CTL) &
9704 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009705 DRM_ERROR("Switching to FCLK failed\n");
9706
9707 val = I915_READ(LCPLL_CTL);
9708 }
9709
9710 val |= LCPLL_PLL_DISABLE;
9711 I915_WRITE(LCPLL_CTL, val);
9712 POSTING_READ(LCPLL_CTL);
9713
Chris Wilson24d84412016-06-30 15:33:07 +01009714 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009715 DRM_ERROR("LCPLL still locked\n");
9716
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009717 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009718 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009719 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009720 ndelay(100);
9721
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009722 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9723 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009724 DRM_ERROR("D_COMP RCOMP still in progress\n");
9725
9726 if (allow_power_down) {
9727 val = I915_READ(LCPLL_CTL);
9728 val |= LCPLL_POWER_DOWN_ALLOW;
9729 I915_WRITE(LCPLL_CTL, val);
9730 POSTING_READ(LCPLL_CTL);
9731 }
9732}
9733
9734/*
9735 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9736 * source.
9737 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009738static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009739{
9740 uint32_t val;
9741
9742 val = I915_READ(LCPLL_CTL);
9743
9744 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9745 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9746 return;
9747
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009748 /*
9749 * Make sure we're not on PC8 state before disabling PC8, otherwise
9750 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009751 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009752 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009753
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009754 if (val & LCPLL_POWER_DOWN_ALLOW) {
9755 val &= ~LCPLL_POWER_DOWN_ALLOW;
9756 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009757 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009758 }
9759
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009760 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009761 val |= D_COMP_COMP_FORCE;
9762 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009763 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009764
9765 val = I915_READ(LCPLL_CTL);
9766 val &= ~LCPLL_PLL_DISABLE;
9767 I915_WRITE(LCPLL_CTL, val);
9768
Chris Wilson93220c02016-06-30 15:33:08 +01009769 if (intel_wait_for_register(dev_priv,
9770 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9771 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009772 DRM_ERROR("LCPLL not locked yet\n");
9773
9774 if (val & LCPLL_CD_SOURCE_FCLK) {
9775 val = I915_READ(LCPLL_CTL);
9776 val &= ~LCPLL_CD_SOURCE_FCLK;
9777 I915_WRITE(LCPLL_CTL, val);
9778
Imre Deakf53dd632016-06-28 13:37:32 +03009779 if (wait_for_us((I915_READ(LCPLL_CTL) &
9780 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009781 DRM_ERROR("Switching back to LCPLL failed\n");
9782 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009783
Mika Kuoppala59bad942015-01-16 11:34:40 +02009784 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +01009785 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009786}
9787
Paulo Zanoni765dab672014-03-07 20:08:18 -03009788/*
9789 * Package states C8 and deeper are really deep PC states that can only be
9790 * reached when all the devices on the system allow it, so even if the graphics
9791 * device allows PC8+, it doesn't mean the system will actually get to these
9792 * states. Our driver only allows PC8+ when going into runtime PM.
9793 *
9794 * The requirements for PC8+ are that all the outputs are disabled, the power
9795 * well is disabled and most interrupts are disabled, and these are also
9796 * requirements for runtime PM. When these conditions are met, we manually do
9797 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9798 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9799 * hang the machine.
9800 *
9801 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9802 * the state of some registers, so when we come back from PC8+ we need to
9803 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9804 * need to take care of the registers kept by RC6. Notice that this happens even
9805 * if we don't put the device in PCI D3 state (which is what currently happens
9806 * because of the runtime PM support).
9807 *
9808 * For more, read "Display Sequences for Package C8" on the hardware
9809 * documentation.
9810 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009811void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009812{
Chris Wilson91c8a322016-07-05 10:40:23 +01009813 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009814 uint32_t val;
9815
Paulo Zanonic67a4702013-08-19 13:18:09 -03009816 DRM_DEBUG_KMS("Enabling package C8+\n");
9817
Ville Syrjäläc2699522015-08-27 23:55:59 +03009818 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009819 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9820 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9821 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9822 }
9823
9824 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009825 hsw_disable_lcpll(dev_priv, true, true);
9826}
9827
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009828void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009829{
Chris Wilson91c8a322016-07-05 10:40:23 +01009830 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009831 uint32_t val;
9832
Paulo Zanonic67a4702013-08-19 13:18:09 -03009833 DRM_DEBUG_KMS("Disabling package C8+\n");
9834
9835 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009836 lpt_init_pch_refclk(dev);
9837
Ville Syrjäläc2699522015-08-27 23:55:59 +03009838 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009839 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9840 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9841 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9842 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009843}
9844
Imre Deak324513c2016-06-13 16:44:36 +03009845static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309846{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009847 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009848 struct intel_atomic_state *old_intel_state =
9849 to_intel_atomic_state(old_state);
9850 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309851
Imre Deak324513c2016-06-13 16:44:36 +03009852 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309853}
9854
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009855/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009856static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009857{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009858 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01009859 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009860 struct drm_crtc *crtc;
9861 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009862 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009863 unsigned max_pixel_rate = 0, i;
9864 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009865
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009866 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9867 sizeof(intel_state->min_pixclk));
9868
9869 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009870 int pixel_rate;
9871
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009872 crtc_state = to_intel_crtc_state(cstate);
9873 if (!crtc_state->base.enable) {
9874 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009875 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009876 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009877
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009878 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009879
9880 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009881 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009882 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9883
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009884 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009885 }
9886
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009887 for_each_pipe(dev_priv, pipe)
9888 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9889
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009890 return max_pixel_rate;
9891}
9892
9893static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9894{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009895 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009896 uint32_t val, data;
9897 int ret;
9898
9899 if (WARN((I915_READ(LCPLL_CTL) &
9900 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9901 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9902 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9903 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9904 "trying to change cdclk frequency with cdclk not enabled\n"))
9905 return;
9906
9907 mutex_lock(&dev_priv->rps.hw_lock);
9908 ret = sandybridge_pcode_write(dev_priv,
9909 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9910 mutex_unlock(&dev_priv->rps.hw_lock);
9911 if (ret) {
9912 DRM_ERROR("failed to inform pcode about cdclk change\n");
9913 return;
9914 }
9915
9916 val = I915_READ(LCPLL_CTL);
9917 val |= LCPLL_CD_SOURCE_FCLK;
9918 I915_WRITE(LCPLL_CTL, val);
9919
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009920 if (wait_for_us(I915_READ(LCPLL_CTL) &
9921 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009922 DRM_ERROR("Switching to FCLK failed\n");
9923
9924 val = I915_READ(LCPLL_CTL);
9925 val &= ~LCPLL_CLK_FREQ_MASK;
9926
9927 switch (cdclk) {
9928 case 450000:
9929 val |= LCPLL_CLK_FREQ_450;
9930 data = 0;
9931 break;
9932 case 540000:
9933 val |= LCPLL_CLK_FREQ_54O_BDW;
9934 data = 1;
9935 break;
9936 case 337500:
9937 val |= LCPLL_CLK_FREQ_337_5_BDW;
9938 data = 2;
9939 break;
9940 case 675000:
9941 val |= LCPLL_CLK_FREQ_675_BDW;
9942 data = 3;
9943 break;
9944 default:
9945 WARN(1, "invalid cdclk frequency\n");
9946 return;
9947 }
9948
9949 I915_WRITE(LCPLL_CTL, val);
9950
9951 val = I915_READ(LCPLL_CTL);
9952 val &= ~LCPLL_CD_SOURCE_FCLK;
9953 I915_WRITE(LCPLL_CTL, val);
9954
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009955 if (wait_for_us((I915_READ(LCPLL_CTL) &
9956 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009957 DRM_ERROR("Switching back to LCPLL failed\n");
9958
9959 mutex_lock(&dev_priv->rps.hw_lock);
9960 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9961 mutex_unlock(&dev_priv->rps.hw_lock);
9962
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009963 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9964
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009965 intel_update_cdclk(dev);
9966
9967 WARN(cdclk != dev_priv->cdclk_freq,
9968 "cdclk requested %d kHz but got %d kHz\n",
9969 cdclk, dev_priv->cdclk_freq);
9970}
9971
Ville Syrjälä587c7912016-05-11 22:44:41 +03009972static int broadwell_calc_cdclk(int max_pixclk)
9973{
9974 if (max_pixclk > 540000)
9975 return 675000;
9976 else if (max_pixclk > 450000)
9977 return 540000;
9978 else if (max_pixclk > 337500)
9979 return 450000;
9980 else
9981 return 337500;
9982}
9983
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009984static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009985{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009986 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009987 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009988 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009989 int cdclk;
9990
9991 /*
9992 * FIXME should also account for plane ratio
9993 * once 64bpp pixel formats are supported.
9994 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009995 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009996
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009997 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009998 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9999 cdclk, dev_priv->max_cdclk_freq);
10000 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010001 }
10002
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010003 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10004 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +030010005 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010006
10007 return 0;
10008}
10009
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010010static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010011{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010012 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010013 struct intel_atomic_state *old_intel_state =
10014 to_intel_atomic_state(old_state);
10015 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010016
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010017 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010018}
10019
Clint Taylorc89e39f2016-05-13 23:41:21 +030010020static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10021{
10022 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10023 struct drm_i915_private *dev_priv = to_i915(state->dev);
10024 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010025 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010026 int cdclk;
10027
10028 /*
10029 * FIXME should also account for plane ratio
10030 * once 64bpp pixel formats are supported.
10031 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010032 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010033
10034 /*
10035 * FIXME move the cdclk caclulation to
10036 * compute_config() so we can fail gracegully.
10037 */
10038 if (cdclk > dev_priv->max_cdclk_freq) {
10039 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10040 cdclk, dev_priv->max_cdclk_freq);
10041 cdclk = dev_priv->max_cdclk_freq;
10042 }
10043
10044 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10045 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010046 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010047
10048 return 0;
10049}
10050
10051static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10052{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010053 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10054 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10055 unsigned int req_cdclk = intel_state->dev_cdclk;
10056 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010057
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010058 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010059}
10060
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +020010061static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10062 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010063{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010064 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +020010065 if (!intel_ddi_pll_select(crtc, crtc_state))
10066 return -EINVAL;
10067 }
Daniel Vetter716c2e52014-06-25 22:02:02 +030010068
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +030010069 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +020010070
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +020010071 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010072}
10073
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010074static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10075 enum port port,
10076 struct intel_crtc_state *pipe_config)
10077{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010078 enum intel_dpll_id id;
10079
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010080 switch (port) {
10081 case PORT_A:
10082 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +020010083 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010084 break;
10085 case PORT_B:
10086 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +020010087 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010088 break;
10089 case PORT_C:
10090 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +020010091 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010092 break;
10093 default:
10094 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010095 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010096 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010097
10098 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010099}
10100
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010101static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10102 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010103 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010104{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010105 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +020010106 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010107
10108 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
10109 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
10110
10111 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +000010112 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +020010113 id = DPLL_ID_SKL_DPLL0;
10114 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010115 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010116 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010117 break;
10118 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010119 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010120 break;
10121 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010122 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010123 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010124 default:
10125 MISSING_CASE(pipe_config->ddi_pll_sel);
10126 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010127 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010128
10129 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010130}
10131
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010132static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10133 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010134 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010135{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010136 enum intel_dpll_id id;
10137
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010138 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
10139
10140 switch (pipe_config->ddi_pll_sel) {
10141 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010142 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010143 break;
10144 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010145 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010146 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010147 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010148 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010149 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010150 case PORT_CLK_SEL_LCPLL_810:
10151 id = DPLL_ID_LCPLL_810;
10152 break;
10153 case PORT_CLK_SEL_LCPLL_1350:
10154 id = DPLL_ID_LCPLL_1350;
10155 break;
10156 case PORT_CLK_SEL_LCPLL_2700:
10157 id = DPLL_ID_LCPLL_2700;
10158 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010159 default:
10160 MISSING_CASE(pipe_config->ddi_pll_sel);
10161 /* fall through */
10162 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010163 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010164 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010165
10166 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010167}
10168
Jani Nikulacf304292016-03-18 17:05:41 +020010169static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10170 struct intel_crtc_state *pipe_config,
10171 unsigned long *power_domain_mask)
10172{
10173 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010174 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +020010175 enum intel_display_power_domain power_domain;
10176 u32 tmp;
10177
Imre Deakd9a7bc62016-05-12 16:18:50 +030010178 /*
10179 * The pipe->transcoder mapping is fixed with the exception of the eDP
10180 * transcoder handled below.
10181 */
Jani Nikulacf304292016-03-18 17:05:41 +020010182 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10183
10184 /*
10185 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10186 * consistency and less surprising code; it's in always on power).
10187 */
10188 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10189 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10190 enum pipe trans_edp_pipe;
10191 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10192 default:
10193 WARN(1, "unknown pipe linked to edp transcoder\n");
10194 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10195 case TRANS_DDI_EDP_INPUT_A_ON:
10196 trans_edp_pipe = PIPE_A;
10197 break;
10198 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10199 trans_edp_pipe = PIPE_B;
10200 break;
10201 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10202 trans_edp_pipe = PIPE_C;
10203 break;
10204 }
10205
10206 if (trans_edp_pipe == crtc->pipe)
10207 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10208 }
10209
10210 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10211 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10212 return false;
10213 *power_domain_mask |= BIT(power_domain);
10214
10215 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10216
10217 return tmp & PIPECONF_ENABLE;
10218}
10219
Jani Nikula4d1de972016-03-18 17:05:42 +020010220static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10221 struct intel_crtc_state *pipe_config,
10222 unsigned long *power_domain_mask)
10223{
10224 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010225 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010226 enum intel_display_power_domain power_domain;
10227 enum port port;
10228 enum transcoder cpu_transcoder;
10229 u32 tmp;
10230
Jani Nikula4d1de972016-03-18 17:05:42 +020010231 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10232 if (port == PORT_A)
10233 cpu_transcoder = TRANSCODER_DSI_A;
10234 else
10235 cpu_transcoder = TRANSCODER_DSI_C;
10236
10237 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10238 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10239 continue;
10240 *power_domain_mask |= BIT(power_domain);
10241
Imre Deakdb18b6a2016-03-24 12:41:40 +020010242 /*
10243 * The PLL needs to be enabled with a valid divider
10244 * configuration, otherwise accessing DSI registers will hang
10245 * the machine. See BSpec North Display Engine
10246 * registers/MIPI[BXT]. We can break out here early, since we
10247 * need the same DSI PLL to be enabled for both DSI ports.
10248 */
10249 if (!intel_dsi_pll_is_enabled(dev_priv))
10250 break;
10251
Jani Nikula4d1de972016-03-18 17:05:42 +020010252 /* XXX: this works for video mode only */
10253 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10254 if (!(tmp & DPI_ENABLE))
10255 continue;
10256
10257 tmp = I915_READ(MIPI_CTRL(port));
10258 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10259 continue;
10260
10261 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010262 break;
10263 }
10264
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010265 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010266}
10267
Daniel Vetter26804af2014-06-25 22:01:55 +030010268static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010269 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010270{
10271 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010272 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010273 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010274 enum port port;
10275 uint32_t tmp;
10276
10277 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10278
10279 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10280
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010281 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010282 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010283 else if (IS_BROXTON(dev))
10284 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010285 else
10286 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010287
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010288 pll = pipe_config->shared_dpll;
10289 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010290 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10291 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010292 }
10293
Daniel Vetter26804af2014-06-25 22:01:55 +030010294 /*
10295 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10296 * DDI E. So just check whether this pipe is wired to DDI E and whether
10297 * the PCH transcoder is on.
10298 */
Damien Lespiauca370452013-12-03 13:56:24 +000010299 if (INTEL_INFO(dev)->gen < 9 &&
10300 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010301 pipe_config->has_pch_encoder = true;
10302
10303 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10304 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10305 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10306
10307 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10308 }
10309}
10310
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010311static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010312 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010313{
10314 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010315 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010316 enum intel_display_power_domain power_domain;
10317 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010318 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010319
Imre Deak17290502016-02-12 18:55:11 +020010320 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10321 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010322 return false;
Imre Deak17290502016-02-12 18:55:11 +020010323 power_domain_mask = BIT(power_domain);
10324
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010325 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010326
Jani Nikulacf304292016-03-18 17:05:41 +020010327 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010328
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010329 if (IS_BROXTON(dev_priv) &&
10330 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10331 WARN_ON(active);
10332 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010333 }
10334
Jani Nikulacf304292016-03-18 17:05:41 +020010335 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010336 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010337
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010338 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010339 haswell_get_ddi_port_state(crtc, pipe_config);
10340 intel_get_pipe_timings(crtc, pipe_config);
10341 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010342
Jani Nikulabc58be62016-03-18 17:05:39 +020010343 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010344
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010345 pipe_config->gamma_mode =
10346 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10347
Chandra Kondurua1b22782015-04-07 15:28:45 -070010348 if (INTEL_INFO(dev)->gen >= 9) {
10349 skl_init_scalers(dev, crtc, pipe_config);
10350 }
10351
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070010352 if (INTEL_INFO(dev)->gen >= 9) {
10353 pipe_config->scaler_state.scaler_id = -1;
10354 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10355 }
10356
Imre Deak17290502016-02-12 18:55:11 +020010357 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10358 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10359 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010360 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010361 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010362 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010363 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010364 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010365
Jesse Barnese59150d2014-01-07 13:30:45 -080010366 if (IS_HASWELL(dev))
10367 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10368 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010369
Jani Nikula4d1de972016-03-18 17:05:42 +020010370 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10371 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010372 pipe_config->pixel_multiplier =
10373 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10374 } else {
10375 pipe_config->pixel_multiplier = 1;
10376 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010377
Imre Deak17290502016-02-12 18:55:11 +020010378out:
10379 for_each_power_domain(power_domain, power_domain_mask)
10380 intel_display_power_put(dev_priv, power_domain);
10381
Jani Nikulacf304292016-03-18 17:05:41 +020010382 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010383}
10384
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010385static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10386 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010387{
10388 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010389 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010391 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010392
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010393 if (plane_state && plane_state->visible) {
10394 unsigned int width = plane_state->base.crtc_w;
10395 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010396 unsigned int stride = roundup_pow_of_two(width) * 4;
10397
10398 switch (stride) {
10399 default:
10400 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10401 width, stride);
10402 stride = 256;
10403 /* fallthrough */
10404 case 256:
10405 case 512:
10406 case 1024:
10407 case 2048:
10408 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010409 }
10410
Ville Syrjälädc41c152014-08-13 11:57:05 +030010411 cntl |= CURSOR_ENABLE |
10412 CURSOR_GAMMA_ENABLE |
10413 CURSOR_FORMAT_ARGB |
10414 CURSOR_STRIDE(stride);
10415
10416 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010417 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010418
Ville Syrjälädc41c152014-08-13 11:57:05 +030010419 if (intel_crtc->cursor_cntl != 0 &&
10420 (intel_crtc->cursor_base != base ||
10421 intel_crtc->cursor_size != size ||
10422 intel_crtc->cursor_cntl != cntl)) {
10423 /* On these chipsets we can only modify the base/size/stride
10424 * whilst the cursor is disabled.
10425 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010426 I915_WRITE(CURCNTR(PIPE_A), 0);
10427 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010428 intel_crtc->cursor_cntl = 0;
10429 }
10430
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010431 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010432 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010433 intel_crtc->cursor_base = base;
10434 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010435
10436 if (intel_crtc->cursor_size != size) {
10437 I915_WRITE(CURSIZE, size);
10438 intel_crtc->cursor_size = size;
10439 }
10440
Chris Wilson4b0e3332014-05-30 16:35:26 +030010441 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010442 I915_WRITE(CURCNTR(PIPE_A), cntl);
10443 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010444 intel_crtc->cursor_cntl = cntl;
10445 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010446}
10447
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010448static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10449 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010450{
10451 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010452 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10454 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010455 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010456
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010457 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010458 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010459 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010460 case 64:
10461 cntl |= CURSOR_MODE_64_ARGB_AX;
10462 break;
10463 case 128:
10464 cntl |= CURSOR_MODE_128_ARGB_AX;
10465 break;
10466 case 256:
10467 cntl |= CURSOR_MODE_256_ARGB_AX;
10468 break;
10469 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010470 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010471 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010472 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010473 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010474
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010475 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010476 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010477
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010478 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10479 cntl |= CURSOR_ROTATE_180;
10480 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010481
Chris Wilson4b0e3332014-05-30 16:35:26 +030010482 if (intel_crtc->cursor_cntl != cntl) {
10483 I915_WRITE(CURCNTR(pipe), cntl);
10484 POSTING_READ(CURCNTR(pipe));
10485 intel_crtc->cursor_cntl = cntl;
10486 }
10487
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010488 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010489 I915_WRITE(CURBASE(pipe), base);
10490 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010491
10492 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010493}
10494
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010495/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010496static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010497 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010498{
10499 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010500 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10502 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010503 u32 base = intel_crtc->cursor_addr;
10504 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010505
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010506 if (plane_state) {
10507 int x = plane_state->base.crtc_x;
10508 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010509
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010510 if (x < 0) {
10511 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10512 x = -x;
10513 }
10514 pos |= x << CURSOR_X_SHIFT;
10515
10516 if (y < 0) {
10517 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10518 y = -y;
10519 }
10520 pos |= y << CURSOR_Y_SHIFT;
10521
10522 /* ILK+ do this automagically */
10523 if (HAS_GMCH_DISPLAY(dev) &&
10524 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10525 base += (plane_state->base.crtc_h *
10526 plane_state->base.crtc_w - 1) * 4;
10527 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010528 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010529
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010530 I915_WRITE(CURPOS(pipe), pos);
10531
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010532 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010533 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010534 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010535 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010536}
10537
Ville Syrjälädc41c152014-08-13 11:57:05 +030010538static bool cursor_size_ok(struct drm_device *dev,
10539 uint32_t width, uint32_t height)
10540{
10541 if (width == 0 || height == 0)
10542 return false;
10543
10544 /*
10545 * 845g/865g are special in that they are only limited by
10546 * the width of their cursors, the height is arbitrary up to
10547 * the precision of the register. Everything else requires
10548 * square cursors, limited to a few power-of-two sizes.
10549 */
10550 if (IS_845G(dev) || IS_I865G(dev)) {
10551 if ((width & 63) != 0)
10552 return false;
10553
10554 if (width > (IS_845G(dev) ? 64 : 512))
10555 return false;
10556
10557 if (height > 1023)
10558 return false;
10559 } else {
10560 switch (width | height) {
10561 case 256:
10562 case 128:
10563 if (IS_GEN2(dev))
10564 return false;
10565 case 64:
10566 break;
10567 default:
10568 return false;
10569 }
10570 }
10571
10572 return true;
10573}
10574
Jesse Barnes79e53942008-11-07 14:24:08 -080010575/* VESA 640x480x72Hz mode to set on the pipe */
10576static struct drm_display_mode load_detect_mode = {
10577 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10578 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10579};
10580
Daniel Vettera8bb6812014-02-10 18:00:39 +010010581struct drm_framebuffer *
10582__intel_framebuffer_create(struct drm_device *dev,
10583 struct drm_mode_fb_cmd2 *mode_cmd,
10584 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010585{
10586 struct intel_framebuffer *intel_fb;
10587 int ret;
10588
10589 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010590 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010591 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010592
10593 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010594 if (ret)
10595 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010596
10597 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010598
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010599err:
10600 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010601 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010602}
10603
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010604static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010605intel_framebuffer_create(struct drm_device *dev,
10606 struct drm_mode_fb_cmd2 *mode_cmd,
10607 struct drm_i915_gem_object *obj)
10608{
10609 struct drm_framebuffer *fb;
10610 int ret;
10611
10612 ret = i915_mutex_lock_interruptible(dev);
10613 if (ret)
10614 return ERR_PTR(ret);
10615 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10616 mutex_unlock(&dev->struct_mutex);
10617
10618 return fb;
10619}
10620
Chris Wilsond2dff872011-04-19 08:36:26 +010010621static u32
10622intel_framebuffer_pitch_for_width(int width, int bpp)
10623{
10624 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10625 return ALIGN(pitch, 64);
10626}
10627
10628static u32
10629intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10630{
10631 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010632 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010633}
10634
10635static struct drm_framebuffer *
10636intel_framebuffer_create_for_mode(struct drm_device *dev,
10637 struct drm_display_mode *mode,
10638 int depth, int bpp)
10639{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010640 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010641 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010642 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010643
Dave Gordond37cd8a2016-04-22 19:14:32 +010010644 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010645 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010646 if (IS_ERR(obj))
10647 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010648
10649 mode_cmd.width = mode->hdisplay;
10650 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010651 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10652 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010653 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010654
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010655 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10656 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010010657 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010658
10659 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010660}
10661
10662static struct drm_framebuffer *
10663mode_fits_in_fbdev(struct drm_device *dev,
10664 struct drm_display_mode *mode)
10665{
Daniel Vetter06957262015-08-10 13:34:08 +020010666#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010010667 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010010668 struct drm_i915_gem_object *obj;
10669 struct drm_framebuffer *fb;
10670
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010671 if (!dev_priv->fbdev)
10672 return NULL;
10673
10674 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010675 return NULL;
10676
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010677 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010678 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010679
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010680 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010681 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10682 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010683 return NULL;
10684
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010685 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010686 return NULL;
10687
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010688 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010689 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010690#else
10691 return NULL;
10692#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010693}
10694
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010695static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10696 struct drm_crtc *crtc,
10697 struct drm_display_mode *mode,
10698 struct drm_framebuffer *fb,
10699 int x, int y)
10700{
10701 struct drm_plane_state *plane_state;
10702 int hdisplay, vdisplay;
10703 int ret;
10704
10705 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10706 if (IS_ERR(plane_state))
10707 return PTR_ERR(plane_state);
10708
10709 if (mode)
10710 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10711 else
10712 hdisplay = vdisplay = 0;
10713
10714 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10715 if (ret)
10716 return ret;
10717 drm_atomic_set_fb_for_plane(plane_state, fb);
10718 plane_state->crtc_x = 0;
10719 plane_state->crtc_y = 0;
10720 plane_state->crtc_w = hdisplay;
10721 plane_state->crtc_h = vdisplay;
10722 plane_state->src_x = x << 16;
10723 plane_state->src_y = y << 16;
10724 plane_state->src_w = hdisplay << 16;
10725 plane_state->src_h = vdisplay << 16;
10726
10727 return 0;
10728}
10729
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010730bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010731 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010732 struct intel_load_detect_pipe *old,
10733 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010734{
10735 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010736 struct intel_encoder *intel_encoder =
10737 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010738 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010739 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010740 struct drm_crtc *crtc = NULL;
10741 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010742 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010743 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010744 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010745 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010746 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010747 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010748
Chris Wilsond2dff872011-04-19 08:36:26 +010010749 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010750 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010751 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010752
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010753 old->restore_state = NULL;
10754
Rob Clark51fd3712013-11-19 12:10:12 -050010755retry:
10756 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10757 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010758 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010759
Jesse Barnes79e53942008-11-07 14:24:08 -080010760 /*
10761 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010762 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010763 * - if the connector already has an assigned crtc, use it (but make
10764 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010765 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010766 * - try to find the first unused crtc that can drive this connector,
10767 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010768 */
10769
10770 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010771 if (connector->state->crtc) {
10772 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010773
Rob Clark51fd3712013-11-19 12:10:12 -050010774 ret = drm_modeset_lock(&crtc->mutex, ctx);
10775 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010776 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010777
10778 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010779 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010780 }
10781
10782 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010783 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010784 i++;
10785 if (!(encoder->possible_crtcs & (1 << i)))
10786 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010787
10788 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10789 if (ret)
10790 goto fail;
10791
10792 if (possible_crtc->state->enable) {
10793 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010794 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010795 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010796
10797 crtc = possible_crtc;
10798 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010799 }
10800
10801 /*
10802 * If we didn't find an unused CRTC, don't use any.
10803 */
10804 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010805 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010806 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010807 }
10808
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010809found:
10810 intel_crtc = to_intel_crtc(crtc);
10811
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010812 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10813 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010814 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010815
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010816 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010817 restore_state = drm_atomic_state_alloc(dev);
10818 if (!state || !restore_state) {
10819 ret = -ENOMEM;
10820 goto fail;
10821 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010822
10823 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010824 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010825
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010826 connector_state = drm_atomic_get_connector_state(state, connector);
10827 if (IS_ERR(connector_state)) {
10828 ret = PTR_ERR(connector_state);
10829 goto fail;
10830 }
10831
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010832 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10833 if (ret)
10834 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010835
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010836 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10837 if (IS_ERR(crtc_state)) {
10838 ret = PTR_ERR(crtc_state);
10839 goto fail;
10840 }
10841
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010842 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010843
Chris Wilson64927112011-04-20 07:25:26 +010010844 if (!mode)
10845 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010846
Chris Wilsond2dff872011-04-19 08:36:26 +010010847 /* We need a framebuffer large enough to accommodate all accesses
10848 * that the plane may generate whilst we perform load detection.
10849 * We can not rely on the fbcon either being present (we get called
10850 * during its initialisation to detect all boot displays, or it may
10851 * not even exist) or that it is large enough to satisfy the
10852 * requested mode.
10853 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010854 fb = mode_fits_in_fbdev(dev, mode);
10855 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010856 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010857 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010858 } else
10859 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010860 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010861 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010862 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010863 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010864
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010865 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10866 if (ret)
10867 goto fail;
10868
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010869 drm_framebuffer_unreference(fb);
10870
10871 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10872 if (ret)
10873 goto fail;
10874
10875 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10876 if (!ret)
10877 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10878 if (!ret)
10879 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10880 if (ret) {
10881 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10882 goto fail;
10883 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010884
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010885 ret = drm_atomic_commit(state);
10886 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010887 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010888 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010889 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010890
10891 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010892
Jesse Barnes79e53942008-11-07 14:24:08 -080010893 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010894 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010895 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010896
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010897fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010898 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010899 drm_atomic_state_free(restore_state);
10900 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010901
Rob Clark51fd3712013-11-19 12:10:12 -050010902 if (ret == -EDEADLK) {
10903 drm_modeset_backoff(ctx);
10904 goto retry;
10905 }
10906
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010907 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010908}
10909
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010910void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010911 struct intel_load_detect_pipe *old,
10912 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010913{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010914 struct intel_encoder *intel_encoder =
10915 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010916 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010917 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010918 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010919
Chris Wilsond2dff872011-04-19 08:36:26 +010010920 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010921 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010922 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010923
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010924 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010925 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010926
10927 ret = drm_atomic_commit(state);
10928 if (ret) {
10929 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10930 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010931 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010932}
10933
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010934static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010935 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010936{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010937 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010938 u32 dpll = pipe_config->dpll_hw_state.dpll;
10939
10940 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010941 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010942 else if (HAS_PCH_SPLIT(dev))
10943 return 120000;
10944 else if (!IS_GEN2(dev))
10945 return 96000;
10946 else
10947 return 48000;
10948}
10949
Jesse Barnes79e53942008-11-07 14:24:08 -080010950/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010951static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010952 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010953{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010954 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010955 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010956 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010957 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010958 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010959 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010960 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010961 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010962
10963 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010964 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010965 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010966 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010967
10968 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010969 if (IS_PINEVIEW(dev)) {
10970 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10971 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010972 } else {
10973 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10974 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10975 }
10976
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010977 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010978 if (IS_PINEVIEW(dev))
10979 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10980 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010981 else
10982 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010983 DPLL_FPA01_P1_POST_DIV_SHIFT);
10984
10985 switch (dpll & DPLL_MODE_MASK) {
10986 case DPLLB_MODE_DAC_SERIAL:
10987 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10988 5 : 10;
10989 break;
10990 case DPLLB_MODE_LVDS:
10991 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10992 7 : 14;
10993 break;
10994 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010995 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010996 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010997 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010998 }
10999
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011000 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030011001 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011002 else
Imre Deakdccbea32015-06-22 23:35:51 +030011003 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011004 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020011005 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011006 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080011007
11008 if (is_lvds) {
11009 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11010 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011011
11012 if (lvds & LVDS_CLKB_POWER_UP)
11013 clock.p2 = 7;
11014 else
11015 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080011016 } else {
11017 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11018 clock.p1 = 2;
11019 else {
11020 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11021 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11022 }
11023 if (dpll & PLL_P2_DIVIDE_BY_4)
11024 clock.p2 = 4;
11025 else
11026 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080011027 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011028
Imre Deakdccbea32015-06-22 23:35:51 +030011029 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011030 }
11031
Ville Syrjälä18442d02013-09-13 16:00:08 +030011032 /*
11033 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010011034 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030011035 * encoder's get_config() function.
11036 */
Imre Deakdccbea32015-06-22 23:35:51 +030011037 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011038}
11039
Ville Syrjälä6878da02013-09-13 15:59:11 +030011040int intel_dotclock_calculate(int link_freq,
11041 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011042{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011043 /*
11044 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011045 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011046 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011047 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011048 *
11049 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011050 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080011051 */
11052
Ville Syrjälä6878da02013-09-13 15:59:11 +030011053 if (!m_n->link_n)
11054 return 0;
11055
11056 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11057}
11058
Ville Syrjälä18442d02013-09-13 16:00:08 +030011059static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011060 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030011061{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011062 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011063
11064 /* read out port_clock from the DPLL */
11065 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030011066
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011067 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011068 * In case there is an active pipe without active ports,
11069 * we may need some idea for the dotclock anyway.
11070 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011071 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011072 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011073 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011074 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080011075}
11076
11077/** Returns the currently programmed mode of the given pipe. */
11078struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11079 struct drm_crtc *crtc)
11080{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011081 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011083 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011084 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011085 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020011086 int htot = I915_READ(HTOTAL(cpu_transcoder));
11087 int hsync = I915_READ(HSYNC(cpu_transcoder));
11088 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11089 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030011090 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080011091
11092 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11093 if (!mode)
11094 return NULL;
11095
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011096 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11097 if (!pipe_config) {
11098 kfree(mode);
11099 return NULL;
11100 }
11101
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011102 /*
11103 * Construct a pipe_config sufficient for getting the clock info
11104 * back out of crtc_clock_get.
11105 *
11106 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11107 * to use a real value here instead.
11108 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011109 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11110 pipe_config->pixel_multiplier = 1;
11111 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11112 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11113 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11114 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011115
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011116 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080011117 mode->hdisplay = (htot & 0xffff) + 1;
11118 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11119 mode->hsync_start = (hsync & 0xffff) + 1;
11120 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11121 mode->vdisplay = (vtot & 0xffff) + 1;
11122 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11123 mode->vsync_start = (vsync & 0xffff) + 1;
11124 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11125
11126 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080011127
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011128 kfree(pipe_config);
11129
Jesse Barnes79e53942008-11-07 14:24:08 -080011130 return mode;
11131}
11132
11133static void intel_crtc_destroy(struct drm_crtc *crtc)
11134{
11135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011136 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011137 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011138
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011139 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011140 work = intel_crtc->flip_work;
11141 intel_crtc->flip_work = NULL;
11142 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011143
Daniel Vetter5a21b662016-05-24 17:13:53 +020011144 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011145 cancel_work_sync(&work->mmio_work);
11146 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011147 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011148 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011149
11150 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011151
Jesse Barnes79e53942008-11-07 14:24:08 -080011152 kfree(intel_crtc);
11153}
11154
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011155static void intel_unpin_work_fn(struct work_struct *__work)
11156{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011157 struct intel_flip_work *work =
11158 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011159 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11160 struct drm_device *dev = crtc->base.dev;
11161 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011162
Daniel Vetter5a21b662016-05-24 17:13:53 +020011163 if (is_mmio_work(work))
11164 flush_work(&work->mmio_work);
11165
11166 mutex_lock(&dev->struct_mutex);
11167 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010011168 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011169 mutex_unlock(&dev->struct_mutex);
11170
Chris Wilsone8a261e2016-07-20 13:31:49 +010011171 i915_gem_request_put(work->flip_queued_req);
11172
Chris Wilson5748b6a2016-08-04 16:32:38 +010011173 intel_frontbuffer_flip_complete(to_i915(dev),
11174 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011175 intel_fbc_post_update(crtc);
11176 drm_framebuffer_unreference(work->old_fb);
11177
11178 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11179 atomic_dec(&crtc->unpin_work_count);
11180
11181 kfree(work);
11182}
11183
11184/* Is 'a' after or equal to 'b'? */
11185static bool g4x_flip_count_after_eq(u32 a, u32 b)
11186{
11187 return !((a - b) & 0x80000000);
11188}
11189
11190static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11191 struct intel_flip_work *work)
11192{
11193 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011194 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011195 unsigned reset_counter;
11196
11197 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11198 if (crtc->reset_counter != reset_counter)
11199 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011200
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011201 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011202 * The relevant registers doen't exist on pre-ctg.
11203 * As the flip done interrupt doesn't trigger for mmio
11204 * flips on gmch platforms, a flip count check isn't
11205 * really needed there. But since ctg has the registers,
11206 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011207 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011208 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11209 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011210
Daniel Vetter5a21b662016-05-24 17:13:53 +020011211 /*
11212 * BDW signals flip done immediately if the plane
11213 * is disabled, even if the plane enable is already
11214 * armed to occur at the next vblank :(
11215 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011216
Daniel Vetter5a21b662016-05-24 17:13:53 +020011217 /*
11218 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11219 * used the same base address. In that case the mmio flip might
11220 * have completed, but the CS hasn't even executed the flip yet.
11221 *
11222 * A flip count check isn't enough as the CS might have updated
11223 * the base address just after start of vblank, but before we
11224 * managed to process the interrupt. This means we'd complete the
11225 * CS flip too soon.
11226 *
11227 * Combining both checks should get us a good enough result. It may
11228 * still happen that the CS flip has been executed, but has not
11229 * yet actually completed. But in case the base address is the same
11230 * anyway, we don't really care.
11231 */
11232 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11233 crtc->flip_work->gtt_offset &&
11234 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11235 crtc->flip_work->flip_count);
11236}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011237
Daniel Vetter5a21b662016-05-24 17:13:53 +020011238static bool
11239__pageflip_finished_mmio(struct intel_crtc *crtc,
11240 struct intel_flip_work *work)
11241{
11242 /*
11243 * MMIO work completes when vblank is different from
11244 * flip_queued_vblank.
11245 *
11246 * Reset counter value doesn't matter, this is handled by
11247 * i915_wait_request finishing early, so no need to handle
11248 * reset here.
11249 */
11250 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011251}
11252
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011253
11254static bool pageflip_finished(struct intel_crtc *crtc,
11255 struct intel_flip_work *work)
11256{
11257 if (!atomic_read(&work->pending))
11258 return false;
11259
11260 smp_rmb();
11261
Daniel Vetter5a21b662016-05-24 17:13:53 +020011262 if (is_mmio_work(work))
11263 return __pageflip_finished_mmio(crtc, work);
11264 else
11265 return __pageflip_finished_cs(crtc, work);
11266}
11267
11268void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11269{
Chris Wilson91c8a322016-07-05 10:40:23 +010011270 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011271 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11273 struct intel_flip_work *work;
11274 unsigned long flags;
11275
11276 /* Ignore early vblank irqs */
11277 if (!crtc)
11278 return;
11279
Daniel Vetterf3260382014-09-15 14:55:23 +020011280 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011281 * This is called both by irq handlers and the reset code (to complete
11282 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011283 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011284 spin_lock_irqsave(&dev->event_lock, flags);
11285 work = intel_crtc->flip_work;
11286
11287 if (work != NULL &&
11288 !is_mmio_work(work) &&
11289 pageflip_finished(intel_crtc, work))
11290 page_flip_completed(intel_crtc);
11291
11292 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011293}
11294
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011295void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011296{
Chris Wilson91c8a322016-07-05 10:40:23 +010011297 struct drm_device *dev = &dev_priv->drm;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011298 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11300 struct intel_flip_work *work;
11301 unsigned long flags;
11302
11303 /* Ignore early vblank irqs */
11304 if (!crtc)
11305 return;
11306
11307 /*
11308 * This is called both by irq handlers and the reset code (to complete
11309 * lost pageflips) so needs the full irqsave spinlocks.
11310 */
11311 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011312 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011313
Daniel Vetter5a21b662016-05-24 17:13:53 +020011314 if (work != NULL &&
11315 is_mmio_work(work) &&
11316 pageflip_finished(intel_crtc, work))
11317 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011318
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011319 spin_unlock_irqrestore(&dev->event_lock, flags);
11320}
11321
Daniel Vetter5a21b662016-05-24 17:13:53 +020011322static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11323 struct intel_flip_work *work)
11324{
11325 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11326
11327 /* Ensure that the work item is consistent when activating it ... */
11328 smp_mb__before_atomic();
11329 atomic_set(&work->pending, 1);
11330}
11331
11332static int intel_gen2_queue_flip(struct drm_device *dev,
11333 struct drm_crtc *crtc,
11334 struct drm_framebuffer *fb,
11335 struct drm_i915_gem_object *obj,
11336 struct drm_i915_gem_request *req,
11337 uint32_t flags)
11338{
Chris Wilson7e37f882016-08-02 22:50:21 +010011339 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11341 u32 flip_mask;
11342 int ret;
11343
11344 ret = intel_ring_begin(req, 6);
11345 if (ret)
11346 return ret;
11347
11348 /* Can't queue multiple flips, so wait for the previous
11349 * one to finish before executing the next.
11350 */
11351 if (intel_crtc->plane)
11352 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11353 else
11354 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011355 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11356 intel_ring_emit(ring, MI_NOOP);
11357 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011358 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011359 intel_ring_emit(ring, fb->pitches[0]);
11360 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11361 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011362
11363 return 0;
11364}
11365
11366static int intel_gen3_queue_flip(struct drm_device *dev,
11367 struct drm_crtc *crtc,
11368 struct drm_framebuffer *fb,
11369 struct drm_i915_gem_object *obj,
11370 struct drm_i915_gem_request *req,
11371 uint32_t flags)
11372{
Chris Wilson7e37f882016-08-02 22:50:21 +010011373 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11375 u32 flip_mask;
11376 int ret;
11377
11378 ret = intel_ring_begin(req, 6);
11379 if (ret)
11380 return ret;
11381
11382 if (intel_crtc->plane)
11383 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11384 else
11385 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011386 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11387 intel_ring_emit(ring, MI_NOOP);
11388 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011389 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011390 intel_ring_emit(ring, fb->pitches[0]);
11391 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11392 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011393
11394 return 0;
11395}
11396
11397static int intel_gen4_queue_flip(struct drm_device *dev,
11398 struct drm_crtc *crtc,
11399 struct drm_framebuffer *fb,
11400 struct drm_i915_gem_object *obj,
11401 struct drm_i915_gem_request *req,
11402 uint32_t flags)
11403{
Chris Wilson7e37f882016-08-02 22:50:21 +010011404 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011405 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11407 uint32_t pf, pipesrc;
11408 int ret;
11409
11410 ret = intel_ring_begin(req, 4);
11411 if (ret)
11412 return ret;
11413
11414 /* i965+ uses the linear or tiled offsets from the
11415 * Display Registers (which do not change across a page-flip)
11416 * so we need only reprogram the base address.
11417 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011418 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011419 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011420 intel_ring_emit(ring, fb->pitches[0]);
11421 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Chris Wilson3e510a82016-08-05 10:14:23 +010011422 i915_gem_object_get_tiling(obj));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011423
11424 /* XXX Enabling the panel-fitter across page-flip is so far
11425 * untested on non-native modes, so ignore it for now.
11426 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11427 */
11428 pf = 0;
11429 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011430 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011431
11432 return 0;
11433}
11434
11435static int intel_gen6_queue_flip(struct drm_device *dev,
11436 struct drm_crtc *crtc,
11437 struct drm_framebuffer *fb,
11438 struct drm_i915_gem_object *obj,
11439 struct drm_i915_gem_request *req,
11440 uint32_t flags)
11441{
Chris Wilson7e37f882016-08-02 22:50:21 +010011442 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011443 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11445 uint32_t pf, pipesrc;
11446 int ret;
11447
11448 ret = intel_ring_begin(req, 4);
11449 if (ret)
11450 return ret;
11451
Chris Wilsonb5321f32016-08-02 22:50:18 +010011452 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011453 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilson3e510a82016-08-05 10:14:23 +010011454 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011455 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011456
11457 /* Contrary to the suggestions in the documentation,
11458 * "Enable Panel Fitter" does not seem to be required when page
11459 * flipping with a non-native mode, and worse causes a normal
11460 * modeset to fail.
11461 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11462 */
11463 pf = 0;
11464 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011465 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011466
11467 return 0;
11468}
11469
11470static int intel_gen7_queue_flip(struct drm_device *dev,
11471 struct drm_crtc *crtc,
11472 struct drm_framebuffer *fb,
11473 struct drm_i915_gem_object *obj,
11474 struct drm_i915_gem_request *req,
11475 uint32_t flags)
11476{
Chris Wilson7e37f882016-08-02 22:50:21 +010011477 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11479 uint32_t plane_bit = 0;
11480 int len, ret;
11481
11482 switch (intel_crtc->plane) {
11483 case PLANE_A:
11484 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11485 break;
11486 case PLANE_B:
11487 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11488 break;
11489 case PLANE_C:
11490 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11491 break;
11492 default:
11493 WARN_ONCE(1, "unknown plane in flip command\n");
11494 return -ENODEV;
11495 }
11496
11497 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011498 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011499 len += 6;
11500 /*
11501 * On Gen 8, SRM is now taking an extra dword to accommodate
11502 * 48bits addresses, and we need a NOOP for the batch size to
11503 * stay even.
11504 */
11505 if (IS_GEN8(dev))
11506 len += 2;
11507 }
11508
11509 /*
11510 * BSpec MI_DISPLAY_FLIP for IVB:
11511 * "The full packet must be contained within the same cache line."
11512 *
11513 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11514 * cacheline, if we ever start emitting more commands before
11515 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11516 * then do the cacheline alignment, and finally emit the
11517 * MI_DISPLAY_FLIP.
11518 */
11519 ret = intel_ring_cacheline_align(req);
11520 if (ret)
11521 return ret;
11522
11523 ret = intel_ring_begin(req, len);
11524 if (ret)
11525 return ret;
11526
11527 /* Unmask the flip-done completion message. Note that the bspec says that
11528 * we should do this for both the BCS and RCS, and that we must not unmask
11529 * more than one flip event at any time (or ensure that one flip message
11530 * can be sent by waiting for flip-done prior to queueing new flips).
11531 * Experimentation says that BCS works despite DERRMR masking all
11532 * flip-done completion events and that unmasking all planes at once
11533 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11534 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11535 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011536 if (req->engine->id == RCS) {
11537 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11538 intel_ring_emit_reg(ring, DERRMR);
11539 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011540 DERRMR_PIPEB_PRI_FLIP_DONE |
11541 DERRMR_PIPEC_PRI_FLIP_DONE));
11542 if (IS_GEN8(dev))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011543 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011544 MI_SRM_LRM_GLOBAL_GTT);
11545 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011546 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011547 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011548 intel_ring_emit_reg(ring, DERRMR);
11549 intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011550 if (IS_GEN8(dev)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011551 intel_ring_emit(ring, 0);
11552 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011553 }
11554 }
11555
Chris Wilsonb5321f32016-08-02 22:50:18 +010011556 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Chris Wilson3e510a82016-08-05 10:14:23 +010011557 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011558 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11559 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011560
11561 return 0;
11562}
11563
11564static bool use_mmio_flip(struct intel_engine_cs *engine,
11565 struct drm_i915_gem_object *obj)
11566{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011567 struct reservation_object *resv;
11568
Daniel Vetter5a21b662016-05-24 17:13:53 +020011569 /*
11570 * This is not being used for older platforms, because
11571 * non-availability of flip done interrupt forces us to use
11572 * CS flips. Older platforms derive flip done using some clever
11573 * tricks involving the flip_pending status bits and vblank irqs.
11574 * So using MMIO flips there would disrupt this mechanism.
11575 */
11576
11577 if (engine == NULL)
11578 return true;
11579
11580 if (INTEL_GEN(engine->i915) < 5)
11581 return false;
11582
11583 if (i915.use_mmio_flip < 0)
11584 return false;
11585 else if (i915.use_mmio_flip > 0)
11586 return true;
11587 else if (i915.enable_execlists)
11588 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011589
11590 resv = i915_gem_object_get_dmabuf_resv(obj);
11591 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011592 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011593
Chris Wilsond72d9082016-08-04 07:52:31 +010011594 return engine != i915_gem_active_get_engine(&obj->last_write,
11595 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011596}
11597
11598static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11599 unsigned int rotation,
11600 struct intel_flip_work *work)
11601{
11602 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011603 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011604 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11605 const enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä6687c902015-09-15 13:16:41 +030011606 u32 ctl, stride;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011607
11608 ctl = I915_READ(PLANE_CTL(pipe, 0));
11609 ctl &= ~PLANE_CTL_TILED_MASK;
11610 switch (fb->modifier[0]) {
11611 case DRM_FORMAT_MOD_NONE:
11612 break;
11613 case I915_FORMAT_MOD_X_TILED:
11614 ctl |= PLANE_CTL_TILED_X;
11615 break;
11616 case I915_FORMAT_MOD_Y_TILED:
11617 ctl |= PLANE_CTL_TILED_Y;
11618 break;
11619 case I915_FORMAT_MOD_Yf_TILED:
11620 ctl |= PLANE_CTL_TILED_YF;
11621 break;
11622 default:
11623 MISSING_CASE(fb->modifier[0]);
11624 }
11625
11626 /*
11627 * The stride is either expressed as a multiple of 64 bytes chunks for
11628 * linear buffers or in number of tiles for tiled buffers.
11629 */
11630 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +030011631 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
11632 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11633
11634 stride = intel_fb->rotated[0].pitch /
11635 intel_tile_height(dev_priv, fb->modifier[0], cpp);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011636 } else {
11637 stride = fb->pitches[0] /
11638 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11639 fb->pixel_format);
11640 }
11641
11642 /*
11643 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11644 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11645 */
11646 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11647 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11648
11649 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11650 POSTING_READ(PLANE_SURF(pipe, 0));
11651}
11652
11653static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11654 struct intel_flip_work *work)
11655{
11656 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011657 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011658 struct intel_framebuffer *intel_fb =
11659 to_intel_framebuffer(intel_crtc->base.primary->fb);
11660 struct drm_i915_gem_object *obj = intel_fb->obj;
11661 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11662 u32 dspcntr;
11663
11664 dspcntr = I915_READ(reg);
11665
Chris Wilson3e510a82016-08-05 10:14:23 +010011666 if (i915_gem_object_is_tiled(obj))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011667 dspcntr |= DISPPLANE_TILED;
11668 else
11669 dspcntr &= ~DISPPLANE_TILED;
11670
11671 I915_WRITE(reg, dspcntr);
11672
11673 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11674 POSTING_READ(DSPSURF(intel_crtc->plane));
11675}
11676
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011677static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011678{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011679 struct intel_flip_work *work =
11680 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011681 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11682 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11683 struct intel_framebuffer *intel_fb =
11684 to_intel_framebuffer(crtc->base.primary->fb);
11685 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011686 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011687
11688 if (work->flip_queued_req)
Chris Wilson776f3232016-08-04 07:52:40 +010011689 WARN_ON(i915_wait_request(work->flip_queued_req,
11690 false, NULL,
11691 NO_WAITBOOST));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011692
11693 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011694 resv = i915_gem_object_get_dmabuf_resv(obj);
11695 if (resv)
11696 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011697 MAX_SCHEDULE_TIMEOUT) < 0);
11698
11699 intel_pipe_update_start(crtc);
11700
11701 if (INTEL_GEN(dev_priv) >= 9)
11702 skl_do_mmio_flip(crtc, work->rotation, work);
11703 else
11704 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11705 ilk_do_mmio_flip(crtc, work);
11706
11707 intel_pipe_update_end(crtc, work);
11708}
11709
11710static int intel_default_queue_flip(struct drm_device *dev,
11711 struct drm_crtc *crtc,
11712 struct drm_framebuffer *fb,
11713 struct drm_i915_gem_object *obj,
11714 struct drm_i915_gem_request *req,
11715 uint32_t flags)
11716{
11717 return -ENODEV;
11718}
11719
11720static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11721 struct intel_crtc *intel_crtc,
11722 struct intel_flip_work *work)
11723{
11724 u32 addr, vblank;
11725
11726 if (!atomic_read(&work->pending))
11727 return false;
11728
11729 smp_rmb();
11730
11731 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11732 if (work->flip_ready_vblank == 0) {
11733 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010011734 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011735 return false;
11736
11737 work->flip_ready_vblank = vblank;
11738 }
11739
11740 if (vblank - work->flip_ready_vblank < 3)
11741 return false;
11742
11743 /* Potential stall - if we see that the flip has happened,
11744 * assume a missed interrupt. */
11745 if (INTEL_GEN(dev_priv) >= 4)
11746 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11747 else
11748 addr = I915_READ(DSPADDR(intel_crtc->plane));
11749
11750 /* There is a potential issue here with a false positive after a flip
11751 * to the same address. We could address this by checking for a
11752 * non-incrementing frame counter.
11753 */
11754 return addr == work->gtt_offset;
11755}
11756
11757void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11758{
Chris Wilson91c8a322016-07-05 10:40:23 +010011759 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011760 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011762 struct intel_flip_work *work;
11763
11764 WARN_ON(!in_interrupt());
11765
11766 if (crtc == NULL)
11767 return;
11768
11769 spin_lock(&dev->event_lock);
11770 work = intel_crtc->flip_work;
11771
11772 if (work != NULL && !is_mmio_work(work) &&
11773 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11774 WARN_ONCE(1,
11775 "Kicking stuck page flip: queued at %d, now %d\n",
11776 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11777 page_flip_completed(intel_crtc);
11778 work = NULL;
11779 }
11780
11781 if (work != NULL && !is_mmio_work(work) &&
11782 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11783 intel_queue_rps_boost_for_request(work->flip_queued_req);
11784 spin_unlock(&dev->event_lock);
11785}
11786
11787static int intel_crtc_page_flip(struct drm_crtc *crtc,
11788 struct drm_framebuffer *fb,
11789 struct drm_pending_vblank_event *event,
11790 uint32_t page_flip_flags)
11791{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011792 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011793 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011794 struct drm_framebuffer *old_fb = crtc->primary->fb;
11795 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11797 struct drm_plane *primary = crtc->primary;
11798 enum pipe pipe = intel_crtc->pipe;
11799 struct intel_flip_work *work;
11800 struct intel_engine_cs *engine;
11801 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010011802 struct drm_i915_gem_request *request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011803 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011804
Daniel Vetter5a21b662016-05-24 17:13:53 +020011805 /*
11806 * drm_mode_page_flip_ioctl() should already catch this, but double
11807 * check to be safe. In the future we may enable pageflipping from
11808 * a disabled primary plane.
11809 */
11810 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11811 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011812
Daniel Vetter5a21b662016-05-24 17:13:53 +020011813 /* Can't change pixel format via MI display flips. */
11814 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11815 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011816
Daniel Vetter5a21b662016-05-24 17:13:53 +020011817 /*
11818 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11819 * Note that pitch changes could also affect these register.
11820 */
11821 if (INTEL_INFO(dev)->gen > 3 &&
11822 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11823 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11824 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011825
Daniel Vetter5a21b662016-05-24 17:13:53 +020011826 if (i915_terminally_wedged(&dev_priv->gpu_error))
11827 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011828
Daniel Vetter5a21b662016-05-24 17:13:53 +020011829 work = kzalloc(sizeof(*work), GFP_KERNEL);
11830 if (work == NULL)
11831 return -ENOMEM;
11832
11833 work->event = event;
11834 work->crtc = crtc;
11835 work->old_fb = old_fb;
11836 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011837
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011838 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011839 if (ret)
11840 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011841
Daniel Vetter5a21b662016-05-24 17:13:53 +020011842 /* We borrow the event spin lock for protecting flip_work */
11843 spin_lock_irq(&dev->event_lock);
11844 if (intel_crtc->flip_work) {
11845 /* Before declaring the flip queue wedged, check if
11846 * the hardware completed the operation behind our backs.
11847 */
11848 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11849 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11850 page_flip_completed(intel_crtc);
11851 } else {
11852 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11853 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011854
Daniel Vetter5a21b662016-05-24 17:13:53 +020011855 drm_crtc_vblank_put(crtc);
11856 kfree(work);
11857 return -EBUSY;
11858 }
11859 }
11860 intel_crtc->flip_work = work;
11861 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011862
Daniel Vetter5a21b662016-05-24 17:13:53 +020011863 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11864 flush_workqueue(dev_priv->wq);
11865
11866 /* Reference the objects for the scheduled work. */
11867 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011868
11869 crtc->primary->fb = fb;
11870 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011871
11872 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11873 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011874
Chris Wilson25dc5562016-07-20 13:31:52 +010011875 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011876
11877 ret = i915_mutex_lock_interruptible(dev);
11878 if (ret)
11879 goto cleanup;
11880
11881 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11882 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11883 ret = -EIO;
11884 goto cleanup;
11885 }
11886
11887 atomic_inc(&intel_crtc->unpin_work_count);
11888
11889 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11890 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11891
11892 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11893 engine = &dev_priv->engine[BCS];
Chris Wilson3e510a82016-08-05 10:14:23 +010011894 if (i915_gem_object_get_tiling(obj) !=
11895 i915_gem_object_get_tiling(intel_fb_obj(work->old_fb)))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011896 /* vlv: DISPLAY_FLIP fails to change tiling */
11897 engine = NULL;
11898 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11899 engine = &dev_priv->engine[BCS];
11900 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsond72d9082016-08-04 07:52:31 +010011901 engine = i915_gem_active_get_engine(&obj->last_write,
11902 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011903 if (engine == NULL || engine->id != RCS)
11904 engine = &dev_priv->engine[BCS];
11905 } else {
11906 engine = &dev_priv->engine[RCS];
11907 }
11908
11909 mmio_flip = use_mmio_flip(engine, obj);
11910
Daniel Vetter5a21b662016-05-24 17:13:53 +020011911 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11912 if (ret)
11913 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011914
Ville Syrjälä6687c902015-09-15 13:16:41 +030011915 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011916 work->gtt_offset += intel_crtc->dspaddr_offset;
11917 work->rotation = crtc->primary->state->rotation;
11918
11919 if (mmio_flip) {
11920 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11921
Chris Wilsond72d9082016-08-04 07:52:31 +010011922 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
11923 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011924 schedule_work(&work->mmio_work);
11925 } else {
Chris Wilson8e637172016-08-02 22:50:26 +010011926 request = i915_gem_request_alloc(engine, engine->last_context);
11927 if (IS_ERR(request)) {
11928 ret = PTR_ERR(request);
11929 goto cleanup_unpin;
11930 }
11931
11932 ret = i915_gem_object_sync(obj, request);
11933 if (ret)
11934 goto cleanup_request;
11935
Daniel Vetter5a21b662016-05-24 17:13:53 +020011936 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11937 page_flip_flags);
11938 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010011939 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011940
11941 intel_mark_page_flip_active(intel_crtc, work);
11942
Chris Wilson8e637172016-08-02 22:50:26 +010011943 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011944 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011945 }
11946
Daniel Vetter5a21b662016-05-24 17:13:53 +020011947 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11948 to_intel_plane(primary)->frontbuffer_bit);
11949 mutex_unlock(&dev->struct_mutex);
11950
Chris Wilson5748b6a2016-08-04 16:32:38 +010011951 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020011952 to_intel_plane(primary)->frontbuffer_bit);
11953
11954 trace_i915_flip_request(intel_crtc->plane, obj);
11955
11956 return 0;
11957
Chris Wilson8e637172016-08-02 22:50:26 +010011958cleanup_request:
11959 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011960cleanup_unpin:
11961 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11962cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020011963 atomic_dec(&intel_crtc->unpin_work_count);
11964 mutex_unlock(&dev->struct_mutex);
11965cleanup:
11966 crtc->primary->fb = old_fb;
11967 update_state_fb(crtc->primary);
11968
Chris Wilson34911fd2016-07-20 13:31:54 +010011969 i915_gem_object_put_unlocked(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011970 drm_framebuffer_unreference(work->old_fb);
11971
11972 spin_lock_irq(&dev->event_lock);
11973 intel_crtc->flip_work = NULL;
11974 spin_unlock_irq(&dev->event_lock);
11975
11976 drm_crtc_vblank_put(crtc);
11977free_work:
11978 kfree(work);
11979
11980 if (ret == -EIO) {
11981 struct drm_atomic_state *state;
11982 struct drm_plane_state *plane_state;
11983
11984out_hang:
11985 state = drm_atomic_state_alloc(dev);
11986 if (!state)
11987 return -ENOMEM;
11988 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11989
11990retry:
11991 plane_state = drm_atomic_get_plane_state(state, primary);
11992 ret = PTR_ERR_OR_ZERO(plane_state);
11993 if (!ret) {
11994 drm_atomic_set_fb_for_plane(plane_state, fb);
11995
11996 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11997 if (!ret)
11998 ret = drm_atomic_commit(state);
11999 }
12000
12001 if (ret == -EDEADLK) {
12002 drm_modeset_backoff(state->acquire_ctx);
12003 drm_atomic_state_clear(state);
12004 goto retry;
12005 }
12006
12007 if (ret)
12008 drm_atomic_state_free(state);
12009
12010 if (ret == 0 && event) {
12011 spin_lock_irq(&dev->event_lock);
12012 drm_crtc_send_vblank_event(crtc, event);
12013 spin_unlock_irq(&dev->event_lock);
12014 }
12015 }
12016 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012017}
12018
Daniel Vetter5a21b662016-05-24 17:13:53 +020012019
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012020/**
12021 * intel_wm_need_update - Check whether watermarks need updating
12022 * @plane: drm plane
12023 * @state: new plane state
12024 *
12025 * Check current plane state versus the new one to determine whether
12026 * watermarks need to be recalculated.
12027 *
12028 * Returns true or false.
12029 */
12030static bool intel_wm_need_update(struct drm_plane *plane,
12031 struct drm_plane_state *state)
12032{
Matt Roperd21fbe82015-09-24 15:53:12 -070012033 struct intel_plane_state *new = to_intel_plane_state(state);
12034 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12035
12036 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012037 if (new->visible != cur->visible)
12038 return true;
12039
12040 if (!cur->base.fb || !new->base.fb)
12041 return false;
12042
12043 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12044 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070012045 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
12046 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
12047 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
12048 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012049 return true;
12050
12051 return false;
12052}
12053
Matt Roperd21fbe82015-09-24 15:53:12 -070012054static bool needs_scaling(struct intel_plane_state *state)
12055{
12056 int src_w = drm_rect_width(&state->src) >> 16;
12057 int src_h = drm_rect_height(&state->src) >> 16;
12058 int dst_w = drm_rect_width(&state->dst);
12059 int dst_h = drm_rect_height(&state->dst);
12060
12061 return (src_w != dst_w || src_h != dst_h);
12062}
12063
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012064int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12065 struct drm_plane_state *plane_state)
12066{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012067 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012068 struct drm_crtc *crtc = crtc_state->crtc;
12069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12070 struct drm_plane *plane = plane_state->plane;
12071 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080012072 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012073 struct intel_plane_state *old_plane_state =
12074 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012075 bool mode_changed = needs_modeset(crtc_state);
12076 bool was_crtc_enabled = crtc->state->active;
12077 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012078 bool turn_off, turn_on, visible, was_visible;
12079 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030012080 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012081
Chris Wilson84114992016-07-02 15:36:06 +010012082 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012083 ret = skl_update_scaler_plane(
12084 to_intel_crtc_state(crtc_state),
12085 to_intel_plane_state(plane_state));
12086 if (ret)
12087 return ret;
12088 }
12089
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012090 was_visible = old_plane_state->visible;
12091 visible = to_intel_plane_state(plane_state)->visible;
12092
12093 if (!was_crtc_enabled && WARN_ON(was_visible))
12094 was_visible = false;
12095
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012096 /*
12097 * Visibility is calculated as if the crtc was on, but
12098 * after scaler setup everything depends on it being off
12099 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030012100 *
12101 * FIXME this is wrong for watermarks. Watermarks should also
12102 * be computed as if the pipe would be active. Perhaps move
12103 * per-plane wm computation to the .check_plane() hook, and
12104 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012105 */
12106 if (!is_crtc_enabled)
12107 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012108
12109 if (!was_visible && !visible)
12110 return 0;
12111
Maarten Lankhorste8861672016-02-24 11:24:26 +010012112 if (fb != old_plane_state->base.fb)
12113 pipe_config->fb_changed = true;
12114
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012115 turn_off = was_visible && (!visible || mode_changed);
12116 turn_on = visible && (!was_visible || mode_changed);
12117
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012118 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030012119 intel_crtc->base.base.id,
12120 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012121 plane->base.id, plane->name,
12122 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012123
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012124 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12125 plane->base.id, plane->name,
12126 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012127 turn_off, turn_on, mode_changed);
12128
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012129 if (turn_on) {
12130 pipe_config->update_wm_pre = true;
12131
12132 /* must disable cxsr around plane enable/disable */
12133 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12134 pipe_config->disable_cxsr = true;
12135 } else if (turn_off) {
12136 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012137
Ville Syrjälä852eb002015-06-24 22:00:07 +030012138 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012139 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012140 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012141 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012142 /* FIXME bollocks */
12143 pipe_config->update_wm_pre = true;
12144 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012145 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012146
Matt Ropered4a6a72016-02-23 17:20:13 -080012147 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012148 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12149 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012150 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12151
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012152 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012153 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012154
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012155 /*
12156 * WaCxSRDisabledForSpriteScaling:ivb
12157 *
12158 * cstate->update_wm was already set above, so this flag will
12159 * take effect when we commit and program watermarks.
12160 */
12161 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12162 needs_scaling(to_intel_plane_state(plane_state)) &&
12163 !needs_scaling(old_plane_state))
12164 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012165
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012166 return 0;
12167}
12168
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012169static bool encoders_cloneable(const struct intel_encoder *a,
12170 const struct intel_encoder *b)
12171{
12172 /* masks could be asymmetric, so check both ways */
12173 return a == b || (a->cloneable & (1 << b->type) &&
12174 b->cloneable & (1 << a->type));
12175}
12176
12177static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12178 struct intel_crtc *crtc,
12179 struct intel_encoder *encoder)
12180{
12181 struct intel_encoder *source_encoder;
12182 struct drm_connector *connector;
12183 struct drm_connector_state *connector_state;
12184 int i;
12185
12186 for_each_connector_in_state(state, connector, connector_state, i) {
12187 if (connector_state->crtc != &crtc->base)
12188 continue;
12189
12190 source_encoder =
12191 to_intel_encoder(connector_state->best_encoder);
12192 if (!encoders_cloneable(encoder, source_encoder))
12193 return false;
12194 }
12195
12196 return true;
12197}
12198
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012199static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12200 struct drm_crtc_state *crtc_state)
12201{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012202 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012203 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012205 struct intel_crtc_state *pipe_config =
12206 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012207 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012208 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012209 bool mode_changed = needs_modeset(crtc_state);
12210
Ville Syrjälä852eb002015-06-24 22:00:07 +030012211 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012212 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012213
Maarten Lankhorstad421372015-06-15 12:33:42 +020012214 if (mode_changed && crtc_state->enable &&
12215 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012216 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012217 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12218 pipe_config);
12219 if (ret)
12220 return ret;
12221 }
12222
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012223 if (crtc_state->color_mgmt_changed) {
12224 ret = intel_color_check(crtc, crtc_state);
12225 if (ret)
12226 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012227
12228 /*
12229 * Changing color management on Intel hardware is
12230 * handled as part of planes update.
12231 */
12232 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012233 }
12234
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012235 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012236 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012237 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012238 if (ret) {
12239 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012240 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012241 }
12242 }
12243
12244 if (dev_priv->display.compute_intermediate_wm &&
12245 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12246 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12247 return 0;
12248
12249 /*
12250 * Calculate 'intermediate' watermarks that satisfy both the
12251 * old state and the new state. We can program these
12252 * immediately.
12253 */
12254 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12255 intel_crtc,
12256 pipe_config);
12257 if (ret) {
12258 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12259 return ret;
12260 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012261 } else if (dev_priv->display.compute_intermediate_wm) {
12262 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12263 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012264 }
12265
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012266 if (INTEL_INFO(dev)->gen >= 9) {
12267 if (mode_changed)
12268 ret = skl_update_scaler_crtc(pipe_config);
12269
12270 if (!ret)
12271 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12272 pipe_config);
12273 }
12274
12275 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012276}
12277
Jani Nikula65b38e02015-04-13 11:26:56 +030012278static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012279 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012280 .atomic_begin = intel_begin_crtc_commit,
12281 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012282 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012283};
12284
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012285static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12286{
12287 struct intel_connector *connector;
12288
12289 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012290 if (connector->base.state->crtc)
12291 drm_connector_unreference(&connector->base);
12292
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012293 if (connector->base.encoder) {
12294 connector->base.state->best_encoder =
12295 connector->base.encoder;
12296 connector->base.state->crtc =
12297 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012298
12299 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012300 } else {
12301 connector->base.state->best_encoder = NULL;
12302 connector->base.state->crtc = NULL;
12303 }
12304 }
12305}
12306
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012307static void
Robin Schroereba905b2014-05-18 02:24:50 +020012308connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012309 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012310{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012311 int bpp = pipe_config->pipe_bpp;
12312
12313 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12314 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012315 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012316
12317 /* Don't use an invalid EDID bpc value */
12318 if (connector->base.display_info.bpc &&
12319 connector->base.display_info.bpc * 3 < bpp) {
12320 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12321 bpp, connector->base.display_info.bpc*3);
12322 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12323 }
12324
Jani Nikula013dd9e2016-01-13 16:35:20 +020012325 /* Clamp bpp to default limit on screens without EDID 1.4 */
12326 if (connector->base.display_info.bpc == 0) {
12327 int type = connector->base.connector_type;
12328 int clamp_bpp = 24;
12329
12330 /* Fall back to 18 bpp when DP sink capability is unknown. */
12331 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12332 type == DRM_MODE_CONNECTOR_eDP)
12333 clamp_bpp = 18;
12334
12335 if (bpp > clamp_bpp) {
12336 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12337 bpp, clamp_bpp);
12338 pipe_config->pipe_bpp = clamp_bpp;
12339 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012340 }
12341}
12342
12343static int
12344compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012345 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012346{
12347 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012348 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012349 struct drm_connector *connector;
12350 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012351 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012352
Wayne Boyer666a4532015-12-09 12:29:35 -080012353 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012354 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012355 else if (INTEL_INFO(dev)->gen >= 5)
12356 bpp = 12*3;
12357 else
12358 bpp = 8*3;
12359
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012360
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012361 pipe_config->pipe_bpp = bpp;
12362
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012363 state = pipe_config->base.state;
12364
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012365 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012366 for_each_connector_in_state(state, connector, connector_state, i) {
12367 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012368 continue;
12369
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012370 connected_sink_compute_bpp(to_intel_connector(connector),
12371 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012372 }
12373
12374 return bpp;
12375}
12376
Daniel Vetter644db712013-09-19 14:53:58 +020012377static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12378{
12379 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12380 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012381 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012382 mode->crtc_hdisplay, mode->crtc_hsync_start,
12383 mode->crtc_hsync_end, mode->crtc_htotal,
12384 mode->crtc_vdisplay, mode->crtc_vsync_start,
12385 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12386}
12387
Daniel Vetterc0b03412013-05-28 12:05:54 +020012388static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012389 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012390 const char *context)
12391{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012392 struct drm_device *dev = crtc->base.dev;
12393 struct drm_plane *plane;
12394 struct intel_plane *intel_plane;
12395 struct intel_plane_state *state;
12396 struct drm_framebuffer *fb;
12397
Ville Syrjälä78108b72016-05-27 20:59:19 +030012398 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12399 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012400 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012401
Jani Nikulada205632016-03-15 21:51:10 +020012402 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012403 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12404 pipe_config->pipe_bpp, pipe_config->dither);
12405 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12406 pipe_config->has_pch_encoder,
12407 pipe_config->fdi_lanes,
12408 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12409 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12410 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012411 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012412 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012413 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012414 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12415 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12416 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012417
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012418 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012419 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012420 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012421 pipe_config->dp_m2_n2.gmch_m,
12422 pipe_config->dp_m2_n2.gmch_n,
12423 pipe_config->dp_m2_n2.link_m,
12424 pipe_config->dp_m2_n2.link_n,
12425 pipe_config->dp_m2_n2.tu);
12426
Daniel Vetter55072d12014-11-20 16:10:28 +010012427 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12428 pipe_config->has_audio,
12429 pipe_config->has_infoframe);
12430
Daniel Vetterc0b03412013-05-28 12:05:54 +020012431 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012432 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012433 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012434 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12435 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012436 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012437 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12438 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012439 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12440 crtc->num_scalers,
12441 pipe_config->scaler_state.scaler_users,
12442 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012443 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12444 pipe_config->gmch_pfit.control,
12445 pipe_config->gmch_pfit.pgm_ratios,
12446 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012447 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012448 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012449 pipe_config->pch_pfit.size,
12450 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012451 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012452 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012453
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012454 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012455 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012456 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012457 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012458 pipe_config->ddi_pll_sel,
12459 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012460 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012461 pipe_config->dpll_hw_state.pll0,
12462 pipe_config->dpll_hw_state.pll1,
12463 pipe_config->dpll_hw_state.pll2,
12464 pipe_config->dpll_hw_state.pll3,
12465 pipe_config->dpll_hw_state.pll6,
12466 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012467 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012468 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012469 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012470 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012471 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12472 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12473 pipe_config->ddi_pll_sel,
12474 pipe_config->dpll_hw_state.ctrl1,
12475 pipe_config->dpll_hw_state.cfgcr1,
12476 pipe_config->dpll_hw_state.cfgcr2);
12477 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012478 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012479 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012480 pipe_config->dpll_hw_state.wrpll,
12481 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012482 } else {
12483 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12484 "fp0: 0x%x, fp1: 0x%x\n",
12485 pipe_config->dpll_hw_state.dpll,
12486 pipe_config->dpll_hw_state.dpll_md,
12487 pipe_config->dpll_hw_state.fp0,
12488 pipe_config->dpll_hw_state.fp1);
12489 }
12490
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012491 DRM_DEBUG_KMS("planes on this crtc\n");
12492 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12493 intel_plane = to_intel_plane(plane);
12494 if (intel_plane->pipe != crtc->pipe)
12495 continue;
12496
12497 state = to_intel_plane_state(plane->state);
12498 fb = state->base.fb;
12499 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012500 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12501 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012502 continue;
12503 }
12504
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012505 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12506 plane->base.id, plane->name);
12507 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12508 fb->base.id, fb->width, fb->height,
12509 drm_get_format_name(fb->pixel_format));
12510 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12511 state->scaler_id,
12512 state->src.x1 >> 16, state->src.y1 >> 16,
12513 drm_rect_width(&state->src) >> 16,
12514 drm_rect_height(&state->src) >> 16,
12515 state->dst.x1, state->dst.y1,
12516 drm_rect_width(&state->dst),
12517 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012518 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012519}
12520
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012521static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012522{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012523 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012524 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012525 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012526 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012527
12528 /*
12529 * Walk the connector list instead of the encoder
12530 * list to detect the problem on ddi platforms
12531 * where there's just one encoder per digital port.
12532 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012533 drm_for_each_connector(connector, dev) {
12534 struct drm_connector_state *connector_state;
12535 struct intel_encoder *encoder;
12536
12537 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12538 if (!connector_state)
12539 connector_state = connector->state;
12540
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012541 if (!connector_state->best_encoder)
12542 continue;
12543
12544 encoder = to_intel_encoder(connector_state->best_encoder);
12545
12546 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012547
12548 switch (encoder->type) {
12549 unsigned int port_mask;
12550 case INTEL_OUTPUT_UNKNOWN:
12551 if (WARN_ON(!HAS_DDI(dev)))
12552 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012553 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012554 case INTEL_OUTPUT_HDMI:
12555 case INTEL_OUTPUT_EDP:
12556 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12557
12558 /* the same port mustn't appear more than once */
12559 if (used_ports & port_mask)
12560 return false;
12561
12562 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012563 break;
12564 case INTEL_OUTPUT_DP_MST:
12565 used_mst_ports |=
12566 1 << enc_to_mst(&encoder->base)->primary->port;
12567 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012568 default:
12569 break;
12570 }
12571 }
12572
Ville Syrjälä477321e2016-07-28 17:50:40 +030012573 /* can't mix MST and SST/HDMI on the same port */
12574 if (used_ports & used_mst_ports)
12575 return false;
12576
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012577 return true;
12578}
12579
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012580static void
12581clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12582{
12583 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012584 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012585 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012586 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012587 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012588 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012589
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012590 /* FIXME: before the switch to atomic started, a new pipe_config was
12591 * kzalloc'd. Code that depends on any field being zero should be
12592 * fixed, so that the crtc_state can be safely duplicated. For now,
12593 * only fields that are know to not cause problems are preserved. */
12594
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012595 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012596 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012597 shared_dpll = crtc_state->shared_dpll;
12598 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012599 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012600 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012601
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012602 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012603
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012604 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012605 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012606 crtc_state->shared_dpll = shared_dpll;
12607 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012608 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012609 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012610}
12611
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012612static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012613intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012614 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012615{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012616 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012617 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012618 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012619 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012620 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012621 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012622 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012623
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012624 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012625
Daniel Vettere143a212013-07-04 12:01:15 +020012626 pipe_config->cpu_transcoder =
12627 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012628
Imre Deak2960bc92013-07-30 13:36:32 +030012629 /*
12630 * Sanitize sync polarity flags based on requested ones. If neither
12631 * positive or negative polarity is requested, treat this as meaning
12632 * negative polarity.
12633 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012634 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012635 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012636 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012637
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012638 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012639 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012640 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012641
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012642 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12643 pipe_config);
12644 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012645 goto fail;
12646
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012647 /*
12648 * Determine the real pipe dimensions. Note that stereo modes can
12649 * increase the actual pipe size due to the frame doubling and
12650 * insertion of additional space for blanks between the frame. This
12651 * is stored in the crtc timings. We use the requested mode to do this
12652 * computation to clearly distinguish it from the adjusted mode, which
12653 * can be changed by the connectors in the below retry loop.
12654 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012655 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012656 &pipe_config->pipe_src_w,
12657 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012658
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012659 for_each_connector_in_state(state, connector, connector_state, i) {
12660 if (connector_state->crtc != crtc)
12661 continue;
12662
12663 encoder = to_intel_encoder(connector_state->best_encoder);
12664
Ville Syrjäläe25148d2016-06-22 21:57:09 +030012665 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12666 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12667 goto fail;
12668 }
12669
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012670 /*
12671 * Determine output_types before calling the .compute_config()
12672 * hooks so that the hooks can use this information safely.
12673 */
12674 pipe_config->output_types |= 1 << encoder->type;
12675 }
12676
Daniel Vettere29c22c2013-02-21 00:00:16 +010012677encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012678 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012679 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012680 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012681
Daniel Vetter135c81b2013-07-21 21:37:09 +020012682 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012683 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12684 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012685
Daniel Vetter7758a112012-07-08 19:40:39 +020012686 /* Pass our mode to the connectors and the CRTC to give them a chance to
12687 * adjust it according to limitations or connector properties, and also
12688 * a chance to reject the mode entirely.
12689 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012690 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012691 if (connector_state->crtc != crtc)
12692 continue;
12693
12694 encoder = to_intel_encoder(connector_state->best_encoder);
12695
Daniel Vetterefea6e82013-07-21 21:36:59 +020012696 if (!(encoder->compute_config(encoder, pipe_config))) {
12697 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012698 goto fail;
12699 }
12700 }
12701
Daniel Vetterff9a6752013-06-01 17:16:21 +020012702 /* Set default port clock if not overwritten by the encoder. Needs to be
12703 * done afterwards in case the encoder adjusts the mode. */
12704 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012705 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012706 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012707
Daniel Vettera43f6e02013-06-07 23:10:32 +020012708 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012709 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012710 DRM_DEBUG_KMS("CRTC fixup failed\n");
12711 goto fail;
12712 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012713
12714 if (ret == RETRY) {
12715 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12716 ret = -EINVAL;
12717 goto fail;
12718 }
12719
12720 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12721 retry = false;
12722 goto encoder_retry;
12723 }
12724
Daniel Vettere8fa4272015-08-12 11:43:34 +020012725 /* Dithering seems to not pass-through bits correctly when it should, so
12726 * only enable it on 6bpc panels. */
12727 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012728 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012729 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012730
Daniel Vetter7758a112012-07-08 19:40:39 +020012731fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012732 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012733}
12734
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012735static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012736intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012737{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012738 struct drm_crtc *crtc;
12739 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012740 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012741
Ville Syrjälä76688512014-01-10 11:28:06 +020012742 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012743 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012744 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012745
12746 /* Update hwmode for vblank functions */
12747 if (crtc->state->active)
12748 crtc->hwmode = crtc->state->adjusted_mode;
12749 else
12750 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012751
12752 /*
12753 * Update legacy state to satisfy fbc code. This can
12754 * be removed when fbc uses the atomic state.
12755 */
12756 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12757 struct drm_plane_state *plane_state = crtc->primary->state;
12758
12759 crtc->primary->fb = plane_state->fb;
12760 crtc->x = plane_state->src_x >> 16;
12761 crtc->y = plane_state->src_y >> 16;
12762 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012763 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012764}
12765
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012766static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012767{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012768 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012769
12770 if (clock1 == clock2)
12771 return true;
12772
12773 if (!clock1 || !clock2)
12774 return false;
12775
12776 diff = abs(clock1 - clock2);
12777
12778 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12779 return true;
12780
12781 return false;
12782}
12783
Daniel Vetter25c5b262012-07-08 22:08:04 +020012784#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12785 list_for_each_entry((intel_crtc), \
12786 &(dev)->mode_config.crtc_list, \
12787 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012788 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012789
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012790static bool
12791intel_compare_m_n(unsigned int m, unsigned int n,
12792 unsigned int m2, unsigned int n2,
12793 bool exact)
12794{
12795 if (m == m2 && n == n2)
12796 return true;
12797
12798 if (exact || !m || !n || !m2 || !n2)
12799 return false;
12800
12801 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12802
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012803 if (n > n2) {
12804 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012805 m2 <<= 1;
12806 n2 <<= 1;
12807 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012808 } else if (n < n2) {
12809 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012810 m <<= 1;
12811 n <<= 1;
12812 }
12813 }
12814
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012815 if (n != n2)
12816 return false;
12817
12818 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012819}
12820
12821static bool
12822intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12823 struct intel_link_m_n *m2_n2,
12824 bool adjust)
12825{
12826 if (m_n->tu == m2_n2->tu &&
12827 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12828 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12829 intel_compare_m_n(m_n->link_m, m_n->link_n,
12830 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12831 if (adjust)
12832 *m2_n2 = *m_n;
12833
12834 return true;
12835 }
12836
12837 return false;
12838}
12839
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012840static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012841intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012842 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012843 struct intel_crtc_state *pipe_config,
12844 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012845{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012846 bool ret = true;
12847
12848#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12849 do { \
12850 if (!adjust) \
12851 DRM_ERROR(fmt, ##__VA_ARGS__); \
12852 else \
12853 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12854 } while (0)
12855
Daniel Vetter66e985c2013-06-05 13:34:20 +020012856#define PIPE_CONF_CHECK_X(name) \
12857 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012858 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012859 "(expected 0x%08x, found 0x%08x)\n", \
12860 current_config->name, \
12861 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012862 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012863 }
12864
Daniel Vetter08a24032013-04-19 11:25:34 +020012865#define PIPE_CONF_CHECK_I(name) \
12866 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012867 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012868 "(expected %i, found %i)\n", \
12869 current_config->name, \
12870 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012871 ret = false; \
12872 }
12873
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012874#define PIPE_CONF_CHECK_P(name) \
12875 if (current_config->name != pipe_config->name) { \
12876 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12877 "(expected %p, found %p)\n", \
12878 current_config->name, \
12879 pipe_config->name); \
12880 ret = false; \
12881 }
12882
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012883#define PIPE_CONF_CHECK_M_N(name) \
12884 if (!intel_compare_link_m_n(&current_config->name, \
12885 &pipe_config->name,\
12886 adjust)) { \
12887 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12888 "(expected tu %i gmch %i/%i link %i/%i, " \
12889 "found tu %i, gmch %i/%i link %i/%i)\n", \
12890 current_config->name.tu, \
12891 current_config->name.gmch_m, \
12892 current_config->name.gmch_n, \
12893 current_config->name.link_m, \
12894 current_config->name.link_n, \
12895 pipe_config->name.tu, \
12896 pipe_config->name.gmch_m, \
12897 pipe_config->name.gmch_n, \
12898 pipe_config->name.link_m, \
12899 pipe_config->name.link_n); \
12900 ret = false; \
12901 }
12902
Daniel Vetter55c561a2016-03-30 11:34:36 +020012903/* This is required for BDW+ where there is only one set of registers for
12904 * switching between high and low RR.
12905 * This macro can be used whenever a comparison has to be made between one
12906 * hw state and multiple sw state variables.
12907 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012908#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12909 if (!intel_compare_link_m_n(&current_config->name, \
12910 &pipe_config->name, adjust) && \
12911 !intel_compare_link_m_n(&current_config->alt_name, \
12912 &pipe_config->name, adjust)) { \
12913 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12914 "(expected tu %i gmch %i/%i link %i/%i, " \
12915 "or tu %i gmch %i/%i link %i/%i, " \
12916 "found tu %i, gmch %i/%i link %i/%i)\n", \
12917 current_config->name.tu, \
12918 current_config->name.gmch_m, \
12919 current_config->name.gmch_n, \
12920 current_config->name.link_m, \
12921 current_config->name.link_n, \
12922 current_config->alt_name.tu, \
12923 current_config->alt_name.gmch_m, \
12924 current_config->alt_name.gmch_n, \
12925 current_config->alt_name.link_m, \
12926 current_config->alt_name.link_n, \
12927 pipe_config->name.tu, \
12928 pipe_config->name.gmch_m, \
12929 pipe_config->name.gmch_n, \
12930 pipe_config->name.link_m, \
12931 pipe_config->name.link_n); \
12932 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012933 }
12934
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012935#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12936 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012937 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012938 "(expected %i, found %i)\n", \
12939 current_config->name & (mask), \
12940 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012941 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012942 }
12943
Ville Syrjälä5e550652013-09-06 23:29:07 +030012944#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12945 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012946 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012947 "(expected %i, found %i)\n", \
12948 current_config->name, \
12949 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012950 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012951 }
12952
Daniel Vetterbb760062013-06-06 14:55:52 +020012953#define PIPE_CONF_QUIRK(quirk) \
12954 ((current_config->quirks | pipe_config->quirks) & (quirk))
12955
Daniel Vettereccb1402013-05-22 00:50:22 +020012956 PIPE_CONF_CHECK_I(cpu_transcoder);
12957
Daniel Vetter08a24032013-04-19 11:25:34 +020012958 PIPE_CONF_CHECK_I(has_pch_encoder);
12959 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012960 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012961
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012962 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012963 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012964
12965 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012966 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012967
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012968 if (current_config->has_drrs)
12969 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12970 } else
12971 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012972
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012973 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020012974
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012975 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12976 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12977 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12978 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12979 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12980 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012981
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012982 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12983 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12984 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12985 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12986 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12987 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012988
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012989 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012990 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012991 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012992 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012993 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012994 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012995
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012996 PIPE_CONF_CHECK_I(has_audio);
12997
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012998 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012999 DRM_MODE_FLAG_INTERLACE);
13000
Daniel Vetterbb760062013-06-06 14:55:52 +020013001 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013002 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013003 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013004 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013005 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013006 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013007 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013008 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013009 DRM_MODE_FLAG_NVSYNC);
13010 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013011
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013012 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020013013 /* pfit ratios are autocomputed by the hw on gen4+ */
13014 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020013015 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013016 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020013017
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013018 if (!adjust) {
13019 PIPE_CONF_CHECK_I(pipe_src_w);
13020 PIPE_CONF_CHECK_I(pipe_src_h);
13021
13022 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13023 if (current_config->pch_pfit.enabled) {
13024 PIPE_CONF_CHECK_X(pch_pfit.pos);
13025 PIPE_CONF_CHECK_X(pch_pfit.size);
13026 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020013027
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020013028 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13029 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070013030
Jesse Barnese59150d2014-01-07 13:30:45 -080013031 /* BDW+ don't expose a synchronous way to read the state */
13032 if (IS_HASWELL(dev))
13033 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030013034
Ville Syrjälä282740f2013-09-04 18:30:03 +030013035 PIPE_CONF_CHECK_I(double_wide);
13036
Daniel Vetter26804af2014-06-25 22:01:55 +030013037 PIPE_CONF_CHECK_X(ddi_pll_sel);
13038
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013039 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013040 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020013041 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013042 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13043 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030013044 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010013045 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000013046 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13047 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13048 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020013049
Ville Syrjälä47eacba2016-04-12 22:14:35 +030013050 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13051 PIPE_CONF_CHECK_X(dsi_pll.div);
13052
Ville Syrjälä42571ae2013-09-06 23:29:00 +030013053 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
13054 PIPE_CONF_CHECK_I(pipe_bpp);
13055
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013056 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080013057 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030013058
Daniel Vetter66e985c2013-06-05 13:34:20 +020013059#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020013060#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013061#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013062#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030013063#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020013064#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013065#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020013066
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013067 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013068}
13069
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013070static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13071 const struct intel_crtc_state *pipe_config)
13072{
13073 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020013074 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013075 &pipe_config->fdi_m_n);
13076 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13077
13078 /*
13079 * FDI already provided one idea for the dotclock.
13080 * Yell if the encoder disagrees.
13081 */
13082 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13083 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13084 fdi_dotclock, dotclock);
13085 }
13086}
13087
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013088static void verify_wm_state(struct drm_crtc *crtc,
13089 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000013090{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013091 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013092 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000013093 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013094 struct skl_ddb_entry *hw_entry, *sw_entry;
13095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13096 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000013097 int plane;
13098
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013099 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000013100 return;
13101
13102 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13103 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13104
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013105 /* planes */
13106 for_each_plane(dev_priv, pipe, plane) {
13107 hw_entry = &hw_ddb.plane[pipe][plane];
13108 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000013109
13110 if (skl_ddb_entry_equal(hw_entry, sw_entry))
13111 continue;
13112
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013113 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
13114 "(expected (%u,%u), found (%u,%u))\n",
13115 pipe_name(pipe), plane + 1,
13116 sw_entry->start, sw_entry->end,
13117 hw_entry->start, hw_entry->end);
13118 }
13119
13120 /* cursor */
13121 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13122 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
13123
13124 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000013125 DRM_ERROR("mismatch in DDB state pipe %c cursor "
13126 "(expected (%u,%u), found (%u,%u))\n",
13127 pipe_name(pipe),
13128 sw_entry->start, sw_entry->end,
13129 hw_entry->start, hw_entry->end);
13130 }
13131}
13132
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013133static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013134verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013135{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013136 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013137
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013138 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013139 struct drm_encoder *encoder = connector->encoder;
13140 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013141
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013142 if (state->crtc != crtc)
13143 continue;
13144
Daniel Vetter5a21b662016-05-24 17:13:53 +020013145 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013146
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013147 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013148 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013149 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013150}
13151
13152static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013153verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013154{
13155 struct intel_encoder *encoder;
13156 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013157
Damien Lespiaub2784e12014-08-05 11:29:37 +010013158 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013159 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013160 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013161
13162 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13163 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013164 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013165
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013166 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013167 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013168 continue;
13169 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013170
13171 I915_STATE_WARN(connector->base.state->crtc !=
13172 encoder->base.crtc,
13173 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013174 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013175
Rob Clarke2c719b2014-12-15 13:56:32 -050013176 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013177 "encoder's enabled state mismatch "
13178 "(expected %i, found %i)\n",
13179 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013180
13181 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013182 bool active;
13183
13184 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013185 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013186 "encoder detached but still enabled on pipe %c.\n",
13187 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013188 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013189 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013190}
13191
13192static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013193verify_crtc_state(struct drm_crtc *crtc,
13194 struct drm_crtc_state *old_crtc_state,
13195 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013196{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013197 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013198 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013199 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13201 struct intel_crtc_state *pipe_config, *sw_config;
13202 struct drm_atomic_state *old_state;
13203 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013204
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013205 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013206 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013207 pipe_config = to_intel_crtc_state(old_crtc_state);
13208 memset(pipe_config, 0, sizeof(*pipe_config));
13209 pipe_config->base.crtc = crtc;
13210 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013211
Ville Syrjälä78108b72016-05-27 20:59:19 +030013212 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013213
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013214 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013215
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013216 /* hw state is inconsistent with the pipe quirk */
13217 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13218 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13219 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013220
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013221 I915_STATE_WARN(new_crtc_state->active != active,
13222 "crtc active state doesn't match with hw state "
13223 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013224
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013225 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13226 "transitional active state does not match atomic hw state "
13227 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013228
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013229 for_each_encoder_on_crtc(dev, crtc, encoder) {
13230 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013231
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013232 active = encoder->get_hw_state(encoder, &pipe);
13233 I915_STATE_WARN(active != new_crtc_state->active,
13234 "[ENCODER:%i] active %i with crtc active %i\n",
13235 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013236
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013237 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13238 "Encoder connected to wrong pipe %c\n",
13239 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013240
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013241 if (active) {
13242 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013243 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013244 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013245 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013246
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013247 if (!new_crtc_state->active)
13248 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013249
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013250 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013251
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013252 sw_config = to_intel_crtc_state(crtc->state);
13253 if (!intel_pipe_config_compare(dev, sw_config,
13254 pipe_config, false)) {
13255 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13256 intel_dump_pipe_config(intel_crtc, pipe_config,
13257 "[hw state]");
13258 intel_dump_pipe_config(intel_crtc, sw_config,
13259 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013260 }
13261}
13262
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013263static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013264verify_single_dpll_state(struct drm_i915_private *dev_priv,
13265 struct intel_shared_dpll *pll,
13266 struct drm_crtc *crtc,
13267 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013268{
13269 struct intel_dpll_hw_state dpll_hw_state;
13270 unsigned crtc_mask;
13271 bool active;
13272
13273 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13274
13275 DRM_DEBUG_KMS("%s\n", pll->name);
13276
13277 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13278
13279 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13280 I915_STATE_WARN(!pll->on && pll->active_mask,
13281 "pll in active use but not on in sw tracking\n");
13282 I915_STATE_WARN(pll->on && !pll->active_mask,
13283 "pll is on but not used by any active crtc\n");
13284 I915_STATE_WARN(pll->on != active,
13285 "pll on state mismatch (expected %i, found %i)\n",
13286 pll->on, active);
13287 }
13288
13289 if (!crtc) {
13290 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13291 "more active pll users than references: %x vs %x\n",
13292 pll->active_mask, pll->config.crtc_mask);
13293
13294 return;
13295 }
13296
13297 crtc_mask = 1 << drm_crtc_index(crtc);
13298
13299 if (new_state->active)
13300 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13301 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13302 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13303 else
13304 I915_STATE_WARN(pll->active_mask & crtc_mask,
13305 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13306 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13307
13308 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13309 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13310 crtc_mask, pll->config.crtc_mask);
13311
13312 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13313 &dpll_hw_state,
13314 sizeof(dpll_hw_state)),
13315 "pll hw state mismatch\n");
13316}
13317
13318static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013319verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13320 struct drm_crtc_state *old_crtc_state,
13321 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013322{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013323 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013324 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13325 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13326
13327 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013328 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013329
13330 if (old_state->shared_dpll &&
13331 old_state->shared_dpll != new_state->shared_dpll) {
13332 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13333 struct intel_shared_dpll *pll = old_state->shared_dpll;
13334
13335 I915_STATE_WARN(pll->active_mask & crtc_mask,
13336 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13337 pipe_name(drm_crtc_index(crtc)));
13338 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13339 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13340 pipe_name(drm_crtc_index(crtc)));
13341 }
13342}
13343
13344static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013345intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013346 struct drm_crtc_state *old_state,
13347 struct drm_crtc_state *new_state)
13348{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013349 if (!needs_modeset(new_state) &&
13350 !to_intel_crtc_state(new_state)->update_pipe)
13351 return;
13352
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013353 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013354 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013355 verify_crtc_state(crtc, old_state, new_state);
13356 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013357}
13358
13359static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013360verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013361{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013362 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013363 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013364
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013365 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013366 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013367}
Daniel Vetter53589012013-06-05 13:34:16 +020013368
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013369static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013370intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013371{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013372 verify_encoder_state(dev);
13373 verify_connector_state(dev, NULL);
13374 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013375}
13376
Ville Syrjälä80715b22014-05-15 20:23:23 +030013377static void update_scanline_offset(struct intel_crtc *crtc)
13378{
13379 struct drm_device *dev = crtc->base.dev;
13380
13381 /*
13382 * The scanline counter increments at the leading edge of hsync.
13383 *
13384 * On most platforms it starts counting from vtotal-1 on the
13385 * first active line. That means the scanline counter value is
13386 * always one less than what we would expect. Ie. just after
13387 * start of vblank, which also occurs at start of hsync (on the
13388 * last active line), the scanline counter will read vblank_start-1.
13389 *
13390 * On gen2 the scanline counter starts counting from 1 instead
13391 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13392 * to keep the value positive), instead of adding one.
13393 *
13394 * On HSW+ the behaviour of the scanline counter depends on the output
13395 * type. For DP ports it behaves like most other platforms, but on HDMI
13396 * there's an extra 1 line difference. So we need to add two instead of
13397 * one to the value.
13398 */
13399 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013400 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013401 int vtotal;
13402
Ville Syrjälä124abe02015-09-08 13:40:45 +030013403 vtotal = adjusted_mode->crtc_vtotal;
13404 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013405 vtotal /= 2;
13406
13407 crtc->scanline_offset = vtotal - 1;
13408 } else if (HAS_DDI(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013409 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013410 crtc->scanline_offset = 2;
13411 } else
13412 crtc->scanline_offset = 1;
13413}
13414
Maarten Lankhorstad421372015-06-15 12:33:42 +020013415static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013416{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013417 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013418 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013419 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013420 struct drm_crtc *crtc;
13421 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013422 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013423
13424 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013425 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013426
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013427 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013429 struct intel_shared_dpll *old_dpll =
13430 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013431
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013432 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013433 continue;
13434
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013435 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013436
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013437 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013438 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013439
Maarten Lankhorstad421372015-06-15 12:33:42 +020013440 if (!shared_dpll)
13441 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13442
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013443 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013444 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013445}
13446
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013447/*
13448 * This implements the workaround described in the "notes" section of the mode
13449 * set sequence documentation. When going from no pipes or single pipe to
13450 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13451 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13452 */
13453static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13454{
13455 struct drm_crtc_state *crtc_state;
13456 struct intel_crtc *intel_crtc;
13457 struct drm_crtc *crtc;
13458 struct intel_crtc_state *first_crtc_state = NULL;
13459 struct intel_crtc_state *other_crtc_state = NULL;
13460 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13461 int i;
13462
13463 /* look at all crtc's that are going to be enabled in during modeset */
13464 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13465 intel_crtc = to_intel_crtc(crtc);
13466
13467 if (!crtc_state->active || !needs_modeset(crtc_state))
13468 continue;
13469
13470 if (first_crtc_state) {
13471 other_crtc_state = to_intel_crtc_state(crtc_state);
13472 break;
13473 } else {
13474 first_crtc_state = to_intel_crtc_state(crtc_state);
13475 first_pipe = intel_crtc->pipe;
13476 }
13477 }
13478
13479 /* No workaround needed? */
13480 if (!first_crtc_state)
13481 return 0;
13482
13483 /* w/a possibly needed, check how many crtc's are already enabled. */
13484 for_each_intel_crtc(state->dev, intel_crtc) {
13485 struct intel_crtc_state *pipe_config;
13486
13487 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13488 if (IS_ERR(pipe_config))
13489 return PTR_ERR(pipe_config);
13490
13491 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13492
13493 if (!pipe_config->base.active ||
13494 needs_modeset(&pipe_config->base))
13495 continue;
13496
13497 /* 2 or more enabled crtcs means no need for w/a */
13498 if (enabled_pipe != INVALID_PIPE)
13499 return 0;
13500
13501 enabled_pipe = intel_crtc->pipe;
13502 }
13503
13504 if (enabled_pipe != INVALID_PIPE)
13505 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13506 else if (other_crtc_state)
13507 other_crtc_state->hsw_workaround_pipe = first_pipe;
13508
13509 return 0;
13510}
13511
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013512static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13513{
13514 struct drm_crtc *crtc;
13515 struct drm_crtc_state *crtc_state;
13516 int ret = 0;
13517
13518 /* add all active pipes to the state */
13519 for_each_crtc(state->dev, crtc) {
13520 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13521 if (IS_ERR(crtc_state))
13522 return PTR_ERR(crtc_state);
13523
13524 if (!crtc_state->active || needs_modeset(crtc_state))
13525 continue;
13526
13527 crtc_state->mode_changed = true;
13528
13529 ret = drm_atomic_add_affected_connectors(state, crtc);
13530 if (ret)
13531 break;
13532
13533 ret = drm_atomic_add_affected_planes(state, crtc);
13534 if (ret)
13535 break;
13536 }
13537
13538 return ret;
13539}
13540
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013541static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013542{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013543 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013544 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013545 struct drm_crtc *crtc;
13546 struct drm_crtc_state *crtc_state;
13547 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013548
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013549 if (!check_digital_port_conflicts(state)) {
13550 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13551 return -EINVAL;
13552 }
13553
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013554 intel_state->modeset = true;
13555 intel_state->active_crtcs = dev_priv->active_crtcs;
13556
13557 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13558 if (crtc_state->active)
13559 intel_state->active_crtcs |= 1 << i;
13560 else
13561 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013562
13563 if (crtc_state->active != crtc->state->active)
13564 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013565 }
13566
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013567 /*
13568 * See if the config requires any additional preparation, e.g.
13569 * to adjust global state with pipes off. We need to do this
13570 * here so we can get the modeset_pipe updated config for the new
13571 * mode set on this crtc. For other crtcs we need to use the
13572 * adjusted_mode bits in the crtc directly.
13573 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013574 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013575 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013576 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013577 if (!intel_state->cdclk_pll_vco)
13578 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013579
Clint Taylorc89e39f2016-05-13 23:41:21 +030013580 ret = dev_priv->display.modeset_calc_cdclk(state);
13581 if (ret < 0)
13582 return ret;
13583
13584 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013585 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013586 ret = intel_modeset_all_pipes(state);
13587
13588 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013589 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013590
13591 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13592 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013593 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013594 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013595
Maarten Lankhorstad421372015-06-15 12:33:42 +020013596 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013597
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013598 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013599 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013600
Maarten Lankhorstad421372015-06-15 12:33:42 +020013601 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013602}
13603
Matt Roperaa363132015-09-24 15:53:18 -070013604/*
13605 * Handle calculation of various watermark data at the end of the atomic check
13606 * phase. The code here should be run after the per-crtc and per-plane 'check'
13607 * handlers to ensure that all derived state has been updated.
13608 */
Matt Roper55994c22016-05-12 07:06:08 -070013609static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013610{
13611 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013612 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013613
13614 /* Is there platform-specific watermark information to calculate? */
13615 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013616 return dev_priv->display.compute_global_watermarks(state);
13617
13618 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013619}
13620
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013621/**
13622 * intel_atomic_check - validate state object
13623 * @dev: drm device
13624 * @state: state to validate
13625 */
13626static int intel_atomic_check(struct drm_device *dev,
13627 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013628{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013629 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013630 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013631 struct drm_crtc *crtc;
13632 struct drm_crtc_state *crtc_state;
13633 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013634 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013635
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013636 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013637 if (ret)
13638 return ret;
13639
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013640 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013641 struct intel_crtc_state *pipe_config =
13642 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013643
13644 /* Catch I915_MODE_FLAG_INHERITED */
13645 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13646 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013647
Daniel Vetter26495482015-07-15 14:15:52 +020013648 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013649 continue;
13650
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013651 if (!crtc_state->enable) {
13652 any_ms = true;
13653 continue;
13654 }
13655
Daniel Vetter26495482015-07-15 14:15:52 +020013656 /* FIXME: For only active_changed we shouldn't need to do any
13657 * state recomputation at all. */
13658
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013659 ret = drm_atomic_add_affected_connectors(state, crtc);
13660 if (ret)
13661 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013662
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013663 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013664 if (ret) {
13665 intel_dump_pipe_config(to_intel_crtc(crtc),
13666 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013667 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013668 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013669
Jani Nikula73831232015-11-19 10:26:30 +020013670 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013671 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013672 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013673 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013674 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013675 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013676 }
13677
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013678 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013679 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013680
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013681 ret = drm_atomic_add_affected_planes(state, crtc);
13682 if (ret)
13683 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013684
Daniel Vetter26495482015-07-15 14:15:52 +020013685 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13686 needs_modeset(crtc_state) ?
13687 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013688 }
13689
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013690 if (any_ms) {
13691 ret = intel_modeset_checks(state);
13692
13693 if (ret)
13694 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013695 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013696 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013697
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013698 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013699 if (ret)
13700 return ret;
13701
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013702 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013703 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013704}
13705
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013706static int intel_atomic_prepare_commit(struct drm_device *dev,
13707 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013708 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013709{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013710 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013711 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013712 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013713 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013714 struct drm_crtc *crtc;
13715 int i, ret;
13716
Daniel Vetter5a21b662016-05-24 17:13:53 +020013717 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13718 if (state->legacy_cursor_update)
13719 continue;
13720
13721 ret = intel_crtc_wait_for_pending_flips(crtc);
13722 if (ret)
13723 return ret;
13724
13725 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13726 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013727 }
13728
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013729 ret = mutex_lock_interruptible(&dev->struct_mutex);
13730 if (ret)
13731 return ret;
13732
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013733 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013734 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013735
Dave Airlie21daaee2016-05-05 09:56:30 +100013736 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013737 for_each_plane_in_state(state, plane, plane_state, i) {
13738 struct intel_plane_state *intel_plane_state =
13739 to_intel_plane_state(plane_state);
13740
13741 if (!intel_plane_state->wait_req)
13742 continue;
13743
Chris Wilson776f3232016-08-04 07:52:40 +010013744 ret = i915_wait_request(intel_plane_state->wait_req,
13745 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013746 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013747 /* Any hang should be swallowed by the wait */
13748 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013749 mutex_lock(&dev->struct_mutex);
13750 drm_atomic_helper_cleanup_planes(dev, state);
13751 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013752 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013753 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013754 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013755 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013756
13757 return ret;
13758}
13759
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013760u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13761{
13762 struct drm_device *dev = crtc->base.dev;
13763
13764 if (!dev->max_vblank_count)
13765 return drm_accurate_vblank_count(&crtc->base);
13766
13767 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13768}
13769
Daniel Vetter5a21b662016-05-24 17:13:53 +020013770static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13771 struct drm_i915_private *dev_priv,
13772 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013773{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013774 unsigned last_vblank_count[I915_MAX_PIPES];
13775 enum pipe pipe;
13776 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013777
Daniel Vetter5a21b662016-05-24 17:13:53 +020013778 if (!crtc_mask)
13779 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013780
Daniel Vetter5a21b662016-05-24 17:13:53 +020013781 for_each_pipe(dev_priv, pipe) {
13782 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013783
Daniel Vetter5a21b662016-05-24 17:13:53 +020013784 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013785 continue;
13786
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013787 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013788 if (WARN_ON(ret != 0)) {
13789 crtc_mask &= ~(1 << pipe);
13790 continue;
13791 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013792
Daniel Vetter5a21b662016-05-24 17:13:53 +020013793 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13794 }
13795
13796 for_each_pipe(dev_priv, pipe) {
13797 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13798 long lret;
13799
13800 if (!((1 << pipe) & crtc_mask))
13801 continue;
13802
13803 lret = wait_event_timeout(dev->vblank[pipe].queue,
13804 last_vblank_count[pipe] !=
13805 drm_crtc_vblank_count(crtc),
13806 msecs_to_jiffies(50));
13807
13808 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13809
13810 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013811 }
13812}
13813
Daniel Vetter5a21b662016-05-24 17:13:53 +020013814static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013815{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013816 /* fb updated, need to unpin old fb */
13817 if (crtc_state->fb_changed)
13818 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013819
Daniel Vetter5a21b662016-05-24 17:13:53 +020013820 /* wm changes, need vblank before final wm's */
13821 if (crtc_state->update_wm_post)
13822 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013823
Daniel Vetter5a21b662016-05-24 17:13:53 +020013824 /*
13825 * cxsr is re-enabled after vblank.
13826 * This is already handled by crtc_state->update_wm_post,
13827 * but added for clarity.
13828 */
13829 if (crtc_state->disable_cxsr)
13830 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013831
Daniel Vetter5a21b662016-05-24 17:13:53 +020013832 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013833}
13834
Daniel Vetter94f05022016-06-14 18:01:00 +020013835static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013836{
Daniel Vetter94f05022016-06-14 18:01:00 +020013837 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013838 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013839 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013840 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013841 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013842 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013843 struct drm_plane *plane;
13844 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013845 bool hw_check = intel_state->modeset;
13846 unsigned long put_domains[I915_MAX_PIPES] = {};
13847 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013848 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013849
Daniel Vetter94f05022016-06-14 18:01:00 +020013850 for_each_plane_in_state(state, plane, plane_state, i) {
13851 struct intel_plane_state *intel_plane_state =
13852 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013853
Daniel Vetter94f05022016-06-14 18:01:00 +020013854 if (!intel_plane_state->wait_req)
13855 continue;
13856
Chris Wilson776f3232016-08-04 07:52:40 +010013857 ret = i915_wait_request(intel_plane_state->wait_req,
13858 true, NULL, NULL);
Daniel Vetter94f05022016-06-14 18:01:00 +020013859 /* EIO should be eaten, and we can't get interrupted in the
13860 * worker, and blocking commits have waited already. */
13861 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013862 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013863
Daniel Vetterea0000f2016-06-13 16:13:46 +020013864 drm_atomic_helper_wait_for_dependencies(state);
13865
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013866 if (intel_state->modeset) {
13867 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13868 sizeof(intel_state->min_pixclk));
13869 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013870 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013871
13872 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013873 }
13874
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013875 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13877
Daniel Vetter5a21b662016-05-24 17:13:53 +020013878 if (needs_modeset(crtc->state) ||
13879 to_intel_crtc_state(crtc->state)->update_pipe) {
13880 hw_check = true;
13881
13882 put_domains[to_intel_crtc(crtc)->pipe] =
13883 modeset_get_crtc_power_domains(crtc,
13884 to_intel_crtc_state(crtc->state));
13885 }
13886
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013887 if (!needs_modeset(crtc->state))
13888 continue;
13889
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013890 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013891
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013892 if (old_crtc_state->active) {
13893 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013894 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013895 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013896 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013897 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013898
13899 /*
13900 * Underruns don't always raise
13901 * interrupts, so check manually.
13902 */
13903 intel_check_cpu_fifo_underruns(dev_priv);
13904 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013905
13906 if (!crtc->state->active)
13907 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013908 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013909 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013910
Daniel Vetterea9d7582012-07-10 10:42:52 +020013911 /* Only after disabling all output pipelines that will be changed can we
13912 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013913 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013914
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013915 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013916 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013917
13918 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013919 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013920 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013921 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013922
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013923 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013924 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013925
Daniel Vettera6778b32012-07-02 09:56:42 +020013926 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013927 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13929 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013930 struct intel_crtc_state *pipe_config =
13931 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013932
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013933 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013934 update_scanline_offset(to_intel_crtc(crtc));
13935 dev_priv->display.crtc_enable(crtc);
13936 }
13937
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013938 /* Complete events for now disable pipes here. */
13939 if (modeset && !crtc->state->active && crtc->state->event) {
13940 spin_lock_irq(&dev->event_lock);
13941 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13942 spin_unlock_irq(&dev->event_lock);
13943
13944 crtc->state->event = NULL;
13945 }
13946
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013947 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013948 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013949
Daniel Vetter5a21b662016-05-24 17:13:53 +020013950 if (crtc->state->active &&
13951 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013952 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013953
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013954 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013955 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013956
Daniel Vetter5a21b662016-05-24 17:13:53 +020013957 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13958 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013959 }
13960
Daniel Vetter94f05022016-06-14 18:01:00 +020013961 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13962 * already, but still need the state for the delayed optimization. To
13963 * fix this:
13964 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13965 * - schedule that vblank worker _before_ calling hw_done
13966 * - at the start of commit_tail, cancel it _synchrously
13967 * - switch over to the vblank wait helper in the core after that since
13968 * we don't need out special handling any more.
13969 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013970 if (!state->legacy_cursor_update)
13971 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13972
13973 /*
13974 * Now that the vblank has passed, we can go ahead and program the
13975 * optimal watermarks on platforms that need two-step watermark
13976 * programming.
13977 *
13978 * TODO: Move this (and other cleanup) to an async worker eventually.
13979 */
13980 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13981 intel_cstate = to_intel_crtc_state(crtc->state);
13982
13983 if (dev_priv->display.optimize_watermarks)
13984 dev_priv->display.optimize_watermarks(intel_cstate);
13985 }
13986
13987 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13988 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13989
13990 if (put_domains[i])
13991 modeset_put_power_domains(dev_priv, put_domains[i]);
13992
13993 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13994 }
13995
Daniel Vetter94f05022016-06-14 18:01:00 +020013996 drm_atomic_helper_commit_hw_done(state);
13997
Daniel Vetter5a21b662016-05-24 17:13:53 +020013998 if (intel_state->modeset)
13999 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14000
14001 mutex_lock(&dev->struct_mutex);
14002 drm_atomic_helper_cleanup_planes(dev, state);
14003 mutex_unlock(&dev->struct_mutex);
14004
Daniel Vetterea0000f2016-06-13 16:13:46 +020014005 drm_atomic_helper_commit_cleanup_done(state);
14006
Maarten Lankhorstee165b12015-08-05 12:37:00 +020014007 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080014008
Mika Kuoppala75714942015-12-16 09:26:48 +020014009 /* As one of the primary mmio accessors, KMS has a high likelihood
14010 * of triggering bugs in unclaimed access. After we finish
14011 * modesetting, see if an error has been flagged, and if so
14012 * enable debugging for the next modeset - and hope we catch
14013 * the culprit.
14014 *
14015 * XXX note that we assume display power is on at this point.
14016 * This might hold true now but we need to add pm helper to check
14017 * unclaimed only when the hardware is on, as atomic commits
14018 * can happen also when the device is completely off.
14019 */
14020 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020014021}
14022
14023static void intel_atomic_commit_work(struct work_struct *work)
14024{
14025 struct drm_atomic_state *state = container_of(work,
14026 struct drm_atomic_state,
14027 commit_work);
14028 intel_atomic_commit_tail(state);
14029}
14030
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014031static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14032{
14033 struct drm_plane_state *old_plane_state;
14034 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014035 int i;
14036
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010014037 for_each_plane_in_state(state, plane, old_plane_state, i)
14038 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14039 intel_fb_obj(plane->state->fb),
14040 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014041}
14042
Daniel Vetter94f05022016-06-14 18:01:00 +020014043/**
14044 * intel_atomic_commit - commit validated state object
14045 * @dev: DRM device
14046 * @state: the top-level driver state object
14047 * @nonblock: nonblocking commit
14048 *
14049 * This function commits a top-level state object that has been validated
14050 * with drm_atomic_helper_check().
14051 *
14052 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
14053 * nonblocking commits are only safe for pure plane updates. Everything else
14054 * should work though.
14055 *
14056 * RETURNS
14057 * Zero for success or -errno.
14058 */
14059static int intel_atomic_commit(struct drm_device *dev,
14060 struct drm_atomic_state *state,
14061 bool nonblock)
14062{
14063 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014064 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020014065 int ret = 0;
14066
14067 if (intel_state->modeset && nonblock) {
14068 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
14069 return -EINVAL;
14070 }
14071
14072 ret = drm_atomic_helper_setup_commit(state, nonblock);
14073 if (ret)
14074 return ret;
14075
14076 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
14077
14078 ret = intel_atomic_prepare_commit(dev, state, nonblock);
14079 if (ret) {
14080 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
14081 return ret;
14082 }
14083
14084 drm_atomic_helper_swap_state(state, true);
14085 dev_priv->wm.distrust_bios_wm = false;
14086 dev_priv->wm.skl_results = intel_state->wm_results;
14087 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014088 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014089
14090 if (nonblock)
14091 queue_work(system_unbound_wq, &state->commit_work);
14092 else
14093 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020014094
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014095 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020014096}
14097
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014098void intel_crtc_restore_mode(struct drm_crtc *crtc)
14099{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014100 struct drm_device *dev = crtc->dev;
14101 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014102 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014103 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014104
14105 state = drm_atomic_state_alloc(dev);
14106 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030014107 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14108 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014109 return;
14110 }
14111
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014112 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014113
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014114retry:
14115 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14116 ret = PTR_ERR_OR_ZERO(crtc_state);
14117 if (!ret) {
14118 if (!crtc_state->active)
14119 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014120
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014121 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014122 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014123 }
14124
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014125 if (ret == -EDEADLK) {
14126 drm_atomic_state_clear(state);
14127 drm_modeset_backoff(state->acquire_ctx);
14128 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030014129 }
14130
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014131 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014132out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014133 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014134}
14135
Daniel Vetter25c5b262012-07-08 22:08:04 +020014136#undef for_each_intel_crtc_masked
14137
Bob Paauwea8784872016-07-15 14:59:02 +010014138/*
14139 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14140 * drm_atomic_helper_legacy_gamma_set() directly.
14141 */
14142static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14143 u16 *red, u16 *green, u16 *blue,
14144 uint32_t size)
14145{
14146 struct drm_device *dev = crtc->dev;
14147 struct drm_mode_config *config = &dev->mode_config;
14148 struct drm_crtc_state *state;
14149 int ret;
14150
14151 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14152 if (ret)
14153 return ret;
14154
14155 /*
14156 * Make sure we update the legacy properties so this works when
14157 * atomic is not enabled.
14158 */
14159
14160 state = crtc->state;
14161
14162 drm_object_property_set_value(&crtc->base,
14163 config->degamma_lut_property,
14164 (state->degamma_lut) ?
14165 state->degamma_lut->base.id : 0);
14166
14167 drm_object_property_set_value(&crtc->base,
14168 config->ctm_property,
14169 (state->ctm) ?
14170 state->ctm->base.id : 0);
14171
14172 drm_object_property_set_value(&crtc->base,
14173 config->gamma_lut_property,
14174 (state->gamma_lut) ?
14175 state->gamma_lut->base.id : 0);
14176
14177 return 0;
14178}
14179
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014180static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010014181 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014182 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014183 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014184 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014185 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014186 .atomic_duplicate_state = intel_crtc_duplicate_state,
14187 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014188};
14189
Matt Roper6beb8c232014-12-01 15:40:14 -080014190/**
14191 * intel_prepare_plane_fb - Prepare fb for usage on plane
14192 * @plane: drm plane to prepare for
14193 * @fb: framebuffer to prepare for presentation
14194 *
14195 * Prepares a framebuffer for usage on a display plane. Generally this
14196 * involves pinning the underlying object and updating the frontbuffer tracking
14197 * bits. Some older platforms need special physical address handling for
14198 * cursor planes.
14199 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014200 * Must be called with struct_mutex held.
14201 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014202 * Returns 0 on success, negative error code on failure.
14203 */
14204int
14205intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014206 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014207{
14208 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014209 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014210 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014211 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010014212 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080014213 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014214
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014215 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014216 return 0;
14217
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014218 if (old_obj) {
14219 struct drm_crtc_state *crtc_state =
14220 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14221
14222 /* Big Hammer, we also need to ensure that any pending
14223 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14224 * current scanout is retired before unpinning the old
14225 * framebuffer. Note that we rely on userspace rendering
14226 * into the buffer attached to the pipe they are waiting
14227 * on. If not, userspace generates a GPU hang with IPEHR
14228 * point to the MI_WAIT_FOR_EVENT.
14229 *
14230 * This should only fail upon a hung GPU, in which case we
14231 * can safely continue.
14232 */
14233 if (needs_modeset(crtc_state))
14234 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014235 if (ret) {
14236 /* GPU hangs should have been swallowed by the wait */
14237 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014238 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014239 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014240 }
14241
Chris Wilsonc37efb92016-06-17 08:28:47 +010014242 if (!obj)
14243 return 0;
14244
Daniel Vetter5a21b662016-05-24 17:13:53 +020014245 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014246 resv = i915_gem_object_get_dmabuf_resv(obj);
14247 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014248 long lret;
14249
Chris Wilsonc37efb92016-06-17 08:28:47 +010014250 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014251 MAX_SCHEDULE_TIMEOUT);
14252 if (lret == -ERESTARTSYS)
14253 return lret;
14254
14255 WARN(lret < 0, "waiting returns %li\n", lret);
14256 }
14257
Chris Wilsonc37efb92016-06-17 08:28:47 +010014258 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014259 INTEL_INFO(dev)->cursor_needs_physical) {
14260 int align = IS_I830(dev) ? 16 * 1024 : 256;
14261 ret = i915_gem_object_attach_phys(obj, align);
14262 if (ret)
14263 DRM_DEBUG_KMS("failed to attach phys object\n");
14264 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014265 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014266 }
14267
Chris Wilsonc37efb92016-06-17 08:28:47 +010014268 if (ret == 0) {
Chris Wilson27c01aa2016-08-04 07:52:30 +010014269 to_intel_plane_state(new_state)->wait_req =
Chris Wilsond72d9082016-08-04 07:52:31 +010014270 i915_gem_active_get(&obj->last_write,
14271 &obj->base.dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014272 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014273
Matt Roper6beb8c232014-12-01 15:40:14 -080014274 return ret;
14275}
14276
Matt Roper38f3ce32014-12-02 07:45:25 -080014277/**
14278 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14279 * @plane: drm plane to clean up for
14280 * @fb: old framebuffer that was on plane
14281 *
14282 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014283 *
14284 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014285 */
14286void
14287intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014288 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014289{
14290 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014291 struct intel_plane_state *old_intel_state;
Keith Packard84978252016-07-31 00:54:51 -070014292 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014293 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14294 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014295
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014296 old_intel_state = to_intel_plane_state(old_state);
14297
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014298 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014299 return;
14300
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014301 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14302 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014303 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014304
Keith Packard84978252016-07-31 00:54:51 -070014305 i915_gem_request_assign(&intel_state->wait_req, NULL);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014306 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014307}
14308
Chandra Konduru6156a452015-04-27 13:48:39 -070014309int
14310skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14311{
14312 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014313 int crtc_clock, cdclk;
14314
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014315 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014316 return DRM_PLANE_HELPER_NO_SCALING;
14317
Chandra Konduru6156a452015-04-27 13:48:39 -070014318 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014319 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014320
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014321 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014322 return DRM_PLANE_HELPER_NO_SCALING;
14323
14324 /*
14325 * skl max scale is lower of:
14326 * close to 3 but not 3, -1 is for that purpose
14327 * or
14328 * cdclk/crtc_clock
14329 */
14330 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14331
14332 return max_scale;
14333}
14334
Matt Roper465c1202014-05-29 08:06:54 -070014335static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014336intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014337 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014338 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014339{
Matt Roper2b875c22014-12-01 15:40:13 -080014340 struct drm_crtc *crtc = state->base.crtc;
14341 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014342 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014343 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14344 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014345
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014346 if (INTEL_INFO(plane->dev)->gen >= 9) {
14347 /* use scaler when colorkey is not required */
14348 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14349 min_scale = 1;
14350 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14351 }
Sonika Jindald8106362015-04-10 14:37:28 +053014352 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014353 }
Sonika Jindald8106362015-04-10 14:37:28 +053014354
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014355 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14356 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014357 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014358 min_scale, max_scale,
14359 can_position, true,
14360 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014361}
14362
Daniel Vetter5a21b662016-05-24 17:13:53 +020014363static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14364 struct drm_crtc_state *old_crtc_state)
14365{
14366 struct drm_device *dev = crtc->dev;
14367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14368 struct intel_crtc_state *old_intel_state =
14369 to_intel_crtc_state(old_crtc_state);
14370 bool modeset = needs_modeset(crtc->state);
14371
14372 /* Perform vblank evasion around commit operation */
14373 intel_pipe_update_start(intel_crtc);
14374
14375 if (modeset)
14376 return;
14377
14378 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14379 intel_color_set_csc(crtc->state);
14380 intel_color_load_luts(crtc->state);
14381 }
14382
14383 if (to_intel_crtc_state(crtc->state)->update_pipe)
14384 intel_update_pipe_config(intel_crtc, old_intel_state);
14385 else if (INTEL_INFO(dev)->gen >= 9)
14386 skl_detach_scalers(intel_crtc);
14387}
14388
14389static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14390 struct drm_crtc_state *old_crtc_state)
14391{
14392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14393
14394 intel_pipe_update_end(intel_crtc, NULL);
14395}
14396
Matt Ropercf4c7c12014-12-04 10:27:42 -080014397/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014398 * intel_plane_destroy - destroy a plane
14399 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014400 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014401 * Common destruction function for all types of planes (primary, cursor,
14402 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014403 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014404void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014405{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014406 if (!plane)
14407 return;
14408
Matt Roper465c1202014-05-29 08:06:54 -070014409 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014410 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014411}
14412
Matt Roper65a3fea2015-01-21 16:35:42 -080014413const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014414 .update_plane = drm_atomic_helper_update_plane,
14415 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014416 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014417 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014418 .atomic_get_property = intel_plane_atomic_get_property,
14419 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014420 .atomic_duplicate_state = intel_plane_duplicate_state,
14421 .atomic_destroy_state = intel_plane_destroy_state,
14422
Matt Roper465c1202014-05-29 08:06:54 -070014423};
14424
14425static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14426 int pipe)
14427{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014428 struct intel_plane *primary = NULL;
14429 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014430 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014431 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014432 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014433
14434 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014435 if (!primary)
14436 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014437
Matt Roper8e7d6882015-01-21 16:35:41 -080014438 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014439 if (!state)
14440 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014441 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014442
Matt Roper465c1202014-05-29 08:06:54 -070014443 primary->can_scale = false;
14444 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014445 if (INTEL_INFO(dev)->gen >= 9) {
14446 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014447 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014448 }
Matt Roper465c1202014-05-29 08:06:54 -070014449 primary->pipe = pipe;
14450 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014451 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014452 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014453 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14454 primary->plane = !pipe;
14455
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014456 if (INTEL_INFO(dev)->gen >= 9) {
14457 intel_primary_formats = skl_primary_formats;
14458 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014459
14460 primary->update_plane = skylake_update_primary_plane;
14461 primary->disable_plane = skylake_disable_primary_plane;
14462 } else if (HAS_PCH_SPLIT(dev)) {
14463 intel_primary_formats = i965_primary_formats;
14464 num_formats = ARRAY_SIZE(i965_primary_formats);
14465
14466 primary->update_plane = ironlake_update_primary_plane;
14467 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014468 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014469 intel_primary_formats = i965_primary_formats;
14470 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014471
14472 primary->update_plane = i9xx_update_primary_plane;
14473 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014474 } else {
14475 intel_primary_formats = i8xx_primary_formats;
14476 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014477
14478 primary->update_plane = i9xx_update_primary_plane;
14479 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014480 }
14481
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014482 if (INTEL_INFO(dev)->gen >= 9)
14483 ret = drm_universal_plane_init(dev, &primary->base, 0,
14484 &intel_plane_funcs,
14485 intel_primary_formats, num_formats,
14486 DRM_PLANE_TYPE_PRIMARY,
14487 "plane 1%c", pipe_name(pipe));
14488 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14489 ret = drm_universal_plane_init(dev, &primary->base, 0,
14490 &intel_plane_funcs,
14491 intel_primary_formats, num_formats,
14492 DRM_PLANE_TYPE_PRIMARY,
14493 "primary %c", pipe_name(pipe));
14494 else
14495 ret = drm_universal_plane_init(dev, &primary->base, 0,
14496 &intel_plane_funcs,
14497 intel_primary_formats, num_formats,
14498 DRM_PLANE_TYPE_PRIMARY,
14499 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014500 if (ret)
14501 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014502
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014503 if (INTEL_INFO(dev)->gen >= 4)
14504 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014505
Matt Roperea2c67b2014-12-23 10:41:52 -080014506 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14507
Matt Roper465c1202014-05-29 08:06:54 -070014508 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014509
14510fail:
14511 kfree(state);
14512 kfree(primary);
14513
14514 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014515}
14516
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014517void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14518{
14519 if (!dev->mode_config.rotation_property) {
14520 unsigned long flags = BIT(DRM_ROTATE_0) |
14521 BIT(DRM_ROTATE_180);
14522
14523 if (INTEL_INFO(dev)->gen >= 9)
14524 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14525
14526 dev->mode_config.rotation_property =
14527 drm_mode_create_rotation_property(dev, flags);
14528 }
14529 if (dev->mode_config.rotation_property)
14530 drm_object_attach_property(&plane->base.base,
14531 dev->mode_config.rotation_property,
14532 plane->base.state->rotation);
14533}
14534
Matt Roper3d7d6512014-06-10 08:28:13 -070014535static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014536intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014537 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014538 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014539{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014540 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014541 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014542 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014543 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014544 unsigned stride;
14545 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014546
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014547 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14548 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014549 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014550 DRM_PLANE_HELPER_NO_SCALING,
14551 DRM_PLANE_HELPER_NO_SCALING,
14552 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014553 if (ret)
14554 return ret;
14555
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014556 /* if we want to turn off the cursor ignore width and height */
14557 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014558 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014559
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014560 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014561 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014562 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14563 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014564 return -EINVAL;
14565 }
14566
Matt Roperea2c67b2014-12-23 10:41:52 -080014567 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14568 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014569 DRM_DEBUG_KMS("buffer is too small\n");
14570 return -ENOMEM;
14571 }
14572
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014573 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014574 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014575 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014576 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014577
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014578 /*
14579 * There's something wrong with the cursor on CHV pipe C.
14580 * If it straddles the left edge of the screen then
14581 * moving it away from the edge or disabling it often
14582 * results in a pipe underrun, and often that can lead to
14583 * dead pipe (constant underrun reported, and it scans
14584 * out just a solid color). To recover from that, the
14585 * display power well must be turned off and on again.
14586 * Refuse the put the cursor into that compromised position.
14587 */
14588 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14589 state->visible && state->base.crtc_x < 0) {
14590 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14591 return -EINVAL;
14592 }
14593
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014594 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014595}
14596
Matt Roperf4a2cf22014-12-01 15:40:12 -080014597static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014598intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014599 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014600{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14602
14603 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014604 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014605}
14606
14607static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014608intel_update_cursor_plane(struct drm_plane *plane,
14609 const struct intel_crtc_state *crtc_state,
14610 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014611{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014612 struct drm_crtc *crtc = crtc_state->base.crtc;
14613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014614 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014615 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014616 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014617
Matt Roperf4a2cf22014-12-01 15:40:12 -080014618 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014619 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014620 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014621 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014622 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014623 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014624
Gustavo Padovana912f122014-12-01 15:40:10 -080014625 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014626 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014627}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014628
Matt Roper3d7d6512014-06-10 08:28:13 -070014629static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14630 int pipe)
14631{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014632 struct intel_plane *cursor = NULL;
14633 struct intel_plane_state *state = NULL;
14634 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014635
14636 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014637 if (!cursor)
14638 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014639
Matt Roper8e7d6882015-01-21 16:35:41 -080014640 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014641 if (!state)
14642 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014643 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014644
Matt Roper3d7d6512014-06-10 08:28:13 -070014645 cursor->can_scale = false;
14646 cursor->max_downscale = 1;
14647 cursor->pipe = pipe;
14648 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014649 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014650 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014651 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014652 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014653
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014654 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14655 &intel_plane_funcs,
14656 intel_cursor_formats,
14657 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014658 DRM_PLANE_TYPE_CURSOR,
14659 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014660 if (ret)
14661 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014662
14663 if (INTEL_INFO(dev)->gen >= 4) {
14664 if (!dev->mode_config.rotation_property)
14665 dev->mode_config.rotation_property =
14666 drm_mode_create_rotation_property(dev,
14667 BIT(DRM_ROTATE_0) |
14668 BIT(DRM_ROTATE_180));
14669 if (dev->mode_config.rotation_property)
14670 drm_object_attach_property(&cursor->base.base,
14671 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014672 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014673 }
14674
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014675 if (INTEL_INFO(dev)->gen >=9)
14676 state->scaler_id = -1;
14677
Matt Roperea2c67b2014-12-23 10:41:52 -080014678 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14679
Matt Roper3d7d6512014-06-10 08:28:13 -070014680 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014681
14682fail:
14683 kfree(state);
14684 kfree(cursor);
14685
14686 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014687}
14688
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014689static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14690 struct intel_crtc_state *crtc_state)
14691{
14692 int i;
14693 struct intel_scaler *intel_scaler;
14694 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14695
14696 for (i = 0; i < intel_crtc->num_scalers; i++) {
14697 intel_scaler = &scaler_state->scalers[i];
14698 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014699 intel_scaler->mode = PS_SCALER_MODE_DYN;
14700 }
14701
14702 scaler_state->scaler_id = -1;
14703}
14704
Hannes Ederb358d0a2008-12-18 21:18:47 +010014705static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014706{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014707 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014708 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014709 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014710 struct drm_plane *primary = NULL;
14711 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014712 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014713
Daniel Vetter955382f2013-09-19 14:05:45 +020014714 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014715 if (intel_crtc == NULL)
14716 return;
14717
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014718 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14719 if (!crtc_state)
14720 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014721 intel_crtc->config = crtc_state;
14722 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014723 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014724
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014725 /* initialize shared scalers */
14726 if (INTEL_INFO(dev)->gen >= 9) {
14727 if (pipe == PIPE_C)
14728 intel_crtc->num_scalers = 1;
14729 else
14730 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14731
14732 skl_init_scalers(dev, intel_crtc, crtc_state);
14733 }
14734
Matt Roper465c1202014-05-29 08:06:54 -070014735 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014736 if (!primary)
14737 goto fail;
14738
14739 cursor = intel_cursor_plane_create(dev, pipe);
14740 if (!cursor)
14741 goto fail;
14742
Matt Roper465c1202014-05-29 08:06:54 -070014743 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014744 cursor, &intel_crtc_funcs,
14745 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014746 if (ret)
14747 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014748
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014749 /*
14750 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014751 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014752 */
Jesse Barnes80824002009-09-10 15:28:06 -070014753 intel_crtc->pipe = pipe;
14754 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014755 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014756 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014757 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014758 }
14759
Chris Wilson4b0e3332014-05-30 16:35:26 +030014760 intel_crtc->cursor_base = ~0;
14761 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014762 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014763
Ville Syrjälä852eb002015-06-24 22:00:07 +030014764 intel_crtc->wm.cxsr_allowed = true;
14765
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014766 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14767 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14768 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14769 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14770
Jesse Barnes79e53942008-11-07 14:24:08 -080014771 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014772
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014773 intel_color_init(&intel_crtc->base);
14774
Daniel Vetter87b6b102014-05-15 15:33:46 +020014775 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014776 return;
14777
14778fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014779 intel_plane_destroy(primary);
14780 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014781 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014782 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014783}
14784
Jesse Barnes752aa882013-10-31 18:55:49 +020014785enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14786{
14787 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014788 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014789
Rob Clark51fd3712013-11-19 12:10:12 -050014790 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014791
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014792 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014793 return INVALID_PIPE;
14794
14795 return to_intel_crtc(encoder->crtc)->pipe;
14796}
14797
Carl Worth08d7b3d2009-04-29 14:43:54 -070014798int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014799 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014800{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014801 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014802 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014803 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014804
Rob Clark7707e652014-07-17 23:30:04 -040014805 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014806 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014807 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014808
Rob Clark7707e652014-07-17 23:30:04 -040014809 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014810 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014811
Daniel Vetterc05422d2009-08-11 16:05:30 +020014812 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014813}
14814
Daniel Vetter66a92782012-07-12 20:08:18 +020014815static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014816{
Daniel Vetter66a92782012-07-12 20:08:18 +020014817 struct drm_device *dev = encoder->base.dev;
14818 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014819 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014820 int entry = 0;
14821
Damien Lespiaub2784e12014-08-05 11:29:37 +010014822 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014823 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014824 index_mask |= (1 << entry);
14825
Jesse Barnes79e53942008-11-07 14:24:08 -080014826 entry++;
14827 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014828
Jesse Barnes79e53942008-11-07 14:24:08 -080014829 return index_mask;
14830}
14831
Chris Wilson4d302442010-12-14 19:21:29 +000014832static bool has_edp_a(struct drm_device *dev)
14833{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014834 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000014835
14836 if (!IS_MOBILE(dev))
14837 return false;
14838
14839 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14840 return false;
14841
Damien Lespiaue3589902014-02-07 19:12:50 +000014842 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014843 return false;
14844
14845 return true;
14846}
14847
Jesse Barnes84b4e042014-06-25 08:24:29 -070014848static bool intel_crt_present(struct drm_device *dev)
14849{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014850 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070014851
Damien Lespiau884497e2013-12-03 13:56:23 +000014852 if (INTEL_INFO(dev)->gen >= 9)
14853 return false;
14854
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014855 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014856 return false;
14857
14858 if (IS_CHERRYVIEW(dev))
14859 return false;
14860
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014861 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14862 return false;
14863
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014864 /* DDI E can't be used if DDI A requires 4 lanes */
14865 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14866 return false;
14867
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014868 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014869 return false;
14870
14871 return true;
14872}
14873
Imre Deak8090ba82016-08-10 14:07:33 +030014874void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14875{
14876 int pps_num;
14877 int pps_idx;
14878
14879 if (HAS_DDI(dev_priv))
14880 return;
14881 /*
14882 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14883 * everywhere where registers can be write protected.
14884 */
14885 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14886 pps_num = 2;
14887 else
14888 pps_num = 1;
14889
14890 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14891 u32 val = I915_READ(PP_CONTROL(pps_idx));
14892
14893 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14894 I915_WRITE(PP_CONTROL(pps_idx), val);
14895 }
14896}
14897
Imre Deak44cb7342016-08-10 14:07:29 +030014898static void intel_pps_init(struct drm_i915_private *dev_priv)
14899{
14900 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
14901 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14902 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14903 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14904 else
14905 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030014906
14907 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030014908}
14909
Jesse Barnes79e53942008-11-07 14:24:08 -080014910static void intel_setup_outputs(struct drm_device *dev)
14911{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014912 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010014913 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014914 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014915
Imre Deak44cb7342016-08-10 14:07:29 +030014916 intel_pps_init(dev_priv);
14917
Imre Deak97a824e12016-06-21 11:51:47 +030014918 /*
14919 * intel_edp_init_connector() depends on this completing first, to
14920 * prevent the registeration of both eDP and LVDS and the incorrect
14921 * sharing of the PPS.
14922 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014923 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014924
Jesse Barnes84b4e042014-06-25 08:24:29 -070014925 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014926 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014927
Vandana Kannanc776eb22014-08-19 12:05:01 +053014928 if (IS_BROXTON(dev)) {
14929 /*
14930 * FIXME: Broxton doesn't support port detection via the
14931 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14932 * detect the ports.
14933 */
14934 intel_ddi_init(dev, PORT_A);
14935 intel_ddi_init(dev, PORT_B);
14936 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014937
14938 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014939 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014940 int found;
14941
Jesse Barnesde31fac2015-03-06 15:53:32 -080014942 /*
14943 * Haswell uses DDI functions to detect digital outputs.
14944 * On SKL pre-D0 the strap isn't connected, so we assume
14945 * it's there.
14946 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014947 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014948 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014949 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014950 intel_ddi_init(dev, PORT_A);
14951
14952 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14953 * register */
14954 found = I915_READ(SFUSE_STRAP);
14955
14956 if (found & SFUSE_STRAP_DDIB_DETECTED)
14957 intel_ddi_init(dev, PORT_B);
14958 if (found & SFUSE_STRAP_DDIC_DETECTED)
14959 intel_ddi_init(dev, PORT_C);
14960 if (found & SFUSE_STRAP_DDID_DETECTED)
14961 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014962 /*
14963 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14964 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014965 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014966 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14967 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14968 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14969 intel_ddi_init(dev, PORT_E);
14970
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014971 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014972 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014973 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014974
14975 if (has_edp_a(dev))
14976 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014977
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014978 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014979 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014980 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014981 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014982 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014983 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014984 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014985 }
14986
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014987 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014988 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014989
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014990 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014991 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014992
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014993 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014994 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014995
Daniel Vetter270b3042012-10-27 15:52:05 +020014996 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014997 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014998 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014999 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010015000
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015001 /*
15002 * The DP_DETECTED bit is the latched state of the DDC
15003 * SDA pin at boot. However since eDP doesn't require DDC
15004 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15005 * eDP ports may have been muxed to an alternate function.
15006 * Thus we can't rely on the DP_DETECTED bit alone to detect
15007 * eDP ports. Consult the VBT as well as DP_DETECTED to
15008 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030015009 *
15010 * Sadly the straps seem to be missing sometimes even for HDMI
15011 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15012 * and VBT for the presence of the port. Additionally we can't
15013 * trust the port type the VBT declares as we've seen at least
15014 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015015 */
Chris Wilson457c52d2016-06-01 08:27:50 +010015016 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015017 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15018 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010015019 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015020 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015021 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030015022
Chris Wilson457c52d2016-06-01 08:27:50 +010015023 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015024 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15025 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010015026 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015027 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015028 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053015029
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030015030 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015031 /*
15032 * eDP not supported on port D,
15033 * so no need to worry about it
15034 */
15035 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15036 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015037 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015038 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15039 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030015040 }
15041
Jani Nikula3cfca972013-08-27 15:12:26 +030015042 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020015043 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015044 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080015045
Paulo Zanonie2debe92013-02-18 19:00:27 -030015046 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015047 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015048 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020015049 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015050 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030015051 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015052 }
Ma Ling27185ae2009-08-24 13:50:23 +080015053
Daniel Vetter3fec3d22015-07-07 09:10:07 +020015054 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015055 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080015056 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015057
15058 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015059
Paulo Zanonie2debe92013-02-18 19:00:27 -030015060 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015061 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015062 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015063 }
Ma Ling27185ae2009-08-24 13:50:23 +080015064
Paulo Zanonie2debe92013-02-18 19:00:27 -030015065 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015066
Daniel Vetter3fec3d22015-07-07 09:10:07 +020015067 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015068 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030015069 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015070 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020015071 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015072 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080015073 }
Ma Ling27185ae2009-08-24 13:50:23 +080015074
Daniel Vetter3fec3d22015-07-07 09:10:07 +020015075 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030015076 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015077 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070015078 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080015079 intel_dvo_init(dev);
15080
Zhenyu Wang103a1962009-11-27 11:44:36 +080015081 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080015082 intel_tv_init(dev);
15083
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080015084 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070015085
Damien Lespiaub2784e12014-08-05 11:29:37 +010015086 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010015087 encoder->base.possible_crtcs = encoder->crtc_mask;
15088 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020015089 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080015090 }
Chris Wilson47356eb2011-01-11 17:06:04 +000015091
Paulo Zanonidde86e22012-12-01 12:04:25 -020015092 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020015093
15094 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015095}
15096
15097static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15098{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015099 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080015100 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080015101
Daniel Vetteref2d6332014-02-10 18:00:38 +010015102 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015103 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010015104 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010015105 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015106 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015107 kfree(intel_fb);
15108}
15109
15110static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000015111 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080015112 unsigned int *handle)
15113{
15114 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000015115 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080015116
Chris Wilsoncc917ab2015-10-13 14:22:26 +010015117 if (obj->userptr.mm) {
15118 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15119 return -EINVAL;
15120 }
15121
Chris Wilson05394f32010-11-08 19:18:58 +000015122 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080015123}
15124
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015125static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15126 struct drm_file *file,
15127 unsigned flags, unsigned color,
15128 struct drm_clip_rect *clips,
15129 unsigned num_clips)
15130{
15131 struct drm_device *dev = fb->dev;
15132 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15133 struct drm_i915_gem_object *obj = intel_fb->obj;
15134
15135 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030015136 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015137 mutex_unlock(&dev->struct_mutex);
15138
15139 return 0;
15140}
15141
Jesse Barnes79e53942008-11-07 14:24:08 -080015142static const struct drm_framebuffer_funcs intel_fb_funcs = {
15143 .destroy = intel_user_framebuffer_destroy,
15144 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015145 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080015146};
15147
Damien Lespiaub3218032015-02-27 11:15:18 +000015148static
15149u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
15150 uint32_t pixel_format)
15151{
15152 u32 gen = INTEL_INFO(dev)->gen;
15153
15154 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020015155 int cpp = drm_format_plane_cpp(pixel_format, 0);
15156
Damien Lespiaub3218032015-02-27 11:15:18 +000015157 /* "The stride in bytes must not exceed the of the size of 8K
15158 * pixels and 32K bytes."
15159 */
Ville Syrjäläac484962016-01-20 21:05:26 +020015160 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080015161 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015162 return 32*1024;
15163 } else if (gen >= 4) {
15164 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15165 return 16*1024;
15166 else
15167 return 32*1024;
15168 } else if (gen >= 3) {
15169 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15170 return 8*1024;
15171 else
15172 return 16*1024;
15173 } else {
15174 /* XXX DSPC is limited to 4k tiled */
15175 return 8*1024;
15176 }
15177}
15178
Daniel Vetterb5ea6422014-03-02 21:18:00 +010015179static int intel_framebuffer_init(struct drm_device *dev,
15180 struct intel_framebuffer *intel_fb,
15181 struct drm_mode_fb_cmd2 *mode_cmd,
15182 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015183{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015184 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015185 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015186 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080015187
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015188 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15189
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015190 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
15191 /* Enforce that fb modifier and tiling mode match, but only for
15192 * X-tiled. This is needed for FBC. */
Chris Wilson3e510a82016-08-05 10:14:23 +010015193 if (!!(i915_gem_object_get_tiling(obj) == I915_TILING_X) !=
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015194 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
15195 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15196 return -EINVAL;
15197 }
15198 } else {
Chris Wilson3e510a82016-08-05 10:14:23 +010015199 if (i915_gem_object_get_tiling(obj) == I915_TILING_X)
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015200 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Chris Wilson3e510a82016-08-05 10:14:23 +010015201 else if (i915_gem_object_get_tiling(obj) == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015202 DRM_DEBUG("No Y tiling for legacy addfb\n");
15203 return -EINVAL;
15204 }
15205 }
15206
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015207 /* Passed in modifier sanity checking. */
15208 switch (mode_cmd->modifier[0]) {
15209 case I915_FORMAT_MOD_Y_TILED:
15210 case I915_FORMAT_MOD_Yf_TILED:
15211 if (INTEL_INFO(dev)->gen < 9) {
15212 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15213 mode_cmd->modifier[0]);
15214 return -EINVAL;
15215 }
15216 case DRM_FORMAT_MOD_NONE:
15217 case I915_FORMAT_MOD_X_TILED:
15218 break;
15219 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015220 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15221 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015222 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015223 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015224
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015225 stride_alignment = intel_fb_stride_alignment(dev_priv,
15226 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015227 mode_cmd->pixel_format);
15228 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15229 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15230 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015231 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015232 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015233
Damien Lespiaub3218032015-02-27 11:15:18 +000015234 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15235 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015236 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015237 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15238 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015239 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015240 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015241 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015242 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015243
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015244 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilson3e510a82016-08-05 10:14:23 +010015245 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015246 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilson3e510a82016-08-05 10:14:23 +010015247 mode_cmd->pitches[0],
15248 i915_gem_object_get_stride(obj));
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015249 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015250 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015251
Ville Syrjälä57779d02012-10-31 17:50:14 +020015252 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015253 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015254 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015255 case DRM_FORMAT_RGB565:
15256 case DRM_FORMAT_XRGB8888:
15257 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015258 break;
15259 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015260 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015261 DRM_DEBUG("unsupported pixel format: %s\n",
15262 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015263 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015264 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015265 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015266 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015267 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15268 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015269 DRM_DEBUG("unsupported pixel format: %s\n",
15270 drm_get_format_name(mode_cmd->pixel_format));
15271 return -EINVAL;
15272 }
15273 break;
15274 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015275 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015276 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015277 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015278 DRM_DEBUG("unsupported pixel format: %s\n",
15279 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015280 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015281 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015282 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015283 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015284 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015285 DRM_DEBUG("unsupported pixel format: %s\n",
15286 drm_get_format_name(mode_cmd->pixel_format));
15287 return -EINVAL;
15288 }
15289 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015290 case DRM_FORMAT_YUYV:
15291 case DRM_FORMAT_UYVY:
15292 case DRM_FORMAT_YVYU:
15293 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015294 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015295 DRM_DEBUG("unsupported pixel format: %s\n",
15296 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015297 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015298 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015299 break;
15300 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015301 DRM_DEBUG("unsupported pixel format: %s\n",
15302 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015303 return -EINVAL;
15304 }
15305
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015306 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15307 if (mode_cmd->offsets[0] != 0)
15308 return -EINVAL;
15309
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015310 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15311 intel_fb->obj = obj;
15312
Ville Syrjälä6687c902015-09-15 13:16:41 +030015313 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15314 if (ret)
15315 return ret;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015316
Jesse Barnes79e53942008-11-07 14:24:08 -080015317 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15318 if (ret) {
15319 DRM_ERROR("framebuffer init failed %d\n", ret);
15320 return ret;
15321 }
15322
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015323 intel_fb->obj->framebuffer_references++;
15324
Jesse Barnes79e53942008-11-07 14:24:08 -080015325 return 0;
15326}
15327
Jesse Barnes79e53942008-11-07 14:24:08 -080015328static struct drm_framebuffer *
15329intel_user_framebuffer_create(struct drm_device *dev,
15330 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015331 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015332{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015333 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015334 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015335 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015336
Chris Wilson03ac0642016-07-20 13:31:51 +010015337 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15338 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015339 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015340
Daniel Vetter92907cb2015-11-23 09:04:05 +010015341 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015342 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010015343 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015344
15345 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015346}
15347
Daniel Vetter06957262015-08-10 13:34:08 +020015348#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015349static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015350{
15351}
15352#endif
15353
Jesse Barnes79e53942008-11-07 14:24:08 -080015354static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015355 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015356 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015357 .atomic_check = intel_atomic_check,
15358 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015359 .atomic_state_alloc = intel_atomic_state_alloc,
15360 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015361};
15362
Imre Deak88212942016-03-16 13:38:53 +020015363/**
15364 * intel_init_display_hooks - initialize the display modesetting hooks
15365 * @dev_priv: device private
15366 */
15367void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015368{
Imre Deak88212942016-03-16 13:38:53 +020015369 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015370 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015371 dev_priv->display.get_initial_plane_config =
15372 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015373 dev_priv->display.crtc_compute_clock =
15374 haswell_crtc_compute_clock;
15375 dev_priv->display.crtc_enable = haswell_crtc_enable;
15376 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015377 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015378 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015379 dev_priv->display.get_initial_plane_config =
15380 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015381 dev_priv->display.crtc_compute_clock =
15382 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015383 dev_priv->display.crtc_enable = haswell_crtc_enable;
15384 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015385 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015386 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015387 dev_priv->display.get_initial_plane_config =
15388 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015389 dev_priv->display.crtc_compute_clock =
15390 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015391 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15392 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015393 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015394 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015395 dev_priv->display.get_initial_plane_config =
15396 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015397 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15398 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15399 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15400 } else if (IS_VALLEYVIEW(dev_priv)) {
15401 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15402 dev_priv->display.get_initial_plane_config =
15403 i9xx_get_initial_plane_config;
15404 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015405 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15406 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015407 } else if (IS_G4X(dev_priv)) {
15408 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15409 dev_priv->display.get_initial_plane_config =
15410 i9xx_get_initial_plane_config;
15411 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15412 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15413 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015414 } else if (IS_PINEVIEW(dev_priv)) {
15415 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15416 dev_priv->display.get_initial_plane_config =
15417 i9xx_get_initial_plane_config;
15418 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15419 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15420 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015421 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015422 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015423 dev_priv->display.get_initial_plane_config =
15424 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015425 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015426 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15427 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015428 } else {
15429 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15430 dev_priv->display.get_initial_plane_config =
15431 i9xx_get_initial_plane_config;
15432 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15433 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15434 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015435 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015436
Jesse Barnese70236a2009-09-21 10:42:27 -070015437 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015438 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015439 dev_priv->display.get_display_clock_speed =
15440 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015441 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015442 dev_priv->display.get_display_clock_speed =
15443 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015444 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015445 dev_priv->display.get_display_clock_speed =
15446 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015447 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015448 dev_priv->display.get_display_clock_speed =
15449 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015450 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015451 dev_priv->display.get_display_clock_speed =
15452 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015453 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015454 dev_priv->display.get_display_clock_speed =
15455 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015456 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15457 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015458 dev_priv->display.get_display_clock_speed =
15459 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015460 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015461 dev_priv->display.get_display_clock_speed =
15462 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015463 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015464 dev_priv->display.get_display_clock_speed =
15465 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015466 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015467 dev_priv->display.get_display_clock_speed =
15468 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015469 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015470 dev_priv->display.get_display_clock_speed =
15471 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015472 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015473 dev_priv->display.get_display_clock_speed =
15474 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015475 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015476 dev_priv->display.get_display_clock_speed =
15477 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015478 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015479 dev_priv->display.get_display_clock_speed =
15480 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015481 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015482 dev_priv->display.get_display_clock_speed =
15483 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015484 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015485 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015486 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015487 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015488 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015489 dev_priv->display.get_display_clock_speed =
15490 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015491 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015492
Imre Deak88212942016-03-16 13:38:53 +020015493 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015494 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015495 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015496 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015497 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015498 /* FIXME: detect B0+ stepping and use auto training */
15499 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015500 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015501 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015502 }
15503
15504 if (IS_BROADWELL(dev_priv)) {
15505 dev_priv->display.modeset_commit_cdclk =
15506 broadwell_modeset_commit_cdclk;
15507 dev_priv->display.modeset_calc_cdclk =
15508 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015509 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015510 dev_priv->display.modeset_commit_cdclk =
15511 valleyview_modeset_commit_cdclk;
15512 dev_priv->display.modeset_calc_cdclk =
15513 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015514 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015515 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015516 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015517 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015518 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015519 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15520 dev_priv->display.modeset_commit_cdclk =
15521 skl_modeset_commit_cdclk;
15522 dev_priv->display.modeset_calc_cdclk =
15523 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015524 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015525
15526 switch (INTEL_INFO(dev_priv)->gen) {
15527 case 2:
15528 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15529 break;
15530
15531 case 3:
15532 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15533 break;
15534
15535 case 4:
15536 case 5:
15537 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15538 break;
15539
15540 case 6:
15541 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15542 break;
15543 case 7:
15544 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15545 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15546 break;
15547 case 9:
15548 /* Drop through - unsupported since execlist only. */
15549 default:
15550 /* Default just returns -ENODEV to indicate unsupported */
15551 dev_priv->display.queue_flip = intel_default_queue_flip;
15552 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015553}
15554
Jesse Barnesb690e962010-07-19 13:53:12 -070015555/*
15556 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15557 * resume, or other times. This quirk makes sure that's the case for
15558 * affected systems.
15559 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015560static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015561{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015562 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070015563
15564 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015565 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015566}
15567
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015568static void quirk_pipeb_force(struct drm_device *dev)
15569{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015570 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015571
15572 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15573 DRM_INFO("applying pipe b force quirk\n");
15574}
15575
Keith Packard435793d2011-07-12 14:56:22 -070015576/*
15577 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15578 */
15579static void quirk_ssc_force_disable(struct drm_device *dev)
15580{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015581 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070015582 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015583 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015584}
15585
Carsten Emde4dca20e2012-03-15 15:56:26 +010015586/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015587 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15588 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015589 */
15590static void quirk_invert_brightness(struct drm_device *dev)
15591{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015592 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010015593 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015594 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015595}
15596
Scot Doyle9c72cc62014-07-03 23:27:50 +000015597/* Some VBT's incorrectly indicate no backlight is present */
15598static void quirk_backlight_present(struct drm_device *dev)
15599{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015600 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000015601 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15602 DRM_INFO("applying backlight present quirk\n");
15603}
15604
Jesse Barnesb690e962010-07-19 13:53:12 -070015605struct intel_quirk {
15606 int device;
15607 int subsystem_vendor;
15608 int subsystem_device;
15609 void (*hook)(struct drm_device *dev);
15610};
15611
Egbert Eich5f85f172012-10-14 15:46:38 +020015612/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15613struct intel_dmi_quirk {
15614 void (*hook)(struct drm_device *dev);
15615 const struct dmi_system_id (*dmi_id_list)[];
15616};
15617
15618static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15619{
15620 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15621 return 1;
15622}
15623
15624static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15625 {
15626 .dmi_id_list = &(const struct dmi_system_id[]) {
15627 {
15628 .callback = intel_dmi_reverse_brightness,
15629 .ident = "NCR Corporation",
15630 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15631 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15632 },
15633 },
15634 { } /* terminating entry */
15635 },
15636 .hook = quirk_invert_brightness,
15637 },
15638};
15639
Ben Widawskyc43b5632012-04-16 14:07:40 -070015640static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015641 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15642 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15643
Jesse Barnesb690e962010-07-19 13:53:12 -070015644 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15645 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15646
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015647 /* 830 needs to leave pipe A & dpll A up */
15648 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15649
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015650 /* 830 needs to leave pipe B & dpll B up */
15651 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15652
Keith Packard435793d2011-07-12 14:56:22 -070015653 /* Lenovo U160 cannot use SSC on LVDS */
15654 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015655
15656 /* Sony Vaio Y cannot use SSC on LVDS */
15657 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015658
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015659 /* Acer Aspire 5734Z must invert backlight brightness */
15660 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15661
15662 /* Acer/eMachines G725 */
15663 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15664
15665 /* Acer/eMachines e725 */
15666 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15667
15668 /* Acer/Packard Bell NCL20 */
15669 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15670
15671 /* Acer Aspire 4736Z */
15672 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015673
15674 /* Acer Aspire 5336 */
15675 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015676
15677 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15678 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015679
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015680 /* Acer C720 Chromebook (Core i3 4005U) */
15681 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15682
jens steinb2a96012014-10-28 20:25:53 +010015683 /* Apple Macbook 2,1 (Core 2 T7400) */
15684 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15685
Jani Nikula1b9448b2015-11-05 11:49:59 +020015686 /* Apple Macbook 4,1 */
15687 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15688
Scot Doyled4967d82014-07-03 23:27:52 +000015689 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15690 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015691
15692 /* HP Chromebook 14 (Celeron 2955U) */
15693 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015694
15695 /* Dell Chromebook 11 */
15696 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015697
15698 /* Dell Chromebook 11 (2015 version) */
15699 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015700};
15701
15702static void intel_init_quirks(struct drm_device *dev)
15703{
15704 struct pci_dev *d = dev->pdev;
15705 int i;
15706
15707 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15708 struct intel_quirk *q = &intel_quirks[i];
15709
15710 if (d->device == q->device &&
15711 (d->subsystem_vendor == q->subsystem_vendor ||
15712 q->subsystem_vendor == PCI_ANY_ID) &&
15713 (d->subsystem_device == q->subsystem_device ||
15714 q->subsystem_device == PCI_ANY_ID))
15715 q->hook(dev);
15716 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015717 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15718 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15719 intel_dmi_quirks[i].hook(dev);
15720 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015721}
15722
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015723/* Disable the VGA plane that we never use */
15724static void i915_disable_vga(struct drm_device *dev)
15725{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015726 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015727 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015728 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015729
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015730 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015731 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015732 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015733 sr1 = inb(VGA_SR_DATA);
15734 outb(sr1 | 1<<5, VGA_SR_DATA);
15735 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15736 udelay(300);
15737
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015738 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015739 POSTING_READ(vga_reg);
15740}
15741
Daniel Vetterf8175862012-04-10 15:50:11 +020015742void intel_modeset_init_hw(struct drm_device *dev)
15743{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015744 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015745
Ville Syrjäläb6283052015-06-03 15:45:07 +030015746 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015747
15748 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15749
Daniel Vetterf8175862012-04-10 15:50:11 +020015750 intel_init_clock_gating(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015751}
15752
Matt Roperd93c0372015-12-03 11:37:41 -080015753/*
15754 * Calculate what we think the watermarks should be for the state we've read
15755 * out of the hardware and then immediately program those watermarks so that
15756 * we ensure the hardware settings match our internal state.
15757 *
15758 * We can calculate what we think WM's should be by creating a duplicate of the
15759 * current state (which was constructed during hardware readout) and running it
15760 * through the atomic check code to calculate new watermark values in the
15761 * state object.
15762 */
15763static void sanitize_watermarks(struct drm_device *dev)
15764{
15765 struct drm_i915_private *dev_priv = to_i915(dev);
15766 struct drm_atomic_state *state;
15767 struct drm_crtc *crtc;
15768 struct drm_crtc_state *cstate;
15769 struct drm_modeset_acquire_ctx ctx;
15770 int ret;
15771 int i;
15772
15773 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015774 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015775 return;
15776
15777 /*
15778 * We need to hold connection_mutex before calling duplicate_state so
15779 * that the connector loop is protected.
15780 */
15781 drm_modeset_acquire_init(&ctx, 0);
15782retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015783 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015784 if (ret == -EDEADLK) {
15785 drm_modeset_backoff(&ctx);
15786 goto retry;
15787 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015788 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015789 }
15790
15791 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15792 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015793 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015794
Matt Ropered4a6a72016-02-23 17:20:13 -080015795 /*
15796 * Hardware readout is the only time we don't want to calculate
15797 * intermediate watermarks (since we don't trust the current
15798 * watermarks).
15799 */
15800 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15801
Matt Roperd93c0372015-12-03 11:37:41 -080015802 ret = intel_atomic_check(dev, state);
15803 if (ret) {
15804 /*
15805 * If we fail here, it means that the hardware appears to be
15806 * programmed in a way that shouldn't be possible, given our
15807 * understanding of watermark requirements. This might mean a
15808 * mistake in the hardware readout code or a mistake in the
15809 * watermark calculations for a given platform. Raise a WARN
15810 * so that this is noticeable.
15811 *
15812 * If this actually happens, we'll have to just leave the
15813 * BIOS-programmed watermarks untouched and hope for the best.
15814 */
15815 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015816 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015817 }
15818
15819 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015820 for_each_crtc_in_state(state, crtc, cstate, i) {
15821 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15822
Matt Ropered4a6a72016-02-23 17:20:13 -080015823 cs->wm.need_postvbl_update = true;
15824 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015825 }
15826
15827 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015828fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015829 drm_modeset_drop_locks(&ctx);
15830 drm_modeset_acquire_fini(&ctx);
15831}
15832
Jesse Barnes79e53942008-11-07 14:24:08 -080015833void intel_modeset_init(struct drm_device *dev)
15834{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015835 struct drm_i915_private *dev_priv = to_i915(dev);
15836 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015837 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015838 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015839 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015840
15841 drm_mode_config_init(dev);
15842
15843 dev->mode_config.min_width = 0;
15844 dev->mode_config.min_height = 0;
15845
Dave Airlie019d96c2011-09-29 16:20:42 +010015846 dev->mode_config.preferred_depth = 24;
15847 dev->mode_config.prefer_shadow = 1;
15848
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015849 dev->mode_config.allow_fb_modifiers = true;
15850
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015851 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015852
Jesse Barnesb690e962010-07-19 13:53:12 -070015853 intel_init_quirks(dev);
15854
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015855 intel_init_pm(dev);
15856
Ben Widawskye3c74752013-04-05 13:12:39 -070015857 if (INTEL_INFO(dev)->num_pipes == 0)
15858 return;
15859
Lukas Wunner69f92f62015-07-15 13:57:35 +020015860 /*
15861 * There may be no VBT; and if the BIOS enabled SSC we can
15862 * just keep using it to avoid unnecessary flicker. Whereas if the
15863 * BIOS isn't using it, don't assume it will work even if the VBT
15864 * indicates as much.
15865 */
15866 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15867 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15868 DREF_SSC1_ENABLE);
15869
15870 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15871 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15872 bios_lvds_use_ssc ? "en" : "dis",
15873 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15874 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15875 }
15876 }
15877
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015878 if (IS_GEN2(dev)) {
15879 dev->mode_config.max_width = 2048;
15880 dev->mode_config.max_height = 2048;
15881 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015882 dev->mode_config.max_width = 4096;
15883 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015884 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015885 dev->mode_config.max_width = 8192;
15886 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015887 }
Damien Lespiau068be562014-03-28 14:17:49 +000015888
Ville Syrjälädc41c152014-08-13 11:57:05 +030015889 if (IS_845G(dev) || IS_I865G(dev)) {
15890 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15891 dev->mode_config.cursor_height = 1023;
15892 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015893 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15894 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15895 } else {
15896 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15897 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15898 }
15899
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015900 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015901
Zhao Yakui28c97732009-10-09 11:39:41 +080015902 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015903 INTEL_INFO(dev)->num_pipes,
15904 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015905
Damien Lespiau055e3932014-08-18 13:49:10 +010015906 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015907 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015908 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015909 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015910 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015911 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015912 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015913 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015914 }
15915
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015916 intel_update_czclk(dev_priv);
15917 intel_update_cdclk(dev);
15918
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015919 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015920
Ville Syrjäläb2045352016-05-13 23:41:27 +030015921 if (dev_priv->max_cdclk_freq == 0)
15922 intel_update_max_cdclk(dev);
15923
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015924 /* Just disable it once at startup */
15925 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015926 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015927
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015928 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015929 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015930 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015931
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015932 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015933 struct intel_initial_plane_config plane_config = {};
15934
Jesse Barnes46f297f2014-03-07 08:57:48 -080015935 if (!crtc->active)
15936 continue;
15937
Jesse Barnes46f297f2014-03-07 08:57:48 -080015938 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015939 * Note that reserving the BIOS fb up front prevents us
15940 * from stuffing other stolen allocations like the ring
15941 * on top. This prevents some ugliness at boot time, and
15942 * can even allow for smooth boot transitions if the BIOS
15943 * fb is large enough for the active pipe configuration.
15944 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015945 dev_priv->display.get_initial_plane_config(crtc,
15946 &plane_config);
15947
15948 /*
15949 * If the fb is shared between multiple heads, we'll
15950 * just get the first one.
15951 */
15952 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015953 }
Matt Roperd93c0372015-12-03 11:37:41 -080015954
15955 /*
15956 * Make sure hardware watermarks really match the state we read out.
15957 * Note that we need to do this after reconstructing the BIOS fb's
15958 * since the watermark calculation done here will use pstate->fb.
15959 */
15960 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015961}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015962
Daniel Vetter7fad7982012-07-04 17:51:47 +020015963static void intel_enable_pipe_a(struct drm_device *dev)
15964{
15965 struct intel_connector *connector;
15966 struct drm_connector *crt = NULL;
15967 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015968 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015969
15970 /* We can't just switch on the pipe A, we need to set things up with a
15971 * proper mode and output configuration. As a gross hack, enable pipe A
15972 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015973 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015974 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15975 crt = &connector->base;
15976 break;
15977 }
15978 }
15979
15980 if (!crt)
15981 return;
15982
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015983 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015984 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015985}
15986
Daniel Vetterfa555832012-10-10 23:14:00 +020015987static bool
15988intel_check_plane_mapping(struct intel_crtc *crtc)
15989{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015990 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015991 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015992 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015993
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015994 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015995 return true;
15996
Ville Syrjälä649636e2015-09-22 19:50:01 +030015997 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015998
15999 if ((val & DISPLAY_PLANE_ENABLE) &&
16000 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16001 return false;
16002
16003 return true;
16004}
16005
Ville Syrjälä02e93c32015-08-26 19:39:19 +030016006static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16007{
16008 struct drm_device *dev = crtc->base.dev;
16009 struct intel_encoder *encoder;
16010
16011 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16012 return true;
16013
16014 return false;
16015}
16016
Ville Syrjälädd756192016-02-17 21:28:45 +020016017static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
16018{
16019 struct drm_device *dev = encoder->base.dev;
16020 struct intel_connector *connector;
16021
16022 for_each_connector_on_encoder(dev, &encoder->base, connector)
16023 return true;
16024
16025 return false;
16026}
16027
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016028static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16029 enum transcoder pch_transcoder)
16030{
16031 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16032 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16033}
16034
Daniel Vetter24929352012-07-02 20:28:59 +020016035static void intel_sanitize_crtc(struct intel_crtc *crtc)
16036{
16037 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010016038 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020016039 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020016040
Daniel Vetter24929352012-07-02 20:28:59 +020016041 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020016042 if (!transcoder_is_dsi(cpu_transcoder)) {
16043 i915_reg_t reg = PIPECONF(cpu_transcoder);
16044
16045 I915_WRITE(reg,
16046 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16047 }
Daniel Vetter24929352012-07-02 20:28:59 +020016048
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016049 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010016050 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030016051 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016052 struct intel_plane *plane;
16053
Daniel Vetter96256042015-02-13 21:03:42 +010016054 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016055
16056 /* Disable everything but the primary plane */
16057 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16058 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16059 continue;
16060
16061 plane->disable_plane(&plane->base, &crtc->base);
16062 }
Daniel Vetter96256042015-02-13 21:03:42 +010016063 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016064
Daniel Vetter24929352012-07-02 20:28:59 +020016065 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020016066 * disable the crtc (and hence change the state) if it is wrong. Note
16067 * that gen4+ has a fixed plane -> pipe mapping. */
16068 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020016069 bool plane;
16070
Ville Syrjälä78108b72016-05-27 20:59:19 +030016071 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16072 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016073
16074 /* Pipe has the wrong plane attached and the plane is active.
16075 * Temporarily change the plane mapping and disable everything
16076 * ... */
16077 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016078 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020016079 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016080 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016081 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020016082 }
Daniel Vetter24929352012-07-02 20:28:59 +020016083
Daniel Vetter7fad7982012-07-04 17:51:47 +020016084 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16085 crtc->pipe == PIPE_A && !crtc->active) {
16086 /* BIOS forgot to enable pipe A, this mostly happens after
16087 * resume. Force-enable the pipe to fix this, the update_dpms
16088 * call below we restore the pipe to the right state, but leave
16089 * the required bits on. */
16090 intel_enable_pipe_a(dev);
16091 }
16092
Daniel Vetter24929352012-07-02 20:28:59 +020016093 /* Adjust the state of the output pipe according to whether we
16094 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010016095 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016096 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016097
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030016098 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010016099 /*
16100 * We start out with underrun reporting disabled to avoid races.
16101 * For correct bookkeeping mark this on active crtcs.
16102 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020016103 * Also on gmch platforms we dont have any hardware bits to
16104 * disable the underrun reporting. Which means we need to start
16105 * out with underrun reporting disabled also on inactive pipes,
16106 * since otherwise we'll complain about the garbage we read when
16107 * e.g. coming up after runtime pm.
16108 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010016109 * No protection against concurrent access is required - at
16110 * worst a fifo underrun happens which also sets this to false.
16111 */
16112 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016113 /*
16114 * We track the PCH trancoder underrun reporting state
16115 * within the crtc. With crtc for pipe A housing the underrun
16116 * reporting state for PCH transcoder A, crtc for pipe B housing
16117 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16118 * and marking underrun reporting as disabled for the non-existing
16119 * PCH transcoders B and C would prevent enabling the south
16120 * error interrupt (see cpt_can_enable_serr_int()).
16121 */
16122 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16123 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010016124 }
Daniel Vetter24929352012-07-02 20:28:59 +020016125}
16126
16127static void intel_sanitize_encoder(struct intel_encoder *encoder)
16128{
16129 struct intel_connector *connector;
16130 struct drm_device *dev = encoder->base.dev;
16131
16132 /* We need to check both for a crtc link (meaning that the
16133 * encoder is active and trying to read from a pipe) and the
16134 * pipe itself being active. */
16135 bool has_active_crtc = encoder->base.crtc &&
16136 to_intel_crtc(encoder->base.crtc)->active;
16137
Ville Syrjälädd756192016-02-17 21:28:45 +020016138 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020016139 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16140 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016141 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016142
16143 /* Connector is active, but has no active pipe. This is
16144 * fallout from our resume register restoring. Disable
16145 * the encoder manually again. */
16146 if (encoder->base.crtc) {
16147 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16148 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016149 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016150 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030016151 if (encoder->post_disable)
16152 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020016153 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020016154 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016155
16156 /* Inconsistent output/port/pipe state happens presumably due to
16157 * a bug in one of the get_hw_state functions. Or someplace else
16158 * in our code, like the register restore mess on resume. Clamp
16159 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016160 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016161 if (connector->encoder != encoder)
16162 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020016163 connector->base.dpms = DRM_MODE_DPMS_OFF;
16164 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016165 }
16166 }
16167 /* Enabled encoders without active connectors will be fixed in
16168 * the crtc fixup. */
16169}
16170
Imre Deak04098752014-02-18 00:02:16 +020016171void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016172{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016173 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020016174 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016175
Imre Deak04098752014-02-18 00:02:16 +020016176 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16177 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16178 i915_disable_vga(dev);
16179 }
16180}
16181
16182void i915_redisable_vga(struct drm_device *dev)
16183{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016184 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020016185
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016186 /* This function can be called both from intel_modeset_setup_hw_state or
16187 * at a very early point in our resume sequence, where the power well
16188 * structures are not yet restored. Since this function is at a very
16189 * paranoid "someone might have enabled VGA while we were not looking"
16190 * level, just check if the power well is enabled instead of trying to
16191 * follow the "don't touch the power well if we don't need it" policy
16192 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016193 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016194 return;
16195
Imre Deak04098752014-02-18 00:02:16 +020016196 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020016197
16198 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016199}
16200
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016201static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016202{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016203 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016204
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016205 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016206}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016207
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016208/* FIXME read out full plane state for all planes */
16209static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016210{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016211 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016212 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016213 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016214
Matt Roper19b8d382015-09-24 15:53:17 -070016215 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016216 primary_get_hw_state(to_intel_plane(primary));
16217
16218 if (plane_state->visible)
16219 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016220}
16221
Daniel Vetter30e984d2013-06-05 13:34:17 +020016222static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016223{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016224 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016225 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016226 struct intel_crtc *crtc;
16227 struct intel_encoder *encoder;
16228 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016229 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016230
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016231 dev_priv->active_crtcs = 0;
16232
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016233 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016234 struct intel_crtc_state *crtc_state = crtc->config;
16235 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016236
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016237 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016238 memset(crtc_state, 0, sizeof(*crtc_state));
16239 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016240
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016241 crtc_state->base.active = crtc_state->base.enable =
16242 dev_priv->display.get_pipe_config(crtc, crtc_state);
16243
16244 crtc->base.enabled = crtc_state->base.enable;
16245 crtc->active = crtc_state->base.active;
16246
16247 if (crtc_state->base.active) {
16248 dev_priv->active_crtcs |= 1 << crtc->pipe;
16249
Clint Taylorc89e39f2016-05-13 23:41:21 +030016250 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016251 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016252 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016253 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16254 else
16255 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016256
16257 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16258 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16259 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016260 }
16261
16262 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016263
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016264 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016265
Ville Syrjälä78108b72016-05-27 20:59:19 +030016266 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16267 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016268 crtc->active ? "enabled" : "disabled");
16269 }
16270
Daniel Vetter53589012013-06-05 13:34:16 +020016271 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16272 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16273
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016274 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16275 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016276 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016277 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016278 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016279 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016280 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016281 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016282
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016283 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016284 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016285 }
16286
Damien Lespiaub2784e12014-08-05 11:29:37 +010016287 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016288 pipe = 0;
16289
16290 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016291 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16292 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016293 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016294 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016295 } else {
16296 encoder->base.crtc = NULL;
16297 }
16298
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016299 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016300 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016301 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016302 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016303 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016304 }
16305
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016306 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016307 if (connector->get_hw_state(connector)) {
16308 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016309
16310 encoder = connector->encoder;
16311 connector->base.encoder = &encoder->base;
16312
16313 if (encoder->base.crtc &&
16314 encoder->base.crtc->state->active) {
16315 /*
16316 * This has to be done during hardware readout
16317 * because anything calling .crtc_disable may
16318 * rely on the connector_mask being accurate.
16319 */
16320 encoder->base.crtc->state->connector_mask |=
16321 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016322 encoder->base.crtc->state->encoder_mask |=
16323 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016324 }
16325
Daniel Vetter24929352012-07-02 20:28:59 +020016326 } else {
16327 connector->base.dpms = DRM_MODE_DPMS_OFF;
16328 connector->base.encoder = NULL;
16329 }
16330 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16331 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016332 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016333 connector->base.encoder ? "enabled" : "disabled");
16334 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016335
16336 for_each_intel_crtc(dev, crtc) {
16337 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16338
16339 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16340 if (crtc->base.state->active) {
16341 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16342 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16343 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16344
16345 /*
16346 * The initial mode needs to be set in order to keep
16347 * the atomic core happy. It wants a valid mode if the
16348 * crtc's enabled, so we do the above call.
16349 *
16350 * At this point some state updated by the connectors
16351 * in their ->detect() callback has not run yet, so
16352 * no recalculation can be done yet.
16353 *
16354 * Even if we could do a recalculation and modeset
16355 * right now it would cause a double modeset if
16356 * fbdev or userspace chooses a different initial mode.
16357 *
16358 * If that happens, someone indicated they wanted a
16359 * mode change, which means it's safe to do a full
16360 * recalculation.
16361 */
16362 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016363
16364 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16365 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016366 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016367
16368 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016369 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016370}
16371
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016372/* Scan out the current hw modeset state,
16373 * and sanitizes it to the current state
16374 */
16375static void
16376intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016377{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016378 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016379 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016380 struct intel_crtc *crtc;
16381 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016382 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016383
16384 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016385
16386 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016387 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016388 intel_sanitize_encoder(encoder);
16389 }
16390
Damien Lespiau055e3932014-08-18 13:49:10 +010016391 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016392 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16393 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016394 intel_dump_pipe_config(crtc, crtc->config,
16395 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016396 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016397
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016398 intel_modeset_update_connector_atomic_state(dev);
16399
Daniel Vetter35c95372013-07-17 06:55:04 +020016400 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16401 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16402
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016403 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016404 continue;
16405
16406 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16407
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016408 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016409 pll->on = false;
16410 }
16411
Wayne Boyer666a4532015-12-09 12:29:35 -080016412 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016413 vlv_wm_get_hw_state(dev);
16414 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016415 skl_wm_get_hw_state(dev);
16416 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016417 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016418
16419 for_each_intel_crtc(dev, crtc) {
16420 unsigned long put_domains;
16421
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016422 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016423 if (WARN_ON(put_domains))
16424 modeset_put_power_domains(dev_priv, put_domains);
16425 }
16426 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016427
16428 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016429}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016430
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016431void intel_display_resume(struct drm_device *dev)
16432{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016433 struct drm_i915_private *dev_priv = to_i915(dev);
16434 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16435 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016436 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020016437
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016438 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030016439 if (state)
16440 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016441
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016442 /*
16443 * This is a cludge because with real atomic modeset mode_config.mutex
16444 * won't be taken. Unfortunately some probed state like
16445 * audio_codec_enable is still protected by mode_config.mutex, so lock
16446 * it here for now.
16447 */
16448 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016449 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016450
Maarten Lankhorst73974892016-08-05 23:28:27 +030016451 while (1) {
16452 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16453 if (ret != -EDEADLK)
16454 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016455
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016456 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016457 }
16458
Maarten Lankhorst73974892016-08-05 23:28:27 +030016459 if (!ret)
16460 ret = __intel_display_resume(dev, state);
16461
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016462 drm_modeset_drop_locks(&ctx);
16463 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016464 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016465
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016466 if (ret) {
16467 DRM_ERROR("Restoring old state failed with %i\n", ret);
16468 drm_atomic_state_free(state);
16469 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016470}
16471
16472void intel_modeset_gem_init(struct drm_device *dev)
16473{
Chris Wilsondc979972016-05-10 14:10:04 +010016474 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016475 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016476 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016477 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016478
Chris Wilsondc979972016-05-10 14:10:04 +010016479 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016480
Chris Wilson1833b132012-05-09 11:56:28 +010016481 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016482
Chris Wilson1ee8da62016-05-12 12:43:23 +010016483 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016484
16485 /*
16486 * Make sure any fbs we allocated at startup are properly
16487 * pinned & fenced. When we do the allocation it's too early
16488 * for this.
16489 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016490 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016491 obj = intel_fb_obj(c->primary->fb);
16492 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016493 continue;
16494
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016495 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016496 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16497 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016498 mutex_unlock(&dev->struct_mutex);
16499 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016500 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16501 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016502 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016503 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016504 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016505 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016506 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016507 }
16508 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016509}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016510
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016511int intel_connector_register(struct drm_connector *connector)
16512{
16513 struct intel_connector *intel_connector = to_intel_connector(connector);
16514 int ret;
16515
16516 ret = intel_backlight_device_register(intel_connector);
16517 if (ret)
16518 goto err;
16519
16520 return 0;
16521
16522err:
16523 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016524}
16525
Chris Wilsonc191eca2016-06-17 11:40:33 +010016526void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016527{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016528 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016529
Chris Wilsone63d87c2016-06-17 11:40:34 +010016530 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016531 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016532}
16533
Jesse Barnes79e53942008-11-07 14:24:08 -080016534void intel_modeset_cleanup(struct drm_device *dev)
16535{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016536 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070016537
Chris Wilsondc979972016-05-10 14:10:04 +010016538 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016539
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016540 /*
16541 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016542 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016543 * experience fancy races otherwise.
16544 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016545 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016546
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016547 /*
16548 * Due to the hpd irq storm handling the hotplug work can re-arm the
16549 * poll handlers. Hence disable polling after hpd handling is shut down.
16550 */
Keith Packardf87ea762010-10-03 19:36:26 -070016551 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016552
Jesse Barnes723bfd72010-10-07 16:01:13 -070016553 intel_unregister_dsm_handler();
16554
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016555 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016556
Chris Wilson1630fe72011-07-08 12:22:42 +010016557 /* flush any delayed tasks or pending work */
16558 flush_scheduled_work();
16559
Jesse Barnes79e53942008-11-07 14:24:08 -080016560 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016561
Chris Wilson1ee8da62016-05-12 12:43:23 +010016562 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016563
Chris Wilsondc979972016-05-10 14:10:04 +010016564 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016565
16566 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016567}
16568
Chris Wilsondf0e9242010-09-09 16:20:55 +010016569void intel_connector_attach_encoder(struct intel_connector *connector,
16570 struct intel_encoder *encoder)
16571{
16572 connector->encoder = encoder;
16573 drm_mode_connector_attach_encoder(&connector->base,
16574 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016575}
Dave Airlie28d52042009-09-21 14:33:58 +100016576
16577/*
16578 * set vga decode state - true == enable VGA decode
16579 */
16580int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16581{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016582 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000016583 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016584 u16 gmch_ctrl;
16585
Chris Wilson75fa0412014-02-07 18:37:02 -020016586 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16587 DRM_ERROR("failed to read control word\n");
16588 return -EIO;
16589 }
16590
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016591 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16592 return 0;
16593
Dave Airlie28d52042009-09-21 14:33:58 +100016594 if (state)
16595 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16596 else
16597 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016598
16599 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16600 DRM_ERROR("failed to write control word\n");
16601 return -EIO;
16602 }
16603
Dave Airlie28d52042009-09-21 14:33:58 +100016604 return 0;
16605}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016606
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016607struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016608
16609 u32 power_well_driver;
16610
Chris Wilson63b66e52013-08-08 15:12:06 +020016611 int num_transcoders;
16612
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016613 struct intel_cursor_error_state {
16614 u32 control;
16615 u32 position;
16616 u32 base;
16617 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016618 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016619
16620 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016621 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016622 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016623 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016624 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016625
16626 struct intel_plane_error_state {
16627 u32 control;
16628 u32 stride;
16629 u32 size;
16630 u32 pos;
16631 u32 addr;
16632 u32 surface;
16633 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016634 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016635
16636 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016637 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016638 enum transcoder cpu_transcoder;
16639
16640 u32 conf;
16641
16642 u32 htotal;
16643 u32 hblank;
16644 u32 hsync;
16645 u32 vtotal;
16646 u32 vblank;
16647 u32 vsync;
16648 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016649};
16650
16651struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016652intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016653{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016654 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016655 int transcoders[] = {
16656 TRANSCODER_A,
16657 TRANSCODER_B,
16658 TRANSCODER_C,
16659 TRANSCODER_EDP,
16660 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016661 int i;
16662
Chris Wilsonc0336662016-05-06 15:40:21 +010016663 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016664 return NULL;
16665
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016666 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016667 if (error == NULL)
16668 return NULL;
16669
Chris Wilsonc0336662016-05-06 15:40:21 +010016670 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016671 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16672
Damien Lespiau055e3932014-08-18 13:49:10 +010016673 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016674 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016675 __intel_display_power_is_enabled(dev_priv,
16676 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016677 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016678 continue;
16679
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016680 error->cursor[i].control = I915_READ(CURCNTR(i));
16681 error->cursor[i].position = I915_READ(CURPOS(i));
16682 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016683
16684 error->plane[i].control = I915_READ(DSPCNTR(i));
16685 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016686 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016687 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016688 error->plane[i].pos = I915_READ(DSPPOS(i));
16689 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016690 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016691 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016692 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016693 error->plane[i].surface = I915_READ(DSPSURF(i));
16694 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16695 }
16696
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016697 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016698
Chris Wilsonc0336662016-05-06 15:40:21 +010016699 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030016700 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016701 }
16702
Jani Nikula4d1de972016-03-18 17:05:42 +020016703 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016704 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016705 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016706 error->num_transcoders++; /* Account for eDP. */
16707
16708 for (i = 0; i < error->num_transcoders; i++) {
16709 enum transcoder cpu_transcoder = transcoders[i];
16710
Imre Deakddf9c532013-11-27 22:02:02 +020016711 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016712 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016713 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016714 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016715 continue;
16716
Chris Wilson63b66e52013-08-08 15:12:06 +020016717 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16718
16719 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16720 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16721 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16722 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16723 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16724 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16725 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016726 }
16727
16728 return error;
16729}
16730
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016731#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16732
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016733void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016734intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016735 struct drm_device *dev,
16736 struct intel_display_error_state *error)
16737{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016738 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016739 int i;
16740
Chris Wilson63b66e52013-08-08 15:12:06 +020016741 if (!error)
16742 return;
16743
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016744 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016745 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016746 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016747 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016748 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016749 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016750 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016751 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016752 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016753 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016754
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016755 err_printf(m, "Plane [%d]:\n", i);
16756 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16757 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016758 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016759 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16760 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016761 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016762 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016763 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016764 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016765 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16766 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016767 }
16768
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016769 err_printf(m, "Cursor [%d]:\n", i);
16770 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16771 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16772 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016773 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016774
16775 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016776 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016777 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016778 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016779 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016780 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16781 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16782 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16783 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16784 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16785 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16786 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16787 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016788}