blob: 9cbf5431c1e34dcf41865161a9e880fa60a809af [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
Jesse Barnesea0760c2011-01-04 15:09:32 -08001205 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001206 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1207
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 */
1214 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1215 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001216 } else {
1217 pp_reg = PP_CONTROL;
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 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +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);
Jesse Barnes040484a2011-01-03 12:14:26 -08001967 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
2150static void
2151intel_fill_fb_info(struct drm_i915_private *dev_priv,
2152 struct drm_framebuffer *fb)
2153{
2154 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002155 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002156
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002157 tile_size = intel_tile_size(dev_priv);
2158
2159 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002160 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2161 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002162
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002163 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2164 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002165
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002166 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002167 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002168 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2169 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002170
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002171 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002172 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2173 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002174 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002175}
2176
Ville Syrjälä603525d2016-01-12 21:08:37 +02002177static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002178{
2179 if (INTEL_INFO(dev_priv)->gen >= 9)
2180 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002181 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002182 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002183 return 128 * 1024;
2184 else if (INTEL_INFO(dev_priv)->gen >= 4)
2185 return 4 * 1024;
2186 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002187 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002188}
2189
Ville Syrjälä603525d2016-01-12 21:08:37 +02002190static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2191 uint64_t fb_modifier)
2192{
2193 switch (fb_modifier) {
2194 case DRM_FORMAT_MOD_NONE:
2195 return intel_linear_alignment(dev_priv);
2196 case I915_FORMAT_MOD_X_TILED:
2197 if (INTEL_INFO(dev_priv)->gen >= 9)
2198 return 256 * 1024;
2199 return 0;
2200 case I915_FORMAT_MOD_Y_TILED:
2201 case I915_FORMAT_MOD_Yf_TILED:
2202 return 1 * 1024 * 1024;
2203 default:
2204 MISSING_CASE(fb_modifier);
2205 return 0;
2206 }
2207}
2208
Chris Wilson127bd2a2010-07-23 23:32:05 +01002209int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002210intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2211 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002212{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002213 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002214 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002215 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002216 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002217 u32 alignment;
2218 int ret;
2219
Matt Roperebcdd392014-07-09 16:22:11 -07002220 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2221
Ville Syrjälä603525d2016-01-12 21:08:37 +02002222 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002223
Ville Syrjälä3465c582016-02-15 22:54:43 +02002224 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002225
Chris Wilson693db182013-03-05 14:52:39 +00002226 /* Note that the w/a also requires 64 PTE of padding following the
2227 * bo. We currently fill all unused PTE with the shadow page and so
2228 * we should always have valid PTE following the scanout preventing
2229 * the VT-d warning.
2230 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002231 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002232 alignment = 256 * 1024;
2233
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002234 /*
2235 * Global gtt pte registers are special registers which actually forward
2236 * writes to a chunk of system memory. Which means that there is no risk
2237 * that the register values disappear as soon as we call
2238 * intel_runtime_pm_put(), so it is correct to wrap only the
2239 * pin/unpin/fence and not more.
2240 */
2241 intel_runtime_pm_get(dev_priv);
2242
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002243 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2244 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002245 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002246 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002247
2248 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2249 * fence, whereas 965+ only requires a fence if using
2250 * framebuffer compression. For simplicity, we always install
2251 * a fence as the cost is not that onerous.
2252 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002253 if (view.type == I915_GGTT_VIEW_NORMAL) {
2254 ret = i915_gem_object_get_fence(obj);
2255 if (ret == -EDEADLK) {
2256 /*
2257 * -EDEADLK means there are no free fences
2258 * no pending flips.
2259 *
2260 * This is propagated to atomic, but it uses
2261 * -EDEADLK to force a locking recovery, so
2262 * change the returned error to -EBUSY.
2263 */
2264 ret = -EBUSY;
2265 goto err_unpin;
2266 } else if (ret)
2267 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002268
Vivek Kasireddy98072162015-10-29 18:54:38 -07002269 i915_gem_object_pin_fence(obj);
2270 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002271
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002272 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002273 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002274
2275err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002276 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002277err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002278 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002279 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002280}
2281
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002282void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002283{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002284 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002285 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002286
Matt Roperebcdd392014-07-09 16:22:11 -07002287 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2288
Ville Syrjälä3465c582016-02-15 22:54:43 +02002289 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002290
Vivek Kasireddy98072162015-10-29 18:54:38 -07002291 if (view.type == I915_GGTT_VIEW_NORMAL)
2292 i915_gem_object_unpin_fence(obj);
2293
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002294 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002295}
2296
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002297/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002298 * Adjust the tile offset by moving the difference into
2299 * the x/y offsets.
2300 *
2301 * Input tile dimensions and pitch must already be
2302 * rotated to match x and y, and in pixel units.
2303 */
2304static u32 intel_adjust_tile_offset(int *x, int *y,
2305 unsigned int tile_width,
2306 unsigned int tile_height,
2307 unsigned int tile_size,
2308 unsigned int pitch_tiles,
2309 u32 old_offset,
2310 u32 new_offset)
2311{
2312 unsigned int tiles;
2313
2314 WARN_ON(old_offset & (tile_size - 1));
2315 WARN_ON(new_offset & (tile_size - 1));
2316 WARN_ON(new_offset > old_offset);
2317
2318 tiles = (old_offset - new_offset) / tile_size;
2319
2320 *y += tiles / pitch_tiles * tile_height;
2321 *x += tiles % pitch_tiles * tile_width;
2322
2323 return new_offset;
2324}
2325
2326/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002327 * Computes the linear offset to the base tile and adjusts
2328 * x, y. bytes per pixel is assumed to be a power-of-two.
2329 *
2330 * In the 90/270 rotated case, x and y are assumed
2331 * to be already rotated to match the rotated GTT view, and
2332 * pitch is the tile_height aligned framebuffer height.
2333 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002334u32 intel_compute_tile_offset(int *x, int *y,
2335 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002336 unsigned int pitch,
2337 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002338{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002339 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2340 uint64_t fb_modifier = fb->modifier[plane];
2341 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002342 u32 offset, offset_aligned, alignment;
2343
2344 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2345 if (alignment)
2346 alignment--;
2347
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002348 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002349 unsigned int tile_size, tile_width, tile_height;
2350 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002351
Ville Syrjäläd8433102016-01-12 21:08:35 +02002352 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002353 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2354 fb_modifier, cpp);
2355
2356 if (intel_rotation_90_or_270(rotation)) {
2357 pitch_tiles = pitch / tile_height;
2358 swap(tile_width, tile_height);
2359 } else {
2360 pitch_tiles = pitch / (tile_width * cpp);
2361 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002362
Ville Syrjäläd8433102016-01-12 21:08:35 +02002363 tile_rows = *y / tile_height;
2364 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002365
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002366 tiles = *x / tile_width;
2367 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002368
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002369 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2370 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002371
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002372 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2373 tile_size, pitch_tiles,
2374 offset, offset_aligned);
2375 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002376 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002377 offset_aligned = offset & ~alignment;
2378
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002379 *y = (offset & alignment) / pitch;
2380 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002381 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002382
2383 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002384}
2385
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002386static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002387{
2388 switch (format) {
2389 case DISPPLANE_8BPP:
2390 return DRM_FORMAT_C8;
2391 case DISPPLANE_BGRX555:
2392 return DRM_FORMAT_XRGB1555;
2393 case DISPPLANE_BGRX565:
2394 return DRM_FORMAT_RGB565;
2395 default:
2396 case DISPPLANE_BGRX888:
2397 return DRM_FORMAT_XRGB8888;
2398 case DISPPLANE_RGBX888:
2399 return DRM_FORMAT_XBGR8888;
2400 case DISPPLANE_BGRX101010:
2401 return DRM_FORMAT_XRGB2101010;
2402 case DISPPLANE_RGBX101010:
2403 return DRM_FORMAT_XBGR2101010;
2404 }
2405}
2406
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002407static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2408{
2409 switch (format) {
2410 case PLANE_CTL_FORMAT_RGB_565:
2411 return DRM_FORMAT_RGB565;
2412 default:
2413 case PLANE_CTL_FORMAT_XRGB_8888:
2414 if (rgb_order) {
2415 if (alpha)
2416 return DRM_FORMAT_ABGR8888;
2417 else
2418 return DRM_FORMAT_XBGR8888;
2419 } else {
2420 if (alpha)
2421 return DRM_FORMAT_ARGB8888;
2422 else
2423 return DRM_FORMAT_XRGB8888;
2424 }
2425 case PLANE_CTL_FORMAT_XRGB_2101010:
2426 if (rgb_order)
2427 return DRM_FORMAT_XBGR2101010;
2428 else
2429 return DRM_FORMAT_XRGB2101010;
2430 }
2431}
2432
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002433static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002434intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2435 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002436{
2437 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002438 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002439 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002440 struct drm_i915_gem_object *obj = NULL;
2441 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002442 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002443 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2444 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2445 PAGE_SIZE);
2446
2447 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002448
Chris Wilsonff2652e2014-03-10 08:07:02 +00002449 if (plane_config->size == 0)
2450 return false;
2451
Paulo Zanoni3badb492015-09-23 12:52:23 -03002452 /* If the FB is too big, just don't use it since fbdev is not very
2453 * important and we should probably use that space with FBC or other
2454 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002455 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002456 return false;
2457
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002458 mutex_lock(&dev->struct_mutex);
2459
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002460 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2461 base_aligned,
2462 base_aligned,
2463 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002464 if (!obj) {
2465 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002466 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002467 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002468
Chris Wilson3e510a82016-08-05 10:14:23 +01002469 if (plane_config->tiling == I915_TILING_X)
2470 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002471
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002472 mode_cmd.pixel_format = fb->pixel_format;
2473 mode_cmd.width = fb->width;
2474 mode_cmd.height = fb->height;
2475 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002476 mode_cmd.modifier[0] = fb->modifier[0];
2477 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002479 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002480 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481 DRM_DEBUG_KMS("intel fb init failed\n");
2482 goto out_unref_obj;
2483 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002484
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002486
Daniel Vetterf6936e22015-03-26 12:17:05 +01002487 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002488 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489
2490out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002491 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002492 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002493 return false;
2494}
2495
Daniel Vetter5a21b662016-05-24 17:13:53 +02002496/* Update plane->state->fb to match plane->fb after driver-internal updates */
2497static void
2498update_state_fb(struct drm_plane *plane)
2499{
2500 if (plane->fb == plane->state->fb)
2501 return;
2502
2503 if (plane->state->fb)
2504 drm_framebuffer_unreference(plane->state->fb);
2505 plane->state->fb = plane->fb;
2506 if (plane->state->fb)
2507 drm_framebuffer_reference(plane->state->fb);
2508}
2509
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002510static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002511intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2512 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002513{
2514 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002515 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002516 struct drm_crtc *c;
2517 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002518 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002519 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002520 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002521 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2522 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002523 struct intel_plane_state *intel_state =
2524 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002525 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002526
Damien Lespiau2d140302015-02-05 17:22:18 +00002527 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002528 return;
2529
Daniel Vetterf6936e22015-03-26 12:17:05 +01002530 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002531 fb = &plane_config->fb->base;
2532 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002533 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534
Damien Lespiau2d140302015-02-05 17:22:18 +00002535 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002536
2537 /*
2538 * Failed to alloc the obj, check to see if we should share
2539 * an fb with another CRTC instead
2540 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002541 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 i = to_intel_crtc(c);
2543
2544 if (c == &intel_crtc->base)
2545 continue;
2546
Matt Roper2ff8fde2014-07-08 07:50:07 -07002547 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548 continue;
2549
Daniel Vetter88595ac2015-03-26 12:42:24 +01002550 fb = c->primary->fb;
2551 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002552 continue;
2553
Daniel Vetter88595ac2015-03-26 12:42:24 +01002554 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002555 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556 drm_framebuffer_reference(fb);
2557 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 }
2559 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560
Matt Roper200757f2015-12-03 11:37:36 -08002561 /*
2562 * We've failed to reconstruct the BIOS FB. Current display state
2563 * indicates that the primary plane is visible, but has a NULL FB,
2564 * which will lead to problems later if we don't fix it up. The
2565 * simplest solution is to just disable the primary plane now and
2566 * pretend the BIOS never had it enabled.
2567 */
2568 to_intel_plane_state(plane_state)->visible = false;
2569 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002570 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002571 intel_plane->disable_plane(primary, &intel_crtc->base);
2572
Daniel Vetter88595ac2015-03-26 12:42:24 +01002573 return;
2574
2575valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002576 plane_state->src_x = 0;
2577 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002578 plane_state->src_w = fb->width << 16;
2579 plane_state->src_h = fb->height << 16;
2580
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002581 plane_state->crtc_x = 0;
2582 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002583 plane_state->crtc_w = fb->width;
2584 plane_state->crtc_h = fb->height;
2585
Matt Roper0a8d8a82015-12-03 11:37:38 -08002586 intel_state->src.x1 = plane_state->src_x;
2587 intel_state->src.y1 = plane_state->src_y;
2588 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2589 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2590 intel_state->dst.x1 = plane_state->crtc_x;
2591 intel_state->dst.y1 = plane_state->crtc_y;
2592 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2593 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2594
Daniel Vetter88595ac2015-03-26 12:42:24 +01002595 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002596 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002597 dev_priv->preserve_bios_swizzle = true;
2598
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002599 drm_framebuffer_reference(fb);
2600 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002601 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002602 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002603 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2604 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002605}
2606
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002607static void i9xx_update_primary_plane(struct drm_plane *primary,
2608 const struct intel_crtc_state *crtc_state,
2609 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002610{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002611 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002612 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2614 struct drm_framebuffer *fb = plane_state->base.fb;
2615 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002616 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002617 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002618 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002619 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002620 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002621 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002622 int x = plane_state->src.x1 >> 16;
2623 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002624
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002625 dspcntr = DISPPLANE_GAMMA_ENABLE;
2626
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002627 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002628
2629 if (INTEL_INFO(dev)->gen < 4) {
2630 if (intel_crtc->pipe == PIPE_B)
2631 dspcntr |= DISPPLANE_SEL_PIPE_B;
2632
2633 /* pipesrc and dspsize control the size that is scaled from,
2634 * which should always be the user's requested size.
2635 */
2636 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002637 ((crtc_state->pipe_src_h - 1) << 16) |
2638 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002639 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002640 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2641 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002642 ((crtc_state->pipe_src_h - 1) << 16) |
2643 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002644 I915_WRITE(PRIMPOS(plane), 0);
2645 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002646 }
2647
Ville Syrjälä57779d02012-10-31 17:50:14 +02002648 switch (fb->pixel_format) {
2649 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002650 dspcntr |= DISPPLANE_8BPP;
2651 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002652 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002653 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002654 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002655 case DRM_FORMAT_RGB565:
2656 dspcntr |= DISPPLANE_BGRX565;
2657 break;
2658 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002659 dspcntr |= DISPPLANE_BGRX888;
2660 break;
2661 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002662 dspcntr |= DISPPLANE_RGBX888;
2663 break;
2664 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002665 dspcntr |= DISPPLANE_BGRX101010;
2666 break;
2667 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002669 break;
2670 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002671 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002672 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002673
Chris Wilson3e510a82016-08-05 10:14:23 +01002674 if (INTEL_INFO(dev)->gen >= 4 && i915_gem_object_is_tiled(obj))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002675 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002676
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002677 if (IS_G4X(dev))
2678 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2679
Ville Syrjäläac484962016-01-20 21:05:26 +02002680 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002681
Daniel Vetterc2c75132012-07-05 12:17:30 +02002682 if (INTEL_INFO(dev)->gen >= 4) {
2683 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002684 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002685 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002686 linear_offset -= intel_crtc->dspaddr_offset;
2687 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002688 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002689 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002690
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002691 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302692 dspcntr |= DISPPLANE_ROTATE_180;
2693
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002694 x += (crtc_state->pipe_src_w - 1);
2695 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302696
2697 /* Finding the last pixel of the last line of the display
2698 data and adding to linear_offset*/
2699 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002700 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002701 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302702 }
2703
Paulo Zanoni2db33662015-09-14 15:20:03 -03002704 intel_crtc->adjusted_x = x;
2705 intel_crtc->adjusted_y = y;
2706
Sonika Jindal48404c12014-08-22 14:06:04 +05302707 I915_WRITE(reg, dspcntr);
2708
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002709 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002710 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002711 I915_WRITE(DSPSURF(plane),
2712 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002713 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002714 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002715 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002716 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002717 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002718}
2719
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002720static void i9xx_disable_primary_plane(struct drm_plane *primary,
2721 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002722{
2723 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002724 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002726 int plane = intel_crtc->plane;
2727
2728 I915_WRITE(DSPCNTR(plane), 0);
2729 if (INTEL_INFO(dev_priv)->gen >= 4)
2730 I915_WRITE(DSPSURF(plane), 0);
2731 else
2732 I915_WRITE(DSPADDR(plane), 0);
2733 POSTING_READ(DSPCNTR(plane));
2734}
2735
2736static void ironlake_update_primary_plane(struct drm_plane *primary,
2737 const struct intel_crtc_state *crtc_state,
2738 const struct intel_plane_state *plane_state)
2739{
2740 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002741 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2743 struct drm_framebuffer *fb = plane_state->base.fb;
2744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002745 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002746 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002747 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002748 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002749 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002750 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751 int x = plane_state->src.x1 >> 16;
2752 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002753
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002754 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002755 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002756
2757 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2758 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2759
Ville Syrjälä57779d02012-10-31 17:50:14 +02002760 switch (fb->pixel_format) {
2761 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002762 dspcntr |= DISPPLANE_8BPP;
2763 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002764 case DRM_FORMAT_RGB565:
2765 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002766 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002767 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002768 dspcntr |= DISPPLANE_BGRX888;
2769 break;
2770 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002771 dspcntr |= DISPPLANE_RGBX888;
2772 break;
2773 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 dspcntr |= DISPPLANE_BGRX101010;
2775 break;
2776 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002777 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 break;
2779 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002780 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781 }
2782
Chris Wilson3e510a82016-08-05 10:14:23 +01002783 if (i915_gem_object_is_tiled(obj))
Jesse Barnes17638cd2011-06-24 12:19:23 -07002784 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002786 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002787 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788
Ville Syrjäläac484962016-01-20 21:05:26 +02002789 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002790 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002791 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002792 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002793 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002794 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302795 dspcntr |= DISPPLANE_ROTATE_180;
2796
2797 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002798 x += (crtc_state->pipe_src_w - 1);
2799 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302800
2801 /* Finding the last pixel of the last line of the display
2802 data and adding to linear_offset*/
2803 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002804 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002805 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302806 }
2807 }
2808
Paulo Zanoni2db33662015-09-14 15:20:03 -03002809 intel_crtc->adjusted_x = x;
2810 intel_crtc->adjusted_y = y;
2811
Sonika Jindal48404c12014-08-22 14:06:04 +05302812 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002814 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002815 I915_WRITE(DSPSURF(plane),
2816 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002817 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002818 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2819 } else {
2820 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2821 I915_WRITE(DSPLINOFF(plane), linear_offset);
2822 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002824}
2825
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002826u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2827 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002828{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002829 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2830 return 64;
2831 } else {
2832 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002833
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002834 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002835 }
2836}
2837
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002838u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2839 struct drm_i915_gem_object *obj,
2840 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002841{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002842 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002843 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002844 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002845
Ville Syrjäläe7941292016-01-19 18:23:17 +02002846 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002847 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002848
Daniel Vetterce7f1722015-10-14 16:51:06 +02002849 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002850 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002851 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002852 return -1;
2853
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002854 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002855
2856 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002857 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002858 PAGE_SIZE;
2859 }
2860
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002861 WARN_ON(upper_32_bits(offset));
2862
2863 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002864}
2865
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002866static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2867{
2868 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002869 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002870
2871 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2872 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2873 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002874}
2875
Chandra Kondurua1b22782015-04-07 15:28:45 -07002876/*
2877 * This function detaches (aka. unbinds) unused scalers in hardware
2878 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002879static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002880{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002881 struct intel_crtc_scaler_state *scaler_state;
2882 int i;
2883
Chandra Kondurua1b22782015-04-07 15:28:45 -07002884 scaler_state = &intel_crtc->config->scaler_state;
2885
2886 /* loop through and disable scalers that aren't in use */
2887 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002888 if (!scaler_state->scalers[i].in_use)
2889 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002890 }
2891}
2892
Chandra Konduru6156a452015-04-27 13:48:39 -07002893u32 skl_plane_ctl_format(uint32_t pixel_format)
2894{
Chandra Konduru6156a452015-04-27 13:48:39 -07002895 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002896 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002897 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002898 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002899 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002900 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002901 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002902 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002903 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002904 /*
2905 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2906 * to be already pre-multiplied. We need to add a knob (or a different
2907 * DRM_FORMAT) for user-space to configure that.
2908 */
2909 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002915 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002916 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002917 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002918 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002922 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002923 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002928 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002930
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932}
2933
2934u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2935{
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 switch (fb_modifier) {
2937 case DRM_FORMAT_MOD_NONE:
2938 break;
2939 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 default:
2946 MISSING_CASE(fb_modifier);
2947 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002948
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950}
2951
2952u32 skl_plane_ctl_rotation(unsigned int rotation)
2953{
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 switch (rotation) {
2955 case BIT(DRM_ROTATE_0):
2956 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302957 /*
2958 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2959 * while i915 HW rotation is clockwise, thats why this swapping.
2960 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302962 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302966 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 default:
2968 MISSING_CASE(rotation);
2969 }
2970
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972}
2973
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002974static void skylake_update_primary_plane(struct drm_plane *plane,
2975 const struct intel_crtc_state *crtc_state,
2976 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002977{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002978 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002979 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2981 struct drm_framebuffer *fb = plane_state->base.fb;
2982 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002983 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302984 u32 plane_ctl, stride_div, stride;
2985 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002986 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302987 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002988 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002989 int scaler_id = plane_state->scaler_id;
2990 int src_x = plane_state->src.x1 >> 16;
2991 int src_y = plane_state->src.y1 >> 16;
2992 int src_w = drm_rect_width(&plane_state->src) >> 16;
2993 int src_h = drm_rect_height(&plane_state->src) >> 16;
2994 int dst_x = plane_state->dst.x1;
2995 int dst_y = plane_state->dst.y1;
2996 int dst_w = drm_rect_width(&plane_state->dst);
2997 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002998
2999 plane_ctl = PLANE_CTL_ENABLE |
3000 PLANE_CTL_PIPE_GAMMA_ENABLE |
3001 PLANE_CTL_PIPE_CSC_ENABLE;
3002
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3004 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003005 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003007
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003008 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003009 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003010 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303011
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003012 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003013
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303014 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003015 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3016
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303017 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003018 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020 x_offset = stride * tile_height - src_y - src_h;
3021 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303023 } else {
3024 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003025 x_offset = src_x;
3026 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 }
3029 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003030
Paulo Zanoni2db33662015-09-14 15:20:03 -03003031 intel_crtc->adjusted_x = x_offset;
3032 intel_crtc->adjusted_y = y_offset;
3033
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3036 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3037 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003038
3039 if (scaler_id >= 0) {
3040 uint32_t ps_ctrl = 0;
3041
3042 WARN_ON(!dst_w || !dst_h);
3043 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3044 crtc_state->scaler_state.scalers[scaler_id].mode;
3045 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3046 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3047 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3048 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3049 I915_WRITE(PLANE_POS(pipe, 0), 0);
3050 } else {
3051 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3052 }
3053
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003054 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055
3056 POSTING_READ(PLANE_SURF(pipe, 0));
3057}
3058
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003059static void skylake_disable_primary_plane(struct drm_plane *primary,
3060 struct drm_crtc *crtc)
3061{
3062 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003063 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003064 int pipe = to_intel_crtc(crtc)->pipe;
3065
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003066 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3067 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3068 POSTING_READ(PLANE_SURF(pipe, 0));
3069}
3070
Jesse Barnes17638cd2011-06-24 12:19:23 -07003071/* Assume fb object is pinned & idle & fenced and just update base pointers */
3072static int
3073intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3074 int x, int y, enum mode_set_atomic state)
3075{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003076 /* Support for kgdboc is disabled, this needs a major rework. */
3077 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003078
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003079 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003080}
3081
Daniel Vetter5a21b662016-05-24 17:13:53 +02003082static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3083{
3084 struct intel_crtc *crtc;
3085
Chris Wilson91c8a322016-07-05 10:40:23 +01003086 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003087 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3088}
3089
Ville Syrjälä75147472014-11-24 18:28:11 +02003090static void intel_update_primary_planes(struct drm_device *dev)
3091{
Ville Syrjälä75147472014-11-24 18:28:11 +02003092 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003093
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003094 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003095 struct intel_plane *plane = to_intel_plane(crtc->primary);
3096 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003097
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003098 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003099 plane_state = to_intel_plane_state(plane->base.state);
3100
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003101 if (plane_state->visible)
3102 plane->update_plane(&plane->base,
3103 to_intel_crtc_state(crtc->state),
3104 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003105
3106 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003107 }
3108}
3109
Chris Wilsonc0336662016-05-06 15:40:21 +01003110void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003111{
3112 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003113 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003114 return;
3115
3116 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003117 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003118 return;
3119
Chris Wilson91c8a322016-07-05 10:40:23 +01003120 drm_modeset_lock_all(&dev_priv->drm);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003121 /*
3122 * Disabling the crtcs gracefully seems nicer. Also the
3123 * g33 docs say we should at least disable all the planes.
3124 */
Chris Wilson91c8a322016-07-05 10:40:23 +01003125 intel_display_suspend(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003126}
3127
Chris Wilsonc0336662016-05-06 15:40:21 +01003128void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003129{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003130 /*
3131 * Flips in the rings will be nuked by the reset,
3132 * so complete all pending flips so that user space
3133 * will get its events and not get stuck.
3134 */
3135 intel_complete_page_flips(dev_priv);
3136
Ville Syrjälä75147472014-11-24 18:28:11 +02003137 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003138 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003139 return;
3140
3141 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003142 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003143 /*
3144 * Flips in the rings have been nuked by the reset,
3145 * so update the base address of all primary
3146 * planes to the the last fb to make sure we're
3147 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003148 *
3149 * FIXME: Atomic will make this obsolete since we won't schedule
3150 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003151 */
Chris Wilson91c8a322016-07-05 10:40:23 +01003152 intel_update_primary_planes(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003153 return;
3154 }
3155
3156 /*
3157 * The display has been reset as well,
3158 * so need a full re-initialization.
3159 */
3160 intel_runtime_pm_disable_interrupts(dev_priv);
3161 intel_runtime_pm_enable_interrupts(dev_priv);
3162
Chris Wilson91c8a322016-07-05 10:40:23 +01003163 intel_modeset_init_hw(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003164
3165 spin_lock_irq(&dev_priv->irq_lock);
3166 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003167 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003168 spin_unlock_irq(&dev_priv->irq_lock);
3169
Chris Wilson91c8a322016-07-05 10:40:23 +01003170 intel_display_resume(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003171
3172 intel_hpd_init(dev_priv);
3173
Chris Wilson91c8a322016-07-05 10:40:23 +01003174 drm_modeset_unlock_all(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003175}
3176
Chris Wilson7d5e3792014-03-04 13:15:08 +00003177static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3178{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003179 struct drm_device *dev = crtc->dev;
3180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3181 unsigned reset_counter;
3182 bool pending;
3183
3184 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3185 if (intel_crtc->reset_counter != reset_counter)
3186 return false;
3187
3188 spin_lock_irq(&dev->event_lock);
3189 pending = to_intel_crtc(crtc)->flip_work != NULL;
3190 spin_unlock_irq(&dev->event_lock);
3191
3192 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003193}
3194
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003195static void intel_update_pipe_config(struct intel_crtc *crtc,
3196 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003197{
3198 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003199 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003200 struct intel_crtc_state *pipe_config =
3201 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003202
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003203 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3204 crtc->base.mode = crtc->base.state->mode;
3205
3206 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3207 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3208 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003209
3210 /*
3211 * Update pipe size and adjust fitter if needed: the reason for this is
3212 * that in compute_mode_changes we check the native mode (not the pfit
3213 * mode) to see if we can flip rather than do a full mode set. In the
3214 * fastboot case, we'll flip, but if we don't update the pipesrc and
3215 * pfit state, we'll end up with a big fb scanned out into the wrong
3216 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003217 */
3218
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003219 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003220 ((pipe_config->pipe_src_w - 1) << 16) |
3221 (pipe_config->pipe_src_h - 1));
3222
3223 /* on skylake this is done by detaching scalers */
3224 if (INTEL_INFO(dev)->gen >= 9) {
3225 skl_detach_scalers(crtc);
3226
3227 if (pipe_config->pch_pfit.enabled)
3228 skylake_pfit_enable(crtc);
3229 } else if (HAS_PCH_SPLIT(dev)) {
3230 if (pipe_config->pch_pfit.enabled)
3231 ironlake_pfit_enable(crtc);
3232 else if (old_crtc_state->pch_pfit.enabled)
3233 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003234 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003235}
3236
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003237static void intel_fdi_normal_train(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003240 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3242 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003243 i915_reg_t reg;
3244 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003245
3246 /* enable normal train */
3247 reg = FDI_TX_CTL(pipe);
3248 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003249 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003250 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3251 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003252 } else {
3253 temp &= ~FDI_LINK_TRAIN_NONE;
3254 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003255 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003256 I915_WRITE(reg, temp);
3257
3258 reg = FDI_RX_CTL(pipe);
3259 temp = I915_READ(reg);
3260 if (HAS_PCH_CPT(dev)) {
3261 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3262 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3263 } else {
3264 temp &= ~FDI_LINK_TRAIN_NONE;
3265 temp |= FDI_LINK_TRAIN_NONE;
3266 }
3267 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3268
3269 /* wait one idle pattern time */
3270 POSTING_READ(reg);
3271 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003272
3273 /* IVB wants error correction enabled */
3274 if (IS_IVYBRIDGE(dev))
3275 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3276 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003277}
3278
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003279/* The FDI link training functions for ILK/Ibexpeak. */
3280static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003283 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3285 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003286 i915_reg_t reg;
3287 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003288
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003289 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003290 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003291
Adam Jacksone1a44742010-06-25 15:32:14 -04003292 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3293 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003294 reg = FDI_RX_IMR(pipe);
3295 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003296 temp &= ~FDI_RX_SYMBOL_LOCK;
3297 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003298 I915_WRITE(reg, temp);
3299 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003300 udelay(150);
3301
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003302 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003303 reg = FDI_TX_CTL(pipe);
3304 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003305 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003306 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003309 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003310
Chris Wilson5eddb702010-09-11 13:48:45 +01003311 reg = FDI_RX_CTL(pipe);
3312 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003313 temp &= ~FDI_LINK_TRAIN_NONE;
3314 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003315 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3316
3317 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003318 udelay(150);
3319
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003320 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003321 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3322 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3323 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003324
Chris Wilson5eddb702010-09-11 13:48:45 +01003325 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003326 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003327 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003328 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3329
3330 if ((temp & FDI_RX_BIT_LOCK)) {
3331 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003333 break;
3334 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003335 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003336 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003337 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003338
3339 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003340 reg = FDI_TX_CTL(pipe);
3341 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003345
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 reg = FDI_RX_CTL(pipe);
3347 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 I915_WRITE(reg, temp);
3351
3352 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 udelay(150);
3354
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003356 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003358 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3359
3360 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 DRM_DEBUG_KMS("FDI train 2 done.\n");
3363 break;
3364 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003366 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368
3369 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003370
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371}
3372
Akshay Joshi0206e352011-08-16 15:34:10 -04003373static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3375 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3376 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3377 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3378};
3379
3380/* The FDI link training functions for SNB/Cougarpoint. */
3381static void gen6_fdi_link_train(struct drm_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003384 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3386 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003387 i915_reg_t reg;
3388 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389
Adam Jacksone1a44742010-06-25 15:32:14 -04003390 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3391 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 reg = FDI_RX_IMR(pipe);
3393 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003394 temp &= ~FDI_RX_SYMBOL_LOCK;
3395 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 I915_WRITE(reg, temp);
3397
3398 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003399 udelay(150);
3400
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 reg = FDI_TX_CTL(pipe);
3403 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003404 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003405 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_PATTERN_1;
3408 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3409 /* SNB-B */
3410 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412
Daniel Vetterd74cf322012-10-26 10:58:13 +02003413 I915_WRITE(FDI_RX_MISC(pipe),
3414 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3415
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418 if (HAS_PCH_CPT(dev)) {
3419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3421 } else {
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_1;
3424 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3426
3427 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 udelay(150);
3429
Akshay Joshi0206e352011-08-16 15:34:10 -04003430 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 reg = FDI_TX_CTL(pipe);
3432 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3434 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 I915_WRITE(reg, temp);
3436
3437 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 udelay(500);
3439
Sean Paulfa37d392012-03-02 12:53:39 -05003440 for (retry = 0; retry < 5; retry++) {
3441 reg = FDI_RX_IIR(pipe);
3442 temp = I915_READ(reg);
3443 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3444 if (temp & FDI_RX_BIT_LOCK) {
3445 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3446 DRM_DEBUG_KMS("FDI train 1 done.\n");
3447 break;
3448 }
3449 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450 }
Sean Paulfa37d392012-03-02 12:53:39 -05003451 if (retry < 5)
3452 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 }
3454 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456
3457 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 reg = FDI_TX_CTL(pipe);
3459 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_2;
3462 if (IS_GEN6(dev)) {
3463 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3464 /* SNB-B */
3465 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3466 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 reg = FDI_RX_CTL(pipe);
3470 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 if (HAS_PCH_CPT(dev)) {
3472 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3473 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3474 } else {
3475 temp &= ~FDI_LINK_TRAIN_NONE;
3476 temp |= FDI_LINK_TRAIN_PATTERN_2;
3477 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 I915_WRITE(reg, temp);
3479
3480 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 udelay(150);
3482
Akshay Joshi0206e352011-08-16 15:34:10 -04003483 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 reg = FDI_TX_CTL(pipe);
3485 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 udelay(500);
3492
Sean Paulfa37d392012-03-02 12:53:39 -05003493 for (retry = 0; retry < 5; retry++) {
3494 reg = FDI_RX_IIR(pipe);
3495 temp = I915_READ(reg);
3496 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3497 if (temp & FDI_RX_SYMBOL_LOCK) {
3498 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3499 DRM_DEBUG_KMS("FDI train 2 done.\n");
3500 break;
3501 }
3502 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 }
Sean Paulfa37d392012-03-02 12:53:39 -05003504 if (retry < 5)
3505 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 }
3507 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509
3510 DRM_DEBUG_KMS("FDI train done.\n");
3511}
3512
Jesse Barnes357555c2011-04-28 15:09:55 -07003513/* Manual link training for Ivy Bridge A0 parts */
3514static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3515{
3516 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003517 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3519 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003520 i915_reg_t reg;
3521 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003522
3523 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3524 for train result */
3525 reg = FDI_RX_IMR(pipe);
3526 temp = I915_READ(reg);
3527 temp &= ~FDI_RX_SYMBOL_LOCK;
3528 temp &= ~FDI_RX_BIT_LOCK;
3529 I915_WRITE(reg, temp);
3530
3531 POSTING_READ(reg);
3532 udelay(150);
3533
Daniel Vetter01a415f2012-10-27 15:58:40 +02003534 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3535 I915_READ(FDI_RX_IIR(pipe)));
3536
Jesse Barnes139ccd32013-08-19 11:04:55 -07003537 /* Try each vswing and preemphasis setting twice before moving on */
3538 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3539 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003540 reg = FDI_TX_CTL(pipe);
3541 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003542 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3543 temp &= ~FDI_TX_ENABLE;
3544 I915_WRITE(reg, temp);
3545
3546 reg = FDI_RX_CTL(pipe);
3547 temp = I915_READ(reg);
3548 temp &= ~FDI_LINK_TRAIN_AUTO;
3549 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3550 temp &= ~FDI_RX_ENABLE;
3551 I915_WRITE(reg, temp);
3552
3553 /* enable CPU FDI TX and PCH FDI RX */
3554 reg = FDI_TX_CTL(pipe);
3555 temp = I915_READ(reg);
3556 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003557 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003558 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003559 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003560 temp |= snb_b_fdi_train_param[j/2];
3561 temp |= FDI_COMPOSITE_SYNC;
3562 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3563
3564 I915_WRITE(FDI_RX_MISC(pipe),
3565 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3566
3567 reg = FDI_RX_CTL(pipe);
3568 temp = I915_READ(reg);
3569 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3570 temp |= FDI_COMPOSITE_SYNC;
3571 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3572
3573 POSTING_READ(reg);
3574 udelay(1); /* should be 0.5us */
3575
3576 for (i = 0; i < 4; i++) {
3577 reg = FDI_RX_IIR(pipe);
3578 temp = I915_READ(reg);
3579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3580
3581 if (temp & FDI_RX_BIT_LOCK ||
3582 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3583 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3584 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3585 i);
3586 break;
3587 }
3588 udelay(1); /* should be 0.5us */
3589 }
3590 if (i == 4) {
3591 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3592 continue;
3593 }
3594
3595 /* Train 2 */
3596 reg = FDI_TX_CTL(pipe);
3597 temp = I915_READ(reg);
3598 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3599 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3600 I915_WRITE(reg, temp);
3601
3602 reg = FDI_RX_CTL(pipe);
3603 temp = I915_READ(reg);
3604 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3605 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003606 I915_WRITE(reg, temp);
3607
3608 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003609 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003610
Jesse Barnes139ccd32013-08-19 11:04:55 -07003611 for (i = 0; i < 4; i++) {
3612 reg = FDI_RX_IIR(pipe);
3613 temp = I915_READ(reg);
3614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003615
Jesse Barnes139ccd32013-08-19 11:04:55 -07003616 if (temp & FDI_RX_SYMBOL_LOCK ||
3617 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3618 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3619 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3620 i);
3621 goto train_done;
3622 }
3623 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003624 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003625 if (i == 4)
3626 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003627 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003628
Jesse Barnes139ccd32013-08-19 11:04:55 -07003629train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003630 DRM_DEBUG_KMS("FDI train done.\n");
3631}
3632
Daniel Vetter88cefb62012-08-12 19:27:14 +02003633static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003634{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003635 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003636 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003637 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003638 i915_reg_t reg;
3639 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003640
Jesse Barnes0e23b992010-09-10 11:10:00 -07003641 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003642 reg = FDI_RX_CTL(pipe);
3643 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003644 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003645 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003646 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003647 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3648
3649 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003650 udelay(200);
3651
3652 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003653 temp = I915_READ(reg);
3654 I915_WRITE(reg, temp | FDI_PCDCLK);
3655
3656 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003657 udelay(200);
3658
Paulo Zanoni20749732012-11-23 15:30:38 -02003659 /* Enable CPU FDI TX PLL, always on for Ironlake */
3660 reg = FDI_TX_CTL(pipe);
3661 temp = I915_READ(reg);
3662 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3663 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003664
Paulo Zanoni20749732012-11-23 15:30:38 -02003665 POSTING_READ(reg);
3666 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003667 }
3668}
3669
Daniel Vetter88cefb62012-08-12 19:27:14 +02003670static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3671{
3672 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003673 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02003674 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003675 i915_reg_t reg;
3676 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003677
3678 /* Switch from PCDclk to Rawclk */
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3682
3683 /* Disable CPU FDI TX PLL */
3684 reg = FDI_TX_CTL(pipe);
3685 temp = I915_READ(reg);
3686 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3687
3688 POSTING_READ(reg);
3689 udelay(100);
3690
3691 reg = FDI_RX_CTL(pipe);
3692 temp = I915_READ(reg);
3693 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3694
3695 /* Wait for the clocks to turn off. */
3696 POSTING_READ(reg);
3697 udelay(100);
3698}
3699
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003700static void ironlake_fdi_disable(struct drm_crtc *crtc)
3701{
3702 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003703 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3705 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003706 i915_reg_t reg;
3707 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003708
3709 /* disable CPU FDI tx and PCH FDI rx */
3710 reg = FDI_TX_CTL(pipe);
3711 temp = I915_READ(reg);
3712 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3713 POSTING_READ(reg);
3714
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003718 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003719 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3720
3721 POSTING_READ(reg);
3722 udelay(100);
3723
3724 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003725 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003726 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003727
3728 /* still set train pattern 1 */
3729 reg = FDI_TX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 temp &= ~FDI_LINK_TRAIN_NONE;
3732 temp |= FDI_LINK_TRAIN_PATTERN_1;
3733 I915_WRITE(reg, temp);
3734
3735 reg = FDI_RX_CTL(pipe);
3736 temp = I915_READ(reg);
3737 if (HAS_PCH_CPT(dev)) {
3738 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3739 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3740 } else {
3741 temp &= ~FDI_LINK_TRAIN_NONE;
3742 temp |= FDI_LINK_TRAIN_PATTERN_1;
3743 }
3744 /* BPC in FDI rx is consistent with that in PIPECONF */
3745 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003746 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003747 I915_WRITE(reg, temp);
3748
3749 POSTING_READ(reg);
3750 udelay(100);
3751}
3752
Chris Wilson5dce5b932014-01-20 10:17:36 +00003753bool intel_has_pending_fb_unpin(struct drm_device *dev)
3754{
3755 struct intel_crtc *crtc;
3756
3757 /* Note that we don't need to be called with mode_config.lock here
3758 * as our list of CRTC objects is static for the lifetime of the
3759 * device and so cannot disappear as we iterate. Similarly, we can
3760 * happily treat the predicates as racy, atomic checks as userspace
3761 * cannot claim and pin a new fb without at least acquring the
3762 * struct_mutex and so serialising with us.
3763 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003764 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003765 if (atomic_read(&crtc->unpin_work_count) == 0)
3766 continue;
3767
Daniel Vetter5a21b662016-05-24 17:13:53 +02003768 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003769 intel_wait_for_vblank(dev, crtc->pipe);
3770
3771 return true;
3772 }
3773
3774 return false;
3775}
3776
Daniel Vetter5a21b662016-05-24 17:13:53 +02003777static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003778{
3779 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003780 struct intel_flip_work *work = intel_crtc->flip_work;
3781
3782 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003783
3784 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003785 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003786
3787 drm_crtc_vblank_put(&intel_crtc->base);
3788
Daniel Vetter5a21b662016-05-24 17:13:53 +02003789 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003790 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003791
3792 trace_i915_flip_complete(intel_crtc->plane,
3793 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003794}
3795
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003796static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003797{
Chris Wilson0f911282012-04-17 10:05:38 +01003798 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003799 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003800 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003801
Daniel Vetter2c10d572012-12-20 21:24:07 +01003802 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003803
3804 ret = wait_event_interruptible_timeout(
3805 dev_priv->pending_flip_queue,
3806 !intel_crtc_has_pending_flip(crtc),
3807 60*HZ);
3808
3809 if (ret < 0)
3810 return ret;
3811
Daniel Vetter5a21b662016-05-24 17:13:53 +02003812 if (ret == 0) {
3813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3814 struct intel_flip_work *work;
3815
3816 spin_lock_irq(&dev->event_lock);
3817 work = intel_crtc->flip_work;
3818 if (work && !is_mmio_work(work)) {
3819 WARN_ONCE(1, "Removing stuck page flip\n");
3820 page_flip_completed(intel_crtc);
3821 }
3822 spin_unlock_irq(&dev->event_lock);
3823 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003824
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003825 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003826}
3827
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003828static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3829{
3830 u32 temp;
3831
3832 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3833
3834 mutex_lock(&dev_priv->sb_lock);
3835
3836 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3837 temp |= SBI_SSCCTL_DISABLE;
3838 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3839
3840 mutex_unlock(&dev_priv->sb_lock);
3841}
3842
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003843/* Program iCLKIP clock to the desired frequency */
3844static void lpt_program_iclkip(struct drm_crtc *crtc)
3845{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003846 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003847 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003848 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3849 u32 temp;
3850
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003851 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003852
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003853 /* The iCLK virtual clock root frequency is in MHz,
3854 * but the adjusted_mode->crtc_clock in in KHz. To get the
3855 * divisors, it is necessary to divide one by another, so we
3856 * convert the virtual clock precision to KHz here for higher
3857 * precision.
3858 */
3859 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003860 u32 iclk_virtual_root_freq = 172800 * 1000;
3861 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003862 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003863
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003864 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3865 clock << auxdiv);
3866 divsel = (desired_divisor / iclk_pi_range) - 2;
3867 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003868
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003869 /*
3870 * Near 20MHz is a corner case which is
3871 * out of range for the 7-bit divisor
3872 */
3873 if (divsel <= 0x7f)
3874 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003875 }
3876
3877 /* This should not happen with any sane values */
3878 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3879 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3880 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3881 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3882
3883 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 +03003884 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003885 auxdiv,
3886 divsel,
3887 phasedir,
3888 phaseinc);
3889
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003890 mutex_lock(&dev_priv->sb_lock);
3891
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003892 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003893 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003894 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3895 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3896 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3897 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3898 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3899 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003900 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901
3902 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003903 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003904 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3905 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003906 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003907
3908 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003909 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003911 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003912
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003913 mutex_unlock(&dev_priv->sb_lock);
3914
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915 /* Wait for initialization time */
3916 udelay(24);
3917
3918 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3919}
3920
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003921int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3922{
3923 u32 divsel, phaseinc, auxdiv;
3924 u32 iclk_virtual_root_freq = 172800 * 1000;
3925 u32 iclk_pi_range = 64;
3926 u32 desired_divisor;
3927 u32 temp;
3928
3929 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3930 return 0;
3931
3932 mutex_lock(&dev_priv->sb_lock);
3933
3934 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3935 if (temp & SBI_SSCCTL_DISABLE) {
3936 mutex_unlock(&dev_priv->sb_lock);
3937 return 0;
3938 }
3939
3940 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3941 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3942 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3943 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3944 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3945
3946 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3947 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3948 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3949
3950 mutex_unlock(&dev_priv->sb_lock);
3951
3952 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3953
3954 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3955 desired_divisor << auxdiv);
3956}
3957
Daniel Vetter275f01b22013-05-03 11:49:47 +02003958static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3959 enum pipe pch_transcoder)
3960{
3961 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003962 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003963 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003964
3965 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3966 I915_READ(HTOTAL(cpu_transcoder)));
3967 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3968 I915_READ(HBLANK(cpu_transcoder)));
3969 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3970 I915_READ(HSYNC(cpu_transcoder)));
3971
3972 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3973 I915_READ(VTOTAL(cpu_transcoder)));
3974 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3975 I915_READ(VBLANK(cpu_transcoder)));
3976 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3977 I915_READ(VSYNC(cpu_transcoder)));
3978 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3979 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3980}
3981
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003982static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003983{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003984 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003985 uint32_t temp;
3986
3987 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003988 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003989 return;
3990
3991 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3992 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3993
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003994 temp &= ~FDI_BC_BIFURCATION_SELECT;
3995 if (enable)
3996 temp |= FDI_BC_BIFURCATION_SELECT;
3997
3998 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003999 I915_WRITE(SOUTH_CHICKEN1, temp);
4000 POSTING_READ(SOUTH_CHICKEN1);
4001}
4002
4003static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4004{
4005 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004006
4007 switch (intel_crtc->pipe) {
4008 case PIPE_A:
4009 break;
4010 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004011 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004012 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004013 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004014 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004015
4016 break;
4017 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004018 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004019
4020 break;
4021 default:
4022 BUG();
4023 }
4024}
4025
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004026/* Return which DP Port should be selected for Transcoder DP control */
4027static enum port
4028intel_trans_dp_port_sel(struct drm_crtc *crtc)
4029{
4030 struct drm_device *dev = crtc->dev;
4031 struct intel_encoder *encoder;
4032
4033 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004034 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004035 encoder->type == INTEL_OUTPUT_EDP)
4036 return enc_to_dig_port(&encoder->base)->port;
4037 }
4038
4039 return -1;
4040}
4041
Jesse Barnesf67a5592011-01-05 10:31:48 -08004042/*
4043 * Enable PCH resources required for PCH ports:
4044 * - PCH PLLs
4045 * - FDI training & RX/TX
4046 * - update transcoder timings
4047 * - DP transcoding bits
4048 * - transcoder
4049 */
4050static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004051{
4052 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004053 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4055 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004056 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004057
Daniel Vetterab9412b2013-05-03 11:49:46 +02004058 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004059
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060 if (IS_IVYBRIDGE(dev))
4061 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4062
Daniel Vettercd986ab2012-10-26 10:58:12 +02004063 /* Write the TU size bits before fdi link training, so that error
4064 * detection works. */
4065 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4066 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4067
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004068 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004069 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004070
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004071 /* We need to program the right clock selection before writing the pixel
4072 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004073 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004074 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004075
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004076 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004077 temp |= TRANS_DPLL_ENABLE(pipe);
4078 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004079 if (intel_crtc->config->shared_dpll ==
4080 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004081 temp |= sel;
4082 else
4083 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004084 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004085 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004086
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004087 /* XXX: pch pll's can be enabled any time before we enable the PCH
4088 * transcoder, and we actually should do this to not upset any PCH
4089 * transcoder that already use the clock when we share it.
4090 *
4091 * Note that enable_shared_dpll tries to do the right thing, but
4092 * get_shared_dpll unconditionally resets the pll - we need that to have
4093 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004094 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004095
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004096 /* set transcoder timing, panel must allow it */
4097 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004098 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004099
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004100 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004101
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004102 /* For PCH DP, enable TRANS_DP_CTL */
Ville Syrjälä37a56502016-06-22 21:57:04 +03004103 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004104 const struct drm_display_mode *adjusted_mode =
4105 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004106 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004107 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004108 temp = I915_READ(reg);
4109 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004110 TRANS_DP_SYNC_MASK |
4111 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004112 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004113 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004115 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004116 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004117 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004118 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119
4120 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004121 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004122 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004124 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004127 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004128 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129 break;
4130 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004131 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 }
4133
Chris Wilson5eddb702010-09-11 13:48:45 +01004134 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 }
4136
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004137 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004138}
4139
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004140static void lpt_pch_enable(struct drm_crtc *crtc)
4141{
4142 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004143 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004145 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004146
Daniel Vetterab9412b2013-05-03 11:49:46 +02004147 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004148
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004149 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004150
Paulo Zanoni0540e482012-10-31 18:12:40 -02004151 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004152 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004153
Paulo Zanoni937bb612012-10-31 18:12:47 -02004154 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004155}
4156
Daniel Vettera1520312013-05-03 11:49:50 +02004157static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004158{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004159 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004160 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004161 u32 temp;
4162
4163 temp = I915_READ(dslreg);
4164 udelay(500);
4165 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004166 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004167 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004168 }
4169}
4170
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004171static int
4172skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4173 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4174 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004175{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004176 struct intel_crtc_scaler_state *scaler_state =
4177 &crtc_state->scaler_state;
4178 struct intel_crtc *intel_crtc =
4179 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004180 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004181
4182 need_scaling = intel_rotation_90_or_270(rotation) ?
4183 (src_h != dst_w || src_w != dst_h):
4184 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004185
4186 /*
4187 * if plane is being disabled or scaler is no more required or force detach
4188 * - free scaler binded to this plane/crtc
4189 * - in order to do this, update crtc->scaler_usage
4190 *
4191 * Here scaler state in crtc_state is set free so that
4192 * scaler can be assigned to other user. Actual register
4193 * update to free the scaler is done in plane/panel-fit programming.
4194 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4195 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004196 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004197 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004198 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004199 scaler_state->scalers[*scaler_id].in_use = 0;
4200
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004201 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4202 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4203 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004204 scaler_state->scaler_users);
4205 *scaler_id = -1;
4206 }
4207 return 0;
4208 }
4209
4210 /* range checks */
4211 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4212 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4213
4214 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4215 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004216 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004217 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004218 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004219 return -EINVAL;
4220 }
4221
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004222 /* mark this plane as a scaler user in crtc_state */
4223 scaler_state->scaler_users |= (1 << scaler_user);
4224 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4225 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4226 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4227 scaler_state->scaler_users);
4228
4229 return 0;
4230}
4231
4232/**
4233 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4234 *
4235 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004236 *
4237 * Return
4238 * 0 - scaler_usage updated successfully
4239 * error - requested scaling cannot be supported or other error condition
4240 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004241int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004242{
4243 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004244 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004245
Ville Syrjälä78108b72016-05-27 20:59:19 +03004246 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4247 intel_crtc->base.base.id, intel_crtc->base.name,
4248 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004249
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004250 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004251 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004252 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004253 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004254}
4255
4256/**
4257 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4258 *
4259 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004260 * @plane_state: atomic plane state to update
4261 *
4262 * Return
4263 * 0 - scaler_usage updated successfully
4264 * error - requested scaling cannot be supported or other error condition
4265 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004266static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4267 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004268{
4269
4270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004271 struct intel_plane *intel_plane =
4272 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004273 struct drm_framebuffer *fb = plane_state->base.fb;
4274 int ret;
4275
4276 bool force_detach = !fb || !plane_state->visible;
4277
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004278 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4279 intel_plane->base.base.id, intel_plane->base.name,
4280 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004281
4282 ret = skl_update_scaler(crtc_state, force_detach,
4283 drm_plane_index(&intel_plane->base),
4284 &plane_state->scaler_id,
4285 plane_state->base.rotation,
4286 drm_rect_width(&plane_state->src) >> 16,
4287 drm_rect_height(&plane_state->src) >> 16,
4288 drm_rect_width(&plane_state->dst),
4289 drm_rect_height(&plane_state->dst));
4290
4291 if (ret || plane_state->scaler_id < 0)
4292 return ret;
4293
Chandra Kondurua1b22782015-04-07 15:28:45 -07004294 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004295 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004296 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4297 intel_plane->base.base.id,
4298 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004299 return -EINVAL;
4300 }
4301
4302 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004303 switch (fb->pixel_format) {
4304 case DRM_FORMAT_RGB565:
4305 case DRM_FORMAT_XBGR8888:
4306 case DRM_FORMAT_XRGB8888:
4307 case DRM_FORMAT_ABGR8888:
4308 case DRM_FORMAT_ARGB8888:
4309 case DRM_FORMAT_XRGB2101010:
4310 case DRM_FORMAT_XBGR2101010:
4311 case DRM_FORMAT_YUYV:
4312 case DRM_FORMAT_YVYU:
4313 case DRM_FORMAT_UYVY:
4314 case DRM_FORMAT_VYUY:
4315 break;
4316 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004317 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4318 intel_plane->base.base.id, intel_plane->base.name,
4319 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004320 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004321 }
4322
Chandra Kondurua1b22782015-04-07 15:28:45 -07004323 return 0;
4324}
4325
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004326static void skylake_scaler_disable(struct intel_crtc *crtc)
4327{
4328 int i;
4329
4330 for (i = 0; i < crtc->num_scalers; i++)
4331 skl_detach_scaler(crtc, i);
4332}
4333
4334static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004335{
4336 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004337 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004338 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004339 struct intel_crtc_scaler_state *scaler_state =
4340 &crtc->config->scaler_state;
4341
4342 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4343
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004344 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004345 int id;
4346
4347 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4348 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4349 return;
4350 }
4351
4352 id = scaler_state->scaler_id;
4353 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4354 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4355 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4356 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4357
4358 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004359 }
4360}
4361
Jesse Barnesb074cec2013-04-25 12:55:02 -07004362static void ironlake_pfit_enable(struct intel_crtc *crtc)
4363{
4364 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004365 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004366 int pipe = crtc->pipe;
4367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004368 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004369 /* Force use of hard-coded filter coefficients
4370 * as some pre-programmed values are broken,
4371 * e.g. x201.
4372 */
4373 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4374 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4375 PF_PIPE_SEL_IVB(pipe));
4376 else
4377 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004378 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4379 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004380 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004381}
4382
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004383void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004384{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004385 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004386 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004387
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004388 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004389 return;
4390
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004391 /*
4392 * We can only enable IPS after we enable a plane and wait for a vblank
4393 * This function is called from post_plane_update, which is run after
4394 * a vblank wait.
4395 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004396
Paulo Zanonid77e4532013-09-24 13:52:55 -03004397 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004398 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004399 mutex_lock(&dev_priv->rps.hw_lock);
4400 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4401 mutex_unlock(&dev_priv->rps.hw_lock);
4402 /* Quoting Art Runyan: "its not safe to expect any particular
4403 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004404 * mailbox." Moreover, the mailbox may return a bogus state,
4405 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004406 */
4407 } else {
4408 I915_WRITE(IPS_CTL, IPS_ENABLE);
4409 /* The bit only becomes 1 in the next vblank, so this wait here
4410 * is essentially intel_wait_for_vblank. If we don't have this
4411 * and don't wait for vblanks until the end of crtc_enable, then
4412 * the HW state readout code will complain that the expected
4413 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004414 if (intel_wait_for_register(dev_priv,
4415 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4416 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004417 DRM_ERROR("Timed out waiting for IPS enable\n");
4418 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004419}
4420
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004421void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004422{
4423 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004424 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004425
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004426 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004427 return;
4428
4429 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004430 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004431 mutex_lock(&dev_priv->rps.hw_lock);
4432 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4433 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004434 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004435 if (intel_wait_for_register(dev_priv,
4436 IPS_CTL, IPS_ENABLE, 0,
4437 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004438 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004439 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004440 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004441 POSTING_READ(IPS_CTL);
4442 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004443
4444 /* We need to wait for a vblank before we can disable the plane. */
4445 intel_wait_for_vblank(dev, crtc->pipe);
4446}
4447
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004448static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004449{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004450 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004451 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004452 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004453
4454 mutex_lock(&dev->struct_mutex);
4455 dev_priv->mm.interruptible = false;
4456 (void) intel_overlay_switch_off(intel_crtc->overlay);
4457 dev_priv->mm.interruptible = true;
4458 mutex_unlock(&dev->struct_mutex);
4459 }
4460
4461 /* Let userspace switch the overlay on again. In most cases userspace
4462 * has to recompute where to put it anyway.
4463 */
4464}
4465
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004466/**
4467 * intel_post_enable_primary - Perform operations after enabling primary plane
4468 * @crtc: the CRTC whose primary plane was just enabled
4469 *
4470 * Performs potentially sleeping operations that must be done after the primary
4471 * plane is enabled, such as updating FBC and IPS. Note that this may be
4472 * called due to an explicit primary plane update, or due to an implicit
4473 * re-enable that is caused when a sprite plane is updated to no longer
4474 * completely hide the primary plane.
4475 */
4476static void
4477intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004478{
4479 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004480 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4482 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004483
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004484 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004485 * FIXME IPS should be fine as long as one plane is
4486 * enabled, but in practice it seems to have problems
4487 * when going from primary only to sprite only and vice
4488 * versa.
4489 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004490 hsw_enable_ips(intel_crtc);
4491
Daniel Vetterf99d7062014-06-19 16:01:59 +02004492 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004493 * Gen2 reports pipe underruns whenever all planes are disabled.
4494 * So don't enable underrun reporting before at least some planes
4495 * are enabled.
4496 * FIXME: Need to fix the logic to work when we turn off all planes
4497 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004498 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004499 if (IS_GEN2(dev))
4500 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4501
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004502 /* Underruns don't always raise interrupts, so check manually. */
4503 intel_check_cpu_fifo_underruns(dev_priv);
4504 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004505}
4506
Ville Syrjälä2622a082016-03-09 19:07:26 +02004507/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004508static void
4509intel_pre_disable_primary(struct drm_crtc *crtc)
4510{
4511 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004512 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4514 int pipe = intel_crtc->pipe;
4515
4516 /*
4517 * Gen2 reports pipe underruns whenever all planes are disabled.
4518 * So diasble underrun reporting before all the planes get disabled.
4519 * FIXME: Need to fix the logic to work when we turn off all planes
4520 * but leave the pipe running.
4521 */
4522 if (IS_GEN2(dev))
4523 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4524
4525 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004526 * FIXME IPS should be fine as long as one plane is
4527 * enabled, but in practice it seems to have problems
4528 * when going from primary only to sprite only and vice
4529 * versa.
4530 */
4531 hsw_disable_ips(intel_crtc);
4532}
4533
4534/* FIXME get rid of this and use pre_plane_update */
4535static void
4536intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4537{
4538 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004539 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4541 int pipe = intel_crtc->pipe;
4542
4543 intel_pre_disable_primary(crtc);
4544
4545 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004546 * Vblank time updates from the shadow to live plane control register
4547 * are blocked if the memory self-refresh mode is active at that
4548 * moment. So to make sure the plane gets truly disabled, disable
4549 * first the self-refresh mode. The self-refresh enable bit in turn
4550 * will be checked/applied by the HW only at the next frame start
4551 * event which is after the vblank start event, so we need to have a
4552 * wait-for-vblank between disabling the plane and the pipe.
4553 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004554 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004555 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004556 dev_priv->wm.vlv.cxsr = false;
4557 intel_wait_for_vblank(dev, pipe);
4558 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004559}
4560
Daniel Vetter5a21b662016-05-24 17:13:53 +02004561static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4562{
4563 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4564 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4565 struct intel_crtc_state *pipe_config =
4566 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004567 struct drm_plane *primary = crtc->base.primary;
4568 struct drm_plane_state *old_pri_state =
4569 drm_atomic_get_existing_plane_state(old_state, primary);
4570
Chris Wilson5748b6a2016-08-04 16:32:38 +01004571 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004572
4573 crtc->wm.cxsr_allowed = true;
4574
4575 if (pipe_config->update_wm_post && pipe_config->base.active)
4576 intel_update_watermarks(&crtc->base);
4577
4578 if (old_pri_state) {
4579 struct intel_plane_state *primary_state =
4580 to_intel_plane_state(primary->state);
4581 struct intel_plane_state *old_primary_state =
4582 to_intel_plane_state(old_pri_state);
4583
4584 intel_fbc_post_update(crtc);
4585
4586 if (primary_state->visible &&
4587 (needs_modeset(&pipe_config->base) ||
4588 !old_primary_state->visible))
4589 intel_post_enable_primary(&crtc->base);
4590 }
4591}
4592
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004593static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004594{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004595 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004596 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004597 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004598 struct intel_crtc_state *pipe_config =
4599 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004600 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4601 struct drm_plane *primary = crtc->base.primary;
4602 struct drm_plane_state *old_pri_state =
4603 drm_atomic_get_existing_plane_state(old_state, primary);
4604 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004605
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004606 if (old_pri_state) {
4607 struct intel_plane_state *primary_state =
4608 to_intel_plane_state(primary->state);
4609 struct intel_plane_state *old_primary_state =
4610 to_intel_plane_state(old_pri_state);
4611
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004612 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004613
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004614 if (old_primary_state->visible &&
4615 (modeset || !primary_state->visible))
4616 intel_pre_disable_primary(&crtc->base);
4617 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004618
David Weinehalla4015f92016-05-19 15:50:36 +03004619 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004620 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004621
Ville Syrjälä2622a082016-03-09 19:07:26 +02004622 /*
4623 * Vblank time updates from the shadow to live plane control register
4624 * are blocked if the memory self-refresh mode is active at that
4625 * moment. So to make sure the plane gets truly disabled, disable
4626 * first the self-refresh mode. The self-refresh enable bit in turn
4627 * will be checked/applied by the HW only at the next frame start
4628 * event which is after the vblank start event, so we need to have a
4629 * wait-for-vblank between disabling the plane and the pipe.
4630 */
4631 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004632 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004633 dev_priv->wm.vlv.cxsr = false;
4634 intel_wait_for_vblank(dev, crtc->pipe);
4635 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004636 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004637
Matt Ropered4a6a72016-02-23 17:20:13 -08004638 /*
4639 * IVB workaround: must disable low power watermarks for at least
4640 * one frame before enabling scaling. LP watermarks can be re-enabled
4641 * when scaling is disabled.
4642 *
4643 * WaCxSRDisabledForSpriteScaling:ivb
4644 */
4645 if (pipe_config->disable_lp_wm) {
4646 ilk_disable_lp_wm(dev);
4647 intel_wait_for_vblank(dev, crtc->pipe);
4648 }
4649
4650 /*
4651 * If we're doing a modeset, we're done. No need to do any pre-vblank
4652 * watermark programming here.
4653 */
4654 if (needs_modeset(&pipe_config->base))
4655 return;
4656
4657 /*
4658 * For platforms that support atomic watermarks, program the
4659 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4660 * will be the intermediate values that are safe for both pre- and
4661 * post- vblank; when vblank happens, the 'active' values will be set
4662 * to the final 'target' values and we'll do this again to get the
4663 * optimal watermarks. For gen9+ platforms, the values we program here
4664 * will be the final target values which will get automatically latched
4665 * at vblank time; no further programming will be necessary.
4666 *
4667 * If a platform hasn't been transitioned to atomic watermarks yet,
4668 * we'll continue to update watermarks the old way, if flags tell
4669 * us to.
4670 */
4671 if (dev_priv->display.initial_watermarks != NULL)
4672 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004673 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004674 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004675}
4676
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004677static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004678{
4679 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004680 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004681 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004682 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004683
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004684 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004685
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004686 drm_for_each_plane_mask(p, dev, plane_mask)
4687 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004688
Daniel Vetterf99d7062014-06-19 16:01:59 +02004689 /*
4690 * FIXME: Once we grow proper nuclear flip support out of this we need
4691 * to compute the mask of flip planes precisely. For the time being
4692 * consider this a flip to a NULL plane.
4693 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01004694 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004695}
4696
Jesse Barnesf67a5592011-01-05 10:31:48 -08004697static void ironlake_crtc_enable(struct drm_crtc *crtc)
4698{
4699 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004700 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004702 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004703 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004704 struct intel_crtc_state *pipe_config =
4705 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004706
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004707 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004708 return;
4709
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004710 /*
4711 * Sometimes spurious CPU pipe underruns happen during FDI
4712 * training, at least with VGA+HDMI cloning. Suppress them.
4713 *
4714 * On ILK we get an occasional spurious CPU pipe underruns
4715 * between eDP port A enable and vdd enable. Also PCH port
4716 * enable seems to result in the occasional CPU pipe underrun.
4717 *
4718 * Spurious PCH underruns also occur during PCH enabling.
4719 */
4720 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4721 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004722 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004723 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4724
4725 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004726 intel_prepare_shared_dpll(intel_crtc);
4727
Ville Syrjälä37a56502016-06-22 21:57:04 +03004728 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304729 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004730
4731 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004732 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004733
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004734 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004735 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004736 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004737 }
4738
4739 ironlake_set_pipeconf(crtc);
4740
Jesse Barnesf67a5592011-01-05 10:31:48 -08004741 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004742
Daniel Vetterf6736a12013-06-05 13:34:30 +02004743 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004744 if (encoder->pre_enable)
4745 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004746
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004747 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004748 /* Note: FDI PLL enabling _must_ be done before we enable the
4749 * cpu pipes, hence this is separate from all the other fdi/pch
4750 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004751 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004752 } else {
4753 assert_fdi_tx_disabled(dev_priv, pipe);
4754 assert_fdi_rx_disabled(dev_priv, pipe);
4755 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004756
Jesse Barnesb074cec2013-04-25 12:55:02 -07004757 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004758
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004759 /*
4760 * On ILK+ LUT must be loaded before the pipe is running but with
4761 * clocks enabled
4762 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004763 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004764
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004765 if (dev_priv->display.initial_watermarks != NULL)
4766 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004767 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004768
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004769 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004770 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004771
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004772 assert_vblank_disabled(crtc);
4773 drm_crtc_vblank_on(crtc);
4774
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004775 for_each_encoder_on_crtc(dev, crtc, encoder)
4776 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004777
4778 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004779 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004780
4781 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4782 if (intel_crtc->config->has_pch_encoder)
4783 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004784 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004785 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004786}
4787
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004788/* IPS only exists on ULT machines and is tied to pipe A. */
4789static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4790{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004791 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004792}
4793
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004794static void haswell_crtc_enable(struct drm_crtc *crtc)
4795{
4796 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004797 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4799 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004800 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004801 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004802 struct intel_crtc_state *pipe_config =
4803 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004804
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004805 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004806 return;
4807
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004808 if (intel_crtc->config->has_pch_encoder)
4809 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4810 false);
4811
Imre Deak95a7a2a2016-06-13 16:44:35 +03004812 for_each_encoder_on_crtc(dev, crtc, encoder)
4813 if (encoder->pre_pll_enable)
4814 encoder->pre_pll_enable(encoder);
4815
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004816 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004817 intel_enable_shared_dpll(intel_crtc);
4818
Ville Syrjälä37a56502016-06-22 21:57:04 +03004819 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304820 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004821
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004822 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004823 intel_set_pipe_timings(intel_crtc);
4824
Jani Nikulabc58be62016-03-18 17:05:39 +02004825 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004826
Jani Nikula4d1de972016-03-18 17:05:42 +02004827 if (cpu_transcoder != TRANSCODER_EDP &&
4828 !transcoder_is_dsi(cpu_transcoder)) {
4829 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004830 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004831 }
4832
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004833 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004834 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004835 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004836 }
4837
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004838 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004839 haswell_set_pipeconf(crtc);
4840
Jani Nikula391bf042016-03-18 17:05:40 +02004841 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004842
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004843 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004844
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004845 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004846
Daniel Vetter6b698512015-11-28 11:05:39 +01004847 if (intel_crtc->config->has_pch_encoder)
4848 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4849 else
4850 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4851
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304852 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004853 if (encoder->pre_enable)
4854 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304855 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004856
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004857 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004858 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004859
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004860 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304861 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004862
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004863 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004864 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004865 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004866 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004867
4868 /*
4869 * On ILK+ LUT must be loaded before the pipe is running but with
4870 * clocks enabled
4871 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004872 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004873
Paulo Zanoni1f544382012-10-24 11:32:00 -02004874 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004875 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304876 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004877
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004878 if (dev_priv->display.initial_watermarks != NULL)
4879 dev_priv->display.initial_watermarks(pipe_config);
4880 else
4881 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004882
4883 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004884 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004885 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004886
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004887 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004888 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004889
Jani Nikulaa65347b2015-11-27 12:21:46 +02004890 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004891 intel_ddi_set_vc_payload_alloc(crtc, true);
4892
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004893 assert_vblank_disabled(crtc);
4894 drm_crtc_vblank_on(crtc);
4895
Jani Nikula8807e552013-08-30 19:40:32 +03004896 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004897 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004898 intel_opregion_notify_encoder(encoder, true);
4899 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004900
Daniel Vetter6b698512015-11-28 11:05:39 +01004901 if (intel_crtc->config->has_pch_encoder) {
4902 intel_wait_for_vblank(dev, pipe);
4903 intel_wait_for_vblank(dev, pipe);
4904 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004905 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4906 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004907 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004908
Paulo Zanonie4916942013-09-20 16:21:19 -03004909 /* If we change the relative order between pipe/planes enabling, we need
4910 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004911 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4912 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4913 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4914 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4915 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004916}
4917
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004918static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004919{
4920 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004921 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004922 int pipe = crtc->pipe;
4923
4924 /* To avoid upsetting the power well on haswell only disable the pfit if
4925 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004926 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004927 I915_WRITE(PF_CTL(pipe), 0);
4928 I915_WRITE(PF_WIN_POS(pipe), 0);
4929 I915_WRITE(PF_WIN_SZ(pipe), 0);
4930 }
4931}
4932
Jesse Barnes6be4a602010-09-10 10:26:01 -07004933static void ironlake_crtc_disable(struct drm_crtc *crtc)
4934{
4935 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004936 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004938 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004939 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004940
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004941 /*
4942 * Sometimes spurious CPU pipe underruns happen when the
4943 * pipe is already disabled, but FDI RX/TX is still enabled.
4944 * Happens at least with VGA+HDMI cloning. Suppress them.
4945 */
4946 if (intel_crtc->config->has_pch_encoder) {
4947 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004948 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004949 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004950
Daniel Vetterea9d7582012-07-10 10:42:52 +02004951 for_each_encoder_on_crtc(dev, crtc, encoder)
4952 encoder->disable(encoder);
4953
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004954 drm_crtc_vblank_off(crtc);
4955 assert_vblank_disabled(crtc);
4956
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004957 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004958
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004959 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004960
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004961 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004962 ironlake_fdi_disable(crtc);
4963
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004964 for_each_encoder_on_crtc(dev, crtc, encoder)
4965 if (encoder->post_disable)
4966 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004967
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004968 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004969 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004970
Daniel Vetterd925c592013-06-05 13:34:04 +02004971 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004972 i915_reg_t reg;
4973 u32 temp;
4974
Daniel Vetterd925c592013-06-05 13:34:04 +02004975 /* disable TRANS_DP_CTL */
4976 reg = TRANS_DP_CTL(pipe);
4977 temp = I915_READ(reg);
4978 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4979 TRANS_DP_PORT_SEL_MASK);
4980 temp |= TRANS_DP_PORT_SEL_NONE;
4981 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982
Daniel Vetterd925c592013-06-05 13:34:04 +02004983 /* disable DPLL_SEL */
4984 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004985 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004986 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004987 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004988
Daniel Vetterd925c592013-06-05 13:34:04 +02004989 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004990 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004991
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004992 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004993 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004994}
4995
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004996static void haswell_crtc_disable(struct drm_crtc *crtc)
4997{
4998 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004999 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5001 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005002 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005003
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005004 if (intel_crtc->config->has_pch_encoder)
5005 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5006 false);
5007
Jani Nikula8807e552013-08-30 19:40:32 +03005008 for_each_encoder_on_crtc(dev, crtc, encoder) {
5009 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005010 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005011 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005012
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005013 drm_crtc_vblank_off(crtc);
5014 assert_vblank_disabled(crtc);
5015
Jani Nikula4d1de972016-03-18 17:05:42 +02005016 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005017 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005018 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005019
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005020 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005021 intel_ddi_set_vc_payload_alloc(crtc, false);
5022
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005023 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305024 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005026 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005027 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005028 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005029 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005030
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005031 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305032 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005033
Imre Deak97b040a2014-06-25 22:01:50 +03005034 for_each_encoder_on_crtc(dev, crtc, encoder)
5035 if (encoder->post_disable)
5036 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005037
Ville Syrjälä92966a32015-12-08 16:05:48 +02005038 if (intel_crtc->config->has_pch_encoder) {
5039 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005040 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005041 intel_ddi_fdi_disable(crtc);
5042
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005043 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5044 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005045 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005046}
5047
Jesse Barnes2dd24552013-04-25 12:55:01 -07005048static void i9xx_pfit_enable(struct intel_crtc *crtc)
5049{
5050 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005051 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005052 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005053
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005054 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005055 return;
5056
Daniel Vetterc0b03412013-05-28 12:05:54 +02005057 /*
5058 * The panel fitter should only be adjusted whilst the pipe is disabled,
5059 * according to register description and PRM.
5060 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005061 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5062 assert_pipe_disabled(dev_priv, crtc->pipe);
5063
Jesse Barnesb074cec2013-04-25 12:55:02 -07005064 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5065 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005066
5067 /* Border color in case we don't scale up to the full screen. Black by
5068 * default, change to something else for debugging. */
5069 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005070}
5071
Dave Airlied05410f2014-06-05 13:22:59 +10005072static enum intel_display_power_domain port_to_power_domain(enum port port)
5073{
5074 switch (port) {
5075 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005076 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005077 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005078 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005079 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005080 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005081 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005082 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005083 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005084 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005085 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005086 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005087 return POWER_DOMAIN_PORT_OTHER;
5088 }
5089}
5090
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005091static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5092{
5093 switch (port) {
5094 case PORT_A:
5095 return POWER_DOMAIN_AUX_A;
5096 case PORT_B:
5097 return POWER_DOMAIN_AUX_B;
5098 case PORT_C:
5099 return POWER_DOMAIN_AUX_C;
5100 case PORT_D:
5101 return POWER_DOMAIN_AUX_D;
5102 case PORT_E:
5103 /* FIXME: Check VBT for actual wiring of PORT E */
5104 return POWER_DOMAIN_AUX_D;
5105 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005106 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005107 return POWER_DOMAIN_AUX_A;
5108 }
5109}
5110
Imre Deak319be8a2014-03-04 19:22:57 +02005111enum intel_display_power_domain
5112intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005113{
Imre Deak319be8a2014-03-04 19:22:57 +02005114 struct drm_device *dev = intel_encoder->base.dev;
5115 struct intel_digital_port *intel_dig_port;
5116
5117 switch (intel_encoder->type) {
5118 case INTEL_OUTPUT_UNKNOWN:
5119 /* Only DDI platforms should ever use this output type */
5120 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005121 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005122 case INTEL_OUTPUT_HDMI:
5123 case INTEL_OUTPUT_EDP:
5124 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005125 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005126 case INTEL_OUTPUT_DP_MST:
5127 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5128 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005129 case INTEL_OUTPUT_ANALOG:
5130 return POWER_DOMAIN_PORT_CRT;
5131 case INTEL_OUTPUT_DSI:
5132 return POWER_DOMAIN_PORT_DSI;
5133 default:
5134 return POWER_DOMAIN_PORT_OTHER;
5135 }
5136}
5137
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005138enum intel_display_power_domain
5139intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5140{
5141 struct drm_device *dev = intel_encoder->base.dev;
5142 struct intel_digital_port *intel_dig_port;
5143
5144 switch (intel_encoder->type) {
5145 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005146 case INTEL_OUTPUT_HDMI:
5147 /*
5148 * Only DDI platforms should ever use these output types.
5149 * We can get here after the HDMI detect code has already set
5150 * the type of the shared encoder. Since we can't be sure
5151 * what's the status of the given connectors, play safe and
5152 * run the DP detection too.
5153 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005154 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005155 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005156 case INTEL_OUTPUT_EDP:
5157 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5158 return port_to_aux_power_domain(intel_dig_port->port);
5159 case INTEL_OUTPUT_DP_MST:
5160 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5161 return port_to_aux_power_domain(intel_dig_port->port);
5162 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005163 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005164 return POWER_DOMAIN_AUX_A;
5165 }
5166}
5167
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005168static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5169 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005170{
5171 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005172 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5174 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005175 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005176 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005177
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005178 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005179 return 0;
5180
Imre Deak77d22dc2014-03-05 16:20:52 +02005181 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5182 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005183 if (crtc_state->pch_pfit.enabled ||
5184 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005185 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5186
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005187 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5188 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5189
Imre Deak319be8a2014-03-04 19:22:57 +02005190 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005191 }
Imre Deak319be8a2014-03-04 19:22:57 +02005192
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005193 if (crtc_state->shared_dpll)
5194 mask |= BIT(POWER_DOMAIN_PLLS);
5195
Imre Deak77d22dc2014-03-05 16:20:52 +02005196 return mask;
5197}
5198
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005199static unsigned long
5200modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5201 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005202{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005203 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5205 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005206 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005207
5208 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005209 intel_crtc->enabled_power_domains = new_domains =
5210 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005211
Daniel Vetter5a21b662016-05-24 17:13:53 +02005212 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005213
5214 for_each_power_domain(domain, domains)
5215 intel_display_power_get(dev_priv, domain);
5216
Daniel Vetter5a21b662016-05-24 17:13:53 +02005217 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005218}
5219
5220static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5221 unsigned long domains)
5222{
5223 enum intel_display_power_domain domain;
5224
5225 for_each_power_domain(domain, domains)
5226 intel_display_power_put(dev_priv, domain);
5227}
5228
Mika Kaholaadafdc62015-08-18 14:36:59 +03005229static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5230{
5231 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5232
5233 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5234 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5235 return max_cdclk_freq;
5236 else if (IS_CHERRYVIEW(dev_priv))
5237 return max_cdclk_freq*95/100;
5238 else if (INTEL_INFO(dev_priv)->gen < 4)
5239 return 2*max_cdclk_freq*90/100;
5240 else
5241 return max_cdclk_freq*90/100;
5242}
5243
Ville Syrjäläb2045352016-05-13 23:41:27 +03005244static int skl_calc_cdclk(int max_pixclk, int vco);
5245
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005246static void intel_update_max_cdclk(struct drm_device *dev)
5247{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005248 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005249
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005250 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005251 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005252 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005253
Ville Syrjäläb2045352016-05-13 23:41:27 +03005254 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005255 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005256
5257 /*
5258 * Use the lower (vco 8640) cdclk values as a
5259 * first guess. skl_calc_cdclk() will correct it
5260 * if the preferred vco is 8100 instead.
5261 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005262 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005263 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005264 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005265 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005266 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005267 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005268 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005269 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005270
5271 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005272 } else if (IS_BROXTON(dev)) {
5273 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005274 } else if (IS_BROADWELL(dev)) {
5275 /*
5276 * FIXME with extra cooling we can allow
5277 * 540 MHz for ULX and 675 Mhz for ULT.
5278 * How can we know if extra cooling is
5279 * available? PCI ID, VTB, something else?
5280 */
5281 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5282 dev_priv->max_cdclk_freq = 450000;
5283 else if (IS_BDW_ULX(dev))
5284 dev_priv->max_cdclk_freq = 450000;
5285 else if (IS_BDW_ULT(dev))
5286 dev_priv->max_cdclk_freq = 540000;
5287 else
5288 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005289 } else if (IS_CHERRYVIEW(dev)) {
5290 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005291 } else if (IS_VALLEYVIEW(dev)) {
5292 dev_priv->max_cdclk_freq = 400000;
5293 } else {
5294 /* otherwise assume cdclk is fixed */
5295 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5296 }
5297
Mika Kaholaadafdc62015-08-18 14:36:59 +03005298 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5299
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005300 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5301 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005302
5303 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5304 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005305}
5306
5307static void intel_update_cdclk(struct drm_device *dev)
5308{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005309 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005310
5311 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005312
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005313 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005314 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5315 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5316 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005317 else
5318 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5319 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005320
5321 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005322 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5323 * Programmng [sic] note: bit[9:2] should be programmed to the number
5324 * of cdclk that generates 4MHz reference clock freq which is used to
5325 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005326 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005327 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005328 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005329}
5330
Ville Syrjälä92891e42016-05-11 22:44:45 +03005331/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5332static int skl_cdclk_decimal(int cdclk)
5333{
5334 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5335}
5336
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005337static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5338{
5339 int ratio;
5340
5341 if (cdclk == dev_priv->cdclk_pll.ref)
5342 return 0;
5343
5344 switch (cdclk) {
5345 default:
5346 MISSING_CASE(cdclk);
5347 case 144000:
5348 case 288000:
5349 case 384000:
5350 case 576000:
5351 ratio = 60;
5352 break;
5353 case 624000:
5354 ratio = 65;
5355 break;
5356 }
5357
5358 return dev_priv->cdclk_pll.ref * ratio;
5359}
5360
Ville Syrjälä2b730012016-05-13 23:41:34 +03005361static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5362{
5363 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5364
5365 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005366 if (intel_wait_for_register(dev_priv,
5367 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5368 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005369 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005370
5371 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005372}
5373
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005374static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005375{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005376 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005377 u32 val;
5378
5379 val = I915_READ(BXT_DE_PLL_CTL);
5380 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005381 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005382 I915_WRITE(BXT_DE_PLL_CTL, val);
5383
5384 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5385
5386 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005387 if (intel_wait_for_register(dev_priv,
5388 BXT_DE_PLL_ENABLE,
5389 BXT_DE_PLL_LOCK,
5390 BXT_DE_PLL_LOCK,
5391 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005392 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005393
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005394 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005395}
5396
Imre Deak324513c2016-06-13 16:44:36 +03005397static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305398{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005399 u32 val, divider;
5400 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305401
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005402 vco = bxt_de_pll_vco(dev_priv, cdclk);
5403
5404 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5405
5406 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5407 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5408 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305409 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305410 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005411 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305412 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305413 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005414 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305415 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305416 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005417 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305418 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305419 break;
5420 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005421 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5422 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305423
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005424 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5425 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305426 }
5427
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305428 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005429 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305430 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5431 0x80000000);
5432 mutex_unlock(&dev_priv->rps.hw_lock);
5433
5434 if (ret) {
5435 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005436 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305437 return;
5438 }
5439
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005440 if (dev_priv->cdclk_pll.vco != 0 &&
5441 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005442 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305443
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005444 if (dev_priv->cdclk_pll.vco != vco)
5445 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305446
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005447 val = divider | skl_cdclk_decimal(cdclk);
5448 /*
5449 * FIXME if only the cd2x divider needs changing, it could be done
5450 * without shutting off the pipe (if only one pipe is active).
5451 */
5452 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5453 /*
5454 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5455 * enable otherwise.
5456 */
5457 if (cdclk >= 500000)
5458 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5459 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305460
5461 mutex_lock(&dev_priv->rps.hw_lock);
5462 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005463 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305464 mutex_unlock(&dev_priv->rps.hw_lock);
5465
5466 if (ret) {
5467 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005468 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305469 return;
5470 }
5471
Chris Wilson91c8a322016-07-05 10:40:23 +01005472 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305473}
5474
Imre Deakd66a2192016-05-24 15:38:33 +03005475static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305476{
Imre Deakd66a2192016-05-24 15:38:33 +03005477 u32 cdctl, expected;
5478
Chris Wilson91c8a322016-07-05 10:40:23 +01005479 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305480
Imre Deakd66a2192016-05-24 15:38:33 +03005481 if (dev_priv->cdclk_pll.vco == 0 ||
5482 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5483 goto sanitize;
5484
5485 /* DPLL okay; verify the cdclock
5486 *
5487 * Some BIOS versions leave an incorrect decimal frequency value and
5488 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5489 * so sanitize this register.
5490 */
5491 cdctl = I915_READ(CDCLK_CTL);
5492 /*
5493 * Let's ignore the pipe field, since BIOS could have configured the
5494 * dividers both synching to an active pipe, or asynchronously
5495 * (PIPE_NONE).
5496 */
5497 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5498
5499 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5500 skl_cdclk_decimal(dev_priv->cdclk_freq);
5501 /*
5502 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5503 * enable otherwise.
5504 */
5505 if (dev_priv->cdclk_freq >= 500000)
5506 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5507
5508 if (cdctl == expected)
5509 /* All well; nothing to sanitize */
5510 return;
5511
5512sanitize:
5513 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5514
5515 /* force cdclk programming */
5516 dev_priv->cdclk_freq = 0;
5517
5518 /* force full PLL disable + enable */
5519 dev_priv->cdclk_pll.vco = -1;
5520}
5521
Imre Deak324513c2016-06-13 16:44:36 +03005522void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005523{
5524 bxt_sanitize_cdclk(dev_priv);
5525
5526 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005527 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005528
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305529 /*
5530 * FIXME:
5531 * - The initial CDCLK needs to be read from VBT.
5532 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305533 */
Imre Deak324513c2016-06-13 16:44:36 +03005534 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305535}
5536
Imre Deak324513c2016-06-13 16:44:36 +03005537void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305538{
Imre Deak324513c2016-06-13 16:44:36 +03005539 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305540}
5541
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005542static int skl_calc_cdclk(int max_pixclk, int vco)
5543{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005544 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005545 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005546 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005547 else if (max_pixclk > 432000)
5548 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005549 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005550 return 432000;
5551 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005552 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005553 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005554 if (max_pixclk > 540000)
5555 return 675000;
5556 else if (max_pixclk > 450000)
5557 return 540000;
5558 else if (max_pixclk > 337500)
5559 return 450000;
5560 else
5561 return 337500;
5562 }
5563}
5564
Ville Syrjäläea617912016-05-13 23:41:24 +03005565static void
5566skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005567{
Ville Syrjäläea617912016-05-13 23:41:24 +03005568 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005569
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005570 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005571 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005572
Ville Syrjäläea617912016-05-13 23:41:24 +03005573 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005574 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005575 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005576
Imre Deak1c3f7702016-05-24 15:38:32 +03005577 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5578 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005579
Ville Syrjäläea617912016-05-13 23:41:24 +03005580 val = I915_READ(DPLL_CTRL1);
5581
Imre Deak1c3f7702016-05-24 15:38:32 +03005582 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5583 DPLL_CTRL1_SSC(SKL_DPLL0) |
5584 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5585 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5586 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005587
Ville Syrjäläea617912016-05-13 23:41:24 +03005588 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5589 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5590 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5591 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5592 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005593 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005594 break;
5595 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5596 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005597 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005598 break;
5599 default:
5600 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005601 break;
5602 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005603}
5604
Ville Syrjäläb2045352016-05-13 23:41:27 +03005605void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5606{
5607 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5608
5609 dev_priv->skl_preferred_vco_freq = vco;
5610
5611 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01005612 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005613}
5614
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005615static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005616skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005617{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005618 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005619 u32 val;
5620
Ville Syrjälä63911d72016-05-13 23:41:32 +03005621 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005622
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005623 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005624 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005625 I915_WRITE(CDCLK_CTL, val);
5626 POSTING_READ(CDCLK_CTL);
5627
5628 /*
5629 * We always enable DPLL0 with the lowest link rate possible, but still
5630 * taking into account the VCO required to operate the eDP panel at the
5631 * desired frequency. The usual DP link rates operate with a VCO of
5632 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5633 * The modeset code is responsible for the selection of the exact link
5634 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005635 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005636 */
5637 val = I915_READ(DPLL_CTRL1);
5638
5639 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5640 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5641 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005642 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005643 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5644 SKL_DPLL0);
5645 else
5646 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5647 SKL_DPLL0);
5648
5649 I915_WRITE(DPLL_CTRL1, val);
5650 POSTING_READ(DPLL_CTRL1);
5651
5652 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5653
Chris Wilsone24ca052016-06-30 15:33:05 +01005654 if (intel_wait_for_register(dev_priv,
5655 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5656 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005657 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005658
Ville Syrjälä63911d72016-05-13 23:41:32 +03005659 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005660
5661 /* We'll want to keep using the current vco from now on. */
5662 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005663}
5664
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005665static void
5666skl_dpll0_disable(struct drm_i915_private *dev_priv)
5667{
5668 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01005669 if (intel_wait_for_register(dev_priv,
5670 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5671 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005672 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005673
Ville Syrjälä63911d72016-05-13 23:41:32 +03005674 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005675}
5676
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005677static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5678{
5679 int ret;
5680 u32 val;
5681
5682 /* inform PCU we want to change CDCLK */
5683 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5684 mutex_lock(&dev_priv->rps.hw_lock);
5685 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5686 mutex_unlock(&dev_priv->rps.hw_lock);
5687
5688 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5689}
5690
5691static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5692{
Ville Syrjälä848496e2016-07-13 16:32:03 +03005693 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005694}
5695
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005696static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005697{
Chris Wilson91c8a322016-07-05 10:40:23 +01005698 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005699 u32 freq_select, pcu_ack;
5700
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005701 WARN_ON((cdclk == 24000) != (vco == 0));
5702
Ville Syrjälä63911d72016-05-13 23:41:32 +03005703 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005704
5705 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5706 DRM_ERROR("failed to inform PCU about cdclk change\n");
5707 return;
5708 }
5709
5710 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005711 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005712 case 450000:
5713 case 432000:
5714 freq_select = CDCLK_FREQ_450_432;
5715 pcu_ack = 1;
5716 break;
5717 case 540000:
5718 freq_select = CDCLK_FREQ_540;
5719 pcu_ack = 2;
5720 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005721 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005722 case 337500:
5723 default:
5724 freq_select = CDCLK_FREQ_337_308;
5725 pcu_ack = 0;
5726 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005727 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005728 case 675000:
5729 freq_select = CDCLK_FREQ_675_617;
5730 pcu_ack = 3;
5731 break;
5732 }
5733
Ville Syrjälä63911d72016-05-13 23:41:32 +03005734 if (dev_priv->cdclk_pll.vco != 0 &&
5735 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005736 skl_dpll0_disable(dev_priv);
5737
Ville Syrjälä63911d72016-05-13 23:41:32 +03005738 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005739 skl_dpll0_enable(dev_priv, vco);
5740
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005741 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005742 POSTING_READ(CDCLK_CTL);
5743
5744 /* inform PCU of the change */
5745 mutex_lock(&dev_priv->rps.hw_lock);
5746 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5747 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005748
5749 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005750}
5751
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005752static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5753
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005754void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5755{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005756 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005757}
5758
5759void skl_init_cdclk(struct drm_i915_private *dev_priv)
5760{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005761 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005762
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005763 skl_sanitize_cdclk(dev_priv);
5764
Ville Syrjälä63911d72016-05-13 23:41:32 +03005765 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005766 /*
5767 * Use the current vco as our initial
5768 * guess as to what the preferred vco is.
5769 */
5770 if (dev_priv->skl_preferred_vco_freq == 0)
5771 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005772 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005773 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005774 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005775
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005776 vco = dev_priv->skl_preferred_vco_freq;
5777 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005778 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005779 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005780
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005781 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005782}
5783
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005784static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305785{
Ville Syrjälä09492492016-05-13 23:41:28 +03005786 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305787
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305788 /*
5789 * check if the pre-os intialized the display
5790 * There is SWF18 scratchpad register defined which is set by the
5791 * pre-os which can be used by the OS drivers to check the status
5792 */
5793 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5794 goto sanitize;
5795
Chris Wilson91c8a322016-07-05 10:40:23 +01005796 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03005797 /* Is PLL enabled and locked ? */
5798 if (dev_priv->cdclk_pll.vco == 0 ||
5799 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5800 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005801
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305802 /* DPLL okay; verify the cdclock
5803 *
5804 * Noticed in some instances that the freq selection is correct but
5805 * decimal part is programmed wrong from BIOS where pre-os does not
5806 * enable display. Verify the same as well.
5807 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005808 cdctl = I915_READ(CDCLK_CTL);
5809 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5810 skl_cdclk_decimal(dev_priv->cdclk_freq);
5811 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305812 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005813 return;
5814
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305815sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005816 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005817
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005818 /* force cdclk programming */
5819 dev_priv->cdclk_freq = 0;
5820 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005821 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305822}
5823
Jesse Barnes30a970c2013-11-04 13:48:12 -08005824/* Adjust CDclk dividers to allow high res or save power if possible */
5825static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5826{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005827 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005828 u32 val, cmd;
5829
Vandana Kannan164dfd22014-11-24 13:37:41 +05305830 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5831 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005832
Ville Syrjälädfcab172014-06-13 13:37:47 +03005833 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005835 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005836 cmd = 1;
5837 else
5838 cmd = 0;
5839
5840 mutex_lock(&dev_priv->rps.hw_lock);
5841 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5842 val &= ~DSPFREQGUAR_MASK;
5843 val |= (cmd << DSPFREQGUAR_SHIFT);
5844 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5845 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5846 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5847 50)) {
5848 DRM_ERROR("timed out waiting for CDclk change\n");
5849 }
5850 mutex_unlock(&dev_priv->rps.hw_lock);
5851
Ville Syrjälä54433e92015-05-26 20:42:31 +03005852 mutex_lock(&dev_priv->sb_lock);
5853
Ville Syrjälädfcab172014-06-13 13:37:47 +03005854 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005855 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005857 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005858
Jesse Barnes30a970c2013-11-04 13:48:12 -08005859 /* adjust cdclk divider */
5860 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005861 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862 val |= divider;
5863 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005864
5865 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005866 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005867 50))
5868 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005869 }
5870
Jesse Barnes30a970c2013-11-04 13:48:12 -08005871 /* adjust self-refresh exit latency value */
5872 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5873 val &= ~0x7f;
5874
5875 /*
5876 * For high bandwidth configs, we set a higher latency in the bunit
5877 * so that the core display fetch happens in time to avoid underruns.
5878 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005879 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 val |= 4500 / 250; /* 4.5 usec */
5881 else
5882 val |= 3000 / 250; /* 3.0 usec */
5883 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005884
Ville Syrjäläa5805162015-05-26 20:42:30 +03005885 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886
Ville Syrjäläb6283052015-06-03 15:45:07 +03005887 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005888}
5889
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005890static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5891{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005892 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005893 u32 val, cmd;
5894
Vandana Kannan164dfd22014-11-24 13:37:41 +05305895 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5896 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005897
5898 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005899 case 333333:
5900 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005901 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005902 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005903 break;
5904 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005905 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005906 return;
5907 }
5908
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005909 /*
5910 * Specs are full of misinformation, but testing on actual
5911 * hardware has shown that we just need to write the desired
5912 * CCK divider into the Punit register.
5913 */
5914 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5915
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005916 mutex_lock(&dev_priv->rps.hw_lock);
5917 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5918 val &= ~DSPFREQGUAR_MASK_CHV;
5919 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5920 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5921 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5922 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5923 50)) {
5924 DRM_ERROR("timed out waiting for CDclk change\n");
5925 }
5926 mutex_unlock(&dev_priv->rps.hw_lock);
5927
Ville Syrjäläb6283052015-06-03 15:45:07 +03005928 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005929}
5930
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5932 int max_pixclk)
5933{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005934 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005935 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005936
Jesse Barnes30a970c2013-11-04 13:48:12 -08005937 /*
5938 * Really only a few cases to deal with, as only 4 CDclks are supported:
5939 * 200MHz
5940 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005941 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005942 * 400MHz (VLV only)
5943 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5944 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005945 *
5946 * We seem to get an unstable or solid color picture at 200MHz.
5947 * Not sure what's wrong. For now use 200MHz only when all pipes
5948 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005950 if (!IS_CHERRYVIEW(dev_priv) &&
5951 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005952 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005953 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005954 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005955 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005956 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005957 else
5958 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959}
5960
Imre Deak324513c2016-06-13 16:44:36 +03005961static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005962{
Ville Syrjälä760e1472016-05-11 22:44:46 +03005963 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305964 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005965 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305966 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005967 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305968 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005969 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305970 return 288000;
5971 else
5972 return 144000;
5973}
5974
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005975/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005976static int intel_mode_max_pixclk(struct drm_device *dev,
5977 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005978{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005979 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01005980 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005981 struct drm_crtc *crtc;
5982 struct drm_crtc_state *crtc_state;
5983 unsigned max_pixclk = 0, i;
5984 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005985
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005986 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5987 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005988
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005989 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5990 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005991
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005992 if (crtc_state->enable)
5993 pixclk = crtc_state->adjusted_mode.crtc_clock;
5994
5995 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005996 }
5997
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005998 for_each_pipe(dev_priv, pipe)
5999 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6000
Jesse Barnes30a970c2013-11-04 13:48:12 -08006001 return max_pixclk;
6002}
6003
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006004static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006005{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006006 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006007 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006008 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006009 struct intel_atomic_state *intel_state =
6010 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006011
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006012 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006013 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306014
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006015 if (!intel_state->active_crtcs)
6016 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6017
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006018 return 0;
6019}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006020
Imre Deak324513c2016-06-13 16:44:36 +03006021static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006022{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006023 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006024 struct intel_atomic_state *intel_state =
6025 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006026
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006027 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006028 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006029
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006030 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006031 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006032
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006033 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006034}
6035
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006036static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6037{
6038 unsigned int credits, default_credits;
6039
6040 if (IS_CHERRYVIEW(dev_priv))
6041 default_credits = PFI_CREDIT(12);
6042 else
6043 default_credits = PFI_CREDIT(8);
6044
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006045 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006046 /* CHV suggested value is 31 or 63 */
6047 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006048 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006049 else
6050 credits = PFI_CREDIT(15);
6051 } else {
6052 credits = default_credits;
6053 }
6054
6055 /*
6056 * WA - write default credits before re-programming
6057 * FIXME: should we also set the resend bit here?
6058 */
6059 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6060 default_credits);
6061
6062 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6063 credits | PFI_CREDIT_RESEND);
6064
6065 /*
6066 * FIXME is this guaranteed to clear
6067 * immediately or should we poll for it?
6068 */
6069 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6070}
6071
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006072static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006073{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006074 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006075 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006076 struct intel_atomic_state *old_intel_state =
6077 to_intel_atomic_state(old_state);
6078 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006079
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006080 /*
6081 * FIXME: We can end up here with all power domains off, yet
6082 * with a CDCLK frequency other than the minimum. To account
6083 * for this take the PIPE-A power domain, which covers the HW
6084 * blocks needed for the following programming. This can be
6085 * removed once it's guaranteed that we get here either with
6086 * the minimum CDCLK set, or the required power domains
6087 * enabled.
6088 */
6089 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006090
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006091 if (IS_CHERRYVIEW(dev))
6092 cherryview_set_cdclk(dev, req_cdclk);
6093 else
6094 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006095
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006096 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006097
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006098 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006099}
6100
Jesse Barnes89b667f2013-04-18 14:51:36 -07006101static void valleyview_crtc_enable(struct drm_crtc *crtc)
6102{
6103 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006104 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6106 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006107 struct intel_crtc_state *pipe_config =
6108 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006109 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006110
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006111 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006112 return;
6113
Ville Syrjälä37a56502016-06-22 21:57:04 +03006114 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306115 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006116
6117 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006118 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006119
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006120 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006121 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006122
6123 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6124 I915_WRITE(CHV_CANVAS(pipe), 0);
6125 }
6126
Daniel Vetter5b18e572014-04-24 23:55:06 +02006127 i9xx_set_pipeconf(intel_crtc);
6128
Jesse Barnes89b667f2013-04-18 14:51:36 -07006129 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006130
Daniel Vettera72e4c92014-09-30 10:56:47 +02006131 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006132
Jesse Barnes89b667f2013-04-18 14:51:36 -07006133 for_each_encoder_on_crtc(dev, crtc, encoder)
6134 if (encoder->pre_pll_enable)
6135 encoder->pre_pll_enable(encoder);
6136
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006137 if (IS_CHERRYVIEW(dev)) {
6138 chv_prepare_pll(intel_crtc, intel_crtc->config);
6139 chv_enable_pll(intel_crtc, intel_crtc->config);
6140 } else {
6141 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6142 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006143 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006144
6145 for_each_encoder_on_crtc(dev, crtc, encoder)
6146 if (encoder->pre_enable)
6147 encoder->pre_enable(encoder);
6148
Jesse Barnes2dd24552013-04-25 12:55:01 -07006149 i9xx_pfit_enable(intel_crtc);
6150
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006151 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006152
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006153 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006154 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006155
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006156 assert_vblank_disabled(crtc);
6157 drm_crtc_vblank_on(crtc);
6158
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006159 for_each_encoder_on_crtc(dev, crtc, encoder)
6160 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006161}
6162
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006163static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6164{
6165 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006166 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006167
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006168 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6169 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006170}
6171
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006172static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006173{
6174 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006175 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006177 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006178 struct intel_crtc_state *pipe_config =
6179 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006180 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006181
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006182 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006183 return;
6184
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006185 i9xx_set_pll_dividers(intel_crtc);
6186
Ville Syrjälä37a56502016-06-22 21:57:04 +03006187 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306188 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006189
6190 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006191 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006192
Daniel Vetter5b18e572014-04-24 23:55:06 +02006193 i9xx_set_pipeconf(intel_crtc);
6194
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006195 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006196
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006197 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006198 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006199
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006200 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006201 if (encoder->pre_enable)
6202 encoder->pre_enable(encoder);
6203
Daniel Vetterf6736a12013-06-05 13:34:30 +02006204 i9xx_enable_pll(intel_crtc);
6205
Jesse Barnes2dd24552013-04-25 12:55:01 -07006206 i9xx_pfit_enable(intel_crtc);
6207
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006208 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006209
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006210 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006211 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006212
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006213 assert_vblank_disabled(crtc);
6214 drm_crtc_vblank_on(crtc);
6215
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006216 for_each_encoder_on_crtc(dev, crtc, encoder)
6217 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006218}
6219
Daniel Vetter87476d62013-04-11 16:29:06 +02006220static void i9xx_pfit_disable(struct intel_crtc *crtc)
6221{
6222 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006223 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006224
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006225 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006226 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006227
6228 assert_pipe_disabled(dev_priv, crtc->pipe);
6229
Daniel Vetter328d8e82013-05-08 10:36:31 +02006230 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6231 I915_READ(PFIT_CONTROL));
6232 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006233}
6234
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006235static void i9xx_crtc_disable(struct drm_crtc *crtc)
6236{
6237 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006238 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006240 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006241 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006242
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006243 /*
6244 * On gen2 planes are double buffered but the pipe isn't, so we must
6245 * wait for planes to fully turn off before disabling the pipe.
6246 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006247 if (IS_GEN2(dev))
6248 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006249
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006250 for_each_encoder_on_crtc(dev, crtc, encoder)
6251 encoder->disable(encoder);
6252
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006253 drm_crtc_vblank_off(crtc);
6254 assert_vblank_disabled(crtc);
6255
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006256 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006257
Daniel Vetter87476d62013-04-11 16:29:06 +02006258 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006259
Jesse Barnes89b667f2013-04-18 14:51:36 -07006260 for_each_encoder_on_crtc(dev, crtc, encoder)
6261 if (encoder->post_disable)
6262 encoder->post_disable(encoder);
6263
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006264 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006265 if (IS_CHERRYVIEW(dev))
6266 chv_disable_pll(dev_priv, pipe);
6267 else if (IS_VALLEYVIEW(dev))
6268 vlv_disable_pll(dev_priv, pipe);
6269 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006270 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006271 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006272
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006273 for_each_encoder_on_crtc(dev, crtc, encoder)
6274 if (encoder->post_pll_disable)
6275 encoder->post_pll_disable(encoder);
6276
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006277 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006278 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006279}
6280
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006281static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006282{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006283 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006285 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006286 enum intel_display_power_domain domain;
6287 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006288
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006289 if (!intel_crtc->active)
6290 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006291
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006292 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006293 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006294
Ville Syrjälä2622a082016-03-09 19:07:26 +02006295 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006296
6297 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6298 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006299 }
6300
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006301 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006302
Ville Syrjälä78108b72016-05-27 20:59:19 +03006303 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6304 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006305
6306 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6307 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006308 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006309 crtc->enabled = false;
6310 crtc->state->connector_mask = 0;
6311 crtc->state->encoder_mask = 0;
6312
6313 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6314 encoder->base.crtc = NULL;
6315
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006316 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006317 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006318 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006319
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006320 domains = intel_crtc->enabled_power_domains;
6321 for_each_power_domain(domain, domains)
6322 intel_display_power_put(dev_priv, domain);
6323 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006324
6325 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6326 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006327}
6328
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006329/*
6330 * turn all crtc's off, but do not adjust state
6331 * This has to be paired with a call to intel_modeset_setup_hw_state.
6332 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006333int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006334{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006335 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006336 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006337 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006338
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006339 state = drm_atomic_helper_suspend(dev);
6340 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006341 if (ret)
6342 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006343 else
6344 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006345 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006346}
6347
Chris Wilsonea5b2132010-08-04 13:50:23 +01006348void intel_encoder_destroy(struct drm_encoder *encoder)
6349{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006350 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006351
Chris Wilsonea5b2132010-08-04 13:50:23 +01006352 drm_encoder_cleanup(encoder);
6353 kfree(intel_encoder);
6354}
6355
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006356/* Cross check the actual hw state with our own modeset state tracking (and it's
6357 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006358static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006359{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006360 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006361
6362 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6363 connector->base.base.id,
6364 connector->base.name);
6365
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006366 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006367 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006368 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006369
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006370 I915_STATE_WARN(!crtc,
6371 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006372
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006373 if (!crtc)
6374 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006375
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006376 I915_STATE_WARN(!crtc->state->active,
6377 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006378
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006379 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006380 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006381
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006382 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006383 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006384
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006385 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006386 "attached encoder crtc differs from connector crtc\n");
6387 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006388 I915_STATE_WARN(crtc && crtc->state->active,
6389 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006390 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006391 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006392 }
6393}
6394
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006395int intel_connector_init(struct intel_connector *connector)
6396{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006397 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006398
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006399 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006400 return -ENOMEM;
6401
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006402 return 0;
6403}
6404
6405struct intel_connector *intel_connector_alloc(void)
6406{
6407 struct intel_connector *connector;
6408
6409 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6410 if (!connector)
6411 return NULL;
6412
6413 if (intel_connector_init(connector) < 0) {
6414 kfree(connector);
6415 return NULL;
6416 }
6417
6418 return connector;
6419}
6420
Daniel Vetterf0947c32012-07-02 13:10:34 +02006421/* Simple connector->get_hw_state implementation for encoders that support only
6422 * one connector and no cloning and hence the encoder state determines the state
6423 * of the connector. */
6424bool intel_connector_get_hw_state(struct intel_connector *connector)
6425{
Daniel Vetter24929352012-07-02 20:28:59 +02006426 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006427 struct intel_encoder *encoder = connector->encoder;
6428
6429 return encoder->get_hw_state(encoder, &pipe);
6430}
6431
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006432static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006433{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6435 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006436
6437 return 0;
6438}
6439
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006440static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006441 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006442{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006443 struct drm_atomic_state *state = pipe_config->base.state;
6444 struct intel_crtc *other_crtc;
6445 struct intel_crtc_state *other_crtc_state;
6446
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006447 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6448 pipe_name(pipe), pipe_config->fdi_lanes);
6449 if (pipe_config->fdi_lanes > 4) {
6450 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6451 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006453 }
6454
Paulo Zanonibafb6552013-11-02 21:07:44 -07006455 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006456 if (pipe_config->fdi_lanes > 2) {
6457 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6458 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006459 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006460 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006462 }
6463 }
6464
6465 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006466 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006467
6468 /* Ivybridge 3 pipe is really complicated */
6469 switch (pipe) {
6470 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006471 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006472 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006473 if (pipe_config->fdi_lanes <= 2)
6474 return 0;
6475
6476 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6477 other_crtc_state =
6478 intel_atomic_get_crtc_state(state, other_crtc);
6479 if (IS_ERR(other_crtc_state))
6480 return PTR_ERR(other_crtc_state);
6481
6482 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006483 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6484 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006486 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006487 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006488 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006489 if (pipe_config->fdi_lanes > 2) {
6490 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6491 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006492 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006493 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006494
6495 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6496 other_crtc_state =
6497 intel_atomic_get_crtc_state(state, other_crtc);
6498 if (IS_ERR(other_crtc_state))
6499 return PTR_ERR(other_crtc_state);
6500
6501 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006502 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006504 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006505 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006506 default:
6507 BUG();
6508 }
6509}
6510
Daniel Vettere29c22c2013-02-21 00:00:16 +01006511#define RETRY 1
6512static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006513 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006514{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006515 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006516 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006517 int lane, link_bw, fdi_dotclock, ret;
6518 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006519
Daniel Vettere29c22c2013-02-21 00:00:16 +01006520retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006521 /* FDI is a binary signal running at ~2.7GHz, encoding
6522 * each output octet as 10 bits. The actual frequency
6523 * is stored as a divider into a 100MHz clock, and the
6524 * mode pixel clock is stored in units of 1KHz.
6525 * Hence the bw of each lane in terms of the mode signal
6526 * is:
6527 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006528 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006529
Damien Lespiau241bfc32013-09-25 16:45:37 +01006530 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006531
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006532 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006533 pipe_config->pipe_bpp);
6534
6535 pipe_config->fdi_lanes = lane;
6536
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006537 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006538 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006539
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006540 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006541 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006542 pipe_config->pipe_bpp -= 2*3;
6543 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6544 pipe_config->pipe_bpp);
6545 needs_recompute = true;
6546 pipe_config->bw_constrained = true;
6547
6548 goto retry;
6549 }
6550
6551 if (needs_recompute)
6552 return RETRY;
6553
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006554 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006555}
6556
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006557static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6558 struct intel_crtc_state *pipe_config)
6559{
6560 if (pipe_config->pipe_bpp > 24)
6561 return false;
6562
6563 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006564 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006565 return true;
6566
6567 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006568 * We compare against max which means we must take
6569 * the increased cdclk requirement into account when
6570 * calculating the new cdclk.
6571 *
6572 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006573 */
6574 return ilk_pipe_pixel_rate(pipe_config) <=
6575 dev_priv->max_cdclk_freq * 95 / 100;
6576}
6577
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006578static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006579 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006580{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006581 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006582 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006583
Jani Nikulad330a952014-01-21 11:24:25 +02006584 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006585 hsw_crtc_supports_ips(crtc) &&
6586 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006587}
6588
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006589static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6590{
6591 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6592
6593 /* GDG double wide on either pipe, otherwise pipe A only */
6594 return INTEL_INFO(dev_priv)->gen < 4 &&
6595 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6596}
6597
Daniel Vettera43f6e02013-06-07 23:10:32 +02006598static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006599 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006600{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006601 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006602 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006603 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006604 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006605
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006606 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006607 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006608
6609 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006610 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006611 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006612 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006613 if (intel_crtc_supports_double_wide(crtc) &&
6614 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006615 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006616 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006617 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006618 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006619
Ville Syrjäläf3261152016-05-24 21:34:18 +03006620 if (adjusted_mode->crtc_clock > clock_limit) {
6621 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6622 adjusted_mode->crtc_clock, clock_limit,
6623 yesno(pipe_config->double_wide));
6624 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006625 }
Chris Wilson89749352010-09-12 18:25:19 +01006626
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006627 /*
6628 * Pipe horizontal size must be even in:
6629 * - DVO ganged mode
6630 * - LVDS dual channel mode
6631 * - Double wide pipe
6632 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006633 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006634 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6635 pipe_config->pipe_src_w &= ~1;
6636
Damien Lespiau8693a822013-05-03 18:48:11 +01006637 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6638 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006639 */
6640 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006641 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006642 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006643
Damien Lespiauf5adf942013-06-24 18:29:34 +01006644 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006645 hsw_compute_ips_config(crtc, pipe_config);
6646
Daniel Vetter877d48d2013-04-19 11:24:43 +02006647 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006648 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006649
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006650 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006651}
6652
Ville Syrjälä1652d192015-03-31 14:12:01 +03006653static int skylake_get_display_clock_speed(struct drm_device *dev)
6654{
6655 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006656 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006657
Ville Syrjäläea617912016-05-13 23:41:24 +03006658 skl_dpll0_update(dev_priv);
6659
Ville Syrjälä63911d72016-05-13 23:41:32 +03006660 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006661 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006662
Ville Syrjäläea617912016-05-13 23:41:24 +03006663 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006664
Ville Syrjälä63911d72016-05-13 23:41:32 +03006665 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006666 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6667 case CDCLK_FREQ_450_432:
6668 return 432000;
6669 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006670 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006671 case CDCLK_FREQ_540:
6672 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006673 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006674 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006675 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006676 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006677 }
6678 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006679 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6680 case CDCLK_FREQ_450_432:
6681 return 450000;
6682 case CDCLK_FREQ_337_308:
6683 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006684 case CDCLK_FREQ_540:
6685 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006686 case CDCLK_FREQ_675_617:
6687 return 675000;
6688 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006689 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006690 }
6691 }
6692
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006693 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006694}
6695
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006696static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6697{
6698 u32 val;
6699
6700 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006701 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006702
6703 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006704 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006705 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006706
Imre Deak1c3f7702016-05-24 15:38:32 +03006707 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6708 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006709
6710 val = I915_READ(BXT_DE_PLL_CTL);
6711 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6712 dev_priv->cdclk_pll.ref;
6713}
6714
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006715static int broxton_get_display_clock_speed(struct drm_device *dev)
6716{
6717 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006718 u32 divider;
6719 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006720
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006721 bxt_de_pll_update(dev_priv);
6722
Ville Syrjäläf5986242016-05-13 23:41:37 +03006723 vco = dev_priv->cdclk_pll.vco;
6724 if (vco == 0)
6725 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006726
Ville Syrjäläf5986242016-05-13 23:41:37 +03006727 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006728
Ville Syrjäläf5986242016-05-13 23:41:37 +03006729 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006730 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006731 div = 2;
6732 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006733 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006734 div = 3;
6735 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006736 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006737 div = 4;
6738 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006739 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006740 div = 8;
6741 break;
6742 default:
6743 MISSING_CASE(divider);
6744 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006745 }
6746
Ville Syrjäläf5986242016-05-13 23:41:37 +03006747 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006748}
6749
Ville Syrjälä1652d192015-03-31 14:12:01 +03006750static int broadwell_get_display_clock_speed(struct drm_device *dev)
6751{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006752 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006753 uint32_t lcpll = I915_READ(LCPLL_CTL);
6754 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6755
6756 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6757 return 800000;
6758 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6759 return 450000;
6760 else if (freq == LCPLL_CLK_FREQ_450)
6761 return 450000;
6762 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6763 return 540000;
6764 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6765 return 337500;
6766 else
6767 return 675000;
6768}
6769
6770static int haswell_get_display_clock_speed(struct drm_device *dev)
6771{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006772 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006773 uint32_t lcpll = I915_READ(LCPLL_CTL);
6774 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6775
6776 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6777 return 800000;
6778 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6779 return 450000;
6780 else if (freq == LCPLL_CLK_FREQ_450)
6781 return 450000;
6782 else if (IS_HSW_ULT(dev))
6783 return 337500;
6784 else
6785 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006786}
6787
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006788static int valleyview_get_display_clock_speed(struct drm_device *dev)
6789{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006790 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6791 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006792}
6793
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006794static int ilk_get_display_clock_speed(struct drm_device *dev)
6795{
6796 return 450000;
6797}
6798
Jesse Barnese70236a2009-09-21 10:42:27 -07006799static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006800{
Jesse Barnese70236a2009-09-21 10:42:27 -07006801 return 400000;
6802}
Jesse Barnes79e53942008-11-07 14:24:08 -08006803
Jesse Barnese70236a2009-09-21 10:42:27 -07006804static int i915_get_display_clock_speed(struct drm_device *dev)
6805{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006806 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006807}
Jesse Barnes79e53942008-11-07 14:24:08 -08006808
Jesse Barnese70236a2009-09-21 10:42:27 -07006809static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6810{
6811 return 200000;
6812}
Jesse Barnes79e53942008-11-07 14:24:08 -08006813
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006814static int pnv_get_display_clock_speed(struct drm_device *dev)
6815{
6816 u16 gcfgc = 0;
6817
6818 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6819
6820 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6821 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006822 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006823 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006824 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006825 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006826 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006827 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6828 return 200000;
6829 default:
6830 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6831 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006832 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006833 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006834 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006835 }
6836}
6837
Jesse Barnese70236a2009-09-21 10:42:27 -07006838static int i915gm_get_display_clock_speed(struct drm_device *dev)
6839{
6840 u16 gcfgc = 0;
6841
6842 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6843
6844 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006845 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006846 else {
6847 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6848 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006849 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006850 default:
6851 case GC_DISPLAY_CLOCK_190_200_MHZ:
6852 return 190000;
6853 }
6854 }
6855}
Jesse Barnes79e53942008-11-07 14:24:08 -08006856
Jesse Barnese70236a2009-09-21 10:42:27 -07006857static int i865_get_display_clock_speed(struct drm_device *dev)
6858{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006859 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006860}
6861
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006862static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006863{
6864 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006865
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006866 /*
6867 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6868 * encoding is different :(
6869 * FIXME is this the right way to detect 852GM/852GMV?
6870 */
6871 if (dev->pdev->revision == 0x1)
6872 return 133333;
6873
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006874 pci_bus_read_config_word(dev->pdev->bus,
6875 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6876
Jesse Barnese70236a2009-09-21 10:42:27 -07006877 /* Assume that the hardware is in the high speed state. This
6878 * should be the default.
6879 */
6880 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6881 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006882 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006883 case GC_CLOCK_100_200:
6884 return 200000;
6885 case GC_CLOCK_166_250:
6886 return 250000;
6887 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006888 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006889 case GC_CLOCK_133_266:
6890 case GC_CLOCK_133_266_2:
6891 case GC_CLOCK_166_266:
6892 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006893 }
6894
6895 /* Shouldn't happen */
6896 return 0;
6897}
6898
6899static int i830_get_display_clock_speed(struct drm_device *dev)
6900{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006901 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006902}
6903
Ville Syrjälä34edce22015-05-22 11:22:33 +03006904static unsigned int intel_hpll_vco(struct drm_device *dev)
6905{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006906 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03006907 static const unsigned int blb_vco[8] = {
6908 [0] = 3200000,
6909 [1] = 4000000,
6910 [2] = 5333333,
6911 [3] = 4800000,
6912 [4] = 6400000,
6913 };
6914 static const unsigned int pnv_vco[8] = {
6915 [0] = 3200000,
6916 [1] = 4000000,
6917 [2] = 5333333,
6918 [3] = 4800000,
6919 [4] = 2666667,
6920 };
6921 static const unsigned int cl_vco[8] = {
6922 [0] = 3200000,
6923 [1] = 4000000,
6924 [2] = 5333333,
6925 [3] = 6400000,
6926 [4] = 3333333,
6927 [5] = 3566667,
6928 [6] = 4266667,
6929 };
6930 static const unsigned int elk_vco[8] = {
6931 [0] = 3200000,
6932 [1] = 4000000,
6933 [2] = 5333333,
6934 [3] = 4800000,
6935 };
6936 static const unsigned int ctg_vco[8] = {
6937 [0] = 3200000,
6938 [1] = 4000000,
6939 [2] = 5333333,
6940 [3] = 6400000,
6941 [4] = 2666667,
6942 [5] = 4266667,
6943 };
6944 const unsigned int *vco_table;
6945 unsigned int vco;
6946 uint8_t tmp = 0;
6947
6948 /* FIXME other chipsets? */
6949 if (IS_GM45(dev))
6950 vco_table = ctg_vco;
6951 else if (IS_G4X(dev))
6952 vco_table = elk_vco;
6953 else if (IS_CRESTLINE(dev))
6954 vco_table = cl_vco;
6955 else if (IS_PINEVIEW(dev))
6956 vco_table = pnv_vco;
6957 else if (IS_G33(dev))
6958 vco_table = blb_vco;
6959 else
6960 return 0;
6961
6962 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6963
6964 vco = vco_table[tmp & 0x7];
6965 if (vco == 0)
6966 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6967 else
6968 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6969
6970 return vco;
6971}
6972
6973static int gm45_get_display_clock_speed(struct drm_device *dev)
6974{
6975 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6976 uint16_t tmp = 0;
6977
6978 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6979
6980 cdclk_sel = (tmp >> 12) & 0x1;
6981
6982 switch (vco) {
6983 case 2666667:
6984 case 4000000:
6985 case 5333333:
6986 return cdclk_sel ? 333333 : 222222;
6987 case 3200000:
6988 return cdclk_sel ? 320000 : 228571;
6989 default:
6990 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6991 return 222222;
6992 }
6993}
6994
6995static int i965gm_get_display_clock_speed(struct drm_device *dev)
6996{
6997 static const uint8_t div_3200[] = { 16, 10, 8 };
6998 static const uint8_t div_4000[] = { 20, 12, 10 };
6999 static const uint8_t div_5333[] = { 24, 16, 14 };
7000 const uint8_t *div_table;
7001 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7002 uint16_t tmp = 0;
7003
7004 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7005
7006 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7007
7008 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7009 goto fail;
7010
7011 switch (vco) {
7012 case 3200000:
7013 div_table = div_3200;
7014 break;
7015 case 4000000:
7016 div_table = div_4000;
7017 break;
7018 case 5333333:
7019 div_table = div_5333;
7020 break;
7021 default:
7022 goto fail;
7023 }
7024
7025 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7026
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007027fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007028 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7029 return 200000;
7030}
7031
7032static int g33_get_display_clock_speed(struct drm_device *dev)
7033{
7034 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7035 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7036 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7037 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7038 const uint8_t *div_table;
7039 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7040 uint16_t tmp = 0;
7041
7042 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7043
7044 cdclk_sel = (tmp >> 4) & 0x7;
7045
7046 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7047 goto fail;
7048
7049 switch (vco) {
7050 case 3200000:
7051 div_table = div_3200;
7052 break;
7053 case 4000000:
7054 div_table = div_4000;
7055 break;
7056 case 4800000:
7057 div_table = div_4800;
7058 break;
7059 case 5333333:
7060 div_table = div_5333;
7061 break;
7062 default:
7063 goto fail;
7064 }
7065
7066 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7067
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007068fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007069 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7070 return 190476;
7071}
7072
Zhenyu Wang2c072452009-06-05 15:38:42 +08007073static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007074intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007075{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007076 while (*num > DATA_LINK_M_N_MASK ||
7077 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007078 *num >>= 1;
7079 *den >>= 1;
7080 }
7081}
7082
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007083static void compute_m_n(unsigned int m, unsigned int n,
7084 uint32_t *ret_m, uint32_t *ret_n)
7085{
7086 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7087 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7088 intel_reduce_m_n_ratio(ret_m, ret_n);
7089}
7090
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007091void
7092intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7093 int pixel_clock, int link_clock,
7094 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007095{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007096 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007097
7098 compute_m_n(bits_per_pixel * pixel_clock,
7099 link_clock * nlanes * 8,
7100 &m_n->gmch_m, &m_n->gmch_n);
7101
7102 compute_m_n(pixel_clock, link_clock,
7103 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007104}
7105
Chris Wilsona7615032011-01-12 17:04:08 +00007106static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7107{
Jani Nikulad330a952014-01-21 11:24:25 +02007108 if (i915.panel_use_ssc >= 0)
7109 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007110 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007111 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007112}
7113
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007114static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007115{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007116 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007117}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007118
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007119static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7120{
7121 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007122}
7123
Daniel Vetterf47709a2013-03-28 10:42:02 +01007124static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007125 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007126 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007127{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007128 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007129 u32 fp, fp2 = 0;
7130
7131 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007132 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007133 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007134 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007135 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007136 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007137 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007138 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007139 }
7140
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007141 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007142
Daniel Vetterf47709a2013-03-28 10:42:02 +01007143 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007144 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007145 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007146 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007147 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007148 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007149 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007150 }
7151}
7152
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007153static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7154 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007155{
7156 u32 reg_val;
7157
7158 /*
7159 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7160 * and set it to a reasonable value instead.
7161 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007162 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007163 reg_val &= 0xffffff00;
7164 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007165 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007166
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007167 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007168 reg_val &= 0x8cffffff;
7169 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007170 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007171
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007172 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007173 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007174 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007175
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007176 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007177 reg_val &= 0x00ffffff;
7178 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007179 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007180}
7181
Daniel Vetterb5518422013-05-03 11:49:48 +02007182static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7183 struct intel_link_m_n *m_n)
7184{
7185 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007186 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007187 int pipe = crtc->pipe;
7188
Daniel Vettere3b95f12013-05-03 11:49:49 +02007189 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7190 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7191 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7192 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007193}
7194
7195static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007196 struct intel_link_m_n *m_n,
7197 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007198{
7199 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007200 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007201 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007202 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007203
7204 if (INTEL_INFO(dev)->gen >= 5) {
7205 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7206 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7207 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7208 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007209 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7210 * for gen < 8) and if DRRS is supported (to make sure the
7211 * registers are not unnecessarily accessed).
7212 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307213 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007214 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007215 I915_WRITE(PIPE_DATA_M2(transcoder),
7216 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7217 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7218 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7219 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7220 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007221 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007222 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7223 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7224 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7225 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007226 }
7227}
7228
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307229void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007230{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307231 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7232
7233 if (m_n == M1_N1) {
7234 dp_m_n = &crtc->config->dp_m_n;
7235 dp_m2_n2 = &crtc->config->dp_m2_n2;
7236 } else if (m_n == M2_N2) {
7237
7238 /*
7239 * M2_N2 registers are not supported. Hence m2_n2 divider value
7240 * needs to be programmed into M1_N1.
7241 */
7242 dp_m_n = &crtc->config->dp_m2_n2;
7243 } else {
7244 DRM_ERROR("Unsupported divider value\n");
7245 return;
7246 }
7247
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007248 if (crtc->config->has_pch_encoder)
7249 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007250 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307251 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007252}
7253
Daniel Vetter251ac862015-06-18 10:30:24 +02007254static void vlv_compute_dpll(struct intel_crtc *crtc,
7255 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007256{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007257 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007258 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007259 if (crtc->pipe != PIPE_A)
7260 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007261
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007262 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007263 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007264 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7265 DPLL_EXT_BUFFER_ENABLE_VLV;
7266
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007267 pipe_config->dpll_hw_state.dpll_md =
7268 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7269}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007270
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007271static void chv_compute_dpll(struct intel_crtc *crtc,
7272 struct intel_crtc_state *pipe_config)
7273{
7274 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007275 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007276 if (crtc->pipe != PIPE_A)
7277 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7278
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007279 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007280 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007281 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7282
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007283 pipe_config->dpll_hw_state.dpll_md =
7284 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007285}
7286
Ville Syrjäläd288f652014-10-28 13:20:22 +02007287static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007288 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007289{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007290 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007291 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007292 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007293 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007294 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007295 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007296
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007297 /* Enable Refclk */
7298 I915_WRITE(DPLL(pipe),
7299 pipe_config->dpll_hw_state.dpll &
7300 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7301
7302 /* No need to actually set up the DPLL with DSI */
7303 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7304 return;
7305
Ville Syrjäläa5805162015-05-26 20:42:30 +03007306 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007307
Ville Syrjäläd288f652014-10-28 13:20:22 +02007308 bestn = pipe_config->dpll.n;
7309 bestm1 = pipe_config->dpll.m1;
7310 bestm2 = pipe_config->dpll.m2;
7311 bestp1 = pipe_config->dpll.p1;
7312 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007313
Jesse Barnes89b667f2013-04-18 14:51:36 -07007314 /* See eDP HDMI DPIO driver vbios notes doc */
7315
7316 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007317 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007318 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007319
7320 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007321 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007322
7323 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007324 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007325 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007326 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007327
7328 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007329 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007330
7331 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007332 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7333 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7334 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007335 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007336
7337 /*
7338 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7339 * but we don't support that).
7340 * Note: don't use the DAC post divider as it seems unstable.
7341 */
7342 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007343 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007344
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007345 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007346 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007347
Jesse Barnes89b667f2013-04-18 14:51:36 -07007348 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007349 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007350 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7351 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007352 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007353 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007354 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007355 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007356 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007357
Ville Syrjälä37a56502016-06-22 21:57:04 +03007358 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007359 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007360 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007361 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007362 0x0df40000);
7363 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007364 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007365 0x0df70000);
7366 } else { /* HDMI or VGA */
7367 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007368 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007369 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007370 0x0df70000);
7371 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007372 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007373 0x0df40000);
7374 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007375
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007376 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007377 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007378 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007379 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007380 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007381
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007382 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007383 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007384}
7385
Ville Syrjäläd288f652014-10-28 13:20:22 +02007386static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007387 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007388{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007389 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007390 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007391 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007392 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307393 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007394 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307395 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307396 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007397
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007398 /* Enable Refclk and SSC */
7399 I915_WRITE(DPLL(pipe),
7400 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7401
7402 /* No need to actually set up the DPLL with DSI */
7403 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7404 return;
7405
Ville Syrjäläd288f652014-10-28 13:20:22 +02007406 bestn = pipe_config->dpll.n;
7407 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7408 bestm1 = pipe_config->dpll.m1;
7409 bestm2 = pipe_config->dpll.m2 >> 22;
7410 bestp1 = pipe_config->dpll.p1;
7411 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307412 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307413 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307414 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007415
Ville Syrjäläa5805162015-05-26 20:42:30 +03007416 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007417
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007418 /* p1 and p2 divider */
7419 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7420 5 << DPIO_CHV_S1_DIV_SHIFT |
7421 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7422 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7423 1 << DPIO_CHV_K_DIV_SHIFT);
7424
7425 /* Feedback post-divider - m2 */
7426 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7427
7428 /* Feedback refclk divider - n and m1 */
7429 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7430 DPIO_CHV_M1_DIV_BY_2 |
7431 1 << DPIO_CHV_N_DIV_SHIFT);
7432
7433 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007434 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007435
7436 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307437 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7438 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7439 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7440 if (bestm2_frac)
7441 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7442 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007443
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307444 /* Program digital lock detect threshold */
7445 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7446 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7447 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7448 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7449 if (!bestm2_frac)
7450 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7451 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7452
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007453 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307454 if (vco == 5400000) {
7455 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7456 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7457 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7458 tribuf_calcntr = 0x9;
7459 } else if (vco <= 6200000) {
7460 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7461 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7462 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7463 tribuf_calcntr = 0x9;
7464 } else if (vco <= 6480000) {
7465 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7466 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7467 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7468 tribuf_calcntr = 0x8;
7469 } else {
7470 /* Not supported. Apply the same limits as in the max case */
7471 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7472 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7473 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7474 tribuf_calcntr = 0;
7475 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007476 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7477
Ville Syrjälä968040b2015-03-11 22:52:08 +02007478 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307479 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7480 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7481 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7482
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007483 /* AFC Recal */
7484 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7485 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7486 DPIO_AFC_RECAL);
7487
Ville Syrjäläa5805162015-05-26 20:42:30 +03007488 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007489}
7490
Ville Syrjäläd288f652014-10-28 13:20:22 +02007491/**
7492 * vlv_force_pll_on - forcibly enable just the PLL
7493 * @dev_priv: i915 private structure
7494 * @pipe: pipe PLL to enable
7495 * @dpll: PLL configuration
7496 *
7497 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7498 * in cases where we need the PLL enabled even when @pipe is not going to
7499 * be enabled.
7500 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007501int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7502 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007503{
7504 struct intel_crtc *crtc =
7505 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007506 struct intel_crtc_state *pipe_config;
7507
7508 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7509 if (!pipe_config)
7510 return -ENOMEM;
7511
7512 pipe_config->base.crtc = &crtc->base;
7513 pipe_config->pixel_multiplier = 1;
7514 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007515
7516 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007517 chv_compute_dpll(crtc, pipe_config);
7518 chv_prepare_pll(crtc, pipe_config);
7519 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007520 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007521 vlv_compute_dpll(crtc, pipe_config);
7522 vlv_prepare_pll(crtc, pipe_config);
7523 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007524 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007525
7526 kfree(pipe_config);
7527
7528 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007529}
7530
7531/**
7532 * vlv_force_pll_off - forcibly disable just the PLL
7533 * @dev_priv: i915 private structure
7534 * @pipe: pipe PLL to disable
7535 *
7536 * Disable the PLL for @pipe. To be used in cases where we need
7537 * the PLL enabled even when @pipe is not going to be enabled.
7538 */
7539void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7540{
7541 if (IS_CHERRYVIEW(dev))
7542 chv_disable_pll(to_i915(dev), pipe);
7543 else
7544 vlv_disable_pll(to_i915(dev), pipe);
7545}
7546
Daniel Vetter251ac862015-06-18 10:30:24 +02007547static void i9xx_compute_dpll(struct intel_crtc *crtc,
7548 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007549 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007550{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007551 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007552 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007553 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007554 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007555
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007556 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307557
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007558 dpll = DPLL_VGA_MODE_DIS;
7559
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007560 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007561 dpll |= DPLLB_MODE_LVDS;
7562 else
7563 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007564
Daniel Vetteref1b4602013-06-01 17:17:04 +02007565 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007566 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007567 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007568 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007569
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007570 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7571 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007572 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007573
Ville Syrjälä37a56502016-06-22 21:57:04 +03007574 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007575 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007576
7577 /* compute bitmask from p1 value */
7578 if (IS_PINEVIEW(dev))
7579 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7580 else {
7581 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7582 if (IS_G4X(dev) && reduced_clock)
7583 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7584 }
7585 switch (clock->p2) {
7586 case 5:
7587 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7588 break;
7589 case 7:
7590 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7591 break;
7592 case 10:
7593 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7594 break;
7595 case 14:
7596 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7597 break;
7598 }
7599 if (INTEL_INFO(dev)->gen >= 4)
7600 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7601
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007602 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007603 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007604 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007605 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007606 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7607 else
7608 dpll |= PLL_REF_INPUT_DREFCLK;
7609
7610 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007611 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007612
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007613 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007614 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007615 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007616 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007617 }
7618}
7619
Daniel Vetter251ac862015-06-18 10:30:24 +02007620static void i8xx_compute_dpll(struct intel_crtc *crtc,
7621 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007622 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007623{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007624 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007625 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007626 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007627 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007628
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007629 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307630
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007631 dpll = DPLL_VGA_MODE_DIS;
7632
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007633 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007634 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7635 } else {
7636 if (clock->p1 == 2)
7637 dpll |= PLL_P1_DIVIDE_BY_TWO;
7638 else
7639 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7640 if (clock->p2 == 4)
7641 dpll |= PLL_P2_DIVIDE_BY_4;
7642 }
7643
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007644 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007645 dpll |= DPLL_DVO_2X_MODE;
7646
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007647 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007648 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007649 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7650 else
7651 dpll |= PLL_REF_INPUT_DREFCLK;
7652
7653 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007654 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007655}
7656
Daniel Vetter8a654f32013-06-01 17:16:22 +02007657static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007658{
7659 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007660 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007661 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007662 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007663 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007664 uint32_t crtc_vtotal, crtc_vblank_end;
7665 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007666
7667 /* We need to be careful not to changed the adjusted mode, for otherwise
7668 * the hw state checker will get angry at the mismatch. */
7669 crtc_vtotal = adjusted_mode->crtc_vtotal;
7670 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007671
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007672 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007673 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007674 crtc_vtotal -= 1;
7675 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007676
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007677 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007678 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7679 else
7680 vsyncshift = adjusted_mode->crtc_hsync_start -
7681 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007682 if (vsyncshift < 0)
7683 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007684 }
7685
7686 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007687 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007688
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007689 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007690 (adjusted_mode->crtc_hdisplay - 1) |
7691 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007692 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007693 (adjusted_mode->crtc_hblank_start - 1) |
7694 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007695 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007696 (adjusted_mode->crtc_hsync_start - 1) |
7697 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7698
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007699 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007700 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007701 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007702 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007703 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007704 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007705 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007706 (adjusted_mode->crtc_vsync_start - 1) |
7707 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7708
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007709 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7710 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7711 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7712 * bits. */
7713 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7714 (pipe == PIPE_B || pipe == PIPE_C))
7715 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7716
Jani Nikulabc58be62016-03-18 17:05:39 +02007717}
7718
7719static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7720{
7721 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007722 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007723 enum pipe pipe = intel_crtc->pipe;
7724
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007725 /* pipesrc controls the size that is scaled from, which should
7726 * always be the user's requested size.
7727 */
7728 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007729 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7730 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007731}
7732
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007733static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007734 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007735{
7736 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007737 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007738 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7739 uint32_t tmp;
7740
7741 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007742 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7743 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007744 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007745 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7746 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007747 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007748 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7749 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007750
7751 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007752 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7753 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007754 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007755 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7756 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007757 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007758 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7759 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007760
7761 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007762 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7763 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7764 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007765 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007766}
7767
7768static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7769 struct intel_crtc_state *pipe_config)
7770{
7771 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007772 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007773 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007774
7775 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007776 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7777 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7778
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007779 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7780 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007781}
7782
Daniel Vetterf6a83282014-02-11 15:28:57 -08007783void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007784 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007785{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007786 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7787 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7788 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7789 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007790
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007791 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7792 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7793 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7794 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007795
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007796 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007797 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007798
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007799 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7800 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007801
7802 mode->hsync = drm_mode_hsync(mode);
7803 mode->vrefresh = drm_mode_vrefresh(mode);
7804 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007805}
7806
Daniel Vetter84b046f2013-02-19 18:48:54 +01007807static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7808{
7809 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007810 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007811 uint32_t pipeconf;
7812
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007813 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007814
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007815 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7816 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7817 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007818
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007819 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007820 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007821
Daniel Vetterff9ce462013-04-24 14:57:17 +02007822 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007823 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007824 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007825 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007826 pipeconf |= PIPECONF_DITHER_EN |
7827 PIPECONF_DITHER_TYPE_SP;
7828
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007829 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007830 case 18:
7831 pipeconf |= PIPECONF_6BPC;
7832 break;
7833 case 24:
7834 pipeconf |= PIPECONF_8BPC;
7835 break;
7836 case 30:
7837 pipeconf |= PIPECONF_10BPC;
7838 break;
7839 default:
7840 /* Case prevented by intel_choose_pipe_bpp_dither. */
7841 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007842 }
7843 }
7844
7845 if (HAS_PIPE_CXSR(dev)) {
7846 if (intel_crtc->lowfreq_avail) {
7847 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7848 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7849 } else {
7850 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007851 }
7852 }
7853
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007854 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007855 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007856 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007857 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7858 else
7859 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7860 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007861 pipeconf |= PIPECONF_PROGRESSIVE;
7862
Wayne Boyer666a4532015-12-09 12:29:35 -08007863 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7864 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007865 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007866
Daniel Vetter84b046f2013-02-19 18:48:54 +01007867 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7868 POSTING_READ(PIPECONF(intel_crtc->pipe));
7869}
7870
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007871static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7872 struct intel_crtc_state *crtc_state)
7873{
7874 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007875 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007876 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007877 int refclk = 48000;
7878
7879 memset(&crtc_state->dpll_hw_state, 0,
7880 sizeof(crtc_state->dpll_hw_state));
7881
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007882 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007883 if (intel_panel_use_ssc(dev_priv)) {
7884 refclk = dev_priv->vbt.lvds_ssc_freq;
7885 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7886 }
7887
7888 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007889 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007890 limit = &intel_limits_i8xx_dvo;
7891 } else {
7892 limit = &intel_limits_i8xx_dac;
7893 }
7894
7895 if (!crtc_state->clock_set &&
7896 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7897 refclk, NULL, &crtc_state->dpll)) {
7898 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7899 return -EINVAL;
7900 }
7901
7902 i8xx_compute_dpll(crtc, crtc_state, NULL);
7903
7904 return 0;
7905}
7906
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007907static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7908 struct intel_crtc_state *crtc_state)
7909{
7910 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007911 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007912 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007913 int refclk = 96000;
7914
7915 memset(&crtc_state->dpll_hw_state, 0,
7916 sizeof(crtc_state->dpll_hw_state));
7917
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007918 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007919 if (intel_panel_use_ssc(dev_priv)) {
7920 refclk = dev_priv->vbt.lvds_ssc_freq;
7921 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7922 }
7923
7924 if (intel_is_dual_link_lvds(dev))
7925 limit = &intel_limits_g4x_dual_channel_lvds;
7926 else
7927 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007928 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7929 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007930 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007931 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007932 limit = &intel_limits_g4x_sdvo;
7933 } else {
7934 /* The option is for other outputs */
7935 limit = &intel_limits_i9xx_sdvo;
7936 }
7937
7938 if (!crtc_state->clock_set &&
7939 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7940 refclk, NULL, &crtc_state->dpll)) {
7941 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7942 return -EINVAL;
7943 }
7944
7945 i9xx_compute_dpll(crtc, crtc_state, NULL);
7946
7947 return 0;
7948}
7949
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007950static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7951 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007952{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007953 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007954 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007955 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007956 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007957
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007958 memset(&crtc_state->dpll_hw_state, 0,
7959 sizeof(crtc_state->dpll_hw_state));
7960
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007961 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007962 if (intel_panel_use_ssc(dev_priv)) {
7963 refclk = dev_priv->vbt.lvds_ssc_freq;
7964 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7965 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007966
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007967 limit = &intel_limits_pineview_lvds;
7968 } else {
7969 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007970 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007971
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007972 if (!crtc_state->clock_set &&
7973 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7974 refclk, NULL, &crtc_state->dpll)) {
7975 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7976 return -EINVAL;
7977 }
7978
7979 i9xx_compute_dpll(crtc, crtc_state, NULL);
7980
7981 return 0;
7982}
7983
7984static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7985 struct intel_crtc_state *crtc_state)
7986{
7987 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007988 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007989 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007990 int refclk = 96000;
7991
7992 memset(&crtc_state->dpll_hw_state, 0,
7993 sizeof(crtc_state->dpll_hw_state));
7994
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007995 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007996 if (intel_panel_use_ssc(dev_priv)) {
7997 refclk = dev_priv->vbt.lvds_ssc_freq;
7998 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007999 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008000
8001 limit = &intel_limits_i9xx_lvds;
8002 } else {
8003 limit = &intel_limits_i9xx_sdvo;
8004 }
8005
8006 if (!crtc_state->clock_set &&
8007 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8008 refclk, NULL, &crtc_state->dpll)) {
8009 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8010 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008011 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008012
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008013 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008014
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008015 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008016}
8017
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008018static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8019 struct intel_crtc_state *crtc_state)
8020{
8021 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008022 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008023
8024 memset(&crtc_state->dpll_hw_state, 0,
8025 sizeof(crtc_state->dpll_hw_state));
8026
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008027 if (!crtc_state->clock_set &&
8028 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8029 refclk, NULL, &crtc_state->dpll)) {
8030 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8031 return -EINVAL;
8032 }
8033
8034 chv_compute_dpll(crtc, crtc_state);
8035
8036 return 0;
8037}
8038
8039static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8040 struct intel_crtc_state *crtc_state)
8041{
8042 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008043 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008044
8045 memset(&crtc_state->dpll_hw_state, 0,
8046 sizeof(crtc_state->dpll_hw_state));
8047
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008048 if (!crtc_state->clock_set &&
8049 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8050 refclk, NULL, &crtc_state->dpll)) {
8051 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8052 return -EINVAL;
8053 }
8054
8055 vlv_compute_dpll(crtc, crtc_state);
8056
8057 return 0;
8058}
8059
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008060static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008061 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008062{
8063 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008064 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008065 uint32_t tmp;
8066
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008067 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8068 return;
8069
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008070 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008071 if (!(tmp & PFIT_ENABLE))
8072 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008073
Daniel Vetter06922822013-07-11 13:35:40 +02008074 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008075 if (INTEL_INFO(dev)->gen < 4) {
8076 if (crtc->pipe != PIPE_B)
8077 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008078 } else {
8079 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8080 return;
8081 }
8082
Daniel Vetter06922822013-07-11 13:35:40 +02008083 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008084 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008085}
8086
Jesse Barnesacbec812013-09-20 11:29:32 -07008087static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008088 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008089{
8090 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008091 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008092 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008093 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008094 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008095 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008096
Ville Syrjäläb5219732016-03-15 16:40:01 +02008097 /* In case of DSI, DPLL will not be used */
8098 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308099 return;
8100
Ville Syrjäläa5805162015-05-26 20:42:30 +03008101 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008102 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008103 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008104
8105 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8106 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8107 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8108 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8109 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8110
Imre Deakdccbea32015-06-22 23:35:51 +03008111 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008112}
8113
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008114static void
8115i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8116 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008117{
8118 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008119 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008120 u32 val, base, offset;
8121 int pipe = crtc->pipe, plane = crtc->plane;
8122 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008123 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008124 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008125 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008126
Damien Lespiau42a7b082015-02-05 19:35:13 +00008127 val = I915_READ(DSPCNTR(plane));
8128 if (!(val & DISPLAY_PLANE_ENABLE))
8129 return;
8130
Damien Lespiaud9806c92015-01-21 14:07:19 +00008131 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008132 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008133 DRM_DEBUG_KMS("failed to alloc fb\n");
8134 return;
8135 }
8136
Damien Lespiau1b842c82015-01-21 13:50:54 +00008137 fb = &intel_fb->base;
8138
Daniel Vetter18c52472015-02-10 17:16:09 +00008139 if (INTEL_INFO(dev)->gen >= 4) {
8140 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008141 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008142 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8143 }
8144 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008145
8146 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008147 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008148 fb->pixel_format = fourcc;
8149 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008150
8151 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008152 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008153 offset = I915_READ(DSPTILEOFF(plane));
8154 else
8155 offset = I915_READ(DSPLINOFF(plane));
8156 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8157 } else {
8158 base = I915_READ(DSPADDR(plane));
8159 }
8160 plane_config->base = base;
8161
8162 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008163 fb->width = ((val >> 16) & 0xfff) + 1;
8164 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008165
8166 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008167 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008168
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008169 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008170 fb->pixel_format,
8171 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008172
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008173 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008174
Damien Lespiau2844a922015-01-20 12:51:48 +00008175 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8176 pipe_name(pipe), plane, fb->width, fb->height,
8177 fb->bits_per_pixel, base, fb->pitches[0],
8178 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008179
Damien Lespiau2d140302015-02-05 17:22:18 +00008180 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008181}
8182
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008183static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008184 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008185{
8186 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008187 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008188 int pipe = pipe_config->cpu_transcoder;
8189 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008190 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008191 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008192 int refclk = 100000;
8193
Ville Syrjäläb5219732016-03-15 16:40:01 +02008194 /* In case of DSI, DPLL will not be used */
8195 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8196 return;
8197
Ville Syrjäläa5805162015-05-26 20:42:30 +03008198 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008199 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8200 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8201 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8202 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008203 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008204 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008205
8206 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008207 clock.m2 = (pll_dw0 & 0xff) << 22;
8208 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8209 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008210 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8211 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8212 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8213
Imre Deakdccbea32015-06-22 23:35:51 +03008214 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008215}
8216
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008217static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008218 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008219{
8220 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008221 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008222 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008223 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008224 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008225
Imre Deak17290502016-02-12 18:55:11 +02008226 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8227 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008228 return false;
8229
Daniel Vettere143a212013-07-04 12:01:15 +02008230 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008231 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008232
Imre Deak17290502016-02-12 18:55:11 +02008233 ret = false;
8234
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008235 tmp = I915_READ(PIPECONF(crtc->pipe));
8236 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008237 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008238
Wayne Boyer666a4532015-12-09 12:29:35 -08008239 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008240 switch (tmp & PIPECONF_BPC_MASK) {
8241 case PIPECONF_6BPC:
8242 pipe_config->pipe_bpp = 18;
8243 break;
8244 case PIPECONF_8BPC:
8245 pipe_config->pipe_bpp = 24;
8246 break;
8247 case PIPECONF_10BPC:
8248 pipe_config->pipe_bpp = 30;
8249 break;
8250 default:
8251 break;
8252 }
8253 }
8254
Wayne Boyer666a4532015-12-09 12:29:35 -08008255 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8256 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008257 pipe_config->limited_color_range = true;
8258
Ville Syrjälä282740f2013-09-04 18:30:03 +03008259 if (INTEL_INFO(dev)->gen < 4)
8260 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8261
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008262 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008263 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008264
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008265 i9xx_get_pfit_config(crtc, pipe_config);
8266
Daniel Vetter6c49f242013-06-06 12:45:25 +02008267 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008268 /* No way to read it out on pipes B and C */
8269 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8270 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8271 else
8272 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008273 pipe_config->pixel_multiplier =
8274 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8275 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008276 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008277 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8278 tmp = I915_READ(DPLL(crtc->pipe));
8279 pipe_config->pixel_multiplier =
8280 ((tmp & SDVO_MULTIPLIER_MASK)
8281 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8282 } else {
8283 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8284 * port and will be fixed up in the encoder->get_config
8285 * function. */
8286 pipe_config->pixel_multiplier = 1;
8287 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008288 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008289 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008290 /*
8291 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8292 * on 830. Filter it out here so that we don't
8293 * report errors due to that.
8294 */
8295 if (IS_I830(dev))
8296 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8297
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008298 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8299 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008300 } else {
8301 /* Mask out read-only status bits. */
8302 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8303 DPLL_PORTC_READY_MASK |
8304 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008305 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008306
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008307 if (IS_CHERRYVIEW(dev))
8308 chv_crtc_clock_get(crtc, pipe_config);
8309 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008310 vlv_crtc_clock_get(crtc, pipe_config);
8311 else
8312 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008313
Ville Syrjälä0f646142015-08-26 19:39:18 +03008314 /*
8315 * Normally the dotclock is filled in by the encoder .get_config()
8316 * but in case the pipe is enabled w/o any ports we need a sane
8317 * default.
8318 */
8319 pipe_config->base.adjusted_mode.crtc_clock =
8320 pipe_config->port_clock / pipe_config->pixel_multiplier;
8321
Imre Deak17290502016-02-12 18:55:11 +02008322 ret = true;
8323
8324out:
8325 intel_display_power_put(dev_priv, power_domain);
8326
8327 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008328}
8329
Paulo Zanonidde86e22012-12-01 12:04:25 -02008330static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008331{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008332 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008333 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008334 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008335 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008336 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008337 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008338 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008339 bool has_ck505 = false;
8340 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008341 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008342
8343 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008344 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008345 switch (encoder->type) {
8346 case INTEL_OUTPUT_LVDS:
8347 has_panel = true;
8348 has_lvds = true;
8349 break;
8350 case INTEL_OUTPUT_EDP:
8351 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008352 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008353 has_cpu_edp = true;
8354 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008355 default:
8356 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008357 }
8358 }
8359
Keith Packard99eb6a02011-09-26 14:29:12 -07008360 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008361 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008362 can_ssc = has_ck505;
8363 } else {
8364 has_ck505 = false;
8365 can_ssc = true;
8366 }
8367
Lyude1c1a24d2016-06-14 11:04:09 -04008368 /* Check if any DPLLs are using the SSC source */
8369 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8370 u32 temp = I915_READ(PCH_DPLL(i));
8371
8372 if (!(temp & DPLL_VCO_ENABLE))
8373 continue;
8374
8375 if ((temp & PLL_REF_INPUT_MASK) ==
8376 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8377 using_ssc_source = true;
8378 break;
8379 }
8380 }
8381
8382 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8383 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008384
8385 /* Ironlake: try to setup display ref clock before DPLL
8386 * enabling. This is only under driver's control after
8387 * PCH B stepping, previous chipset stepping should be
8388 * ignoring this setting.
8389 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008390 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008391
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008392 /* As we must carefully and slowly disable/enable each source in turn,
8393 * compute the final state we want first and check if we need to
8394 * make any changes at all.
8395 */
8396 final = val;
8397 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008398 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008399 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008400 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008401 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8402
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008403 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008404 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008405 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008406
Keith Packard199e5d72011-09-22 12:01:57 -07008407 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008408 final |= DREF_SSC_SOURCE_ENABLE;
8409
8410 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8411 final |= DREF_SSC1_ENABLE;
8412
8413 if (has_cpu_edp) {
8414 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8415 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8416 else
8417 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8418 } else
8419 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008420 } else if (using_ssc_source) {
8421 final |= DREF_SSC_SOURCE_ENABLE;
8422 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008423 }
8424
8425 if (final == val)
8426 return;
8427
8428 /* Always enable nonspread source */
8429 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8430
8431 if (has_ck505)
8432 val |= DREF_NONSPREAD_CK505_ENABLE;
8433 else
8434 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8435
8436 if (has_panel) {
8437 val &= ~DREF_SSC_SOURCE_MASK;
8438 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008439
Keith Packard199e5d72011-09-22 12:01:57 -07008440 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008441 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008442 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008443 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008444 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008445 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008446
8447 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008448 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008449 POSTING_READ(PCH_DREF_CONTROL);
8450 udelay(200);
8451
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008452 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008453
8454 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008455 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008456 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008457 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008458 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008459 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008460 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008461 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008462 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008463
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008464 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008465 POSTING_READ(PCH_DREF_CONTROL);
8466 udelay(200);
8467 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008468 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008469
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008470 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008471
8472 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008473 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008474
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008475 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008476 POSTING_READ(PCH_DREF_CONTROL);
8477 udelay(200);
8478
Lyude1c1a24d2016-06-14 11:04:09 -04008479 if (!using_ssc_source) {
8480 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008481
Lyude1c1a24d2016-06-14 11:04:09 -04008482 /* Turn off the SSC source */
8483 val &= ~DREF_SSC_SOURCE_MASK;
8484 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008485
Lyude1c1a24d2016-06-14 11:04:09 -04008486 /* Turn off SSC1 */
8487 val &= ~DREF_SSC1_ENABLE;
8488
8489 I915_WRITE(PCH_DREF_CONTROL, val);
8490 POSTING_READ(PCH_DREF_CONTROL);
8491 udelay(200);
8492 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008493 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008494
8495 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008496}
8497
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008498static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008499{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008500 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008501
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008502 tmp = I915_READ(SOUTH_CHICKEN2);
8503 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8504 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008505
Imre Deakcf3598c2016-06-28 13:37:31 +03008506 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8507 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008508 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008509
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008510 tmp = I915_READ(SOUTH_CHICKEN2);
8511 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8512 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008513
Imre Deakcf3598c2016-06-28 13:37:31 +03008514 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8515 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008516 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008517}
8518
8519/* WaMPhyProgramming:hsw */
8520static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8521{
8522 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008523
8524 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8525 tmp &= ~(0xFF << 24);
8526 tmp |= (0x12 << 24);
8527 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8528
Paulo Zanonidde86e22012-12-01 12:04:25 -02008529 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8530 tmp |= (1 << 11);
8531 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8532
8533 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8534 tmp |= (1 << 11);
8535 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8536
Paulo Zanonidde86e22012-12-01 12:04:25 -02008537 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8538 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8539 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8540
8541 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8542 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8543 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8544
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008545 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8546 tmp &= ~(7 << 13);
8547 tmp |= (5 << 13);
8548 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008549
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008550 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8551 tmp &= ~(7 << 13);
8552 tmp |= (5 << 13);
8553 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008554
8555 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8556 tmp &= ~0xFF;
8557 tmp |= 0x1C;
8558 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8559
8560 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8561 tmp &= ~0xFF;
8562 tmp |= 0x1C;
8563 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8564
8565 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8566 tmp &= ~(0xFF << 16);
8567 tmp |= (0x1C << 16);
8568 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8569
8570 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8571 tmp &= ~(0xFF << 16);
8572 tmp |= (0x1C << 16);
8573 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8574
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008575 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8576 tmp |= (1 << 27);
8577 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008578
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008579 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8580 tmp |= (1 << 27);
8581 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008582
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008583 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8584 tmp &= ~(0xF << 28);
8585 tmp |= (4 << 28);
8586 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008587
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008588 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8589 tmp &= ~(0xF << 28);
8590 tmp |= (4 << 28);
8591 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008592}
8593
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008594/* Implements 3 different sequences from BSpec chapter "Display iCLK
8595 * Programming" based on the parameters passed:
8596 * - Sequence to enable CLKOUT_DP
8597 * - Sequence to enable CLKOUT_DP without spread
8598 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8599 */
8600static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8601 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008602{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008603 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008604 uint32_t reg, tmp;
8605
8606 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8607 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008608 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008609 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008610
Ville Syrjäläa5805162015-05-26 20:42:30 +03008611 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008612
8613 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8614 tmp &= ~SBI_SSCCTL_DISABLE;
8615 tmp |= SBI_SSCCTL_PATHALT;
8616 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8617
8618 udelay(24);
8619
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008620 if (with_spread) {
8621 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8622 tmp &= ~SBI_SSCCTL_PATHALT;
8623 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008624
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008625 if (with_fdi) {
8626 lpt_reset_fdi_mphy(dev_priv);
8627 lpt_program_fdi_mphy(dev_priv);
8628 }
8629 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008630
Ville Syrjäläc2699522015-08-27 23:55:59 +03008631 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008632 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8633 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8634 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008635
Ville Syrjäläa5805162015-05-26 20:42:30 +03008636 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008637}
8638
Paulo Zanoni47701c32013-07-23 11:19:25 -03008639/* Sequence to disable CLKOUT_DP */
8640static void lpt_disable_clkout_dp(struct drm_device *dev)
8641{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008642 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008643 uint32_t reg, tmp;
8644
Ville Syrjäläa5805162015-05-26 20:42:30 +03008645 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008646
Ville Syrjäläc2699522015-08-27 23:55:59 +03008647 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008648 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8649 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8650 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8651
8652 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8653 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8654 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8655 tmp |= SBI_SSCCTL_PATHALT;
8656 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8657 udelay(32);
8658 }
8659 tmp |= SBI_SSCCTL_DISABLE;
8660 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8661 }
8662
Ville Syrjäläa5805162015-05-26 20:42:30 +03008663 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008664}
8665
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008666#define BEND_IDX(steps) ((50 + (steps)) / 5)
8667
8668static const uint16_t sscdivintphase[] = {
8669 [BEND_IDX( 50)] = 0x3B23,
8670 [BEND_IDX( 45)] = 0x3B23,
8671 [BEND_IDX( 40)] = 0x3C23,
8672 [BEND_IDX( 35)] = 0x3C23,
8673 [BEND_IDX( 30)] = 0x3D23,
8674 [BEND_IDX( 25)] = 0x3D23,
8675 [BEND_IDX( 20)] = 0x3E23,
8676 [BEND_IDX( 15)] = 0x3E23,
8677 [BEND_IDX( 10)] = 0x3F23,
8678 [BEND_IDX( 5)] = 0x3F23,
8679 [BEND_IDX( 0)] = 0x0025,
8680 [BEND_IDX( -5)] = 0x0025,
8681 [BEND_IDX(-10)] = 0x0125,
8682 [BEND_IDX(-15)] = 0x0125,
8683 [BEND_IDX(-20)] = 0x0225,
8684 [BEND_IDX(-25)] = 0x0225,
8685 [BEND_IDX(-30)] = 0x0325,
8686 [BEND_IDX(-35)] = 0x0325,
8687 [BEND_IDX(-40)] = 0x0425,
8688 [BEND_IDX(-45)] = 0x0425,
8689 [BEND_IDX(-50)] = 0x0525,
8690};
8691
8692/*
8693 * Bend CLKOUT_DP
8694 * steps -50 to 50 inclusive, in steps of 5
8695 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8696 * change in clock period = -(steps / 10) * 5.787 ps
8697 */
8698static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8699{
8700 uint32_t tmp;
8701 int idx = BEND_IDX(steps);
8702
8703 if (WARN_ON(steps % 5 != 0))
8704 return;
8705
8706 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8707 return;
8708
8709 mutex_lock(&dev_priv->sb_lock);
8710
8711 if (steps % 10 != 0)
8712 tmp = 0xAAAAAAAB;
8713 else
8714 tmp = 0x00000000;
8715 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8716
8717 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8718 tmp &= 0xffff0000;
8719 tmp |= sscdivintphase[idx];
8720 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8721
8722 mutex_unlock(&dev_priv->sb_lock);
8723}
8724
8725#undef BEND_IDX
8726
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008727static void lpt_init_pch_refclk(struct drm_device *dev)
8728{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008729 struct intel_encoder *encoder;
8730 bool has_vga = false;
8731
Damien Lespiaub2784e12014-08-05 11:29:37 +01008732 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008733 switch (encoder->type) {
8734 case INTEL_OUTPUT_ANALOG:
8735 has_vga = true;
8736 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008737 default:
8738 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008739 }
8740 }
8741
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008742 if (has_vga) {
8743 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008744 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008745 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008746 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008747 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008748}
8749
Paulo Zanonidde86e22012-12-01 12:04:25 -02008750/*
8751 * Initialize reference clocks when the driver loads
8752 */
8753void intel_init_pch_refclk(struct drm_device *dev)
8754{
8755 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8756 ironlake_init_pch_refclk(dev);
8757 else if (HAS_PCH_LPT(dev))
8758 lpt_init_pch_refclk(dev);
8759}
8760
Daniel Vetter6ff93602013-04-19 11:24:36 +02008761static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008762{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008763 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8765 int pipe = intel_crtc->pipe;
8766 uint32_t val;
8767
Daniel Vetter78114072013-06-13 00:54:57 +02008768 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008769
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008770 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008771 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008772 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008773 break;
8774 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008775 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008776 break;
8777 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008778 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008779 break;
8780 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008781 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008782 break;
8783 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008784 /* Case prevented by intel_choose_pipe_bpp_dither. */
8785 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008786 }
8787
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008788 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008789 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8790
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008791 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008792 val |= PIPECONF_INTERLACED_ILK;
8793 else
8794 val |= PIPECONF_PROGRESSIVE;
8795
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008796 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008797 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008798
Paulo Zanonic8203562012-09-12 10:06:29 -03008799 I915_WRITE(PIPECONF(pipe), val);
8800 POSTING_READ(PIPECONF(pipe));
8801}
8802
Daniel Vetter6ff93602013-04-19 11:24:36 +02008803static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008804{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008805 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008807 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008808 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008809
Jani Nikula391bf042016-03-18 17:05:40 +02008810 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008811 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8812
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008813 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008814 val |= PIPECONF_INTERLACED_ILK;
8815 else
8816 val |= PIPECONF_PROGRESSIVE;
8817
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008818 I915_WRITE(PIPECONF(cpu_transcoder), val);
8819 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008820}
8821
Jani Nikula391bf042016-03-18 17:05:40 +02008822static void haswell_set_pipemisc(struct drm_crtc *crtc)
8823{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008824 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8826
8827 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8828 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008829
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008830 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008831 case 18:
8832 val |= PIPEMISC_DITHER_6_BPC;
8833 break;
8834 case 24:
8835 val |= PIPEMISC_DITHER_8_BPC;
8836 break;
8837 case 30:
8838 val |= PIPEMISC_DITHER_10_BPC;
8839 break;
8840 case 36:
8841 val |= PIPEMISC_DITHER_12_BPC;
8842 break;
8843 default:
8844 /* Case prevented by pipe_config_set_bpp. */
8845 BUG();
8846 }
8847
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008848 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008849 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8850
Jani Nikula391bf042016-03-18 17:05:40 +02008851 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008852 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008853}
8854
Paulo Zanonid4b19312012-11-29 11:29:32 -02008855int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8856{
8857 /*
8858 * Account for spread spectrum to avoid
8859 * oversubscribing the link. Max center spread
8860 * is 2.5%; use 5% for safety's sake.
8861 */
8862 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008863 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008864}
8865
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008866static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008867{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008868 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008869}
8870
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008871static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8872 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008873 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008874{
8875 struct drm_crtc *crtc = &intel_crtc->base;
8876 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008877 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008878 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008879 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008880
Chris Wilsonc1858122010-12-03 21:35:48 +00008881 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008882 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008883 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008884 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008885 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008886 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008887 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008888 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008889 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008890
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008891 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008892
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008893 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8894 fp |= FP_CB_TUNE;
8895
8896 if (reduced_clock) {
8897 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8898
8899 if (reduced_clock->m < factor * reduced_clock->n)
8900 fp2 |= FP_CB_TUNE;
8901 } else {
8902 fp2 = fp;
8903 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008904
Chris Wilson5eddb702010-09-11 13:48:45 +01008905 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008906
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008907 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008908 dpll |= DPLLB_MODE_LVDS;
8909 else
8910 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008911
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008912 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008913 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008914
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008915 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8916 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008917 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008918
Ville Syrjälä37a56502016-06-22 21:57:04 +03008919 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008920 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008921
Eric Anholta07d6782011-03-30 13:01:08 -07008922 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008923 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008924 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008925 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008926
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008927 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008928 case 5:
8929 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8930 break;
8931 case 7:
8932 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8933 break;
8934 case 10:
8935 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8936 break;
8937 case 14:
8938 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8939 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008940 }
8941
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008942 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8943 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008944 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008945 else
8946 dpll |= PLL_REF_INPUT_DREFCLK;
8947
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008948 dpll |= DPLL_VCO_ENABLE;
8949
8950 crtc_state->dpll_hw_state.dpll = dpll;
8951 crtc_state->dpll_hw_state.fp0 = fp;
8952 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008953}
8954
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008955static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8956 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008957{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008958 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008959 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008960 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008961 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008962 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008963 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008964 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008965
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008966 memset(&crtc_state->dpll_hw_state, 0,
8967 sizeof(crtc_state->dpll_hw_state));
8968
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008969 crtc->lowfreq_avail = false;
8970
8971 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8972 if (!crtc_state->has_pch_encoder)
8973 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008974
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008975 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008976 if (intel_panel_use_ssc(dev_priv)) {
8977 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8978 dev_priv->vbt.lvds_ssc_freq);
8979 refclk = dev_priv->vbt.lvds_ssc_freq;
8980 }
8981
8982 if (intel_is_dual_link_lvds(dev)) {
8983 if (refclk == 100000)
8984 limit = &intel_limits_ironlake_dual_lvds_100m;
8985 else
8986 limit = &intel_limits_ironlake_dual_lvds;
8987 } else {
8988 if (refclk == 100000)
8989 limit = &intel_limits_ironlake_single_lvds_100m;
8990 else
8991 limit = &intel_limits_ironlake_single_lvds;
8992 }
8993 } else {
8994 limit = &intel_limits_ironlake_dac;
8995 }
8996
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008997 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008998 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8999 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009000 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9001 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009002 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009003
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009004 ironlake_compute_dpll(crtc, crtc_state,
9005 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009006
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009007 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9008 if (pll == NULL) {
9009 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9010 pipe_name(crtc->pipe));
9011 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009012 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009013
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009014 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009015 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009016 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009017
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009018 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009019}
9020
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009021static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9022 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009023{
9024 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009025 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009026 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009027
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009028 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9029 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9030 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9031 & ~TU_SIZE_MASK;
9032 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9033 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9034 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9035}
9036
9037static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9038 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009039 struct intel_link_m_n *m_n,
9040 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009041{
9042 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009043 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009044 enum pipe pipe = crtc->pipe;
9045
9046 if (INTEL_INFO(dev)->gen >= 5) {
9047 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9048 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9049 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9050 & ~TU_SIZE_MASK;
9051 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9052 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9053 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009054 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9055 * gen < 8) and if DRRS is supported (to make sure the
9056 * registers are not unnecessarily read).
9057 */
9058 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009059 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009060 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9061 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9062 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9063 & ~TU_SIZE_MASK;
9064 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9065 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9066 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9067 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009068 } else {
9069 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9070 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9071 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9072 & ~TU_SIZE_MASK;
9073 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9074 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9075 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9076 }
9077}
9078
9079void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009080 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009081{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009082 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009083 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9084 else
9085 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009086 &pipe_config->dp_m_n,
9087 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009088}
9089
Daniel Vetter72419202013-04-04 13:28:53 +02009090static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009091 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009092{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009093 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009094 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009095}
9096
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009097static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009098 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009099{
9100 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009101 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009102 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9103 uint32_t ps_ctrl = 0;
9104 int id = -1;
9105 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009106
Chandra Kondurua1b22782015-04-07 15:28:45 -07009107 /* find scaler attached to this pipe */
9108 for (i = 0; i < crtc->num_scalers; i++) {
9109 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9110 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9111 id = i;
9112 pipe_config->pch_pfit.enabled = true;
9113 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9114 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9115 break;
9116 }
9117 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009118
Chandra Kondurua1b22782015-04-07 15:28:45 -07009119 scaler_state->scaler_id = id;
9120 if (id >= 0) {
9121 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9122 } else {
9123 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009124 }
9125}
9126
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009127static void
9128skylake_get_initial_plane_config(struct intel_crtc *crtc,
9129 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009130{
9131 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009132 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009133 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009134 int pipe = crtc->pipe;
9135 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009136 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009137 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009138 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009139
Damien Lespiaud9806c92015-01-21 14:07:19 +00009140 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009141 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009142 DRM_DEBUG_KMS("failed to alloc fb\n");
9143 return;
9144 }
9145
Damien Lespiau1b842c82015-01-21 13:50:54 +00009146 fb = &intel_fb->base;
9147
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009148 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009149 if (!(val & PLANE_CTL_ENABLE))
9150 goto error;
9151
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009152 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9153 fourcc = skl_format_to_fourcc(pixel_format,
9154 val & PLANE_CTL_ORDER_RGBX,
9155 val & PLANE_CTL_ALPHA_MASK);
9156 fb->pixel_format = fourcc;
9157 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9158
Damien Lespiau40f46282015-02-27 11:15:21 +00009159 tiling = val & PLANE_CTL_TILED_MASK;
9160 switch (tiling) {
9161 case PLANE_CTL_TILED_LINEAR:
9162 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9163 break;
9164 case PLANE_CTL_TILED_X:
9165 plane_config->tiling = I915_TILING_X;
9166 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9167 break;
9168 case PLANE_CTL_TILED_Y:
9169 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9170 break;
9171 case PLANE_CTL_TILED_YF:
9172 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9173 break;
9174 default:
9175 MISSING_CASE(tiling);
9176 goto error;
9177 }
9178
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009179 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9180 plane_config->base = base;
9181
9182 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9183
9184 val = I915_READ(PLANE_SIZE(pipe, 0));
9185 fb->height = ((val >> 16) & 0xfff) + 1;
9186 fb->width = ((val >> 0) & 0x1fff) + 1;
9187
9188 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009189 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009190 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009191 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9192
9193 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009194 fb->pixel_format,
9195 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009196
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009197 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009198
9199 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9200 pipe_name(pipe), fb->width, fb->height,
9201 fb->bits_per_pixel, base, fb->pitches[0],
9202 plane_config->size);
9203
Damien Lespiau2d140302015-02-05 17:22:18 +00009204 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009205 return;
9206
9207error:
9208 kfree(fb);
9209}
9210
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009211static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009212 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009213{
9214 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009215 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009216 uint32_t tmp;
9217
9218 tmp = I915_READ(PF_CTL(crtc->pipe));
9219
9220 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009221 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009222 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9223 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009224
9225 /* We currently do not free assignements of panel fitters on
9226 * ivb/hsw (since we don't use the higher upscaling modes which
9227 * differentiates them) so just WARN about this case for now. */
9228 if (IS_GEN7(dev)) {
9229 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9230 PF_PIPE_SEL_IVB(crtc->pipe));
9231 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009232 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009233}
9234
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009235static void
9236ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9237 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009238{
9239 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009240 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009241 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009242 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009243 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009244 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009245 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009246 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009247
Damien Lespiau42a7b082015-02-05 19:35:13 +00009248 val = I915_READ(DSPCNTR(pipe));
9249 if (!(val & DISPLAY_PLANE_ENABLE))
9250 return;
9251
Damien Lespiaud9806c92015-01-21 14:07:19 +00009252 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009253 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009254 DRM_DEBUG_KMS("failed to alloc fb\n");
9255 return;
9256 }
9257
Damien Lespiau1b842c82015-01-21 13:50:54 +00009258 fb = &intel_fb->base;
9259
Daniel Vetter18c52472015-02-10 17:16:09 +00009260 if (INTEL_INFO(dev)->gen >= 4) {
9261 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009262 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009263 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9264 }
9265 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009266
9267 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009268 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009269 fb->pixel_format = fourcc;
9270 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009271
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009272 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009273 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009274 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009275 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009276 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009277 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009278 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009279 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009280 }
9281 plane_config->base = base;
9282
9283 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009284 fb->width = ((val >> 16) & 0xfff) + 1;
9285 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009286
9287 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009288 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009289
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009290 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009291 fb->pixel_format,
9292 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009293
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009294 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009295
Damien Lespiau2844a922015-01-20 12:51:48 +00009296 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9297 pipe_name(pipe), fb->width, fb->height,
9298 fb->bits_per_pixel, base, fb->pitches[0],
9299 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009300
Damien Lespiau2d140302015-02-05 17:22:18 +00009301 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009302}
9303
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009304static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009305 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009306{
9307 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009308 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009309 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009310 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009311 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009312
Imre Deak17290502016-02-12 18:55:11 +02009313 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9314 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009315 return false;
9316
Daniel Vettere143a212013-07-04 12:01:15 +02009317 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009318 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009319
Imre Deak17290502016-02-12 18:55:11 +02009320 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009321 tmp = I915_READ(PIPECONF(crtc->pipe));
9322 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009323 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009324
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009325 switch (tmp & PIPECONF_BPC_MASK) {
9326 case PIPECONF_6BPC:
9327 pipe_config->pipe_bpp = 18;
9328 break;
9329 case PIPECONF_8BPC:
9330 pipe_config->pipe_bpp = 24;
9331 break;
9332 case PIPECONF_10BPC:
9333 pipe_config->pipe_bpp = 30;
9334 break;
9335 case PIPECONF_12BPC:
9336 pipe_config->pipe_bpp = 36;
9337 break;
9338 default:
9339 break;
9340 }
9341
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009342 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9343 pipe_config->limited_color_range = true;
9344
Daniel Vetterab9412b2013-05-03 11:49:46 +02009345 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009346 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009347 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009348
Daniel Vetter88adfff2013-03-28 10:42:01 +01009349 pipe_config->has_pch_encoder = true;
9350
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009351 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9352 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9353 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009354
9355 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009356
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009357 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009358 /*
9359 * The pipe->pch transcoder and pch transcoder->pll
9360 * mapping is fixed.
9361 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009362 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009363 } else {
9364 tmp = I915_READ(PCH_DPLL_SEL);
9365 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009366 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009367 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009368 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009369 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009370
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009371 pipe_config->shared_dpll =
9372 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9373 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009374
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009375 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9376 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009377
9378 tmp = pipe_config->dpll_hw_state.dpll;
9379 pipe_config->pixel_multiplier =
9380 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9381 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009382
9383 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009384 } else {
9385 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009386 }
9387
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009388 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009389 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009390
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009391 ironlake_get_pfit_config(crtc, pipe_config);
9392
Imre Deak17290502016-02-12 18:55:11 +02009393 ret = true;
9394
9395out:
9396 intel_display_power_put(dev_priv, power_domain);
9397
9398 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009399}
9400
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009401static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9402{
Chris Wilson91c8a322016-07-05 10:40:23 +01009403 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009405
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009406 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009407 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009408 pipe_name(crtc->pipe));
9409
Rob Clarke2c719b2014-12-15 13:56:32 -05009410 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9411 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009412 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9413 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009414 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9415 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009416 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009417 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009418 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009419 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009420 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009421 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009422 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009423 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009424 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009425
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009426 /*
9427 * In theory we can still leave IRQs enabled, as long as only the HPD
9428 * interrupts remain enabled. We used to check for that, but since it's
9429 * gen-specific and since we only disable LCPLL after we fully disable
9430 * the interrupts, the check below should be enough.
9431 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009432 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009433}
9434
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009435static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9436{
Chris Wilson91c8a322016-07-05 10:40:23 +01009437 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009438
9439 if (IS_HASWELL(dev))
9440 return I915_READ(D_COMP_HSW);
9441 else
9442 return I915_READ(D_COMP_BDW);
9443}
9444
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009445static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9446{
Chris Wilson91c8a322016-07-05 10:40:23 +01009447 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009448
9449 if (IS_HASWELL(dev)) {
9450 mutex_lock(&dev_priv->rps.hw_lock);
9451 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9452 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009453 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009454 mutex_unlock(&dev_priv->rps.hw_lock);
9455 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009456 I915_WRITE(D_COMP_BDW, val);
9457 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009458 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009459}
9460
9461/*
9462 * This function implements pieces of two sequences from BSpec:
9463 * - Sequence for display software to disable LCPLL
9464 * - Sequence for display software to allow package C8+
9465 * The steps implemented here are just the steps that actually touch the LCPLL
9466 * register. Callers should take care of disabling all the display engine
9467 * functions, doing the mode unset, fixing interrupts, etc.
9468 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009469static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9470 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471{
9472 uint32_t val;
9473
9474 assert_can_disable_lcpll(dev_priv);
9475
9476 val = I915_READ(LCPLL_CTL);
9477
9478 if (switch_to_fclk) {
9479 val |= LCPLL_CD_SOURCE_FCLK;
9480 I915_WRITE(LCPLL_CTL, val);
9481
Imre Deakf53dd632016-06-28 13:37:32 +03009482 if (wait_for_us(I915_READ(LCPLL_CTL) &
9483 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009484 DRM_ERROR("Switching to FCLK failed\n");
9485
9486 val = I915_READ(LCPLL_CTL);
9487 }
9488
9489 val |= LCPLL_PLL_DISABLE;
9490 I915_WRITE(LCPLL_CTL, val);
9491 POSTING_READ(LCPLL_CTL);
9492
Chris Wilson24d84412016-06-30 15:33:07 +01009493 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009494 DRM_ERROR("LCPLL still locked\n");
9495
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009496 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009497 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009498 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009499 ndelay(100);
9500
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009501 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9502 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009503 DRM_ERROR("D_COMP RCOMP still in progress\n");
9504
9505 if (allow_power_down) {
9506 val = I915_READ(LCPLL_CTL);
9507 val |= LCPLL_POWER_DOWN_ALLOW;
9508 I915_WRITE(LCPLL_CTL, val);
9509 POSTING_READ(LCPLL_CTL);
9510 }
9511}
9512
9513/*
9514 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9515 * source.
9516 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009517static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009518{
9519 uint32_t val;
9520
9521 val = I915_READ(LCPLL_CTL);
9522
9523 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9524 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9525 return;
9526
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009527 /*
9528 * Make sure we're not on PC8 state before disabling PC8, otherwise
9529 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009530 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009531 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009532
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009533 if (val & LCPLL_POWER_DOWN_ALLOW) {
9534 val &= ~LCPLL_POWER_DOWN_ALLOW;
9535 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009536 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009537 }
9538
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009539 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009540 val |= D_COMP_COMP_FORCE;
9541 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009542 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009543
9544 val = I915_READ(LCPLL_CTL);
9545 val &= ~LCPLL_PLL_DISABLE;
9546 I915_WRITE(LCPLL_CTL, val);
9547
Chris Wilson93220c02016-06-30 15:33:08 +01009548 if (intel_wait_for_register(dev_priv,
9549 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9550 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009551 DRM_ERROR("LCPLL not locked yet\n");
9552
9553 if (val & LCPLL_CD_SOURCE_FCLK) {
9554 val = I915_READ(LCPLL_CTL);
9555 val &= ~LCPLL_CD_SOURCE_FCLK;
9556 I915_WRITE(LCPLL_CTL, val);
9557
Imre Deakf53dd632016-06-28 13:37:32 +03009558 if (wait_for_us((I915_READ(LCPLL_CTL) &
9559 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009560 DRM_ERROR("Switching back to LCPLL failed\n");
9561 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009562
Mika Kuoppala59bad942015-01-16 11:34:40 +02009563 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +01009564 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009565}
9566
Paulo Zanoni765dab672014-03-07 20:08:18 -03009567/*
9568 * Package states C8 and deeper are really deep PC states that can only be
9569 * reached when all the devices on the system allow it, so even if the graphics
9570 * device allows PC8+, it doesn't mean the system will actually get to these
9571 * states. Our driver only allows PC8+ when going into runtime PM.
9572 *
9573 * The requirements for PC8+ are that all the outputs are disabled, the power
9574 * well is disabled and most interrupts are disabled, and these are also
9575 * requirements for runtime PM. When these conditions are met, we manually do
9576 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9577 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9578 * hang the machine.
9579 *
9580 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9581 * the state of some registers, so when we come back from PC8+ we need to
9582 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9583 * need to take care of the registers kept by RC6. Notice that this happens even
9584 * if we don't put the device in PCI D3 state (which is what currently happens
9585 * because of the runtime PM support).
9586 *
9587 * For more, read "Display Sequences for Package C8" on the hardware
9588 * documentation.
9589 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009590void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009591{
Chris Wilson91c8a322016-07-05 10:40:23 +01009592 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009593 uint32_t val;
9594
Paulo Zanonic67a4702013-08-19 13:18:09 -03009595 DRM_DEBUG_KMS("Enabling package C8+\n");
9596
Ville Syrjäläc2699522015-08-27 23:55:59 +03009597 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009598 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9599 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9600 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9601 }
9602
9603 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009604 hsw_disable_lcpll(dev_priv, true, true);
9605}
9606
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009607void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009608{
Chris Wilson91c8a322016-07-05 10:40:23 +01009609 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009610 uint32_t val;
9611
Paulo Zanonic67a4702013-08-19 13:18:09 -03009612 DRM_DEBUG_KMS("Disabling package C8+\n");
9613
9614 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009615 lpt_init_pch_refclk(dev);
9616
Ville Syrjäläc2699522015-08-27 23:55:59 +03009617 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009618 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9619 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9620 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9621 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009622}
9623
Imre Deak324513c2016-06-13 16:44:36 +03009624static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309625{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009626 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009627 struct intel_atomic_state *old_intel_state =
9628 to_intel_atomic_state(old_state);
9629 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309630
Imre Deak324513c2016-06-13 16:44:36 +03009631 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309632}
9633
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009634/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009635static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009636{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009637 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01009638 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009639 struct drm_crtc *crtc;
9640 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009641 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009642 unsigned max_pixel_rate = 0, i;
9643 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009644
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009645 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9646 sizeof(intel_state->min_pixclk));
9647
9648 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009649 int pixel_rate;
9650
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009651 crtc_state = to_intel_crtc_state(cstate);
9652 if (!crtc_state->base.enable) {
9653 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009654 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009655 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009656
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009657 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009658
9659 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009660 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009661 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9662
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009663 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009664 }
9665
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009666 for_each_pipe(dev_priv, pipe)
9667 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9668
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009669 return max_pixel_rate;
9670}
9671
9672static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9673{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009674 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009675 uint32_t val, data;
9676 int ret;
9677
9678 if (WARN((I915_READ(LCPLL_CTL) &
9679 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9680 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9681 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9682 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9683 "trying to change cdclk frequency with cdclk not enabled\n"))
9684 return;
9685
9686 mutex_lock(&dev_priv->rps.hw_lock);
9687 ret = sandybridge_pcode_write(dev_priv,
9688 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9689 mutex_unlock(&dev_priv->rps.hw_lock);
9690 if (ret) {
9691 DRM_ERROR("failed to inform pcode about cdclk change\n");
9692 return;
9693 }
9694
9695 val = I915_READ(LCPLL_CTL);
9696 val |= LCPLL_CD_SOURCE_FCLK;
9697 I915_WRITE(LCPLL_CTL, val);
9698
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009699 if (wait_for_us(I915_READ(LCPLL_CTL) &
9700 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009701 DRM_ERROR("Switching to FCLK failed\n");
9702
9703 val = I915_READ(LCPLL_CTL);
9704 val &= ~LCPLL_CLK_FREQ_MASK;
9705
9706 switch (cdclk) {
9707 case 450000:
9708 val |= LCPLL_CLK_FREQ_450;
9709 data = 0;
9710 break;
9711 case 540000:
9712 val |= LCPLL_CLK_FREQ_54O_BDW;
9713 data = 1;
9714 break;
9715 case 337500:
9716 val |= LCPLL_CLK_FREQ_337_5_BDW;
9717 data = 2;
9718 break;
9719 case 675000:
9720 val |= LCPLL_CLK_FREQ_675_BDW;
9721 data = 3;
9722 break;
9723 default:
9724 WARN(1, "invalid cdclk frequency\n");
9725 return;
9726 }
9727
9728 I915_WRITE(LCPLL_CTL, val);
9729
9730 val = I915_READ(LCPLL_CTL);
9731 val &= ~LCPLL_CD_SOURCE_FCLK;
9732 I915_WRITE(LCPLL_CTL, val);
9733
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009734 if (wait_for_us((I915_READ(LCPLL_CTL) &
9735 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009736 DRM_ERROR("Switching back to LCPLL failed\n");
9737
9738 mutex_lock(&dev_priv->rps.hw_lock);
9739 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9740 mutex_unlock(&dev_priv->rps.hw_lock);
9741
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009742 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9743
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009744 intel_update_cdclk(dev);
9745
9746 WARN(cdclk != dev_priv->cdclk_freq,
9747 "cdclk requested %d kHz but got %d kHz\n",
9748 cdclk, dev_priv->cdclk_freq);
9749}
9750
Ville Syrjälä587c7912016-05-11 22:44:41 +03009751static int broadwell_calc_cdclk(int max_pixclk)
9752{
9753 if (max_pixclk > 540000)
9754 return 675000;
9755 else if (max_pixclk > 450000)
9756 return 540000;
9757 else if (max_pixclk > 337500)
9758 return 450000;
9759 else
9760 return 337500;
9761}
9762
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009763static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009764{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009765 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009766 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009767 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009768 int cdclk;
9769
9770 /*
9771 * FIXME should also account for plane ratio
9772 * once 64bpp pixel formats are supported.
9773 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009774 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009775
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009776 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009777 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9778 cdclk, dev_priv->max_cdclk_freq);
9779 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009780 }
9781
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009782 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9783 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009784 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009785
9786 return 0;
9787}
9788
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009789static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009790{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009791 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009792 struct intel_atomic_state *old_intel_state =
9793 to_intel_atomic_state(old_state);
9794 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009795
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009796 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009797}
9798
Clint Taylorc89e39f2016-05-13 23:41:21 +03009799static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9800{
9801 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9802 struct drm_i915_private *dev_priv = to_i915(state->dev);
9803 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009804 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009805 int cdclk;
9806
9807 /*
9808 * FIXME should also account for plane ratio
9809 * once 64bpp pixel formats are supported.
9810 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009811 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009812
9813 /*
9814 * FIXME move the cdclk caclulation to
9815 * compute_config() so we can fail gracegully.
9816 */
9817 if (cdclk > dev_priv->max_cdclk_freq) {
9818 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9819 cdclk, dev_priv->max_cdclk_freq);
9820 cdclk = dev_priv->max_cdclk_freq;
9821 }
9822
9823 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9824 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009825 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009826
9827 return 0;
9828}
9829
9830static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9831{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009832 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9833 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9834 unsigned int req_cdclk = intel_state->dev_cdclk;
9835 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009836
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009837 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009838}
9839
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009840static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9841 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009842{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009843 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009844 if (!intel_ddi_pll_select(crtc, crtc_state))
9845 return -EINVAL;
9846 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009847
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009848 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009849
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009850 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009851}
9852
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309853static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9854 enum port port,
9855 struct intel_crtc_state *pipe_config)
9856{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009857 enum intel_dpll_id id;
9858
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309859 switch (port) {
9860 case PORT_A:
9861 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009862 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309863 break;
9864 case PORT_B:
9865 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009866 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309867 break;
9868 case PORT_C:
9869 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009870 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309871 break;
9872 default:
9873 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009874 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309875 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009876
9877 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309878}
9879
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009880static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9881 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009882 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009883{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009884 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009885 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009886
9887 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9888 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9889
9890 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009891 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009892 id = DPLL_ID_SKL_DPLL0;
9893 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009894 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009895 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009896 break;
9897 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009898 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009899 break;
9900 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009901 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009902 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009903 default:
9904 MISSING_CASE(pipe_config->ddi_pll_sel);
9905 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009906 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009907
9908 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009909}
9910
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009911static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9912 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009913 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009914{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009915 enum intel_dpll_id id;
9916
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009917 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9918
9919 switch (pipe_config->ddi_pll_sel) {
9920 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009921 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009922 break;
9923 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009924 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009925 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009926 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009927 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009928 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009929 case PORT_CLK_SEL_LCPLL_810:
9930 id = DPLL_ID_LCPLL_810;
9931 break;
9932 case PORT_CLK_SEL_LCPLL_1350:
9933 id = DPLL_ID_LCPLL_1350;
9934 break;
9935 case PORT_CLK_SEL_LCPLL_2700:
9936 id = DPLL_ID_LCPLL_2700;
9937 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009938 default:
9939 MISSING_CASE(pipe_config->ddi_pll_sel);
9940 /* fall through */
9941 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009942 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009943 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009944
9945 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009946}
9947
Jani Nikulacf304292016-03-18 17:05:41 +02009948static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9949 struct intel_crtc_state *pipe_config,
9950 unsigned long *power_domain_mask)
9951{
9952 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009953 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02009954 enum intel_display_power_domain power_domain;
9955 u32 tmp;
9956
Imre Deakd9a7bc62016-05-12 16:18:50 +03009957 /*
9958 * The pipe->transcoder mapping is fixed with the exception of the eDP
9959 * transcoder handled below.
9960 */
Jani Nikulacf304292016-03-18 17:05:41 +02009961 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9962
9963 /*
9964 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9965 * consistency and less surprising code; it's in always on power).
9966 */
9967 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9968 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9969 enum pipe trans_edp_pipe;
9970 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9971 default:
9972 WARN(1, "unknown pipe linked to edp transcoder\n");
9973 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9974 case TRANS_DDI_EDP_INPUT_A_ON:
9975 trans_edp_pipe = PIPE_A;
9976 break;
9977 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9978 trans_edp_pipe = PIPE_B;
9979 break;
9980 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9981 trans_edp_pipe = PIPE_C;
9982 break;
9983 }
9984
9985 if (trans_edp_pipe == crtc->pipe)
9986 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9987 }
9988
9989 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9990 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9991 return false;
9992 *power_domain_mask |= BIT(power_domain);
9993
9994 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9995
9996 return tmp & PIPECONF_ENABLE;
9997}
9998
Jani Nikula4d1de972016-03-18 17:05:42 +02009999static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10000 struct intel_crtc_state *pipe_config,
10001 unsigned long *power_domain_mask)
10002{
10003 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010004 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010005 enum intel_display_power_domain power_domain;
10006 enum port port;
10007 enum transcoder cpu_transcoder;
10008 u32 tmp;
10009
Jani Nikula4d1de972016-03-18 17:05:42 +020010010 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10011 if (port == PORT_A)
10012 cpu_transcoder = TRANSCODER_DSI_A;
10013 else
10014 cpu_transcoder = TRANSCODER_DSI_C;
10015
10016 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10017 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10018 continue;
10019 *power_domain_mask |= BIT(power_domain);
10020
Imre Deakdb18b6a2016-03-24 12:41:40 +020010021 /*
10022 * The PLL needs to be enabled with a valid divider
10023 * configuration, otherwise accessing DSI registers will hang
10024 * the machine. See BSpec North Display Engine
10025 * registers/MIPI[BXT]. We can break out here early, since we
10026 * need the same DSI PLL to be enabled for both DSI ports.
10027 */
10028 if (!intel_dsi_pll_is_enabled(dev_priv))
10029 break;
10030
Jani Nikula4d1de972016-03-18 17:05:42 +020010031 /* XXX: this works for video mode only */
10032 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10033 if (!(tmp & DPI_ENABLE))
10034 continue;
10035
10036 tmp = I915_READ(MIPI_CTRL(port));
10037 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10038 continue;
10039
10040 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010041 break;
10042 }
10043
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010044 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010045}
10046
Daniel Vetter26804af2014-06-25 22:01:55 +030010047static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010048 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010049{
10050 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010051 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010052 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010053 enum port port;
10054 uint32_t tmp;
10055
10056 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10057
10058 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10059
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010060 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010061 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010062 else if (IS_BROXTON(dev))
10063 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010064 else
10065 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010066
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010067 pll = pipe_config->shared_dpll;
10068 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010069 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10070 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010071 }
10072
Daniel Vetter26804af2014-06-25 22:01:55 +030010073 /*
10074 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10075 * DDI E. So just check whether this pipe is wired to DDI E and whether
10076 * the PCH transcoder is on.
10077 */
Damien Lespiauca370452013-12-03 13:56:24 +000010078 if (INTEL_INFO(dev)->gen < 9 &&
10079 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010080 pipe_config->has_pch_encoder = true;
10081
10082 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10083 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10084 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10085
10086 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10087 }
10088}
10089
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010090static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010091 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010092{
10093 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010094 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010095 enum intel_display_power_domain power_domain;
10096 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010097 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010098
Imre Deak17290502016-02-12 18:55:11 +020010099 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10100 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010101 return false;
Imre Deak17290502016-02-12 18:55:11 +020010102 power_domain_mask = BIT(power_domain);
10103
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010104 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010105
Jani Nikulacf304292016-03-18 17:05:41 +020010106 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010107
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010108 if (IS_BROXTON(dev_priv) &&
10109 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10110 WARN_ON(active);
10111 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010112 }
10113
Jani Nikulacf304292016-03-18 17:05:41 +020010114 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010115 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010116
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010117 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010118 haswell_get_ddi_port_state(crtc, pipe_config);
10119 intel_get_pipe_timings(crtc, pipe_config);
10120 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010121
Jani Nikulabc58be62016-03-18 17:05:39 +020010122 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010123
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010124 pipe_config->gamma_mode =
10125 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10126
Chandra Kondurua1b22782015-04-07 15:28:45 -070010127 if (INTEL_INFO(dev)->gen >= 9) {
10128 skl_init_scalers(dev, crtc, pipe_config);
10129 }
10130
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010131 if (INTEL_INFO(dev)->gen >= 9) {
10132 pipe_config->scaler_state.scaler_id = -1;
10133 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10134 }
10135
Imre Deak17290502016-02-12 18:55:11 +020010136 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10137 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10138 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010139 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010140 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010141 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010142 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010143 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010144
Jesse Barnese59150d2014-01-07 13:30:45 -080010145 if (IS_HASWELL(dev))
10146 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10147 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010148
Jani Nikula4d1de972016-03-18 17:05:42 +020010149 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10150 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010151 pipe_config->pixel_multiplier =
10152 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10153 } else {
10154 pipe_config->pixel_multiplier = 1;
10155 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010156
Imre Deak17290502016-02-12 18:55:11 +020010157out:
10158 for_each_power_domain(power_domain, power_domain_mask)
10159 intel_display_power_put(dev_priv, power_domain);
10160
Jani Nikulacf304292016-03-18 17:05:41 +020010161 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010162}
10163
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010164static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10165 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010166{
10167 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010168 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010170 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010171
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010172 if (plane_state && plane_state->visible) {
10173 unsigned int width = plane_state->base.crtc_w;
10174 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010175 unsigned int stride = roundup_pow_of_two(width) * 4;
10176
10177 switch (stride) {
10178 default:
10179 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10180 width, stride);
10181 stride = 256;
10182 /* fallthrough */
10183 case 256:
10184 case 512:
10185 case 1024:
10186 case 2048:
10187 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010188 }
10189
Ville Syrjälädc41c152014-08-13 11:57:05 +030010190 cntl |= CURSOR_ENABLE |
10191 CURSOR_GAMMA_ENABLE |
10192 CURSOR_FORMAT_ARGB |
10193 CURSOR_STRIDE(stride);
10194
10195 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010196 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010197
Ville Syrjälädc41c152014-08-13 11:57:05 +030010198 if (intel_crtc->cursor_cntl != 0 &&
10199 (intel_crtc->cursor_base != base ||
10200 intel_crtc->cursor_size != size ||
10201 intel_crtc->cursor_cntl != cntl)) {
10202 /* On these chipsets we can only modify the base/size/stride
10203 * whilst the cursor is disabled.
10204 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010205 I915_WRITE(CURCNTR(PIPE_A), 0);
10206 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010207 intel_crtc->cursor_cntl = 0;
10208 }
10209
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010210 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010211 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010212 intel_crtc->cursor_base = base;
10213 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010214
10215 if (intel_crtc->cursor_size != size) {
10216 I915_WRITE(CURSIZE, size);
10217 intel_crtc->cursor_size = size;
10218 }
10219
Chris Wilson4b0e3332014-05-30 16:35:26 +030010220 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010221 I915_WRITE(CURCNTR(PIPE_A), cntl);
10222 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010223 intel_crtc->cursor_cntl = cntl;
10224 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010225}
10226
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010227static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10228 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010229{
10230 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010231 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010232 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10233 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010234 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010235
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010236 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010237 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010238 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010239 case 64:
10240 cntl |= CURSOR_MODE_64_ARGB_AX;
10241 break;
10242 case 128:
10243 cntl |= CURSOR_MODE_128_ARGB_AX;
10244 break;
10245 case 256:
10246 cntl |= CURSOR_MODE_256_ARGB_AX;
10247 break;
10248 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010249 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010250 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010251 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010252 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010253
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010254 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010255 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010256
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010257 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10258 cntl |= CURSOR_ROTATE_180;
10259 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010260
Chris Wilson4b0e3332014-05-30 16:35:26 +030010261 if (intel_crtc->cursor_cntl != cntl) {
10262 I915_WRITE(CURCNTR(pipe), cntl);
10263 POSTING_READ(CURCNTR(pipe));
10264 intel_crtc->cursor_cntl = cntl;
10265 }
10266
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010267 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010268 I915_WRITE(CURBASE(pipe), base);
10269 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010270
10271 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010272}
10273
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010274/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010275static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010276 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010277{
10278 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010279 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10281 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010282 u32 base = intel_crtc->cursor_addr;
10283 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010284
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010285 if (plane_state) {
10286 int x = plane_state->base.crtc_x;
10287 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010288
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010289 if (x < 0) {
10290 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10291 x = -x;
10292 }
10293 pos |= x << CURSOR_X_SHIFT;
10294
10295 if (y < 0) {
10296 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10297 y = -y;
10298 }
10299 pos |= y << CURSOR_Y_SHIFT;
10300
10301 /* ILK+ do this automagically */
10302 if (HAS_GMCH_DISPLAY(dev) &&
10303 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10304 base += (plane_state->base.crtc_h *
10305 plane_state->base.crtc_w - 1) * 4;
10306 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010307 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010308
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010309 I915_WRITE(CURPOS(pipe), pos);
10310
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010311 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010312 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010313 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010314 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010315}
10316
Ville Syrjälädc41c152014-08-13 11:57:05 +030010317static bool cursor_size_ok(struct drm_device *dev,
10318 uint32_t width, uint32_t height)
10319{
10320 if (width == 0 || height == 0)
10321 return false;
10322
10323 /*
10324 * 845g/865g are special in that they are only limited by
10325 * the width of their cursors, the height is arbitrary up to
10326 * the precision of the register. Everything else requires
10327 * square cursors, limited to a few power-of-two sizes.
10328 */
10329 if (IS_845G(dev) || IS_I865G(dev)) {
10330 if ((width & 63) != 0)
10331 return false;
10332
10333 if (width > (IS_845G(dev) ? 64 : 512))
10334 return false;
10335
10336 if (height > 1023)
10337 return false;
10338 } else {
10339 switch (width | height) {
10340 case 256:
10341 case 128:
10342 if (IS_GEN2(dev))
10343 return false;
10344 case 64:
10345 break;
10346 default:
10347 return false;
10348 }
10349 }
10350
10351 return true;
10352}
10353
Jesse Barnes79e53942008-11-07 14:24:08 -080010354/* VESA 640x480x72Hz mode to set on the pipe */
10355static struct drm_display_mode load_detect_mode = {
10356 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10357 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10358};
10359
Daniel Vettera8bb6812014-02-10 18:00:39 +010010360struct drm_framebuffer *
10361__intel_framebuffer_create(struct drm_device *dev,
10362 struct drm_mode_fb_cmd2 *mode_cmd,
10363 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010364{
10365 struct intel_framebuffer *intel_fb;
10366 int ret;
10367
10368 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010369 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010370 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010371
10372 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010373 if (ret)
10374 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010375
10376 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010377
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010378err:
10379 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010380 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010381}
10382
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010383static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010384intel_framebuffer_create(struct drm_device *dev,
10385 struct drm_mode_fb_cmd2 *mode_cmd,
10386 struct drm_i915_gem_object *obj)
10387{
10388 struct drm_framebuffer *fb;
10389 int ret;
10390
10391 ret = i915_mutex_lock_interruptible(dev);
10392 if (ret)
10393 return ERR_PTR(ret);
10394 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10395 mutex_unlock(&dev->struct_mutex);
10396
10397 return fb;
10398}
10399
Chris Wilsond2dff872011-04-19 08:36:26 +010010400static u32
10401intel_framebuffer_pitch_for_width(int width, int bpp)
10402{
10403 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10404 return ALIGN(pitch, 64);
10405}
10406
10407static u32
10408intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10409{
10410 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010411 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010412}
10413
10414static struct drm_framebuffer *
10415intel_framebuffer_create_for_mode(struct drm_device *dev,
10416 struct drm_display_mode *mode,
10417 int depth, int bpp)
10418{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010419 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010420 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010421 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010422
Dave Gordond37cd8a2016-04-22 19:14:32 +010010423 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010424 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010425 if (IS_ERR(obj))
10426 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010427
10428 mode_cmd.width = mode->hdisplay;
10429 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010430 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10431 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010432 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010433
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010434 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10435 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010010436 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010437
10438 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010439}
10440
10441static struct drm_framebuffer *
10442mode_fits_in_fbdev(struct drm_device *dev,
10443 struct drm_display_mode *mode)
10444{
Daniel Vetter06957262015-08-10 13:34:08 +020010445#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010010446 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010010447 struct drm_i915_gem_object *obj;
10448 struct drm_framebuffer *fb;
10449
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010450 if (!dev_priv->fbdev)
10451 return NULL;
10452
10453 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010454 return NULL;
10455
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010456 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010457 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010458
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010459 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010460 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10461 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010462 return NULL;
10463
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010464 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010465 return NULL;
10466
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010467 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010468 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010469#else
10470 return NULL;
10471#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010472}
10473
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010474static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10475 struct drm_crtc *crtc,
10476 struct drm_display_mode *mode,
10477 struct drm_framebuffer *fb,
10478 int x, int y)
10479{
10480 struct drm_plane_state *plane_state;
10481 int hdisplay, vdisplay;
10482 int ret;
10483
10484 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10485 if (IS_ERR(plane_state))
10486 return PTR_ERR(plane_state);
10487
10488 if (mode)
10489 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10490 else
10491 hdisplay = vdisplay = 0;
10492
10493 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10494 if (ret)
10495 return ret;
10496 drm_atomic_set_fb_for_plane(plane_state, fb);
10497 plane_state->crtc_x = 0;
10498 plane_state->crtc_y = 0;
10499 plane_state->crtc_w = hdisplay;
10500 plane_state->crtc_h = vdisplay;
10501 plane_state->src_x = x << 16;
10502 plane_state->src_y = y << 16;
10503 plane_state->src_w = hdisplay << 16;
10504 plane_state->src_h = vdisplay << 16;
10505
10506 return 0;
10507}
10508
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010509bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010510 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010511 struct intel_load_detect_pipe *old,
10512 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010513{
10514 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010515 struct intel_encoder *intel_encoder =
10516 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010517 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010518 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010519 struct drm_crtc *crtc = NULL;
10520 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010521 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010522 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010523 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010524 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010525 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010526 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010527
Chris Wilsond2dff872011-04-19 08:36:26 +010010528 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010529 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010530 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010531
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010532 old->restore_state = NULL;
10533
Rob Clark51fd3712013-11-19 12:10:12 -050010534retry:
10535 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10536 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010537 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010538
Jesse Barnes79e53942008-11-07 14:24:08 -080010539 /*
10540 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010541 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010542 * - if the connector already has an assigned crtc, use it (but make
10543 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010544 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010545 * - try to find the first unused crtc that can drive this connector,
10546 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010547 */
10548
10549 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010550 if (connector->state->crtc) {
10551 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010552
Rob Clark51fd3712013-11-19 12:10:12 -050010553 ret = drm_modeset_lock(&crtc->mutex, ctx);
10554 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010555 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010556
10557 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010558 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010559 }
10560
10561 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010562 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010563 i++;
10564 if (!(encoder->possible_crtcs & (1 << i)))
10565 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010566
10567 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10568 if (ret)
10569 goto fail;
10570
10571 if (possible_crtc->state->enable) {
10572 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010573 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010574 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010575
10576 crtc = possible_crtc;
10577 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010578 }
10579
10580 /*
10581 * If we didn't find an unused CRTC, don't use any.
10582 */
10583 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010584 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010585 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010586 }
10587
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010588found:
10589 intel_crtc = to_intel_crtc(crtc);
10590
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010591 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10592 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010593 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010594
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010595 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010596 restore_state = drm_atomic_state_alloc(dev);
10597 if (!state || !restore_state) {
10598 ret = -ENOMEM;
10599 goto fail;
10600 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010601
10602 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010603 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010604
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010605 connector_state = drm_atomic_get_connector_state(state, connector);
10606 if (IS_ERR(connector_state)) {
10607 ret = PTR_ERR(connector_state);
10608 goto fail;
10609 }
10610
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010611 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10612 if (ret)
10613 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010614
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010615 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10616 if (IS_ERR(crtc_state)) {
10617 ret = PTR_ERR(crtc_state);
10618 goto fail;
10619 }
10620
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010621 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010622
Chris Wilson64927112011-04-20 07:25:26 +010010623 if (!mode)
10624 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010625
Chris Wilsond2dff872011-04-19 08:36:26 +010010626 /* We need a framebuffer large enough to accommodate all accesses
10627 * that the plane may generate whilst we perform load detection.
10628 * We can not rely on the fbcon either being present (we get called
10629 * during its initialisation to detect all boot displays, or it may
10630 * not even exist) or that it is large enough to satisfy the
10631 * requested mode.
10632 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010633 fb = mode_fits_in_fbdev(dev, mode);
10634 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010635 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010636 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010637 } else
10638 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010639 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010640 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010641 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010642 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010643
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010644 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10645 if (ret)
10646 goto fail;
10647
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010648 drm_framebuffer_unreference(fb);
10649
10650 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10651 if (ret)
10652 goto fail;
10653
10654 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10655 if (!ret)
10656 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10657 if (!ret)
10658 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10659 if (ret) {
10660 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10661 goto fail;
10662 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010663
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010664 ret = drm_atomic_commit(state);
10665 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010666 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010667 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010668 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010669
10670 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010671
Jesse Barnes79e53942008-11-07 14:24:08 -080010672 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010673 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010674 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010675
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010676fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010677 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010678 drm_atomic_state_free(restore_state);
10679 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010680
Rob Clark51fd3712013-11-19 12:10:12 -050010681 if (ret == -EDEADLK) {
10682 drm_modeset_backoff(ctx);
10683 goto retry;
10684 }
10685
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010686 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010687}
10688
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010689void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010690 struct intel_load_detect_pipe *old,
10691 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010692{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010693 struct intel_encoder *intel_encoder =
10694 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010695 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010696 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010697 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010698
Chris Wilsond2dff872011-04-19 08:36:26 +010010699 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010700 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010701 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010702
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010703 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010704 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010705
10706 ret = drm_atomic_commit(state);
10707 if (ret) {
10708 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10709 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010710 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010711}
10712
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010713static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010714 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010715{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010716 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010717 u32 dpll = pipe_config->dpll_hw_state.dpll;
10718
10719 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010720 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010721 else if (HAS_PCH_SPLIT(dev))
10722 return 120000;
10723 else if (!IS_GEN2(dev))
10724 return 96000;
10725 else
10726 return 48000;
10727}
10728
Jesse Barnes79e53942008-11-07 14:24:08 -080010729/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010730static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010731 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010732{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010733 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010734 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010735 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010736 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010737 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010738 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010739 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010740 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010741
10742 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010743 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010744 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010745 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010746
10747 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010748 if (IS_PINEVIEW(dev)) {
10749 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10750 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010751 } else {
10752 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10753 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10754 }
10755
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010756 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010757 if (IS_PINEVIEW(dev))
10758 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10759 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010760 else
10761 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010762 DPLL_FPA01_P1_POST_DIV_SHIFT);
10763
10764 switch (dpll & DPLL_MODE_MASK) {
10765 case DPLLB_MODE_DAC_SERIAL:
10766 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10767 5 : 10;
10768 break;
10769 case DPLLB_MODE_LVDS:
10770 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10771 7 : 14;
10772 break;
10773 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010774 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010775 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010776 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010777 }
10778
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010779 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010780 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010781 else
Imre Deakdccbea32015-06-22 23:35:51 +030010782 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010783 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010784 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010785 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010786
10787 if (is_lvds) {
10788 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10789 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010790
10791 if (lvds & LVDS_CLKB_POWER_UP)
10792 clock.p2 = 7;
10793 else
10794 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010795 } else {
10796 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10797 clock.p1 = 2;
10798 else {
10799 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10800 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10801 }
10802 if (dpll & PLL_P2_DIVIDE_BY_4)
10803 clock.p2 = 4;
10804 else
10805 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010806 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010807
Imre Deakdccbea32015-06-22 23:35:51 +030010808 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010809 }
10810
Ville Syrjälä18442d02013-09-13 16:00:08 +030010811 /*
10812 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010813 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010814 * encoder's get_config() function.
10815 */
Imre Deakdccbea32015-06-22 23:35:51 +030010816 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010817}
10818
Ville Syrjälä6878da02013-09-13 15:59:11 +030010819int intel_dotclock_calculate(int link_freq,
10820 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010821{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010822 /*
10823 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010824 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010825 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010826 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010827 *
10828 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010829 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010830 */
10831
Ville Syrjälä6878da02013-09-13 15:59:11 +030010832 if (!m_n->link_n)
10833 return 0;
10834
10835 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10836}
10837
Ville Syrjälä18442d02013-09-13 16:00:08 +030010838static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010839 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010840{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010841 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010842
10843 /* read out port_clock from the DPLL */
10844 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010845
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010846 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010847 * In case there is an active pipe without active ports,
10848 * we may need some idea for the dotclock anyway.
10849 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010850 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010851 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010852 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010853 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010854}
10855
10856/** Returns the currently programmed mode of the given pipe. */
10857struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10858 struct drm_crtc *crtc)
10859{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010860 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010862 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010863 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010864 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010865 int htot = I915_READ(HTOTAL(cpu_transcoder));
10866 int hsync = I915_READ(HSYNC(cpu_transcoder));
10867 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10868 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010869 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010870
10871 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10872 if (!mode)
10873 return NULL;
10874
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010875 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10876 if (!pipe_config) {
10877 kfree(mode);
10878 return NULL;
10879 }
10880
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010881 /*
10882 * Construct a pipe_config sufficient for getting the clock info
10883 * back out of crtc_clock_get.
10884 *
10885 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10886 * to use a real value here instead.
10887 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010888 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10889 pipe_config->pixel_multiplier = 1;
10890 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10891 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10892 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10893 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010894
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010895 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010896 mode->hdisplay = (htot & 0xffff) + 1;
10897 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10898 mode->hsync_start = (hsync & 0xffff) + 1;
10899 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10900 mode->vdisplay = (vtot & 0xffff) + 1;
10901 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10902 mode->vsync_start = (vsync & 0xffff) + 1;
10903 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10904
10905 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010906
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010907 kfree(pipe_config);
10908
Jesse Barnes79e53942008-11-07 14:24:08 -080010909 return mode;
10910}
10911
10912static void intel_crtc_destroy(struct drm_crtc *crtc)
10913{
10914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010915 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010916 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010917
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010918 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010919 work = intel_crtc->flip_work;
10920 intel_crtc->flip_work = NULL;
10921 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010922
Daniel Vetter5a21b662016-05-24 17:13:53 +020010923 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010924 cancel_work_sync(&work->mmio_work);
10925 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010926 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010927 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010928
10929 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010930
Jesse Barnes79e53942008-11-07 14:24:08 -080010931 kfree(intel_crtc);
10932}
10933
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010934static void intel_unpin_work_fn(struct work_struct *__work)
10935{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010936 struct intel_flip_work *work =
10937 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010938 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10939 struct drm_device *dev = crtc->base.dev;
10940 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010941
Daniel Vetter5a21b662016-05-24 17:13:53 +020010942 if (is_mmio_work(work))
10943 flush_work(&work->mmio_work);
10944
10945 mutex_lock(&dev->struct_mutex);
10946 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010010947 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010948 mutex_unlock(&dev->struct_mutex);
10949
Chris Wilsone8a261e2016-07-20 13:31:49 +010010950 i915_gem_request_put(work->flip_queued_req);
10951
Chris Wilson5748b6a2016-08-04 16:32:38 +010010952 intel_frontbuffer_flip_complete(to_i915(dev),
10953 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010954 intel_fbc_post_update(crtc);
10955 drm_framebuffer_unreference(work->old_fb);
10956
10957 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10958 atomic_dec(&crtc->unpin_work_count);
10959
10960 kfree(work);
10961}
10962
10963/* Is 'a' after or equal to 'b'? */
10964static bool g4x_flip_count_after_eq(u32 a, u32 b)
10965{
10966 return !((a - b) & 0x80000000);
10967}
10968
10969static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10970 struct intel_flip_work *work)
10971{
10972 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010973 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010974 unsigned reset_counter;
10975
10976 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10977 if (crtc->reset_counter != reset_counter)
10978 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010979
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010980 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010981 * The relevant registers doen't exist on pre-ctg.
10982 * As the flip done interrupt doesn't trigger for mmio
10983 * flips on gmch platforms, a flip count check isn't
10984 * really needed there. But since ctg has the registers,
10985 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010986 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020010987 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10988 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010989
Daniel Vetter5a21b662016-05-24 17:13:53 +020010990 /*
10991 * BDW signals flip done immediately if the plane
10992 * is disabled, even if the plane enable is already
10993 * armed to occur at the next vblank :(
10994 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010995
Daniel Vetter5a21b662016-05-24 17:13:53 +020010996 /*
10997 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10998 * used the same base address. In that case the mmio flip might
10999 * have completed, but the CS hasn't even executed the flip yet.
11000 *
11001 * A flip count check isn't enough as the CS might have updated
11002 * the base address just after start of vblank, but before we
11003 * managed to process the interrupt. This means we'd complete the
11004 * CS flip too soon.
11005 *
11006 * Combining both checks should get us a good enough result. It may
11007 * still happen that the CS flip has been executed, but has not
11008 * yet actually completed. But in case the base address is the same
11009 * anyway, we don't really care.
11010 */
11011 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11012 crtc->flip_work->gtt_offset &&
11013 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11014 crtc->flip_work->flip_count);
11015}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011016
Daniel Vetter5a21b662016-05-24 17:13:53 +020011017static bool
11018__pageflip_finished_mmio(struct intel_crtc *crtc,
11019 struct intel_flip_work *work)
11020{
11021 /*
11022 * MMIO work completes when vblank is different from
11023 * flip_queued_vblank.
11024 *
11025 * Reset counter value doesn't matter, this is handled by
11026 * i915_wait_request finishing early, so no need to handle
11027 * reset here.
11028 */
11029 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011030}
11031
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011032
11033static bool pageflip_finished(struct intel_crtc *crtc,
11034 struct intel_flip_work *work)
11035{
11036 if (!atomic_read(&work->pending))
11037 return false;
11038
11039 smp_rmb();
11040
Daniel Vetter5a21b662016-05-24 17:13:53 +020011041 if (is_mmio_work(work))
11042 return __pageflip_finished_mmio(crtc, work);
11043 else
11044 return __pageflip_finished_cs(crtc, work);
11045}
11046
11047void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11048{
Chris Wilson91c8a322016-07-05 10:40:23 +010011049 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011050 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11052 struct intel_flip_work *work;
11053 unsigned long flags;
11054
11055 /* Ignore early vblank irqs */
11056 if (!crtc)
11057 return;
11058
Daniel Vetterf3260382014-09-15 14:55:23 +020011059 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011060 * This is called both by irq handlers and the reset code (to complete
11061 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011062 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011063 spin_lock_irqsave(&dev->event_lock, flags);
11064 work = intel_crtc->flip_work;
11065
11066 if (work != NULL &&
11067 !is_mmio_work(work) &&
11068 pageflip_finished(intel_crtc, work))
11069 page_flip_completed(intel_crtc);
11070
11071 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011072}
11073
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011074void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011075{
Chris Wilson91c8a322016-07-05 10:40:23 +010011076 struct drm_device *dev = &dev_priv->drm;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011077 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11079 struct intel_flip_work *work;
11080 unsigned long flags;
11081
11082 /* Ignore early vblank irqs */
11083 if (!crtc)
11084 return;
11085
11086 /*
11087 * This is called both by irq handlers and the reset code (to complete
11088 * lost pageflips) so needs the full irqsave spinlocks.
11089 */
11090 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011091 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011092
Daniel Vetter5a21b662016-05-24 17:13:53 +020011093 if (work != NULL &&
11094 is_mmio_work(work) &&
11095 pageflip_finished(intel_crtc, work))
11096 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011097
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011098 spin_unlock_irqrestore(&dev->event_lock, flags);
11099}
11100
Daniel Vetter5a21b662016-05-24 17:13:53 +020011101static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11102 struct intel_flip_work *work)
11103{
11104 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11105
11106 /* Ensure that the work item is consistent when activating it ... */
11107 smp_mb__before_atomic();
11108 atomic_set(&work->pending, 1);
11109}
11110
11111static int intel_gen2_queue_flip(struct drm_device *dev,
11112 struct drm_crtc *crtc,
11113 struct drm_framebuffer *fb,
11114 struct drm_i915_gem_object *obj,
11115 struct drm_i915_gem_request *req,
11116 uint32_t flags)
11117{
Chris Wilson7e37f882016-08-02 22:50:21 +010011118 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11120 u32 flip_mask;
11121 int ret;
11122
11123 ret = intel_ring_begin(req, 6);
11124 if (ret)
11125 return ret;
11126
11127 /* Can't queue multiple flips, so wait for the previous
11128 * one to finish before executing the next.
11129 */
11130 if (intel_crtc->plane)
11131 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11132 else
11133 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011134 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11135 intel_ring_emit(ring, MI_NOOP);
11136 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011137 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011138 intel_ring_emit(ring, fb->pitches[0]);
11139 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11140 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011141
11142 return 0;
11143}
11144
11145static int intel_gen3_queue_flip(struct drm_device *dev,
11146 struct drm_crtc *crtc,
11147 struct drm_framebuffer *fb,
11148 struct drm_i915_gem_object *obj,
11149 struct drm_i915_gem_request *req,
11150 uint32_t flags)
11151{
Chris Wilson7e37f882016-08-02 22:50:21 +010011152 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11154 u32 flip_mask;
11155 int ret;
11156
11157 ret = intel_ring_begin(req, 6);
11158 if (ret)
11159 return ret;
11160
11161 if (intel_crtc->plane)
11162 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11163 else
11164 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011165 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11166 intel_ring_emit(ring, MI_NOOP);
11167 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011168 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011169 intel_ring_emit(ring, fb->pitches[0]);
11170 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11171 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011172
11173 return 0;
11174}
11175
11176static int intel_gen4_queue_flip(struct drm_device *dev,
11177 struct drm_crtc *crtc,
11178 struct drm_framebuffer *fb,
11179 struct drm_i915_gem_object *obj,
11180 struct drm_i915_gem_request *req,
11181 uint32_t flags)
11182{
Chris Wilson7e37f882016-08-02 22:50:21 +010011183 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011184 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11186 uint32_t pf, pipesrc;
11187 int ret;
11188
11189 ret = intel_ring_begin(req, 4);
11190 if (ret)
11191 return ret;
11192
11193 /* i965+ uses the linear or tiled offsets from the
11194 * Display Registers (which do not change across a page-flip)
11195 * so we need only reprogram the base address.
11196 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011197 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011198 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011199 intel_ring_emit(ring, fb->pitches[0]);
11200 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Chris Wilson3e510a82016-08-05 10:14:23 +010011201 i915_gem_object_get_tiling(obj));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011202
11203 /* XXX Enabling the panel-fitter across page-flip is so far
11204 * untested on non-native modes, so ignore it for now.
11205 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11206 */
11207 pf = 0;
11208 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011209 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011210
11211 return 0;
11212}
11213
11214static int intel_gen6_queue_flip(struct drm_device *dev,
11215 struct drm_crtc *crtc,
11216 struct drm_framebuffer *fb,
11217 struct drm_i915_gem_object *obj,
11218 struct drm_i915_gem_request *req,
11219 uint32_t flags)
11220{
Chris Wilson7e37f882016-08-02 22:50:21 +010011221 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011222 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11224 uint32_t pf, pipesrc;
11225 int ret;
11226
11227 ret = intel_ring_begin(req, 4);
11228 if (ret)
11229 return ret;
11230
Chris Wilsonb5321f32016-08-02 22:50:18 +010011231 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011232 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilson3e510a82016-08-05 10:14:23 +010011233 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011234 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011235
11236 /* Contrary to the suggestions in the documentation,
11237 * "Enable Panel Fitter" does not seem to be required when page
11238 * flipping with a non-native mode, and worse causes a normal
11239 * modeset to fail.
11240 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11241 */
11242 pf = 0;
11243 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011244 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011245
11246 return 0;
11247}
11248
11249static int intel_gen7_queue_flip(struct drm_device *dev,
11250 struct drm_crtc *crtc,
11251 struct drm_framebuffer *fb,
11252 struct drm_i915_gem_object *obj,
11253 struct drm_i915_gem_request *req,
11254 uint32_t flags)
11255{
Chris Wilson7e37f882016-08-02 22:50:21 +010011256 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11258 uint32_t plane_bit = 0;
11259 int len, ret;
11260
11261 switch (intel_crtc->plane) {
11262 case PLANE_A:
11263 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11264 break;
11265 case PLANE_B:
11266 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11267 break;
11268 case PLANE_C:
11269 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11270 break;
11271 default:
11272 WARN_ONCE(1, "unknown plane in flip command\n");
11273 return -ENODEV;
11274 }
11275
11276 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011277 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011278 len += 6;
11279 /*
11280 * On Gen 8, SRM is now taking an extra dword to accommodate
11281 * 48bits addresses, and we need a NOOP for the batch size to
11282 * stay even.
11283 */
11284 if (IS_GEN8(dev))
11285 len += 2;
11286 }
11287
11288 /*
11289 * BSpec MI_DISPLAY_FLIP for IVB:
11290 * "The full packet must be contained within the same cache line."
11291 *
11292 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11293 * cacheline, if we ever start emitting more commands before
11294 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11295 * then do the cacheline alignment, and finally emit the
11296 * MI_DISPLAY_FLIP.
11297 */
11298 ret = intel_ring_cacheline_align(req);
11299 if (ret)
11300 return ret;
11301
11302 ret = intel_ring_begin(req, len);
11303 if (ret)
11304 return ret;
11305
11306 /* Unmask the flip-done completion message. Note that the bspec says that
11307 * we should do this for both the BCS and RCS, and that we must not unmask
11308 * more than one flip event at any time (or ensure that one flip message
11309 * can be sent by waiting for flip-done prior to queueing new flips).
11310 * Experimentation says that BCS works despite DERRMR masking all
11311 * flip-done completion events and that unmasking all planes at once
11312 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11313 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11314 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011315 if (req->engine->id == RCS) {
11316 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11317 intel_ring_emit_reg(ring, DERRMR);
11318 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011319 DERRMR_PIPEB_PRI_FLIP_DONE |
11320 DERRMR_PIPEC_PRI_FLIP_DONE));
11321 if (IS_GEN8(dev))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011322 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011323 MI_SRM_LRM_GLOBAL_GTT);
11324 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011325 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011326 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011327 intel_ring_emit_reg(ring, DERRMR);
11328 intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011329 if (IS_GEN8(dev)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011330 intel_ring_emit(ring, 0);
11331 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011332 }
11333 }
11334
Chris Wilsonb5321f32016-08-02 22:50:18 +010011335 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Chris Wilson3e510a82016-08-05 10:14:23 +010011336 intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011337 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11338 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011339
11340 return 0;
11341}
11342
11343static bool use_mmio_flip(struct intel_engine_cs *engine,
11344 struct drm_i915_gem_object *obj)
11345{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011346 struct reservation_object *resv;
11347
Daniel Vetter5a21b662016-05-24 17:13:53 +020011348 /*
11349 * This is not being used for older platforms, because
11350 * non-availability of flip done interrupt forces us to use
11351 * CS flips. Older platforms derive flip done using some clever
11352 * tricks involving the flip_pending status bits and vblank irqs.
11353 * So using MMIO flips there would disrupt this mechanism.
11354 */
11355
11356 if (engine == NULL)
11357 return true;
11358
11359 if (INTEL_GEN(engine->i915) < 5)
11360 return false;
11361
11362 if (i915.use_mmio_flip < 0)
11363 return false;
11364 else if (i915.use_mmio_flip > 0)
11365 return true;
11366 else if (i915.enable_execlists)
11367 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011368
11369 resv = i915_gem_object_get_dmabuf_resv(obj);
11370 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011371 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011372
Chris Wilsond72d9082016-08-04 07:52:31 +010011373 return engine != i915_gem_active_get_engine(&obj->last_write,
11374 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011375}
11376
11377static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11378 unsigned int rotation,
11379 struct intel_flip_work *work)
11380{
11381 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011382 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011383 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11384 const enum pipe pipe = intel_crtc->pipe;
11385 u32 ctl, stride, tile_height;
11386
11387 ctl = I915_READ(PLANE_CTL(pipe, 0));
11388 ctl &= ~PLANE_CTL_TILED_MASK;
11389 switch (fb->modifier[0]) {
11390 case DRM_FORMAT_MOD_NONE:
11391 break;
11392 case I915_FORMAT_MOD_X_TILED:
11393 ctl |= PLANE_CTL_TILED_X;
11394 break;
11395 case I915_FORMAT_MOD_Y_TILED:
11396 ctl |= PLANE_CTL_TILED_Y;
11397 break;
11398 case I915_FORMAT_MOD_Yf_TILED:
11399 ctl |= PLANE_CTL_TILED_YF;
11400 break;
11401 default:
11402 MISSING_CASE(fb->modifier[0]);
11403 }
11404
11405 /*
11406 * The stride is either expressed as a multiple of 64 bytes chunks for
11407 * linear buffers or in number of tiles for tiled buffers.
11408 */
11409 if (intel_rotation_90_or_270(rotation)) {
11410 /* stride = Surface height in tiles */
11411 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11412 stride = DIV_ROUND_UP(fb->height, tile_height);
11413 } else {
11414 stride = fb->pitches[0] /
11415 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11416 fb->pixel_format);
11417 }
11418
11419 /*
11420 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11421 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11422 */
11423 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11424 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11425
11426 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11427 POSTING_READ(PLANE_SURF(pipe, 0));
11428}
11429
11430static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11431 struct intel_flip_work *work)
11432{
11433 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011434 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011435 struct intel_framebuffer *intel_fb =
11436 to_intel_framebuffer(intel_crtc->base.primary->fb);
11437 struct drm_i915_gem_object *obj = intel_fb->obj;
11438 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11439 u32 dspcntr;
11440
11441 dspcntr = I915_READ(reg);
11442
Chris Wilson3e510a82016-08-05 10:14:23 +010011443 if (i915_gem_object_is_tiled(obj))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011444 dspcntr |= DISPPLANE_TILED;
11445 else
11446 dspcntr &= ~DISPPLANE_TILED;
11447
11448 I915_WRITE(reg, dspcntr);
11449
11450 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11451 POSTING_READ(DSPSURF(intel_crtc->plane));
11452}
11453
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011454static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011455{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011456 struct intel_flip_work *work =
11457 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011458 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11459 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11460 struct intel_framebuffer *intel_fb =
11461 to_intel_framebuffer(crtc->base.primary->fb);
11462 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011463 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011464
11465 if (work->flip_queued_req)
Chris Wilson776f3232016-08-04 07:52:40 +010011466 WARN_ON(i915_wait_request(work->flip_queued_req,
11467 false, NULL,
11468 NO_WAITBOOST));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011469
11470 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011471 resv = i915_gem_object_get_dmabuf_resv(obj);
11472 if (resv)
11473 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011474 MAX_SCHEDULE_TIMEOUT) < 0);
11475
11476 intel_pipe_update_start(crtc);
11477
11478 if (INTEL_GEN(dev_priv) >= 9)
11479 skl_do_mmio_flip(crtc, work->rotation, work);
11480 else
11481 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11482 ilk_do_mmio_flip(crtc, work);
11483
11484 intel_pipe_update_end(crtc, work);
11485}
11486
11487static int intel_default_queue_flip(struct drm_device *dev,
11488 struct drm_crtc *crtc,
11489 struct drm_framebuffer *fb,
11490 struct drm_i915_gem_object *obj,
11491 struct drm_i915_gem_request *req,
11492 uint32_t flags)
11493{
11494 return -ENODEV;
11495}
11496
11497static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11498 struct intel_crtc *intel_crtc,
11499 struct intel_flip_work *work)
11500{
11501 u32 addr, vblank;
11502
11503 if (!atomic_read(&work->pending))
11504 return false;
11505
11506 smp_rmb();
11507
11508 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11509 if (work->flip_ready_vblank == 0) {
11510 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010011511 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011512 return false;
11513
11514 work->flip_ready_vblank = vblank;
11515 }
11516
11517 if (vblank - work->flip_ready_vblank < 3)
11518 return false;
11519
11520 /* Potential stall - if we see that the flip has happened,
11521 * assume a missed interrupt. */
11522 if (INTEL_GEN(dev_priv) >= 4)
11523 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11524 else
11525 addr = I915_READ(DSPADDR(intel_crtc->plane));
11526
11527 /* There is a potential issue here with a false positive after a flip
11528 * to the same address. We could address this by checking for a
11529 * non-incrementing frame counter.
11530 */
11531 return addr == work->gtt_offset;
11532}
11533
11534void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11535{
Chris Wilson91c8a322016-07-05 10:40:23 +010011536 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011537 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011539 struct intel_flip_work *work;
11540
11541 WARN_ON(!in_interrupt());
11542
11543 if (crtc == NULL)
11544 return;
11545
11546 spin_lock(&dev->event_lock);
11547 work = intel_crtc->flip_work;
11548
11549 if (work != NULL && !is_mmio_work(work) &&
11550 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11551 WARN_ONCE(1,
11552 "Kicking stuck page flip: queued at %d, now %d\n",
11553 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11554 page_flip_completed(intel_crtc);
11555 work = NULL;
11556 }
11557
11558 if (work != NULL && !is_mmio_work(work) &&
11559 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11560 intel_queue_rps_boost_for_request(work->flip_queued_req);
11561 spin_unlock(&dev->event_lock);
11562}
11563
11564static int intel_crtc_page_flip(struct drm_crtc *crtc,
11565 struct drm_framebuffer *fb,
11566 struct drm_pending_vblank_event *event,
11567 uint32_t page_flip_flags)
11568{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011569 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011570 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011571 struct drm_framebuffer *old_fb = crtc->primary->fb;
11572 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11574 struct drm_plane *primary = crtc->primary;
11575 enum pipe pipe = intel_crtc->pipe;
11576 struct intel_flip_work *work;
11577 struct intel_engine_cs *engine;
11578 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010011579 struct drm_i915_gem_request *request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011580 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011581
Daniel Vetter5a21b662016-05-24 17:13:53 +020011582 /*
11583 * drm_mode_page_flip_ioctl() should already catch this, but double
11584 * check to be safe. In the future we may enable pageflipping from
11585 * a disabled primary plane.
11586 */
11587 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11588 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011589
Daniel Vetter5a21b662016-05-24 17:13:53 +020011590 /* Can't change pixel format via MI display flips. */
11591 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11592 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011593
Daniel Vetter5a21b662016-05-24 17:13:53 +020011594 /*
11595 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11596 * Note that pitch changes could also affect these register.
11597 */
11598 if (INTEL_INFO(dev)->gen > 3 &&
11599 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11600 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11601 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011602
Daniel Vetter5a21b662016-05-24 17:13:53 +020011603 if (i915_terminally_wedged(&dev_priv->gpu_error))
11604 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011605
Daniel Vetter5a21b662016-05-24 17:13:53 +020011606 work = kzalloc(sizeof(*work), GFP_KERNEL);
11607 if (work == NULL)
11608 return -ENOMEM;
11609
11610 work->event = event;
11611 work->crtc = crtc;
11612 work->old_fb = old_fb;
11613 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011614
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011615 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011616 if (ret)
11617 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011618
Daniel Vetter5a21b662016-05-24 17:13:53 +020011619 /* We borrow the event spin lock for protecting flip_work */
11620 spin_lock_irq(&dev->event_lock);
11621 if (intel_crtc->flip_work) {
11622 /* Before declaring the flip queue wedged, check if
11623 * the hardware completed the operation behind our backs.
11624 */
11625 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11626 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11627 page_flip_completed(intel_crtc);
11628 } else {
11629 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11630 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011631
Daniel Vetter5a21b662016-05-24 17:13:53 +020011632 drm_crtc_vblank_put(crtc);
11633 kfree(work);
11634 return -EBUSY;
11635 }
11636 }
11637 intel_crtc->flip_work = work;
11638 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011639
Daniel Vetter5a21b662016-05-24 17:13:53 +020011640 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11641 flush_workqueue(dev_priv->wq);
11642
11643 /* Reference the objects for the scheduled work. */
11644 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011645
11646 crtc->primary->fb = fb;
11647 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011648
11649 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11650 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011651
Chris Wilson25dc5562016-07-20 13:31:52 +010011652 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011653
11654 ret = i915_mutex_lock_interruptible(dev);
11655 if (ret)
11656 goto cleanup;
11657
11658 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11659 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11660 ret = -EIO;
11661 goto cleanup;
11662 }
11663
11664 atomic_inc(&intel_crtc->unpin_work_count);
11665
11666 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11667 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11668
11669 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11670 engine = &dev_priv->engine[BCS];
Chris Wilson3e510a82016-08-05 10:14:23 +010011671 if (i915_gem_object_get_tiling(obj) !=
11672 i915_gem_object_get_tiling(intel_fb_obj(work->old_fb)))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011673 /* vlv: DISPLAY_FLIP fails to change tiling */
11674 engine = NULL;
11675 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11676 engine = &dev_priv->engine[BCS];
11677 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsond72d9082016-08-04 07:52:31 +010011678 engine = i915_gem_active_get_engine(&obj->last_write,
11679 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011680 if (engine == NULL || engine->id != RCS)
11681 engine = &dev_priv->engine[BCS];
11682 } else {
11683 engine = &dev_priv->engine[RCS];
11684 }
11685
11686 mmio_flip = use_mmio_flip(engine, obj);
11687
Daniel Vetter5a21b662016-05-24 17:13:53 +020011688 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11689 if (ret)
11690 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011691
Daniel Vetter5a21b662016-05-24 17:13:53 +020011692 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11693 obj, 0);
11694 work->gtt_offset += intel_crtc->dspaddr_offset;
11695 work->rotation = crtc->primary->state->rotation;
11696
11697 if (mmio_flip) {
11698 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11699
Chris Wilsond72d9082016-08-04 07:52:31 +010011700 work->flip_queued_req = i915_gem_active_get(&obj->last_write,
11701 &obj->base.dev->struct_mutex);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011702 schedule_work(&work->mmio_work);
11703 } else {
Chris Wilson8e637172016-08-02 22:50:26 +010011704 request = i915_gem_request_alloc(engine, engine->last_context);
11705 if (IS_ERR(request)) {
11706 ret = PTR_ERR(request);
11707 goto cleanup_unpin;
11708 }
11709
11710 ret = i915_gem_object_sync(obj, request);
11711 if (ret)
11712 goto cleanup_request;
11713
Daniel Vetter5a21b662016-05-24 17:13:53 +020011714 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11715 page_flip_flags);
11716 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010011717 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011718
11719 intel_mark_page_flip_active(intel_crtc, work);
11720
Chris Wilson8e637172016-08-02 22:50:26 +010011721 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011722 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011723 }
11724
Daniel Vetter5a21b662016-05-24 17:13:53 +020011725 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11726 to_intel_plane(primary)->frontbuffer_bit);
11727 mutex_unlock(&dev->struct_mutex);
11728
Chris Wilson5748b6a2016-08-04 16:32:38 +010011729 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020011730 to_intel_plane(primary)->frontbuffer_bit);
11731
11732 trace_i915_flip_request(intel_crtc->plane, obj);
11733
11734 return 0;
11735
Chris Wilson8e637172016-08-02 22:50:26 +010011736cleanup_request:
11737 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011738cleanup_unpin:
11739 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11740cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020011741 atomic_dec(&intel_crtc->unpin_work_count);
11742 mutex_unlock(&dev->struct_mutex);
11743cleanup:
11744 crtc->primary->fb = old_fb;
11745 update_state_fb(crtc->primary);
11746
Chris Wilson34911fd2016-07-20 13:31:54 +010011747 i915_gem_object_put_unlocked(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011748 drm_framebuffer_unreference(work->old_fb);
11749
11750 spin_lock_irq(&dev->event_lock);
11751 intel_crtc->flip_work = NULL;
11752 spin_unlock_irq(&dev->event_lock);
11753
11754 drm_crtc_vblank_put(crtc);
11755free_work:
11756 kfree(work);
11757
11758 if (ret == -EIO) {
11759 struct drm_atomic_state *state;
11760 struct drm_plane_state *plane_state;
11761
11762out_hang:
11763 state = drm_atomic_state_alloc(dev);
11764 if (!state)
11765 return -ENOMEM;
11766 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11767
11768retry:
11769 plane_state = drm_atomic_get_plane_state(state, primary);
11770 ret = PTR_ERR_OR_ZERO(plane_state);
11771 if (!ret) {
11772 drm_atomic_set_fb_for_plane(plane_state, fb);
11773
11774 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11775 if (!ret)
11776 ret = drm_atomic_commit(state);
11777 }
11778
11779 if (ret == -EDEADLK) {
11780 drm_modeset_backoff(state->acquire_ctx);
11781 drm_atomic_state_clear(state);
11782 goto retry;
11783 }
11784
11785 if (ret)
11786 drm_atomic_state_free(state);
11787
11788 if (ret == 0 && event) {
11789 spin_lock_irq(&dev->event_lock);
11790 drm_crtc_send_vblank_event(crtc, event);
11791 spin_unlock_irq(&dev->event_lock);
11792 }
11793 }
11794 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011795}
11796
Daniel Vetter5a21b662016-05-24 17:13:53 +020011797
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011798/**
11799 * intel_wm_need_update - Check whether watermarks need updating
11800 * @plane: drm plane
11801 * @state: new plane state
11802 *
11803 * Check current plane state versus the new one to determine whether
11804 * watermarks need to be recalculated.
11805 *
11806 * Returns true or false.
11807 */
11808static bool intel_wm_need_update(struct drm_plane *plane,
11809 struct drm_plane_state *state)
11810{
Matt Roperd21fbe82015-09-24 15:53:12 -070011811 struct intel_plane_state *new = to_intel_plane_state(state);
11812 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11813
11814 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011815 if (new->visible != cur->visible)
11816 return true;
11817
11818 if (!cur->base.fb || !new->base.fb)
11819 return false;
11820
11821 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11822 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011823 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11824 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11825 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11826 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011827 return true;
11828
11829 return false;
11830}
11831
Matt Roperd21fbe82015-09-24 15:53:12 -070011832static bool needs_scaling(struct intel_plane_state *state)
11833{
11834 int src_w = drm_rect_width(&state->src) >> 16;
11835 int src_h = drm_rect_height(&state->src) >> 16;
11836 int dst_w = drm_rect_width(&state->dst);
11837 int dst_h = drm_rect_height(&state->dst);
11838
11839 return (src_w != dst_w || src_h != dst_h);
11840}
11841
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011842int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11843 struct drm_plane_state *plane_state)
11844{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011845 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011846 struct drm_crtc *crtc = crtc_state->crtc;
11847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11848 struct drm_plane *plane = plane_state->plane;
11849 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011850 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011851 struct intel_plane_state *old_plane_state =
11852 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011853 bool mode_changed = needs_modeset(crtc_state);
11854 bool was_crtc_enabled = crtc->state->active;
11855 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011856 bool turn_off, turn_on, visible, was_visible;
11857 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011858 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011859
Chris Wilson84114992016-07-02 15:36:06 +010011860 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011861 ret = skl_update_scaler_plane(
11862 to_intel_crtc_state(crtc_state),
11863 to_intel_plane_state(plane_state));
11864 if (ret)
11865 return ret;
11866 }
11867
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011868 was_visible = old_plane_state->visible;
11869 visible = to_intel_plane_state(plane_state)->visible;
11870
11871 if (!was_crtc_enabled && WARN_ON(was_visible))
11872 was_visible = false;
11873
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011874 /*
11875 * Visibility is calculated as if the crtc was on, but
11876 * after scaler setup everything depends on it being off
11877 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011878 *
11879 * FIXME this is wrong for watermarks. Watermarks should also
11880 * be computed as if the pipe would be active. Perhaps move
11881 * per-plane wm computation to the .check_plane() hook, and
11882 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011883 */
11884 if (!is_crtc_enabled)
11885 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011886
11887 if (!was_visible && !visible)
11888 return 0;
11889
Maarten Lankhorste8861672016-02-24 11:24:26 +010011890 if (fb != old_plane_state->base.fb)
11891 pipe_config->fb_changed = true;
11892
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011893 turn_off = was_visible && (!visible || mode_changed);
11894 turn_on = visible && (!was_visible || mode_changed);
11895
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011896 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030011897 intel_crtc->base.base.id,
11898 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011899 plane->base.id, plane->name,
11900 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011901
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011902 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11903 plane->base.id, plane->name,
11904 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011905 turn_off, turn_on, mode_changed);
11906
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011907 if (turn_on) {
11908 pipe_config->update_wm_pre = true;
11909
11910 /* must disable cxsr around plane enable/disable */
11911 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11912 pipe_config->disable_cxsr = true;
11913 } else if (turn_off) {
11914 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011915
Ville Syrjälä852eb002015-06-24 22:00:07 +030011916 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011917 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011918 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011919 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011920 /* FIXME bollocks */
11921 pipe_config->update_wm_pre = true;
11922 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011923 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011924
Matt Ropered4a6a72016-02-23 17:20:13 -080011925 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011926 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11927 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011928 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11929
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011930 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011931 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011932
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011933 /*
11934 * WaCxSRDisabledForSpriteScaling:ivb
11935 *
11936 * cstate->update_wm was already set above, so this flag will
11937 * take effect when we commit and program watermarks.
11938 */
11939 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11940 needs_scaling(to_intel_plane_state(plane_state)) &&
11941 !needs_scaling(old_plane_state))
11942 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011943
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011944 return 0;
11945}
11946
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011947static bool encoders_cloneable(const struct intel_encoder *a,
11948 const struct intel_encoder *b)
11949{
11950 /* masks could be asymmetric, so check both ways */
11951 return a == b || (a->cloneable & (1 << b->type) &&
11952 b->cloneable & (1 << a->type));
11953}
11954
11955static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11956 struct intel_crtc *crtc,
11957 struct intel_encoder *encoder)
11958{
11959 struct intel_encoder *source_encoder;
11960 struct drm_connector *connector;
11961 struct drm_connector_state *connector_state;
11962 int i;
11963
11964 for_each_connector_in_state(state, connector, connector_state, i) {
11965 if (connector_state->crtc != &crtc->base)
11966 continue;
11967
11968 source_encoder =
11969 to_intel_encoder(connector_state->best_encoder);
11970 if (!encoders_cloneable(encoder, source_encoder))
11971 return false;
11972 }
11973
11974 return true;
11975}
11976
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011977static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11978 struct drm_crtc_state *crtc_state)
11979{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011980 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011981 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011983 struct intel_crtc_state *pipe_config =
11984 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011985 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011986 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011987 bool mode_changed = needs_modeset(crtc_state);
11988
Ville Syrjälä852eb002015-06-24 22:00:07 +030011989 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011990 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011991
Maarten Lankhorstad421372015-06-15 12:33:42 +020011992 if (mode_changed && crtc_state->enable &&
11993 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011994 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011995 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11996 pipe_config);
11997 if (ret)
11998 return ret;
11999 }
12000
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012001 if (crtc_state->color_mgmt_changed) {
12002 ret = intel_color_check(crtc, crtc_state);
12003 if (ret)
12004 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012005
12006 /*
12007 * Changing color management on Intel hardware is
12008 * handled as part of planes update.
12009 */
12010 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012011 }
12012
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012013 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012014 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012015 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012016 if (ret) {
12017 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012018 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012019 }
12020 }
12021
12022 if (dev_priv->display.compute_intermediate_wm &&
12023 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12024 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12025 return 0;
12026
12027 /*
12028 * Calculate 'intermediate' watermarks that satisfy both the
12029 * old state and the new state. We can program these
12030 * immediately.
12031 */
12032 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12033 intel_crtc,
12034 pipe_config);
12035 if (ret) {
12036 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12037 return ret;
12038 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012039 } else if (dev_priv->display.compute_intermediate_wm) {
12040 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12041 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012042 }
12043
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012044 if (INTEL_INFO(dev)->gen >= 9) {
12045 if (mode_changed)
12046 ret = skl_update_scaler_crtc(pipe_config);
12047
12048 if (!ret)
12049 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12050 pipe_config);
12051 }
12052
12053 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012054}
12055
Jani Nikula65b38e02015-04-13 11:26:56 +030012056static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012057 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012058 .atomic_begin = intel_begin_crtc_commit,
12059 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012060 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012061};
12062
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012063static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12064{
12065 struct intel_connector *connector;
12066
12067 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012068 if (connector->base.state->crtc)
12069 drm_connector_unreference(&connector->base);
12070
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012071 if (connector->base.encoder) {
12072 connector->base.state->best_encoder =
12073 connector->base.encoder;
12074 connector->base.state->crtc =
12075 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012076
12077 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012078 } else {
12079 connector->base.state->best_encoder = NULL;
12080 connector->base.state->crtc = NULL;
12081 }
12082 }
12083}
12084
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012085static void
Robin Schroereba905b2014-05-18 02:24:50 +020012086connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012087 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012088{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012089 int bpp = pipe_config->pipe_bpp;
12090
12091 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12092 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012093 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012094
12095 /* Don't use an invalid EDID bpc value */
12096 if (connector->base.display_info.bpc &&
12097 connector->base.display_info.bpc * 3 < bpp) {
12098 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12099 bpp, connector->base.display_info.bpc*3);
12100 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12101 }
12102
Jani Nikula013dd9e2016-01-13 16:35:20 +020012103 /* Clamp bpp to default limit on screens without EDID 1.4 */
12104 if (connector->base.display_info.bpc == 0) {
12105 int type = connector->base.connector_type;
12106 int clamp_bpp = 24;
12107
12108 /* Fall back to 18 bpp when DP sink capability is unknown. */
12109 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12110 type == DRM_MODE_CONNECTOR_eDP)
12111 clamp_bpp = 18;
12112
12113 if (bpp > clamp_bpp) {
12114 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12115 bpp, clamp_bpp);
12116 pipe_config->pipe_bpp = clamp_bpp;
12117 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012118 }
12119}
12120
12121static int
12122compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012123 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012124{
12125 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012126 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012127 struct drm_connector *connector;
12128 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012129 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012130
Wayne Boyer666a4532015-12-09 12:29:35 -080012131 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012132 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012133 else if (INTEL_INFO(dev)->gen >= 5)
12134 bpp = 12*3;
12135 else
12136 bpp = 8*3;
12137
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012138
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012139 pipe_config->pipe_bpp = bpp;
12140
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012141 state = pipe_config->base.state;
12142
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012143 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012144 for_each_connector_in_state(state, connector, connector_state, i) {
12145 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012146 continue;
12147
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012148 connected_sink_compute_bpp(to_intel_connector(connector),
12149 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012150 }
12151
12152 return bpp;
12153}
12154
Daniel Vetter644db712013-09-19 14:53:58 +020012155static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12156{
12157 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12158 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012159 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012160 mode->crtc_hdisplay, mode->crtc_hsync_start,
12161 mode->crtc_hsync_end, mode->crtc_htotal,
12162 mode->crtc_vdisplay, mode->crtc_vsync_start,
12163 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12164}
12165
Daniel Vetterc0b03412013-05-28 12:05:54 +020012166static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012167 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012168 const char *context)
12169{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012170 struct drm_device *dev = crtc->base.dev;
12171 struct drm_plane *plane;
12172 struct intel_plane *intel_plane;
12173 struct intel_plane_state *state;
12174 struct drm_framebuffer *fb;
12175
Ville Syrjälä78108b72016-05-27 20:59:19 +030012176 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12177 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012178 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012179
Jani Nikulada205632016-03-15 21:51:10 +020012180 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012181 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12182 pipe_config->pipe_bpp, pipe_config->dither);
12183 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12184 pipe_config->has_pch_encoder,
12185 pipe_config->fdi_lanes,
12186 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12187 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12188 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012189 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 +030012190 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012191 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012192 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12193 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12194 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012195
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012196 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 +030012197 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012198 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012199 pipe_config->dp_m2_n2.gmch_m,
12200 pipe_config->dp_m2_n2.gmch_n,
12201 pipe_config->dp_m2_n2.link_m,
12202 pipe_config->dp_m2_n2.link_n,
12203 pipe_config->dp_m2_n2.tu);
12204
Daniel Vetter55072d12014-11-20 16:10:28 +010012205 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12206 pipe_config->has_audio,
12207 pipe_config->has_infoframe);
12208
Daniel Vetterc0b03412013-05-28 12:05:54 +020012209 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012210 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012211 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012212 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12213 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012214 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012215 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12216 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012217 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12218 crtc->num_scalers,
12219 pipe_config->scaler_state.scaler_users,
12220 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012221 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12222 pipe_config->gmch_pfit.control,
12223 pipe_config->gmch_pfit.pgm_ratios,
12224 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012225 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012226 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012227 pipe_config->pch_pfit.size,
12228 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012229 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012230 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012231
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012232 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012233 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012234 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012235 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012236 pipe_config->ddi_pll_sel,
12237 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012238 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012239 pipe_config->dpll_hw_state.pll0,
12240 pipe_config->dpll_hw_state.pll1,
12241 pipe_config->dpll_hw_state.pll2,
12242 pipe_config->dpll_hw_state.pll3,
12243 pipe_config->dpll_hw_state.pll6,
12244 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012245 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012246 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012247 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012248 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012249 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12250 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12251 pipe_config->ddi_pll_sel,
12252 pipe_config->dpll_hw_state.ctrl1,
12253 pipe_config->dpll_hw_state.cfgcr1,
12254 pipe_config->dpll_hw_state.cfgcr2);
12255 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012256 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 +010012257 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012258 pipe_config->dpll_hw_state.wrpll,
12259 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012260 } else {
12261 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12262 "fp0: 0x%x, fp1: 0x%x\n",
12263 pipe_config->dpll_hw_state.dpll,
12264 pipe_config->dpll_hw_state.dpll_md,
12265 pipe_config->dpll_hw_state.fp0,
12266 pipe_config->dpll_hw_state.fp1);
12267 }
12268
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012269 DRM_DEBUG_KMS("planes on this crtc\n");
12270 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12271 intel_plane = to_intel_plane(plane);
12272 if (intel_plane->pipe != crtc->pipe)
12273 continue;
12274
12275 state = to_intel_plane_state(plane->state);
12276 fb = state->base.fb;
12277 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012278 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12279 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012280 continue;
12281 }
12282
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012283 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12284 plane->base.id, plane->name);
12285 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12286 fb->base.id, fb->width, fb->height,
12287 drm_get_format_name(fb->pixel_format));
12288 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12289 state->scaler_id,
12290 state->src.x1 >> 16, state->src.y1 >> 16,
12291 drm_rect_width(&state->src) >> 16,
12292 drm_rect_height(&state->src) >> 16,
12293 state->dst.x1, state->dst.y1,
12294 drm_rect_width(&state->dst),
12295 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012296 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012297}
12298
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012299static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012300{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012301 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012302 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012303 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012304 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012305
12306 /*
12307 * Walk the connector list instead of the encoder
12308 * list to detect the problem on ddi platforms
12309 * where there's just one encoder per digital port.
12310 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012311 drm_for_each_connector(connector, dev) {
12312 struct drm_connector_state *connector_state;
12313 struct intel_encoder *encoder;
12314
12315 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12316 if (!connector_state)
12317 connector_state = connector->state;
12318
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012319 if (!connector_state->best_encoder)
12320 continue;
12321
12322 encoder = to_intel_encoder(connector_state->best_encoder);
12323
12324 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012325
12326 switch (encoder->type) {
12327 unsigned int port_mask;
12328 case INTEL_OUTPUT_UNKNOWN:
12329 if (WARN_ON(!HAS_DDI(dev)))
12330 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012331 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012332 case INTEL_OUTPUT_HDMI:
12333 case INTEL_OUTPUT_EDP:
12334 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12335
12336 /* the same port mustn't appear more than once */
12337 if (used_ports & port_mask)
12338 return false;
12339
12340 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012341 break;
12342 case INTEL_OUTPUT_DP_MST:
12343 used_mst_ports |=
12344 1 << enc_to_mst(&encoder->base)->primary->port;
12345 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012346 default:
12347 break;
12348 }
12349 }
12350
Ville Syrjälä477321e2016-07-28 17:50:40 +030012351 /* can't mix MST and SST/HDMI on the same port */
12352 if (used_ports & used_mst_ports)
12353 return false;
12354
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012355 return true;
12356}
12357
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012358static void
12359clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12360{
12361 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012362 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012363 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012364 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012365 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012366 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012367
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012368 /* FIXME: before the switch to atomic started, a new pipe_config was
12369 * kzalloc'd. Code that depends on any field being zero should be
12370 * fixed, so that the crtc_state can be safely duplicated. For now,
12371 * only fields that are know to not cause problems are preserved. */
12372
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012373 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012374 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012375 shared_dpll = crtc_state->shared_dpll;
12376 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012377 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012378 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012379
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012380 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012381
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012382 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012383 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012384 crtc_state->shared_dpll = shared_dpll;
12385 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012386 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012387 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012388}
12389
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012390static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012391intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012392 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012393{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012394 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012395 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012396 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012397 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012398 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012399 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012400 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012401
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012402 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012403
Daniel Vettere143a212013-07-04 12:01:15 +020012404 pipe_config->cpu_transcoder =
12405 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012406
Imre Deak2960bc92013-07-30 13:36:32 +030012407 /*
12408 * Sanitize sync polarity flags based on requested ones. If neither
12409 * positive or negative polarity is requested, treat this as meaning
12410 * negative polarity.
12411 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012412 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012413 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012414 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012415
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012416 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012417 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012418 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012419
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012420 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12421 pipe_config);
12422 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012423 goto fail;
12424
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012425 /*
12426 * Determine the real pipe dimensions. Note that stereo modes can
12427 * increase the actual pipe size due to the frame doubling and
12428 * insertion of additional space for blanks between the frame. This
12429 * is stored in the crtc timings. We use the requested mode to do this
12430 * computation to clearly distinguish it from the adjusted mode, which
12431 * can be changed by the connectors in the below retry loop.
12432 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012433 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012434 &pipe_config->pipe_src_w,
12435 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012436
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012437 for_each_connector_in_state(state, connector, connector_state, i) {
12438 if (connector_state->crtc != crtc)
12439 continue;
12440
12441 encoder = to_intel_encoder(connector_state->best_encoder);
12442
Ville Syrjäläe25148d2016-06-22 21:57:09 +030012443 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12444 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12445 goto fail;
12446 }
12447
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012448 /*
12449 * Determine output_types before calling the .compute_config()
12450 * hooks so that the hooks can use this information safely.
12451 */
12452 pipe_config->output_types |= 1 << encoder->type;
12453 }
12454
Daniel Vettere29c22c2013-02-21 00:00:16 +010012455encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012456 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012457 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012458 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012459
Daniel Vetter135c81b2013-07-21 21:37:09 +020012460 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012461 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12462 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012463
Daniel Vetter7758a112012-07-08 19:40:39 +020012464 /* Pass our mode to the connectors and the CRTC to give them a chance to
12465 * adjust it according to limitations or connector properties, and also
12466 * a chance to reject the mode entirely.
12467 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012468 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012469 if (connector_state->crtc != crtc)
12470 continue;
12471
12472 encoder = to_intel_encoder(connector_state->best_encoder);
12473
Daniel Vetterefea6e82013-07-21 21:36:59 +020012474 if (!(encoder->compute_config(encoder, pipe_config))) {
12475 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012476 goto fail;
12477 }
12478 }
12479
Daniel Vetterff9a6752013-06-01 17:16:21 +020012480 /* Set default port clock if not overwritten by the encoder. Needs to be
12481 * done afterwards in case the encoder adjusts the mode. */
12482 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012483 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012484 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012485
Daniel Vettera43f6e02013-06-07 23:10:32 +020012486 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012487 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012488 DRM_DEBUG_KMS("CRTC fixup failed\n");
12489 goto fail;
12490 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012491
12492 if (ret == RETRY) {
12493 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12494 ret = -EINVAL;
12495 goto fail;
12496 }
12497
12498 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12499 retry = false;
12500 goto encoder_retry;
12501 }
12502
Daniel Vettere8fa4272015-08-12 11:43:34 +020012503 /* Dithering seems to not pass-through bits correctly when it should, so
12504 * only enable it on 6bpc panels. */
12505 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012506 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012507 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012508
Daniel Vetter7758a112012-07-08 19:40:39 +020012509fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012510 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012511}
12512
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012513static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012514intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012515{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012516 struct drm_crtc *crtc;
12517 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012518 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012519
Ville Syrjälä76688512014-01-10 11:28:06 +020012520 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012521 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012522 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012523
12524 /* Update hwmode for vblank functions */
12525 if (crtc->state->active)
12526 crtc->hwmode = crtc->state->adjusted_mode;
12527 else
12528 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012529
12530 /*
12531 * Update legacy state to satisfy fbc code. This can
12532 * be removed when fbc uses the atomic state.
12533 */
12534 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12535 struct drm_plane_state *plane_state = crtc->primary->state;
12536
12537 crtc->primary->fb = plane_state->fb;
12538 crtc->x = plane_state->src_x >> 16;
12539 crtc->y = plane_state->src_y >> 16;
12540 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012541 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012542}
12543
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012544static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012545{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012546 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012547
12548 if (clock1 == clock2)
12549 return true;
12550
12551 if (!clock1 || !clock2)
12552 return false;
12553
12554 diff = abs(clock1 - clock2);
12555
12556 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12557 return true;
12558
12559 return false;
12560}
12561
Daniel Vetter25c5b262012-07-08 22:08:04 +020012562#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12563 list_for_each_entry((intel_crtc), \
12564 &(dev)->mode_config.crtc_list, \
12565 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012566 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012567
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012568static bool
12569intel_compare_m_n(unsigned int m, unsigned int n,
12570 unsigned int m2, unsigned int n2,
12571 bool exact)
12572{
12573 if (m == m2 && n == n2)
12574 return true;
12575
12576 if (exact || !m || !n || !m2 || !n2)
12577 return false;
12578
12579 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12580
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012581 if (n > n2) {
12582 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012583 m2 <<= 1;
12584 n2 <<= 1;
12585 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012586 } else if (n < n2) {
12587 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012588 m <<= 1;
12589 n <<= 1;
12590 }
12591 }
12592
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012593 if (n != n2)
12594 return false;
12595
12596 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012597}
12598
12599static bool
12600intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12601 struct intel_link_m_n *m2_n2,
12602 bool adjust)
12603{
12604 if (m_n->tu == m2_n2->tu &&
12605 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12606 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12607 intel_compare_m_n(m_n->link_m, m_n->link_n,
12608 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12609 if (adjust)
12610 *m2_n2 = *m_n;
12611
12612 return true;
12613 }
12614
12615 return false;
12616}
12617
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012618static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012619intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012620 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012621 struct intel_crtc_state *pipe_config,
12622 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012623{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012624 bool ret = true;
12625
12626#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12627 do { \
12628 if (!adjust) \
12629 DRM_ERROR(fmt, ##__VA_ARGS__); \
12630 else \
12631 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12632 } while (0)
12633
Daniel Vetter66e985c2013-06-05 13:34:20 +020012634#define PIPE_CONF_CHECK_X(name) \
12635 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012636 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012637 "(expected 0x%08x, found 0x%08x)\n", \
12638 current_config->name, \
12639 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012640 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012641 }
12642
Daniel Vetter08a24032013-04-19 11:25:34 +020012643#define PIPE_CONF_CHECK_I(name) \
12644 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012645 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012646 "(expected %i, found %i)\n", \
12647 current_config->name, \
12648 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012649 ret = false; \
12650 }
12651
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012652#define PIPE_CONF_CHECK_P(name) \
12653 if (current_config->name != pipe_config->name) { \
12654 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12655 "(expected %p, found %p)\n", \
12656 current_config->name, \
12657 pipe_config->name); \
12658 ret = false; \
12659 }
12660
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012661#define PIPE_CONF_CHECK_M_N(name) \
12662 if (!intel_compare_link_m_n(&current_config->name, \
12663 &pipe_config->name,\
12664 adjust)) { \
12665 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12666 "(expected tu %i gmch %i/%i link %i/%i, " \
12667 "found tu %i, gmch %i/%i link %i/%i)\n", \
12668 current_config->name.tu, \
12669 current_config->name.gmch_m, \
12670 current_config->name.gmch_n, \
12671 current_config->name.link_m, \
12672 current_config->name.link_n, \
12673 pipe_config->name.tu, \
12674 pipe_config->name.gmch_m, \
12675 pipe_config->name.gmch_n, \
12676 pipe_config->name.link_m, \
12677 pipe_config->name.link_n); \
12678 ret = false; \
12679 }
12680
Daniel Vetter55c561a2016-03-30 11:34:36 +020012681/* This is required for BDW+ where there is only one set of registers for
12682 * switching between high and low RR.
12683 * This macro can be used whenever a comparison has to be made between one
12684 * hw state and multiple sw state variables.
12685 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012686#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12687 if (!intel_compare_link_m_n(&current_config->name, \
12688 &pipe_config->name, adjust) && \
12689 !intel_compare_link_m_n(&current_config->alt_name, \
12690 &pipe_config->name, adjust)) { \
12691 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12692 "(expected tu %i gmch %i/%i link %i/%i, " \
12693 "or tu %i gmch %i/%i link %i/%i, " \
12694 "found tu %i, gmch %i/%i link %i/%i)\n", \
12695 current_config->name.tu, \
12696 current_config->name.gmch_m, \
12697 current_config->name.gmch_n, \
12698 current_config->name.link_m, \
12699 current_config->name.link_n, \
12700 current_config->alt_name.tu, \
12701 current_config->alt_name.gmch_m, \
12702 current_config->alt_name.gmch_n, \
12703 current_config->alt_name.link_m, \
12704 current_config->alt_name.link_n, \
12705 pipe_config->name.tu, \
12706 pipe_config->name.gmch_m, \
12707 pipe_config->name.gmch_n, \
12708 pipe_config->name.link_m, \
12709 pipe_config->name.link_n); \
12710 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012711 }
12712
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012713#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12714 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012715 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012716 "(expected %i, found %i)\n", \
12717 current_config->name & (mask), \
12718 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012719 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012720 }
12721
Ville Syrjälä5e550652013-09-06 23:29:07 +030012722#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12723 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012724 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012725 "(expected %i, found %i)\n", \
12726 current_config->name, \
12727 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012728 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012729 }
12730
Daniel Vetterbb760062013-06-06 14:55:52 +020012731#define PIPE_CONF_QUIRK(quirk) \
12732 ((current_config->quirks | pipe_config->quirks) & (quirk))
12733
Daniel Vettereccb1402013-05-22 00:50:22 +020012734 PIPE_CONF_CHECK_I(cpu_transcoder);
12735
Daniel Vetter08a24032013-04-19 11:25:34 +020012736 PIPE_CONF_CHECK_I(has_pch_encoder);
12737 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012738 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012739
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012740 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012741 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012742
12743 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012744 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012745
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012746 if (current_config->has_drrs)
12747 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12748 } else
12749 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012750
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012751 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020012752
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012753 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12754 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12755 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12756 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12757 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12758 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012759
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012760 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12761 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12762 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12763 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12764 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12765 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012766
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012767 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012768 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012769 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012770 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012771 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012772 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012773
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012774 PIPE_CONF_CHECK_I(has_audio);
12775
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012776 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012777 DRM_MODE_FLAG_INTERLACE);
12778
Daniel Vetterbb760062013-06-06 14:55:52 +020012779 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012780 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012781 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012782 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012783 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012784 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012785 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012786 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012787 DRM_MODE_FLAG_NVSYNC);
12788 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012789
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012790 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012791 /* pfit ratios are autocomputed by the hw on gen4+ */
12792 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012793 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012794 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012795
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012796 if (!adjust) {
12797 PIPE_CONF_CHECK_I(pipe_src_w);
12798 PIPE_CONF_CHECK_I(pipe_src_h);
12799
12800 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12801 if (current_config->pch_pfit.enabled) {
12802 PIPE_CONF_CHECK_X(pch_pfit.pos);
12803 PIPE_CONF_CHECK_X(pch_pfit.size);
12804 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012805
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012806 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12807 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012808
Jesse Barnese59150d2014-01-07 13:30:45 -080012809 /* BDW+ don't expose a synchronous way to read the state */
12810 if (IS_HASWELL(dev))
12811 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012812
Ville Syrjälä282740f2013-09-04 18:30:03 +030012813 PIPE_CONF_CHECK_I(double_wide);
12814
Daniel Vetter26804af2014-06-25 22:01:55 +030012815 PIPE_CONF_CHECK_X(ddi_pll_sel);
12816
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012817 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012818 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012819 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012820 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12821 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012822 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012823 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012824 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12825 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12826 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012827
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012828 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12829 PIPE_CONF_CHECK_X(dsi_pll.div);
12830
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012831 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12832 PIPE_CONF_CHECK_I(pipe_bpp);
12833
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012834 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012835 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012836
Daniel Vetter66e985c2013-06-05 13:34:20 +020012837#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012838#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012839#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012840#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012841#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012842#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012843#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012844
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012845 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012846}
12847
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012848static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12849 const struct intel_crtc_state *pipe_config)
12850{
12851 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012852 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012853 &pipe_config->fdi_m_n);
12854 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12855
12856 /*
12857 * FDI already provided one idea for the dotclock.
12858 * Yell if the encoder disagrees.
12859 */
12860 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12861 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12862 fdi_dotclock, dotclock);
12863 }
12864}
12865
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012866static void verify_wm_state(struct drm_crtc *crtc,
12867 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012868{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012869 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012870 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000012871 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012872 struct skl_ddb_entry *hw_entry, *sw_entry;
12873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12874 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012875 int plane;
12876
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012877 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012878 return;
12879
12880 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12881 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12882
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012883 /* planes */
12884 for_each_plane(dev_priv, pipe, plane) {
12885 hw_entry = &hw_ddb.plane[pipe][plane];
12886 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012887
12888 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12889 continue;
12890
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012891 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12892 "(expected (%u,%u), found (%u,%u))\n",
12893 pipe_name(pipe), plane + 1,
12894 sw_entry->start, sw_entry->end,
12895 hw_entry->start, hw_entry->end);
12896 }
12897
12898 /* cursor */
12899 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12900 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12901
12902 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012903 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12904 "(expected (%u,%u), found (%u,%u))\n",
12905 pipe_name(pipe),
12906 sw_entry->start, sw_entry->end,
12907 hw_entry->start, hw_entry->end);
12908 }
12909}
12910
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012911static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012912verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012913{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012914 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012915
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012916 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012917 struct drm_encoder *encoder = connector->encoder;
12918 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012919
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012920 if (state->crtc != crtc)
12921 continue;
12922
Daniel Vetter5a21b662016-05-24 17:13:53 +020012923 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012924
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012925 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012926 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012927 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012928}
12929
12930static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012931verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012932{
12933 struct intel_encoder *encoder;
12934 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012935
Damien Lespiaub2784e12014-08-05 11:29:37 +010012936 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012937 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012938 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012939
12940 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12941 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012942 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012943
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012944 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012945 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012946 continue;
12947 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012948
12949 I915_STATE_WARN(connector->base.state->crtc !=
12950 encoder->base.crtc,
12951 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012952 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012953
Rob Clarke2c719b2014-12-15 13:56:32 -050012954 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012955 "encoder's enabled state mismatch "
12956 "(expected %i, found %i)\n",
12957 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012958
12959 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012960 bool active;
12961
12962 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012963 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012964 "encoder detached but still enabled on pipe %c.\n",
12965 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012966 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012967 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012968}
12969
12970static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012971verify_crtc_state(struct drm_crtc *crtc,
12972 struct drm_crtc_state *old_crtc_state,
12973 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012974{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012975 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012976 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012977 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12979 struct intel_crtc_state *pipe_config, *sw_config;
12980 struct drm_atomic_state *old_state;
12981 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012982
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012983 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020012984 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012985 pipe_config = to_intel_crtc_state(old_crtc_state);
12986 memset(pipe_config, 0, sizeof(*pipe_config));
12987 pipe_config->base.crtc = crtc;
12988 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012989
Ville Syrjälä78108b72016-05-27 20:59:19 +030012990 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012991
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012992 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012993
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012994 /* hw state is inconsistent with the pipe quirk */
12995 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12996 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12997 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012998
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012999 I915_STATE_WARN(new_crtc_state->active != active,
13000 "crtc active state doesn't match with hw state "
13001 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013002
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013003 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13004 "transitional active state does not match atomic hw state "
13005 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013006
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013007 for_each_encoder_on_crtc(dev, crtc, encoder) {
13008 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013009
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013010 active = encoder->get_hw_state(encoder, &pipe);
13011 I915_STATE_WARN(active != new_crtc_state->active,
13012 "[ENCODER:%i] active %i with crtc active %i\n",
13013 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013014
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013015 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13016 "Encoder connected to wrong pipe %c\n",
13017 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013018
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013019 if (active) {
13020 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013021 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013022 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013023 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013024
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013025 if (!new_crtc_state->active)
13026 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013027
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013028 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013029
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013030 sw_config = to_intel_crtc_state(crtc->state);
13031 if (!intel_pipe_config_compare(dev, sw_config,
13032 pipe_config, false)) {
13033 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13034 intel_dump_pipe_config(intel_crtc, pipe_config,
13035 "[hw state]");
13036 intel_dump_pipe_config(intel_crtc, sw_config,
13037 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013038 }
13039}
13040
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013041static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013042verify_single_dpll_state(struct drm_i915_private *dev_priv,
13043 struct intel_shared_dpll *pll,
13044 struct drm_crtc *crtc,
13045 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013046{
13047 struct intel_dpll_hw_state dpll_hw_state;
13048 unsigned crtc_mask;
13049 bool active;
13050
13051 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13052
13053 DRM_DEBUG_KMS("%s\n", pll->name);
13054
13055 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13056
13057 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13058 I915_STATE_WARN(!pll->on && pll->active_mask,
13059 "pll in active use but not on in sw tracking\n");
13060 I915_STATE_WARN(pll->on && !pll->active_mask,
13061 "pll is on but not used by any active crtc\n");
13062 I915_STATE_WARN(pll->on != active,
13063 "pll on state mismatch (expected %i, found %i)\n",
13064 pll->on, active);
13065 }
13066
13067 if (!crtc) {
13068 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13069 "more active pll users than references: %x vs %x\n",
13070 pll->active_mask, pll->config.crtc_mask);
13071
13072 return;
13073 }
13074
13075 crtc_mask = 1 << drm_crtc_index(crtc);
13076
13077 if (new_state->active)
13078 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13079 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13080 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13081 else
13082 I915_STATE_WARN(pll->active_mask & crtc_mask,
13083 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13084 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13085
13086 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13087 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13088 crtc_mask, pll->config.crtc_mask);
13089
13090 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13091 &dpll_hw_state,
13092 sizeof(dpll_hw_state)),
13093 "pll hw state mismatch\n");
13094}
13095
13096static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013097verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13098 struct drm_crtc_state *old_crtc_state,
13099 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013100{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013101 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013102 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13103 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13104
13105 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013106 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013107
13108 if (old_state->shared_dpll &&
13109 old_state->shared_dpll != new_state->shared_dpll) {
13110 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13111 struct intel_shared_dpll *pll = old_state->shared_dpll;
13112
13113 I915_STATE_WARN(pll->active_mask & crtc_mask,
13114 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13115 pipe_name(drm_crtc_index(crtc)));
13116 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13117 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13118 pipe_name(drm_crtc_index(crtc)));
13119 }
13120}
13121
13122static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013123intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013124 struct drm_crtc_state *old_state,
13125 struct drm_crtc_state *new_state)
13126{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013127 if (!needs_modeset(new_state) &&
13128 !to_intel_crtc_state(new_state)->update_pipe)
13129 return;
13130
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013131 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013132 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013133 verify_crtc_state(crtc, old_state, new_state);
13134 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013135}
13136
13137static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013138verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013139{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013140 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013141 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013142
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013143 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013144 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013145}
Daniel Vetter53589012013-06-05 13:34:16 +020013146
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013147static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013148intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013149{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013150 verify_encoder_state(dev);
13151 verify_connector_state(dev, NULL);
13152 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013153}
13154
Ville Syrjälä80715b22014-05-15 20:23:23 +030013155static void update_scanline_offset(struct intel_crtc *crtc)
13156{
13157 struct drm_device *dev = crtc->base.dev;
13158
13159 /*
13160 * The scanline counter increments at the leading edge of hsync.
13161 *
13162 * On most platforms it starts counting from vtotal-1 on the
13163 * first active line. That means the scanline counter value is
13164 * always one less than what we would expect. Ie. just after
13165 * start of vblank, which also occurs at start of hsync (on the
13166 * last active line), the scanline counter will read vblank_start-1.
13167 *
13168 * On gen2 the scanline counter starts counting from 1 instead
13169 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13170 * to keep the value positive), instead of adding one.
13171 *
13172 * On HSW+ the behaviour of the scanline counter depends on the output
13173 * type. For DP ports it behaves like most other platforms, but on HDMI
13174 * there's an extra 1 line difference. So we need to add two instead of
13175 * one to the value.
13176 */
13177 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013178 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013179 int vtotal;
13180
Ville Syrjälä124abe02015-09-08 13:40:45 +030013181 vtotal = adjusted_mode->crtc_vtotal;
13182 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013183 vtotal /= 2;
13184
13185 crtc->scanline_offset = vtotal - 1;
13186 } else if (HAS_DDI(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013187 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013188 crtc->scanline_offset = 2;
13189 } else
13190 crtc->scanline_offset = 1;
13191}
13192
Maarten Lankhorstad421372015-06-15 12:33:42 +020013193static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013194{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013195 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013196 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013197 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013198 struct drm_crtc *crtc;
13199 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013200 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013201
13202 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013203 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013204
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013205 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013207 struct intel_shared_dpll *old_dpll =
13208 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013209
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013210 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013211 continue;
13212
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013213 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013214
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013215 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013216 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013217
Maarten Lankhorstad421372015-06-15 12:33:42 +020013218 if (!shared_dpll)
13219 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13220
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013221 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013222 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013223}
13224
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013225/*
13226 * This implements the workaround described in the "notes" section of the mode
13227 * set sequence documentation. When going from no pipes or single pipe to
13228 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13229 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13230 */
13231static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13232{
13233 struct drm_crtc_state *crtc_state;
13234 struct intel_crtc *intel_crtc;
13235 struct drm_crtc *crtc;
13236 struct intel_crtc_state *first_crtc_state = NULL;
13237 struct intel_crtc_state *other_crtc_state = NULL;
13238 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13239 int i;
13240
13241 /* look at all crtc's that are going to be enabled in during modeset */
13242 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13243 intel_crtc = to_intel_crtc(crtc);
13244
13245 if (!crtc_state->active || !needs_modeset(crtc_state))
13246 continue;
13247
13248 if (first_crtc_state) {
13249 other_crtc_state = to_intel_crtc_state(crtc_state);
13250 break;
13251 } else {
13252 first_crtc_state = to_intel_crtc_state(crtc_state);
13253 first_pipe = intel_crtc->pipe;
13254 }
13255 }
13256
13257 /* No workaround needed? */
13258 if (!first_crtc_state)
13259 return 0;
13260
13261 /* w/a possibly needed, check how many crtc's are already enabled. */
13262 for_each_intel_crtc(state->dev, intel_crtc) {
13263 struct intel_crtc_state *pipe_config;
13264
13265 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13266 if (IS_ERR(pipe_config))
13267 return PTR_ERR(pipe_config);
13268
13269 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13270
13271 if (!pipe_config->base.active ||
13272 needs_modeset(&pipe_config->base))
13273 continue;
13274
13275 /* 2 or more enabled crtcs means no need for w/a */
13276 if (enabled_pipe != INVALID_PIPE)
13277 return 0;
13278
13279 enabled_pipe = intel_crtc->pipe;
13280 }
13281
13282 if (enabled_pipe != INVALID_PIPE)
13283 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13284 else if (other_crtc_state)
13285 other_crtc_state->hsw_workaround_pipe = first_pipe;
13286
13287 return 0;
13288}
13289
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013290static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13291{
13292 struct drm_crtc *crtc;
13293 struct drm_crtc_state *crtc_state;
13294 int ret = 0;
13295
13296 /* add all active pipes to the state */
13297 for_each_crtc(state->dev, crtc) {
13298 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13299 if (IS_ERR(crtc_state))
13300 return PTR_ERR(crtc_state);
13301
13302 if (!crtc_state->active || needs_modeset(crtc_state))
13303 continue;
13304
13305 crtc_state->mode_changed = true;
13306
13307 ret = drm_atomic_add_affected_connectors(state, crtc);
13308 if (ret)
13309 break;
13310
13311 ret = drm_atomic_add_affected_planes(state, crtc);
13312 if (ret)
13313 break;
13314 }
13315
13316 return ret;
13317}
13318
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013319static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013320{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013321 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013322 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013323 struct drm_crtc *crtc;
13324 struct drm_crtc_state *crtc_state;
13325 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013326
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013327 if (!check_digital_port_conflicts(state)) {
13328 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13329 return -EINVAL;
13330 }
13331
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013332 intel_state->modeset = true;
13333 intel_state->active_crtcs = dev_priv->active_crtcs;
13334
13335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13336 if (crtc_state->active)
13337 intel_state->active_crtcs |= 1 << i;
13338 else
13339 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013340
13341 if (crtc_state->active != crtc->state->active)
13342 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013343 }
13344
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013345 /*
13346 * See if the config requires any additional preparation, e.g.
13347 * to adjust global state with pipes off. We need to do this
13348 * here so we can get the modeset_pipe updated config for the new
13349 * mode set on this crtc. For other crtcs we need to use the
13350 * adjusted_mode bits in the crtc directly.
13351 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013352 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013353 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013354 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013355 if (!intel_state->cdclk_pll_vco)
13356 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013357
Clint Taylorc89e39f2016-05-13 23:41:21 +030013358 ret = dev_priv->display.modeset_calc_cdclk(state);
13359 if (ret < 0)
13360 return ret;
13361
13362 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013363 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013364 ret = intel_modeset_all_pipes(state);
13365
13366 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013367 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013368
13369 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13370 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013371 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013372 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013373
Maarten Lankhorstad421372015-06-15 12:33:42 +020013374 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013375
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013376 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013377 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013378
Maarten Lankhorstad421372015-06-15 12:33:42 +020013379 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013380}
13381
Matt Roperaa363132015-09-24 15:53:18 -070013382/*
13383 * Handle calculation of various watermark data at the end of the atomic check
13384 * phase. The code here should be run after the per-crtc and per-plane 'check'
13385 * handlers to ensure that all derived state has been updated.
13386 */
Matt Roper55994c22016-05-12 07:06:08 -070013387static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013388{
13389 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013390 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013391
13392 /* Is there platform-specific watermark information to calculate? */
13393 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013394 return dev_priv->display.compute_global_watermarks(state);
13395
13396 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013397}
13398
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013399/**
13400 * intel_atomic_check - validate state object
13401 * @dev: drm device
13402 * @state: state to validate
13403 */
13404static int intel_atomic_check(struct drm_device *dev,
13405 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013406{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013407 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013408 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013409 struct drm_crtc *crtc;
13410 struct drm_crtc_state *crtc_state;
13411 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013412 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013413
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013414 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013415 if (ret)
13416 return ret;
13417
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013418 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013419 struct intel_crtc_state *pipe_config =
13420 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013421
13422 /* Catch I915_MODE_FLAG_INHERITED */
13423 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13424 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013425
Daniel Vetter26495482015-07-15 14:15:52 +020013426 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013427 continue;
13428
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013429 if (!crtc_state->enable) {
13430 any_ms = true;
13431 continue;
13432 }
13433
Daniel Vetter26495482015-07-15 14:15:52 +020013434 /* FIXME: For only active_changed we shouldn't need to do any
13435 * state recomputation at all. */
13436
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013437 ret = drm_atomic_add_affected_connectors(state, crtc);
13438 if (ret)
13439 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013440
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013441 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013442 if (ret) {
13443 intel_dump_pipe_config(to_intel_crtc(crtc),
13444 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013445 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013446 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013447
Jani Nikula73831232015-11-19 10:26:30 +020013448 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013449 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013450 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013451 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013452 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013453 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013454 }
13455
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013456 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013457 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013458
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013459 ret = drm_atomic_add_affected_planes(state, crtc);
13460 if (ret)
13461 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013462
Daniel Vetter26495482015-07-15 14:15:52 +020013463 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13464 needs_modeset(crtc_state) ?
13465 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013466 }
13467
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013468 if (any_ms) {
13469 ret = intel_modeset_checks(state);
13470
13471 if (ret)
13472 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013473 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013474 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013475
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013476 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013477 if (ret)
13478 return ret;
13479
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013480 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013481 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013482}
13483
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013484static int intel_atomic_prepare_commit(struct drm_device *dev,
13485 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013486 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013487{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013488 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013489 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013490 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013491 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013492 struct drm_crtc *crtc;
13493 int i, ret;
13494
Daniel Vetter5a21b662016-05-24 17:13:53 +020013495 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13496 if (state->legacy_cursor_update)
13497 continue;
13498
13499 ret = intel_crtc_wait_for_pending_flips(crtc);
13500 if (ret)
13501 return ret;
13502
13503 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13504 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013505 }
13506
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013507 ret = mutex_lock_interruptible(&dev->struct_mutex);
13508 if (ret)
13509 return ret;
13510
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013511 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013512 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013513
Dave Airlie21daaee2016-05-05 09:56:30 +100013514 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013515 for_each_plane_in_state(state, plane, plane_state, i) {
13516 struct intel_plane_state *intel_plane_state =
13517 to_intel_plane_state(plane_state);
13518
13519 if (!intel_plane_state->wait_req)
13520 continue;
13521
Chris Wilson776f3232016-08-04 07:52:40 +010013522 ret = i915_wait_request(intel_plane_state->wait_req,
13523 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013524 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013525 /* Any hang should be swallowed by the wait */
13526 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013527 mutex_lock(&dev->struct_mutex);
13528 drm_atomic_helper_cleanup_planes(dev, state);
13529 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013530 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013531 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013532 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013533 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013534
13535 return ret;
13536}
13537
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013538u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13539{
13540 struct drm_device *dev = crtc->base.dev;
13541
13542 if (!dev->max_vblank_count)
13543 return drm_accurate_vblank_count(&crtc->base);
13544
13545 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13546}
13547
Daniel Vetter5a21b662016-05-24 17:13:53 +020013548static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13549 struct drm_i915_private *dev_priv,
13550 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013551{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013552 unsigned last_vblank_count[I915_MAX_PIPES];
13553 enum pipe pipe;
13554 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013555
Daniel Vetter5a21b662016-05-24 17:13:53 +020013556 if (!crtc_mask)
13557 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013558
Daniel Vetter5a21b662016-05-24 17:13:53 +020013559 for_each_pipe(dev_priv, pipe) {
13560 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013561
Daniel Vetter5a21b662016-05-24 17:13:53 +020013562 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013563 continue;
13564
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013565 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013566 if (WARN_ON(ret != 0)) {
13567 crtc_mask &= ~(1 << pipe);
13568 continue;
13569 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013570
Daniel Vetter5a21b662016-05-24 17:13:53 +020013571 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13572 }
13573
13574 for_each_pipe(dev_priv, pipe) {
13575 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13576 long lret;
13577
13578 if (!((1 << pipe) & crtc_mask))
13579 continue;
13580
13581 lret = wait_event_timeout(dev->vblank[pipe].queue,
13582 last_vblank_count[pipe] !=
13583 drm_crtc_vblank_count(crtc),
13584 msecs_to_jiffies(50));
13585
13586 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13587
13588 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013589 }
13590}
13591
Daniel Vetter5a21b662016-05-24 17:13:53 +020013592static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013593{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013594 /* fb updated, need to unpin old fb */
13595 if (crtc_state->fb_changed)
13596 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013597
Daniel Vetter5a21b662016-05-24 17:13:53 +020013598 /* wm changes, need vblank before final wm's */
13599 if (crtc_state->update_wm_post)
13600 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013601
Daniel Vetter5a21b662016-05-24 17:13:53 +020013602 /*
13603 * cxsr is re-enabled after vblank.
13604 * This is already handled by crtc_state->update_wm_post,
13605 * but added for clarity.
13606 */
13607 if (crtc_state->disable_cxsr)
13608 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013609
Daniel Vetter5a21b662016-05-24 17:13:53 +020013610 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013611}
13612
Daniel Vetter94f05022016-06-14 18:01:00 +020013613static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013614{
Daniel Vetter94f05022016-06-14 18:01:00 +020013615 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013616 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013617 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013618 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013619 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013620 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013621 struct drm_plane *plane;
13622 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013623 bool hw_check = intel_state->modeset;
13624 unsigned long put_domains[I915_MAX_PIPES] = {};
13625 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013626 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013627
Daniel Vetter94f05022016-06-14 18:01:00 +020013628 for_each_plane_in_state(state, plane, plane_state, i) {
13629 struct intel_plane_state *intel_plane_state =
13630 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013631
Daniel Vetter94f05022016-06-14 18:01:00 +020013632 if (!intel_plane_state->wait_req)
13633 continue;
13634
Chris Wilson776f3232016-08-04 07:52:40 +010013635 ret = i915_wait_request(intel_plane_state->wait_req,
13636 true, NULL, NULL);
Daniel Vetter94f05022016-06-14 18:01:00 +020013637 /* EIO should be eaten, and we can't get interrupted in the
13638 * worker, and blocking commits have waited already. */
13639 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013640 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013641
Daniel Vetterea0000f2016-06-13 16:13:46 +020013642 drm_atomic_helper_wait_for_dependencies(state);
13643
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013644 if (intel_state->modeset) {
13645 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13646 sizeof(intel_state->min_pixclk));
13647 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013648 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013649
13650 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013651 }
13652
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013653 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13655
Daniel Vetter5a21b662016-05-24 17:13:53 +020013656 if (needs_modeset(crtc->state) ||
13657 to_intel_crtc_state(crtc->state)->update_pipe) {
13658 hw_check = true;
13659
13660 put_domains[to_intel_crtc(crtc)->pipe] =
13661 modeset_get_crtc_power_domains(crtc,
13662 to_intel_crtc_state(crtc->state));
13663 }
13664
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013665 if (!needs_modeset(crtc->state))
13666 continue;
13667
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013668 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013669
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013670 if (old_crtc_state->active) {
13671 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013672 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013673 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013674 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013675 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013676
13677 /*
13678 * Underruns don't always raise
13679 * interrupts, so check manually.
13680 */
13681 intel_check_cpu_fifo_underruns(dev_priv);
13682 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013683
13684 if (!crtc->state->active)
13685 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013686 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013687 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013688
Daniel Vetterea9d7582012-07-10 10:42:52 +020013689 /* Only after disabling all output pipelines that will be changed can we
13690 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013691 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013692
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013693 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013694 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013695
13696 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013697 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013698 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013699 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013700
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013701 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013702 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013703
Daniel Vettera6778b32012-07-02 09:56:42 +020013704 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013705 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13707 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013708 struct intel_crtc_state *pipe_config =
13709 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013710
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013711 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013712 update_scanline_offset(to_intel_crtc(crtc));
13713 dev_priv->display.crtc_enable(crtc);
13714 }
13715
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013716 /* Complete events for now disable pipes here. */
13717 if (modeset && !crtc->state->active && crtc->state->event) {
13718 spin_lock_irq(&dev->event_lock);
13719 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13720 spin_unlock_irq(&dev->event_lock);
13721
13722 crtc->state->event = NULL;
13723 }
13724
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013725 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013726 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013727
Daniel Vetter5a21b662016-05-24 17:13:53 +020013728 if (crtc->state->active &&
13729 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013730 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013731
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013732 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013733 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013734
Daniel Vetter5a21b662016-05-24 17:13:53 +020013735 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13736 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013737 }
13738
Daniel Vetter94f05022016-06-14 18:01:00 +020013739 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13740 * already, but still need the state for the delayed optimization. To
13741 * fix this:
13742 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13743 * - schedule that vblank worker _before_ calling hw_done
13744 * - at the start of commit_tail, cancel it _synchrously
13745 * - switch over to the vblank wait helper in the core after that since
13746 * we don't need out special handling any more.
13747 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013748 if (!state->legacy_cursor_update)
13749 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13750
13751 /*
13752 * Now that the vblank has passed, we can go ahead and program the
13753 * optimal watermarks on platforms that need two-step watermark
13754 * programming.
13755 *
13756 * TODO: Move this (and other cleanup) to an async worker eventually.
13757 */
13758 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13759 intel_cstate = to_intel_crtc_state(crtc->state);
13760
13761 if (dev_priv->display.optimize_watermarks)
13762 dev_priv->display.optimize_watermarks(intel_cstate);
13763 }
13764
13765 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13766 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13767
13768 if (put_domains[i])
13769 modeset_put_power_domains(dev_priv, put_domains[i]);
13770
13771 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13772 }
13773
Daniel Vetter94f05022016-06-14 18:01:00 +020013774 drm_atomic_helper_commit_hw_done(state);
13775
Daniel Vetter5a21b662016-05-24 17:13:53 +020013776 if (intel_state->modeset)
13777 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13778
13779 mutex_lock(&dev->struct_mutex);
13780 drm_atomic_helper_cleanup_planes(dev, state);
13781 mutex_unlock(&dev->struct_mutex);
13782
Daniel Vetterea0000f2016-06-13 16:13:46 +020013783 drm_atomic_helper_commit_cleanup_done(state);
13784
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013785 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013786
Mika Kuoppala75714942015-12-16 09:26:48 +020013787 /* As one of the primary mmio accessors, KMS has a high likelihood
13788 * of triggering bugs in unclaimed access. After we finish
13789 * modesetting, see if an error has been flagged, and if so
13790 * enable debugging for the next modeset - and hope we catch
13791 * the culprit.
13792 *
13793 * XXX note that we assume display power is on at this point.
13794 * This might hold true now but we need to add pm helper to check
13795 * unclaimed only when the hardware is on, as atomic commits
13796 * can happen also when the device is completely off.
13797 */
13798 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013799}
13800
13801static void intel_atomic_commit_work(struct work_struct *work)
13802{
13803 struct drm_atomic_state *state = container_of(work,
13804 struct drm_atomic_state,
13805 commit_work);
13806 intel_atomic_commit_tail(state);
13807}
13808
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013809static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13810{
13811 struct drm_plane_state *old_plane_state;
13812 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013813 int i;
13814
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010013815 for_each_plane_in_state(state, plane, old_plane_state, i)
13816 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
13817 intel_fb_obj(plane->state->fb),
13818 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013819}
13820
Daniel Vetter94f05022016-06-14 18:01:00 +020013821/**
13822 * intel_atomic_commit - commit validated state object
13823 * @dev: DRM device
13824 * @state: the top-level driver state object
13825 * @nonblock: nonblocking commit
13826 *
13827 * This function commits a top-level state object that has been validated
13828 * with drm_atomic_helper_check().
13829 *
13830 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13831 * nonblocking commits are only safe for pure plane updates. Everything else
13832 * should work though.
13833 *
13834 * RETURNS
13835 * Zero for success or -errno.
13836 */
13837static int intel_atomic_commit(struct drm_device *dev,
13838 struct drm_atomic_state *state,
13839 bool nonblock)
13840{
13841 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013842 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013843 int ret = 0;
13844
13845 if (intel_state->modeset && nonblock) {
13846 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13847 return -EINVAL;
13848 }
13849
13850 ret = drm_atomic_helper_setup_commit(state, nonblock);
13851 if (ret)
13852 return ret;
13853
13854 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13855
13856 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13857 if (ret) {
13858 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13859 return ret;
13860 }
13861
13862 drm_atomic_helper_swap_state(state, true);
13863 dev_priv->wm.distrust_bios_wm = false;
13864 dev_priv->wm.skl_results = intel_state->wm_results;
13865 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013866 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013867
13868 if (nonblock)
13869 queue_work(system_unbound_wq, &state->commit_work);
13870 else
13871 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013872
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013873 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013874}
13875
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013876void intel_crtc_restore_mode(struct drm_crtc *crtc)
13877{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013878 struct drm_device *dev = crtc->dev;
13879 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013880 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013881 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013882
13883 state = drm_atomic_state_alloc(dev);
13884 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013885 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13886 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013887 return;
13888 }
13889
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013890 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013891
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013892retry:
13893 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13894 ret = PTR_ERR_OR_ZERO(crtc_state);
13895 if (!ret) {
13896 if (!crtc_state->active)
13897 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013898
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013899 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013900 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013901 }
13902
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013903 if (ret == -EDEADLK) {
13904 drm_atomic_state_clear(state);
13905 drm_modeset_backoff(state->acquire_ctx);
13906 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013907 }
13908
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013909 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013910out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013911 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013912}
13913
Daniel Vetter25c5b262012-07-08 22:08:04 +020013914#undef for_each_intel_crtc_masked
13915
Bob Paauwea8784872016-07-15 14:59:02 +010013916/*
13917 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13918 * drm_atomic_helper_legacy_gamma_set() directly.
13919 */
13920static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13921 u16 *red, u16 *green, u16 *blue,
13922 uint32_t size)
13923{
13924 struct drm_device *dev = crtc->dev;
13925 struct drm_mode_config *config = &dev->mode_config;
13926 struct drm_crtc_state *state;
13927 int ret;
13928
13929 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13930 if (ret)
13931 return ret;
13932
13933 /*
13934 * Make sure we update the legacy properties so this works when
13935 * atomic is not enabled.
13936 */
13937
13938 state = crtc->state;
13939
13940 drm_object_property_set_value(&crtc->base,
13941 config->degamma_lut_property,
13942 (state->degamma_lut) ?
13943 state->degamma_lut->base.id : 0);
13944
13945 drm_object_property_set_value(&crtc->base,
13946 config->ctm_property,
13947 (state->ctm) ?
13948 state->ctm->base.id : 0);
13949
13950 drm_object_property_set_value(&crtc->base,
13951 config->gamma_lut_property,
13952 (state->gamma_lut) ?
13953 state->gamma_lut->base.id : 0);
13954
13955 return 0;
13956}
13957
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013958static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010013959 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013960 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013961 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013962 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010013963 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013964 .atomic_duplicate_state = intel_crtc_duplicate_state,
13965 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013966};
13967
Matt Roper6beb8c232014-12-01 15:40:14 -080013968/**
13969 * intel_prepare_plane_fb - Prepare fb for usage on plane
13970 * @plane: drm plane to prepare for
13971 * @fb: framebuffer to prepare for presentation
13972 *
13973 * Prepares a framebuffer for usage on a display plane. Generally this
13974 * involves pinning the underlying object and updating the frontbuffer tracking
13975 * bits. Some older platforms need special physical address handling for
13976 * cursor planes.
13977 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013978 * Must be called with struct_mutex held.
13979 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013980 * Returns 0 on success, negative error code on failure.
13981 */
13982int
13983intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013984 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013985{
13986 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013987 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013988 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013989 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010013990 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080013991 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013992
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013993 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013994 return 0;
13995
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013996 if (old_obj) {
13997 struct drm_crtc_state *crtc_state =
13998 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13999
14000 /* Big Hammer, we also need to ensure that any pending
14001 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14002 * current scanout is retired before unpinning the old
14003 * framebuffer. Note that we rely on userspace rendering
14004 * into the buffer attached to the pipe they are waiting
14005 * on. If not, userspace generates a GPU hang with IPEHR
14006 * point to the MI_WAIT_FOR_EVENT.
14007 *
14008 * This should only fail upon a hung GPU, in which case we
14009 * can safely continue.
14010 */
14011 if (needs_modeset(crtc_state))
14012 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014013 if (ret) {
14014 /* GPU hangs should have been swallowed by the wait */
14015 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014016 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014017 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014018 }
14019
Chris Wilsonc37efb92016-06-17 08:28:47 +010014020 if (!obj)
14021 return 0;
14022
Daniel Vetter5a21b662016-05-24 17:13:53 +020014023 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014024 resv = i915_gem_object_get_dmabuf_resv(obj);
14025 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014026 long lret;
14027
Chris Wilsonc37efb92016-06-17 08:28:47 +010014028 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014029 MAX_SCHEDULE_TIMEOUT);
14030 if (lret == -ERESTARTSYS)
14031 return lret;
14032
14033 WARN(lret < 0, "waiting returns %li\n", lret);
14034 }
14035
Chris Wilsonc37efb92016-06-17 08:28:47 +010014036 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014037 INTEL_INFO(dev)->cursor_needs_physical) {
14038 int align = IS_I830(dev) ? 16 * 1024 : 256;
14039 ret = i915_gem_object_attach_phys(obj, align);
14040 if (ret)
14041 DRM_DEBUG_KMS("failed to attach phys object\n");
14042 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014043 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014044 }
14045
Chris Wilsonc37efb92016-06-17 08:28:47 +010014046 if (ret == 0) {
Chris Wilson27c01aa2016-08-04 07:52:30 +010014047 to_intel_plane_state(new_state)->wait_req =
Chris Wilsond72d9082016-08-04 07:52:31 +010014048 i915_gem_active_get(&obj->last_write,
14049 &obj->base.dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014050 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014051
Matt Roper6beb8c232014-12-01 15:40:14 -080014052 return ret;
14053}
14054
Matt Roper38f3ce32014-12-02 07:45:25 -080014055/**
14056 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14057 * @plane: drm plane to clean up for
14058 * @fb: old framebuffer that was on plane
14059 *
14060 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014061 *
14062 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014063 */
14064void
14065intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014066 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014067{
14068 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014069 struct intel_plane_state *old_intel_state;
Keith Packard84978252016-07-31 00:54:51 -070014070 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014071 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14072 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014073
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014074 old_intel_state = to_intel_plane_state(old_state);
14075
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014076 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014077 return;
14078
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014079 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14080 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014081 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014082
Keith Packard84978252016-07-31 00:54:51 -070014083 i915_gem_request_assign(&intel_state->wait_req, NULL);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014084 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014085}
14086
Chandra Konduru6156a452015-04-27 13:48:39 -070014087int
14088skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14089{
14090 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014091 int crtc_clock, cdclk;
14092
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014093 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014094 return DRM_PLANE_HELPER_NO_SCALING;
14095
Chandra Konduru6156a452015-04-27 13:48:39 -070014096 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014097 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014098
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014099 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014100 return DRM_PLANE_HELPER_NO_SCALING;
14101
14102 /*
14103 * skl max scale is lower of:
14104 * close to 3 but not 3, -1 is for that purpose
14105 * or
14106 * cdclk/crtc_clock
14107 */
14108 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14109
14110 return max_scale;
14111}
14112
Matt Roper465c1202014-05-29 08:06:54 -070014113static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014114intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014115 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014116 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014117{
Matt Roper2b875c22014-12-01 15:40:13 -080014118 struct drm_crtc *crtc = state->base.crtc;
14119 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014120 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014121 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14122 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014123
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014124 if (INTEL_INFO(plane->dev)->gen >= 9) {
14125 /* use scaler when colorkey is not required */
14126 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14127 min_scale = 1;
14128 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14129 }
Sonika Jindald8106362015-04-10 14:37:28 +053014130 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014131 }
Sonika Jindald8106362015-04-10 14:37:28 +053014132
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014133 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14134 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014135 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014136 min_scale, max_scale,
14137 can_position, true,
14138 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014139}
14140
Daniel Vetter5a21b662016-05-24 17:13:53 +020014141static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14142 struct drm_crtc_state *old_crtc_state)
14143{
14144 struct drm_device *dev = crtc->dev;
14145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14146 struct intel_crtc_state *old_intel_state =
14147 to_intel_crtc_state(old_crtc_state);
14148 bool modeset = needs_modeset(crtc->state);
14149
14150 /* Perform vblank evasion around commit operation */
14151 intel_pipe_update_start(intel_crtc);
14152
14153 if (modeset)
14154 return;
14155
14156 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14157 intel_color_set_csc(crtc->state);
14158 intel_color_load_luts(crtc->state);
14159 }
14160
14161 if (to_intel_crtc_state(crtc->state)->update_pipe)
14162 intel_update_pipe_config(intel_crtc, old_intel_state);
14163 else if (INTEL_INFO(dev)->gen >= 9)
14164 skl_detach_scalers(intel_crtc);
14165}
14166
14167static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14168 struct drm_crtc_state *old_crtc_state)
14169{
14170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14171
14172 intel_pipe_update_end(intel_crtc, NULL);
14173}
14174
Matt Ropercf4c7c12014-12-04 10:27:42 -080014175/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014176 * intel_plane_destroy - destroy a plane
14177 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014178 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014179 * Common destruction function for all types of planes (primary, cursor,
14180 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014181 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014182void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014183{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014184 if (!plane)
14185 return;
14186
Matt Roper465c1202014-05-29 08:06:54 -070014187 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014188 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014189}
14190
Matt Roper65a3fea2015-01-21 16:35:42 -080014191const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014192 .update_plane = drm_atomic_helper_update_plane,
14193 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014194 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014195 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014196 .atomic_get_property = intel_plane_atomic_get_property,
14197 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014198 .atomic_duplicate_state = intel_plane_duplicate_state,
14199 .atomic_destroy_state = intel_plane_destroy_state,
14200
Matt Roper465c1202014-05-29 08:06:54 -070014201};
14202
14203static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14204 int pipe)
14205{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014206 struct intel_plane *primary = NULL;
14207 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014208 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014209 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014210 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014211
14212 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014213 if (!primary)
14214 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014215
Matt Roper8e7d6882015-01-21 16:35:41 -080014216 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014217 if (!state)
14218 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014219 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014220
Matt Roper465c1202014-05-29 08:06:54 -070014221 primary->can_scale = false;
14222 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014223 if (INTEL_INFO(dev)->gen >= 9) {
14224 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014225 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014226 }
Matt Roper465c1202014-05-29 08:06:54 -070014227 primary->pipe = pipe;
14228 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014229 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014230 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014231 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14232 primary->plane = !pipe;
14233
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014234 if (INTEL_INFO(dev)->gen >= 9) {
14235 intel_primary_formats = skl_primary_formats;
14236 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014237
14238 primary->update_plane = skylake_update_primary_plane;
14239 primary->disable_plane = skylake_disable_primary_plane;
14240 } else if (HAS_PCH_SPLIT(dev)) {
14241 intel_primary_formats = i965_primary_formats;
14242 num_formats = ARRAY_SIZE(i965_primary_formats);
14243
14244 primary->update_plane = ironlake_update_primary_plane;
14245 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014246 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014247 intel_primary_formats = i965_primary_formats;
14248 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014249
14250 primary->update_plane = i9xx_update_primary_plane;
14251 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014252 } else {
14253 intel_primary_formats = i8xx_primary_formats;
14254 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014255
14256 primary->update_plane = i9xx_update_primary_plane;
14257 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014258 }
14259
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014260 if (INTEL_INFO(dev)->gen >= 9)
14261 ret = drm_universal_plane_init(dev, &primary->base, 0,
14262 &intel_plane_funcs,
14263 intel_primary_formats, num_formats,
14264 DRM_PLANE_TYPE_PRIMARY,
14265 "plane 1%c", pipe_name(pipe));
14266 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14267 ret = drm_universal_plane_init(dev, &primary->base, 0,
14268 &intel_plane_funcs,
14269 intel_primary_formats, num_formats,
14270 DRM_PLANE_TYPE_PRIMARY,
14271 "primary %c", pipe_name(pipe));
14272 else
14273 ret = drm_universal_plane_init(dev, &primary->base, 0,
14274 &intel_plane_funcs,
14275 intel_primary_formats, num_formats,
14276 DRM_PLANE_TYPE_PRIMARY,
14277 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014278 if (ret)
14279 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014280
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014281 if (INTEL_INFO(dev)->gen >= 4)
14282 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014283
Matt Roperea2c67b2014-12-23 10:41:52 -080014284 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14285
Matt Roper465c1202014-05-29 08:06:54 -070014286 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014287
14288fail:
14289 kfree(state);
14290 kfree(primary);
14291
14292 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014293}
14294
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014295void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14296{
14297 if (!dev->mode_config.rotation_property) {
14298 unsigned long flags = BIT(DRM_ROTATE_0) |
14299 BIT(DRM_ROTATE_180);
14300
14301 if (INTEL_INFO(dev)->gen >= 9)
14302 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14303
14304 dev->mode_config.rotation_property =
14305 drm_mode_create_rotation_property(dev, flags);
14306 }
14307 if (dev->mode_config.rotation_property)
14308 drm_object_attach_property(&plane->base.base,
14309 dev->mode_config.rotation_property,
14310 plane->base.state->rotation);
14311}
14312
Matt Roper3d7d6512014-06-10 08:28:13 -070014313static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014314intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014315 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014316 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014317{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014318 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014319 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014320 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014321 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014322 unsigned stride;
14323 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014324
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014325 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14326 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014327 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014328 DRM_PLANE_HELPER_NO_SCALING,
14329 DRM_PLANE_HELPER_NO_SCALING,
14330 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014331 if (ret)
14332 return ret;
14333
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014334 /* if we want to turn off the cursor ignore width and height */
14335 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014336 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014337
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014338 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014339 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014340 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14341 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014342 return -EINVAL;
14343 }
14344
Matt Roperea2c67b2014-12-23 10:41:52 -080014345 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14346 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014347 DRM_DEBUG_KMS("buffer is too small\n");
14348 return -ENOMEM;
14349 }
14350
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014351 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014352 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014353 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014354 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014355
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014356 /*
14357 * There's something wrong with the cursor on CHV pipe C.
14358 * If it straddles the left edge of the screen then
14359 * moving it away from the edge or disabling it often
14360 * results in a pipe underrun, and often that can lead to
14361 * dead pipe (constant underrun reported, and it scans
14362 * out just a solid color). To recover from that, the
14363 * display power well must be turned off and on again.
14364 * Refuse the put the cursor into that compromised position.
14365 */
14366 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14367 state->visible && state->base.crtc_x < 0) {
14368 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14369 return -EINVAL;
14370 }
14371
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014372 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014373}
14374
Matt Roperf4a2cf22014-12-01 15:40:12 -080014375static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014376intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014377 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014378{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14380
14381 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014382 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014383}
14384
14385static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014386intel_update_cursor_plane(struct drm_plane *plane,
14387 const struct intel_crtc_state *crtc_state,
14388 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014389{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014390 struct drm_crtc *crtc = crtc_state->base.crtc;
14391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014392 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014393 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014394 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014395
Matt Roperf4a2cf22014-12-01 15:40:12 -080014396 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014397 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014398 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014399 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014400 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014401 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014402
Gustavo Padovana912f122014-12-01 15:40:10 -080014403 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014404 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014405}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014406
Matt Roper3d7d6512014-06-10 08:28:13 -070014407static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14408 int pipe)
14409{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014410 struct intel_plane *cursor = NULL;
14411 struct intel_plane_state *state = NULL;
14412 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014413
14414 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014415 if (!cursor)
14416 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014417
Matt Roper8e7d6882015-01-21 16:35:41 -080014418 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014419 if (!state)
14420 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014421 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014422
Matt Roper3d7d6512014-06-10 08:28:13 -070014423 cursor->can_scale = false;
14424 cursor->max_downscale = 1;
14425 cursor->pipe = pipe;
14426 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014427 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014428 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014429 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014430 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014431
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014432 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14433 &intel_plane_funcs,
14434 intel_cursor_formats,
14435 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014436 DRM_PLANE_TYPE_CURSOR,
14437 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014438 if (ret)
14439 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014440
14441 if (INTEL_INFO(dev)->gen >= 4) {
14442 if (!dev->mode_config.rotation_property)
14443 dev->mode_config.rotation_property =
14444 drm_mode_create_rotation_property(dev,
14445 BIT(DRM_ROTATE_0) |
14446 BIT(DRM_ROTATE_180));
14447 if (dev->mode_config.rotation_property)
14448 drm_object_attach_property(&cursor->base.base,
14449 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014450 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014451 }
14452
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014453 if (INTEL_INFO(dev)->gen >=9)
14454 state->scaler_id = -1;
14455
Matt Roperea2c67b2014-12-23 10:41:52 -080014456 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14457
Matt Roper3d7d6512014-06-10 08:28:13 -070014458 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014459
14460fail:
14461 kfree(state);
14462 kfree(cursor);
14463
14464 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014465}
14466
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014467static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14468 struct intel_crtc_state *crtc_state)
14469{
14470 int i;
14471 struct intel_scaler *intel_scaler;
14472 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14473
14474 for (i = 0; i < intel_crtc->num_scalers; i++) {
14475 intel_scaler = &scaler_state->scalers[i];
14476 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014477 intel_scaler->mode = PS_SCALER_MODE_DYN;
14478 }
14479
14480 scaler_state->scaler_id = -1;
14481}
14482
Hannes Ederb358d0a2008-12-18 21:18:47 +010014483static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014484{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014485 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014486 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014487 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014488 struct drm_plane *primary = NULL;
14489 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014490 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014491
Daniel Vetter955382f2013-09-19 14:05:45 +020014492 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014493 if (intel_crtc == NULL)
14494 return;
14495
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014496 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14497 if (!crtc_state)
14498 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014499 intel_crtc->config = crtc_state;
14500 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014501 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014502
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014503 /* initialize shared scalers */
14504 if (INTEL_INFO(dev)->gen >= 9) {
14505 if (pipe == PIPE_C)
14506 intel_crtc->num_scalers = 1;
14507 else
14508 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14509
14510 skl_init_scalers(dev, intel_crtc, crtc_state);
14511 }
14512
Matt Roper465c1202014-05-29 08:06:54 -070014513 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014514 if (!primary)
14515 goto fail;
14516
14517 cursor = intel_cursor_plane_create(dev, pipe);
14518 if (!cursor)
14519 goto fail;
14520
Matt Roper465c1202014-05-29 08:06:54 -070014521 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014522 cursor, &intel_crtc_funcs,
14523 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014524 if (ret)
14525 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014526
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014527 /*
14528 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014529 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014530 */
Jesse Barnes80824002009-09-10 15:28:06 -070014531 intel_crtc->pipe = pipe;
14532 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014533 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014534 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014535 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014536 }
14537
Chris Wilson4b0e3332014-05-30 16:35:26 +030014538 intel_crtc->cursor_base = ~0;
14539 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014540 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014541
Ville Syrjälä852eb002015-06-24 22:00:07 +030014542 intel_crtc->wm.cxsr_allowed = true;
14543
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014544 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14545 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14546 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14547 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14548
Jesse Barnes79e53942008-11-07 14:24:08 -080014549 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014550
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014551 intel_color_init(&intel_crtc->base);
14552
Daniel Vetter87b6b102014-05-15 15:33:46 +020014553 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014554 return;
14555
14556fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014557 intel_plane_destroy(primary);
14558 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014559 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014560 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014561}
14562
Jesse Barnes752aa882013-10-31 18:55:49 +020014563enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14564{
14565 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014566 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014567
Rob Clark51fd3712013-11-19 12:10:12 -050014568 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014569
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014570 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014571 return INVALID_PIPE;
14572
14573 return to_intel_crtc(encoder->crtc)->pipe;
14574}
14575
Carl Worth08d7b3d2009-04-29 14:43:54 -070014576int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014577 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014578{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014579 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014580 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014581 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014582
Rob Clark7707e652014-07-17 23:30:04 -040014583 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014584 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014585 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014586
Rob Clark7707e652014-07-17 23:30:04 -040014587 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014588 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014589
Daniel Vetterc05422d2009-08-11 16:05:30 +020014590 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014591}
14592
Daniel Vetter66a92782012-07-12 20:08:18 +020014593static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014594{
Daniel Vetter66a92782012-07-12 20:08:18 +020014595 struct drm_device *dev = encoder->base.dev;
14596 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014597 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014598 int entry = 0;
14599
Damien Lespiaub2784e12014-08-05 11:29:37 +010014600 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014601 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014602 index_mask |= (1 << entry);
14603
Jesse Barnes79e53942008-11-07 14:24:08 -080014604 entry++;
14605 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014606
Jesse Barnes79e53942008-11-07 14:24:08 -080014607 return index_mask;
14608}
14609
Chris Wilson4d302442010-12-14 19:21:29 +000014610static bool has_edp_a(struct drm_device *dev)
14611{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014612 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000014613
14614 if (!IS_MOBILE(dev))
14615 return false;
14616
14617 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14618 return false;
14619
Damien Lespiaue3589902014-02-07 19:12:50 +000014620 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014621 return false;
14622
14623 return true;
14624}
14625
Jesse Barnes84b4e042014-06-25 08:24:29 -070014626static bool intel_crt_present(struct drm_device *dev)
14627{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014628 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070014629
Damien Lespiau884497e2013-12-03 13:56:23 +000014630 if (INTEL_INFO(dev)->gen >= 9)
14631 return false;
14632
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014633 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014634 return false;
14635
14636 if (IS_CHERRYVIEW(dev))
14637 return false;
14638
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014639 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14640 return false;
14641
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014642 /* DDI E can't be used if DDI A requires 4 lanes */
14643 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14644 return false;
14645
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014646 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014647 return false;
14648
14649 return true;
14650}
14651
Jesse Barnes79e53942008-11-07 14:24:08 -080014652static void intel_setup_outputs(struct drm_device *dev)
14653{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014654 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010014655 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014656 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014657
Imre Deak97a824e12016-06-21 11:51:47 +030014658 /*
14659 * intel_edp_init_connector() depends on this completing first, to
14660 * prevent the registeration of both eDP and LVDS and the incorrect
14661 * sharing of the PPS.
14662 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014663 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014664
Jesse Barnes84b4e042014-06-25 08:24:29 -070014665 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014666 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014667
Vandana Kannanc776eb22014-08-19 12:05:01 +053014668 if (IS_BROXTON(dev)) {
14669 /*
14670 * FIXME: Broxton doesn't support port detection via the
14671 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14672 * detect the ports.
14673 */
14674 intel_ddi_init(dev, PORT_A);
14675 intel_ddi_init(dev, PORT_B);
14676 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014677
14678 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014679 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014680 int found;
14681
Jesse Barnesde31fac2015-03-06 15:53:32 -080014682 /*
14683 * Haswell uses DDI functions to detect digital outputs.
14684 * On SKL pre-D0 the strap isn't connected, so we assume
14685 * it's there.
14686 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014687 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014688 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014689 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014690 intel_ddi_init(dev, PORT_A);
14691
14692 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14693 * register */
14694 found = I915_READ(SFUSE_STRAP);
14695
14696 if (found & SFUSE_STRAP_DDIB_DETECTED)
14697 intel_ddi_init(dev, PORT_B);
14698 if (found & SFUSE_STRAP_DDIC_DETECTED)
14699 intel_ddi_init(dev, PORT_C);
14700 if (found & SFUSE_STRAP_DDID_DETECTED)
14701 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014702 /*
14703 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14704 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014705 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014706 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14707 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14708 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14709 intel_ddi_init(dev, PORT_E);
14710
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014711 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014712 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014713 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014714
14715 if (has_edp_a(dev))
14716 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014717
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014718 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014719 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014720 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014721 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014722 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014723 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014724 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014725 }
14726
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014727 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014728 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014729
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014730 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014731 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014732
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014733 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014734 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014735
Daniel Vetter270b3042012-10-27 15:52:05 +020014736 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014737 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014738 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014739 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014740
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014741 /*
14742 * The DP_DETECTED bit is the latched state of the DDC
14743 * SDA pin at boot. However since eDP doesn't require DDC
14744 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14745 * eDP ports may have been muxed to an alternate function.
14746 * Thus we can't rely on the DP_DETECTED bit alone to detect
14747 * eDP ports. Consult the VBT as well as DP_DETECTED to
14748 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014749 *
14750 * Sadly the straps seem to be missing sometimes even for HDMI
14751 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14752 * and VBT for the presence of the port. Additionally we can't
14753 * trust the port type the VBT declares as we've seen at least
14754 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014755 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014756 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014757 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14758 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014759 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014760 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014761 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014762
Chris Wilson457c52d2016-06-01 08:27:50 +010014763 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014764 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14765 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014766 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014767 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014768 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014769
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014770 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014771 /*
14772 * eDP not supported on port D,
14773 * so no need to worry about it
14774 */
14775 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14776 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014777 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014778 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14779 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014780 }
14781
Jani Nikula3cfca972013-08-27 15:12:26 +030014782 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014783 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014784 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014785
Paulo Zanonie2debe92013-02-18 19:00:27 -030014786 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014787 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014788 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014789 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014790 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014791 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014792 }
Ma Ling27185ae2009-08-24 13:50:23 +080014793
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014794 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014795 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014796 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014797
14798 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014799
Paulo Zanonie2debe92013-02-18 19:00:27 -030014800 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014801 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014802 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014803 }
Ma Ling27185ae2009-08-24 13:50:23 +080014804
Paulo Zanonie2debe92013-02-18 19:00:27 -030014805 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014806
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014807 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014808 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014809 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014810 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014811 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014812 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014813 }
Ma Ling27185ae2009-08-24 13:50:23 +080014814
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014815 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014816 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014817 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014818 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014819 intel_dvo_init(dev);
14820
Zhenyu Wang103a1962009-11-27 11:44:36 +080014821 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014822 intel_tv_init(dev);
14823
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014824 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014825
Damien Lespiaub2784e12014-08-05 11:29:37 +010014826 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014827 encoder->base.possible_crtcs = encoder->crtc_mask;
14828 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014829 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014830 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014831
Paulo Zanonidde86e22012-12-01 12:04:25 -020014832 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014833
14834 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014835}
14836
14837static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14838{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014839 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014840 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014841
Daniel Vetteref2d6332014-02-10 18:00:38 +010014842 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014843 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014844 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010014845 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014846 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014847 kfree(intel_fb);
14848}
14849
14850static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014851 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014852 unsigned int *handle)
14853{
14854 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014855 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014856
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014857 if (obj->userptr.mm) {
14858 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14859 return -EINVAL;
14860 }
14861
Chris Wilson05394f32010-11-08 19:18:58 +000014862 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014863}
14864
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014865static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14866 struct drm_file *file,
14867 unsigned flags, unsigned color,
14868 struct drm_clip_rect *clips,
14869 unsigned num_clips)
14870{
14871 struct drm_device *dev = fb->dev;
14872 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14873 struct drm_i915_gem_object *obj = intel_fb->obj;
14874
14875 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014876 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014877 mutex_unlock(&dev->struct_mutex);
14878
14879 return 0;
14880}
14881
Jesse Barnes79e53942008-11-07 14:24:08 -080014882static const struct drm_framebuffer_funcs intel_fb_funcs = {
14883 .destroy = intel_user_framebuffer_destroy,
14884 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014885 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014886};
14887
Damien Lespiaub3218032015-02-27 11:15:18 +000014888static
14889u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14890 uint32_t pixel_format)
14891{
14892 u32 gen = INTEL_INFO(dev)->gen;
14893
14894 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014895 int cpp = drm_format_plane_cpp(pixel_format, 0);
14896
Damien Lespiaub3218032015-02-27 11:15:18 +000014897 /* "The stride in bytes must not exceed the of the size of 8K
14898 * pixels and 32K bytes."
14899 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014900 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014901 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014902 return 32*1024;
14903 } else if (gen >= 4) {
14904 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14905 return 16*1024;
14906 else
14907 return 32*1024;
14908 } else if (gen >= 3) {
14909 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14910 return 8*1024;
14911 else
14912 return 16*1024;
14913 } else {
14914 /* XXX DSPC is limited to 4k tiled */
14915 return 8*1024;
14916 }
14917}
14918
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014919static int intel_framebuffer_init(struct drm_device *dev,
14920 struct intel_framebuffer *intel_fb,
14921 struct drm_mode_fb_cmd2 *mode_cmd,
14922 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014923{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014924 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014925 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014926 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014927 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014928
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014929 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14930
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014931 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14932 /* Enforce that fb modifier and tiling mode match, but only for
14933 * X-tiled. This is needed for FBC. */
Chris Wilson3e510a82016-08-05 10:14:23 +010014934 if (!!(i915_gem_object_get_tiling(obj) == I915_TILING_X) !=
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014935 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14936 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14937 return -EINVAL;
14938 }
14939 } else {
Chris Wilson3e510a82016-08-05 10:14:23 +010014940 if (i915_gem_object_get_tiling(obj) == I915_TILING_X)
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014941 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Chris Wilson3e510a82016-08-05 10:14:23 +010014942 else if (i915_gem_object_get_tiling(obj) == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014943 DRM_DEBUG("No Y tiling for legacy addfb\n");
14944 return -EINVAL;
14945 }
14946 }
14947
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014948 /* Passed in modifier sanity checking. */
14949 switch (mode_cmd->modifier[0]) {
14950 case I915_FORMAT_MOD_Y_TILED:
14951 case I915_FORMAT_MOD_Yf_TILED:
14952 if (INTEL_INFO(dev)->gen < 9) {
14953 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14954 mode_cmd->modifier[0]);
14955 return -EINVAL;
14956 }
14957 case DRM_FORMAT_MOD_NONE:
14958 case I915_FORMAT_MOD_X_TILED:
14959 break;
14960 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014961 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14962 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014963 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014964 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014965
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014966 stride_alignment = intel_fb_stride_alignment(dev_priv,
14967 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014968 mode_cmd->pixel_format);
14969 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14970 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14971 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014972 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014973 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014974
Damien Lespiaub3218032015-02-27 11:15:18 +000014975 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14976 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014977 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014978 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14979 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014980 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014981 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014982 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014983 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014984
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014985 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilson3e510a82016-08-05 10:14:23 +010014986 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014987 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilson3e510a82016-08-05 10:14:23 +010014988 mode_cmd->pitches[0],
14989 i915_gem_object_get_stride(obj));
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014990 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014991 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014992
Ville Syrjälä57779d02012-10-31 17:50:14 +020014993 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014994 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014995 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014996 case DRM_FORMAT_RGB565:
14997 case DRM_FORMAT_XRGB8888:
14998 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014999 break;
15000 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015001 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015002 DRM_DEBUG("unsupported pixel format: %s\n",
15003 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015004 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015005 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015006 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015007 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015008 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15009 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015010 DRM_DEBUG("unsupported pixel format: %s\n",
15011 drm_get_format_name(mode_cmd->pixel_format));
15012 return -EINVAL;
15013 }
15014 break;
15015 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015016 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015017 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015018 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015019 DRM_DEBUG("unsupported pixel format: %s\n",
15020 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015021 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015022 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015023 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015024 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015025 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015026 DRM_DEBUG("unsupported pixel format: %s\n",
15027 drm_get_format_name(mode_cmd->pixel_format));
15028 return -EINVAL;
15029 }
15030 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015031 case DRM_FORMAT_YUYV:
15032 case DRM_FORMAT_UYVY:
15033 case DRM_FORMAT_YVYU:
15034 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015035 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015036 DRM_DEBUG("unsupported pixel format: %s\n",
15037 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015038 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015039 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015040 break;
15041 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015042 DRM_DEBUG("unsupported pixel format: %s\n",
15043 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015044 return -EINVAL;
15045 }
15046
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015047 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15048 if (mode_cmd->offsets[0] != 0)
15049 return -EINVAL;
15050
Damien Lespiauec2c9812015-01-20 12:51:45 +000015051 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015052 mode_cmd->pixel_format,
15053 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015054 /* FIXME drm helper for size checks (especially planar formats)? */
15055 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15056 return -EINVAL;
15057
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015058 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15059 intel_fb->obj = obj;
15060
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015061 intel_fill_fb_info(dev_priv, &intel_fb->base);
15062
Jesse Barnes79e53942008-11-07 14:24:08 -080015063 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15064 if (ret) {
15065 DRM_ERROR("framebuffer init failed %d\n", ret);
15066 return ret;
15067 }
15068
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015069 intel_fb->obj->framebuffer_references++;
15070
Jesse Barnes79e53942008-11-07 14:24:08 -080015071 return 0;
15072}
15073
Jesse Barnes79e53942008-11-07 14:24:08 -080015074static struct drm_framebuffer *
15075intel_user_framebuffer_create(struct drm_device *dev,
15076 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020015077 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015078{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015079 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015080 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015081 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015082
Chris Wilson03ac0642016-07-20 13:31:51 +010015083 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15084 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015085 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015086
Daniel Vetter92907cb2015-11-23 09:04:05 +010015087 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015088 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010015089 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015090
15091 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015092}
15093
Daniel Vetter06957262015-08-10 13:34:08 +020015094#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015095static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015096{
15097}
15098#endif
15099
Jesse Barnes79e53942008-11-07 14:24:08 -080015100static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015101 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015102 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015103 .atomic_check = intel_atomic_check,
15104 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015105 .atomic_state_alloc = intel_atomic_state_alloc,
15106 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015107};
15108
Imre Deak88212942016-03-16 13:38:53 +020015109/**
15110 * intel_init_display_hooks - initialize the display modesetting hooks
15111 * @dev_priv: device private
15112 */
15113void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015114{
Imre Deak88212942016-03-16 13:38:53 +020015115 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015116 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015117 dev_priv->display.get_initial_plane_config =
15118 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015119 dev_priv->display.crtc_compute_clock =
15120 haswell_crtc_compute_clock;
15121 dev_priv->display.crtc_enable = haswell_crtc_enable;
15122 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015123 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015124 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015125 dev_priv->display.get_initial_plane_config =
15126 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015127 dev_priv->display.crtc_compute_clock =
15128 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015129 dev_priv->display.crtc_enable = haswell_crtc_enable;
15130 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015131 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015132 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015133 dev_priv->display.get_initial_plane_config =
15134 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015135 dev_priv->display.crtc_compute_clock =
15136 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015137 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15138 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015139 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015140 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015141 dev_priv->display.get_initial_plane_config =
15142 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015143 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15144 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15145 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15146 } else if (IS_VALLEYVIEW(dev_priv)) {
15147 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15148 dev_priv->display.get_initial_plane_config =
15149 i9xx_get_initial_plane_config;
15150 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015151 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15152 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015153 } else if (IS_G4X(dev_priv)) {
15154 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15155 dev_priv->display.get_initial_plane_config =
15156 i9xx_get_initial_plane_config;
15157 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15158 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15159 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015160 } else if (IS_PINEVIEW(dev_priv)) {
15161 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15162 dev_priv->display.get_initial_plane_config =
15163 i9xx_get_initial_plane_config;
15164 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15165 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15166 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015167 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015168 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015169 dev_priv->display.get_initial_plane_config =
15170 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015171 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015172 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15173 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015174 } else {
15175 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15176 dev_priv->display.get_initial_plane_config =
15177 i9xx_get_initial_plane_config;
15178 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15179 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15180 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015181 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015182
Jesse Barnese70236a2009-09-21 10:42:27 -070015183 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015184 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015185 dev_priv->display.get_display_clock_speed =
15186 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015187 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015188 dev_priv->display.get_display_clock_speed =
15189 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015190 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015191 dev_priv->display.get_display_clock_speed =
15192 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015193 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015194 dev_priv->display.get_display_clock_speed =
15195 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015196 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015197 dev_priv->display.get_display_clock_speed =
15198 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015199 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015200 dev_priv->display.get_display_clock_speed =
15201 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015202 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15203 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015204 dev_priv->display.get_display_clock_speed =
15205 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015206 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015207 dev_priv->display.get_display_clock_speed =
15208 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015209 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015210 dev_priv->display.get_display_clock_speed =
15211 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015212 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015213 dev_priv->display.get_display_clock_speed =
15214 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015215 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015216 dev_priv->display.get_display_clock_speed =
15217 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015218 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015219 dev_priv->display.get_display_clock_speed =
15220 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015221 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015222 dev_priv->display.get_display_clock_speed =
15223 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015224 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015225 dev_priv->display.get_display_clock_speed =
15226 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015227 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015228 dev_priv->display.get_display_clock_speed =
15229 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015230 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015231 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015232 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015233 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015234 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015235 dev_priv->display.get_display_clock_speed =
15236 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015237 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015238
Imre Deak88212942016-03-16 13:38:53 +020015239 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015240 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015241 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015242 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015243 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015244 /* FIXME: detect B0+ stepping and use auto training */
15245 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015246 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015247 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015248 }
15249
15250 if (IS_BROADWELL(dev_priv)) {
15251 dev_priv->display.modeset_commit_cdclk =
15252 broadwell_modeset_commit_cdclk;
15253 dev_priv->display.modeset_calc_cdclk =
15254 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015255 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015256 dev_priv->display.modeset_commit_cdclk =
15257 valleyview_modeset_commit_cdclk;
15258 dev_priv->display.modeset_calc_cdclk =
15259 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015260 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015261 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015262 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015263 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015264 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015265 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15266 dev_priv->display.modeset_commit_cdclk =
15267 skl_modeset_commit_cdclk;
15268 dev_priv->display.modeset_calc_cdclk =
15269 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015270 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015271
15272 switch (INTEL_INFO(dev_priv)->gen) {
15273 case 2:
15274 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15275 break;
15276
15277 case 3:
15278 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15279 break;
15280
15281 case 4:
15282 case 5:
15283 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15284 break;
15285
15286 case 6:
15287 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15288 break;
15289 case 7:
15290 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15291 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15292 break;
15293 case 9:
15294 /* Drop through - unsupported since execlist only. */
15295 default:
15296 /* Default just returns -ENODEV to indicate unsupported */
15297 dev_priv->display.queue_flip = intel_default_queue_flip;
15298 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015299}
15300
Jesse Barnesb690e962010-07-19 13:53:12 -070015301/*
15302 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15303 * resume, or other times. This quirk makes sure that's the case for
15304 * affected systems.
15305 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015306static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015307{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015308 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070015309
15310 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015311 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015312}
15313
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015314static void quirk_pipeb_force(struct drm_device *dev)
15315{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015316 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015317
15318 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15319 DRM_INFO("applying pipe b force quirk\n");
15320}
15321
Keith Packard435793d2011-07-12 14:56:22 -070015322/*
15323 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15324 */
15325static void quirk_ssc_force_disable(struct drm_device *dev)
15326{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015327 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070015328 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015329 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015330}
15331
Carsten Emde4dca20e2012-03-15 15:56:26 +010015332/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015333 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15334 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015335 */
15336static void quirk_invert_brightness(struct drm_device *dev)
15337{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015338 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010015339 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015340 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015341}
15342
Scot Doyle9c72cc62014-07-03 23:27:50 +000015343/* Some VBT's incorrectly indicate no backlight is present */
15344static void quirk_backlight_present(struct drm_device *dev)
15345{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015346 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000015347 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15348 DRM_INFO("applying backlight present quirk\n");
15349}
15350
Jesse Barnesb690e962010-07-19 13:53:12 -070015351struct intel_quirk {
15352 int device;
15353 int subsystem_vendor;
15354 int subsystem_device;
15355 void (*hook)(struct drm_device *dev);
15356};
15357
Egbert Eich5f85f172012-10-14 15:46:38 +020015358/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15359struct intel_dmi_quirk {
15360 void (*hook)(struct drm_device *dev);
15361 const struct dmi_system_id (*dmi_id_list)[];
15362};
15363
15364static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15365{
15366 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15367 return 1;
15368}
15369
15370static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15371 {
15372 .dmi_id_list = &(const struct dmi_system_id[]) {
15373 {
15374 .callback = intel_dmi_reverse_brightness,
15375 .ident = "NCR Corporation",
15376 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15377 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15378 },
15379 },
15380 { } /* terminating entry */
15381 },
15382 .hook = quirk_invert_brightness,
15383 },
15384};
15385
Ben Widawskyc43b5632012-04-16 14:07:40 -070015386static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015387 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15388 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15389
Jesse Barnesb690e962010-07-19 13:53:12 -070015390 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15391 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15392
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015393 /* 830 needs to leave pipe A & dpll A up */
15394 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15395
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015396 /* 830 needs to leave pipe B & dpll B up */
15397 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15398
Keith Packard435793d2011-07-12 14:56:22 -070015399 /* Lenovo U160 cannot use SSC on LVDS */
15400 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015401
15402 /* Sony Vaio Y cannot use SSC on LVDS */
15403 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015404
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015405 /* Acer Aspire 5734Z must invert backlight brightness */
15406 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15407
15408 /* Acer/eMachines G725 */
15409 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15410
15411 /* Acer/eMachines e725 */
15412 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15413
15414 /* Acer/Packard Bell NCL20 */
15415 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15416
15417 /* Acer Aspire 4736Z */
15418 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015419
15420 /* Acer Aspire 5336 */
15421 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015422
15423 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15424 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015425
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015426 /* Acer C720 Chromebook (Core i3 4005U) */
15427 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15428
jens steinb2a96012014-10-28 20:25:53 +010015429 /* Apple Macbook 2,1 (Core 2 T7400) */
15430 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15431
Jani Nikula1b9448b02015-11-05 11:49:59 +020015432 /* Apple Macbook 4,1 */
15433 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15434
Scot Doyled4967d82014-07-03 23:27:52 +000015435 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15436 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015437
15438 /* HP Chromebook 14 (Celeron 2955U) */
15439 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015440
15441 /* Dell Chromebook 11 */
15442 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015443
15444 /* Dell Chromebook 11 (2015 version) */
15445 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015446};
15447
15448static void intel_init_quirks(struct drm_device *dev)
15449{
15450 struct pci_dev *d = dev->pdev;
15451 int i;
15452
15453 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15454 struct intel_quirk *q = &intel_quirks[i];
15455
15456 if (d->device == q->device &&
15457 (d->subsystem_vendor == q->subsystem_vendor ||
15458 q->subsystem_vendor == PCI_ANY_ID) &&
15459 (d->subsystem_device == q->subsystem_device ||
15460 q->subsystem_device == PCI_ANY_ID))
15461 q->hook(dev);
15462 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015463 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15464 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15465 intel_dmi_quirks[i].hook(dev);
15466 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015467}
15468
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015469/* Disable the VGA plane that we never use */
15470static void i915_disable_vga(struct drm_device *dev)
15471{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015472 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015473 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015474 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015475
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015476 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015477 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015478 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015479 sr1 = inb(VGA_SR_DATA);
15480 outb(sr1 | 1<<5, VGA_SR_DATA);
15481 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15482 udelay(300);
15483
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015484 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015485 POSTING_READ(vga_reg);
15486}
15487
Daniel Vetterf8175862012-04-10 15:50:11 +020015488void intel_modeset_init_hw(struct drm_device *dev)
15489{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015490 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015491
Ville Syrjäläb6283052015-06-03 15:45:07 +030015492 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015493
15494 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15495
Daniel Vetterf8175862012-04-10 15:50:11 +020015496 intel_init_clock_gating(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015497}
15498
Matt Roperd93c0372015-12-03 11:37:41 -080015499/*
15500 * Calculate what we think the watermarks should be for the state we've read
15501 * out of the hardware and then immediately program those watermarks so that
15502 * we ensure the hardware settings match our internal state.
15503 *
15504 * We can calculate what we think WM's should be by creating a duplicate of the
15505 * current state (which was constructed during hardware readout) and running it
15506 * through the atomic check code to calculate new watermark values in the
15507 * state object.
15508 */
15509static void sanitize_watermarks(struct drm_device *dev)
15510{
15511 struct drm_i915_private *dev_priv = to_i915(dev);
15512 struct drm_atomic_state *state;
15513 struct drm_crtc *crtc;
15514 struct drm_crtc_state *cstate;
15515 struct drm_modeset_acquire_ctx ctx;
15516 int ret;
15517 int i;
15518
15519 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015520 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015521 return;
15522
15523 /*
15524 * We need to hold connection_mutex before calling duplicate_state so
15525 * that the connector loop is protected.
15526 */
15527 drm_modeset_acquire_init(&ctx, 0);
15528retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015529 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015530 if (ret == -EDEADLK) {
15531 drm_modeset_backoff(&ctx);
15532 goto retry;
15533 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015534 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015535 }
15536
15537 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15538 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015539 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015540
Matt Ropered4a6a72016-02-23 17:20:13 -080015541 /*
15542 * Hardware readout is the only time we don't want to calculate
15543 * intermediate watermarks (since we don't trust the current
15544 * watermarks).
15545 */
15546 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15547
Matt Roperd93c0372015-12-03 11:37:41 -080015548 ret = intel_atomic_check(dev, state);
15549 if (ret) {
15550 /*
15551 * If we fail here, it means that the hardware appears to be
15552 * programmed in a way that shouldn't be possible, given our
15553 * understanding of watermark requirements. This might mean a
15554 * mistake in the hardware readout code or a mistake in the
15555 * watermark calculations for a given platform. Raise a WARN
15556 * so that this is noticeable.
15557 *
15558 * If this actually happens, we'll have to just leave the
15559 * BIOS-programmed watermarks untouched and hope for the best.
15560 */
15561 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015562 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015563 }
15564
15565 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015566 for_each_crtc_in_state(state, crtc, cstate, i) {
15567 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15568
Matt Ropered4a6a72016-02-23 17:20:13 -080015569 cs->wm.need_postvbl_update = true;
15570 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015571 }
15572
15573 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015574fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015575 drm_modeset_drop_locks(&ctx);
15576 drm_modeset_acquire_fini(&ctx);
15577}
15578
Jesse Barnes79e53942008-11-07 14:24:08 -080015579void intel_modeset_init(struct drm_device *dev)
15580{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015581 struct drm_i915_private *dev_priv = to_i915(dev);
15582 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015583 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015584 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015585 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015586
15587 drm_mode_config_init(dev);
15588
15589 dev->mode_config.min_width = 0;
15590 dev->mode_config.min_height = 0;
15591
Dave Airlie019d96c2011-09-29 16:20:42 +010015592 dev->mode_config.preferred_depth = 24;
15593 dev->mode_config.prefer_shadow = 1;
15594
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015595 dev->mode_config.allow_fb_modifiers = true;
15596
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015597 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015598
Jesse Barnesb690e962010-07-19 13:53:12 -070015599 intel_init_quirks(dev);
15600
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015601 intel_init_pm(dev);
15602
Ben Widawskye3c74752013-04-05 13:12:39 -070015603 if (INTEL_INFO(dev)->num_pipes == 0)
15604 return;
15605
Lukas Wunner69f92f62015-07-15 13:57:35 +020015606 /*
15607 * There may be no VBT; and if the BIOS enabled SSC we can
15608 * just keep using it to avoid unnecessary flicker. Whereas if the
15609 * BIOS isn't using it, don't assume it will work even if the VBT
15610 * indicates as much.
15611 */
15612 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15613 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15614 DREF_SSC1_ENABLE);
15615
15616 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15617 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15618 bios_lvds_use_ssc ? "en" : "dis",
15619 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15620 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15621 }
15622 }
15623
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015624 if (IS_GEN2(dev)) {
15625 dev->mode_config.max_width = 2048;
15626 dev->mode_config.max_height = 2048;
15627 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015628 dev->mode_config.max_width = 4096;
15629 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015630 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015631 dev->mode_config.max_width = 8192;
15632 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015633 }
Damien Lespiau068be562014-03-28 14:17:49 +000015634
Ville Syrjälädc41c152014-08-13 11:57:05 +030015635 if (IS_845G(dev) || IS_I865G(dev)) {
15636 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15637 dev->mode_config.cursor_height = 1023;
15638 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015639 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15640 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15641 } else {
15642 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15643 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15644 }
15645
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015646 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015647
Zhao Yakui28c97732009-10-09 11:39:41 +080015648 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015649 INTEL_INFO(dev)->num_pipes,
15650 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015651
Damien Lespiau055e3932014-08-18 13:49:10 +010015652 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015653 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015654 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015655 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015656 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015657 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015658 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015659 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015660 }
15661
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015662 intel_update_czclk(dev_priv);
15663 intel_update_cdclk(dev);
15664
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015665 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015666
Ville Syrjäläb2045352016-05-13 23:41:27 +030015667 if (dev_priv->max_cdclk_freq == 0)
15668 intel_update_max_cdclk(dev);
15669
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015670 /* Just disable it once at startup */
15671 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015672 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015673
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015674 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015675 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015676 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015677
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015678 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015679 struct intel_initial_plane_config plane_config = {};
15680
Jesse Barnes46f297f2014-03-07 08:57:48 -080015681 if (!crtc->active)
15682 continue;
15683
Jesse Barnes46f297f2014-03-07 08:57:48 -080015684 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015685 * Note that reserving the BIOS fb up front prevents us
15686 * from stuffing other stolen allocations like the ring
15687 * on top. This prevents some ugliness at boot time, and
15688 * can even allow for smooth boot transitions if the BIOS
15689 * fb is large enough for the active pipe configuration.
15690 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015691 dev_priv->display.get_initial_plane_config(crtc,
15692 &plane_config);
15693
15694 /*
15695 * If the fb is shared between multiple heads, we'll
15696 * just get the first one.
15697 */
15698 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015699 }
Matt Roperd93c0372015-12-03 11:37:41 -080015700
15701 /*
15702 * Make sure hardware watermarks really match the state we read out.
15703 * Note that we need to do this after reconstructing the BIOS fb's
15704 * since the watermark calculation done here will use pstate->fb.
15705 */
15706 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015707}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015708
Daniel Vetter7fad7982012-07-04 17:51:47 +020015709static void intel_enable_pipe_a(struct drm_device *dev)
15710{
15711 struct intel_connector *connector;
15712 struct drm_connector *crt = NULL;
15713 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015714 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015715
15716 /* We can't just switch on the pipe A, we need to set things up with a
15717 * proper mode and output configuration. As a gross hack, enable pipe A
15718 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015719 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015720 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15721 crt = &connector->base;
15722 break;
15723 }
15724 }
15725
15726 if (!crt)
15727 return;
15728
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015729 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015730 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015731}
15732
Daniel Vetterfa555832012-10-10 23:14:00 +020015733static bool
15734intel_check_plane_mapping(struct intel_crtc *crtc)
15735{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015736 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015737 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015738 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015739
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015740 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015741 return true;
15742
Ville Syrjälä649636e2015-09-22 19:50:01 +030015743 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015744
15745 if ((val & DISPLAY_PLANE_ENABLE) &&
15746 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15747 return false;
15748
15749 return true;
15750}
15751
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015752static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15753{
15754 struct drm_device *dev = crtc->base.dev;
15755 struct intel_encoder *encoder;
15756
15757 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15758 return true;
15759
15760 return false;
15761}
15762
Ville Syrjälädd756192016-02-17 21:28:45 +020015763static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15764{
15765 struct drm_device *dev = encoder->base.dev;
15766 struct intel_connector *connector;
15767
15768 for_each_connector_on_encoder(dev, &encoder->base, connector)
15769 return true;
15770
15771 return false;
15772}
15773
Daniel Vetter24929352012-07-02 20:28:59 +020015774static void intel_sanitize_crtc(struct intel_crtc *crtc)
15775{
15776 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015777 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015778 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015779
Daniel Vetter24929352012-07-02 20:28:59 +020015780 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015781 if (!transcoder_is_dsi(cpu_transcoder)) {
15782 i915_reg_t reg = PIPECONF(cpu_transcoder);
15783
15784 I915_WRITE(reg,
15785 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15786 }
Daniel Vetter24929352012-07-02 20:28:59 +020015787
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015788 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015789 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015790 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015791 struct intel_plane *plane;
15792
Daniel Vetter96256042015-02-13 21:03:42 +010015793 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015794
15795 /* Disable everything but the primary plane */
15796 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15797 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15798 continue;
15799
15800 plane->disable_plane(&plane->base, &crtc->base);
15801 }
Daniel Vetter96256042015-02-13 21:03:42 +010015802 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015803
Daniel Vetter24929352012-07-02 20:28:59 +020015804 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015805 * disable the crtc (and hence change the state) if it is wrong. Note
15806 * that gen4+ has a fixed plane -> pipe mapping. */
15807 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015808 bool plane;
15809
Ville Syrjälä78108b72016-05-27 20:59:19 +030015810 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15811 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015812
15813 /* Pipe has the wrong plane attached and the plane is active.
15814 * Temporarily change the plane mapping and disable everything
15815 * ... */
15816 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015817 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015818 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015819 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015820 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015821 }
Daniel Vetter24929352012-07-02 20:28:59 +020015822
Daniel Vetter7fad7982012-07-04 17:51:47 +020015823 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15824 crtc->pipe == PIPE_A && !crtc->active) {
15825 /* BIOS forgot to enable pipe A, this mostly happens after
15826 * resume. Force-enable the pipe to fix this, the update_dpms
15827 * call below we restore the pipe to the right state, but leave
15828 * the required bits on. */
15829 intel_enable_pipe_a(dev);
15830 }
15831
Daniel Vetter24929352012-07-02 20:28:59 +020015832 /* Adjust the state of the output pipe according to whether we
15833 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015834 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015835 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015836
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015837 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015838 /*
15839 * We start out with underrun reporting disabled to avoid races.
15840 * For correct bookkeeping mark this on active crtcs.
15841 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015842 * Also on gmch platforms we dont have any hardware bits to
15843 * disable the underrun reporting. Which means we need to start
15844 * out with underrun reporting disabled also on inactive pipes,
15845 * since otherwise we'll complain about the garbage we read when
15846 * e.g. coming up after runtime pm.
15847 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015848 * No protection against concurrent access is required - at
15849 * worst a fifo underrun happens which also sets this to false.
15850 */
15851 crtc->cpu_fifo_underrun_disabled = true;
15852 crtc->pch_fifo_underrun_disabled = true;
15853 }
Daniel Vetter24929352012-07-02 20:28:59 +020015854}
15855
15856static void intel_sanitize_encoder(struct intel_encoder *encoder)
15857{
15858 struct intel_connector *connector;
15859 struct drm_device *dev = encoder->base.dev;
15860
15861 /* We need to check both for a crtc link (meaning that the
15862 * encoder is active and trying to read from a pipe) and the
15863 * pipe itself being active. */
15864 bool has_active_crtc = encoder->base.crtc &&
15865 to_intel_crtc(encoder->base.crtc)->active;
15866
Ville Syrjälädd756192016-02-17 21:28:45 +020015867 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015868 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15869 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015870 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015871
15872 /* Connector is active, but has no active pipe. This is
15873 * fallout from our resume register restoring. Disable
15874 * the encoder manually again. */
15875 if (encoder->base.crtc) {
15876 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15877 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015878 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015879 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015880 if (encoder->post_disable)
15881 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015882 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015883 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015884
15885 /* Inconsistent output/port/pipe state happens presumably due to
15886 * a bug in one of the get_hw_state functions. Or someplace else
15887 * in our code, like the register restore mess on resume. Clamp
15888 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015889 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015890 if (connector->encoder != encoder)
15891 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015892 connector->base.dpms = DRM_MODE_DPMS_OFF;
15893 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015894 }
15895 }
15896 /* Enabled encoders without active connectors will be fixed in
15897 * the crtc fixup. */
15898}
15899
Imre Deak04098752014-02-18 00:02:16 +020015900void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015901{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015902 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015903 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015904
Imre Deak04098752014-02-18 00:02:16 +020015905 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15906 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15907 i915_disable_vga(dev);
15908 }
15909}
15910
15911void i915_redisable_vga(struct drm_device *dev)
15912{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015913 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020015914
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015915 /* This function can be called both from intel_modeset_setup_hw_state or
15916 * at a very early point in our resume sequence, where the power well
15917 * structures are not yet restored. Since this function is at a very
15918 * paranoid "someone might have enabled VGA while we were not looking"
15919 * level, just check if the power well is enabled instead of trying to
15920 * follow the "don't touch the power well if we don't need it" policy
15921 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015922 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015923 return;
15924
Imre Deak04098752014-02-18 00:02:16 +020015925 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015926
15927 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015928}
15929
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015930static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015931{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015932 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015933
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015934 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015935}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015936
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015937/* FIXME read out full plane state for all planes */
15938static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015939{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015940 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015941 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015942 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015943
Matt Roper19b8d382015-09-24 15:53:17 -070015944 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015945 primary_get_hw_state(to_intel_plane(primary));
15946
15947 if (plane_state->visible)
15948 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015949}
15950
Daniel Vetter30e984d2013-06-05 13:34:17 +020015951static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015952{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015953 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015954 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015955 struct intel_crtc *crtc;
15956 struct intel_encoder *encoder;
15957 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015958 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015959
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015960 dev_priv->active_crtcs = 0;
15961
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015962 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015963 struct intel_crtc_state *crtc_state = crtc->config;
15964 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015965
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015966 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015967 memset(crtc_state, 0, sizeof(*crtc_state));
15968 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015969
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015970 crtc_state->base.active = crtc_state->base.enable =
15971 dev_priv->display.get_pipe_config(crtc, crtc_state);
15972
15973 crtc->base.enabled = crtc_state->base.enable;
15974 crtc->active = crtc_state->base.active;
15975
15976 if (crtc_state->base.active) {
15977 dev_priv->active_crtcs |= 1 << crtc->pipe;
15978
Clint Taylorc89e39f2016-05-13 23:41:21 +030015979 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015980 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015981 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015982 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15983 else
15984 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015985
15986 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15987 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15988 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015989 }
15990
15991 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015992
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015993 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015994
Ville Syrjälä78108b72016-05-27 20:59:19 +030015995 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15996 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015997 crtc->active ? "enabled" : "disabled");
15998 }
15999
Daniel Vetter53589012013-06-05 13:34:16 +020016000 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16001 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16002
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016003 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16004 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016005 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016006 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016007 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016008 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016009 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016010 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016011
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016012 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016013 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016014 }
16015
Damien Lespiaub2784e12014-08-05 11:29:37 +010016016 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016017 pipe = 0;
16018
16019 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016020 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16021 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016022 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016023 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016024 } else {
16025 encoder->base.crtc = NULL;
16026 }
16027
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016028 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016029 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016030 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016031 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016032 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016033 }
16034
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016035 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016036 if (connector->get_hw_state(connector)) {
16037 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016038
16039 encoder = connector->encoder;
16040 connector->base.encoder = &encoder->base;
16041
16042 if (encoder->base.crtc &&
16043 encoder->base.crtc->state->active) {
16044 /*
16045 * This has to be done during hardware readout
16046 * because anything calling .crtc_disable may
16047 * rely on the connector_mask being accurate.
16048 */
16049 encoder->base.crtc->state->connector_mask |=
16050 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016051 encoder->base.crtc->state->encoder_mask |=
16052 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016053 }
16054
Daniel Vetter24929352012-07-02 20:28:59 +020016055 } else {
16056 connector->base.dpms = DRM_MODE_DPMS_OFF;
16057 connector->base.encoder = NULL;
16058 }
16059 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16060 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016061 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016062 connector->base.encoder ? "enabled" : "disabled");
16063 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016064
16065 for_each_intel_crtc(dev, crtc) {
16066 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16067
16068 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16069 if (crtc->base.state->active) {
16070 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16071 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16072 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16073
16074 /*
16075 * The initial mode needs to be set in order to keep
16076 * the atomic core happy. It wants a valid mode if the
16077 * crtc's enabled, so we do the above call.
16078 *
16079 * At this point some state updated by the connectors
16080 * in their ->detect() callback has not run yet, so
16081 * no recalculation can be done yet.
16082 *
16083 * Even if we could do a recalculation and modeset
16084 * right now it would cause a double modeset if
16085 * fbdev or userspace chooses a different initial mode.
16086 *
16087 * If that happens, someone indicated they wanted a
16088 * mode change, which means it's safe to do a full
16089 * recalculation.
16090 */
16091 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016092
16093 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16094 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016095 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016096
16097 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016098 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016099}
16100
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016101/* Scan out the current hw modeset state,
16102 * and sanitizes it to the current state
16103 */
16104static void
16105intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016106{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016107 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016108 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016109 struct intel_crtc *crtc;
16110 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016111 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016112
16113 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016114
16115 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016116 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016117 intel_sanitize_encoder(encoder);
16118 }
16119
Damien Lespiau055e3932014-08-18 13:49:10 +010016120 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016121 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16122 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016123 intel_dump_pipe_config(crtc, crtc->config,
16124 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016125 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016126
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016127 intel_modeset_update_connector_atomic_state(dev);
16128
Daniel Vetter35c95372013-07-17 06:55:04 +020016129 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16130 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16131
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016132 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016133 continue;
16134
16135 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16136
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016137 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016138 pll->on = false;
16139 }
16140
Wayne Boyer666a4532015-12-09 12:29:35 -080016141 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016142 vlv_wm_get_hw_state(dev);
16143 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016144 skl_wm_get_hw_state(dev);
16145 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016146 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016147
16148 for_each_intel_crtc(dev, crtc) {
16149 unsigned long put_domains;
16150
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016151 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016152 if (WARN_ON(put_domains))
16153 modeset_put_power_domains(dev_priv, put_domains);
16154 }
16155 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016156
16157 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016158}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016159
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016160void intel_display_resume(struct drm_device *dev)
16161{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016162 struct drm_i915_private *dev_priv = to_i915(dev);
16163 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16164 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016165 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016166 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016167
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016168 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016169
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016170 /*
16171 * This is a cludge because with real atomic modeset mode_config.mutex
16172 * won't be taken. Unfortunately some probed state like
16173 * audio_codec_enable is still protected by mode_config.mutex, so lock
16174 * it here for now.
16175 */
16176 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016177 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016178
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016179retry:
16180 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016181
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016182 if (ret == 0 && !setup) {
16183 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016184
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016185 intel_modeset_setup_hw_state(dev);
16186 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016187 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016188
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016189 if (ret == 0 && state) {
16190 struct drm_crtc_state *crtc_state;
16191 struct drm_crtc *crtc;
16192 int i;
16193
16194 state->acquire_ctx = &ctx;
16195
Ville Syrjäläe3d54572016-05-13 10:10:42 -070016196 /* ignore any reset values/BIOS leftovers in the WM registers */
16197 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16198
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016199 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16200 /*
16201 * Force recalculation even if we restore
16202 * current state. With fast modeset this may not result
16203 * in a modeset when the state is compatible.
16204 */
16205 crtc_state->mode_changed = true;
16206 }
16207
16208 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016209 }
16210
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016211 if (ret == -EDEADLK) {
16212 drm_modeset_backoff(&ctx);
16213 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016214 }
16215
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016216 drm_modeset_drop_locks(&ctx);
16217 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016218 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016219
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016220 if (ret) {
16221 DRM_ERROR("Restoring old state failed with %i\n", ret);
16222 drm_atomic_state_free(state);
16223 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016224}
16225
16226void intel_modeset_gem_init(struct drm_device *dev)
16227{
Chris Wilsondc979972016-05-10 14:10:04 +010016228 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016229 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016230 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016231 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016232
Chris Wilsondc979972016-05-10 14:10:04 +010016233 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016234
Chris Wilson1833b132012-05-09 11:56:28 +010016235 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016236
Chris Wilson1ee8da62016-05-12 12:43:23 +010016237 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016238
16239 /*
16240 * Make sure any fbs we allocated at startup are properly
16241 * pinned & fenced. When we do the allocation it's too early
16242 * for this.
16243 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016244 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016245 obj = intel_fb_obj(c->primary->fb);
16246 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016247 continue;
16248
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016249 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016250 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16251 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016252 mutex_unlock(&dev->struct_mutex);
16253 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016254 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16255 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016256 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016257 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016258 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016259 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016260 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016261 }
16262 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016263}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016264
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016265int intel_connector_register(struct drm_connector *connector)
16266{
16267 struct intel_connector *intel_connector = to_intel_connector(connector);
16268 int ret;
16269
16270 ret = intel_backlight_device_register(intel_connector);
16271 if (ret)
16272 goto err;
16273
16274 return 0;
16275
16276err:
16277 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016278}
16279
Chris Wilsonc191eca2016-06-17 11:40:33 +010016280void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016281{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016282 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016283
Chris Wilsone63d87c2016-06-17 11:40:34 +010016284 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016285 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016286}
16287
Jesse Barnes79e53942008-11-07 14:24:08 -080016288void intel_modeset_cleanup(struct drm_device *dev)
16289{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016290 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070016291
Chris Wilsondc979972016-05-10 14:10:04 +010016292 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016293
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016294 /*
16295 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016296 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016297 * experience fancy races otherwise.
16298 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016299 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016300
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016301 /*
16302 * Due to the hpd irq storm handling the hotplug work can re-arm the
16303 * poll handlers. Hence disable polling after hpd handling is shut down.
16304 */
Keith Packardf87ea762010-10-03 19:36:26 -070016305 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016306
Jesse Barnes723bfd72010-10-07 16:01:13 -070016307 intel_unregister_dsm_handler();
16308
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016309 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016310
Chris Wilson1630fe72011-07-08 12:22:42 +010016311 /* flush any delayed tasks or pending work */
16312 flush_scheduled_work();
16313
Jesse Barnes79e53942008-11-07 14:24:08 -080016314 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016315
Chris Wilson1ee8da62016-05-12 12:43:23 +010016316 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016317
Chris Wilsondc979972016-05-10 14:10:04 +010016318 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016319
16320 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016321}
16322
Chris Wilsondf0e9242010-09-09 16:20:55 +010016323void intel_connector_attach_encoder(struct intel_connector *connector,
16324 struct intel_encoder *encoder)
16325{
16326 connector->encoder = encoder;
16327 drm_mode_connector_attach_encoder(&connector->base,
16328 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016329}
Dave Airlie28d52042009-09-21 14:33:58 +100016330
16331/*
16332 * set vga decode state - true == enable VGA decode
16333 */
16334int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16335{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016336 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000016337 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016338 u16 gmch_ctrl;
16339
Chris Wilson75fa0412014-02-07 18:37:02 -020016340 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16341 DRM_ERROR("failed to read control word\n");
16342 return -EIO;
16343 }
16344
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016345 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16346 return 0;
16347
Dave Airlie28d52042009-09-21 14:33:58 +100016348 if (state)
16349 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16350 else
16351 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016352
16353 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16354 DRM_ERROR("failed to write control word\n");
16355 return -EIO;
16356 }
16357
Dave Airlie28d52042009-09-21 14:33:58 +100016358 return 0;
16359}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016360
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016361struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016362
16363 u32 power_well_driver;
16364
Chris Wilson63b66e52013-08-08 15:12:06 +020016365 int num_transcoders;
16366
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016367 struct intel_cursor_error_state {
16368 u32 control;
16369 u32 position;
16370 u32 base;
16371 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016372 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016373
16374 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016375 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016376 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016377 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016378 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016379
16380 struct intel_plane_error_state {
16381 u32 control;
16382 u32 stride;
16383 u32 size;
16384 u32 pos;
16385 u32 addr;
16386 u32 surface;
16387 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016388 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016389
16390 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016391 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016392 enum transcoder cpu_transcoder;
16393
16394 u32 conf;
16395
16396 u32 htotal;
16397 u32 hblank;
16398 u32 hsync;
16399 u32 vtotal;
16400 u32 vblank;
16401 u32 vsync;
16402 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016403};
16404
16405struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016406intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016407{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016408 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016409 int transcoders[] = {
16410 TRANSCODER_A,
16411 TRANSCODER_B,
16412 TRANSCODER_C,
16413 TRANSCODER_EDP,
16414 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016415 int i;
16416
Chris Wilsonc0336662016-05-06 15:40:21 +010016417 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016418 return NULL;
16419
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016420 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016421 if (error == NULL)
16422 return NULL;
16423
Chris Wilsonc0336662016-05-06 15:40:21 +010016424 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016425 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16426
Damien Lespiau055e3932014-08-18 13:49:10 +010016427 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016428 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016429 __intel_display_power_is_enabled(dev_priv,
16430 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016431 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016432 continue;
16433
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016434 error->cursor[i].control = I915_READ(CURCNTR(i));
16435 error->cursor[i].position = I915_READ(CURPOS(i));
16436 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016437
16438 error->plane[i].control = I915_READ(DSPCNTR(i));
16439 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016440 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016441 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016442 error->plane[i].pos = I915_READ(DSPPOS(i));
16443 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016444 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016445 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016446 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016447 error->plane[i].surface = I915_READ(DSPSURF(i));
16448 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16449 }
16450
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016451 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016452
Chris Wilsonc0336662016-05-06 15:40:21 +010016453 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030016454 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016455 }
16456
Jani Nikula4d1de972016-03-18 17:05:42 +020016457 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016458 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016459 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016460 error->num_transcoders++; /* Account for eDP. */
16461
16462 for (i = 0; i < error->num_transcoders; i++) {
16463 enum transcoder cpu_transcoder = transcoders[i];
16464
Imre Deakddf9c532013-11-27 22:02:02 +020016465 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016466 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016467 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016468 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016469 continue;
16470
Chris Wilson63b66e52013-08-08 15:12:06 +020016471 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16472
16473 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16474 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16475 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16476 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16477 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16478 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16479 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016480 }
16481
16482 return error;
16483}
16484
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016485#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16486
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016487void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016488intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016489 struct drm_device *dev,
16490 struct intel_display_error_state *error)
16491{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016492 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016493 int i;
16494
Chris Wilson63b66e52013-08-08 15:12:06 +020016495 if (!error)
16496 return;
16497
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016498 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016499 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016500 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016501 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016502 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016503 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016504 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016505 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016506 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016507 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016508
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016509 err_printf(m, "Plane [%d]:\n", i);
16510 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16511 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016512 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016513 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16514 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016515 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016516 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016517 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016518 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016519 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16520 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016521 }
16522
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016523 err_printf(m, "Cursor [%d]:\n", i);
16524 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16525 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16526 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016527 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016528
16529 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016530 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016531 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016532 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016533 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016534 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16535 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16536 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16537 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16538 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16539 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16540 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16541 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016542}