blob: 1c34d4039d7bde94a9b5796ba0fedfafa0fe0008 [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"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Chris Wilsonc37efb92016-06-17 08:28:47 +010039#include "i915_gem_dmabuf.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200123static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300126static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100127
Ma Lingd4906092009-03-18 20:13:27 +0800128struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300155{
156 u32 val;
157 int divider;
158
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180}
181
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200184{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200186}
187
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300190{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300191 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194}
195
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
198{
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 uint32_t clkcfg;
200
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200219 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300220 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300222 }
223}
224
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300225void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
Wayne Boyer666a4532015-12-09 12:29:35 -0800241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
Chris Wilson021357a2010-09-07 20:54:59 +0100250static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100253{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200258 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200259 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100260}
261
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300262static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200264 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200265 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300275static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200276 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200277 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200278 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300288static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200290 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200291 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
Eric Anholt273e27c2011-03-30 13:01:10 -0700300
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300301static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700312};
313
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300314static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Eric Anholt273e27c2011-03-30 13:01:10 -0700327
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300328static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800340 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300343static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700354};
355
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800367 },
Keith Packarde4b36692009-06-05 19:22:17 -0700368};
369
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800381 },
Keith Packarde4b36692009-06-05 19:22:17 -0700382};
383
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300384static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700397};
398
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300399static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700410};
411
Eric Anholt273e27c2011-03-30 13:01:10 -0700412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300417static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700428};
429
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300430static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800441};
442
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800454};
455
Eric Anholt273e27c2011-03-30 13:01:10 -0700456/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800468};
469
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800481};
482
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300483static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200491 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300495 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700497};
498
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300499static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200507 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300515static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530518 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200530 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200531}
532
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300533/**
534 * Returns whether any output on the specified pipe is of the specified type
535 */
Damien Lespiau40935612014-10-29 11:16:59 +0000536bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300537{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300538 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300539 struct intel_encoder *encoder;
540
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300541 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300542 if (encoder->type == type)
543 return true;
544
545 return false;
546}
547
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200548/**
549 * Returns whether any output on the specified pipe will have the specified
550 * type after a staged modeset is complete, i.e., the same as
551 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
552 * encoder->crtc.
553 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
555 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200556{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200557 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300558 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200560 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200562
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300563 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200564 if (connector_state->crtc != crtc_state->base.crtc)
565 continue;
566
567 num_connectors++;
568
569 encoder = to_intel_encoder(connector_state->best_encoder);
570 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200571 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200572 }
573
574 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200575
576 return false;
577}
578
Imre Deakdccbea32015-06-22 23:35:51 +0300579/*
580 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
581 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
582 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
583 * The helpers' return value is the rate of the clock that is fed to the
584 * display engine's pipe which can be the above fast dot clock rate or a
585 * divided-down version of it.
586 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500587/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300588static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800589{
Shaohua Li21778322009-02-23 15:19:16 +0800590 clock->m = clock->m2 + 2;
591 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200592 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300593 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300594 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300596
597 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800598}
599
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200600static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
601{
602 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
603}
604
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800606{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200607 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200609 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300610 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300613
614 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800615}
616
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300617static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300622 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300623 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
624 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300625
626 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300627}
628
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300629int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300630{
631 clock->m = clock->m1 * clock->m2;
632 clock->p = clock->p1 * clock->p2;
633 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300634 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300635 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
636 clock->n << 22);
637 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300638
639 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300640}
641
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800642#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643/**
644 * Returns whether the given set of divisors are valid for a given refclk with
645 * the given connectors.
646 */
647
Chris Wilson1b894b52010-12-14 20:04:54 +0000648static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300649 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300650 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800651{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300652 if (clock->n < limit->n.min || limit->n.max < clock->n)
653 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400655 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400657 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400659 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300660
Wayne Boyer666a4532015-12-09 12:29:35 -0800661 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
662 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300663 if (clock->m1 <= clock->m2)
664 INTELPllInvalid("m1 <= m2\n");
665
Wayne Boyer666a4532015-12-09 12:29:35 -0800666 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300667 if (clock->p < limit->p.min || limit->p.max < clock->p)
668 INTELPllInvalid("p out of range\n");
669 if (clock->m < limit->m.min || limit->m.max < clock->m)
670 INTELPllInvalid("m out of range\n");
671 }
672
Jesse Barnes79e53942008-11-07 14:24:08 -0800673 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400674 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800675 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
676 * connector, etc., rather than just a single range.
677 */
678 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400679 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800680
681 return true;
682}
683
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300684static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300685i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 const struct intel_crtc_state *crtc_state,
687 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800688{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200691 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100693 * For LVDS just rely on its current settings for dual-channel.
694 * We haven't figured out how to reliably set up different
695 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800696 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100697 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300698 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300700 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 } else {
702 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300705 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300707}
708
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200709/*
710 * Returns a set of divisors for the desired target clock with the given
711 * refclk, or FALSE. The returned values represent the clock equation:
712 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
713 *
714 * Target and reference clocks are specified in kHz.
715 *
716 * If match_clock is provided, then best_clock P divider must match the P
717 * divider from @match_clock used for LVDS downclocking.
718 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300719static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300720i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300721 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300722 int target, int refclk, struct dpll *match_clock,
723 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300724{
725 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300726 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300727 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800728
Akshay Joshi0206e352011-08-16 15:34:10 -0400729 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800730
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300731 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
732
Zhao Yakui42158662009-11-20 11:24:18 +0800733 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
734 clock.m1++) {
735 for (clock.m2 = limit->m2.min;
736 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200737 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800738 break;
739 for (clock.n = limit->n.min;
740 clock.n <= limit->n.max; clock.n++) {
741 for (clock.p1 = limit->p1.min;
742 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 int this_err;
744
Imre Deakdccbea32015-06-22 23:35:51 +0300745 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000746 if (!intel_PLL_is_valid(dev, limit,
747 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800748 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800749 if (match_clock &&
750 clock.p != match_clock->p)
751 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800752
753 this_err = abs(clock.dot - target);
754 if (this_err < err) {
755 *best_clock = clock;
756 err = this_err;
757 }
758 }
759 }
760 }
761 }
762
763 return (err != target);
764}
765
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200766/*
767 * Returns a set of divisors for the desired target clock with the given
768 * refclk, or FALSE. The returned values represent the clock equation:
769 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
770 *
771 * Target and reference clocks are specified in kHz.
772 *
773 * If match_clock is provided, then best_clock P divider must match the P
774 * divider from @match_clock used for LVDS downclocking.
775 */
Ma Lingd4906092009-03-18 20:13:27 +0800776static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300777pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200778 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300779 int target, int refclk, struct dpll *match_clock,
780 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200781{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300782 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300783 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200784 int err = target;
785
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200786 memset(best_clock, 0, sizeof(*best_clock));
787
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300788 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
789
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
791 clock.m1++) {
792 for (clock.m2 = limit->m2.min;
793 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200794 for (clock.n = limit->n.min;
795 clock.n <= limit->n.max; clock.n++) {
796 for (clock.p1 = limit->p1.min;
797 clock.p1 <= limit->p1.max; clock.p1++) {
798 int this_err;
799
Imre Deakdccbea32015-06-22 23:35:51 +0300800 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800801 if (!intel_PLL_is_valid(dev, limit,
802 &clock))
803 continue;
804 if (match_clock &&
805 clock.p != match_clock->p)
806 continue;
807
808 this_err = abs(clock.dot - target);
809 if (this_err < err) {
810 *best_clock = clock;
811 err = this_err;
812 }
813 }
814 }
815 }
816 }
817
818 return (err != target);
819}
820
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200821/*
822 * Returns a set of divisors for the desired target clock with the given
823 * refclk, or FALSE. The returned values represent the clock equation:
824 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200825 *
826 * Target and reference clocks are specified in kHz.
827 *
828 * If match_clock is provided, then best_clock P divider must match the P
829 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200830 */
Ma Lingd4906092009-03-18 20:13:27 +0800831static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300832g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200833 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300834 int target, int refclk, struct dpll *match_clock,
835 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800836{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300837 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300838 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800839 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300840 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400841 /* approximately equals target * 0.00585 */
842 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800843
844 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300845
846 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
847
Ma Lingd4906092009-03-18 20:13:27 +0800848 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200849 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800850 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200851 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800852 for (clock.m1 = limit->m1.max;
853 clock.m1 >= limit->m1.min; clock.m1--) {
854 for (clock.m2 = limit->m2.max;
855 clock.m2 >= limit->m2.min; clock.m2--) {
856 for (clock.p1 = limit->p1.max;
857 clock.p1 >= limit->p1.min; clock.p1--) {
858 int this_err;
859
Imre Deakdccbea32015-06-22 23:35:51 +0300860 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000861 if (!intel_PLL_is_valid(dev, limit,
862 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800863 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000864
865 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800866 if (this_err < err_most) {
867 *best_clock = clock;
868 err_most = this_err;
869 max_n = clock.n;
870 found = true;
871 }
872 }
873 }
874 }
875 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800876 return found;
877}
Ma Lingd4906092009-03-18 20:13:27 +0800878
Imre Deakd5dd62b2015-03-17 11:40:03 +0200879/*
880 * Check if the calculated PLL configuration is more optimal compared to the
881 * best configuration and error found so far. Return the calculated error.
882 */
883static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300884 const struct dpll *calculated_clock,
885 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200886 unsigned int best_error_ppm,
887 unsigned int *error_ppm)
888{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200889 /*
890 * For CHV ignore the error and consider only the P value.
891 * Prefer a bigger P value based on HW requirements.
892 */
893 if (IS_CHERRYVIEW(dev)) {
894 *error_ppm = 0;
895
896 return calculated_clock->p > best_clock->p;
897 }
898
Imre Deak24be4e42015-03-17 11:40:04 +0200899 if (WARN_ON_ONCE(!target_freq))
900 return false;
901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902 *error_ppm = div_u64(1000000ULL *
903 abs(target_freq - calculated_clock->dot),
904 target_freq);
905 /*
906 * Prefer a better P value over a better (smaller) error if the error
907 * is small. Ensure this preference for future configurations too by
908 * setting the error to 0.
909 */
910 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
911 *error_ppm = 0;
912
913 return true;
914 }
915
916 return *error_ppm + 10 < best_error_ppm;
917}
918
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200919/*
920 * Returns a set of divisors for the desired target clock with the given
921 * refclk, or FALSE. The returned values represent the clock equation:
922 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
923 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800924static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300925vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200926 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300927 int target, int refclk, struct dpll *match_clock,
928 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300931 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300932 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300933 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300934 /* min update 19.2 MHz */
935 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300936 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300938 target *= 5; /* fast clock */
939
940 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700941
942 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300943 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300945 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300947 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700948 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300949 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200950 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300951
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
953 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300954
Imre Deakdccbea32015-06-22 23:35:51 +0300955 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300956
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300957 if (!intel_PLL_is_valid(dev, limit,
958 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300959 continue;
960
Imre Deakd5dd62b2015-03-17 11:40:03 +0200961 if (!vlv_PLL_is_optimal(dev, target,
962 &clock,
963 best_clock,
964 bestppm, &ppm))
965 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300966
Imre Deakd5dd62b2015-03-17 11:40:03 +0200967 *best_clock = clock;
968 bestppm = ppm;
969 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970 }
971 }
972 }
973 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700974
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300975 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700976}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700977
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200978/*
979 * Returns a set of divisors for the desired target clock with the given
980 * refclk, or FALSE. The returned values represent the clock equation:
981 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
982 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300983static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300984chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200985 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300986 int target, int refclk, struct dpll *match_clock,
987 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300988{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200989 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300990 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200991 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300992 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300993 uint64_t m2;
994 int found = false;
995
996 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200997 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300998
999 /*
1000 * Based on hardware doc, the n always set to 1, and m1 always
1001 * set to 2. If requires to support 200Mhz refclk, we need to
1002 * revisit this because n may not 1 anymore.
1003 */
1004 clock.n = 1, clock.m1 = 2;
1005 target *= 5; /* fast clock */
1006
1007 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1008 for (clock.p2 = limit->p2.p2_fast;
1009 clock.p2 >= limit->p2.p2_slow;
1010 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001011 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001012
1013 clock.p = clock.p1 * clock.p2;
1014
1015 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1016 clock.n) << 22, refclk * clock.m1);
1017
1018 if (m2 > INT_MAX/clock.m1)
1019 continue;
1020
1021 clock.m2 = m2;
1022
Imre Deakdccbea32015-06-22 23:35:51 +03001023 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001024
1025 if (!intel_PLL_is_valid(dev, limit, &clock))
1026 continue;
1027
Imre Deak9ca3ba02015-03-17 11:40:05 +02001028 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1029 best_error_ppm, &error_ppm))
1030 continue;
1031
1032 *best_clock = clock;
1033 best_error_ppm = error_ppm;
1034 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001035 }
1036 }
1037
1038 return found;
1039}
1040
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001041bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001042 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001043{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001044 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001045 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001046
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001047 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001048 target_clock, refclk, NULL, best_clock);
1049}
1050
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001051bool intel_crtc_active(struct drm_crtc *crtc)
1052{
1053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1054
1055 /* Be paranoid as we can arrive here with only partial
1056 * state retrieved from the hardware during setup.
1057 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001058 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001059 * as Haswell has gained clock readout/fastboot support.
1060 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001061 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001062 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001063 *
1064 * FIXME: The intel_crtc->active here should be switched to
1065 * crtc->state->active once we have proper CRTC states wired up
1066 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001067 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001068 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001069 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001070}
1071
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001072enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1073 enum pipe pipe)
1074{
1075 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1077
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001078 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001079}
1080
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001081static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1082{
1083 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001084 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001085 u32 line1, line2;
1086 u32 line_mask;
1087
1088 if (IS_GEN2(dev))
1089 line_mask = DSL_LINEMASK_GEN2;
1090 else
1091 line_mask = DSL_LINEMASK_GEN3;
1092
1093 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001094 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001095 line2 = I915_READ(reg) & line_mask;
1096
1097 return line1 == line2;
1098}
1099
Keith Packardab7ad7f2010-10-03 00:33:06 -07001100/*
1101 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001102 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001103 *
1104 * After disabling a pipe, we can't wait for vblank in the usual way,
1105 * spinning on the vblank interrupt status bit, since we won't actually
1106 * see an interrupt when the pipe is disabled.
1107 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001108 * On Gen4 and above:
1109 * wait for the pipe register state bit to turn off
1110 *
1111 * Otherwise:
1112 * wait for the display line value to settle (it usually
1113 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001114 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001115 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001116static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001117{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001118 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001119 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001120 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001121 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001122
Keith Packardab7ad7f2010-10-03 00:33:06 -07001123 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001124 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001125
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001127 if (intel_wait_for_register(dev_priv,
1128 reg, I965_PIPECONF_ACTIVE, 0,
1129 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001130 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001131 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001132 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001133 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001134 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001135 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001136}
1137
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001139void assert_pll(struct drm_i915_private *dev_priv,
1140 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142 u32 val;
1143 bool cur_state;
1144
Ville Syrjälä649636e2015-09-22 19:50:01 +03001145 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001146 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001147 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001149 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001150}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001151
Jani Nikula23538ef2013-08-27 15:12:22 +03001152/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001153void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001154{
1155 u32 val;
1156 bool cur_state;
1157
Ville Syrjäläa5805162015-05-26 20:42:30 +03001158 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001159 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001160 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001161
1162 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001163 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001164 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001165 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001166}
Jani Nikula23538ef2013-08-27 15:12:22 +03001167
Jesse Barnes040484a2011-01-03 12:14:26 -08001168static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1169 enum pipe pipe, bool state)
1170{
Jesse Barnes040484a2011-01-03 12:14:26 -08001171 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001172 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1173 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001174
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001175 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001176 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001177 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001178 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001179 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001180 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001181 cur_state = !!(val & FDI_TX_ENABLE);
1182 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001183 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001184 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001185 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001186}
1187#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1188#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1189
1190static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1191 enum pipe pipe, bool state)
1192{
Jesse Barnes040484a2011-01-03 12:14:26 -08001193 u32 val;
1194 bool cur_state;
1195
Ville Syrjälä649636e2015-09-22 19:50:01 +03001196 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001197 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001198 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001199 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001200 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001201}
1202#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1203#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1204
1205static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1206 enum pipe pipe)
1207{
Jesse Barnes040484a2011-01-03 12:14:26 -08001208 u32 val;
1209
1210 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001211 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001212 return;
1213
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001214 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001215 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001216 return;
1217
Ville Syrjälä649636e2015-09-22 19:50:01 +03001218 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001219 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 -08001220}
1221
Daniel Vetter55607e82013-06-16 21:42:39 +02001222void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1223 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001224{
Jesse Barnes040484a2011-01-03 12:14:26 -08001225 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001226 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001227
Ville Syrjälä649636e2015-09-22 19:50:01 +03001228 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001229 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001230 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001231 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001232 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001233}
1234
Daniel Vetterb680c372014-09-19 18:27:27 +02001235void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1236 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001237{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001238 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001239 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001240 u32 val;
1241 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001242 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001243
Jani Nikulabedd4db2014-08-22 15:04:13 +03001244 if (WARN_ON(HAS_DDI(dev)))
1245 return;
1246
1247 if (HAS_PCH_SPLIT(dev)) {
1248 u32 port_sel;
1249
Jesse Barnesea0760c2011-01-04 15:09:32 -08001250 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001251 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1252
1253 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1254 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1255 panel_pipe = PIPE_B;
1256 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001257 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001258 /* presumably write lock depends on pipe, not port select */
1259 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1260 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 } else {
1262 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001263 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1264 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001265 }
1266
1267 val = I915_READ(pp_reg);
1268 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001269 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001270 locked = false;
1271
Rob Clarke2c719b2014-12-15 13:56:32 -05001272 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001273 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001274 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001275}
1276
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001277static void assert_cursor(struct drm_i915_private *dev_priv,
1278 enum pipe pipe, bool state)
1279{
1280 struct drm_device *dev = dev_priv->dev;
1281 bool cur_state;
1282
Paulo Zanonid9d82082014-02-27 16:30:56 -03001283 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001284 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001285 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001286 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001287
Rob Clarke2c719b2014-12-15 13:56:32 -05001288 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001289 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001290 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001291}
1292#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1293#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1294
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001295void assert_pipe(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001297{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001298 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001299 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1300 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001301 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001302
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001303 /* if we need the pipe quirk it must be always on */
1304 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1305 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001306 state = true;
1307
Imre Deak4feed0e2016-02-12 18:55:14 +02001308 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1309 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001310 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001311 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001312
1313 intel_display_power_put(dev_priv, power_domain);
1314 } else {
1315 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001316 }
1317
Rob Clarke2c719b2014-12-15 13:56:32 -05001318 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001319 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001320 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321}
1322
Chris Wilson931872f2012-01-16 23:01:13 +00001323static void assert_plane(struct drm_i915_private *dev_priv,
1324 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001325{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001326 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001327 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001328
Ville Syrjälä649636e2015-09-22 19:50:01 +03001329 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001330 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001331 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001332 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001333 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001334}
1335
Chris Wilson931872f2012-01-16 23:01:13 +00001336#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1337#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1338
Jesse Barnesb24e7172011-01-04 15:09:30 -08001339static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1340 enum pipe pipe)
1341{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001342 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001343 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001344
Ville Syrjälä653e1022013-06-04 13:49:05 +03001345 /* Primary planes are fixed to pipes on gen4+ */
1346 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001347 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001348 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001349 "plane %c assertion failure, should be disabled but not\n",
1350 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001351 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001352 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001353
Jesse Barnesb24e7172011-01-04 15:09:30 -08001354 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001355 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001356 u32 val = I915_READ(DSPCNTR(i));
1357 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001358 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001359 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001360 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1361 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001362 }
1363}
1364
Jesse Barnes19332d72013-03-28 09:55:38 -07001365static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001368 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001369 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001370
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001371 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001372 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001373 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001374 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001375 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1376 sprite, pipe_name(pipe));
1377 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001378 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001379 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001380 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001381 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001382 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001383 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001384 }
1385 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001386 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001389 plane_name(pipe), pipe_name(pipe));
1390 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001391 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001393 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1394 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001395 }
1396}
1397
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001398static void assert_vblank_disabled(struct drm_crtc *crtc)
1399{
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001401 drm_crtc_vblank_put(crtc);
1402}
1403
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001404void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001406{
Jesse Barnes92f25842011-01-04 15:09:34 -08001407 u32 val;
1408 bool enabled;
1409
Ville Syrjälä649636e2015-09-22 19:50:01 +03001410 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001411 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001412 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001413 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1414 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001415}
1416
Keith Packard4e634382011-08-06 10:39:45 -07001417static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1418 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001419{
1420 if ((val & DP_PORT_EN) == 0)
1421 return false;
1422
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001423 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001424 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001425 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1426 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001427 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001428 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1429 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001430 } else {
1431 if ((val & DP_PIPE_MASK) != (pipe << 30))
1432 return false;
1433 }
1434 return true;
1435}
1436
Keith Packard1519b992011-08-06 10:35:34 -07001437static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1438 enum pipe pipe, u32 val)
1439{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001440 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001441 return false;
1442
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001443 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001444 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001445 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001446 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001447 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1448 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001449 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001450 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001451 return false;
1452 }
1453 return true;
1454}
1455
1456static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1457 enum pipe pipe, u32 val)
1458{
1459 if ((val & LVDS_PORT_EN) == 0)
1460 return false;
1461
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001462 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001463 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1464 return false;
1465 } else {
1466 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1467 return false;
1468 }
1469 return true;
1470}
1471
1472static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, u32 val)
1474{
1475 if ((val & ADPA_DAC_ENABLE) == 0)
1476 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001477 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001478 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1479 return false;
1480 } else {
1481 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1482 return false;
1483 }
1484 return true;
1485}
1486
Jesse Barnes291906f2011-02-02 12:28:03 -08001487static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001488 enum pipe pipe, i915_reg_t reg,
1489 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001490{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001491 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001492 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001493 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001494 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001495
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001496 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001497 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001498 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001499}
1500
1501static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001502 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001503{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001504 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001505 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001506 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001507 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001508
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001509 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001510 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001511 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001512}
1513
1514static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1515 enum pipe pipe)
1516{
Jesse Barnes291906f2011-02-02 12:28:03 -08001517 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001518
Keith Packardf0575e92011-07-25 22:12:43 -07001519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1521 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001522
Ville Syrjälä649636e2015-09-22 19:50:01 +03001523 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001524 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001525 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001526 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001527
Ville Syrjälä649636e2015-09-22 19:50:01 +03001528 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001529 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001530 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001531 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001532
Paulo Zanonie2debe92013-02-18 19:00:27 -03001533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1534 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1535 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001536}
1537
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001538static void _vlv_enable_pll(struct intel_crtc *crtc,
1539 const struct intel_crtc_state *pipe_config)
1540{
1541 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1542 enum pipe pipe = crtc->pipe;
1543
1544 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1545 POSTING_READ(DPLL(pipe));
1546 udelay(150);
1547
Chris Wilson2c30b432016-06-30 15:32:54 +01001548 if (intel_wait_for_register(dev_priv,
1549 DPLL(pipe),
1550 DPLL_LOCK_VLV,
1551 DPLL_LOCK_VLV,
1552 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001553 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1554}
1555
Ville Syrjäläd288f652014-10-28 13:20:22 +02001556static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001557 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001558{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001559 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001560 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001561
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001562 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001563
Daniel Vetter87442f72013-06-06 00:52:17 +02001564 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001565 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001566
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001567 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1568 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001569
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001570 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1571 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001572}
1573
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001574
1575static void _chv_enable_pll(struct intel_crtc *crtc,
1576 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001577{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001578 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001579 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001580 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581 u32 tmp;
1582
Ville Syrjäläa5805162015-05-26 20:42:30 +03001583 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001584
1585 /* Enable back the 10bit clock to display controller */
1586 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1587 tmp |= DPIO_DCLKP_EN;
1588 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1589
Ville Syrjälä54433e92015-05-26 20:42:31 +03001590 mutex_unlock(&dev_priv->sb_lock);
1591
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001592 /*
1593 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1594 */
1595 udelay(1);
1596
1597 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001598 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001599
1600 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001601 if (intel_wait_for_register(dev_priv,
1602 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1603 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001604 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001605}
1606
1607static void chv_enable_pll(struct intel_crtc *crtc,
1608 const struct intel_crtc_state *pipe_config)
1609{
1610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1611 enum pipe pipe = crtc->pipe;
1612
1613 assert_pipe_disabled(dev_priv, pipe);
1614
1615 /* PLL is protected by panel, make sure we can write it */
1616 assert_panel_unlocked(dev_priv, pipe);
1617
1618 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1619 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001620
Ville Syrjäläc2317752016-03-15 16:39:56 +02001621 if (pipe != PIPE_A) {
1622 /*
1623 * WaPixelRepeatModeFixForC0:chv
1624 *
1625 * DPLLCMD is AWOL. Use chicken bits to propagate
1626 * the value from DPLLBMD to either pipe B or C.
1627 */
1628 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1629 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1630 I915_WRITE(CBR4_VLV, 0);
1631 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1632
1633 /*
1634 * DPLLB VGA mode also seems to cause problems.
1635 * We should always have it disabled.
1636 */
1637 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1638 } else {
1639 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1640 POSTING_READ(DPLL_MD(pipe));
1641 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001642}
1643
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001644static int intel_num_dvo_pipes(struct drm_device *dev)
1645{
1646 struct intel_crtc *crtc;
1647 int count = 0;
1648
1649 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001650 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001651 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001652
1653 return count;
1654}
1655
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001657{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001658 struct drm_device *dev = crtc->base.dev;
1659 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001660 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001661 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001662
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001664
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001665 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 if (IS_MOBILE(dev) && !IS_I830(dev))
1667 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001668
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001669 /* Enable DVO 2x clock on both PLLs if necessary */
1670 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1671 /*
1672 * It appears to be important that we don't enable this
1673 * for the current pipe before otherwise configuring the
1674 * PLL. No idea how this should be handled if multiple
1675 * DVO outputs are enabled simultaneosly.
1676 */
1677 dpll |= DPLL_DVO_2X_MODE;
1678 I915_WRITE(DPLL(!crtc->pipe),
1679 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1680 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001681
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001682 /*
1683 * Apparently we need to have VGA mode enabled prior to changing
1684 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1685 * dividers, even though the register value does change.
1686 */
1687 I915_WRITE(reg, 0);
1688
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001689 I915_WRITE(reg, dpll);
1690
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001691 /* Wait for the clocks to stabilize. */
1692 POSTING_READ(reg);
1693 udelay(150);
1694
1695 if (INTEL_INFO(dev)->gen >= 4) {
1696 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001697 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001698 } else {
1699 /* The pixel multiplier can only be updated once the
1700 * DPLL is enabled and the clocks are stable.
1701 *
1702 * So write it again.
1703 */
1704 I915_WRITE(reg, dpll);
1705 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001706
1707 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001708 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709 POSTING_READ(reg);
1710 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001711 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001712 POSTING_READ(reg);
1713 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001714 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001715 POSTING_READ(reg);
1716 udelay(150); /* wait for warmup */
1717}
1718
1719/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001720 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001721 * @dev_priv: i915 private structure
1722 * @pipe: pipe PLL to disable
1723 *
1724 * Disable the PLL for @pipe, making sure the pipe is off first.
1725 *
1726 * Note! This is for pre-ILK only.
1727 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001728static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001729{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001730 struct drm_device *dev = crtc->base.dev;
1731 struct drm_i915_private *dev_priv = dev->dev_private;
1732 enum pipe pipe = crtc->pipe;
1733
1734 /* Disable DVO 2x clock on both PLLs if necessary */
1735 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001736 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001737 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001738 I915_WRITE(DPLL(PIPE_B),
1739 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1740 I915_WRITE(DPLL(PIPE_A),
1741 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1742 }
1743
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001744 /* Don't disable pipe or pipe PLLs if needed */
1745 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1746 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747 return;
1748
1749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
1751
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001752 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001753 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001754}
1755
Jesse Barnesf6071162013-10-01 10:41:38 -07001756static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1757{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001758 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001759
1760 /* Make sure the pipe isn't still relying on us */
1761 assert_pipe_disabled(dev_priv, pipe);
1762
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001763 val = DPLL_INTEGRATED_REF_CLK_VLV |
1764 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1765 if (pipe != PIPE_A)
1766 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1767
Jesse Barnesf6071162013-10-01 10:41:38 -07001768 I915_WRITE(DPLL(pipe), val);
1769 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001770}
1771
1772static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1773{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001774 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001775 u32 val;
1776
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001777 /* Make sure the pipe isn't still relying on us */
1778 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001779
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001780 val = DPLL_SSC_REF_CLK_CHV |
1781 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001782 if (pipe != PIPE_A)
1783 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001784
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001785 I915_WRITE(DPLL(pipe), val);
1786 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001787
Ville Syrjäläa5805162015-05-26 20:42:30 +03001788 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001789
1790 /* Disable 10bit clock to display controller */
1791 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1792 val &= ~DPIO_DCLKP_EN;
1793 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1794
Ville Syrjäläa5805162015-05-26 20:42:30 +03001795 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001796}
1797
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001798void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001799 struct intel_digital_port *dport,
1800 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001801{
1802 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001803 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001804
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001805 switch (dport->port) {
1806 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001807 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001808 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001809 break;
1810 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001811 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001812 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001813 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001814 break;
1815 case PORT_D:
1816 port_mask = DPLL_PORTD_READY_MASK;
1817 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001818 break;
1819 default:
1820 BUG();
1821 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001822
Chris Wilson370004d2016-06-30 15:32:56 +01001823 if (intel_wait_for_register(dev_priv,
1824 dpll_reg, port_mask, expected_mask,
1825 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001826 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1827 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001828}
1829
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001830static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1831 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001832{
Daniel Vetter23670b322012-11-01 09:15:30 +01001833 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001834 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001836 i915_reg_t reg;
1837 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001838
Jesse Barnes040484a2011-01-03 12:14:26 -08001839 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001840 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001841
1842 /* FDI must be feeding us bits for PCH ports */
1843 assert_fdi_tx_enabled(dev_priv, pipe);
1844 assert_fdi_rx_enabled(dev_priv, pipe);
1845
Daniel Vetter23670b322012-11-01 09:15:30 +01001846 if (HAS_PCH_CPT(dev)) {
1847 /* Workaround: Set the timing override bit before enabling the
1848 * pch transcoder. */
1849 reg = TRANS_CHICKEN2(pipe);
1850 val = I915_READ(reg);
1851 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1852 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001853 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001854
Daniel Vetterab9412b2013-05-03 11:49:46 +02001855 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001856 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001857 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001858
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001859 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001860 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001861 * Make the BPC in transcoder be consistent with
1862 * that in pipeconf reg. For HDMI we must use 8bpc
1863 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001864 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001865 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001866 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1867 val |= PIPECONF_8BPC;
1868 else
1869 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001870 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001871
1872 val &= ~TRANS_INTERLACE_MASK;
1873 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001874 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001875 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001876 val |= TRANS_LEGACY_INTERLACED_ILK;
1877 else
1878 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001879 else
1880 val |= TRANS_PROGRESSIVE;
1881
Jesse Barnes040484a2011-01-03 12:14:26 -08001882 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001883 if (intel_wait_for_register(dev_priv,
1884 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1885 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001886 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001887}
1888
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001889static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001890 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001891{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001892 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001893
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001894 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001895 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001896 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001897
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001898 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001899 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001900 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001901 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001902
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001903 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001904 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001905
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001906 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1907 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001908 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001909 else
1910 val |= TRANS_PROGRESSIVE;
1911
Daniel Vetterab9412b2013-05-03 11:49:46 +02001912 I915_WRITE(LPT_TRANSCONF, val);
1913 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001914 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001915}
1916
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001917static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1918 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001919{
Daniel Vetter23670b322012-11-01 09:15:30 +01001920 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001921 i915_reg_t reg;
1922 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001923
1924 /* FDI relies on the transcoder */
1925 assert_fdi_tx_disabled(dev_priv, pipe);
1926 assert_fdi_rx_disabled(dev_priv, pipe);
1927
Jesse Barnes291906f2011-02-02 12:28:03 -08001928 /* Ports must be off as well */
1929 assert_pch_ports_disabled(dev_priv, pipe);
1930
Daniel Vetterab9412b2013-05-03 11:49:46 +02001931 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001932 val = I915_READ(reg);
1933 val &= ~TRANS_ENABLE;
1934 I915_WRITE(reg, val);
1935 /* wait for PCH transcoder off, transcoder state */
1936 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001937 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001938
Ville Syrjäläc4656132015-10-29 21:25:56 +02001939 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001940 /* Workaround: Clear the timing override chicken bit again. */
1941 reg = TRANS_CHICKEN2(pipe);
1942 val = I915_READ(reg);
1943 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1944 I915_WRITE(reg, val);
1945 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001946}
1947
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001948static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001949{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001950 u32 val;
1951
Daniel Vetterab9412b2013-05-03 11:49:46 +02001952 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001953 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001954 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001955 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001956 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001957 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001958
1959 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001960 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001961 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001962 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001963}
1964
1965/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001966 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001967 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001968 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001969 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001970 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001971 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001972static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001973{
Paulo Zanoni03722642014-01-17 13:51:09 -02001974 struct drm_device *dev = crtc->base.dev;
1975 struct drm_i915_private *dev_priv = dev->dev_private;
1976 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001977 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001978 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001979 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001980 u32 val;
1981
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001982 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1983
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001984 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001985 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001986 assert_sprites_disabled(dev_priv, pipe);
1987
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001988 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001989 pch_transcoder = TRANSCODER_A;
1990 else
1991 pch_transcoder = pipe;
1992
Jesse Barnesb24e7172011-01-04 15:09:30 -08001993 /*
1994 * A pipe without a PLL won't actually be able to drive bits from
1995 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1996 * need the check.
1997 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001998 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001999 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03002000 assert_dsi_pll_enabled(dev_priv);
2001 else
2002 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002003 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002004 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002005 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002006 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002007 assert_fdi_tx_pll_enabled(dev_priv,
2008 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002009 }
2010 /* FIXME: assert CPU port conditions for SNB+ */
2011 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002012
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002013 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002014 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002015 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002016 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2017 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002018 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002019 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002020
2021 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002022 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002023
2024 /*
2025 * Until the pipe starts DSL will read as 0, which would cause
2026 * an apparent vblank timestamp jump, which messes up also the
2027 * frame count when it's derived from the timestamps. So let's
2028 * wait for the pipe to start properly before we call
2029 * drm_crtc_vblank_on()
2030 */
2031 if (dev->max_vblank_count == 0 &&
2032 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2033 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002034}
2035
2036/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002037 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002038 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002039 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002040 * Disable the pipe of @crtc, making sure that various hardware
2041 * specific requirements are met, if applicable, e.g. plane
2042 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002043 *
2044 * Will wait until the pipe has shut down before returning.
2045 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002046static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002048 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002049 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002050 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002051 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002052 u32 val;
2053
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002054 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2055
Jesse Barnesb24e7172011-01-04 15:09:30 -08002056 /*
2057 * Make sure planes won't keep trying to pump pixels to us,
2058 * or we might hang the display.
2059 */
2060 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002061 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002062 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002063
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002064 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002065 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002066 if ((val & PIPECONF_ENABLE) == 0)
2067 return;
2068
Ville Syrjälä67adc642014-08-15 01:21:57 +03002069 /*
2070 * Double wide has implications for planes
2071 * so best keep it disabled when not needed.
2072 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002073 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002074 val &= ~PIPECONF_DOUBLE_WIDE;
2075
2076 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002077 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2078 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002079 val &= ~PIPECONF_ENABLE;
2080
2081 I915_WRITE(reg, val);
2082 if ((val & PIPECONF_ENABLE) == 0)
2083 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002084}
2085
Chris Wilson693db182013-03-05 14:52:39 +00002086static bool need_vtd_wa(struct drm_device *dev)
2087{
2088#ifdef CONFIG_INTEL_IOMMU
2089 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2090 return true;
2091#endif
2092 return false;
2093}
2094
Ville Syrjälä832be822016-01-12 21:08:33 +02002095static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2096{
2097 return IS_GEN2(dev_priv) ? 2048 : 4096;
2098}
2099
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002100static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2101 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002102{
2103 switch (fb_modifier) {
2104 case DRM_FORMAT_MOD_NONE:
2105 return cpp;
2106 case I915_FORMAT_MOD_X_TILED:
2107 if (IS_GEN2(dev_priv))
2108 return 128;
2109 else
2110 return 512;
2111 case I915_FORMAT_MOD_Y_TILED:
2112 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2113 return 128;
2114 else
2115 return 512;
2116 case I915_FORMAT_MOD_Yf_TILED:
2117 switch (cpp) {
2118 case 1:
2119 return 64;
2120 case 2:
2121 case 4:
2122 return 128;
2123 case 8:
2124 case 16:
2125 return 256;
2126 default:
2127 MISSING_CASE(cpp);
2128 return cpp;
2129 }
2130 break;
2131 default:
2132 MISSING_CASE(fb_modifier);
2133 return cpp;
2134 }
2135}
2136
Ville Syrjälä832be822016-01-12 21:08:33 +02002137unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2138 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002139{
Ville Syrjälä832be822016-01-12 21:08:33 +02002140 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2141 return 1;
2142 else
2143 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002144 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002145}
2146
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002147/* Return the tile dimensions in pixel units */
2148static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2149 unsigned int *tile_width,
2150 unsigned int *tile_height,
2151 uint64_t fb_modifier,
2152 unsigned int cpp)
2153{
2154 unsigned int tile_width_bytes =
2155 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2156
2157 *tile_width = tile_width_bytes / cpp;
2158 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2159}
2160
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002161unsigned int
2162intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002163 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002164{
Ville Syrjälä832be822016-01-12 21:08:33 +02002165 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2166 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2167
2168 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002169}
2170
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002171unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2172{
2173 unsigned int size = 0;
2174 int i;
2175
2176 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2177 size += rot_info->plane[i].width * rot_info->plane[i].height;
2178
2179 return size;
2180}
2181
Daniel Vetter75c82a52015-10-14 16:51:04 +02002182static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002183intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2184 const struct drm_framebuffer *fb,
2185 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002186{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002187 if (intel_rotation_90_or_270(rotation)) {
2188 *view = i915_ggtt_view_rotated;
2189 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2190 } else {
2191 *view = i915_ggtt_view_normal;
2192 }
2193}
2194
2195static void
2196intel_fill_fb_info(struct drm_i915_private *dev_priv,
2197 struct drm_framebuffer *fb)
2198{
2199 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002200 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002201
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002202 tile_size = intel_tile_size(dev_priv);
2203
2204 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002205 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2206 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002207
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002208 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2209 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002210
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002211 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002212 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002213 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2214 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002215
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002216 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002217 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2218 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002219 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002220}
2221
Ville Syrjälä603525d2016-01-12 21:08:37 +02002222static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002223{
2224 if (INTEL_INFO(dev_priv)->gen >= 9)
2225 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002226 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002227 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002228 return 128 * 1024;
2229 else if (INTEL_INFO(dev_priv)->gen >= 4)
2230 return 4 * 1024;
2231 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002232 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002233}
2234
Ville Syrjälä603525d2016-01-12 21:08:37 +02002235static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2236 uint64_t fb_modifier)
2237{
2238 switch (fb_modifier) {
2239 case DRM_FORMAT_MOD_NONE:
2240 return intel_linear_alignment(dev_priv);
2241 case I915_FORMAT_MOD_X_TILED:
2242 if (INTEL_INFO(dev_priv)->gen >= 9)
2243 return 256 * 1024;
2244 return 0;
2245 case I915_FORMAT_MOD_Y_TILED:
2246 case I915_FORMAT_MOD_Yf_TILED:
2247 return 1 * 1024 * 1024;
2248 default:
2249 MISSING_CASE(fb_modifier);
2250 return 0;
2251 }
2252}
2253
Chris Wilson127bd2a2010-07-23 23:32:05 +01002254int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002255intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2256 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002257{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002258 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002259 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002260 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002261 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002262 u32 alignment;
2263 int ret;
2264
Matt Roperebcdd392014-07-09 16:22:11 -07002265 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2266
Ville Syrjälä603525d2016-01-12 21:08:37 +02002267 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002268
Ville Syrjälä3465c582016-02-15 22:54:43 +02002269 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002270
Chris Wilson693db182013-03-05 14:52:39 +00002271 /* Note that the w/a also requires 64 PTE of padding following the
2272 * bo. We currently fill all unused PTE with the shadow page and so
2273 * we should always have valid PTE following the scanout preventing
2274 * the VT-d warning.
2275 */
2276 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2277 alignment = 256 * 1024;
2278
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002279 /*
2280 * Global gtt pte registers are special registers which actually forward
2281 * writes to a chunk of system memory. Which means that there is no risk
2282 * that the register values disappear as soon as we call
2283 * intel_runtime_pm_put(), so it is correct to wrap only the
2284 * pin/unpin/fence and not more.
2285 */
2286 intel_runtime_pm_get(dev_priv);
2287
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002288 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2289 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002290 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002291 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002292
2293 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2294 * fence, whereas 965+ only requires a fence if using
2295 * framebuffer compression. For simplicity, we always install
2296 * a fence as the cost is not that onerous.
2297 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002298 if (view.type == I915_GGTT_VIEW_NORMAL) {
2299 ret = i915_gem_object_get_fence(obj);
2300 if (ret == -EDEADLK) {
2301 /*
2302 * -EDEADLK means there are no free fences
2303 * no pending flips.
2304 *
2305 * This is propagated to atomic, but it uses
2306 * -EDEADLK to force a locking recovery, so
2307 * change the returned error to -EBUSY.
2308 */
2309 ret = -EBUSY;
2310 goto err_unpin;
2311 } else if (ret)
2312 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002313
Vivek Kasireddy98072162015-10-29 18:54:38 -07002314 i915_gem_object_pin_fence(obj);
2315 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002316
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002317 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002318 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002319
2320err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002321 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002322err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002323 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002324 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002325}
2326
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002327void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002328{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002329 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002330 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002331
Matt Roperebcdd392014-07-09 16:22:11 -07002332 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2333
Ville Syrjälä3465c582016-02-15 22:54:43 +02002334 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002335
Vivek Kasireddy98072162015-10-29 18:54:38 -07002336 if (view.type == I915_GGTT_VIEW_NORMAL)
2337 i915_gem_object_unpin_fence(obj);
2338
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002339 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002340}
2341
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002342/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002343 * Adjust the tile offset by moving the difference into
2344 * the x/y offsets.
2345 *
2346 * Input tile dimensions and pitch must already be
2347 * rotated to match x and y, and in pixel units.
2348 */
2349static u32 intel_adjust_tile_offset(int *x, int *y,
2350 unsigned int tile_width,
2351 unsigned int tile_height,
2352 unsigned int tile_size,
2353 unsigned int pitch_tiles,
2354 u32 old_offset,
2355 u32 new_offset)
2356{
2357 unsigned int tiles;
2358
2359 WARN_ON(old_offset & (tile_size - 1));
2360 WARN_ON(new_offset & (tile_size - 1));
2361 WARN_ON(new_offset > old_offset);
2362
2363 tiles = (old_offset - new_offset) / tile_size;
2364
2365 *y += tiles / pitch_tiles * tile_height;
2366 *x += tiles % pitch_tiles * tile_width;
2367
2368 return new_offset;
2369}
2370
2371/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002372 * Computes the linear offset to the base tile and adjusts
2373 * x, y. bytes per pixel is assumed to be a power-of-two.
2374 *
2375 * In the 90/270 rotated case, x and y are assumed
2376 * to be already rotated to match the rotated GTT view, and
2377 * pitch is the tile_height aligned framebuffer height.
2378 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002379u32 intel_compute_tile_offset(int *x, int *y,
2380 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002381 unsigned int pitch,
2382 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002383{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002384 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2385 uint64_t fb_modifier = fb->modifier[plane];
2386 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002387 u32 offset, offset_aligned, alignment;
2388
2389 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2390 if (alignment)
2391 alignment--;
2392
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002393 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002394 unsigned int tile_size, tile_width, tile_height;
2395 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002396
Ville Syrjäläd8433102016-01-12 21:08:35 +02002397 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002398 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2399 fb_modifier, cpp);
2400
2401 if (intel_rotation_90_or_270(rotation)) {
2402 pitch_tiles = pitch / tile_height;
2403 swap(tile_width, tile_height);
2404 } else {
2405 pitch_tiles = pitch / (tile_width * cpp);
2406 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002407
Ville Syrjäläd8433102016-01-12 21:08:35 +02002408 tile_rows = *y / tile_height;
2409 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002410
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002411 tiles = *x / tile_width;
2412 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002413
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002414 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2415 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002416
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002417 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2418 tile_size, pitch_tiles,
2419 offset, offset_aligned);
2420 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002421 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002422 offset_aligned = offset & ~alignment;
2423
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002424 *y = (offset & alignment) / pitch;
2425 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002426 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002427
2428 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002429}
2430
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002431static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002432{
2433 switch (format) {
2434 case DISPPLANE_8BPP:
2435 return DRM_FORMAT_C8;
2436 case DISPPLANE_BGRX555:
2437 return DRM_FORMAT_XRGB1555;
2438 case DISPPLANE_BGRX565:
2439 return DRM_FORMAT_RGB565;
2440 default:
2441 case DISPPLANE_BGRX888:
2442 return DRM_FORMAT_XRGB8888;
2443 case DISPPLANE_RGBX888:
2444 return DRM_FORMAT_XBGR8888;
2445 case DISPPLANE_BGRX101010:
2446 return DRM_FORMAT_XRGB2101010;
2447 case DISPPLANE_RGBX101010:
2448 return DRM_FORMAT_XBGR2101010;
2449 }
2450}
2451
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002452static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2453{
2454 switch (format) {
2455 case PLANE_CTL_FORMAT_RGB_565:
2456 return DRM_FORMAT_RGB565;
2457 default:
2458 case PLANE_CTL_FORMAT_XRGB_8888:
2459 if (rgb_order) {
2460 if (alpha)
2461 return DRM_FORMAT_ABGR8888;
2462 else
2463 return DRM_FORMAT_XBGR8888;
2464 } else {
2465 if (alpha)
2466 return DRM_FORMAT_ARGB8888;
2467 else
2468 return DRM_FORMAT_XRGB8888;
2469 }
2470 case PLANE_CTL_FORMAT_XRGB_2101010:
2471 if (rgb_order)
2472 return DRM_FORMAT_XBGR2101010;
2473 else
2474 return DRM_FORMAT_XRGB2101010;
2475 }
2476}
2477
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002478static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002479intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2480 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481{
2482 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002483 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002484 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485 struct drm_i915_gem_object *obj = NULL;
2486 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002487 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002488 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2489 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2490 PAGE_SIZE);
2491
2492 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002493
Chris Wilsonff2652e2014-03-10 08:07:02 +00002494 if (plane_config->size == 0)
2495 return false;
2496
Paulo Zanoni3badb492015-09-23 12:52:23 -03002497 /* If the FB is too big, just don't use it since fbdev is not very
2498 * important and we should probably use that space with FBC or other
2499 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002500 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002501 return false;
2502
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002503 mutex_lock(&dev->struct_mutex);
2504
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002505 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2506 base_aligned,
2507 base_aligned,
2508 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002509 if (!obj) {
2510 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002511 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002512 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002513
Damien Lespiau49af4492015-01-20 12:51:44 +00002514 obj->tiling_mode = plane_config->tiling;
2515 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002516 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002517
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002518 mode_cmd.pixel_format = fb->pixel_format;
2519 mode_cmd.width = fb->width;
2520 mode_cmd.height = fb->height;
2521 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002522 mode_cmd.modifier[0] = fb->modifier[0];
2523 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002524
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002525 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002526 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002527 DRM_DEBUG_KMS("intel fb init failed\n");
2528 goto out_unref_obj;
2529 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002530
Jesse Barnes46f297f2014-03-07 08:57:48 -08002531 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002532
Daniel Vetterf6936e22015-03-26 12:17:05 +01002533 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002535
2536out_unref_obj:
2537 drm_gem_object_unreference(&obj->base);
2538 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002539 return false;
2540}
2541
Daniel Vetter5a21b662016-05-24 17:13:53 +02002542/* Update plane->state->fb to match plane->fb after driver-internal updates */
2543static void
2544update_state_fb(struct drm_plane *plane)
2545{
2546 if (plane->fb == plane->state->fb)
2547 return;
2548
2549 if (plane->state->fb)
2550 drm_framebuffer_unreference(plane->state->fb);
2551 plane->state->fb = plane->fb;
2552 if (plane->state->fb)
2553 drm_framebuffer_reference(plane->state->fb);
2554}
2555
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002556static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002557intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2558 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559{
2560 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002561 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562 struct drm_crtc *c;
2563 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002564 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002565 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002566 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002567 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2568 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002569 struct intel_plane_state *intel_state =
2570 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002571 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002572
Damien Lespiau2d140302015-02-05 17:22:18 +00002573 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 return;
2575
Daniel Vetterf6936e22015-03-26 12:17:05 +01002576 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002577 fb = &plane_config->fb->base;
2578 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002579 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002580
Damien Lespiau2d140302015-02-05 17:22:18 +00002581 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002582
2583 /*
2584 * Failed to alloc the obj, check to see if we should share
2585 * an fb with another CRTC instead
2586 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002587 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002588 i = to_intel_crtc(c);
2589
2590 if (c == &intel_crtc->base)
2591 continue;
2592
Matt Roper2ff8fde2014-07-08 07:50:07 -07002593 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594 continue;
2595
Daniel Vetter88595ac2015-03-26 12:42:24 +01002596 fb = c->primary->fb;
2597 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002598 continue;
2599
Daniel Vetter88595ac2015-03-26 12:42:24 +01002600 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002601 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002602 drm_framebuffer_reference(fb);
2603 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002604 }
2605 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002606
Matt Roper200757f2015-12-03 11:37:36 -08002607 /*
2608 * We've failed to reconstruct the BIOS FB. Current display state
2609 * indicates that the primary plane is visible, but has a NULL FB,
2610 * which will lead to problems later if we don't fix it up. The
2611 * simplest solution is to just disable the primary plane now and
2612 * pretend the BIOS never had it enabled.
2613 */
2614 to_intel_plane_state(plane_state)->visible = false;
2615 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002616 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002617 intel_plane->disable_plane(primary, &intel_crtc->base);
2618
Daniel Vetter88595ac2015-03-26 12:42:24 +01002619 return;
2620
2621valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002622 plane_state->src_x = 0;
2623 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002624 plane_state->src_w = fb->width << 16;
2625 plane_state->src_h = fb->height << 16;
2626
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002627 plane_state->crtc_x = 0;
2628 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002629 plane_state->crtc_w = fb->width;
2630 plane_state->crtc_h = fb->height;
2631
Matt Roper0a8d8a82015-12-03 11:37:38 -08002632 intel_state->src.x1 = plane_state->src_x;
2633 intel_state->src.y1 = plane_state->src_y;
2634 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2635 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2636 intel_state->dst.x1 = plane_state->crtc_x;
2637 intel_state->dst.y1 = plane_state->crtc_y;
2638 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2639 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2640
Daniel Vetter88595ac2015-03-26 12:42:24 +01002641 obj = intel_fb_obj(fb);
2642 if (obj->tiling_mode != I915_TILING_NONE)
2643 dev_priv->preserve_bios_swizzle = true;
2644
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002645 drm_framebuffer_reference(fb);
2646 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002647 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002648 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002649 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002650}
2651
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002652static void i9xx_update_primary_plane(struct drm_plane *primary,
2653 const struct intel_crtc_state *crtc_state,
2654 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002655{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002656 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002657 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2659 struct drm_framebuffer *fb = plane_state->base.fb;
2660 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002661 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002662 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002663 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002664 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002665 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002666 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002667 int x = plane_state->src.x1 >> 16;
2668 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002669
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002670 dspcntr = DISPPLANE_GAMMA_ENABLE;
2671
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002672 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002673
2674 if (INTEL_INFO(dev)->gen < 4) {
2675 if (intel_crtc->pipe == PIPE_B)
2676 dspcntr |= DISPPLANE_SEL_PIPE_B;
2677
2678 /* pipesrc and dspsize control the size that is scaled from,
2679 * which should always be the user's requested size.
2680 */
2681 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002682 ((crtc_state->pipe_src_h - 1) << 16) |
2683 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002684 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002685 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2686 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002687 ((crtc_state->pipe_src_h - 1) << 16) |
2688 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002689 I915_WRITE(PRIMPOS(plane), 0);
2690 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002691 }
2692
Ville Syrjälä57779d02012-10-31 17:50:14 +02002693 switch (fb->pixel_format) {
2694 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002695 dspcntr |= DISPPLANE_8BPP;
2696 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002697 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002699 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002700 case DRM_FORMAT_RGB565:
2701 dspcntr |= DISPPLANE_BGRX565;
2702 break;
2703 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002704 dspcntr |= DISPPLANE_BGRX888;
2705 break;
2706 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 dspcntr |= DISPPLANE_RGBX888;
2708 break;
2709 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002710 dspcntr |= DISPPLANE_BGRX101010;
2711 break;
2712 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002713 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002714 break;
2715 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002716 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002717 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002718
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002719 if (INTEL_INFO(dev)->gen >= 4 &&
2720 obj->tiling_mode != I915_TILING_NONE)
2721 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002722
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002723 if (IS_G4X(dev))
2724 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2725
Ville Syrjäläac484962016-01-20 21:05:26 +02002726 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002727
Daniel Vetterc2c75132012-07-05 12:17:30 +02002728 if (INTEL_INFO(dev)->gen >= 4) {
2729 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002730 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002731 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002732 linear_offset -= intel_crtc->dspaddr_offset;
2733 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002734 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002735 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002736
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002737 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302738 dspcntr |= DISPPLANE_ROTATE_180;
2739
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002740 x += (crtc_state->pipe_src_w - 1);
2741 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302742
2743 /* Finding the last pixel of the last line of the display
2744 data and adding to linear_offset*/
2745 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002746 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002747 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302748 }
2749
Paulo Zanoni2db33662015-09-14 15:20:03 -03002750 intel_crtc->adjusted_x = x;
2751 intel_crtc->adjusted_y = y;
2752
Sonika Jindal48404c12014-08-22 14:06:04 +05302753 I915_WRITE(reg, dspcntr);
2754
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002755 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002756 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002757 I915_WRITE(DSPSURF(plane),
2758 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002759 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002760 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002761 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002762 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002763 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002764}
2765
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002766static void i9xx_disable_primary_plane(struct drm_plane *primary,
2767 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002768{
2769 struct drm_device *dev = crtc->dev;
2770 struct drm_i915_private *dev_priv = dev->dev_private;
2771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002772 int plane = intel_crtc->plane;
2773
2774 I915_WRITE(DSPCNTR(plane), 0);
2775 if (INTEL_INFO(dev_priv)->gen >= 4)
2776 I915_WRITE(DSPSURF(plane), 0);
2777 else
2778 I915_WRITE(DSPADDR(plane), 0);
2779 POSTING_READ(DSPCNTR(plane));
2780}
2781
2782static void ironlake_update_primary_plane(struct drm_plane *primary,
2783 const struct intel_crtc_state *crtc_state,
2784 const struct intel_plane_state *plane_state)
2785{
2786 struct drm_device *dev = primary->dev;
2787 struct drm_i915_private *dev_priv = dev->dev_private;
2788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2789 struct drm_framebuffer *fb = plane_state->base.fb;
2790 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002791 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002792 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002793 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002794 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002795 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002796 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002797 int x = plane_state->src.x1 >> 16;
2798 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002799
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002800 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002801 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002802
2803 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2804 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2805
Ville Syrjälä57779d02012-10-31 17:50:14 +02002806 switch (fb->pixel_format) {
2807 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002808 dspcntr |= DISPPLANE_8BPP;
2809 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002810 case DRM_FORMAT_RGB565:
2811 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002813 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 dspcntr |= DISPPLANE_BGRX888;
2815 break;
2816 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002817 dspcntr |= DISPPLANE_RGBX888;
2818 break;
2819 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002820 dspcntr |= DISPPLANE_BGRX101010;
2821 break;
2822 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002823 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002824 break;
2825 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002826 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002827 }
2828
2829 if (obj->tiling_mode != I915_TILING_NONE)
2830 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002831
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002832 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002833 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002834
Ville Syrjäläac484962016-01-20 21:05:26 +02002835 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002836 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002837 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002838 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002839 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002840 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302841 dspcntr |= DISPPLANE_ROTATE_180;
2842
2843 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002844 x += (crtc_state->pipe_src_w - 1);
2845 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302846
2847 /* Finding the last pixel of the last line of the display
2848 data and adding to linear_offset*/
2849 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002850 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002851 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302852 }
2853 }
2854
Paulo Zanoni2db33662015-09-14 15:20:03 -03002855 intel_crtc->adjusted_x = x;
2856 intel_crtc->adjusted_y = y;
2857
Sonika Jindal48404c12014-08-22 14:06:04 +05302858 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002859
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002860 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002861 I915_WRITE(DSPSURF(plane),
2862 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002863 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002864 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2865 } else {
2866 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2867 I915_WRITE(DSPLINOFF(plane), linear_offset);
2868 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002869 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002870}
2871
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002872u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2873 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002874{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002875 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2876 return 64;
2877 } else {
2878 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002879
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002880 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002881 }
2882}
2883
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002884u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2885 struct drm_i915_gem_object *obj,
2886 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002887{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002888 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002889 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002890 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002891
Ville Syrjäläe7941292016-01-19 18:23:17 +02002892 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002893 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002894
Daniel Vetterce7f1722015-10-14 16:51:06 +02002895 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002896 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002897 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002898 return -1;
2899
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002900 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002901
2902 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002903 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002904 PAGE_SIZE;
2905 }
2906
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002907 WARN_ON(upper_32_bits(offset));
2908
2909 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002910}
2911
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002912static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2913{
2914 struct drm_device *dev = intel_crtc->base.dev;
2915 struct drm_i915_private *dev_priv = dev->dev_private;
2916
2917 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2918 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2919 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002920}
2921
Chandra Kondurua1b22782015-04-07 15:28:45 -07002922/*
2923 * This function detaches (aka. unbinds) unused scalers in hardware
2924 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002925static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002926{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002927 struct intel_crtc_scaler_state *scaler_state;
2928 int i;
2929
Chandra Kondurua1b22782015-04-07 15:28:45 -07002930 scaler_state = &intel_crtc->config->scaler_state;
2931
2932 /* loop through and disable scalers that aren't in use */
2933 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002934 if (!scaler_state->scalers[i].in_use)
2935 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002936 }
2937}
2938
Chandra Konduru6156a452015-04-27 13:48:39 -07002939u32 skl_plane_ctl_format(uint32_t pixel_format)
2940{
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002942 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002943 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 /*
2951 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2952 * to be already pre-multiplied. We need to add a knob (or a different
2953 * DRM_FORMAT) for user-space to configure that.
2954 */
2955 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002974 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002976
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978}
2979
2980u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2981{
Chandra Konduru6156a452015-04-27 13:48:39 -07002982 switch (fb_modifier) {
2983 case DRM_FORMAT_MOD_NONE:
2984 break;
2985 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002986 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002988 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002990 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002991 default:
2992 MISSING_CASE(fb_modifier);
2993 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002994
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002995 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996}
2997
2998u32 skl_plane_ctl_rotation(unsigned int rotation)
2999{
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 switch (rotation) {
3001 case BIT(DRM_ROTATE_0):
3002 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303003 /*
3004 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3005 * while i915 HW rotation is clockwise, thats why this swapping.
3006 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003007 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303008 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003010 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003011 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303012 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 default:
3014 MISSING_CASE(rotation);
3015 }
3016
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003017 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003018}
3019
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020static void skylake_update_primary_plane(struct drm_plane *plane,
3021 const struct intel_crtc_state *crtc_state,
3022 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003023{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003024 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003025 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3027 struct drm_framebuffer *fb = plane_state->base.fb;
3028 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003029 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303030 u32 plane_ctl, stride_div, stride;
3031 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003032 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303033 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003034 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003035 int scaler_id = plane_state->scaler_id;
3036 int src_x = plane_state->src.x1 >> 16;
3037 int src_y = plane_state->src.y1 >> 16;
3038 int src_w = drm_rect_width(&plane_state->src) >> 16;
3039 int src_h = drm_rect_height(&plane_state->src) >> 16;
3040 int dst_x = plane_state->dst.x1;
3041 int dst_y = plane_state->dst.y1;
3042 int dst_w = drm_rect_width(&plane_state->dst);
3043 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003044
3045 plane_ctl = PLANE_CTL_ENABLE |
3046 PLANE_CTL_PIPE_GAMMA_ENABLE |
3047 PLANE_CTL_PIPE_CSC_ENABLE;
3048
Chandra Konduru6156a452015-04-27 13:48:39 -07003049 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3050 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003051 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003052 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003053
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003054 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003055 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003056 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303057
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003058 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003059
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303060 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003061 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3062
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003064 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303065 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003066 x_offset = stride * tile_height - src_y - src_h;
3067 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003068 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303069 } else {
3070 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003071 x_offset = src_x;
3072 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003073 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303074 }
3075 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003076
Paulo Zanoni2db33662015-09-14 15:20:03 -03003077 intel_crtc->adjusted_x = x_offset;
3078 intel_crtc->adjusted_y = y_offset;
3079
Damien Lespiau70d21f02013-07-03 21:06:04 +01003080 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303081 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3082 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3083 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003084
3085 if (scaler_id >= 0) {
3086 uint32_t ps_ctrl = 0;
3087
3088 WARN_ON(!dst_w || !dst_h);
3089 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3090 crtc_state->scaler_state.scalers[scaler_id].mode;
3091 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3092 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3093 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3094 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3095 I915_WRITE(PLANE_POS(pipe, 0), 0);
3096 } else {
3097 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3098 }
3099
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003100 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003101
3102 POSTING_READ(PLANE_SURF(pipe, 0));
3103}
3104
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003105static void skylake_disable_primary_plane(struct drm_plane *primary,
3106 struct drm_crtc *crtc)
3107{
3108 struct drm_device *dev = crtc->dev;
3109 struct drm_i915_private *dev_priv = dev->dev_private;
3110 int pipe = to_intel_crtc(crtc)->pipe;
3111
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003112 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3113 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3114 POSTING_READ(PLANE_SURF(pipe, 0));
3115}
3116
Jesse Barnes17638cd2011-06-24 12:19:23 -07003117/* Assume fb object is pinned & idle & fenced and just update base pointers */
3118static int
3119intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3120 int x, int y, enum mode_set_atomic state)
3121{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003122 /* Support for kgdboc is disabled, this needs a major rework. */
3123 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003124
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003125 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003126}
3127
Daniel Vetter5a21b662016-05-24 17:13:53 +02003128static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3129{
3130 struct intel_crtc *crtc;
3131
3132 for_each_intel_crtc(dev_priv->dev, crtc)
3133 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3134}
3135
Ville Syrjälä75147472014-11-24 18:28:11 +02003136static void intel_update_primary_planes(struct drm_device *dev)
3137{
Ville Syrjälä75147472014-11-24 18:28:11 +02003138 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003139
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003140 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003141 struct intel_plane *plane = to_intel_plane(crtc->primary);
3142 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003143
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003144 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003145 plane_state = to_intel_plane_state(plane->base.state);
3146
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003147 if (plane_state->visible)
3148 plane->update_plane(&plane->base,
3149 to_intel_crtc_state(crtc->state),
3150 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003151
3152 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003153 }
3154}
3155
Chris Wilsonc0336662016-05-06 15:40:21 +01003156void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003157{
3158 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003159 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003160 return;
3161
3162 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003163 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003164 return;
3165
Chris Wilsonc0336662016-05-06 15:40:21 +01003166 drm_modeset_lock_all(dev_priv->dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003167 /*
3168 * Disabling the crtcs gracefully seems nicer. Also the
3169 * g33 docs say we should at least disable all the planes.
3170 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003171 intel_display_suspend(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003172}
3173
Chris Wilsonc0336662016-05-06 15:40:21 +01003174void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003175{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003176 /*
3177 * Flips in the rings will be nuked by the reset,
3178 * so complete all pending flips so that user space
3179 * will get its events and not get stuck.
3180 */
3181 intel_complete_page_flips(dev_priv);
3182
Ville Syrjälä75147472014-11-24 18:28:11 +02003183 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003184 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003185 return;
3186
3187 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003188 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003189 /*
3190 * Flips in the rings have been nuked by the reset,
3191 * so update the base address of all primary
3192 * planes to the the last fb to make sure we're
3193 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003194 *
3195 * FIXME: Atomic will make this obsolete since we won't schedule
3196 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003197 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003198 intel_update_primary_planes(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003199 return;
3200 }
3201
3202 /*
3203 * The display has been reset as well,
3204 * so need a full re-initialization.
3205 */
3206 intel_runtime_pm_disable_interrupts(dev_priv);
3207 intel_runtime_pm_enable_interrupts(dev_priv);
3208
Chris Wilsonc0336662016-05-06 15:40:21 +01003209 intel_modeset_init_hw(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003210
3211 spin_lock_irq(&dev_priv->irq_lock);
3212 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003213 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003214 spin_unlock_irq(&dev_priv->irq_lock);
3215
Chris Wilsonc0336662016-05-06 15:40:21 +01003216 intel_display_resume(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003217
3218 intel_hpd_init(dev_priv);
3219
Chris Wilsonc0336662016-05-06 15:40:21 +01003220 drm_modeset_unlock_all(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003221}
3222
Chris Wilson7d5e3792014-03-04 13:15:08 +00003223static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3224{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003225 struct drm_device *dev = crtc->dev;
3226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3227 unsigned reset_counter;
3228 bool pending;
3229
3230 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3231 if (intel_crtc->reset_counter != reset_counter)
3232 return false;
3233
3234 spin_lock_irq(&dev->event_lock);
3235 pending = to_intel_crtc(crtc)->flip_work != NULL;
3236 spin_unlock_irq(&dev->event_lock);
3237
3238 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003239}
3240
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003241static void intel_update_pipe_config(struct intel_crtc *crtc,
3242 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003243{
3244 struct drm_device *dev = crtc->base.dev;
3245 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003246 struct intel_crtc_state *pipe_config =
3247 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003248
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003249 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3250 crtc->base.mode = crtc->base.state->mode;
3251
3252 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3253 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3254 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003255
3256 /*
3257 * Update pipe size and adjust fitter if needed: the reason for this is
3258 * that in compute_mode_changes we check the native mode (not the pfit
3259 * mode) to see if we can flip rather than do a full mode set. In the
3260 * fastboot case, we'll flip, but if we don't update the pipesrc and
3261 * pfit state, we'll end up with a big fb scanned out into the wrong
3262 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003263 */
3264
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003265 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003266 ((pipe_config->pipe_src_w - 1) << 16) |
3267 (pipe_config->pipe_src_h - 1));
3268
3269 /* on skylake this is done by detaching scalers */
3270 if (INTEL_INFO(dev)->gen >= 9) {
3271 skl_detach_scalers(crtc);
3272
3273 if (pipe_config->pch_pfit.enabled)
3274 skylake_pfit_enable(crtc);
3275 } else if (HAS_PCH_SPLIT(dev)) {
3276 if (pipe_config->pch_pfit.enabled)
3277 ironlake_pfit_enable(crtc);
3278 else if (old_crtc_state->pch_pfit.enabled)
3279 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003280 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003281}
3282
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003283static void intel_fdi_normal_train(struct drm_crtc *crtc)
3284{
3285 struct drm_device *dev = crtc->dev;
3286 struct drm_i915_private *dev_priv = dev->dev_private;
3287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3288 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003289 i915_reg_t reg;
3290 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003291
3292 /* enable normal train */
3293 reg = FDI_TX_CTL(pipe);
3294 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003295 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003296 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3297 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003298 } else {
3299 temp &= ~FDI_LINK_TRAIN_NONE;
3300 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003301 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003302 I915_WRITE(reg, temp);
3303
3304 reg = FDI_RX_CTL(pipe);
3305 temp = I915_READ(reg);
3306 if (HAS_PCH_CPT(dev)) {
3307 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3308 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3309 } else {
3310 temp &= ~FDI_LINK_TRAIN_NONE;
3311 temp |= FDI_LINK_TRAIN_NONE;
3312 }
3313 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3314
3315 /* wait one idle pattern time */
3316 POSTING_READ(reg);
3317 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003318
3319 /* IVB wants error correction enabled */
3320 if (IS_IVYBRIDGE(dev))
3321 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3322 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003323}
3324
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003325/* The FDI link training functions for ILK/Ibexpeak. */
3326static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3327{
3328 struct drm_device *dev = crtc->dev;
3329 struct drm_i915_private *dev_priv = dev->dev_private;
3330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3331 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003332 i915_reg_t reg;
3333 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003334
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003335 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003336 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003337
Adam Jacksone1a44742010-06-25 15:32:14 -04003338 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3339 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003340 reg = FDI_RX_IMR(pipe);
3341 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003342 temp &= ~FDI_RX_SYMBOL_LOCK;
3343 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 I915_WRITE(reg, temp);
3345 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003346 udelay(150);
3347
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003348 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003349 reg = FDI_TX_CTL(pipe);
3350 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003351 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003352 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 temp &= ~FDI_LINK_TRAIN_NONE;
3354 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 reg = FDI_RX_CTL(pipe);
3358 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359 temp &= ~FDI_LINK_TRAIN_NONE;
3360 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3362
3363 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003364 udelay(150);
3365
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003366 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003367 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3368 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3369 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003370
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003372 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3375
3376 if ((temp & FDI_RX_BIT_LOCK)) {
3377 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379 break;
3380 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003382 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003383 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003384
3385 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 reg = FDI_TX_CTL(pipe);
3387 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 temp &= ~FDI_LINK_TRAIN_NONE;
3389 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 reg = FDI_RX_CTL(pipe);
3393 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 temp &= ~FDI_LINK_TRAIN_NONE;
3395 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 I915_WRITE(reg, temp);
3397
3398 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399 udelay(150);
3400
Chris Wilson5eddb702010-09-11 13:48:45 +01003401 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003402 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003403 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003404 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3405
3406 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 DRM_DEBUG_KMS("FDI train 2 done.\n");
3409 break;
3410 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003412 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414
3415 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003416
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417}
3418
Akshay Joshi0206e352011-08-16 15:34:10 -04003419static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3421 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3422 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3423 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3424};
3425
3426/* The FDI link training functions for SNB/Cougarpoint. */
3427static void gen6_fdi_link_train(struct drm_crtc *crtc)
3428{
3429 struct drm_device *dev = crtc->dev;
3430 struct drm_i915_private *dev_priv = dev->dev_private;
3431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3432 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003433 i915_reg_t reg;
3434 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435
Adam Jacksone1a44742010-06-25 15:32:14 -04003436 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3437 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 reg = FDI_RX_IMR(pipe);
3439 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003440 temp &= ~FDI_RX_SYMBOL_LOCK;
3441 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 I915_WRITE(reg, temp);
3443
3444 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003445 udelay(150);
3446
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 reg = FDI_TX_CTL(pipe);
3449 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003450 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003451 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 temp &= ~FDI_LINK_TRAIN_NONE;
3453 temp |= FDI_LINK_TRAIN_PATTERN_1;
3454 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3455 /* SNB-B */
3456 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003458
Daniel Vetterd74cf322012-10-26 10:58:13 +02003459 I915_WRITE(FDI_RX_MISC(pipe),
3460 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3461
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 reg = FDI_RX_CTL(pipe);
3463 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 if (HAS_PCH_CPT(dev)) {
3465 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3466 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3467 } else {
3468 temp &= ~FDI_LINK_TRAIN_NONE;
3469 temp |= FDI_LINK_TRAIN_PATTERN_1;
3470 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3472
3473 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 udelay(150);
3475
Akshay Joshi0206e352011-08-16 15:34:10 -04003476 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 reg = FDI_TX_CTL(pipe);
3478 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3480 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 I915_WRITE(reg, temp);
3482
3483 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 udelay(500);
3485
Sean Paulfa37d392012-03-02 12:53:39 -05003486 for (retry = 0; retry < 5; retry++) {
3487 reg = FDI_RX_IIR(pipe);
3488 temp = I915_READ(reg);
3489 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3490 if (temp & FDI_RX_BIT_LOCK) {
3491 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3492 DRM_DEBUG_KMS("FDI train 1 done.\n");
3493 break;
3494 }
3495 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 }
Sean Paulfa37d392012-03-02 12:53:39 -05003497 if (retry < 5)
3498 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 }
3500 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502
3503 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 reg = FDI_TX_CTL(pipe);
3505 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 temp &= ~FDI_LINK_TRAIN_NONE;
3507 temp |= FDI_LINK_TRAIN_PATTERN_2;
3508 if (IS_GEN6(dev)) {
3509 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3510 /* SNB-B */
3511 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3512 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003514
Chris Wilson5eddb702010-09-11 13:48:45 +01003515 reg = FDI_RX_CTL(pipe);
3516 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517 if (HAS_PCH_CPT(dev)) {
3518 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3519 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3520 } else {
3521 temp &= ~FDI_LINK_TRAIN_NONE;
3522 temp |= FDI_LINK_TRAIN_PATTERN_2;
3523 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 I915_WRITE(reg, temp);
3525
3526 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 udelay(150);
3528
Akshay Joshi0206e352011-08-16 15:34:10 -04003529 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 reg = FDI_TX_CTL(pipe);
3531 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3533 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003534 I915_WRITE(reg, temp);
3535
3536 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003537 udelay(500);
3538
Sean Paulfa37d392012-03-02 12:53:39 -05003539 for (retry = 0; retry < 5; retry++) {
3540 reg = FDI_RX_IIR(pipe);
3541 temp = I915_READ(reg);
3542 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3543 if (temp & FDI_RX_SYMBOL_LOCK) {
3544 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3545 DRM_DEBUG_KMS("FDI train 2 done.\n");
3546 break;
3547 }
3548 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 }
Sean Paulfa37d392012-03-02 12:53:39 -05003550 if (retry < 5)
3551 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552 }
3553 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003554 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003555
3556 DRM_DEBUG_KMS("FDI train done.\n");
3557}
3558
Jesse Barnes357555c2011-04-28 15:09:55 -07003559/* Manual link training for Ivy Bridge A0 parts */
3560static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3561{
3562 struct drm_device *dev = crtc->dev;
3563 struct drm_i915_private *dev_priv = dev->dev_private;
3564 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3565 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003566 i915_reg_t reg;
3567 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003568
3569 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3570 for train result */
3571 reg = FDI_RX_IMR(pipe);
3572 temp = I915_READ(reg);
3573 temp &= ~FDI_RX_SYMBOL_LOCK;
3574 temp &= ~FDI_RX_BIT_LOCK;
3575 I915_WRITE(reg, temp);
3576
3577 POSTING_READ(reg);
3578 udelay(150);
3579
Daniel Vetter01a415f2012-10-27 15:58:40 +02003580 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3581 I915_READ(FDI_RX_IIR(pipe)));
3582
Jesse Barnes139ccd32013-08-19 11:04:55 -07003583 /* Try each vswing and preemphasis setting twice before moving on */
3584 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3585 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003586 reg = FDI_TX_CTL(pipe);
3587 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003588 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3589 temp &= ~FDI_TX_ENABLE;
3590 I915_WRITE(reg, temp);
3591
3592 reg = FDI_RX_CTL(pipe);
3593 temp = I915_READ(reg);
3594 temp &= ~FDI_LINK_TRAIN_AUTO;
3595 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3596 temp &= ~FDI_RX_ENABLE;
3597 I915_WRITE(reg, temp);
3598
3599 /* enable CPU FDI TX and PCH FDI RX */
3600 reg = FDI_TX_CTL(pipe);
3601 temp = I915_READ(reg);
3602 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003603 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003604 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003605 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003606 temp |= snb_b_fdi_train_param[j/2];
3607 temp |= FDI_COMPOSITE_SYNC;
3608 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3609
3610 I915_WRITE(FDI_RX_MISC(pipe),
3611 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3612
3613 reg = FDI_RX_CTL(pipe);
3614 temp = I915_READ(reg);
3615 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3616 temp |= FDI_COMPOSITE_SYNC;
3617 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3618
3619 POSTING_READ(reg);
3620 udelay(1); /* should be 0.5us */
3621
3622 for (i = 0; i < 4; i++) {
3623 reg = FDI_RX_IIR(pipe);
3624 temp = I915_READ(reg);
3625 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3626
3627 if (temp & FDI_RX_BIT_LOCK ||
3628 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3629 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3630 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3631 i);
3632 break;
3633 }
3634 udelay(1); /* should be 0.5us */
3635 }
3636 if (i == 4) {
3637 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3638 continue;
3639 }
3640
3641 /* Train 2 */
3642 reg = FDI_TX_CTL(pipe);
3643 temp = I915_READ(reg);
3644 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3645 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3646 I915_WRITE(reg, temp);
3647
3648 reg = FDI_RX_CTL(pipe);
3649 temp = I915_READ(reg);
3650 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3651 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003652 I915_WRITE(reg, temp);
3653
3654 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003655 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003656
Jesse Barnes139ccd32013-08-19 11:04:55 -07003657 for (i = 0; i < 4; i++) {
3658 reg = FDI_RX_IIR(pipe);
3659 temp = I915_READ(reg);
3660 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003661
Jesse Barnes139ccd32013-08-19 11:04:55 -07003662 if (temp & FDI_RX_SYMBOL_LOCK ||
3663 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3664 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3665 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3666 i);
3667 goto train_done;
3668 }
3669 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003670 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003671 if (i == 4)
3672 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003673 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003674
Jesse Barnes139ccd32013-08-19 11:04:55 -07003675train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003676 DRM_DEBUG_KMS("FDI train done.\n");
3677}
3678
Daniel Vetter88cefb62012-08-12 19:27:14 +02003679static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003680{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003681 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003682 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003683 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003684 i915_reg_t reg;
3685 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003686
Jesse Barnes0e23b992010-09-10 11:10:00 -07003687 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003688 reg = FDI_RX_CTL(pipe);
3689 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003690 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003691 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003692 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003693 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3694
3695 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003696 udelay(200);
3697
3698 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003699 temp = I915_READ(reg);
3700 I915_WRITE(reg, temp | FDI_PCDCLK);
3701
3702 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003703 udelay(200);
3704
Paulo Zanoni20749732012-11-23 15:30:38 -02003705 /* Enable CPU FDI TX PLL, always on for Ironlake */
3706 reg = FDI_TX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3709 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003710
Paulo Zanoni20749732012-11-23 15:30:38 -02003711 POSTING_READ(reg);
3712 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003713 }
3714}
3715
Daniel Vetter88cefb62012-08-12 19:27:14 +02003716static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3717{
3718 struct drm_device *dev = intel_crtc->base.dev;
3719 struct drm_i915_private *dev_priv = dev->dev_private;
3720 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003721 i915_reg_t reg;
3722 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003723
3724 /* Switch from PCDclk to Rawclk */
3725 reg = FDI_RX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3728
3729 /* Disable CPU FDI TX PLL */
3730 reg = FDI_TX_CTL(pipe);
3731 temp = I915_READ(reg);
3732 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3733
3734 POSTING_READ(reg);
3735 udelay(100);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
3739 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3740
3741 /* Wait for the clocks to turn off. */
3742 POSTING_READ(reg);
3743 udelay(100);
3744}
3745
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003746static void ironlake_fdi_disable(struct drm_crtc *crtc)
3747{
3748 struct drm_device *dev = crtc->dev;
3749 struct drm_i915_private *dev_priv = dev->dev_private;
3750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3751 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003752 i915_reg_t reg;
3753 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003754
3755 /* disable CPU FDI tx and PCH FDI rx */
3756 reg = FDI_TX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3759 POSTING_READ(reg);
3760
3761 reg = FDI_RX_CTL(pipe);
3762 temp = I915_READ(reg);
3763 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003764 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003765 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3766
3767 POSTING_READ(reg);
3768 udelay(100);
3769
3770 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003771 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003772 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003773
3774 /* still set train pattern 1 */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 temp &= ~FDI_LINK_TRAIN_NONE;
3778 temp |= FDI_LINK_TRAIN_PATTERN_1;
3779 I915_WRITE(reg, temp);
3780
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 if (HAS_PCH_CPT(dev)) {
3784 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3785 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3786 } else {
3787 temp &= ~FDI_LINK_TRAIN_NONE;
3788 temp |= FDI_LINK_TRAIN_PATTERN_1;
3789 }
3790 /* BPC in FDI rx is consistent with that in PIPECONF */
3791 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003792 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003793 I915_WRITE(reg, temp);
3794
3795 POSTING_READ(reg);
3796 udelay(100);
3797}
3798
Chris Wilson5dce5b932014-01-20 10:17:36 +00003799bool intel_has_pending_fb_unpin(struct drm_device *dev)
3800{
3801 struct intel_crtc *crtc;
3802
3803 /* Note that we don't need to be called with mode_config.lock here
3804 * as our list of CRTC objects is static for the lifetime of the
3805 * device and so cannot disappear as we iterate. Similarly, we can
3806 * happily treat the predicates as racy, atomic checks as userspace
3807 * cannot claim and pin a new fb without at least acquring the
3808 * struct_mutex and so serialising with us.
3809 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003810 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003811 if (atomic_read(&crtc->unpin_work_count) == 0)
3812 continue;
3813
Daniel Vetter5a21b662016-05-24 17:13:53 +02003814 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003815 intel_wait_for_vblank(dev, crtc->pipe);
3816
3817 return true;
3818 }
3819
3820 return false;
3821}
3822
Daniel Vetter5a21b662016-05-24 17:13:53 +02003823static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003824{
3825 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003826 struct intel_flip_work *work = intel_crtc->flip_work;
3827
3828 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003829
3830 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003831 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003832
3833 drm_crtc_vblank_put(&intel_crtc->base);
3834
Daniel Vetter5a21b662016-05-24 17:13:53 +02003835 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +02003836 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003837
3838 trace_i915_flip_complete(intel_crtc->plane,
3839 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003840}
3841
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003842static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003843{
Chris Wilson0f911282012-04-17 10:05:38 +01003844 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003845 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003846 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003847
Daniel Vetter2c10d572012-12-20 21:24:07 +01003848 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003849
3850 ret = wait_event_interruptible_timeout(
3851 dev_priv->pending_flip_queue,
3852 !intel_crtc_has_pending_flip(crtc),
3853 60*HZ);
3854
3855 if (ret < 0)
3856 return ret;
3857
Daniel Vetter5a21b662016-05-24 17:13:53 +02003858 if (ret == 0) {
3859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3860 struct intel_flip_work *work;
3861
3862 spin_lock_irq(&dev->event_lock);
3863 work = intel_crtc->flip_work;
3864 if (work && !is_mmio_work(work)) {
3865 WARN_ONCE(1, "Removing stuck page flip\n");
3866 page_flip_completed(intel_crtc);
3867 }
3868 spin_unlock_irq(&dev->event_lock);
3869 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003870
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003871 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003872}
3873
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003874static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3875{
3876 u32 temp;
3877
3878 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3879
3880 mutex_lock(&dev_priv->sb_lock);
3881
3882 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3883 temp |= SBI_SSCCTL_DISABLE;
3884 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3885
3886 mutex_unlock(&dev_priv->sb_lock);
3887}
3888
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003889/* Program iCLKIP clock to the desired frequency */
3890static void lpt_program_iclkip(struct drm_crtc *crtc)
3891{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003892 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003893 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003894 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3895 u32 temp;
3896
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003897 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003898
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003899 /* The iCLK virtual clock root frequency is in MHz,
3900 * but the adjusted_mode->crtc_clock in in KHz. To get the
3901 * divisors, it is necessary to divide one by another, so we
3902 * convert the virtual clock precision to KHz here for higher
3903 * precision.
3904 */
3905 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906 u32 iclk_virtual_root_freq = 172800 * 1000;
3907 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003908 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003909
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003910 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3911 clock << auxdiv);
3912 divsel = (desired_divisor / iclk_pi_range) - 2;
3913 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003914
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003915 /*
3916 * Near 20MHz is a corner case which is
3917 * out of range for the 7-bit divisor
3918 */
3919 if (divsel <= 0x7f)
3920 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003921 }
3922
3923 /* This should not happen with any sane values */
3924 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3925 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3926 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3927 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3928
3929 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 +03003930 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003931 auxdiv,
3932 divsel,
3933 phasedir,
3934 phaseinc);
3935
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003936 mutex_lock(&dev_priv->sb_lock);
3937
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003938 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003939 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3941 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3942 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3943 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3944 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3945 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003946 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947
3948 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003949 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3951 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003952 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953
3954 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003955 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003956 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003957 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003959 mutex_unlock(&dev_priv->sb_lock);
3960
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003961 /* Wait for initialization time */
3962 udelay(24);
3963
3964 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3965}
3966
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003967int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3968{
3969 u32 divsel, phaseinc, auxdiv;
3970 u32 iclk_virtual_root_freq = 172800 * 1000;
3971 u32 iclk_pi_range = 64;
3972 u32 desired_divisor;
3973 u32 temp;
3974
3975 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3976 return 0;
3977
3978 mutex_lock(&dev_priv->sb_lock);
3979
3980 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3981 if (temp & SBI_SSCCTL_DISABLE) {
3982 mutex_unlock(&dev_priv->sb_lock);
3983 return 0;
3984 }
3985
3986 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3987 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3988 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3989 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3990 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3991
3992 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3993 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3994 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3995
3996 mutex_unlock(&dev_priv->sb_lock);
3997
3998 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3999
4000 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4001 desired_divisor << auxdiv);
4002}
4003
Daniel Vetter275f01b22013-05-03 11:49:47 +02004004static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4005 enum pipe pch_transcoder)
4006{
4007 struct drm_device *dev = crtc->base.dev;
4008 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004009 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004010
4011 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4012 I915_READ(HTOTAL(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4014 I915_READ(HBLANK(cpu_transcoder)));
4015 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4016 I915_READ(HSYNC(cpu_transcoder)));
4017
4018 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4019 I915_READ(VTOTAL(cpu_transcoder)));
4020 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4021 I915_READ(VBLANK(cpu_transcoder)));
4022 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4023 I915_READ(VSYNC(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4025 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4026}
4027
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004028static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004029{
4030 struct drm_i915_private *dev_priv = dev->dev_private;
4031 uint32_t temp;
4032
4033 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004034 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004035 return;
4036
4037 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4038 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4039
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004040 temp &= ~FDI_BC_BIFURCATION_SELECT;
4041 if (enable)
4042 temp |= FDI_BC_BIFURCATION_SELECT;
4043
4044 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004045 I915_WRITE(SOUTH_CHICKEN1, temp);
4046 POSTING_READ(SOUTH_CHICKEN1);
4047}
4048
4049static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4050{
4051 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004052
4053 switch (intel_crtc->pipe) {
4054 case PIPE_A:
4055 break;
4056 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004057 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004058 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004059 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004060 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004061
4062 break;
4063 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004064 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004065
4066 break;
4067 default:
4068 BUG();
4069 }
4070}
4071
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004072/* Return which DP Port should be selected for Transcoder DP control */
4073static enum port
4074intel_trans_dp_port_sel(struct drm_crtc *crtc)
4075{
4076 struct drm_device *dev = crtc->dev;
4077 struct intel_encoder *encoder;
4078
4079 for_each_encoder_on_crtc(dev, crtc, encoder) {
4080 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4081 encoder->type == INTEL_OUTPUT_EDP)
4082 return enc_to_dig_port(&encoder->base)->port;
4083 }
4084
4085 return -1;
4086}
4087
Jesse Barnesf67a5592011-01-05 10:31:48 -08004088/*
4089 * Enable PCH resources required for PCH ports:
4090 * - PCH PLLs
4091 * - FDI training & RX/TX
4092 * - update transcoder timings
4093 * - DP transcoding bits
4094 * - transcoder
4095 */
4096static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004097{
4098 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004099 struct drm_i915_private *dev_priv = dev->dev_private;
4100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4101 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004102 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004103
Daniel Vetterab9412b2013-05-03 11:49:46 +02004104 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004105
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004106 if (IS_IVYBRIDGE(dev))
4107 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4108
Daniel Vettercd986ab2012-10-26 10:58:12 +02004109 /* Write the TU size bits before fdi link training, so that error
4110 * detection works. */
4111 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4112 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4113
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004115 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004116
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004117 /* We need to program the right clock selection before writing the pixel
4118 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004119 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004120 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004121
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004122 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004123 temp |= TRANS_DPLL_ENABLE(pipe);
4124 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004125 if (intel_crtc->config->shared_dpll ==
4126 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004127 temp |= sel;
4128 else
4129 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004130 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004131 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004133 /* XXX: pch pll's can be enabled any time before we enable the PCH
4134 * transcoder, and we actually should do this to not upset any PCH
4135 * transcoder that already use the clock when we share it.
4136 *
4137 * Note that enable_shared_dpll tries to do the right thing, but
4138 * get_shared_dpll unconditionally resets the pll - we need that to have
4139 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004140 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004141
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004142 /* set transcoder timing, panel must allow it */
4143 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004144 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004145
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004146 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004147
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004148 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004149 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004150 const struct drm_display_mode *adjusted_mode =
4151 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004152 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004153 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004154 temp = I915_READ(reg);
4155 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004156 TRANS_DP_SYNC_MASK |
4157 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004158 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004159 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004161 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004163 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004164 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165
4166 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004167 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004168 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004169 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004170 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004171 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004173 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004174 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004175 break;
4176 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004177 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004178 }
4179
Chris Wilson5eddb702010-09-11 13:48:45 +01004180 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004181 }
4182
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004183 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004184}
4185
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186static void lpt_pch_enable(struct drm_crtc *crtc)
4187{
4188 struct drm_device *dev = crtc->dev;
4189 struct drm_i915_private *dev_priv = dev->dev_private;
4190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004191 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004192
Daniel Vetterab9412b2013-05-03 11:49:46 +02004193 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004194
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004195 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004196
Paulo Zanoni0540e482012-10-31 18:12:40 -02004197 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004198 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004199
Paulo Zanoni937bb612012-10-31 18:12:47 -02004200 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004201}
4202
Daniel Vettera1520312013-05-03 11:49:50 +02004203static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004204{
4205 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004206 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004207 u32 temp;
4208
4209 temp = I915_READ(dslreg);
4210 udelay(500);
4211 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004212 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004213 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004214 }
4215}
4216
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004217static int
4218skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4219 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4220 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004221{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004222 struct intel_crtc_scaler_state *scaler_state =
4223 &crtc_state->scaler_state;
4224 struct intel_crtc *intel_crtc =
4225 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004226 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004227
4228 need_scaling = intel_rotation_90_or_270(rotation) ?
4229 (src_h != dst_w || src_w != dst_h):
4230 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004231
4232 /*
4233 * if plane is being disabled or scaler is no more required or force detach
4234 * - free scaler binded to this plane/crtc
4235 * - in order to do this, update crtc->scaler_usage
4236 *
4237 * Here scaler state in crtc_state is set free so that
4238 * scaler can be assigned to other user. Actual register
4239 * update to free the scaler is done in plane/panel-fit programming.
4240 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4241 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004242 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004243 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004244 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004245 scaler_state->scalers[*scaler_id].in_use = 0;
4246
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004247 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4248 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4249 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250 scaler_state->scaler_users);
4251 *scaler_id = -1;
4252 }
4253 return 0;
4254 }
4255
4256 /* range checks */
4257 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4258 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4259
4260 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4261 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004262 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004263 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004264 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004265 return -EINVAL;
4266 }
4267
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004268 /* mark this plane as a scaler user in crtc_state */
4269 scaler_state->scaler_users |= (1 << scaler_user);
4270 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4271 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4272 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4273 scaler_state->scaler_users);
4274
4275 return 0;
4276}
4277
4278/**
4279 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4280 *
4281 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004282 *
4283 * Return
4284 * 0 - scaler_usage updated successfully
4285 * error - requested scaling cannot be supported or other error condition
4286 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004287int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004288{
4289 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004290 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004291
Ville Syrjälä78108b72016-05-27 20:59:19 +03004292 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4293 intel_crtc->base.base.id, intel_crtc->base.name,
4294 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004295
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004296 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004297 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004298 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004299 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004300}
4301
4302/**
4303 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4304 *
4305 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004306 * @plane_state: atomic plane state to update
4307 *
4308 * Return
4309 * 0 - scaler_usage updated successfully
4310 * error - requested scaling cannot be supported or other error condition
4311 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004312static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4313 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004314{
4315
4316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004317 struct intel_plane *intel_plane =
4318 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004319 struct drm_framebuffer *fb = plane_state->base.fb;
4320 int ret;
4321
4322 bool force_detach = !fb || !plane_state->visible;
4323
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004324 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4325 intel_plane->base.base.id, intel_plane->base.name,
4326 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004327
4328 ret = skl_update_scaler(crtc_state, force_detach,
4329 drm_plane_index(&intel_plane->base),
4330 &plane_state->scaler_id,
4331 plane_state->base.rotation,
4332 drm_rect_width(&plane_state->src) >> 16,
4333 drm_rect_height(&plane_state->src) >> 16,
4334 drm_rect_width(&plane_state->dst),
4335 drm_rect_height(&plane_state->dst));
4336
4337 if (ret || plane_state->scaler_id < 0)
4338 return ret;
4339
Chandra Kondurua1b22782015-04-07 15:28:45 -07004340 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004341 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004342 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4343 intel_plane->base.base.id,
4344 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004345 return -EINVAL;
4346 }
4347
4348 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004349 switch (fb->pixel_format) {
4350 case DRM_FORMAT_RGB565:
4351 case DRM_FORMAT_XBGR8888:
4352 case DRM_FORMAT_XRGB8888:
4353 case DRM_FORMAT_ABGR8888:
4354 case DRM_FORMAT_ARGB8888:
4355 case DRM_FORMAT_XRGB2101010:
4356 case DRM_FORMAT_XBGR2101010:
4357 case DRM_FORMAT_YUYV:
4358 case DRM_FORMAT_YVYU:
4359 case DRM_FORMAT_UYVY:
4360 case DRM_FORMAT_VYUY:
4361 break;
4362 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004363 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4364 intel_plane->base.base.id, intel_plane->base.name,
4365 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004366 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004367 }
4368
Chandra Kondurua1b22782015-04-07 15:28:45 -07004369 return 0;
4370}
4371
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004372static void skylake_scaler_disable(struct intel_crtc *crtc)
4373{
4374 int i;
4375
4376 for (i = 0; i < crtc->num_scalers; i++)
4377 skl_detach_scaler(crtc, i);
4378}
4379
4380static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004381{
4382 struct drm_device *dev = crtc->base.dev;
4383 struct drm_i915_private *dev_priv = dev->dev_private;
4384 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004385 struct intel_crtc_scaler_state *scaler_state =
4386 &crtc->config->scaler_state;
4387
4388 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4389
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004390 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004391 int id;
4392
4393 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4394 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4395 return;
4396 }
4397
4398 id = scaler_state->scaler_id;
4399 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4400 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4401 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4402 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4403
4404 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004405 }
4406}
4407
Jesse Barnesb074cec2013-04-25 12:55:02 -07004408static void ironlake_pfit_enable(struct intel_crtc *crtc)
4409{
4410 struct drm_device *dev = crtc->base.dev;
4411 struct drm_i915_private *dev_priv = dev->dev_private;
4412 int pipe = crtc->pipe;
4413
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004414 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004415 /* Force use of hard-coded filter coefficients
4416 * as some pre-programmed values are broken,
4417 * e.g. x201.
4418 */
4419 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4420 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4421 PF_PIPE_SEL_IVB(pipe));
4422 else
4423 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004424 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4425 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004426 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004427}
4428
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004429void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004430{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004431 struct drm_device *dev = crtc->base.dev;
4432 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004433
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004434 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004435 return;
4436
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004437 /*
4438 * We can only enable IPS after we enable a plane and wait for a vblank
4439 * This function is called from post_plane_update, which is run after
4440 * a vblank wait.
4441 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004442
Paulo Zanonid77e4532013-09-24 13:52:55 -03004443 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004444 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004445 mutex_lock(&dev_priv->rps.hw_lock);
4446 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4447 mutex_unlock(&dev_priv->rps.hw_lock);
4448 /* Quoting Art Runyan: "its not safe to expect any particular
4449 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004450 * mailbox." Moreover, the mailbox may return a bogus state,
4451 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004452 */
4453 } else {
4454 I915_WRITE(IPS_CTL, IPS_ENABLE);
4455 /* The bit only becomes 1 in the next vblank, so this wait here
4456 * is essentially intel_wait_for_vblank. If we don't have this
4457 * and don't wait for vblanks until the end of crtc_enable, then
4458 * the HW state readout code will complain that the expected
4459 * IPS_CTL value is not the one we read. */
4460 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4461 DRM_ERROR("Timed out waiting for IPS enable\n");
4462 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004463}
4464
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004465void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004466{
4467 struct drm_device *dev = crtc->base.dev;
4468 struct drm_i915_private *dev_priv = dev->dev_private;
4469
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004470 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004471 return;
4472
4473 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004474 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004475 mutex_lock(&dev_priv->rps.hw_lock);
4476 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4477 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004478 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4479 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4480 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004481 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004482 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004483 POSTING_READ(IPS_CTL);
4484 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004485
4486 /* We need to wait for a vblank before we can disable the plane. */
4487 intel_wait_for_vblank(dev, crtc->pipe);
4488}
4489
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004490static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004491{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004492 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004493 struct drm_device *dev = intel_crtc->base.dev;
4494 struct drm_i915_private *dev_priv = dev->dev_private;
4495
4496 mutex_lock(&dev->struct_mutex);
4497 dev_priv->mm.interruptible = false;
4498 (void) intel_overlay_switch_off(intel_crtc->overlay);
4499 dev_priv->mm.interruptible = true;
4500 mutex_unlock(&dev->struct_mutex);
4501 }
4502
4503 /* Let userspace switch the overlay on again. In most cases userspace
4504 * has to recompute where to put it anyway.
4505 */
4506}
4507
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004508/**
4509 * intel_post_enable_primary - Perform operations after enabling primary plane
4510 * @crtc: the CRTC whose primary plane was just enabled
4511 *
4512 * Performs potentially sleeping operations that must be done after the primary
4513 * plane is enabled, such as updating FBC and IPS. Note that this may be
4514 * called due to an explicit primary plane update, or due to an implicit
4515 * re-enable that is caused when a sprite plane is updated to no longer
4516 * completely hide the primary plane.
4517 */
4518static void
4519intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004520{
4521 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004522 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4524 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004525
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004526 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004527 * FIXME IPS should be fine as long as one plane is
4528 * enabled, but in practice it seems to have problems
4529 * when going from primary only to sprite only and vice
4530 * versa.
4531 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004532 hsw_enable_ips(intel_crtc);
4533
Daniel Vetterf99d7062014-06-19 16:01:59 +02004534 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004535 * Gen2 reports pipe underruns whenever all planes are disabled.
4536 * So don't enable underrun reporting before at least some planes
4537 * are enabled.
4538 * FIXME: Need to fix the logic to work when we turn off all planes
4539 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004540 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004541 if (IS_GEN2(dev))
4542 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4543
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004544 /* Underruns don't always raise interrupts, so check manually. */
4545 intel_check_cpu_fifo_underruns(dev_priv);
4546 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004547}
4548
Ville Syrjälä2622a082016-03-09 19:07:26 +02004549/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004550static void
4551intel_pre_disable_primary(struct drm_crtc *crtc)
4552{
4553 struct drm_device *dev = crtc->dev;
4554 struct drm_i915_private *dev_priv = dev->dev_private;
4555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4556 int pipe = intel_crtc->pipe;
4557
4558 /*
4559 * Gen2 reports pipe underruns whenever all planes are disabled.
4560 * So diasble underrun reporting before all the planes get disabled.
4561 * FIXME: Need to fix the logic to work when we turn off all planes
4562 * but leave the pipe running.
4563 */
4564 if (IS_GEN2(dev))
4565 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4566
4567 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004568 * FIXME IPS should be fine as long as one plane is
4569 * enabled, but in practice it seems to have problems
4570 * when going from primary only to sprite only and vice
4571 * versa.
4572 */
4573 hsw_disable_ips(intel_crtc);
4574}
4575
4576/* FIXME get rid of this and use pre_plane_update */
4577static void
4578intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4579{
4580 struct drm_device *dev = crtc->dev;
4581 struct drm_i915_private *dev_priv = dev->dev_private;
4582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4583 int pipe = intel_crtc->pipe;
4584
4585 intel_pre_disable_primary(crtc);
4586
4587 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004588 * Vblank time updates from the shadow to live plane control register
4589 * are blocked if the memory self-refresh mode is active at that
4590 * moment. So to make sure the plane gets truly disabled, disable
4591 * first the self-refresh mode. The self-refresh enable bit in turn
4592 * will be checked/applied by the HW only at the next frame start
4593 * event which is after the vblank start event, so we need to have a
4594 * wait-for-vblank between disabling the plane and the pipe.
4595 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004596 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004597 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004598 dev_priv->wm.vlv.cxsr = false;
4599 intel_wait_for_vblank(dev, pipe);
4600 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004601}
4602
Daniel Vetter5a21b662016-05-24 17:13:53 +02004603static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4604{
4605 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4606 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4607 struct intel_crtc_state *pipe_config =
4608 to_intel_crtc_state(crtc->base.state);
4609 struct drm_device *dev = crtc->base.dev;
4610 struct drm_plane *primary = crtc->base.primary;
4611 struct drm_plane_state *old_pri_state =
4612 drm_atomic_get_existing_plane_state(old_state, primary);
4613
4614 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4615
4616 crtc->wm.cxsr_allowed = true;
4617
4618 if (pipe_config->update_wm_post && pipe_config->base.active)
4619 intel_update_watermarks(&crtc->base);
4620
4621 if (old_pri_state) {
4622 struct intel_plane_state *primary_state =
4623 to_intel_plane_state(primary->state);
4624 struct intel_plane_state *old_primary_state =
4625 to_intel_plane_state(old_pri_state);
4626
4627 intel_fbc_post_update(crtc);
4628
4629 if (primary_state->visible &&
4630 (needs_modeset(&pipe_config->base) ||
4631 !old_primary_state->visible))
4632 intel_post_enable_primary(&crtc->base);
4633 }
4634}
4635
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004636static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004637{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004638 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004639 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004640 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004641 struct intel_crtc_state *pipe_config =
4642 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004643 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4644 struct drm_plane *primary = crtc->base.primary;
4645 struct drm_plane_state *old_pri_state =
4646 drm_atomic_get_existing_plane_state(old_state, primary);
4647 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004648
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004649 if (old_pri_state) {
4650 struct intel_plane_state *primary_state =
4651 to_intel_plane_state(primary->state);
4652 struct intel_plane_state *old_primary_state =
4653 to_intel_plane_state(old_pri_state);
4654
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004655 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004656
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004657 if (old_primary_state->visible &&
4658 (modeset || !primary_state->visible))
4659 intel_pre_disable_primary(&crtc->base);
4660 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004661
David Weinehalla4015f92016-05-19 15:50:36 +03004662 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004663 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004664
Ville Syrjälä2622a082016-03-09 19:07:26 +02004665 /*
4666 * Vblank time updates from the shadow to live plane control register
4667 * are blocked if the memory self-refresh mode is active at that
4668 * moment. So to make sure the plane gets truly disabled, disable
4669 * first the self-refresh mode. The self-refresh enable bit in turn
4670 * will be checked/applied by the HW only at the next frame start
4671 * event which is after the vblank start event, so we need to have a
4672 * wait-for-vblank between disabling the plane and the pipe.
4673 */
4674 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004675 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004676 dev_priv->wm.vlv.cxsr = false;
4677 intel_wait_for_vblank(dev, crtc->pipe);
4678 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004679 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004680
Matt Ropered4a6a72016-02-23 17:20:13 -08004681 /*
4682 * IVB workaround: must disable low power watermarks for at least
4683 * one frame before enabling scaling. LP watermarks can be re-enabled
4684 * when scaling is disabled.
4685 *
4686 * WaCxSRDisabledForSpriteScaling:ivb
4687 */
4688 if (pipe_config->disable_lp_wm) {
4689 ilk_disable_lp_wm(dev);
4690 intel_wait_for_vblank(dev, crtc->pipe);
4691 }
4692
4693 /*
4694 * If we're doing a modeset, we're done. No need to do any pre-vblank
4695 * watermark programming here.
4696 */
4697 if (needs_modeset(&pipe_config->base))
4698 return;
4699
4700 /*
4701 * For platforms that support atomic watermarks, program the
4702 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4703 * will be the intermediate values that are safe for both pre- and
4704 * post- vblank; when vblank happens, the 'active' values will be set
4705 * to the final 'target' values and we'll do this again to get the
4706 * optimal watermarks. For gen9+ platforms, the values we program here
4707 * will be the final target values which will get automatically latched
4708 * at vblank time; no further programming will be necessary.
4709 *
4710 * If a platform hasn't been transitioned to atomic watermarks yet,
4711 * we'll continue to update watermarks the old way, if flags tell
4712 * us to.
4713 */
4714 if (dev_priv->display.initial_watermarks != NULL)
4715 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004716 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004717 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004718}
4719
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004720static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004721{
4722 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004724 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004725 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004726
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004727 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004728
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004729 drm_for_each_plane_mask(p, dev, plane_mask)
4730 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004731
Daniel Vetterf99d7062014-06-19 16:01:59 +02004732 /*
4733 * FIXME: Once we grow proper nuclear flip support out of this we need
4734 * to compute the mask of flip planes precisely. For the time being
4735 * consider this a flip to a NULL plane.
4736 */
4737 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004738}
4739
Jesse Barnesf67a5592011-01-05 10:31:48 -08004740static void ironlake_crtc_enable(struct drm_crtc *crtc)
4741{
4742 struct drm_device *dev = crtc->dev;
4743 struct drm_i915_private *dev_priv = dev->dev_private;
4744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004745 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004746 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004747 struct intel_crtc_state *pipe_config =
4748 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004749
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004750 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004751 return;
4752
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004753 /*
4754 * Sometimes spurious CPU pipe underruns happen during FDI
4755 * training, at least with VGA+HDMI cloning. Suppress them.
4756 *
4757 * On ILK we get an occasional spurious CPU pipe underruns
4758 * between eDP port A enable and vdd enable. Also PCH port
4759 * enable seems to result in the occasional CPU pipe underrun.
4760 *
4761 * Spurious PCH underruns also occur during PCH enabling.
4762 */
4763 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4764 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004765 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004766 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4767
4768 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004769 intel_prepare_shared_dpll(intel_crtc);
4770
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004771 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304772 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004773
4774 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004775 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004776
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004777 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004778 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004779 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004780 }
4781
4782 ironlake_set_pipeconf(crtc);
4783
Jesse Barnesf67a5592011-01-05 10:31:48 -08004784 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004785
Daniel Vetterf6736a12013-06-05 13:34:30 +02004786 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004787 if (encoder->pre_enable)
4788 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004789
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004790 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004791 /* Note: FDI PLL enabling _must_ be done before we enable the
4792 * cpu pipes, hence this is separate from all the other fdi/pch
4793 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004794 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004795 } else {
4796 assert_fdi_tx_disabled(dev_priv, pipe);
4797 assert_fdi_rx_disabled(dev_priv, pipe);
4798 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004799
Jesse Barnesb074cec2013-04-25 12:55:02 -07004800 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004801
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004802 /*
4803 * On ILK+ LUT must be loaded before the pipe is running but with
4804 * clocks enabled
4805 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004806 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004807
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004808 if (dev_priv->display.initial_watermarks != NULL)
4809 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004810 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004811
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004812 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004813 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004814
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004815 assert_vblank_disabled(crtc);
4816 drm_crtc_vblank_on(crtc);
4817
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004818 for_each_encoder_on_crtc(dev, crtc, encoder)
4819 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004820
4821 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004822 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004823
4824 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4825 if (intel_crtc->config->has_pch_encoder)
4826 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004827 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004828 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004829}
4830
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004831/* IPS only exists on ULT machines and is tied to pipe A. */
4832static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4833{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004834 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004835}
4836
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004837static void haswell_crtc_enable(struct drm_crtc *crtc)
4838{
4839 struct drm_device *dev = crtc->dev;
4840 struct drm_i915_private *dev_priv = dev->dev_private;
4841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4842 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004843 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004844 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004845 struct intel_crtc_state *pipe_config =
4846 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004847
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004848 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004849 return;
4850
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004851 if (intel_crtc->config->has_pch_encoder)
4852 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4853 false);
4854
Imre Deak95a7a2a2016-06-13 16:44:35 +03004855 for_each_encoder_on_crtc(dev, crtc, encoder)
4856 if (encoder->pre_pll_enable)
4857 encoder->pre_pll_enable(encoder);
4858
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004859 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004860 intel_enable_shared_dpll(intel_crtc);
4861
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004862 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304863 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004864
Jani Nikula4d1de972016-03-18 17:05:42 +02004865 if (!intel_crtc->config->has_dsi_encoder)
4866 intel_set_pipe_timings(intel_crtc);
4867
Jani Nikulabc58be62016-03-18 17:05:39 +02004868 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004869
Jani Nikula4d1de972016-03-18 17:05:42 +02004870 if (cpu_transcoder != TRANSCODER_EDP &&
4871 !transcoder_is_dsi(cpu_transcoder)) {
4872 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004873 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004874 }
4875
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004876 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004877 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004878 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004879 }
4880
Jani Nikula4d1de972016-03-18 17:05:42 +02004881 if (!intel_crtc->config->has_dsi_encoder)
4882 haswell_set_pipeconf(crtc);
4883
Jani Nikula391bf042016-03-18 17:05:40 +02004884 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004885
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004886 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004887
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004888 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004889
Daniel Vetter6b698512015-11-28 11:05:39 +01004890 if (intel_crtc->config->has_pch_encoder)
4891 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4892 else
4893 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4894
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304895 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004896 if (encoder->pre_enable)
4897 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304898 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004899
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004900 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004901 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004902
Jani Nikulaa65347b2015-11-27 12:21:46 +02004903 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304904 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004905
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004906 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004907 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004908 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004909 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004910
4911 /*
4912 * On ILK+ LUT must be loaded before the pipe is running but with
4913 * clocks enabled
4914 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004915 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004916
Paulo Zanoni1f544382012-10-24 11:32:00 -02004917 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004918 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304919 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004921 if (dev_priv->display.initial_watermarks != NULL)
4922 dev_priv->display.initial_watermarks(pipe_config);
4923 else
4924 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004925
4926 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4927 if (!intel_crtc->config->has_dsi_encoder)
4928 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004929
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004930 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004931 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004932
Jani Nikulaa65347b2015-11-27 12:21:46 +02004933 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004934 intel_ddi_set_vc_payload_alloc(crtc, true);
4935
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004936 assert_vblank_disabled(crtc);
4937 drm_crtc_vblank_on(crtc);
4938
Jani Nikula8807e552013-08-30 19:40:32 +03004939 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004940 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004941 intel_opregion_notify_encoder(encoder, true);
4942 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004943
Daniel Vetter6b698512015-11-28 11:05:39 +01004944 if (intel_crtc->config->has_pch_encoder) {
4945 intel_wait_for_vblank(dev, pipe);
4946 intel_wait_for_vblank(dev, pipe);
4947 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004948 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4949 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004950 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004951
Paulo Zanonie4916942013-09-20 16:21:19 -03004952 /* If we change the relative order between pipe/planes enabling, we need
4953 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004954 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4955 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4956 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4957 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4958 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004959}
4960
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004961static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004962{
4963 struct drm_device *dev = crtc->base.dev;
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4965 int pipe = crtc->pipe;
4966
4967 /* To avoid upsetting the power well on haswell only disable the pfit if
4968 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004969 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004970 I915_WRITE(PF_CTL(pipe), 0);
4971 I915_WRITE(PF_WIN_POS(pipe), 0);
4972 I915_WRITE(PF_WIN_SZ(pipe), 0);
4973 }
4974}
4975
Jesse Barnes6be4a602010-09-10 10:26:01 -07004976static void ironlake_crtc_disable(struct drm_crtc *crtc)
4977{
4978 struct drm_device *dev = crtc->dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004981 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004983
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004984 /*
4985 * Sometimes spurious CPU pipe underruns happen when the
4986 * pipe is already disabled, but FDI RX/TX is still enabled.
4987 * Happens at least with VGA+HDMI cloning. Suppress them.
4988 */
4989 if (intel_crtc->config->has_pch_encoder) {
4990 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004991 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004992 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004993
Daniel Vetterea9d7582012-07-10 10:42:52 +02004994 for_each_encoder_on_crtc(dev, crtc, encoder)
4995 encoder->disable(encoder);
4996
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004997 drm_crtc_vblank_off(crtc);
4998 assert_vblank_disabled(crtc);
4999
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005000 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005001
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005002 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005003
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005004 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005005 ironlake_fdi_disable(crtc);
5006
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005007 for_each_encoder_on_crtc(dev, crtc, encoder)
5008 if (encoder->post_disable)
5009 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005010
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005011 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005012 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005013
Daniel Vetterd925c592013-06-05 13:34:04 +02005014 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005015 i915_reg_t reg;
5016 u32 temp;
5017
Daniel Vetterd925c592013-06-05 13:34:04 +02005018 /* disable TRANS_DP_CTL */
5019 reg = TRANS_DP_CTL(pipe);
5020 temp = I915_READ(reg);
5021 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5022 TRANS_DP_PORT_SEL_MASK);
5023 temp |= TRANS_DP_PORT_SEL_NONE;
5024 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005025
Daniel Vetterd925c592013-06-05 13:34:04 +02005026 /* disable DPLL_SEL */
5027 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005028 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005029 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005030 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005031
Daniel Vetterd925c592013-06-05 13:34:04 +02005032 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005033 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005034
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005035 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005036 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005037}
5038
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039static void haswell_crtc_disable(struct drm_crtc *crtc)
5040{
5041 struct drm_device *dev = crtc->dev;
5042 struct drm_i915_private *dev_priv = dev->dev_private;
5043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5044 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005045 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005046
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005047 if (intel_crtc->config->has_pch_encoder)
5048 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5049 false);
5050
Jani Nikula8807e552013-08-30 19:40:32 +03005051 for_each_encoder_on_crtc(dev, crtc, encoder) {
5052 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005054 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005055
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005056 drm_crtc_vblank_off(crtc);
5057 assert_vblank_disabled(crtc);
5058
Jani Nikula4d1de972016-03-18 17:05:42 +02005059 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5060 if (!intel_crtc->config->has_dsi_encoder)
5061 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005062
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005063 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005064 intel_ddi_set_vc_payload_alloc(crtc, false);
5065
Jani Nikulaa65347b2015-11-27 12:21:46 +02005066 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305067 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005068
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005069 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005070 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005071 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005072 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005073
Jani Nikulaa65347b2015-11-27 12:21:46 +02005074 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305075 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005076
Imre Deak97b040a2014-06-25 22:01:50 +03005077 for_each_encoder_on_crtc(dev, crtc, encoder)
5078 if (encoder->post_disable)
5079 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005080
Ville Syrjälä92966a32015-12-08 16:05:48 +02005081 if (intel_crtc->config->has_pch_encoder) {
5082 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005083 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005084 intel_ddi_fdi_disable(crtc);
5085
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005086 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5087 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005088 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005089}
5090
Jesse Barnes2dd24552013-04-25 12:55:01 -07005091static void i9xx_pfit_enable(struct intel_crtc *crtc)
5092{
5093 struct drm_device *dev = crtc->base.dev;
5094 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005095 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005096
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005097 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005098 return;
5099
Daniel Vetterc0b03412013-05-28 12:05:54 +02005100 /*
5101 * The panel fitter should only be adjusted whilst the pipe is disabled,
5102 * according to register description and PRM.
5103 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005104 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5105 assert_pipe_disabled(dev_priv, crtc->pipe);
5106
Jesse Barnesb074cec2013-04-25 12:55:02 -07005107 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5108 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005109
5110 /* Border color in case we don't scale up to the full screen. Black by
5111 * default, change to something else for debugging. */
5112 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005113}
5114
Dave Airlied05410f2014-06-05 13:22:59 +10005115static enum intel_display_power_domain port_to_power_domain(enum port port)
5116{
5117 switch (port) {
5118 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005119 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005120 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005121 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005122 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005123 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005124 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005125 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005126 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005127 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005128 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005129 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005130 return POWER_DOMAIN_PORT_OTHER;
5131 }
5132}
5133
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005134static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5135{
5136 switch (port) {
5137 case PORT_A:
5138 return POWER_DOMAIN_AUX_A;
5139 case PORT_B:
5140 return POWER_DOMAIN_AUX_B;
5141 case PORT_C:
5142 return POWER_DOMAIN_AUX_C;
5143 case PORT_D:
5144 return POWER_DOMAIN_AUX_D;
5145 case PORT_E:
5146 /* FIXME: Check VBT for actual wiring of PORT E */
5147 return POWER_DOMAIN_AUX_D;
5148 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005149 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005150 return POWER_DOMAIN_AUX_A;
5151 }
5152}
5153
Imre Deak319be8a2014-03-04 19:22:57 +02005154enum intel_display_power_domain
5155intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005156{
Imre Deak319be8a2014-03-04 19:22:57 +02005157 struct drm_device *dev = intel_encoder->base.dev;
5158 struct intel_digital_port *intel_dig_port;
5159
5160 switch (intel_encoder->type) {
5161 case INTEL_OUTPUT_UNKNOWN:
5162 /* Only DDI platforms should ever use this output type */
5163 WARN_ON_ONCE(!HAS_DDI(dev));
5164 case INTEL_OUTPUT_DISPLAYPORT:
5165 case INTEL_OUTPUT_HDMI:
5166 case INTEL_OUTPUT_EDP:
5167 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005168 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005169 case INTEL_OUTPUT_DP_MST:
5170 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5171 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005172 case INTEL_OUTPUT_ANALOG:
5173 return POWER_DOMAIN_PORT_CRT;
5174 case INTEL_OUTPUT_DSI:
5175 return POWER_DOMAIN_PORT_DSI;
5176 default:
5177 return POWER_DOMAIN_PORT_OTHER;
5178 }
5179}
5180
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005181enum intel_display_power_domain
5182intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5183{
5184 struct drm_device *dev = intel_encoder->base.dev;
5185 struct intel_digital_port *intel_dig_port;
5186
5187 switch (intel_encoder->type) {
5188 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005189 case INTEL_OUTPUT_HDMI:
5190 /*
5191 * Only DDI platforms should ever use these output types.
5192 * We can get here after the HDMI detect code has already set
5193 * the type of the shared encoder. Since we can't be sure
5194 * what's the status of the given connectors, play safe and
5195 * run the DP detection too.
5196 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005197 WARN_ON_ONCE(!HAS_DDI(dev));
5198 case INTEL_OUTPUT_DISPLAYPORT:
5199 case INTEL_OUTPUT_EDP:
5200 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5201 return port_to_aux_power_domain(intel_dig_port->port);
5202 case INTEL_OUTPUT_DP_MST:
5203 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5204 return port_to_aux_power_domain(intel_dig_port->port);
5205 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005206 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005207 return POWER_DOMAIN_AUX_A;
5208 }
5209}
5210
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005211static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5212 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005213{
5214 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005215 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5217 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005218 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005219 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005220
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005221 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005222 return 0;
5223
Imre Deak77d22dc2014-03-05 16:20:52 +02005224 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5225 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005226 if (crtc_state->pch_pfit.enabled ||
5227 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005228 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5229
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005230 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5231 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5232
Imre Deak319be8a2014-03-04 19:22:57 +02005233 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005234 }
Imre Deak319be8a2014-03-04 19:22:57 +02005235
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005236 if (crtc_state->shared_dpll)
5237 mask |= BIT(POWER_DOMAIN_PLLS);
5238
Imre Deak77d22dc2014-03-05 16:20:52 +02005239 return mask;
5240}
5241
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005242static unsigned long
5243modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5244 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005245{
5246 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5248 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005249 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005250
5251 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005252 intel_crtc->enabled_power_domains = new_domains =
5253 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005254
Daniel Vetter5a21b662016-05-24 17:13:53 +02005255 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005256
5257 for_each_power_domain(domain, domains)
5258 intel_display_power_get(dev_priv, domain);
5259
Daniel Vetter5a21b662016-05-24 17:13:53 +02005260 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005261}
5262
5263static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5264 unsigned long domains)
5265{
5266 enum intel_display_power_domain domain;
5267
5268 for_each_power_domain(domain, domains)
5269 intel_display_power_put(dev_priv, domain);
5270}
5271
Mika Kaholaadafdc62015-08-18 14:36:59 +03005272static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5273{
5274 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5275
5276 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5277 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5278 return max_cdclk_freq;
5279 else if (IS_CHERRYVIEW(dev_priv))
5280 return max_cdclk_freq*95/100;
5281 else if (INTEL_INFO(dev_priv)->gen < 4)
5282 return 2*max_cdclk_freq*90/100;
5283 else
5284 return max_cdclk_freq*90/100;
5285}
5286
Ville Syrjäläb2045352016-05-13 23:41:27 +03005287static int skl_calc_cdclk(int max_pixclk, int vco);
5288
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005289static void intel_update_max_cdclk(struct drm_device *dev)
5290{
5291 struct drm_i915_private *dev_priv = dev->dev_private;
5292
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005293 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005294 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005295 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005296
Ville Syrjäläb2045352016-05-13 23:41:27 +03005297 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005298 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005299
5300 /*
5301 * Use the lower (vco 8640) cdclk values as a
5302 * first guess. skl_calc_cdclk() will correct it
5303 * if the preferred vco is 8100 instead.
5304 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005305 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005306 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005307 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005308 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005309 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005310 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005311 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005312 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005313
5314 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005315 } else if (IS_BROXTON(dev)) {
5316 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005317 } else if (IS_BROADWELL(dev)) {
5318 /*
5319 * FIXME with extra cooling we can allow
5320 * 540 MHz for ULX and 675 Mhz for ULT.
5321 * How can we know if extra cooling is
5322 * available? PCI ID, VTB, something else?
5323 */
5324 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5325 dev_priv->max_cdclk_freq = 450000;
5326 else if (IS_BDW_ULX(dev))
5327 dev_priv->max_cdclk_freq = 450000;
5328 else if (IS_BDW_ULT(dev))
5329 dev_priv->max_cdclk_freq = 540000;
5330 else
5331 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005332 } else if (IS_CHERRYVIEW(dev)) {
5333 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005334 } else if (IS_VALLEYVIEW(dev)) {
5335 dev_priv->max_cdclk_freq = 400000;
5336 } else {
5337 /* otherwise assume cdclk is fixed */
5338 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5339 }
5340
Mika Kaholaadafdc62015-08-18 14:36:59 +03005341 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5342
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005343 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5344 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005345
5346 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5347 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005348}
5349
5350static void intel_update_cdclk(struct drm_device *dev)
5351{
5352 struct drm_i915_private *dev_priv = dev->dev_private;
5353
5354 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005355
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005356 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005357 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5358 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5359 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005360 else
5361 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5362 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005363
5364 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005365 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5366 * Programmng [sic] note: bit[9:2] should be programmed to the number
5367 * of cdclk that generates 4MHz reference clock freq which is used to
5368 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005369 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005370 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005371 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005372}
5373
Ville Syrjälä92891e42016-05-11 22:44:45 +03005374/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5375static int skl_cdclk_decimal(int cdclk)
5376{
5377 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5378}
5379
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005380static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5381{
5382 int ratio;
5383
5384 if (cdclk == dev_priv->cdclk_pll.ref)
5385 return 0;
5386
5387 switch (cdclk) {
5388 default:
5389 MISSING_CASE(cdclk);
5390 case 144000:
5391 case 288000:
5392 case 384000:
5393 case 576000:
5394 ratio = 60;
5395 break;
5396 case 624000:
5397 ratio = 65;
5398 break;
5399 }
5400
5401 return dev_priv->cdclk_pll.ref * ratio;
5402}
5403
Ville Syrjälä2b730012016-05-13 23:41:34 +03005404static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5405{
5406 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5407
5408 /* Timeout 200us */
5409 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5410 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005411
5412 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005413}
5414
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005415static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005416{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005417 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005418 u32 val;
5419
5420 val = I915_READ(BXT_DE_PLL_CTL);
5421 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005422 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005423 I915_WRITE(BXT_DE_PLL_CTL, val);
5424
5425 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5426
5427 /* Timeout 200us */
5428 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5429 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005430
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005431 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005432}
5433
Imre Deak324513c2016-06-13 16:44:36 +03005434static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305435{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005436 u32 val, divider;
5437 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305438
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005439 vco = bxt_de_pll_vco(dev_priv, cdclk);
5440
5441 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5442
5443 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5444 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5445 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305446 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305447 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005448 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305449 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305450 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005451 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305452 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305453 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005454 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305455 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305456 break;
5457 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005458 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5459 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305460
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005461 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5462 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305463 }
5464
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305465 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005466 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305467 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5468 0x80000000);
5469 mutex_unlock(&dev_priv->rps.hw_lock);
5470
5471 if (ret) {
5472 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005473 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305474 return;
5475 }
5476
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005477 if (dev_priv->cdclk_pll.vco != 0 &&
5478 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005479 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305480
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005481 if (dev_priv->cdclk_pll.vco != vco)
5482 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305483
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005484 val = divider | skl_cdclk_decimal(cdclk);
5485 /*
5486 * FIXME if only the cd2x divider needs changing, it could be done
5487 * without shutting off the pipe (if only one pipe is active).
5488 */
5489 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5490 /*
5491 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5492 * enable otherwise.
5493 */
5494 if (cdclk >= 500000)
5495 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5496 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305497
5498 mutex_lock(&dev_priv->rps.hw_lock);
5499 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005500 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305501 mutex_unlock(&dev_priv->rps.hw_lock);
5502
5503 if (ret) {
5504 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005505 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305506 return;
5507 }
5508
Imre Deakc6c46962016-04-01 16:02:40 +03005509 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305510}
5511
Imre Deakd66a2192016-05-24 15:38:33 +03005512static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305513{
Imre Deakd66a2192016-05-24 15:38:33 +03005514 u32 cdctl, expected;
5515
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005516 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305517
Imre Deakd66a2192016-05-24 15:38:33 +03005518 if (dev_priv->cdclk_pll.vco == 0 ||
5519 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5520 goto sanitize;
5521
5522 /* DPLL okay; verify the cdclock
5523 *
5524 * Some BIOS versions leave an incorrect decimal frequency value and
5525 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5526 * so sanitize this register.
5527 */
5528 cdctl = I915_READ(CDCLK_CTL);
5529 /*
5530 * Let's ignore the pipe field, since BIOS could have configured the
5531 * dividers both synching to an active pipe, or asynchronously
5532 * (PIPE_NONE).
5533 */
5534 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5535
5536 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5537 skl_cdclk_decimal(dev_priv->cdclk_freq);
5538 /*
5539 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5540 * enable otherwise.
5541 */
5542 if (dev_priv->cdclk_freq >= 500000)
5543 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5544
5545 if (cdctl == expected)
5546 /* All well; nothing to sanitize */
5547 return;
5548
5549sanitize:
5550 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5551
5552 /* force cdclk programming */
5553 dev_priv->cdclk_freq = 0;
5554
5555 /* force full PLL disable + enable */
5556 dev_priv->cdclk_pll.vco = -1;
5557}
5558
Imre Deak324513c2016-06-13 16:44:36 +03005559void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005560{
5561 bxt_sanitize_cdclk(dev_priv);
5562
5563 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005564 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005565
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305566 /*
5567 * FIXME:
5568 * - The initial CDCLK needs to be read from VBT.
5569 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305570 */
Imre Deak324513c2016-06-13 16:44:36 +03005571 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305572}
5573
Imre Deak324513c2016-06-13 16:44:36 +03005574void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305575{
Imre Deak324513c2016-06-13 16:44:36 +03005576 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305577}
5578
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005579static int skl_calc_cdclk(int max_pixclk, int vco)
5580{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005581 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005582 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005583 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005584 else if (max_pixclk > 432000)
5585 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005586 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005587 return 432000;
5588 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005589 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005590 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005591 if (max_pixclk > 540000)
5592 return 675000;
5593 else if (max_pixclk > 450000)
5594 return 540000;
5595 else if (max_pixclk > 337500)
5596 return 450000;
5597 else
5598 return 337500;
5599 }
5600}
5601
Ville Syrjäläea617912016-05-13 23:41:24 +03005602static void
5603skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005604{
Ville Syrjäläea617912016-05-13 23:41:24 +03005605 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005606
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005607 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005608 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005609
Ville Syrjäläea617912016-05-13 23:41:24 +03005610 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005611 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005612 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005613
Imre Deak1c3f7702016-05-24 15:38:32 +03005614 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5615 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005616
Ville Syrjäläea617912016-05-13 23:41:24 +03005617 val = I915_READ(DPLL_CTRL1);
5618
Imre Deak1c3f7702016-05-24 15:38:32 +03005619 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5620 DPLL_CTRL1_SSC(SKL_DPLL0) |
5621 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5622 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5623 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005624
Ville Syrjäläea617912016-05-13 23:41:24 +03005625 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5626 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5627 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5628 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5629 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005630 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005631 break;
5632 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5633 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005634 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005635 break;
5636 default:
5637 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005638 break;
5639 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005640}
5641
Ville Syrjäläb2045352016-05-13 23:41:27 +03005642void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5643{
5644 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5645
5646 dev_priv->skl_preferred_vco_freq = vco;
5647
5648 if (changed)
5649 intel_update_max_cdclk(dev_priv->dev);
5650}
5651
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005652static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005653skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005654{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005655 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005656 u32 val;
5657
Ville Syrjälä63911d72016-05-13 23:41:32 +03005658 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005659
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005660 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005661 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005662 I915_WRITE(CDCLK_CTL, val);
5663 POSTING_READ(CDCLK_CTL);
5664
5665 /*
5666 * We always enable DPLL0 with the lowest link rate possible, but still
5667 * taking into account the VCO required to operate the eDP panel at the
5668 * desired frequency. The usual DP link rates operate with a VCO of
5669 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5670 * The modeset code is responsible for the selection of the exact link
5671 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005672 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005673 */
5674 val = I915_READ(DPLL_CTRL1);
5675
5676 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5677 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5678 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005679 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005680 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5681 SKL_DPLL0);
5682 else
5683 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5684 SKL_DPLL0);
5685
5686 I915_WRITE(DPLL_CTRL1, val);
5687 POSTING_READ(DPLL_CTRL1);
5688
5689 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5690
5691 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5692 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005693
Ville Syrjälä63911d72016-05-13 23:41:32 +03005694 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005695
5696 /* We'll want to keep using the current vco from now on. */
5697 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005698}
5699
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005700static void
5701skl_dpll0_disable(struct drm_i915_private *dev_priv)
5702{
5703 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5704 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5705 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005706
Ville Syrjälä63911d72016-05-13 23:41:32 +03005707 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005708}
5709
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005710static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5711{
5712 int ret;
5713 u32 val;
5714
5715 /* inform PCU we want to change CDCLK */
5716 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5717 mutex_lock(&dev_priv->rps.hw_lock);
5718 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5719 mutex_unlock(&dev_priv->rps.hw_lock);
5720
5721 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5722}
5723
5724static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5725{
5726 unsigned int i;
5727
5728 for (i = 0; i < 15; i++) {
5729 if (skl_cdclk_pcu_ready(dev_priv))
5730 return true;
5731 udelay(10);
5732 }
5733
5734 return false;
5735}
5736
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005737static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005738{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005739 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005740 u32 freq_select, pcu_ack;
5741
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005742 WARN_ON((cdclk == 24000) != (vco == 0));
5743
Ville Syrjälä63911d72016-05-13 23:41:32 +03005744 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005745
5746 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5747 DRM_ERROR("failed to inform PCU about cdclk change\n");
5748 return;
5749 }
5750
5751 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005752 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005753 case 450000:
5754 case 432000:
5755 freq_select = CDCLK_FREQ_450_432;
5756 pcu_ack = 1;
5757 break;
5758 case 540000:
5759 freq_select = CDCLK_FREQ_540;
5760 pcu_ack = 2;
5761 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005762 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005763 case 337500:
5764 default:
5765 freq_select = CDCLK_FREQ_337_308;
5766 pcu_ack = 0;
5767 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005768 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005769 case 675000:
5770 freq_select = CDCLK_FREQ_675_617;
5771 pcu_ack = 3;
5772 break;
5773 }
5774
Ville Syrjälä63911d72016-05-13 23:41:32 +03005775 if (dev_priv->cdclk_pll.vco != 0 &&
5776 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005777 skl_dpll0_disable(dev_priv);
5778
Ville Syrjälä63911d72016-05-13 23:41:32 +03005779 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005780 skl_dpll0_enable(dev_priv, vco);
5781
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005782 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005783 POSTING_READ(CDCLK_CTL);
5784
5785 /* inform PCU of the change */
5786 mutex_lock(&dev_priv->rps.hw_lock);
5787 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5788 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005789
5790 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005791}
5792
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005793static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5794
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005795void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5796{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005797 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005798}
5799
5800void skl_init_cdclk(struct drm_i915_private *dev_priv)
5801{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005802 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005803
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005804 skl_sanitize_cdclk(dev_priv);
5805
Ville Syrjälä63911d72016-05-13 23:41:32 +03005806 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005807 /*
5808 * Use the current vco as our initial
5809 * guess as to what the preferred vco is.
5810 */
5811 if (dev_priv->skl_preferred_vco_freq == 0)
5812 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005813 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005814 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005815 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005816
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005817 vco = dev_priv->skl_preferred_vco_freq;
5818 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005819 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005820 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005821
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005822 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005823}
5824
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005825static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305826{
Ville Syrjälä09492492016-05-13 23:41:28 +03005827 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305828
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305829 /*
5830 * check if the pre-os intialized the display
5831 * There is SWF18 scratchpad register defined which is set by the
5832 * pre-os which can be used by the OS drivers to check the status
5833 */
5834 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5835 goto sanitize;
5836
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005837 intel_update_cdclk(dev_priv->dev);
Imre Deak1c3f7702016-05-24 15:38:32 +03005838 /* Is PLL enabled and locked ? */
5839 if (dev_priv->cdclk_pll.vco == 0 ||
5840 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5841 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005842
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305843 /* DPLL okay; verify the cdclock
5844 *
5845 * Noticed in some instances that the freq selection is correct but
5846 * decimal part is programmed wrong from BIOS where pre-os does not
5847 * enable display. Verify the same as well.
5848 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005849 cdctl = I915_READ(CDCLK_CTL);
5850 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5851 skl_cdclk_decimal(dev_priv->cdclk_freq);
5852 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305853 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005854 return;
5855
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305856sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005857 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005858
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005859 /* force cdclk programming */
5860 dev_priv->cdclk_freq = 0;
5861 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005862 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305863}
5864
Jesse Barnes30a970c2013-11-04 13:48:12 -08005865/* Adjust CDclk dividers to allow high res or save power if possible */
5866static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5867{
5868 struct drm_i915_private *dev_priv = dev->dev_private;
5869 u32 val, cmd;
5870
Vandana Kannan164dfd22014-11-24 13:37:41 +05305871 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5872 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005873
Ville Syrjälädfcab172014-06-13 13:37:47 +03005874 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005875 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005876 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005877 cmd = 1;
5878 else
5879 cmd = 0;
5880
5881 mutex_lock(&dev_priv->rps.hw_lock);
5882 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5883 val &= ~DSPFREQGUAR_MASK;
5884 val |= (cmd << DSPFREQGUAR_SHIFT);
5885 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5886 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5887 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5888 50)) {
5889 DRM_ERROR("timed out waiting for CDclk change\n");
5890 }
5891 mutex_unlock(&dev_priv->rps.hw_lock);
5892
Ville Syrjälä54433e92015-05-26 20:42:31 +03005893 mutex_lock(&dev_priv->sb_lock);
5894
Ville Syrjälädfcab172014-06-13 13:37:47 +03005895 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005896 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005897
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005898 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005899
Jesse Barnes30a970c2013-11-04 13:48:12 -08005900 /* adjust cdclk divider */
5901 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005902 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005903 val |= divider;
5904 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005905
5906 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005907 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005908 50))
5909 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005910 }
5911
Jesse Barnes30a970c2013-11-04 13:48:12 -08005912 /* adjust self-refresh exit latency value */
5913 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5914 val &= ~0x7f;
5915
5916 /*
5917 * For high bandwidth configs, we set a higher latency in the bunit
5918 * so that the core display fetch happens in time to avoid underruns.
5919 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005920 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005921 val |= 4500 / 250; /* 4.5 usec */
5922 else
5923 val |= 3000 / 250; /* 3.0 usec */
5924 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005925
Ville Syrjäläa5805162015-05-26 20:42:30 +03005926 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005927
Ville Syrjäläb6283052015-06-03 15:45:07 +03005928 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005929}
5930
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005931static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5932{
5933 struct drm_i915_private *dev_priv = dev->dev_private;
5934 u32 val, cmd;
5935
Vandana Kannan164dfd22014-11-24 13:37:41 +05305936 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5937 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005938
5939 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005940 case 333333:
5941 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005942 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005943 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005944 break;
5945 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005946 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005947 return;
5948 }
5949
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005950 /*
5951 * Specs are full of misinformation, but testing on actual
5952 * hardware has shown that we just need to write the desired
5953 * CCK divider into the Punit register.
5954 */
5955 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5956
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005957 mutex_lock(&dev_priv->rps.hw_lock);
5958 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5959 val &= ~DSPFREQGUAR_MASK_CHV;
5960 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5961 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5962 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5963 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5964 50)) {
5965 DRM_ERROR("timed out waiting for CDclk change\n");
5966 }
5967 mutex_unlock(&dev_priv->rps.hw_lock);
5968
Ville Syrjäläb6283052015-06-03 15:45:07 +03005969 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005970}
5971
Jesse Barnes30a970c2013-11-04 13:48:12 -08005972static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5973 int max_pixclk)
5974{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005975 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005976 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005977
Jesse Barnes30a970c2013-11-04 13:48:12 -08005978 /*
5979 * Really only a few cases to deal with, as only 4 CDclks are supported:
5980 * 200MHz
5981 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005982 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005983 * 400MHz (VLV only)
5984 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5985 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005986 *
5987 * We seem to get an unstable or solid color picture at 200MHz.
5988 * Not sure what's wrong. For now use 200MHz only when all pipes
5989 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005990 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005991 if (!IS_CHERRYVIEW(dev_priv) &&
5992 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005993 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005994 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005995 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005996 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005997 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005998 else
5999 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006000}
6001
Imre Deak324513c2016-06-13 16:44:36 +03006002static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006003{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006004 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306005 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006006 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306007 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006008 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306009 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006010 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306011 return 288000;
6012 else
6013 return 144000;
6014}
6015
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006016/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006017static int intel_mode_max_pixclk(struct drm_device *dev,
6018 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006019{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006020 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6021 struct drm_i915_private *dev_priv = dev->dev_private;
6022 struct drm_crtc *crtc;
6023 struct drm_crtc_state *crtc_state;
6024 unsigned max_pixclk = 0, i;
6025 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006026
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006027 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6028 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006029
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006030 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6031 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006032
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006033 if (crtc_state->enable)
6034 pixclk = crtc_state->adjusted_mode.crtc_clock;
6035
6036 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006037 }
6038
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006039 for_each_pipe(dev_priv, pipe)
6040 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6041
Jesse Barnes30a970c2013-11-04 13:48:12 -08006042 return max_pixclk;
6043}
6044
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006045static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006046{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006047 struct drm_device *dev = state->dev;
6048 struct drm_i915_private *dev_priv = dev->dev_private;
6049 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006050 struct intel_atomic_state *intel_state =
6051 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006052
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006053 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006054 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306055
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006056 if (!intel_state->active_crtcs)
6057 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6058
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006059 return 0;
6060}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006061
Imre Deak324513c2016-06-13 16:44:36 +03006062static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006063{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006064 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006065 struct intel_atomic_state *intel_state =
6066 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006067
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006068 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006069 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006070
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006071 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006072 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006073
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006074 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006075}
6076
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006077static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6078{
6079 unsigned int credits, default_credits;
6080
6081 if (IS_CHERRYVIEW(dev_priv))
6082 default_credits = PFI_CREDIT(12);
6083 else
6084 default_credits = PFI_CREDIT(8);
6085
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006086 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006087 /* CHV suggested value is 31 or 63 */
6088 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006089 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006090 else
6091 credits = PFI_CREDIT(15);
6092 } else {
6093 credits = default_credits;
6094 }
6095
6096 /*
6097 * WA - write default credits before re-programming
6098 * FIXME: should we also set the resend bit here?
6099 */
6100 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6101 default_credits);
6102
6103 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6104 credits | PFI_CREDIT_RESEND);
6105
6106 /*
6107 * FIXME is this guaranteed to clear
6108 * immediately or should we poll for it?
6109 */
6110 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6111}
6112
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006113static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006114{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006115 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006116 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006117 struct intel_atomic_state *old_intel_state =
6118 to_intel_atomic_state(old_state);
6119 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006120
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006121 /*
6122 * FIXME: We can end up here with all power domains off, yet
6123 * with a CDCLK frequency other than the minimum. To account
6124 * for this take the PIPE-A power domain, which covers the HW
6125 * blocks needed for the following programming. This can be
6126 * removed once it's guaranteed that we get here either with
6127 * the minimum CDCLK set, or the required power domains
6128 * enabled.
6129 */
6130 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006131
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006132 if (IS_CHERRYVIEW(dev))
6133 cherryview_set_cdclk(dev, req_cdclk);
6134 else
6135 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006136
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006137 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006138
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006139 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006140}
6141
Jesse Barnes89b667f2013-04-18 14:51:36 -07006142static void valleyview_crtc_enable(struct drm_crtc *crtc)
6143{
6144 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006145 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6147 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006148 struct intel_crtc_state *pipe_config =
6149 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006150 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006151
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006152 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006153 return;
6154
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006155 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306156 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006157
6158 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006159 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006160
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006161 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6162 struct drm_i915_private *dev_priv = dev->dev_private;
6163
6164 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6165 I915_WRITE(CHV_CANVAS(pipe), 0);
6166 }
6167
Daniel Vetter5b18e572014-04-24 23:55:06 +02006168 i9xx_set_pipeconf(intel_crtc);
6169
Jesse Barnes89b667f2013-04-18 14:51:36 -07006170 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006171
Daniel Vettera72e4c92014-09-30 10:56:47 +02006172 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006173
Jesse Barnes89b667f2013-04-18 14:51:36 -07006174 for_each_encoder_on_crtc(dev, crtc, encoder)
6175 if (encoder->pre_pll_enable)
6176 encoder->pre_pll_enable(encoder);
6177
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006178 if (IS_CHERRYVIEW(dev)) {
6179 chv_prepare_pll(intel_crtc, intel_crtc->config);
6180 chv_enable_pll(intel_crtc, intel_crtc->config);
6181 } else {
6182 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6183 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006184 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006185
6186 for_each_encoder_on_crtc(dev, crtc, encoder)
6187 if (encoder->pre_enable)
6188 encoder->pre_enable(encoder);
6189
Jesse Barnes2dd24552013-04-25 12:55:01 -07006190 i9xx_pfit_enable(intel_crtc);
6191
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006192 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006193
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006194 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006195 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006196
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006197 assert_vblank_disabled(crtc);
6198 drm_crtc_vblank_on(crtc);
6199
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006200 for_each_encoder_on_crtc(dev, crtc, encoder)
6201 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006202}
6203
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006204static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6205{
6206 struct drm_device *dev = crtc->base.dev;
6207 struct drm_i915_private *dev_priv = dev->dev_private;
6208
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006209 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6210 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006211}
6212
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006213static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006214{
6215 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006216 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006218 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006219 struct intel_crtc_state *pipe_config =
6220 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006221 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006222
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006223 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006224 return;
6225
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006226 i9xx_set_pll_dividers(intel_crtc);
6227
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006228 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306229 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006230
6231 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006232 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006233
Daniel Vetter5b18e572014-04-24 23:55:06 +02006234 i9xx_set_pipeconf(intel_crtc);
6235
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006236 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006237
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006238 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006239 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006240
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006241 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006242 if (encoder->pre_enable)
6243 encoder->pre_enable(encoder);
6244
Daniel Vetterf6736a12013-06-05 13:34:30 +02006245 i9xx_enable_pll(intel_crtc);
6246
Jesse Barnes2dd24552013-04-25 12:55:01 -07006247 i9xx_pfit_enable(intel_crtc);
6248
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006249 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006250
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006251 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006252 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006253
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006254 assert_vblank_disabled(crtc);
6255 drm_crtc_vblank_on(crtc);
6256
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006257 for_each_encoder_on_crtc(dev, crtc, encoder)
6258 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006259}
6260
Daniel Vetter87476d62013-04-11 16:29:06 +02006261static void i9xx_pfit_disable(struct intel_crtc *crtc)
6262{
6263 struct drm_device *dev = crtc->base.dev;
6264 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006265
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006266 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006267 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006268
6269 assert_pipe_disabled(dev_priv, crtc->pipe);
6270
Daniel Vetter328d8e82013-05-08 10:36:31 +02006271 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6272 I915_READ(PFIT_CONTROL));
6273 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006274}
6275
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006276static void i9xx_crtc_disable(struct drm_crtc *crtc)
6277{
6278 struct drm_device *dev = crtc->dev;
6279 struct drm_i915_private *dev_priv = dev->dev_private;
6280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006281 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006282 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006283
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006284 /*
6285 * On gen2 planes are double buffered but the pipe isn't, so we must
6286 * wait for planes to fully turn off before disabling the pipe.
6287 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006288 if (IS_GEN2(dev))
6289 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006290
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006291 for_each_encoder_on_crtc(dev, crtc, encoder)
6292 encoder->disable(encoder);
6293
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006294 drm_crtc_vblank_off(crtc);
6295 assert_vblank_disabled(crtc);
6296
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006297 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006298
Daniel Vetter87476d62013-04-11 16:29:06 +02006299 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006300
Jesse Barnes89b667f2013-04-18 14:51:36 -07006301 for_each_encoder_on_crtc(dev, crtc, encoder)
6302 if (encoder->post_disable)
6303 encoder->post_disable(encoder);
6304
Jani Nikulaa65347b2015-11-27 12:21:46 +02006305 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006306 if (IS_CHERRYVIEW(dev))
6307 chv_disable_pll(dev_priv, pipe);
6308 else if (IS_VALLEYVIEW(dev))
6309 vlv_disable_pll(dev_priv, pipe);
6310 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006311 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006312 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006313
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006314 for_each_encoder_on_crtc(dev, crtc, encoder)
6315 if (encoder->post_pll_disable)
6316 encoder->post_pll_disable(encoder);
6317
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006318 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006319 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006320}
6321
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006322static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006323{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006324 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006326 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006327 enum intel_display_power_domain domain;
6328 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006329
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006330 if (!intel_crtc->active)
6331 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006332
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006333 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006334 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006335
Ville Syrjälä2622a082016-03-09 19:07:26 +02006336 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006337
6338 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6339 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006340 }
6341
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006342 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006343
Ville Syrjälä78108b72016-05-27 20:59:19 +03006344 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6345 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006346
6347 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6348 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006349 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006350 crtc->enabled = false;
6351 crtc->state->connector_mask = 0;
6352 crtc->state->encoder_mask = 0;
6353
6354 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6355 encoder->base.crtc = NULL;
6356
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006357 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006358 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006359 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006360
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006361 domains = intel_crtc->enabled_power_domains;
6362 for_each_power_domain(domain, domains)
6363 intel_display_power_put(dev_priv, domain);
6364 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006365
6366 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6367 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006368}
6369
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006370/*
6371 * turn all crtc's off, but do not adjust state
6372 * This has to be paired with a call to intel_modeset_setup_hw_state.
6373 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006374int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006375{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006376 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006377 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006378 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006379
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006380 state = drm_atomic_helper_suspend(dev);
6381 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006382 if (ret)
6383 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006384 else
6385 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006386 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006387}
6388
Chris Wilsonea5b2132010-08-04 13:50:23 +01006389void intel_encoder_destroy(struct drm_encoder *encoder)
6390{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006391 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006392
Chris Wilsonea5b2132010-08-04 13:50:23 +01006393 drm_encoder_cleanup(encoder);
6394 kfree(intel_encoder);
6395}
6396
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006397/* Cross check the actual hw state with our own modeset state tracking (and it's
6398 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006399static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006400{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006401 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006402
6403 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6404 connector->base.base.id,
6405 connector->base.name);
6406
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006407 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006408 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006409 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006410
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006411 I915_STATE_WARN(!crtc,
6412 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006413
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006414 if (!crtc)
6415 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006416
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006417 I915_STATE_WARN(!crtc->state->active,
6418 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006419
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006420 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006421 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006422
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006423 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006424 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006425
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006426 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006427 "attached encoder crtc differs from connector crtc\n");
6428 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006429 I915_STATE_WARN(crtc && crtc->state->active,
6430 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006431 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006432 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006433 }
6434}
6435
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006436int intel_connector_init(struct intel_connector *connector)
6437{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006438 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006439
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006440 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006441 return -ENOMEM;
6442
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006443 return 0;
6444}
6445
6446struct intel_connector *intel_connector_alloc(void)
6447{
6448 struct intel_connector *connector;
6449
6450 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6451 if (!connector)
6452 return NULL;
6453
6454 if (intel_connector_init(connector) < 0) {
6455 kfree(connector);
6456 return NULL;
6457 }
6458
6459 return connector;
6460}
6461
Daniel Vetterf0947c32012-07-02 13:10:34 +02006462/* Simple connector->get_hw_state implementation for encoders that support only
6463 * one connector and no cloning and hence the encoder state determines the state
6464 * of the connector. */
6465bool intel_connector_get_hw_state(struct intel_connector *connector)
6466{
Daniel Vetter24929352012-07-02 20:28:59 +02006467 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006468 struct intel_encoder *encoder = connector->encoder;
6469
6470 return encoder->get_hw_state(encoder, &pipe);
6471}
6472
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006473static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006474{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006475 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6476 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006477
6478 return 0;
6479}
6480
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006481static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006482 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006483{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006484 struct drm_atomic_state *state = pipe_config->base.state;
6485 struct intel_crtc *other_crtc;
6486 struct intel_crtc_state *other_crtc_state;
6487
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006488 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6489 pipe_name(pipe), pipe_config->fdi_lanes);
6490 if (pipe_config->fdi_lanes > 4) {
6491 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6492 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006493 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006494 }
6495
Paulo Zanonibafb6552013-11-02 21:07:44 -07006496 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006497 if (pipe_config->fdi_lanes > 2) {
6498 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6499 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006500 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006501 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006502 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006503 }
6504 }
6505
6506 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006507 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006508
6509 /* Ivybridge 3 pipe is really complicated */
6510 switch (pipe) {
6511 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006512 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006513 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006514 if (pipe_config->fdi_lanes <= 2)
6515 return 0;
6516
6517 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6518 other_crtc_state =
6519 intel_atomic_get_crtc_state(state, other_crtc);
6520 if (IS_ERR(other_crtc_state))
6521 return PTR_ERR(other_crtc_state);
6522
6523 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006524 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6525 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006526 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006527 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006528 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006529 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006530 if (pipe_config->fdi_lanes > 2) {
6531 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6532 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006533 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006534 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006535
6536 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6537 other_crtc_state =
6538 intel_atomic_get_crtc_state(state, other_crtc);
6539 if (IS_ERR(other_crtc_state))
6540 return PTR_ERR(other_crtc_state);
6541
6542 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006543 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006544 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006545 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006546 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006547 default:
6548 BUG();
6549 }
6550}
6551
Daniel Vettere29c22c2013-02-21 00:00:16 +01006552#define RETRY 1
6553static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006554 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006555{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006556 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006557 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006558 int lane, link_bw, fdi_dotclock, ret;
6559 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006560
Daniel Vettere29c22c2013-02-21 00:00:16 +01006561retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006562 /* FDI is a binary signal running at ~2.7GHz, encoding
6563 * each output octet as 10 bits. The actual frequency
6564 * is stored as a divider into a 100MHz clock, and the
6565 * mode pixel clock is stored in units of 1KHz.
6566 * Hence the bw of each lane in terms of the mode signal
6567 * is:
6568 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006569 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006570
Damien Lespiau241bfc32013-09-25 16:45:37 +01006571 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006572
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006573 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006574 pipe_config->pipe_bpp);
6575
6576 pipe_config->fdi_lanes = lane;
6577
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006578 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006579 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006580
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006581 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006582 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006583 pipe_config->pipe_bpp -= 2*3;
6584 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6585 pipe_config->pipe_bpp);
6586 needs_recompute = true;
6587 pipe_config->bw_constrained = true;
6588
6589 goto retry;
6590 }
6591
6592 if (needs_recompute)
6593 return RETRY;
6594
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006595 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006596}
6597
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006598static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6599 struct intel_crtc_state *pipe_config)
6600{
6601 if (pipe_config->pipe_bpp > 24)
6602 return false;
6603
6604 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006605 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006606 return true;
6607
6608 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006609 * We compare against max which means we must take
6610 * the increased cdclk requirement into account when
6611 * calculating the new cdclk.
6612 *
6613 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006614 */
6615 return ilk_pipe_pixel_rate(pipe_config) <=
6616 dev_priv->max_cdclk_freq * 95 / 100;
6617}
6618
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006619static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006620 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006621{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006622 struct drm_device *dev = crtc->base.dev;
6623 struct drm_i915_private *dev_priv = dev->dev_private;
6624
Jani Nikulad330a952014-01-21 11:24:25 +02006625 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006626 hsw_crtc_supports_ips(crtc) &&
6627 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006628}
6629
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006630static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6631{
6632 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6633
6634 /* GDG double wide on either pipe, otherwise pipe A only */
6635 return INTEL_INFO(dev_priv)->gen < 4 &&
6636 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6637}
6638
Daniel Vettera43f6e02013-06-07 23:10:32 +02006639static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006640 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006641{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006642 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006643 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006644 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006645 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006646
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006647 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006648 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006649
6650 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006651 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006652 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006653 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006654 if (intel_crtc_supports_double_wide(crtc) &&
6655 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006656 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006657 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006658 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006659 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006660
Ville Syrjäläf3261152016-05-24 21:34:18 +03006661 if (adjusted_mode->crtc_clock > clock_limit) {
6662 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6663 adjusted_mode->crtc_clock, clock_limit,
6664 yesno(pipe_config->double_wide));
6665 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006666 }
Chris Wilson89749352010-09-12 18:25:19 +01006667
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006668 /*
6669 * Pipe horizontal size must be even in:
6670 * - DVO ganged mode
6671 * - LVDS dual channel mode
6672 * - Double wide pipe
6673 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006674 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006675 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6676 pipe_config->pipe_src_w &= ~1;
6677
Damien Lespiau8693a822013-05-03 18:48:11 +01006678 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6679 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006680 */
6681 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006682 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006683 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006684
Damien Lespiauf5adf942013-06-24 18:29:34 +01006685 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006686 hsw_compute_ips_config(crtc, pipe_config);
6687
Daniel Vetter877d48d2013-04-19 11:24:43 +02006688 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006689 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006690
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006691 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006692}
6693
Ville Syrjälä1652d192015-03-31 14:12:01 +03006694static int skylake_get_display_clock_speed(struct drm_device *dev)
6695{
6696 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006697 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006698
Ville Syrjäläea617912016-05-13 23:41:24 +03006699 skl_dpll0_update(dev_priv);
6700
Ville Syrjälä63911d72016-05-13 23:41:32 +03006701 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006702 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006703
Ville Syrjäläea617912016-05-13 23:41:24 +03006704 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006705
Ville Syrjälä63911d72016-05-13 23:41:32 +03006706 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006707 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6708 case CDCLK_FREQ_450_432:
6709 return 432000;
6710 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006711 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006712 case CDCLK_FREQ_540:
6713 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006714 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006715 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006716 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006717 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006718 }
6719 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006720 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6721 case CDCLK_FREQ_450_432:
6722 return 450000;
6723 case CDCLK_FREQ_337_308:
6724 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006725 case CDCLK_FREQ_540:
6726 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006727 case CDCLK_FREQ_675_617:
6728 return 675000;
6729 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006730 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006731 }
6732 }
6733
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006734 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006735}
6736
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006737static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6738{
6739 u32 val;
6740
6741 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006742 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006743
6744 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006745 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006746 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006747
Imre Deak1c3f7702016-05-24 15:38:32 +03006748 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6749 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006750
6751 val = I915_READ(BXT_DE_PLL_CTL);
6752 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6753 dev_priv->cdclk_pll.ref;
6754}
6755
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006756static int broxton_get_display_clock_speed(struct drm_device *dev)
6757{
6758 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006759 u32 divider;
6760 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006761
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006762 bxt_de_pll_update(dev_priv);
6763
Ville Syrjäläf5986242016-05-13 23:41:37 +03006764 vco = dev_priv->cdclk_pll.vco;
6765 if (vco == 0)
6766 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006767
Ville Syrjäläf5986242016-05-13 23:41:37 +03006768 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006769
Ville Syrjäläf5986242016-05-13 23:41:37 +03006770 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006771 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006772 div = 2;
6773 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006774 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006775 div = 3;
6776 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006777 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006778 div = 4;
6779 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006780 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006781 div = 8;
6782 break;
6783 default:
6784 MISSING_CASE(divider);
6785 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006786 }
6787
Ville Syrjäläf5986242016-05-13 23:41:37 +03006788 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006789}
6790
Ville Syrjälä1652d192015-03-31 14:12:01 +03006791static int broadwell_get_display_clock_speed(struct drm_device *dev)
6792{
6793 struct drm_i915_private *dev_priv = dev->dev_private;
6794 uint32_t lcpll = I915_READ(LCPLL_CTL);
6795 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6796
6797 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6798 return 800000;
6799 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6800 return 450000;
6801 else if (freq == LCPLL_CLK_FREQ_450)
6802 return 450000;
6803 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6804 return 540000;
6805 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6806 return 337500;
6807 else
6808 return 675000;
6809}
6810
6811static int haswell_get_display_clock_speed(struct drm_device *dev)
6812{
6813 struct drm_i915_private *dev_priv = dev->dev_private;
6814 uint32_t lcpll = I915_READ(LCPLL_CTL);
6815 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6816
6817 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6818 return 800000;
6819 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6820 return 450000;
6821 else if (freq == LCPLL_CLK_FREQ_450)
6822 return 450000;
6823 else if (IS_HSW_ULT(dev))
6824 return 337500;
6825 else
6826 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006827}
6828
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006829static int valleyview_get_display_clock_speed(struct drm_device *dev)
6830{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006831 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6832 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006833}
6834
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006835static int ilk_get_display_clock_speed(struct drm_device *dev)
6836{
6837 return 450000;
6838}
6839
Jesse Barnese70236a2009-09-21 10:42:27 -07006840static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006841{
Jesse Barnese70236a2009-09-21 10:42:27 -07006842 return 400000;
6843}
Jesse Barnes79e53942008-11-07 14:24:08 -08006844
Jesse Barnese70236a2009-09-21 10:42:27 -07006845static int i915_get_display_clock_speed(struct drm_device *dev)
6846{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006847 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006848}
Jesse Barnes79e53942008-11-07 14:24:08 -08006849
Jesse Barnese70236a2009-09-21 10:42:27 -07006850static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6851{
6852 return 200000;
6853}
Jesse Barnes79e53942008-11-07 14:24:08 -08006854
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006855static int pnv_get_display_clock_speed(struct drm_device *dev)
6856{
6857 u16 gcfgc = 0;
6858
6859 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6860
6861 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6862 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006863 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006864 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006865 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006866 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006867 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006868 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6869 return 200000;
6870 default:
6871 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6872 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006873 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006874 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006875 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006876 }
6877}
6878
Jesse Barnese70236a2009-09-21 10:42:27 -07006879static int i915gm_get_display_clock_speed(struct drm_device *dev)
6880{
6881 u16 gcfgc = 0;
6882
6883 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6884
6885 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006886 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006887 else {
6888 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6889 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006890 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006891 default:
6892 case GC_DISPLAY_CLOCK_190_200_MHZ:
6893 return 190000;
6894 }
6895 }
6896}
Jesse Barnes79e53942008-11-07 14:24:08 -08006897
Jesse Barnese70236a2009-09-21 10:42:27 -07006898static int i865_get_display_clock_speed(struct drm_device *dev)
6899{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006900 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006901}
6902
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006903static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006904{
6905 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006906
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006907 /*
6908 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6909 * encoding is different :(
6910 * FIXME is this the right way to detect 852GM/852GMV?
6911 */
6912 if (dev->pdev->revision == 0x1)
6913 return 133333;
6914
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006915 pci_bus_read_config_word(dev->pdev->bus,
6916 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6917
Jesse Barnese70236a2009-09-21 10:42:27 -07006918 /* Assume that the hardware is in the high speed state. This
6919 * should be the default.
6920 */
6921 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6922 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006923 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006924 case GC_CLOCK_100_200:
6925 return 200000;
6926 case GC_CLOCK_166_250:
6927 return 250000;
6928 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006929 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006930 case GC_CLOCK_133_266:
6931 case GC_CLOCK_133_266_2:
6932 case GC_CLOCK_166_266:
6933 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006934 }
6935
6936 /* Shouldn't happen */
6937 return 0;
6938}
6939
6940static int i830_get_display_clock_speed(struct drm_device *dev)
6941{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006942 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006943}
6944
Ville Syrjälä34edce22015-05-22 11:22:33 +03006945static unsigned int intel_hpll_vco(struct drm_device *dev)
6946{
6947 struct drm_i915_private *dev_priv = dev->dev_private;
6948 static const unsigned int blb_vco[8] = {
6949 [0] = 3200000,
6950 [1] = 4000000,
6951 [2] = 5333333,
6952 [3] = 4800000,
6953 [4] = 6400000,
6954 };
6955 static const unsigned int pnv_vco[8] = {
6956 [0] = 3200000,
6957 [1] = 4000000,
6958 [2] = 5333333,
6959 [3] = 4800000,
6960 [4] = 2666667,
6961 };
6962 static const unsigned int cl_vco[8] = {
6963 [0] = 3200000,
6964 [1] = 4000000,
6965 [2] = 5333333,
6966 [3] = 6400000,
6967 [4] = 3333333,
6968 [5] = 3566667,
6969 [6] = 4266667,
6970 };
6971 static const unsigned int elk_vco[8] = {
6972 [0] = 3200000,
6973 [1] = 4000000,
6974 [2] = 5333333,
6975 [3] = 4800000,
6976 };
6977 static const unsigned int ctg_vco[8] = {
6978 [0] = 3200000,
6979 [1] = 4000000,
6980 [2] = 5333333,
6981 [3] = 6400000,
6982 [4] = 2666667,
6983 [5] = 4266667,
6984 };
6985 const unsigned int *vco_table;
6986 unsigned int vco;
6987 uint8_t tmp = 0;
6988
6989 /* FIXME other chipsets? */
6990 if (IS_GM45(dev))
6991 vco_table = ctg_vco;
6992 else if (IS_G4X(dev))
6993 vco_table = elk_vco;
6994 else if (IS_CRESTLINE(dev))
6995 vco_table = cl_vco;
6996 else if (IS_PINEVIEW(dev))
6997 vco_table = pnv_vco;
6998 else if (IS_G33(dev))
6999 vco_table = blb_vco;
7000 else
7001 return 0;
7002
7003 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7004
7005 vco = vco_table[tmp & 0x7];
7006 if (vco == 0)
7007 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7008 else
7009 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7010
7011 return vco;
7012}
7013
7014static int gm45_get_display_clock_speed(struct drm_device *dev)
7015{
7016 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7017 uint16_t tmp = 0;
7018
7019 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7020
7021 cdclk_sel = (tmp >> 12) & 0x1;
7022
7023 switch (vco) {
7024 case 2666667:
7025 case 4000000:
7026 case 5333333:
7027 return cdclk_sel ? 333333 : 222222;
7028 case 3200000:
7029 return cdclk_sel ? 320000 : 228571;
7030 default:
7031 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7032 return 222222;
7033 }
7034}
7035
7036static int i965gm_get_display_clock_speed(struct drm_device *dev)
7037{
7038 static const uint8_t div_3200[] = { 16, 10, 8 };
7039 static const uint8_t div_4000[] = { 20, 12, 10 };
7040 static const uint8_t div_5333[] = { 24, 16, 14 };
7041 const uint8_t *div_table;
7042 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7043 uint16_t tmp = 0;
7044
7045 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7046
7047 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7048
7049 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7050 goto fail;
7051
7052 switch (vco) {
7053 case 3200000:
7054 div_table = div_3200;
7055 break;
7056 case 4000000:
7057 div_table = div_4000;
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%04x\n", vco, tmp);
7070 return 200000;
7071}
7072
7073static int g33_get_display_clock_speed(struct drm_device *dev)
7074{
7075 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7076 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7077 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7078 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7079 const uint8_t *div_table;
7080 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7081 uint16_t tmp = 0;
7082
7083 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7084
7085 cdclk_sel = (tmp >> 4) & 0x7;
7086
7087 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7088 goto fail;
7089
7090 switch (vco) {
7091 case 3200000:
7092 div_table = div_3200;
7093 break;
7094 case 4000000:
7095 div_table = div_4000;
7096 break;
7097 case 4800000:
7098 div_table = div_4800;
7099 break;
7100 case 5333333:
7101 div_table = div_5333;
7102 break;
7103 default:
7104 goto fail;
7105 }
7106
7107 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7108
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007109fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007110 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7111 return 190476;
7112}
7113
Zhenyu Wang2c072452009-06-05 15:38:42 +08007114static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007115intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007116{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007117 while (*num > DATA_LINK_M_N_MASK ||
7118 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007119 *num >>= 1;
7120 *den >>= 1;
7121 }
7122}
7123
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007124static void compute_m_n(unsigned int m, unsigned int n,
7125 uint32_t *ret_m, uint32_t *ret_n)
7126{
7127 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7128 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7129 intel_reduce_m_n_ratio(ret_m, ret_n);
7130}
7131
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007132void
7133intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7134 int pixel_clock, int link_clock,
7135 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007136{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007137 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007138
7139 compute_m_n(bits_per_pixel * pixel_clock,
7140 link_clock * nlanes * 8,
7141 &m_n->gmch_m, &m_n->gmch_n);
7142
7143 compute_m_n(pixel_clock, link_clock,
7144 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007145}
7146
Chris Wilsona7615032011-01-12 17:04:08 +00007147static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7148{
Jani Nikulad330a952014-01-21 11:24:25 +02007149 if (i915.panel_use_ssc >= 0)
7150 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007151 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007152 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007153}
7154
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007155static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007156{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007157 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007158}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007159
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007160static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7161{
7162 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007163}
7164
Daniel Vetterf47709a2013-03-28 10:42:02 +01007165static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007166 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007167 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007168{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007169 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007170 u32 fp, fp2 = 0;
7171
7172 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007173 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007174 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007175 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007176 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007177 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007178 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007179 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007180 }
7181
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007182 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007183
Daniel Vetterf47709a2013-03-28 10:42:02 +01007184 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007185 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007186 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007187 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007188 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007189 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007190 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007191 }
7192}
7193
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007194static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7195 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007196{
7197 u32 reg_val;
7198
7199 /*
7200 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7201 * and set it to a reasonable value instead.
7202 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007203 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007204 reg_val &= 0xffffff00;
7205 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007206 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007207
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007208 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007209 reg_val &= 0x8cffffff;
7210 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007211 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007212
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007213 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007214 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007215 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007216
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007217 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007218 reg_val &= 0x00ffffff;
7219 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007220 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007221}
7222
Daniel Vetterb5518422013-05-03 11:49:48 +02007223static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7224 struct intel_link_m_n *m_n)
7225{
7226 struct drm_device *dev = crtc->base.dev;
7227 struct drm_i915_private *dev_priv = dev->dev_private;
7228 int pipe = crtc->pipe;
7229
Daniel Vettere3b95f12013-05-03 11:49:49 +02007230 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7231 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7232 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7233 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007234}
7235
7236static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007237 struct intel_link_m_n *m_n,
7238 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007239{
7240 struct drm_device *dev = crtc->base.dev;
7241 struct drm_i915_private *dev_priv = dev->dev_private;
7242 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007243 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007244
7245 if (INTEL_INFO(dev)->gen >= 5) {
7246 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7247 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7248 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7249 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007250 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7251 * for gen < 8) and if DRRS is supported (to make sure the
7252 * registers are not unnecessarily accessed).
7253 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307254 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007255 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007256 I915_WRITE(PIPE_DATA_M2(transcoder),
7257 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7258 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7259 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7260 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7261 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007262 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007263 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7264 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7265 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7266 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007267 }
7268}
7269
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307270void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007271{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307272 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7273
7274 if (m_n == M1_N1) {
7275 dp_m_n = &crtc->config->dp_m_n;
7276 dp_m2_n2 = &crtc->config->dp_m2_n2;
7277 } else if (m_n == M2_N2) {
7278
7279 /*
7280 * M2_N2 registers are not supported. Hence m2_n2 divider value
7281 * needs to be programmed into M1_N1.
7282 */
7283 dp_m_n = &crtc->config->dp_m2_n2;
7284 } else {
7285 DRM_ERROR("Unsupported divider value\n");
7286 return;
7287 }
7288
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007289 if (crtc->config->has_pch_encoder)
7290 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007291 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307292 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007293}
7294
Daniel Vetter251ac862015-06-18 10:30:24 +02007295static void vlv_compute_dpll(struct intel_crtc *crtc,
7296 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007297{
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007298 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007299 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007300 if (crtc->pipe != PIPE_A)
7301 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007302
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007303 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007304 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007305 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7306 DPLL_EXT_BUFFER_ENABLE_VLV;
7307
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007308 pipe_config->dpll_hw_state.dpll_md =
7309 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7310}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007311
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007312static void chv_compute_dpll(struct intel_crtc *crtc,
7313 struct intel_crtc_state *pipe_config)
7314{
7315 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007316 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007317 if (crtc->pipe != PIPE_A)
7318 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7319
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007320 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007321 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007322 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7323
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007324 pipe_config->dpll_hw_state.dpll_md =
7325 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007326}
7327
Ville Syrjäläd288f652014-10-28 13:20:22 +02007328static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007329 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007330{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007331 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007332 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007333 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007334 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007335 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007336 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007337
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007338 /* Enable Refclk */
7339 I915_WRITE(DPLL(pipe),
7340 pipe_config->dpll_hw_state.dpll &
7341 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7342
7343 /* No need to actually set up the DPLL with DSI */
7344 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7345 return;
7346
Ville Syrjäläa5805162015-05-26 20:42:30 +03007347 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007348
Ville Syrjäläd288f652014-10-28 13:20:22 +02007349 bestn = pipe_config->dpll.n;
7350 bestm1 = pipe_config->dpll.m1;
7351 bestm2 = pipe_config->dpll.m2;
7352 bestp1 = pipe_config->dpll.p1;
7353 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007354
Jesse Barnes89b667f2013-04-18 14:51:36 -07007355 /* See eDP HDMI DPIO driver vbios notes doc */
7356
7357 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007358 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007359 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007360
7361 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007362 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007363
7364 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007365 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007366 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007367 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007368
7369 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007370 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007371
7372 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007373 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7374 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7375 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007376 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007377
7378 /*
7379 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7380 * but we don't support that).
7381 * Note: don't use the DAC post divider as it seems unstable.
7382 */
7383 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007384 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007385
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007386 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007387 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007388
Jesse Barnes89b667f2013-04-18 14:51:36 -07007389 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007390 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007391 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7392 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007393 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007394 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007395 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007396 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007397 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007398
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007399 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007400 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007401 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007402 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007403 0x0df40000);
7404 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007405 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007406 0x0df70000);
7407 } else { /* HDMI or VGA */
7408 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007409 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007410 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007411 0x0df70000);
7412 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007413 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007414 0x0df40000);
7415 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007416
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007417 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007418 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007419 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7420 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007421 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007422 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007423
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007424 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007425 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007426}
7427
Ville Syrjäläd288f652014-10-28 13:20:22 +02007428static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007429 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007430{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007431 struct drm_device *dev = crtc->base.dev;
7432 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007433 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007434 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307435 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007436 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307437 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307438 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007439
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007440 /* Enable Refclk and SSC */
7441 I915_WRITE(DPLL(pipe),
7442 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7443
7444 /* No need to actually set up the DPLL with DSI */
7445 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7446 return;
7447
Ville Syrjäläd288f652014-10-28 13:20:22 +02007448 bestn = pipe_config->dpll.n;
7449 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7450 bestm1 = pipe_config->dpll.m1;
7451 bestm2 = pipe_config->dpll.m2 >> 22;
7452 bestp1 = pipe_config->dpll.p1;
7453 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307454 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307455 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307456 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007457
Ville Syrjäläa5805162015-05-26 20:42:30 +03007458 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007459
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007460 /* p1 and p2 divider */
7461 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7462 5 << DPIO_CHV_S1_DIV_SHIFT |
7463 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7464 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7465 1 << DPIO_CHV_K_DIV_SHIFT);
7466
7467 /* Feedback post-divider - m2 */
7468 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7469
7470 /* Feedback refclk divider - n and m1 */
7471 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7472 DPIO_CHV_M1_DIV_BY_2 |
7473 1 << DPIO_CHV_N_DIV_SHIFT);
7474
7475 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007476 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007477
7478 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307479 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7480 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7481 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7482 if (bestm2_frac)
7483 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7484 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007485
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307486 /* Program digital lock detect threshold */
7487 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7488 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7489 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7490 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7491 if (!bestm2_frac)
7492 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7493 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7494
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007495 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307496 if (vco == 5400000) {
7497 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7498 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7499 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7500 tribuf_calcntr = 0x9;
7501 } else if (vco <= 6200000) {
7502 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7503 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7504 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7505 tribuf_calcntr = 0x9;
7506 } else if (vco <= 6480000) {
7507 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7508 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7509 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7510 tribuf_calcntr = 0x8;
7511 } else {
7512 /* Not supported. Apply the same limits as in the max case */
7513 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7514 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7515 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7516 tribuf_calcntr = 0;
7517 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007518 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7519
Ville Syrjälä968040b2015-03-11 22:52:08 +02007520 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307521 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7522 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7523 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7524
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007525 /* AFC Recal */
7526 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7527 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7528 DPIO_AFC_RECAL);
7529
Ville Syrjäläa5805162015-05-26 20:42:30 +03007530 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007531}
7532
Ville Syrjäläd288f652014-10-28 13:20:22 +02007533/**
7534 * vlv_force_pll_on - forcibly enable just the PLL
7535 * @dev_priv: i915 private structure
7536 * @pipe: pipe PLL to enable
7537 * @dpll: PLL configuration
7538 *
7539 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7540 * in cases where we need the PLL enabled even when @pipe is not going to
7541 * be enabled.
7542 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007543int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7544 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007545{
7546 struct intel_crtc *crtc =
7547 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007548 struct intel_crtc_state *pipe_config;
7549
7550 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7551 if (!pipe_config)
7552 return -ENOMEM;
7553
7554 pipe_config->base.crtc = &crtc->base;
7555 pipe_config->pixel_multiplier = 1;
7556 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007557
7558 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007559 chv_compute_dpll(crtc, pipe_config);
7560 chv_prepare_pll(crtc, pipe_config);
7561 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007562 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007563 vlv_compute_dpll(crtc, pipe_config);
7564 vlv_prepare_pll(crtc, pipe_config);
7565 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007566 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007567
7568 kfree(pipe_config);
7569
7570 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007571}
7572
7573/**
7574 * vlv_force_pll_off - forcibly disable just the PLL
7575 * @dev_priv: i915 private structure
7576 * @pipe: pipe PLL to disable
7577 *
7578 * Disable the PLL for @pipe. To be used in cases where we need
7579 * the PLL enabled even when @pipe is not going to be enabled.
7580 */
7581void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7582{
7583 if (IS_CHERRYVIEW(dev))
7584 chv_disable_pll(to_i915(dev), pipe);
7585 else
7586 vlv_disable_pll(to_i915(dev), pipe);
7587}
7588
Daniel Vetter251ac862015-06-18 10:30:24 +02007589static void i9xx_compute_dpll(struct intel_crtc *crtc,
7590 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007591 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007592{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007593 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007594 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007595 u32 dpll;
7596 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007597 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007598
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007599 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307600
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007601 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7602 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007603
7604 dpll = DPLL_VGA_MODE_DIS;
7605
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007606 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007607 dpll |= DPLLB_MODE_LVDS;
7608 else
7609 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007610
Daniel Vetteref1b4602013-06-01 17:17:04 +02007611 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007612 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007613 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007614 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007615
7616 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007617 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007618
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007619 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007620 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007621
7622 /* compute bitmask from p1 value */
7623 if (IS_PINEVIEW(dev))
7624 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7625 else {
7626 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7627 if (IS_G4X(dev) && reduced_clock)
7628 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7629 }
7630 switch (clock->p2) {
7631 case 5:
7632 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7633 break;
7634 case 7:
7635 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7636 break;
7637 case 10:
7638 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7639 break;
7640 case 14:
7641 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7642 break;
7643 }
7644 if (INTEL_INFO(dev)->gen >= 4)
7645 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7646
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007647 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007648 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007649 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007650 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007651 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7652 else
7653 dpll |= PLL_REF_INPUT_DREFCLK;
7654
7655 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007656 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007657
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007658 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007659 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007660 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007661 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007662 }
7663}
7664
Daniel Vetter251ac862015-06-18 10:30:24 +02007665static void i8xx_compute_dpll(struct intel_crtc *crtc,
7666 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007667 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007668{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007669 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007670 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007671 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007672 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007673
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007674 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307675
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007676 dpll = DPLL_VGA_MODE_DIS;
7677
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007678 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007679 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7680 } else {
7681 if (clock->p1 == 2)
7682 dpll |= PLL_P1_DIVIDE_BY_TWO;
7683 else
7684 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7685 if (clock->p2 == 4)
7686 dpll |= PLL_P2_DIVIDE_BY_4;
7687 }
7688
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007689 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007690 dpll |= DPLL_DVO_2X_MODE;
7691
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007692 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007693 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007694 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7695 else
7696 dpll |= PLL_REF_INPUT_DREFCLK;
7697
7698 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007699 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007700}
7701
Daniel Vetter8a654f32013-06-01 17:16:22 +02007702static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007703{
7704 struct drm_device *dev = intel_crtc->base.dev;
7705 struct drm_i915_private *dev_priv = dev->dev_private;
7706 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007707 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007708 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007709 uint32_t crtc_vtotal, crtc_vblank_end;
7710 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007711
7712 /* We need to be careful not to changed the adjusted mode, for otherwise
7713 * the hw state checker will get angry at the mismatch. */
7714 crtc_vtotal = adjusted_mode->crtc_vtotal;
7715 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007716
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007717 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007718 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007719 crtc_vtotal -= 1;
7720 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007721
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007722 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007723 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7724 else
7725 vsyncshift = adjusted_mode->crtc_hsync_start -
7726 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007727 if (vsyncshift < 0)
7728 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007729 }
7730
7731 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007732 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007733
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007734 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007735 (adjusted_mode->crtc_hdisplay - 1) |
7736 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007737 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007738 (adjusted_mode->crtc_hblank_start - 1) |
7739 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007740 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007741 (adjusted_mode->crtc_hsync_start - 1) |
7742 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7743
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007744 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007745 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007746 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007747 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007748 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007749 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007750 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007751 (adjusted_mode->crtc_vsync_start - 1) |
7752 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7753
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007754 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7755 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7756 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7757 * bits. */
7758 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7759 (pipe == PIPE_B || pipe == PIPE_C))
7760 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7761
Jani Nikulabc58be62016-03-18 17:05:39 +02007762}
7763
7764static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7765{
7766 struct drm_device *dev = intel_crtc->base.dev;
7767 struct drm_i915_private *dev_priv = dev->dev_private;
7768 enum pipe pipe = intel_crtc->pipe;
7769
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007770 /* pipesrc controls the size that is scaled from, which should
7771 * always be the user's requested size.
7772 */
7773 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007774 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7775 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007776}
7777
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007778static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007779 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007780{
7781 struct drm_device *dev = crtc->base.dev;
7782 struct drm_i915_private *dev_priv = dev->dev_private;
7783 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7784 uint32_t tmp;
7785
7786 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007787 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7788 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007789 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007790 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7791 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007792 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007793 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7794 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007795
7796 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007797 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7798 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007799 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007800 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7801 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007802 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007803 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7804 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007805
7806 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007807 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7808 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7809 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007810 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007811}
7812
7813static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7814 struct intel_crtc_state *pipe_config)
7815{
7816 struct drm_device *dev = crtc->base.dev;
7817 struct drm_i915_private *dev_priv = dev->dev_private;
7818 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007819
7820 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007821 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7822 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7823
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007824 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7825 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007826}
7827
Daniel Vetterf6a83282014-02-11 15:28:57 -08007828void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007829 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007830{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007831 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7832 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7833 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7834 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007835
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007836 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7837 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7838 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7839 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007840
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007841 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007842 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007843
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007844 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7845 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007846
7847 mode->hsync = drm_mode_hsync(mode);
7848 mode->vrefresh = drm_mode_vrefresh(mode);
7849 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007850}
7851
Daniel Vetter84b046f2013-02-19 18:48:54 +01007852static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7853{
7854 struct drm_device *dev = intel_crtc->base.dev;
7855 struct drm_i915_private *dev_priv = dev->dev_private;
7856 uint32_t pipeconf;
7857
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007858 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007859
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007860 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7861 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7862 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007863
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007864 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007865 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007866
Daniel Vetterff9ce462013-04-24 14:57:17 +02007867 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007868 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007869 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007870 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007871 pipeconf |= PIPECONF_DITHER_EN |
7872 PIPECONF_DITHER_TYPE_SP;
7873
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007874 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007875 case 18:
7876 pipeconf |= PIPECONF_6BPC;
7877 break;
7878 case 24:
7879 pipeconf |= PIPECONF_8BPC;
7880 break;
7881 case 30:
7882 pipeconf |= PIPECONF_10BPC;
7883 break;
7884 default:
7885 /* Case prevented by intel_choose_pipe_bpp_dither. */
7886 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007887 }
7888 }
7889
7890 if (HAS_PIPE_CXSR(dev)) {
7891 if (intel_crtc->lowfreq_avail) {
7892 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7893 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7894 } else {
7895 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007896 }
7897 }
7898
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007899 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007900 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007901 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007902 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7903 else
7904 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7905 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007906 pipeconf |= PIPECONF_PROGRESSIVE;
7907
Wayne Boyer666a4532015-12-09 12:29:35 -08007908 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7909 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007910 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007911
Daniel Vetter84b046f2013-02-19 18:48:54 +01007912 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7913 POSTING_READ(PIPECONF(intel_crtc->pipe));
7914}
7915
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007916static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7917 struct intel_crtc_state *crtc_state)
7918{
7919 struct drm_device *dev = crtc->base.dev;
7920 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007921 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007922 int refclk = 48000;
7923
7924 memset(&crtc_state->dpll_hw_state, 0,
7925 sizeof(crtc_state->dpll_hw_state));
7926
7927 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7928 if (intel_panel_use_ssc(dev_priv)) {
7929 refclk = dev_priv->vbt.lvds_ssc_freq;
7930 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7931 }
7932
7933 limit = &intel_limits_i8xx_lvds;
7934 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7935 limit = &intel_limits_i8xx_dvo;
7936 } else {
7937 limit = &intel_limits_i8xx_dac;
7938 }
7939
7940 if (!crtc_state->clock_set &&
7941 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7942 refclk, NULL, &crtc_state->dpll)) {
7943 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7944 return -EINVAL;
7945 }
7946
7947 i8xx_compute_dpll(crtc, crtc_state, NULL);
7948
7949 return 0;
7950}
7951
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007952static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7953 struct intel_crtc_state *crtc_state)
7954{
7955 struct drm_device *dev = crtc->base.dev;
7956 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007957 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007958 int refclk = 96000;
7959
7960 memset(&crtc_state->dpll_hw_state, 0,
7961 sizeof(crtc_state->dpll_hw_state));
7962
7963 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7964 if (intel_panel_use_ssc(dev_priv)) {
7965 refclk = dev_priv->vbt.lvds_ssc_freq;
7966 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7967 }
7968
7969 if (intel_is_dual_link_lvds(dev))
7970 limit = &intel_limits_g4x_dual_channel_lvds;
7971 else
7972 limit = &intel_limits_g4x_single_channel_lvds;
7973 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7974 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7975 limit = &intel_limits_g4x_hdmi;
7976 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7977 limit = &intel_limits_g4x_sdvo;
7978 } else {
7979 /* The option is for other outputs */
7980 limit = &intel_limits_i9xx_sdvo;
7981 }
7982
7983 if (!crtc_state->clock_set &&
7984 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7985 refclk, NULL, &crtc_state->dpll)) {
7986 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7987 return -EINVAL;
7988 }
7989
7990 i9xx_compute_dpll(crtc, crtc_state, NULL);
7991
7992 return 0;
7993}
7994
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007995static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7996 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007997{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007998 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007999 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008000 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008001 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008002
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008003 memset(&crtc_state->dpll_hw_state, 0,
8004 sizeof(crtc_state->dpll_hw_state));
8005
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008006 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8007 if (intel_panel_use_ssc(dev_priv)) {
8008 refclk = dev_priv->vbt.lvds_ssc_freq;
8009 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8010 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008011
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008012 limit = &intel_limits_pineview_lvds;
8013 } else {
8014 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008015 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008016
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008017 if (!crtc_state->clock_set &&
8018 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8019 refclk, NULL, &crtc_state->dpll)) {
8020 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8021 return -EINVAL;
8022 }
8023
8024 i9xx_compute_dpll(crtc, crtc_state, NULL);
8025
8026 return 0;
8027}
8028
8029static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8030 struct intel_crtc_state *crtc_state)
8031{
8032 struct drm_device *dev = crtc->base.dev;
8033 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008034 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008035 int refclk = 96000;
8036
8037 memset(&crtc_state->dpll_hw_state, 0,
8038 sizeof(crtc_state->dpll_hw_state));
8039
8040 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8041 if (intel_panel_use_ssc(dev_priv)) {
8042 refclk = dev_priv->vbt.lvds_ssc_freq;
8043 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008044 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008045
8046 limit = &intel_limits_i9xx_lvds;
8047 } else {
8048 limit = &intel_limits_i9xx_sdvo;
8049 }
8050
8051 if (!crtc_state->clock_set &&
8052 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8053 refclk, NULL, &crtc_state->dpll)) {
8054 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8055 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008056 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008057
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008058 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008059
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008060 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008061}
8062
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008063static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8064 struct intel_crtc_state *crtc_state)
8065{
8066 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008067 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008068
8069 memset(&crtc_state->dpll_hw_state, 0,
8070 sizeof(crtc_state->dpll_hw_state));
8071
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008072 if (!crtc_state->clock_set &&
8073 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8074 refclk, NULL, &crtc_state->dpll)) {
8075 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8076 return -EINVAL;
8077 }
8078
8079 chv_compute_dpll(crtc, crtc_state);
8080
8081 return 0;
8082}
8083
8084static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8085 struct intel_crtc_state *crtc_state)
8086{
8087 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008088 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008089
8090 memset(&crtc_state->dpll_hw_state, 0,
8091 sizeof(crtc_state->dpll_hw_state));
8092
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008093 if (!crtc_state->clock_set &&
8094 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8095 refclk, NULL, &crtc_state->dpll)) {
8096 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8097 return -EINVAL;
8098 }
8099
8100 vlv_compute_dpll(crtc, crtc_state);
8101
8102 return 0;
8103}
8104
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008105static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008106 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008107{
8108 struct drm_device *dev = crtc->base.dev;
8109 struct drm_i915_private *dev_priv = dev->dev_private;
8110 uint32_t tmp;
8111
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008112 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8113 return;
8114
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008115 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008116 if (!(tmp & PFIT_ENABLE))
8117 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008118
Daniel Vetter06922822013-07-11 13:35:40 +02008119 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008120 if (INTEL_INFO(dev)->gen < 4) {
8121 if (crtc->pipe != PIPE_B)
8122 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008123 } else {
8124 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8125 return;
8126 }
8127
Daniel Vetter06922822013-07-11 13:35:40 +02008128 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008129 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008130}
8131
Jesse Barnesacbec812013-09-20 11:29:32 -07008132static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008133 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008134{
8135 struct drm_device *dev = crtc->base.dev;
8136 struct drm_i915_private *dev_priv = dev->dev_private;
8137 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008138 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008139 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008140 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008141
Ville Syrjäläb5219732016-03-15 16:40:01 +02008142 /* In case of DSI, DPLL will not be used */
8143 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308144 return;
8145
Ville Syrjäläa5805162015-05-26 20:42:30 +03008146 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008147 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008148 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008149
8150 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8151 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8152 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8153 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8154 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8155
Imre Deakdccbea32015-06-22 23:35:51 +03008156 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008157}
8158
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008159static void
8160i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8161 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008162{
8163 struct drm_device *dev = crtc->base.dev;
8164 struct drm_i915_private *dev_priv = dev->dev_private;
8165 u32 val, base, offset;
8166 int pipe = crtc->pipe, plane = crtc->plane;
8167 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008168 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008169 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008170 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008171
Damien Lespiau42a7b082015-02-05 19:35:13 +00008172 val = I915_READ(DSPCNTR(plane));
8173 if (!(val & DISPLAY_PLANE_ENABLE))
8174 return;
8175
Damien Lespiaud9806c92015-01-21 14:07:19 +00008176 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008177 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008178 DRM_DEBUG_KMS("failed to alloc fb\n");
8179 return;
8180 }
8181
Damien Lespiau1b842c82015-01-21 13:50:54 +00008182 fb = &intel_fb->base;
8183
Daniel Vetter18c52472015-02-10 17:16:09 +00008184 if (INTEL_INFO(dev)->gen >= 4) {
8185 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008186 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008187 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8188 }
8189 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008190
8191 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008192 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008193 fb->pixel_format = fourcc;
8194 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008195
8196 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008197 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008198 offset = I915_READ(DSPTILEOFF(plane));
8199 else
8200 offset = I915_READ(DSPLINOFF(plane));
8201 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8202 } else {
8203 base = I915_READ(DSPADDR(plane));
8204 }
8205 plane_config->base = base;
8206
8207 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008208 fb->width = ((val >> 16) & 0xfff) + 1;
8209 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008210
8211 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008212 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008213
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008214 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008215 fb->pixel_format,
8216 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008217
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008218 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008219
Damien Lespiau2844a922015-01-20 12:51:48 +00008220 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8221 pipe_name(pipe), plane, fb->width, fb->height,
8222 fb->bits_per_pixel, base, fb->pitches[0],
8223 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008224
Damien Lespiau2d140302015-02-05 17:22:18 +00008225 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008226}
8227
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008228static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008229 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008230{
8231 struct drm_device *dev = crtc->base.dev;
8232 struct drm_i915_private *dev_priv = dev->dev_private;
8233 int pipe = pipe_config->cpu_transcoder;
8234 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008235 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008236 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008237 int refclk = 100000;
8238
Ville Syrjäläb5219732016-03-15 16:40:01 +02008239 /* In case of DSI, DPLL will not be used */
8240 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8241 return;
8242
Ville Syrjäläa5805162015-05-26 20:42:30 +03008243 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008244 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8245 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8246 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8247 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008248 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008249 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008250
8251 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008252 clock.m2 = (pll_dw0 & 0xff) << 22;
8253 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8254 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008255 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8256 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8257 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8258
Imre Deakdccbea32015-06-22 23:35:51 +03008259 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008260}
8261
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008262static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008263 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008264{
8265 struct drm_device *dev = crtc->base.dev;
8266 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008267 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008268 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008269 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008270
Imre Deak17290502016-02-12 18:55:11 +02008271 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8272 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008273 return false;
8274
Daniel Vettere143a212013-07-04 12:01:15 +02008275 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008276 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008277
Imre Deak17290502016-02-12 18:55:11 +02008278 ret = false;
8279
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008280 tmp = I915_READ(PIPECONF(crtc->pipe));
8281 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008282 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008283
Wayne Boyer666a4532015-12-09 12:29:35 -08008284 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008285 switch (tmp & PIPECONF_BPC_MASK) {
8286 case PIPECONF_6BPC:
8287 pipe_config->pipe_bpp = 18;
8288 break;
8289 case PIPECONF_8BPC:
8290 pipe_config->pipe_bpp = 24;
8291 break;
8292 case PIPECONF_10BPC:
8293 pipe_config->pipe_bpp = 30;
8294 break;
8295 default:
8296 break;
8297 }
8298 }
8299
Wayne Boyer666a4532015-12-09 12:29:35 -08008300 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8301 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008302 pipe_config->limited_color_range = true;
8303
Ville Syrjälä282740f2013-09-04 18:30:03 +03008304 if (INTEL_INFO(dev)->gen < 4)
8305 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8306
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008307 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008308 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008309
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008310 i9xx_get_pfit_config(crtc, pipe_config);
8311
Daniel Vetter6c49f242013-06-06 12:45:25 +02008312 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008313 /* No way to read it out on pipes B and C */
8314 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8315 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8316 else
8317 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008318 pipe_config->pixel_multiplier =
8319 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8320 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008321 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008322 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8323 tmp = I915_READ(DPLL(crtc->pipe));
8324 pipe_config->pixel_multiplier =
8325 ((tmp & SDVO_MULTIPLIER_MASK)
8326 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8327 } else {
8328 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8329 * port and will be fixed up in the encoder->get_config
8330 * function. */
8331 pipe_config->pixel_multiplier = 1;
8332 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008333 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008334 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008335 /*
8336 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8337 * on 830. Filter it out here so that we don't
8338 * report errors due to that.
8339 */
8340 if (IS_I830(dev))
8341 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8342
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008343 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8344 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008345 } else {
8346 /* Mask out read-only status bits. */
8347 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8348 DPLL_PORTC_READY_MASK |
8349 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008350 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008351
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008352 if (IS_CHERRYVIEW(dev))
8353 chv_crtc_clock_get(crtc, pipe_config);
8354 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008355 vlv_crtc_clock_get(crtc, pipe_config);
8356 else
8357 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008358
Ville Syrjälä0f646142015-08-26 19:39:18 +03008359 /*
8360 * Normally the dotclock is filled in by the encoder .get_config()
8361 * but in case the pipe is enabled w/o any ports we need a sane
8362 * default.
8363 */
8364 pipe_config->base.adjusted_mode.crtc_clock =
8365 pipe_config->port_clock / pipe_config->pixel_multiplier;
8366
Imre Deak17290502016-02-12 18:55:11 +02008367 ret = true;
8368
8369out:
8370 intel_display_power_put(dev_priv, power_domain);
8371
8372 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008373}
8374
Paulo Zanonidde86e22012-12-01 12:04:25 -02008375static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008376{
8377 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008378 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008379 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008380 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008381 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008382 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008383 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008384 bool has_ck505 = false;
8385 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008386 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008387
8388 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008389 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008390 switch (encoder->type) {
8391 case INTEL_OUTPUT_LVDS:
8392 has_panel = true;
8393 has_lvds = true;
8394 break;
8395 case INTEL_OUTPUT_EDP:
8396 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008397 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008398 has_cpu_edp = true;
8399 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008400 default:
8401 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008402 }
8403 }
8404
Keith Packard99eb6a02011-09-26 14:29:12 -07008405 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008406 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008407 can_ssc = has_ck505;
8408 } else {
8409 has_ck505 = false;
8410 can_ssc = true;
8411 }
8412
Lyude1c1a24d2016-06-14 11:04:09 -04008413 /* Check if any DPLLs are using the SSC source */
8414 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8415 u32 temp = I915_READ(PCH_DPLL(i));
8416
8417 if (!(temp & DPLL_VCO_ENABLE))
8418 continue;
8419
8420 if ((temp & PLL_REF_INPUT_MASK) ==
8421 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8422 using_ssc_source = true;
8423 break;
8424 }
8425 }
8426
8427 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8428 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008429
8430 /* Ironlake: try to setup display ref clock before DPLL
8431 * enabling. This is only under driver's control after
8432 * PCH B stepping, previous chipset stepping should be
8433 * ignoring this setting.
8434 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008435 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008436
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008437 /* As we must carefully and slowly disable/enable each source in turn,
8438 * compute the final state we want first and check if we need to
8439 * make any changes at all.
8440 */
8441 final = val;
8442 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008443 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008444 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008445 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008446 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8447
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008448 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008449 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008450 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008451
Keith Packard199e5d72011-09-22 12:01:57 -07008452 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008453 final |= DREF_SSC_SOURCE_ENABLE;
8454
8455 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8456 final |= DREF_SSC1_ENABLE;
8457
8458 if (has_cpu_edp) {
8459 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8460 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8461 else
8462 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8463 } else
8464 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008465 } else if (using_ssc_source) {
8466 final |= DREF_SSC_SOURCE_ENABLE;
8467 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008468 }
8469
8470 if (final == val)
8471 return;
8472
8473 /* Always enable nonspread source */
8474 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8475
8476 if (has_ck505)
8477 val |= DREF_NONSPREAD_CK505_ENABLE;
8478 else
8479 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8480
8481 if (has_panel) {
8482 val &= ~DREF_SSC_SOURCE_MASK;
8483 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008484
Keith Packard199e5d72011-09-22 12:01:57 -07008485 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008486 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008487 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008488 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008489 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008490 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008491
8492 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008493 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008494 POSTING_READ(PCH_DREF_CONTROL);
8495 udelay(200);
8496
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008497 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008498
8499 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008500 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008501 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008502 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008503 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008504 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008505 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008506 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008507 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008508
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008509 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008510 POSTING_READ(PCH_DREF_CONTROL);
8511 udelay(200);
8512 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008513 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008514
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008515 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008516
8517 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008518 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008519
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008520 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008521 POSTING_READ(PCH_DREF_CONTROL);
8522 udelay(200);
8523
Lyude1c1a24d2016-06-14 11:04:09 -04008524 if (!using_ssc_source) {
8525 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008526
Lyude1c1a24d2016-06-14 11:04:09 -04008527 /* Turn off the SSC source */
8528 val &= ~DREF_SSC_SOURCE_MASK;
8529 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008530
Lyude1c1a24d2016-06-14 11:04:09 -04008531 /* Turn off SSC1 */
8532 val &= ~DREF_SSC1_ENABLE;
8533
8534 I915_WRITE(PCH_DREF_CONTROL, val);
8535 POSTING_READ(PCH_DREF_CONTROL);
8536 udelay(200);
8537 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008538 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008539
8540 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008541}
8542
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008543static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008544{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008545 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008546
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008547 tmp = I915_READ(SOUTH_CHICKEN2);
8548 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8549 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008550
Imre Deakcf3598c2016-06-28 13:37:31 +03008551 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8552 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008553 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008554
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008555 tmp = I915_READ(SOUTH_CHICKEN2);
8556 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8557 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008558
Imre Deakcf3598c2016-06-28 13:37:31 +03008559 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8560 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008561 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008562}
8563
8564/* WaMPhyProgramming:hsw */
8565static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8566{
8567 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008568
8569 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8570 tmp &= ~(0xFF << 24);
8571 tmp |= (0x12 << 24);
8572 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8573
Paulo Zanonidde86e22012-12-01 12:04:25 -02008574 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8575 tmp |= (1 << 11);
8576 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8577
8578 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8579 tmp |= (1 << 11);
8580 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8581
Paulo Zanonidde86e22012-12-01 12:04:25 -02008582 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8583 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8584 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8585
8586 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8587 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8588 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8589
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008590 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8591 tmp &= ~(7 << 13);
8592 tmp |= (5 << 13);
8593 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008594
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008595 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8596 tmp &= ~(7 << 13);
8597 tmp |= (5 << 13);
8598 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008599
8600 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8601 tmp &= ~0xFF;
8602 tmp |= 0x1C;
8603 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8604
8605 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8606 tmp &= ~0xFF;
8607 tmp |= 0x1C;
8608 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8609
8610 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8611 tmp &= ~(0xFF << 16);
8612 tmp |= (0x1C << 16);
8613 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8614
8615 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8616 tmp &= ~(0xFF << 16);
8617 tmp |= (0x1C << 16);
8618 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8619
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008620 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8621 tmp |= (1 << 27);
8622 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008623
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008624 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8625 tmp |= (1 << 27);
8626 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008627
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008628 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8629 tmp &= ~(0xF << 28);
8630 tmp |= (4 << 28);
8631 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008632
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008633 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8634 tmp &= ~(0xF << 28);
8635 tmp |= (4 << 28);
8636 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008637}
8638
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008639/* Implements 3 different sequences from BSpec chapter "Display iCLK
8640 * Programming" based on the parameters passed:
8641 * - Sequence to enable CLKOUT_DP
8642 * - Sequence to enable CLKOUT_DP without spread
8643 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8644 */
8645static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8646 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008647{
8648 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008649 uint32_t reg, tmp;
8650
8651 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8652 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008653 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008654 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008655
Ville Syrjäläa5805162015-05-26 20:42:30 +03008656 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008657
8658 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8659 tmp &= ~SBI_SSCCTL_DISABLE;
8660 tmp |= SBI_SSCCTL_PATHALT;
8661 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8662
8663 udelay(24);
8664
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008665 if (with_spread) {
8666 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8667 tmp &= ~SBI_SSCCTL_PATHALT;
8668 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008669
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008670 if (with_fdi) {
8671 lpt_reset_fdi_mphy(dev_priv);
8672 lpt_program_fdi_mphy(dev_priv);
8673 }
8674 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008675
Ville Syrjäläc2699522015-08-27 23:55:59 +03008676 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008677 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8678 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8679 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008680
Ville Syrjäläa5805162015-05-26 20:42:30 +03008681 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008682}
8683
Paulo Zanoni47701c32013-07-23 11:19:25 -03008684/* Sequence to disable CLKOUT_DP */
8685static void lpt_disable_clkout_dp(struct drm_device *dev)
8686{
8687 struct drm_i915_private *dev_priv = dev->dev_private;
8688 uint32_t reg, tmp;
8689
Ville Syrjäläa5805162015-05-26 20:42:30 +03008690 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008691
Ville Syrjäläc2699522015-08-27 23:55:59 +03008692 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008693 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8694 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8695 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8696
8697 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8698 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8699 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8700 tmp |= SBI_SSCCTL_PATHALT;
8701 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8702 udelay(32);
8703 }
8704 tmp |= SBI_SSCCTL_DISABLE;
8705 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8706 }
8707
Ville Syrjäläa5805162015-05-26 20:42:30 +03008708 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008709}
8710
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008711#define BEND_IDX(steps) ((50 + (steps)) / 5)
8712
8713static const uint16_t sscdivintphase[] = {
8714 [BEND_IDX( 50)] = 0x3B23,
8715 [BEND_IDX( 45)] = 0x3B23,
8716 [BEND_IDX( 40)] = 0x3C23,
8717 [BEND_IDX( 35)] = 0x3C23,
8718 [BEND_IDX( 30)] = 0x3D23,
8719 [BEND_IDX( 25)] = 0x3D23,
8720 [BEND_IDX( 20)] = 0x3E23,
8721 [BEND_IDX( 15)] = 0x3E23,
8722 [BEND_IDX( 10)] = 0x3F23,
8723 [BEND_IDX( 5)] = 0x3F23,
8724 [BEND_IDX( 0)] = 0x0025,
8725 [BEND_IDX( -5)] = 0x0025,
8726 [BEND_IDX(-10)] = 0x0125,
8727 [BEND_IDX(-15)] = 0x0125,
8728 [BEND_IDX(-20)] = 0x0225,
8729 [BEND_IDX(-25)] = 0x0225,
8730 [BEND_IDX(-30)] = 0x0325,
8731 [BEND_IDX(-35)] = 0x0325,
8732 [BEND_IDX(-40)] = 0x0425,
8733 [BEND_IDX(-45)] = 0x0425,
8734 [BEND_IDX(-50)] = 0x0525,
8735};
8736
8737/*
8738 * Bend CLKOUT_DP
8739 * steps -50 to 50 inclusive, in steps of 5
8740 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8741 * change in clock period = -(steps / 10) * 5.787 ps
8742 */
8743static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8744{
8745 uint32_t tmp;
8746 int idx = BEND_IDX(steps);
8747
8748 if (WARN_ON(steps % 5 != 0))
8749 return;
8750
8751 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8752 return;
8753
8754 mutex_lock(&dev_priv->sb_lock);
8755
8756 if (steps % 10 != 0)
8757 tmp = 0xAAAAAAAB;
8758 else
8759 tmp = 0x00000000;
8760 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8761
8762 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8763 tmp &= 0xffff0000;
8764 tmp |= sscdivintphase[idx];
8765 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8766
8767 mutex_unlock(&dev_priv->sb_lock);
8768}
8769
8770#undef BEND_IDX
8771
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008772static void lpt_init_pch_refclk(struct drm_device *dev)
8773{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008774 struct intel_encoder *encoder;
8775 bool has_vga = false;
8776
Damien Lespiaub2784e12014-08-05 11:29:37 +01008777 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008778 switch (encoder->type) {
8779 case INTEL_OUTPUT_ANALOG:
8780 has_vga = true;
8781 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008782 default:
8783 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008784 }
8785 }
8786
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008787 if (has_vga) {
8788 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008789 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008790 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008791 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008792 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008793}
8794
Paulo Zanonidde86e22012-12-01 12:04:25 -02008795/*
8796 * Initialize reference clocks when the driver loads
8797 */
8798void intel_init_pch_refclk(struct drm_device *dev)
8799{
8800 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8801 ironlake_init_pch_refclk(dev);
8802 else if (HAS_PCH_LPT(dev))
8803 lpt_init_pch_refclk(dev);
8804}
8805
Daniel Vetter6ff93602013-04-19 11:24:36 +02008806static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008807{
8808 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8809 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8810 int pipe = intel_crtc->pipe;
8811 uint32_t val;
8812
Daniel Vetter78114072013-06-13 00:54:57 +02008813 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008814
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008815 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008816 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008817 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008818 break;
8819 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008820 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008821 break;
8822 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008823 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008824 break;
8825 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008826 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008827 break;
8828 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008829 /* Case prevented by intel_choose_pipe_bpp_dither. */
8830 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008831 }
8832
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008833 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008834 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8835
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008836 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008837 val |= PIPECONF_INTERLACED_ILK;
8838 else
8839 val |= PIPECONF_PROGRESSIVE;
8840
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008841 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008842 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008843
Paulo Zanonic8203562012-09-12 10:06:29 -03008844 I915_WRITE(PIPECONF(pipe), val);
8845 POSTING_READ(PIPECONF(pipe));
8846}
8847
Daniel Vetter6ff93602013-04-19 11:24:36 +02008848static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008849{
Jani Nikula391bf042016-03-18 17:05:40 +02008850 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008852 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008853 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008854
Jani Nikula391bf042016-03-18 17:05:40 +02008855 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008856 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8857
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008858 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008859 val |= PIPECONF_INTERLACED_ILK;
8860 else
8861 val |= PIPECONF_PROGRESSIVE;
8862
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008863 I915_WRITE(PIPECONF(cpu_transcoder), val);
8864 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008865}
8866
Jani Nikula391bf042016-03-18 17:05:40 +02008867static void haswell_set_pipemisc(struct drm_crtc *crtc)
8868{
8869 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8871
8872 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8873 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008874
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008875 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008876 case 18:
8877 val |= PIPEMISC_DITHER_6_BPC;
8878 break;
8879 case 24:
8880 val |= PIPEMISC_DITHER_8_BPC;
8881 break;
8882 case 30:
8883 val |= PIPEMISC_DITHER_10_BPC;
8884 break;
8885 case 36:
8886 val |= PIPEMISC_DITHER_12_BPC;
8887 break;
8888 default:
8889 /* Case prevented by pipe_config_set_bpp. */
8890 BUG();
8891 }
8892
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008893 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008894 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8895
Jani Nikula391bf042016-03-18 17:05:40 +02008896 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008897 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008898}
8899
Paulo Zanonid4b19312012-11-29 11:29:32 -02008900int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8901{
8902 /*
8903 * Account for spread spectrum to avoid
8904 * oversubscribing the link. Max center spread
8905 * is 2.5%; use 5% for safety's sake.
8906 */
8907 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008908 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008909}
8910
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008911static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008912{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008913 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008914}
8915
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008916static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8917 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008918 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008919{
8920 struct drm_crtc *crtc = &intel_crtc->base;
8921 struct drm_device *dev = crtc->dev;
8922 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008923 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008924 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008925 struct drm_connector_state *connector_state;
8926 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008927 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008928 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008929 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008930
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008931 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008932 if (connector_state->crtc != crtc_state->base.crtc)
8933 continue;
8934
8935 encoder = to_intel_encoder(connector_state->best_encoder);
8936
8937 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008938 case INTEL_OUTPUT_LVDS:
8939 is_lvds = true;
8940 break;
8941 case INTEL_OUTPUT_SDVO:
8942 case INTEL_OUTPUT_HDMI:
8943 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008944 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008945 default:
8946 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008947 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008948 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008949
Chris Wilsonc1858122010-12-03 21:35:48 +00008950 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008951 factor = 21;
8952 if (is_lvds) {
8953 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008954 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008955 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008956 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008957 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008958 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008959
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008960 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008961
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008962 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8963 fp |= FP_CB_TUNE;
8964
8965 if (reduced_clock) {
8966 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8967
8968 if (reduced_clock->m < factor * reduced_clock->n)
8969 fp2 |= FP_CB_TUNE;
8970 } else {
8971 fp2 = fp;
8972 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008973
Chris Wilson5eddb702010-09-11 13:48:45 +01008974 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008975
Eric Anholta07d6782011-03-30 13:01:08 -07008976 if (is_lvds)
8977 dpll |= DPLLB_MODE_LVDS;
8978 else
8979 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008980
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008981 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008982 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008983
8984 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008985 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008986 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008987 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008988
Eric Anholta07d6782011-03-30 13:01:08 -07008989 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008990 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008991 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008992 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008993
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008994 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008995 case 5:
8996 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8997 break;
8998 case 7:
8999 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9000 break;
9001 case 10:
9002 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9003 break;
9004 case 14:
9005 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9006 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009007 }
9008
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02009009 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009010 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009011 else
9012 dpll |= PLL_REF_INPUT_DREFCLK;
9013
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009014 dpll |= DPLL_VCO_ENABLE;
9015
9016 crtc_state->dpll_hw_state.dpll = dpll;
9017 crtc_state->dpll_hw_state.fp0 = fp;
9018 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009019}
9020
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009021static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9022 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009023{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009024 struct drm_device *dev = crtc->base.dev;
9025 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009026 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009027 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009028 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009029 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009030 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009031
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009032 memset(&crtc_state->dpll_hw_state, 0,
9033 sizeof(crtc_state->dpll_hw_state));
9034
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009035 crtc->lowfreq_avail = false;
9036
9037 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9038 if (!crtc_state->has_pch_encoder)
9039 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009040
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009041 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9042 if (intel_panel_use_ssc(dev_priv)) {
9043 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9044 dev_priv->vbt.lvds_ssc_freq);
9045 refclk = dev_priv->vbt.lvds_ssc_freq;
9046 }
9047
9048 if (intel_is_dual_link_lvds(dev)) {
9049 if (refclk == 100000)
9050 limit = &intel_limits_ironlake_dual_lvds_100m;
9051 else
9052 limit = &intel_limits_ironlake_dual_lvds;
9053 } else {
9054 if (refclk == 100000)
9055 limit = &intel_limits_ironlake_single_lvds_100m;
9056 else
9057 limit = &intel_limits_ironlake_single_lvds;
9058 }
9059 } else {
9060 limit = &intel_limits_ironlake_dac;
9061 }
9062
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009063 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009064 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9065 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009066 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9067 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009068 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009069
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009070 ironlake_compute_dpll(crtc, crtc_state,
9071 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009072
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009073 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9074 if (pll == NULL) {
9075 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9076 pipe_name(crtc->pipe));
9077 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009078 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009079
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009080 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9081 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009082 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009083
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009084 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009085}
9086
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009087static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9088 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009089{
9090 struct drm_device *dev = crtc->base.dev;
9091 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009092 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009093
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009094 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9095 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9096 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9097 & ~TU_SIZE_MASK;
9098 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9099 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9100 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9101}
9102
9103static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9104 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009105 struct intel_link_m_n *m_n,
9106 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009107{
9108 struct drm_device *dev = crtc->base.dev;
9109 struct drm_i915_private *dev_priv = dev->dev_private;
9110 enum pipe pipe = crtc->pipe;
9111
9112 if (INTEL_INFO(dev)->gen >= 5) {
9113 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9114 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9115 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9116 & ~TU_SIZE_MASK;
9117 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9118 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9119 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009120 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9121 * gen < 8) and if DRRS is supported (to make sure the
9122 * registers are not unnecessarily read).
9123 */
9124 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009125 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009126 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9127 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9128 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9129 & ~TU_SIZE_MASK;
9130 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9131 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9132 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9133 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009134 } else {
9135 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9136 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9137 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9138 & ~TU_SIZE_MASK;
9139 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9140 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9141 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9142 }
9143}
9144
9145void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009146 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009147{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009148 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009149 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9150 else
9151 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009152 &pipe_config->dp_m_n,
9153 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009154}
9155
Daniel Vetter72419202013-04-04 13:28:53 +02009156static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009157 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009158{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009159 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009160 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009161}
9162
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009163static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009164 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009165{
9166 struct drm_device *dev = crtc->base.dev;
9167 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009168 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9169 uint32_t ps_ctrl = 0;
9170 int id = -1;
9171 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009172
Chandra Kondurua1b22782015-04-07 15:28:45 -07009173 /* find scaler attached to this pipe */
9174 for (i = 0; i < crtc->num_scalers; i++) {
9175 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9176 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9177 id = i;
9178 pipe_config->pch_pfit.enabled = true;
9179 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9180 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9181 break;
9182 }
9183 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009184
Chandra Kondurua1b22782015-04-07 15:28:45 -07009185 scaler_state->scaler_id = id;
9186 if (id >= 0) {
9187 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9188 } else {
9189 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009190 }
9191}
9192
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009193static void
9194skylake_get_initial_plane_config(struct intel_crtc *crtc,
9195 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009196{
9197 struct drm_device *dev = crtc->base.dev;
9198 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009199 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009200 int pipe = crtc->pipe;
9201 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009202 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009203 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009204 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009205
Damien Lespiaud9806c92015-01-21 14:07:19 +00009206 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009207 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009208 DRM_DEBUG_KMS("failed to alloc fb\n");
9209 return;
9210 }
9211
Damien Lespiau1b842c82015-01-21 13:50:54 +00009212 fb = &intel_fb->base;
9213
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009214 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009215 if (!(val & PLANE_CTL_ENABLE))
9216 goto error;
9217
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009218 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9219 fourcc = skl_format_to_fourcc(pixel_format,
9220 val & PLANE_CTL_ORDER_RGBX,
9221 val & PLANE_CTL_ALPHA_MASK);
9222 fb->pixel_format = fourcc;
9223 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9224
Damien Lespiau40f46282015-02-27 11:15:21 +00009225 tiling = val & PLANE_CTL_TILED_MASK;
9226 switch (tiling) {
9227 case PLANE_CTL_TILED_LINEAR:
9228 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9229 break;
9230 case PLANE_CTL_TILED_X:
9231 plane_config->tiling = I915_TILING_X;
9232 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9233 break;
9234 case PLANE_CTL_TILED_Y:
9235 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9236 break;
9237 case PLANE_CTL_TILED_YF:
9238 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9239 break;
9240 default:
9241 MISSING_CASE(tiling);
9242 goto error;
9243 }
9244
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009245 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9246 plane_config->base = base;
9247
9248 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9249
9250 val = I915_READ(PLANE_SIZE(pipe, 0));
9251 fb->height = ((val >> 16) & 0xfff) + 1;
9252 fb->width = ((val >> 0) & 0x1fff) + 1;
9253
9254 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009255 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009256 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009257 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9258
9259 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009260 fb->pixel_format,
9261 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009262
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009263 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009264
9265 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9266 pipe_name(pipe), fb->width, fb->height,
9267 fb->bits_per_pixel, base, fb->pitches[0],
9268 plane_config->size);
9269
Damien Lespiau2d140302015-02-05 17:22:18 +00009270 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009271 return;
9272
9273error:
9274 kfree(fb);
9275}
9276
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009277static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009278 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009279{
9280 struct drm_device *dev = crtc->base.dev;
9281 struct drm_i915_private *dev_priv = dev->dev_private;
9282 uint32_t tmp;
9283
9284 tmp = I915_READ(PF_CTL(crtc->pipe));
9285
9286 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009287 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009288 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9289 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009290
9291 /* We currently do not free assignements of panel fitters on
9292 * ivb/hsw (since we don't use the higher upscaling modes which
9293 * differentiates them) so just WARN about this case for now. */
9294 if (IS_GEN7(dev)) {
9295 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9296 PF_PIPE_SEL_IVB(crtc->pipe));
9297 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009298 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009299}
9300
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009301static void
9302ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9303 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009304{
9305 struct drm_device *dev = crtc->base.dev;
9306 struct drm_i915_private *dev_priv = dev->dev_private;
9307 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009308 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009309 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009310 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009311 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009312 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009313
Damien Lespiau42a7b082015-02-05 19:35:13 +00009314 val = I915_READ(DSPCNTR(pipe));
9315 if (!(val & DISPLAY_PLANE_ENABLE))
9316 return;
9317
Damien Lespiaud9806c92015-01-21 14:07:19 +00009318 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009319 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009320 DRM_DEBUG_KMS("failed to alloc fb\n");
9321 return;
9322 }
9323
Damien Lespiau1b842c82015-01-21 13:50:54 +00009324 fb = &intel_fb->base;
9325
Daniel Vetter18c52472015-02-10 17:16:09 +00009326 if (INTEL_INFO(dev)->gen >= 4) {
9327 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009328 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009329 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9330 }
9331 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009332
9333 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009334 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009335 fb->pixel_format = fourcc;
9336 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009337
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009338 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009339 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009340 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009341 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009342 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009343 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009344 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009345 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009346 }
9347 plane_config->base = base;
9348
9349 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009350 fb->width = ((val >> 16) & 0xfff) + 1;
9351 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009352
9353 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009354 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009355
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009356 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009357 fb->pixel_format,
9358 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009359
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009360 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009361
Damien Lespiau2844a922015-01-20 12:51:48 +00009362 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9363 pipe_name(pipe), fb->width, fb->height,
9364 fb->bits_per_pixel, base, fb->pitches[0],
9365 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009366
Damien Lespiau2d140302015-02-05 17:22:18 +00009367 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009368}
9369
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009370static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009371 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009372{
9373 struct drm_device *dev = crtc->base.dev;
9374 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009375 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009376 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009377 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009378
Imre Deak17290502016-02-12 18:55:11 +02009379 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9380 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009381 return false;
9382
Daniel Vettere143a212013-07-04 12:01:15 +02009383 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009384 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009385
Imre Deak17290502016-02-12 18:55:11 +02009386 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009387 tmp = I915_READ(PIPECONF(crtc->pipe));
9388 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009389 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009390
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009391 switch (tmp & PIPECONF_BPC_MASK) {
9392 case PIPECONF_6BPC:
9393 pipe_config->pipe_bpp = 18;
9394 break;
9395 case PIPECONF_8BPC:
9396 pipe_config->pipe_bpp = 24;
9397 break;
9398 case PIPECONF_10BPC:
9399 pipe_config->pipe_bpp = 30;
9400 break;
9401 case PIPECONF_12BPC:
9402 pipe_config->pipe_bpp = 36;
9403 break;
9404 default:
9405 break;
9406 }
9407
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009408 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9409 pipe_config->limited_color_range = true;
9410
Daniel Vetterab9412b2013-05-03 11:49:46 +02009411 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009412 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009413 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009414
Daniel Vetter88adfff2013-03-28 10:42:01 +01009415 pipe_config->has_pch_encoder = true;
9416
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009417 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9418 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9419 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009420
9421 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009422
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009423 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009424 /*
9425 * The pipe->pch transcoder and pch transcoder->pll
9426 * mapping is fixed.
9427 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009428 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009429 } else {
9430 tmp = I915_READ(PCH_DPLL_SEL);
9431 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009432 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009433 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009434 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009435 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009436
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009437 pipe_config->shared_dpll =
9438 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9439 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009440
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009441 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9442 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009443
9444 tmp = pipe_config->dpll_hw_state.dpll;
9445 pipe_config->pixel_multiplier =
9446 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9447 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009448
9449 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009450 } else {
9451 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009452 }
9453
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009454 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009455 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009456
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009457 ironlake_get_pfit_config(crtc, pipe_config);
9458
Imre Deak17290502016-02-12 18:55:11 +02009459 ret = true;
9460
9461out:
9462 intel_display_power_put(dev_priv, power_domain);
9463
9464 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009465}
9466
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009467static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9468{
9469 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009470 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009472 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009473 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009474 pipe_name(crtc->pipe));
9475
Rob Clarke2c719b2014-12-15 13:56:32 -05009476 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9477 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009478 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9479 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009480 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9481 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009482 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009483 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009484 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009485 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009486 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009487 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009488 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009489 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009490 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009491
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009492 /*
9493 * In theory we can still leave IRQs enabled, as long as only the HPD
9494 * interrupts remain enabled. We used to check for that, but since it's
9495 * gen-specific and since we only disable LCPLL after we fully disable
9496 * the interrupts, the check below should be enough.
9497 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009498 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009499}
9500
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009501static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9502{
9503 struct drm_device *dev = dev_priv->dev;
9504
9505 if (IS_HASWELL(dev))
9506 return I915_READ(D_COMP_HSW);
9507 else
9508 return I915_READ(D_COMP_BDW);
9509}
9510
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009511static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9512{
9513 struct drm_device *dev = dev_priv->dev;
9514
9515 if (IS_HASWELL(dev)) {
9516 mutex_lock(&dev_priv->rps.hw_lock);
9517 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9518 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009519 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009520 mutex_unlock(&dev_priv->rps.hw_lock);
9521 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009522 I915_WRITE(D_COMP_BDW, val);
9523 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009524 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009525}
9526
9527/*
9528 * This function implements pieces of two sequences from BSpec:
9529 * - Sequence for display software to disable LCPLL
9530 * - Sequence for display software to allow package C8+
9531 * The steps implemented here are just the steps that actually touch the LCPLL
9532 * register. Callers should take care of disabling all the display engine
9533 * functions, doing the mode unset, fixing interrupts, etc.
9534 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009535static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9536 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009537{
9538 uint32_t val;
9539
9540 assert_can_disable_lcpll(dev_priv);
9541
9542 val = I915_READ(LCPLL_CTL);
9543
9544 if (switch_to_fclk) {
9545 val |= LCPLL_CD_SOURCE_FCLK;
9546 I915_WRITE(LCPLL_CTL, val);
9547
Imre Deakf53dd632016-06-28 13:37:32 +03009548 if (wait_for_us(I915_READ(LCPLL_CTL) &
9549 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009550 DRM_ERROR("Switching to FCLK failed\n");
9551
9552 val = I915_READ(LCPLL_CTL);
9553 }
9554
9555 val |= LCPLL_PLL_DISABLE;
9556 I915_WRITE(LCPLL_CTL, val);
9557 POSTING_READ(LCPLL_CTL);
9558
9559 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9560 DRM_ERROR("LCPLL still locked\n");
9561
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009562 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009563 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009564 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009565 ndelay(100);
9566
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009567 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9568 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009569 DRM_ERROR("D_COMP RCOMP still in progress\n");
9570
9571 if (allow_power_down) {
9572 val = I915_READ(LCPLL_CTL);
9573 val |= LCPLL_POWER_DOWN_ALLOW;
9574 I915_WRITE(LCPLL_CTL, val);
9575 POSTING_READ(LCPLL_CTL);
9576 }
9577}
9578
9579/*
9580 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9581 * source.
9582 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009583static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009584{
9585 uint32_t val;
9586
9587 val = I915_READ(LCPLL_CTL);
9588
9589 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9590 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9591 return;
9592
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009593 /*
9594 * Make sure we're not on PC8 state before disabling PC8, otherwise
9595 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009596 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009597 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009598
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009599 if (val & LCPLL_POWER_DOWN_ALLOW) {
9600 val &= ~LCPLL_POWER_DOWN_ALLOW;
9601 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009602 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009603 }
9604
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009605 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009606 val |= D_COMP_COMP_FORCE;
9607 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009608 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009609
9610 val = I915_READ(LCPLL_CTL);
9611 val &= ~LCPLL_PLL_DISABLE;
9612 I915_WRITE(LCPLL_CTL, val);
9613
9614 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9615 DRM_ERROR("LCPLL not locked yet\n");
9616
9617 if (val & LCPLL_CD_SOURCE_FCLK) {
9618 val = I915_READ(LCPLL_CTL);
9619 val &= ~LCPLL_CD_SOURCE_FCLK;
9620 I915_WRITE(LCPLL_CTL, val);
9621
Imre Deakf53dd632016-06-28 13:37:32 +03009622 if (wait_for_us((I915_READ(LCPLL_CTL) &
9623 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009624 DRM_ERROR("Switching back to LCPLL failed\n");
9625 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009626
Mika Kuoppala59bad942015-01-16 11:34:40 +02009627 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009628 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009629}
9630
Paulo Zanoni765dab672014-03-07 20:08:18 -03009631/*
9632 * Package states C8 and deeper are really deep PC states that can only be
9633 * reached when all the devices on the system allow it, so even if the graphics
9634 * device allows PC8+, it doesn't mean the system will actually get to these
9635 * states. Our driver only allows PC8+ when going into runtime PM.
9636 *
9637 * The requirements for PC8+ are that all the outputs are disabled, the power
9638 * well is disabled and most interrupts are disabled, and these are also
9639 * requirements for runtime PM. When these conditions are met, we manually do
9640 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9641 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9642 * hang the machine.
9643 *
9644 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9645 * the state of some registers, so when we come back from PC8+ we need to
9646 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9647 * need to take care of the registers kept by RC6. Notice that this happens even
9648 * if we don't put the device in PCI D3 state (which is what currently happens
9649 * because of the runtime PM support).
9650 *
9651 * For more, read "Display Sequences for Package C8" on the hardware
9652 * documentation.
9653 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009654void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009655{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009656 struct drm_device *dev = dev_priv->dev;
9657 uint32_t val;
9658
Paulo Zanonic67a4702013-08-19 13:18:09 -03009659 DRM_DEBUG_KMS("Enabling package C8+\n");
9660
Ville Syrjäläc2699522015-08-27 23:55:59 +03009661 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009662 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9663 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9664 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9665 }
9666
9667 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009668 hsw_disable_lcpll(dev_priv, true, true);
9669}
9670
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009671void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009672{
9673 struct drm_device *dev = dev_priv->dev;
9674 uint32_t val;
9675
Paulo Zanonic67a4702013-08-19 13:18:09 -03009676 DRM_DEBUG_KMS("Disabling package C8+\n");
9677
9678 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009679 lpt_init_pch_refclk(dev);
9680
Ville Syrjäläc2699522015-08-27 23:55:59 +03009681 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009682 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9683 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9684 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9685 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009686}
9687
Imre Deak324513c2016-06-13 16:44:36 +03009688static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309689{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009690 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009691 struct intel_atomic_state *old_intel_state =
9692 to_intel_atomic_state(old_state);
9693 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309694
Imre Deak324513c2016-06-13 16:44:36 +03009695 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309696}
9697
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009698/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009699static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009700{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009701 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9702 struct drm_i915_private *dev_priv = state->dev->dev_private;
9703 struct drm_crtc *crtc;
9704 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009705 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009706 unsigned max_pixel_rate = 0, i;
9707 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009708
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009709 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9710 sizeof(intel_state->min_pixclk));
9711
9712 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009713 int pixel_rate;
9714
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009715 crtc_state = to_intel_crtc_state(cstate);
9716 if (!crtc_state->base.enable) {
9717 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009718 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009719 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009720
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009721 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009722
9723 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009724 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009725 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9726
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009727 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009728 }
9729
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009730 for_each_pipe(dev_priv, pipe)
9731 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9732
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009733 return max_pixel_rate;
9734}
9735
9736static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9737{
9738 struct drm_i915_private *dev_priv = dev->dev_private;
9739 uint32_t val, data;
9740 int ret;
9741
9742 if (WARN((I915_READ(LCPLL_CTL) &
9743 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9744 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9745 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9746 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9747 "trying to change cdclk frequency with cdclk not enabled\n"))
9748 return;
9749
9750 mutex_lock(&dev_priv->rps.hw_lock);
9751 ret = sandybridge_pcode_write(dev_priv,
9752 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9753 mutex_unlock(&dev_priv->rps.hw_lock);
9754 if (ret) {
9755 DRM_ERROR("failed to inform pcode about cdclk change\n");
9756 return;
9757 }
9758
9759 val = I915_READ(LCPLL_CTL);
9760 val |= LCPLL_CD_SOURCE_FCLK;
9761 I915_WRITE(LCPLL_CTL, val);
9762
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009763 if (wait_for_us(I915_READ(LCPLL_CTL) &
9764 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009765 DRM_ERROR("Switching to FCLK failed\n");
9766
9767 val = I915_READ(LCPLL_CTL);
9768 val &= ~LCPLL_CLK_FREQ_MASK;
9769
9770 switch (cdclk) {
9771 case 450000:
9772 val |= LCPLL_CLK_FREQ_450;
9773 data = 0;
9774 break;
9775 case 540000:
9776 val |= LCPLL_CLK_FREQ_54O_BDW;
9777 data = 1;
9778 break;
9779 case 337500:
9780 val |= LCPLL_CLK_FREQ_337_5_BDW;
9781 data = 2;
9782 break;
9783 case 675000:
9784 val |= LCPLL_CLK_FREQ_675_BDW;
9785 data = 3;
9786 break;
9787 default:
9788 WARN(1, "invalid cdclk frequency\n");
9789 return;
9790 }
9791
9792 I915_WRITE(LCPLL_CTL, val);
9793
9794 val = I915_READ(LCPLL_CTL);
9795 val &= ~LCPLL_CD_SOURCE_FCLK;
9796 I915_WRITE(LCPLL_CTL, val);
9797
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009798 if (wait_for_us((I915_READ(LCPLL_CTL) &
9799 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009800 DRM_ERROR("Switching back to LCPLL failed\n");
9801
9802 mutex_lock(&dev_priv->rps.hw_lock);
9803 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9804 mutex_unlock(&dev_priv->rps.hw_lock);
9805
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009806 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9807
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009808 intel_update_cdclk(dev);
9809
9810 WARN(cdclk != dev_priv->cdclk_freq,
9811 "cdclk requested %d kHz but got %d kHz\n",
9812 cdclk, dev_priv->cdclk_freq);
9813}
9814
Ville Syrjälä587c7912016-05-11 22:44:41 +03009815static int broadwell_calc_cdclk(int max_pixclk)
9816{
9817 if (max_pixclk > 540000)
9818 return 675000;
9819 else if (max_pixclk > 450000)
9820 return 540000;
9821 else if (max_pixclk > 337500)
9822 return 450000;
9823 else
9824 return 337500;
9825}
9826
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009827static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009828{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009829 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009830 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009831 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009832 int cdclk;
9833
9834 /*
9835 * FIXME should also account for plane ratio
9836 * once 64bpp pixel formats are supported.
9837 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009838 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009839
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009840 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009841 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9842 cdclk, dev_priv->max_cdclk_freq);
9843 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009844 }
9845
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009846 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9847 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009848 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009849
9850 return 0;
9851}
9852
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009853static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009854{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009855 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009856 struct intel_atomic_state *old_intel_state =
9857 to_intel_atomic_state(old_state);
9858 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009859
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009860 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009861}
9862
Clint Taylorc89e39f2016-05-13 23:41:21 +03009863static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9864{
9865 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9866 struct drm_i915_private *dev_priv = to_i915(state->dev);
9867 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009868 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009869 int cdclk;
9870
9871 /*
9872 * FIXME should also account for plane ratio
9873 * once 64bpp pixel formats are supported.
9874 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009875 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009876
9877 /*
9878 * FIXME move the cdclk caclulation to
9879 * compute_config() so we can fail gracegully.
9880 */
9881 if (cdclk > dev_priv->max_cdclk_freq) {
9882 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9883 cdclk, dev_priv->max_cdclk_freq);
9884 cdclk = dev_priv->max_cdclk_freq;
9885 }
9886
9887 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9888 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009889 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009890
9891 return 0;
9892}
9893
9894static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9895{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009896 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9897 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9898 unsigned int req_cdclk = intel_state->dev_cdclk;
9899 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009900
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009901 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009902}
9903
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009904static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9905 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009906{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009907 struct intel_encoder *intel_encoder =
9908 intel_ddi_get_crtc_new_encoder(crtc_state);
9909
9910 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9911 if (!intel_ddi_pll_select(crtc, crtc_state))
9912 return -EINVAL;
9913 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009914
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009915 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009916
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009917 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009918}
9919
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309920static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9921 enum port port,
9922 struct intel_crtc_state *pipe_config)
9923{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009924 enum intel_dpll_id id;
9925
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309926 switch (port) {
9927 case PORT_A:
9928 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009929 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309930 break;
9931 case PORT_B:
9932 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009933 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309934 break;
9935 case PORT_C:
9936 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009937 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309938 break;
9939 default:
9940 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009941 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309942 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009943
9944 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309945}
9946
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009947static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9948 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009949 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009950{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009951 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009952 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009953
9954 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9955 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9956
9957 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009958 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009959 id = DPLL_ID_SKL_DPLL0;
9960 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009961 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009962 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009963 break;
9964 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009965 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009966 break;
9967 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009968 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009969 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009970 default:
9971 MISSING_CASE(pipe_config->ddi_pll_sel);
9972 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009973 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009974
9975 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009976}
9977
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009978static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9979 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009980 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009981{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009982 enum intel_dpll_id id;
9983
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009984 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9985
9986 switch (pipe_config->ddi_pll_sel) {
9987 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009988 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009989 break;
9990 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009991 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009992 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009993 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009994 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009995 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009996 case PORT_CLK_SEL_LCPLL_810:
9997 id = DPLL_ID_LCPLL_810;
9998 break;
9999 case PORT_CLK_SEL_LCPLL_1350:
10000 id = DPLL_ID_LCPLL_1350;
10001 break;
10002 case PORT_CLK_SEL_LCPLL_2700:
10003 id = DPLL_ID_LCPLL_2700;
10004 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010005 default:
10006 MISSING_CASE(pipe_config->ddi_pll_sel);
10007 /* fall through */
10008 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010009 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010010 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010011
10012 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010013}
10014
Jani Nikulacf304292016-03-18 17:05:41 +020010015static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10016 struct intel_crtc_state *pipe_config,
10017 unsigned long *power_domain_mask)
10018{
10019 struct drm_device *dev = crtc->base.dev;
10020 struct drm_i915_private *dev_priv = dev->dev_private;
10021 enum intel_display_power_domain power_domain;
10022 u32 tmp;
10023
Imre Deakd9a7bc62016-05-12 16:18:50 +030010024 /*
10025 * The pipe->transcoder mapping is fixed with the exception of the eDP
10026 * transcoder handled below.
10027 */
Jani Nikulacf304292016-03-18 17:05:41 +020010028 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10029
10030 /*
10031 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10032 * consistency and less surprising code; it's in always on power).
10033 */
10034 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10035 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10036 enum pipe trans_edp_pipe;
10037 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10038 default:
10039 WARN(1, "unknown pipe linked to edp transcoder\n");
10040 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10041 case TRANS_DDI_EDP_INPUT_A_ON:
10042 trans_edp_pipe = PIPE_A;
10043 break;
10044 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10045 trans_edp_pipe = PIPE_B;
10046 break;
10047 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10048 trans_edp_pipe = PIPE_C;
10049 break;
10050 }
10051
10052 if (trans_edp_pipe == crtc->pipe)
10053 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10054 }
10055
10056 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10057 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10058 return false;
10059 *power_domain_mask |= BIT(power_domain);
10060
10061 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10062
10063 return tmp & PIPECONF_ENABLE;
10064}
10065
Jani Nikula4d1de972016-03-18 17:05:42 +020010066static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10067 struct intel_crtc_state *pipe_config,
10068 unsigned long *power_domain_mask)
10069{
10070 struct drm_device *dev = crtc->base.dev;
10071 struct drm_i915_private *dev_priv = dev->dev_private;
10072 enum intel_display_power_domain power_domain;
10073 enum port port;
10074 enum transcoder cpu_transcoder;
10075 u32 tmp;
10076
10077 pipe_config->has_dsi_encoder = false;
10078
10079 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10080 if (port == PORT_A)
10081 cpu_transcoder = TRANSCODER_DSI_A;
10082 else
10083 cpu_transcoder = TRANSCODER_DSI_C;
10084
10085 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10086 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10087 continue;
10088 *power_domain_mask |= BIT(power_domain);
10089
Imre Deakdb18b6a2016-03-24 12:41:40 +020010090 /*
10091 * The PLL needs to be enabled with a valid divider
10092 * configuration, otherwise accessing DSI registers will hang
10093 * the machine. See BSpec North Display Engine
10094 * registers/MIPI[BXT]. We can break out here early, since we
10095 * need the same DSI PLL to be enabled for both DSI ports.
10096 */
10097 if (!intel_dsi_pll_is_enabled(dev_priv))
10098 break;
10099
Jani Nikula4d1de972016-03-18 17:05:42 +020010100 /* XXX: this works for video mode only */
10101 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10102 if (!(tmp & DPI_ENABLE))
10103 continue;
10104
10105 tmp = I915_READ(MIPI_CTRL(port));
10106 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10107 continue;
10108
10109 pipe_config->cpu_transcoder = cpu_transcoder;
10110 pipe_config->has_dsi_encoder = true;
10111 break;
10112 }
10113
10114 return pipe_config->has_dsi_encoder;
10115}
10116
Daniel Vetter26804af2014-06-25 22:01:55 +030010117static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010118 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010119{
10120 struct drm_device *dev = crtc->base.dev;
10121 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010122 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010123 enum port port;
10124 uint32_t tmp;
10125
10126 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10127
10128 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10129
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010130 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010131 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010132 else if (IS_BROXTON(dev))
10133 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010134 else
10135 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010136
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010137 pll = pipe_config->shared_dpll;
10138 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010139 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10140 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010141 }
10142
Daniel Vetter26804af2014-06-25 22:01:55 +030010143 /*
10144 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10145 * DDI E. So just check whether this pipe is wired to DDI E and whether
10146 * the PCH transcoder is on.
10147 */
Damien Lespiauca370452013-12-03 13:56:24 +000010148 if (INTEL_INFO(dev)->gen < 9 &&
10149 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010150 pipe_config->has_pch_encoder = true;
10151
10152 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10153 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10154 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10155
10156 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10157 }
10158}
10159
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010160static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010161 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010162{
10163 struct drm_device *dev = crtc->base.dev;
10164 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010165 enum intel_display_power_domain power_domain;
10166 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010167 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010168
Imre Deak17290502016-02-12 18:55:11 +020010169 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10170 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010171 return false;
Imre Deak17290502016-02-12 18:55:11 +020010172 power_domain_mask = BIT(power_domain);
10173
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010174 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010175
Jani Nikulacf304292016-03-18 17:05:41 +020010176 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010177
Jani Nikula4d1de972016-03-18 17:05:42 +020010178 if (IS_BROXTON(dev_priv)) {
10179 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10180 &power_domain_mask);
10181 WARN_ON(active && pipe_config->has_dsi_encoder);
10182 if (pipe_config->has_dsi_encoder)
10183 active = true;
10184 }
10185
Jani Nikulacf304292016-03-18 17:05:41 +020010186 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010187 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010188
Jani Nikula4d1de972016-03-18 17:05:42 +020010189 if (!pipe_config->has_dsi_encoder) {
10190 haswell_get_ddi_port_state(crtc, pipe_config);
10191 intel_get_pipe_timings(crtc, pipe_config);
10192 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010193
Jani Nikulabc58be62016-03-18 17:05:39 +020010194 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010195
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010196 pipe_config->gamma_mode =
10197 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10198
Chandra Kondurua1b22782015-04-07 15:28:45 -070010199 if (INTEL_INFO(dev)->gen >= 9) {
10200 skl_init_scalers(dev, crtc, pipe_config);
10201 }
10202
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010203 if (INTEL_INFO(dev)->gen >= 9) {
10204 pipe_config->scaler_state.scaler_id = -1;
10205 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10206 }
10207
Imre Deak17290502016-02-12 18:55:11 +020010208 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10209 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10210 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010211 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010212 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010213 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010214 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010215 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010216
Jesse Barnese59150d2014-01-07 13:30:45 -080010217 if (IS_HASWELL(dev))
10218 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10219 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010220
Jani Nikula4d1de972016-03-18 17:05:42 +020010221 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10222 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010223 pipe_config->pixel_multiplier =
10224 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10225 } else {
10226 pipe_config->pixel_multiplier = 1;
10227 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010228
Imre Deak17290502016-02-12 18:55:11 +020010229out:
10230 for_each_power_domain(power_domain, power_domain_mask)
10231 intel_display_power_put(dev_priv, power_domain);
10232
Jani Nikulacf304292016-03-18 17:05:41 +020010233 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010234}
10235
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010236static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10237 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010238{
10239 struct drm_device *dev = crtc->dev;
10240 struct drm_i915_private *dev_priv = dev->dev_private;
10241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010242 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010243
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010244 if (plane_state && plane_state->visible) {
10245 unsigned int width = plane_state->base.crtc_w;
10246 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010247 unsigned int stride = roundup_pow_of_two(width) * 4;
10248
10249 switch (stride) {
10250 default:
10251 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10252 width, stride);
10253 stride = 256;
10254 /* fallthrough */
10255 case 256:
10256 case 512:
10257 case 1024:
10258 case 2048:
10259 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010260 }
10261
Ville Syrjälädc41c152014-08-13 11:57:05 +030010262 cntl |= CURSOR_ENABLE |
10263 CURSOR_GAMMA_ENABLE |
10264 CURSOR_FORMAT_ARGB |
10265 CURSOR_STRIDE(stride);
10266
10267 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010268 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010269
Ville Syrjälädc41c152014-08-13 11:57:05 +030010270 if (intel_crtc->cursor_cntl != 0 &&
10271 (intel_crtc->cursor_base != base ||
10272 intel_crtc->cursor_size != size ||
10273 intel_crtc->cursor_cntl != cntl)) {
10274 /* On these chipsets we can only modify the base/size/stride
10275 * whilst the cursor is disabled.
10276 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010277 I915_WRITE(CURCNTR(PIPE_A), 0);
10278 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010279 intel_crtc->cursor_cntl = 0;
10280 }
10281
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010282 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010283 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010284 intel_crtc->cursor_base = base;
10285 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010286
10287 if (intel_crtc->cursor_size != size) {
10288 I915_WRITE(CURSIZE, size);
10289 intel_crtc->cursor_size = size;
10290 }
10291
Chris Wilson4b0e3332014-05-30 16:35:26 +030010292 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010293 I915_WRITE(CURCNTR(PIPE_A), cntl);
10294 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010295 intel_crtc->cursor_cntl = cntl;
10296 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010297}
10298
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010299static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10300 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010301{
10302 struct drm_device *dev = crtc->dev;
10303 struct drm_i915_private *dev_priv = dev->dev_private;
10304 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10305 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010306 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010307
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010308 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010309 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010310 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010311 case 64:
10312 cntl |= CURSOR_MODE_64_ARGB_AX;
10313 break;
10314 case 128:
10315 cntl |= CURSOR_MODE_128_ARGB_AX;
10316 break;
10317 case 256:
10318 cntl |= CURSOR_MODE_256_ARGB_AX;
10319 break;
10320 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010321 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010322 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010323 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010324 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010325
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010326 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010327 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010328
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010329 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10330 cntl |= CURSOR_ROTATE_180;
10331 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010332
Chris Wilson4b0e3332014-05-30 16:35:26 +030010333 if (intel_crtc->cursor_cntl != cntl) {
10334 I915_WRITE(CURCNTR(pipe), cntl);
10335 POSTING_READ(CURCNTR(pipe));
10336 intel_crtc->cursor_cntl = cntl;
10337 }
10338
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010339 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010340 I915_WRITE(CURBASE(pipe), base);
10341 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010342
10343 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010344}
10345
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010346/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010347static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010348 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010349{
10350 struct drm_device *dev = crtc->dev;
10351 struct drm_i915_private *dev_priv = dev->dev_private;
10352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10353 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010354 u32 base = intel_crtc->cursor_addr;
10355 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010356
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010357 if (plane_state) {
10358 int x = plane_state->base.crtc_x;
10359 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010360
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010361 if (x < 0) {
10362 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10363 x = -x;
10364 }
10365 pos |= x << CURSOR_X_SHIFT;
10366
10367 if (y < 0) {
10368 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10369 y = -y;
10370 }
10371 pos |= y << CURSOR_Y_SHIFT;
10372
10373 /* ILK+ do this automagically */
10374 if (HAS_GMCH_DISPLAY(dev) &&
10375 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10376 base += (plane_state->base.crtc_h *
10377 plane_state->base.crtc_w - 1) * 4;
10378 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010379 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010380
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010381 I915_WRITE(CURPOS(pipe), pos);
10382
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010383 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010384 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010385 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010386 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010387}
10388
Ville Syrjälädc41c152014-08-13 11:57:05 +030010389static bool cursor_size_ok(struct drm_device *dev,
10390 uint32_t width, uint32_t height)
10391{
10392 if (width == 0 || height == 0)
10393 return false;
10394
10395 /*
10396 * 845g/865g are special in that they are only limited by
10397 * the width of their cursors, the height is arbitrary up to
10398 * the precision of the register. Everything else requires
10399 * square cursors, limited to a few power-of-two sizes.
10400 */
10401 if (IS_845G(dev) || IS_I865G(dev)) {
10402 if ((width & 63) != 0)
10403 return false;
10404
10405 if (width > (IS_845G(dev) ? 64 : 512))
10406 return false;
10407
10408 if (height > 1023)
10409 return false;
10410 } else {
10411 switch (width | height) {
10412 case 256:
10413 case 128:
10414 if (IS_GEN2(dev))
10415 return false;
10416 case 64:
10417 break;
10418 default:
10419 return false;
10420 }
10421 }
10422
10423 return true;
10424}
10425
Jesse Barnes79e53942008-11-07 14:24:08 -080010426/* VESA 640x480x72Hz mode to set on the pipe */
10427static struct drm_display_mode load_detect_mode = {
10428 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10429 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10430};
10431
Daniel Vettera8bb6812014-02-10 18:00:39 +010010432struct drm_framebuffer *
10433__intel_framebuffer_create(struct drm_device *dev,
10434 struct drm_mode_fb_cmd2 *mode_cmd,
10435 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010436{
10437 struct intel_framebuffer *intel_fb;
10438 int ret;
10439
10440 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010441 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010442 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010443
10444 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010445 if (ret)
10446 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010447
10448 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010449
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010450err:
10451 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010452 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010453}
10454
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010455static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010456intel_framebuffer_create(struct drm_device *dev,
10457 struct drm_mode_fb_cmd2 *mode_cmd,
10458 struct drm_i915_gem_object *obj)
10459{
10460 struct drm_framebuffer *fb;
10461 int ret;
10462
10463 ret = i915_mutex_lock_interruptible(dev);
10464 if (ret)
10465 return ERR_PTR(ret);
10466 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10467 mutex_unlock(&dev->struct_mutex);
10468
10469 return fb;
10470}
10471
Chris Wilsond2dff872011-04-19 08:36:26 +010010472static u32
10473intel_framebuffer_pitch_for_width(int width, int bpp)
10474{
10475 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10476 return ALIGN(pitch, 64);
10477}
10478
10479static u32
10480intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10481{
10482 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010483 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010484}
10485
10486static struct drm_framebuffer *
10487intel_framebuffer_create_for_mode(struct drm_device *dev,
10488 struct drm_display_mode *mode,
10489 int depth, int bpp)
10490{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010491 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010492 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010493 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010494
Dave Gordond37cd8a2016-04-22 19:14:32 +010010495 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010496 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010497 if (IS_ERR(obj))
10498 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010499
10500 mode_cmd.width = mode->hdisplay;
10501 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010502 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10503 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010504 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010505
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010506 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10507 if (IS_ERR(fb))
10508 drm_gem_object_unreference_unlocked(&obj->base);
10509
10510 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010511}
10512
10513static struct drm_framebuffer *
10514mode_fits_in_fbdev(struct drm_device *dev,
10515 struct drm_display_mode *mode)
10516{
Daniel Vetter06957262015-08-10 13:34:08 +020010517#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010518 struct drm_i915_private *dev_priv = dev->dev_private;
10519 struct drm_i915_gem_object *obj;
10520 struct drm_framebuffer *fb;
10521
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010522 if (!dev_priv->fbdev)
10523 return NULL;
10524
10525 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010526 return NULL;
10527
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010528 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010529 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010530
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010531 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010532 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10533 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010534 return NULL;
10535
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010536 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010537 return NULL;
10538
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010539 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010540 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010541#else
10542 return NULL;
10543#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010544}
10545
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010546static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10547 struct drm_crtc *crtc,
10548 struct drm_display_mode *mode,
10549 struct drm_framebuffer *fb,
10550 int x, int y)
10551{
10552 struct drm_plane_state *plane_state;
10553 int hdisplay, vdisplay;
10554 int ret;
10555
10556 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10557 if (IS_ERR(plane_state))
10558 return PTR_ERR(plane_state);
10559
10560 if (mode)
10561 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10562 else
10563 hdisplay = vdisplay = 0;
10564
10565 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10566 if (ret)
10567 return ret;
10568 drm_atomic_set_fb_for_plane(plane_state, fb);
10569 plane_state->crtc_x = 0;
10570 plane_state->crtc_y = 0;
10571 plane_state->crtc_w = hdisplay;
10572 plane_state->crtc_h = vdisplay;
10573 plane_state->src_x = x << 16;
10574 plane_state->src_y = y << 16;
10575 plane_state->src_w = hdisplay << 16;
10576 plane_state->src_h = vdisplay << 16;
10577
10578 return 0;
10579}
10580
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010581bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010582 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010583 struct intel_load_detect_pipe *old,
10584 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010585{
10586 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010587 struct intel_encoder *intel_encoder =
10588 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010589 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010590 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010591 struct drm_crtc *crtc = NULL;
10592 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010593 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010594 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010595 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010596 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010597 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010598 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010599
Chris Wilsond2dff872011-04-19 08:36:26 +010010600 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010601 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010602 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010603
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010604 old->restore_state = NULL;
10605
Rob Clark51fd3712013-11-19 12:10:12 -050010606retry:
10607 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10608 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010609 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010610
Jesse Barnes79e53942008-11-07 14:24:08 -080010611 /*
10612 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010613 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010614 * - if the connector already has an assigned crtc, use it (but make
10615 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010616 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010617 * - try to find the first unused crtc that can drive this connector,
10618 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010619 */
10620
10621 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010622 if (connector->state->crtc) {
10623 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010624
Rob Clark51fd3712013-11-19 12:10:12 -050010625 ret = drm_modeset_lock(&crtc->mutex, ctx);
10626 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010627 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010628
10629 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010630 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010631 }
10632
10633 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010634 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 i++;
10636 if (!(encoder->possible_crtcs & (1 << i)))
10637 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010638
10639 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10640 if (ret)
10641 goto fail;
10642
10643 if (possible_crtc->state->enable) {
10644 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010645 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010646 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010647
10648 crtc = possible_crtc;
10649 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010650 }
10651
10652 /*
10653 * If we didn't find an unused CRTC, don't use any.
10654 */
10655 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010656 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010657 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010658 }
10659
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010660found:
10661 intel_crtc = to_intel_crtc(crtc);
10662
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010663 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10664 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010665 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010666
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010667 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010668 restore_state = drm_atomic_state_alloc(dev);
10669 if (!state || !restore_state) {
10670 ret = -ENOMEM;
10671 goto fail;
10672 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010673
10674 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010675 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010676
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010677 connector_state = drm_atomic_get_connector_state(state, connector);
10678 if (IS_ERR(connector_state)) {
10679 ret = PTR_ERR(connector_state);
10680 goto fail;
10681 }
10682
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010683 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10684 if (ret)
10685 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010686
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010687 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10688 if (IS_ERR(crtc_state)) {
10689 ret = PTR_ERR(crtc_state);
10690 goto fail;
10691 }
10692
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010693 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010694
Chris Wilson64927112011-04-20 07:25:26 +010010695 if (!mode)
10696 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010697
Chris Wilsond2dff872011-04-19 08:36:26 +010010698 /* We need a framebuffer large enough to accommodate all accesses
10699 * that the plane may generate whilst we perform load detection.
10700 * We can not rely on the fbcon either being present (we get called
10701 * during its initialisation to detect all boot displays, or it may
10702 * not even exist) or that it is large enough to satisfy the
10703 * requested mode.
10704 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010705 fb = mode_fits_in_fbdev(dev, mode);
10706 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010707 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010708 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010709 } else
10710 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010711 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010712 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010713 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010714 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010715
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010716 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10717 if (ret)
10718 goto fail;
10719
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010720 drm_framebuffer_unreference(fb);
10721
10722 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10723 if (ret)
10724 goto fail;
10725
10726 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10727 if (!ret)
10728 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10729 if (!ret)
10730 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10731 if (ret) {
10732 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10733 goto fail;
10734 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010735
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010736 ret = drm_atomic_commit(state);
10737 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010738 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010739 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010740 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010741
10742 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010743
Jesse Barnes79e53942008-11-07 14:24:08 -080010744 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010745 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010746 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010747
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010748fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010749 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010750 drm_atomic_state_free(restore_state);
10751 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010752
Rob Clark51fd3712013-11-19 12:10:12 -050010753 if (ret == -EDEADLK) {
10754 drm_modeset_backoff(ctx);
10755 goto retry;
10756 }
10757
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010758 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010759}
10760
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010761void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010762 struct intel_load_detect_pipe *old,
10763 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010764{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010765 struct intel_encoder *intel_encoder =
10766 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010767 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010768 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010769 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010770
Chris Wilsond2dff872011-04-19 08:36:26 +010010771 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010772 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010773 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010774
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010775 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010776 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010777
10778 ret = drm_atomic_commit(state);
10779 if (ret) {
10780 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10781 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010782 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010783}
10784
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010785static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010786 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010787{
10788 struct drm_i915_private *dev_priv = dev->dev_private;
10789 u32 dpll = pipe_config->dpll_hw_state.dpll;
10790
10791 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010792 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010793 else if (HAS_PCH_SPLIT(dev))
10794 return 120000;
10795 else if (!IS_GEN2(dev))
10796 return 96000;
10797 else
10798 return 48000;
10799}
10800
Jesse Barnes79e53942008-11-07 14:24:08 -080010801/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010802static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010803 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010804{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010805 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010806 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010807 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010808 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010809 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010810 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010811 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010812 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010813
10814 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010815 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010816 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010817 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010818
10819 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010820 if (IS_PINEVIEW(dev)) {
10821 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10822 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010823 } else {
10824 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10825 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10826 }
10827
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010828 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010829 if (IS_PINEVIEW(dev))
10830 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10831 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010832 else
10833 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010834 DPLL_FPA01_P1_POST_DIV_SHIFT);
10835
10836 switch (dpll & DPLL_MODE_MASK) {
10837 case DPLLB_MODE_DAC_SERIAL:
10838 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10839 5 : 10;
10840 break;
10841 case DPLLB_MODE_LVDS:
10842 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10843 7 : 14;
10844 break;
10845 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010846 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010847 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010848 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010849 }
10850
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010851 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010852 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010853 else
Imre Deakdccbea32015-06-22 23:35:51 +030010854 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010855 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010856 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010857 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010858
10859 if (is_lvds) {
10860 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10861 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010862
10863 if (lvds & LVDS_CLKB_POWER_UP)
10864 clock.p2 = 7;
10865 else
10866 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010867 } else {
10868 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10869 clock.p1 = 2;
10870 else {
10871 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10872 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10873 }
10874 if (dpll & PLL_P2_DIVIDE_BY_4)
10875 clock.p2 = 4;
10876 else
10877 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010878 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010879
Imre Deakdccbea32015-06-22 23:35:51 +030010880 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010881 }
10882
Ville Syrjälä18442d02013-09-13 16:00:08 +030010883 /*
10884 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010885 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010886 * encoder's get_config() function.
10887 */
Imre Deakdccbea32015-06-22 23:35:51 +030010888 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010889}
10890
Ville Syrjälä6878da02013-09-13 15:59:11 +030010891int intel_dotclock_calculate(int link_freq,
10892 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010893{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010894 /*
10895 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010896 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010897 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010898 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010899 *
10900 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010901 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010902 */
10903
Ville Syrjälä6878da02013-09-13 15:59:11 +030010904 if (!m_n->link_n)
10905 return 0;
10906
10907 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10908}
10909
Ville Syrjälä18442d02013-09-13 16:00:08 +030010910static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010911 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010912{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010913 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010914
10915 /* read out port_clock from the DPLL */
10916 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010917
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010918 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010919 * In case there is an active pipe without active ports,
10920 * we may need some idea for the dotclock anyway.
10921 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010922 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010923 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010924 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010925 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010926}
10927
10928/** Returns the currently programmed mode of the given pipe. */
10929struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10930 struct drm_crtc *crtc)
10931{
Jesse Barnes548f2452011-02-17 10:40:53 -080010932 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010934 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010935 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010936 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010937 int htot = I915_READ(HTOTAL(cpu_transcoder));
10938 int hsync = I915_READ(HSYNC(cpu_transcoder));
10939 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10940 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010941 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010942
10943 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10944 if (!mode)
10945 return NULL;
10946
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010947 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10948 if (!pipe_config) {
10949 kfree(mode);
10950 return NULL;
10951 }
10952
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010953 /*
10954 * Construct a pipe_config sufficient for getting the clock info
10955 * back out of crtc_clock_get.
10956 *
10957 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10958 * to use a real value here instead.
10959 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010960 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10961 pipe_config->pixel_multiplier = 1;
10962 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10963 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10964 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10965 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010966
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010967 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010968 mode->hdisplay = (htot & 0xffff) + 1;
10969 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10970 mode->hsync_start = (hsync & 0xffff) + 1;
10971 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10972 mode->vdisplay = (vtot & 0xffff) + 1;
10973 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10974 mode->vsync_start = (vsync & 0xffff) + 1;
10975 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10976
10977 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010978
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010979 kfree(pipe_config);
10980
Jesse Barnes79e53942008-11-07 14:24:08 -080010981 return mode;
10982}
10983
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010984void intel_mark_busy(struct drm_i915_private *dev_priv)
Jesse Barnes652c3932009-08-17 13:31:43 -070010985{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010986 if (dev_priv->mm.busy)
10987 return;
10988
Paulo Zanoni43694d62014-03-07 20:08:08 -030010989 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010990 i915_update_gfx_val(dev_priv);
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010991 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010992 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010993 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010994}
10995
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010996void intel_mark_idle(struct drm_i915_private *dev_priv)
Chris Wilsonf047e392012-07-21 12:31:41 +010010997{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010998 if (!dev_priv->mm.busy)
10999 return;
11000
11001 dev_priv->mm.busy = false;
11002
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010011003 if (INTEL_GEN(dev_priv) >= 6)
11004 gen6_rps_idle(dev_priv);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030011005
Paulo Zanoni43694d62014-03-07 20:08:08 -030011006 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010011007}
11008
Jesse Barnes79e53942008-11-07 14:24:08 -080011009static void intel_crtc_destroy(struct drm_crtc *crtc)
11010{
11011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011012 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011013 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011014
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011015 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011016 work = intel_crtc->flip_work;
11017 intel_crtc->flip_work = NULL;
11018 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011019
Daniel Vetter5a21b662016-05-24 17:13:53 +020011020 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011021 cancel_work_sync(&work->mmio_work);
11022 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011023 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011024 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011025
11026 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011027
Jesse Barnes79e53942008-11-07 14:24:08 -080011028 kfree(intel_crtc);
11029}
11030
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011031static void intel_unpin_work_fn(struct work_struct *__work)
11032{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011033 struct intel_flip_work *work =
11034 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011035 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11036 struct drm_device *dev = crtc->base.dev;
11037 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011038
Daniel Vetter5a21b662016-05-24 17:13:53 +020011039 if (is_mmio_work(work))
11040 flush_work(&work->mmio_work);
11041
11042 mutex_lock(&dev->struct_mutex);
11043 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11044 drm_gem_object_unreference(&work->pending_flip_obj->base);
11045
11046 if (work->flip_queued_req)
11047 i915_gem_request_assign(&work->flip_queued_req, NULL);
11048 mutex_unlock(&dev->struct_mutex);
11049
11050 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11051 intel_fbc_post_update(crtc);
11052 drm_framebuffer_unreference(work->old_fb);
11053
11054 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11055 atomic_dec(&crtc->unpin_work_count);
11056
11057 kfree(work);
11058}
11059
11060/* Is 'a' after or equal to 'b'? */
11061static bool g4x_flip_count_after_eq(u32 a, u32 b)
11062{
11063 return !((a - b) & 0x80000000);
11064}
11065
11066static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11067 struct intel_flip_work *work)
11068{
11069 struct drm_device *dev = crtc->base.dev;
11070 struct drm_i915_private *dev_priv = dev->dev_private;
11071 unsigned reset_counter;
11072
11073 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11074 if (crtc->reset_counter != reset_counter)
11075 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011076
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011077 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011078 * The relevant registers doen't exist on pre-ctg.
11079 * As the flip done interrupt doesn't trigger for mmio
11080 * flips on gmch platforms, a flip count check isn't
11081 * really needed there. But since ctg has the registers,
11082 * include it in the check anyway.
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011083 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011084 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11085 return true;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011086
Daniel Vetter5a21b662016-05-24 17:13:53 +020011087 /*
11088 * BDW signals flip done immediately if the plane
11089 * is disabled, even if the plane enable is already
11090 * armed to occur at the next vblank :(
11091 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011092
Daniel Vetter5a21b662016-05-24 17:13:53 +020011093 /*
11094 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11095 * used the same base address. In that case the mmio flip might
11096 * have completed, but the CS hasn't even executed the flip yet.
11097 *
11098 * A flip count check isn't enough as the CS might have updated
11099 * the base address just after start of vblank, but before we
11100 * managed to process the interrupt. This means we'd complete the
11101 * CS flip too soon.
11102 *
11103 * Combining both checks should get us a good enough result. It may
11104 * still happen that the CS flip has been executed, but has not
11105 * yet actually completed. But in case the base address is the same
11106 * anyway, we don't really care.
11107 */
11108 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11109 crtc->flip_work->gtt_offset &&
11110 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11111 crtc->flip_work->flip_count);
11112}
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011113
Daniel Vetter5a21b662016-05-24 17:13:53 +020011114static bool
11115__pageflip_finished_mmio(struct intel_crtc *crtc,
11116 struct intel_flip_work *work)
11117{
11118 /*
11119 * MMIO work completes when vblank is different from
11120 * flip_queued_vblank.
11121 *
11122 * Reset counter value doesn't matter, this is handled by
11123 * i915_wait_request finishing early, so no need to handle
11124 * reset here.
11125 */
11126 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011127}
11128
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011129
11130static bool pageflip_finished(struct intel_crtc *crtc,
11131 struct intel_flip_work *work)
11132{
11133 if (!atomic_read(&work->pending))
11134 return false;
11135
11136 smp_rmb();
11137
Daniel Vetter5a21b662016-05-24 17:13:53 +020011138 if (is_mmio_work(work))
11139 return __pageflip_finished_mmio(crtc, work);
11140 else
11141 return __pageflip_finished_cs(crtc, work);
11142}
11143
11144void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11145{
11146 struct drm_device *dev = dev_priv->dev;
11147 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11149 struct intel_flip_work *work;
11150 unsigned long flags;
11151
11152 /* Ignore early vblank irqs */
11153 if (!crtc)
11154 return;
11155
Daniel Vetterf3260382014-09-15 14:55:23 +020011156 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011157 * This is called both by irq handlers and the reset code (to complete
11158 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011159 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011160 spin_lock_irqsave(&dev->event_lock, flags);
11161 work = intel_crtc->flip_work;
11162
11163 if (work != NULL &&
11164 !is_mmio_work(work) &&
11165 pageflip_finished(intel_crtc, work))
11166 page_flip_completed(intel_crtc);
11167
11168 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011169}
11170
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011171void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011172{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011173 struct drm_device *dev = dev_priv->dev;
11174 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11176 struct intel_flip_work *work;
11177 unsigned long flags;
11178
11179 /* Ignore early vblank irqs */
11180 if (!crtc)
11181 return;
11182
11183 /*
11184 * This is called both by irq handlers and the reset code (to complete
11185 * lost pageflips) so needs the full irqsave spinlocks.
11186 */
11187 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011188 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011189
Daniel Vetter5a21b662016-05-24 17:13:53 +020011190 if (work != NULL &&
11191 is_mmio_work(work) &&
11192 pageflip_finished(intel_crtc, work))
11193 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011194
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011195 spin_unlock_irqrestore(&dev->event_lock, flags);
11196}
11197
Daniel Vetter5a21b662016-05-24 17:13:53 +020011198static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11199 struct intel_flip_work *work)
11200{
11201 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11202
11203 /* Ensure that the work item is consistent when activating it ... */
11204 smp_mb__before_atomic();
11205 atomic_set(&work->pending, 1);
11206}
11207
11208static int intel_gen2_queue_flip(struct drm_device *dev,
11209 struct drm_crtc *crtc,
11210 struct drm_framebuffer *fb,
11211 struct drm_i915_gem_object *obj,
11212 struct drm_i915_gem_request *req,
11213 uint32_t flags)
11214{
11215 struct intel_engine_cs *engine = req->engine;
11216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11217 u32 flip_mask;
11218 int ret;
11219
11220 ret = intel_ring_begin(req, 6);
11221 if (ret)
11222 return ret;
11223
11224 /* Can't queue multiple flips, so wait for the previous
11225 * one to finish before executing the next.
11226 */
11227 if (intel_crtc->plane)
11228 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11229 else
11230 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11231 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11232 intel_ring_emit(engine, MI_NOOP);
11233 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11234 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11235 intel_ring_emit(engine, fb->pitches[0]);
11236 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11237 intel_ring_emit(engine, 0); /* aux display base address, unused */
11238
11239 return 0;
11240}
11241
11242static int intel_gen3_queue_flip(struct drm_device *dev,
11243 struct drm_crtc *crtc,
11244 struct drm_framebuffer *fb,
11245 struct drm_i915_gem_object *obj,
11246 struct drm_i915_gem_request *req,
11247 uint32_t flags)
11248{
11249 struct intel_engine_cs *engine = req->engine;
11250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11251 u32 flip_mask;
11252 int ret;
11253
11254 ret = intel_ring_begin(req, 6);
11255 if (ret)
11256 return ret;
11257
11258 if (intel_crtc->plane)
11259 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11260 else
11261 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11262 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11263 intel_ring_emit(engine, MI_NOOP);
11264 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11265 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11266 intel_ring_emit(engine, fb->pitches[0]);
11267 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11268 intel_ring_emit(engine, MI_NOOP);
11269
11270 return 0;
11271}
11272
11273static int intel_gen4_queue_flip(struct drm_device *dev,
11274 struct drm_crtc *crtc,
11275 struct drm_framebuffer *fb,
11276 struct drm_i915_gem_object *obj,
11277 struct drm_i915_gem_request *req,
11278 uint32_t flags)
11279{
11280 struct intel_engine_cs *engine = req->engine;
11281 struct drm_i915_private *dev_priv = dev->dev_private;
11282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11283 uint32_t pf, pipesrc;
11284 int ret;
11285
11286 ret = intel_ring_begin(req, 4);
11287 if (ret)
11288 return ret;
11289
11290 /* i965+ uses the linear or tiled offsets from the
11291 * Display Registers (which do not change across a page-flip)
11292 * so we need only reprogram the base address.
11293 */
11294 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11295 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11296 intel_ring_emit(engine, fb->pitches[0]);
11297 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11298 obj->tiling_mode);
11299
11300 /* XXX Enabling the panel-fitter across page-flip is so far
11301 * untested on non-native modes, so ignore it for now.
11302 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11303 */
11304 pf = 0;
11305 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11306 intel_ring_emit(engine, pf | pipesrc);
11307
11308 return 0;
11309}
11310
11311static int intel_gen6_queue_flip(struct drm_device *dev,
11312 struct drm_crtc *crtc,
11313 struct drm_framebuffer *fb,
11314 struct drm_i915_gem_object *obj,
11315 struct drm_i915_gem_request *req,
11316 uint32_t flags)
11317{
11318 struct intel_engine_cs *engine = req->engine;
11319 struct drm_i915_private *dev_priv = dev->dev_private;
11320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11321 uint32_t pf, pipesrc;
11322 int ret;
11323
11324 ret = intel_ring_begin(req, 4);
11325 if (ret)
11326 return ret;
11327
11328 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11329 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11330 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11331 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11332
11333 /* Contrary to the suggestions in the documentation,
11334 * "Enable Panel Fitter" does not seem to be required when page
11335 * flipping with a non-native mode, and worse causes a normal
11336 * modeset to fail.
11337 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11338 */
11339 pf = 0;
11340 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11341 intel_ring_emit(engine, pf | pipesrc);
11342
11343 return 0;
11344}
11345
11346static int intel_gen7_queue_flip(struct drm_device *dev,
11347 struct drm_crtc *crtc,
11348 struct drm_framebuffer *fb,
11349 struct drm_i915_gem_object *obj,
11350 struct drm_i915_gem_request *req,
11351 uint32_t flags)
11352{
11353 struct intel_engine_cs *engine = req->engine;
11354 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11355 uint32_t plane_bit = 0;
11356 int len, ret;
11357
11358 switch (intel_crtc->plane) {
11359 case PLANE_A:
11360 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11361 break;
11362 case PLANE_B:
11363 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11364 break;
11365 case PLANE_C:
11366 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11367 break;
11368 default:
11369 WARN_ONCE(1, "unknown plane in flip command\n");
11370 return -ENODEV;
11371 }
11372
11373 len = 4;
11374 if (engine->id == RCS) {
11375 len += 6;
11376 /*
11377 * On Gen 8, SRM is now taking an extra dword to accommodate
11378 * 48bits addresses, and we need a NOOP for the batch size to
11379 * stay even.
11380 */
11381 if (IS_GEN8(dev))
11382 len += 2;
11383 }
11384
11385 /*
11386 * BSpec MI_DISPLAY_FLIP for IVB:
11387 * "The full packet must be contained within the same cache line."
11388 *
11389 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11390 * cacheline, if we ever start emitting more commands before
11391 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11392 * then do the cacheline alignment, and finally emit the
11393 * MI_DISPLAY_FLIP.
11394 */
11395 ret = intel_ring_cacheline_align(req);
11396 if (ret)
11397 return ret;
11398
11399 ret = intel_ring_begin(req, len);
11400 if (ret)
11401 return ret;
11402
11403 /* Unmask the flip-done completion message. Note that the bspec says that
11404 * we should do this for both the BCS and RCS, and that we must not unmask
11405 * more than one flip event at any time (or ensure that one flip message
11406 * can be sent by waiting for flip-done prior to queueing new flips).
11407 * Experimentation says that BCS works despite DERRMR masking all
11408 * flip-done completion events and that unmasking all planes at once
11409 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11410 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11411 */
11412 if (engine->id == RCS) {
11413 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11414 intel_ring_emit_reg(engine, DERRMR);
11415 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11416 DERRMR_PIPEB_PRI_FLIP_DONE |
11417 DERRMR_PIPEC_PRI_FLIP_DONE));
11418 if (IS_GEN8(dev))
11419 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11420 MI_SRM_LRM_GLOBAL_GTT);
11421 else
11422 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11423 MI_SRM_LRM_GLOBAL_GTT);
11424 intel_ring_emit_reg(engine, DERRMR);
11425 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11426 if (IS_GEN8(dev)) {
11427 intel_ring_emit(engine, 0);
11428 intel_ring_emit(engine, MI_NOOP);
11429 }
11430 }
11431
11432 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11433 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11434 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11435 intel_ring_emit(engine, (MI_NOOP));
11436
11437 return 0;
11438}
11439
11440static bool use_mmio_flip(struct intel_engine_cs *engine,
11441 struct drm_i915_gem_object *obj)
11442{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011443 struct reservation_object *resv;
11444
Daniel Vetter5a21b662016-05-24 17:13:53 +020011445 /*
11446 * This is not being used for older platforms, because
11447 * non-availability of flip done interrupt forces us to use
11448 * CS flips. Older platforms derive flip done using some clever
11449 * tricks involving the flip_pending status bits and vblank irqs.
11450 * So using MMIO flips there would disrupt this mechanism.
11451 */
11452
11453 if (engine == NULL)
11454 return true;
11455
11456 if (INTEL_GEN(engine->i915) < 5)
11457 return false;
11458
11459 if (i915.use_mmio_flip < 0)
11460 return false;
11461 else if (i915.use_mmio_flip > 0)
11462 return true;
11463 else if (i915.enable_execlists)
11464 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011465
11466 resv = i915_gem_object_get_dmabuf_resv(obj);
11467 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011468 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011469
11470 return engine != i915_gem_request_get_engine(obj->last_write_req);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011471}
11472
11473static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11474 unsigned int rotation,
11475 struct intel_flip_work *work)
11476{
11477 struct drm_device *dev = intel_crtc->base.dev;
11478 struct drm_i915_private *dev_priv = dev->dev_private;
11479 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11480 const enum pipe pipe = intel_crtc->pipe;
11481 u32 ctl, stride, tile_height;
11482
11483 ctl = I915_READ(PLANE_CTL(pipe, 0));
11484 ctl &= ~PLANE_CTL_TILED_MASK;
11485 switch (fb->modifier[0]) {
11486 case DRM_FORMAT_MOD_NONE:
11487 break;
11488 case I915_FORMAT_MOD_X_TILED:
11489 ctl |= PLANE_CTL_TILED_X;
11490 break;
11491 case I915_FORMAT_MOD_Y_TILED:
11492 ctl |= PLANE_CTL_TILED_Y;
11493 break;
11494 case I915_FORMAT_MOD_Yf_TILED:
11495 ctl |= PLANE_CTL_TILED_YF;
11496 break;
11497 default:
11498 MISSING_CASE(fb->modifier[0]);
11499 }
11500
11501 /*
11502 * The stride is either expressed as a multiple of 64 bytes chunks for
11503 * linear buffers or in number of tiles for tiled buffers.
11504 */
11505 if (intel_rotation_90_or_270(rotation)) {
11506 /* stride = Surface height in tiles */
11507 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11508 stride = DIV_ROUND_UP(fb->height, tile_height);
11509 } else {
11510 stride = fb->pitches[0] /
11511 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11512 fb->pixel_format);
11513 }
11514
11515 /*
11516 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11517 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11518 */
11519 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11520 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11521
11522 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11523 POSTING_READ(PLANE_SURF(pipe, 0));
11524}
11525
11526static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11527 struct intel_flip_work *work)
11528{
11529 struct drm_device *dev = intel_crtc->base.dev;
11530 struct drm_i915_private *dev_priv = dev->dev_private;
11531 struct intel_framebuffer *intel_fb =
11532 to_intel_framebuffer(intel_crtc->base.primary->fb);
11533 struct drm_i915_gem_object *obj = intel_fb->obj;
11534 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11535 u32 dspcntr;
11536
11537 dspcntr = I915_READ(reg);
11538
11539 if (obj->tiling_mode != I915_TILING_NONE)
11540 dspcntr |= DISPPLANE_TILED;
11541 else
11542 dspcntr &= ~DISPPLANE_TILED;
11543
11544 I915_WRITE(reg, dspcntr);
11545
11546 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11547 POSTING_READ(DSPSURF(intel_crtc->plane));
11548}
11549
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011550static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011551{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011552 struct intel_flip_work *work =
11553 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011554 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11555 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11556 struct intel_framebuffer *intel_fb =
11557 to_intel_framebuffer(crtc->base.primary->fb);
11558 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011559 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011560
11561 if (work->flip_queued_req)
11562 WARN_ON(__i915_wait_request(work->flip_queued_req,
11563 false, NULL,
11564 &dev_priv->rps.mmioflips));
11565
11566 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011567 resv = i915_gem_object_get_dmabuf_resv(obj);
11568 if (resv)
11569 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011570 MAX_SCHEDULE_TIMEOUT) < 0);
11571
11572 intel_pipe_update_start(crtc);
11573
11574 if (INTEL_GEN(dev_priv) >= 9)
11575 skl_do_mmio_flip(crtc, work->rotation, work);
11576 else
11577 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11578 ilk_do_mmio_flip(crtc, work);
11579
11580 intel_pipe_update_end(crtc, work);
11581}
11582
11583static int intel_default_queue_flip(struct drm_device *dev,
11584 struct drm_crtc *crtc,
11585 struct drm_framebuffer *fb,
11586 struct drm_i915_gem_object *obj,
11587 struct drm_i915_gem_request *req,
11588 uint32_t flags)
11589{
11590 return -ENODEV;
11591}
11592
11593static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11594 struct intel_crtc *intel_crtc,
11595 struct intel_flip_work *work)
11596{
11597 u32 addr, vblank;
11598
11599 if (!atomic_read(&work->pending))
11600 return false;
11601
11602 smp_rmb();
11603
11604 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11605 if (work->flip_ready_vblank == 0) {
11606 if (work->flip_queued_req &&
11607 !i915_gem_request_completed(work->flip_queued_req, true))
11608 return false;
11609
11610 work->flip_ready_vblank = vblank;
11611 }
11612
11613 if (vblank - work->flip_ready_vblank < 3)
11614 return false;
11615
11616 /* Potential stall - if we see that the flip has happened,
11617 * assume a missed interrupt. */
11618 if (INTEL_GEN(dev_priv) >= 4)
11619 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11620 else
11621 addr = I915_READ(DSPADDR(intel_crtc->plane));
11622
11623 /* There is a potential issue here with a false positive after a flip
11624 * to the same address. We could address this by checking for a
11625 * non-incrementing frame counter.
11626 */
11627 return addr == work->gtt_offset;
11628}
11629
11630void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11631{
11632 struct drm_device *dev = dev_priv->dev;
11633 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011635 struct intel_flip_work *work;
11636
11637 WARN_ON(!in_interrupt());
11638
11639 if (crtc == NULL)
11640 return;
11641
11642 spin_lock(&dev->event_lock);
11643 work = intel_crtc->flip_work;
11644
11645 if (work != NULL && !is_mmio_work(work) &&
11646 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11647 WARN_ONCE(1,
11648 "Kicking stuck page flip: queued at %d, now %d\n",
11649 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11650 page_flip_completed(intel_crtc);
11651 work = NULL;
11652 }
11653
11654 if (work != NULL && !is_mmio_work(work) &&
11655 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11656 intel_queue_rps_boost_for_request(work->flip_queued_req);
11657 spin_unlock(&dev->event_lock);
11658}
11659
11660static int intel_crtc_page_flip(struct drm_crtc *crtc,
11661 struct drm_framebuffer *fb,
11662 struct drm_pending_vblank_event *event,
11663 uint32_t page_flip_flags)
11664{
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011665 struct drm_device *dev = crtc->dev;
Maarten Lankhorstaa420dd2016-05-17 15:07:51 +020011666 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011667 struct drm_framebuffer *old_fb = crtc->primary->fb;
11668 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11670 struct drm_plane *primary = crtc->primary;
11671 enum pipe pipe = intel_crtc->pipe;
11672 struct intel_flip_work *work;
11673 struct intel_engine_cs *engine;
11674 bool mmio_flip;
11675 struct drm_i915_gem_request *request = NULL;
11676 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011677
Daniel Vetter5a21b662016-05-24 17:13:53 +020011678 /*
11679 * drm_mode_page_flip_ioctl() should already catch this, but double
11680 * check to be safe. In the future we may enable pageflipping from
11681 * a disabled primary plane.
11682 */
11683 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11684 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011685
Daniel Vetter5a21b662016-05-24 17:13:53 +020011686 /* Can't change pixel format via MI display flips. */
11687 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11688 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011689
Daniel Vetter5a21b662016-05-24 17:13:53 +020011690 /*
11691 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11692 * Note that pitch changes could also affect these register.
11693 */
11694 if (INTEL_INFO(dev)->gen > 3 &&
11695 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11696 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11697 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011698
Daniel Vetter5a21b662016-05-24 17:13:53 +020011699 if (i915_terminally_wedged(&dev_priv->gpu_error))
11700 goto out_hang;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011701
Daniel Vetter5a21b662016-05-24 17:13:53 +020011702 work = kzalloc(sizeof(*work), GFP_KERNEL);
11703 if (work == NULL)
11704 return -ENOMEM;
11705
11706 work->event = event;
11707 work->crtc = crtc;
11708 work->old_fb = old_fb;
11709 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011710
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011711 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011712 if (ret)
11713 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011714
Daniel Vetter5a21b662016-05-24 17:13:53 +020011715 /* We borrow the event spin lock for protecting flip_work */
11716 spin_lock_irq(&dev->event_lock);
11717 if (intel_crtc->flip_work) {
11718 /* Before declaring the flip queue wedged, check if
11719 * the hardware completed the operation behind our backs.
11720 */
11721 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11722 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11723 page_flip_completed(intel_crtc);
11724 } else {
11725 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11726 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011727
Daniel Vetter5a21b662016-05-24 17:13:53 +020011728 drm_crtc_vblank_put(crtc);
11729 kfree(work);
11730 return -EBUSY;
11731 }
11732 }
11733 intel_crtc->flip_work = work;
11734 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011735
Daniel Vetter5a21b662016-05-24 17:13:53 +020011736 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11737 flush_workqueue(dev_priv->wq);
11738
11739 /* Reference the objects for the scheduled work. */
11740 drm_framebuffer_reference(work->old_fb);
11741 drm_gem_object_reference(&obj->base);
11742
11743 crtc->primary->fb = fb;
11744 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011745
11746 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11747 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011748
11749 work->pending_flip_obj = obj;
11750
11751 ret = i915_mutex_lock_interruptible(dev);
11752 if (ret)
11753 goto cleanup;
11754
11755 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11756 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11757 ret = -EIO;
11758 goto cleanup;
11759 }
11760
11761 atomic_inc(&intel_crtc->unpin_work_count);
11762
11763 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11764 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11765
11766 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11767 engine = &dev_priv->engine[BCS];
11768 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11769 /* vlv: DISPLAY_FLIP fails to change tiling */
11770 engine = NULL;
11771 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11772 engine = &dev_priv->engine[BCS];
11773 } else if (INTEL_INFO(dev)->gen >= 7) {
11774 engine = i915_gem_request_get_engine(obj->last_write_req);
11775 if (engine == NULL || engine->id != RCS)
11776 engine = &dev_priv->engine[BCS];
11777 } else {
11778 engine = &dev_priv->engine[RCS];
11779 }
11780
11781 mmio_flip = use_mmio_flip(engine, obj);
11782
11783 /* When using CS flips, we want to emit semaphores between rings.
11784 * However, when using mmio flips we will create a task to do the
11785 * synchronisation, so all we want here is to pin the framebuffer
11786 * into the display plane and skip any waits.
11787 */
11788 if (!mmio_flip) {
11789 ret = i915_gem_object_sync(obj, engine, &request);
11790 if (!ret && !request) {
11791 request = i915_gem_request_alloc(engine, NULL);
11792 ret = PTR_ERR_OR_ZERO(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011793 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011794
Daniel Vetter5a21b662016-05-24 17:13:53 +020011795 if (ret)
11796 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011797 }
11798
Daniel Vetter5a21b662016-05-24 17:13:53 +020011799 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11800 if (ret)
11801 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011802
Daniel Vetter5a21b662016-05-24 17:13:53 +020011803 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11804 obj, 0);
11805 work->gtt_offset += intel_crtc->dspaddr_offset;
11806 work->rotation = crtc->primary->state->rotation;
11807
11808 if (mmio_flip) {
11809 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11810
11811 i915_gem_request_assign(&work->flip_queued_req,
11812 obj->last_write_req);
11813
11814 schedule_work(&work->mmio_work);
11815 } else {
11816 i915_gem_request_assign(&work->flip_queued_req, request);
11817 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11818 page_flip_flags);
11819 if (ret)
11820 goto cleanup_unpin;
11821
11822 intel_mark_page_flip_active(intel_crtc, work);
11823
11824 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011825 }
11826
Daniel Vetter5a21b662016-05-24 17:13:53 +020011827 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11828 to_intel_plane(primary)->frontbuffer_bit);
11829 mutex_unlock(&dev->struct_mutex);
11830
11831 intel_frontbuffer_flip_prepare(dev,
11832 to_intel_plane(primary)->frontbuffer_bit);
11833
11834 trace_i915_flip_request(intel_crtc->plane, obj);
11835
11836 return 0;
11837
11838cleanup_unpin:
11839 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11840cleanup_pending:
11841 if (!IS_ERR_OR_NULL(request))
11842 i915_add_request_no_flush(request);
11843 atomic_dec(&intel_crtc->unpin_work_count);
11844 mutex_unlock(&dev->struct_mutex);
11845cleanup:
11846 crtc->primary->fb = old_fb;
11847 update_state_fb(crtc->primary);
11848
11849 drm_gem_object_unreference_unlocked(&obj->base);
11850 drm_framebuffer_unreference(work->old_fb);
11851
11852 spin_lock_irq(&dev->event_lock);
11853 intel_crtc->flip_work = NULL;
11854 spin_unlock_irq(&dev->event_lock);
11855
11856 drm_crtc_vblank_put(crtc);
11857free_work:
11858 kfree(work);
11859
11860 if (ret == -EIO) {
11861 struct drm_atomic_state *state;
11862 struct drm_plane_state *plane_state;
11863
11864out_hang:
11865 state = drm_atomic_state_alloc(dev);
11866 if (!state)
11867 return -ENOMEM;
11868 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11869
11870retry:
11871 plane_state = drm_atomic_get_plane_state(state, primary);
11872 ret = PTR_ERR_OR_ZERO(plane_state);
11873 if (!ret) {
11874 drm_atomic_set_fb_for_plane(plane_state, fb);
11875
11876 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11877 if (!ret)
11878 ret = drm_atomic_commit(state);
11879 }
11880
11881 if (ret == -EDEADLK) {
11882 drm_modeset_backoff(state->acquire_ctx);
11883 drm_atomic_state_clear(state);
11884 goto retry;
11885 }
11886
11887 if (ret)
11888 drm_atomic_state_free(state);
11889
11890 if (ret == 0 && event) {
11891 spin_lock_irq(&dev->event_lock);
11892 drm_crtc_send_vblank_event(crtc, event);
11893 spin_unlock_irq(&dev->event_lock);
11894 }
11895 }
11896 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011897}
11898
Daniel Vetter5a21b662016-05-24 17:13:53 +020011899
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011900/**
11901 * intel_wm_need_update - Check whether watermarks need updating
11902 * @plane: drm plane
11903 * @state: new plane state
11904 *
11905 * Check current plane state versus the new one to determine whether
11906 * watermarks need to be recalculated.
11907 *
11908 * Returns true or false.
11909 */
11910static bool intel_wm_need_update(struct drm_plane *plane,
11911 struct drm_plane_state *state)
11912{
Matt Roperd21fbe82015-09-24 15:53:12 -070011913 struct intel_plane_state *new = to_intel_plane_state(state);
11914 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11915
11916 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011917 if (new->visible != cur->visible)
11918 return true;
11919
11920 if (!cur->base.fb || !new->base.fb)
11921 return false;
11922
11923 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11924 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011925 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11926 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11927 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11928 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011929 return true;
11930
11931 return false;
11932}
11933
Matt Roperd21fbe82015-09-24 15:53:12 -070011934static bool needs_scaling(struct intel_plane_state *state)
11935{
11936 int src_w = drm_rect_width(&state->src) >> 16;
11937 int src_h = drm_rect_height(&state->src) >> 16;
11938 int dst_w = drm_rect_width(&state->dst);
11939 int dst_h = drm_rect_height(&state->dst);
11940
11941 return (src_w != dst_w || src_h != dst_h);
11942}
11943
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011944int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11945 struct drm_plane_state *plane_state)
11946{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011947 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011948 struct drm_crtc *crtc = crtc_state->crtc;
11949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11950 struct drm_plane *plane = plane_state->plane;
11951 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011952 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011953 struct intel_plane_state *old_plane_state =
11954 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011955 bool mode_changed = needs_modeset(crtc_state);
11956 bool was_crtc_enabled = crtc->state->active;
11957 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011958 bool turn_off, turn_on, visible, was_visible;
11959 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011960 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011961
11962 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11963 plane->type != DRM_PLANE_TYPE_CURSOR) {
11964 ret = skl_update_scaler_plane(
11965 to_intel_crtc_state(crtc_state),
11966 to_intel_plane_state(plane_state));
11967 if (ret)
11968 return ret;
11969 }
11970
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011971 was_visible = old_plane_state->visible;
11972 visible = to_intel_plane_state(plane_state)->visible;
11973
11974 if (!was_crtc_enabled && WARN_ON(was_visible))
11975 was_visible = false;
11976
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011977 /*
11978 * Visibility is calculated as if the crtc was on, but
11979 * after scaler setup everything depends on it being off
11980 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011981 *
11982 * FIXME this is wrong for watermarks. Watermarks should also
11983 * be computed as if the pipe would be active. Perhaps move
11984 * per-plane wm computation to the .check_plane() hook, and
11985 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011986 */
11987 if (!is_crtc_enabled)
11988 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011989
11990 if (!was_visible && !visible)
11991 return 0;
11992
Maarten Lankhorste8861672016-02-24 11:24:26 +010011993 if (fb != old_plane_state->base.fb)
11994 pipe_config->fb_changed = true;
11995
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011996 turn_off = was_visible && (!visible || mode_changed);
11997 turn_on = visible && (!was_visible || mode_changed);
11998
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011999 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030012000 intel_crtc->base.base.id,
12001 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012002 plane->base.id, plane->name,
12003 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012004
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012005 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12006 plane->base.id, plane->name,
12007 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012008 turn_off, turn_on, mode_changed);
12009
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012010 if (turn_on) {
12011 pipe_config->update_wm_pre = true;
12012
12013 /* must disable cxsr around plane enable/disable */
12014 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12015 pipe_config->disable_cxsr = true;
12016 } else if (turn_off) {
12017 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012018
Ville Syrjälä852eb002015-06-24 22:00:07 +030012019 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012020 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012021 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012022 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012023 /* FIXME bollocks */
12024 pipe_config->update_wm_pre = true;
12025 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012026 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012027
Matt Ropered4a6a72016-02-23 17:20:13 -080012028 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012029 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12030 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012031 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12032
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012033 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012034 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012035
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012036 /*
12037 * WaCxSRDisabledForSpriteScaling:ivb
12038 *
12039 * cstate->update_wm was already set above, so this flag will
12040 * take effect when we commit and program watermarks.
12041 */
12042 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12043 needs_scaling(to_intel_plane_state(plane_state)) &&
12044 !needs_scaling(old_plane_state))
12045 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012046
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012047 return 0;
12048}
12049
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012050static bool encoders_cloneable(const struct intel_encoder *a,
12051 const struct intel_encoder *b)
12052{
12053 /* masks could be asymmetric, so check both ways */
12054 return a == b || (a->cloneable & (1 << b->type) &&
12055 b->cloneable & (1 << a->type));
12056}
12057
12058static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12059 struct intel_crtc *crtc,
12060 struct intel_encoder *encoder)
12061{
12062 struct intel_encoder *source_encoder;
12063 struct drm_connector *connector;
12064 struct drm_connector_state *connector_state;
12065 int i;
12066
12067 for_each_connector_in_state(state, connector, connector_state, i) {
12068 if (connector_state->crtc != &crtc->base)
12069 continue;
12070
12071 source_encoder =
12072 to_intel_encoder(connector_state->best_encoder);
12073 if (!encoders_cloneable(encoder, source_encoder))
12074 return false;
12075 }
12076
12077 return true;
12078}
12079
12080static bool check_encoder_cloning(struct drm_atomic_state *state,
12081 struct intel_crtc *crtc)
12082{
12083 struct intel_encoder *encoder;
12084 struct drm_connector *connector;
12085 struct drm_connector_state *connector_state;
12086 int i;
12087
12088 for_each_connector_in_state(state, connector, connector_state, i) {
12089 if (connector_state->crtc != &crtc->base)
12090 continue;
12091
12092 encoder = to_intel_encoder(connector_state->best_encoder);
12093 if (!check_single_encoder_cloning(state, crtc, encoder))
12094 return false;
12095 }
12096
12097 return true;
12098}
12099
12100static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12101 struct drm_crtc_state *crtc_state)
12102{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012103 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012104 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012106 struct intel_crtc_state *pipe_config =
12107 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012108 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012109 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012110 bool mode_changed = needs_modeset(crtc_state);
12111
12112 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12113 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12114 return -EINVAL;
12115 }
12116
Ville Syrjälä852eb002015-06-24 22:00:07 +030012117 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012118 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012119
Maarten Lankhorstad421372015-06-15 12:33:42 +020012120 if (mode_changed && crtc_state->enable &&
12121 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012122 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012123 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12124 pipe_config);
12125 if (ret)
12126 return ret;
12127 }
12128
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012129 if (crtc_state->color_mgmt_changed) {
12130 ret = intel_color_check(crtc, crtc_state);
12131 if (ret)
12132 return ret;
12133 }
12134
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012135 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012136 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012137 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012138 if (ret) {
12139 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012140 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012141 }
12142 }
12143
12144 if (dev_priv->display.compute_intermediate_wm &&
12145 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12146 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12147 return 0;
12148
12149 /*
12150 * Calculate 'intermediate' watermarks that satisfy both the
12151 * old state and the new state. We can program these
12152 * immediately.
12153 */
12154 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12155 intel_crtc,
12156 pipe_config);
12157 if (ret) {
12158 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12159 return ret;
12160 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012161 } else if (dev_priv->display.compute_intermediate_wm) {
12162 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12163 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012164 }
12165
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012166 if (INTEL_INFO(dev)->gen >= 9) {
12167 if (mode_changed)
12168 ret = skl_update_scaler_crtc(pipe_config);
12169
12170 if (!ret)
12171 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12172 pipe_config);
12173 }
12174
12175 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012176}
12177
Jani Nikula65b38e02015-04-13 11:26:56 +030012178static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012179 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012180 .atomic_begin = intel_begin_crtc_commit,
12181 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012182 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012183};
12184
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012185static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12186{
12187 struct intel_connector *connector;
12188
12189 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012190 if (connector->base.state->crtc)
12191 drm_connector_unreference(&connector->base);
12192
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012193 if (connector->base.encoder) {
12194 connector->base.state->best_encoder =
12195 connector->base.encoder;
12196 connector->base.state->crtc =
12197 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012198
12199 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012200 } else {
12201 connector->base.state->best_encoder = NULL;
12202 connector->base.state->crtc = NULL;
12203 }
12204 }
12205}
12206
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012207static void
Robin Schroereba905b2014-05-18 02:24:50 +020012208connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012209 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012210{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012211 int bpp = pipe_config->pipe_bpp;
12212
12213 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12214 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012215 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012216
12217 /* Don't use an invalid EDID bpc value */
12218 if (connector->base.display_info.bpc &&
12219 connector->base.display_info.bpc * 3 < bpp) {
12220 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12221 bpp, connector->base.display_info.bpc*3);
12222 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12223 }
12224
Jani Nikula013dd9e2016-01-13 16:35:20 +020012225 /* Clamp bpp to default limit on screens without EDID 1.4 */
12226 if (connector->base.display_info.bpc == 0) {
12227 int type = connector->base.connector_type;
12228 int clamp_bpp = 24;
12229
12230 /* Fall back to 18 bpp when DP sink capability is unknown. */
12231 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12232 type == DRM_MODE_CONNECTOR_eDP)
12233 clamp_bpp = 18;
12234
12235 if (bpp > clamp_bpp) {
12236 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12237 bpp, clamp_bpp);
12238 pipe_config->pipe_bpp = clamp_bpp;
12239 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012240 }
12241}
12242
12243static int
12244compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012245 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012246{
12247 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012248 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012249 struct drm_connector *connector;
12250 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012251 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012252
Wayne Boyer666a4532015-12-09 12:29:35 -080012253 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012254 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012255 else if (INTEL_INFO(dev)->gen >= 5)
12256 bpp = 12*3;
12257 else
12258 bpp = 8*3;
12259
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012260
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012261 pipe_config->pipe_bpp = bpp;
12262
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012263 state = pipe_config->base.state;
12264
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012265 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012266 for_each_connector_in_state(state, connector, connector_state, i) {
12267 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012268 continue;
12269
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012270 connected_sink_compute_bpp(to_intel_connector(connector),
12271 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012272 }
12273
12274 return bpp;
12275}
12276
Daniel Vetter644db712013-09-19 14:53:58 +020012277static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12278{
12279 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12280 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012281 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012282 mode->crtc_hdisplay, mode->crtc_hsync_start,
12283 mode->crtc_hsync_end, mode->crtc_htotal,
12284 mode->crtc_vdisplay, mode->crtc_vsync_start,
12285 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12286}
12287
Daniel Vetterc0b03412013-05-28 12:05:54 +020012288static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012289 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012290 const char *context)
12291{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012292 struct drm_device *dev = crtc->base.dev;
12293 struct drm_plane *plane;
12294 struct intel_plane *intel_plane;
12295 struct intel_plane_state *state;
12296 struct drm_framebuffer *fb;
12297
Ville Syrjälä78108b72016-05-27 20:59:19 +030012298 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12299 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012300 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012301
Jani Nikulada205632016-03-15 21:51:10 +020012302 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012303 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12304 pipe_config->pipe_bpp, pipe_config->dither);
12305 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12306 pipe_config->has_pch_encoder,
12307 pipe_config->fdi_lanes,
12308 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12309 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12310 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012311 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äeb14cb72013-09-10 17:02:54 +030012312 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012313 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012314 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12315 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12316 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012317
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012318 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012319 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012320 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012321 pipe_config->dp_m2_n2.gmch_m,
12322 pipe_config->dp_m2_n2.gmch_n,
12323 pipe_config->dp_m2_n2.link_m,
12324 pipe_config->dp_m2_n2.link_n,
12325 pipe_config->dp_m2_n2.tu);
12326
Daniel Vetter55072d12014-11-20 16:10:28 +010012327 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12328 pipe_config->has_audio,
12329 pipe_config->has_infoframe);
12330
Daniel Vetterc0b03412013-05-28 12:05:54 +020012331 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012332 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012333 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012334 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12335 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012336 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012337 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12338 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012339 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12340 crtc->num_scalers,
12341 pipe_config->scaler_state.scaler_users,
12342 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012343 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12344 pipe_config->gmch_pfit.control,
12345 pipe_config->gmch_pfit.pgm_ratios,
12346 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012347 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012348 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012349 pipe_config->pch_pfit.size,
12350 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012351 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012352 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012353
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012354 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012355 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012356 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012357 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012358 pipe_config->ddi_pll_sel,
12359 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012360 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012361 pipe_config->dpll_hw_state.pll0,
12362 pipe_config->dpll_hw_state.pll1,
12363 pipe_config->dpll_hw_state.pll2,
12364 pipe_config->dpll_hw_state.pll3,
12365 pipe_config->dpll_hw_state.pll6,
12366 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012367 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012368 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012369 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012370 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012371 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12372 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12373 pipe_config->ddi_pll_sel,
12374 pipe_config->dpll_hw_state.ctrl1,
12375 pipe_config->dpll_hw_state.cfgcr1,
12376 pipe_config->dpll_hw_state.cfgcr2);
12377 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012378 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 +010012379 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012380 pipe_config->dpll_hw_state.wrpll,
12381 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012382 } else {
12383 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12384 "fp0: 0x%x, fp1: 0x%x\n",
12385 pipe_config->dpll_hw_state.dpll,
12386 pipe_config->dpll_hw_state.dpll_md,
12387 pipe_config->dpll_hw_state.fp0,
12388 pipe_config->dpll_hw_state.fp1);
12389 }
12390
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012391 DRM_DEBUG_KMS("planes on this crtc\n");
12392 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12393 intel_plane = to_intel_plane(plane);
12394 if (intel_plane->pipe != crtc->pipe)
12395 continue;
12396
12397 state = to_intel_plane_state(plane->state);
12398 fb = state->base.fb;
12399 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012400 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12401 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012402 continue;
12403 }
12404
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012405 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12406 plane->base.id, plane->name);
12407 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12408 fb->base.id, fb->width, fb->height,
12409 drm_get_format_name(fb->pixel_format));
12410 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12411 state->scaler_id,
12412 state->src.x1 >> 16, state->src.y1 >> 16,
12413 drm_rect_width(&state->src) >> 16,
12414 drm_rect_height(&state->src) >> 16,
12415 state->dst.x1, state->dst.y1,
12416 drm_rect_width(&state->dst),
12417 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012418 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012419}
12420
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012421static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012422{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012423 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012424 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012425 unsigned int used_ports = 0;
12426
12427 /*
12428 * Walk the connector list instead of the encoder
12429 * list to detect the problem on ddi platforms
12430 * where there's just one encoder per digital port.
12431 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012432 drm_for_each_connector(connector, dev) {
12433 struct drm_connector_state *connector_state;
12434 struct intel_encoder *encoder;
12435
12436 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12437 if (!connector_state)
12438 connector_state = connector->state;
12439
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012440 if (!connector_state->best_encoder)
12441 continue;
12442
12443 encoder = to_intel_encoder(connector_state->best_encoder);
12444
12445 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012446
12447 switch (encoder->type) {
12448 unsigned int port_mask;
12449 case INTEL_OUTPUT_UNKNOWN:
12450 if (WARN_ON(!HAS_DDI(dev)))
12451 break;
12452 case INTEL_OUTPUT_DISPLAYPORT:
12453 case INTEL_OUTPUT_HDMI:
12454 case INTEL_OUTPUT_EDP:
12455 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12456
12457 /* the same port mustn't appear more than once */
12458 if (used_ports & port_mask)
12459 return false;
12460
12461 used_ports |= port_mask;
12462 default:
12463 break;
12464 }
12465 }
12466
12467 return true;
12468}
12469
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012470static void
12471clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12472{
12473 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012474 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012475 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012476 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012477 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012478 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012479
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012480 /* FIXME: before the switch to atomic started, a new pipe_config was
12481 * kzalloc'd. Code that depends on any field being zero should be
12482 * fixed, so that the crtc_state can be safely duplicated. For now,
12483 * only fields that are know to not cause problems are preserved. */
12484
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012485 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012486 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012487 shared_dpll = crtc_state->shared_dpll;
12488 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012489 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012490 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012491
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012492 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012493
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012494 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012495 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012496 crtc_state->shared_dpll = shared_dpll;
12497 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012498 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012499 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012500}
12501
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012502static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012503intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012504 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012505{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012506 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012507 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012508 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012509 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012510 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012511 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012512 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012513
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012514 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012515
Daniel Vettere143a212013-07-04 12:01:15 +020012516 pipe_config->cpu_transcoder =
12517 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012518
Imre Deak2960bc92013-07-30 13:36:32 +030012519 /*
12520 * Sanitize sync polarity flags based on requested ones. If neither
12521 * positive or negative polarity is requested, treat this as meaning
12522 * negative polarity.
12523 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012524 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012525 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012526 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012527
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012528 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012529 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012530 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012531
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012532 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12533 pipe_config);
12534 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012535 goto fail;
12536
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012537 /*
12538 * Determine the real pipe dimensions. Note that stereo modes can
12539 * increase the actual pipe size due to the frame doubling and
12540 * insertion of additional space for blanks between the frame. This
12541 * is stored in the crtc timings. We use the requested mode to do this
12542 * computation to clearly distinguish it from the adjusted mode, which
12543 * can be changed by the connectors in the below retry loop.
12544 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012545 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012546 &pipe_config->pipe_src_w,
12547 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012548
Daniel Vettere29c22c2013-02-21 00:00:16 +010012549encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012550 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012551 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012552 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012553
Daniel Vetter135c81b2013-07-21 21:37:09 +020012554 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012555 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12556 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012557
Daniel Vetter7758a112012-07-08 19:40:39 +020012558 /* Pass our mode to the connectors and the CRTC to give them a chance to
12559 * adjust it according to limitations or connector properties, and also
12560 * a chance to reject the mode entirely.
12561 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012562 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012563 if (connector_state->crtc != crtc)
12564 continue;
12565
12566 encoder = to_intel_encoder(connector_state->best_encoder);
12567
Daniel Vetterefea6e82013-07-21 21:36:59 +020012568 if (!(encoder->compute_config(encoder, pipe_config))) {
12569 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012570 goto fail;
12571 }
12572 }
12573
Daniel Vetterff9a6752013-06-01 17:16:21 +020012574 /* Set default port clock if not overwritten by the encoder. Needs to be
12575 * done afterwards in case the encoder adjusts the mode. */
12576 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012577 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012578 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012579
Daniel Vettera43f6e02013-06-07 23:10:32 +020012580 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012581 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012582 DRM_DEBUG_KMS("CRTC fixup failed\n");
12583 goto fail;
12584 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012585
12586 if (ret == RETRY) {
12587 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12588 ret = -EINVAL;
12589 goto fail;
12590 }
12591
12592 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12593 retry = false;
12594 goto encoder_retry;
12595 }
12596
Daniel Vettere8fa4272015-08-12 11:43:34 +020012597 /* Dithering seems to not pass-through bits correctly when it should, so
12598 * only enable it on 6bpc panels. */
12599 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012600 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012601 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012602
Daniel Vetter7758a112012-07-08 19:40:39 +020012603fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012604 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012605}
12606
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012607static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012608intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012609{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012610 struct drm_crtc *crtc;
12611 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012612 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012613
Ville Syrjälä76688512014-01-10 11:28:06 +020012614 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012615 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012616 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012617
12618 /* Update hwmode for vblank functions */
12619 if (crtc->state->active)
12620 crtc->hwmode = crtc->state->adjusted_mode;
12621 else
12622 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012623
12624 /*
12625 * Update legacy state to satisfy fbc code. This can
12626 * be removed when fbc uses the atomic state.
12627 */
12628 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12629 struct drm_plane_state *plane_state = crtc->primary->state;
12630
12631 crtc->primary->fb = plane_state->fb;
12632 crtc->x = plane_state->src_x >> 16;
12633 crtc->y = plane_state->src_y >> 16;
12634 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012635 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012636}
12637
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012638static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012639{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012640 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012641
12642 if (clock1 == clock2)
12643 return true;
12644
12645 if (!clock1 || !clock2)
12646 return false;
12647
12648 diff = abs(clock1 - clock2);
12649
12650 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12651 return true;
12652
12653 return false;
12654}
12655
Daniel Vetter25c5b262012-07-08 22:08:04 +020012656#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12657 list_for_each_entry((intel_crtc), \
12658 &(dev)->mode_config.crtc_list, \
12659 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012660 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012661
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012662static bool
12663intel_compare_m_n(unsigned int m, unsigned int n,
12664 unsigned int m2, unsigned int n2,
12665 bool exact)
12666{
12667 if (m == m2 && n == n2)
12668 return true;
12669
12670 if (exact || !m || !n || !m2 || !n2)
12671 return false;
12672
12673 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12674
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012675 if (n > n2) {
12676 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012677 m2 <<= 1;
12678 n2 <<= 1;
12679 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012680 } else if (n < n2) {
12681 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012682 m <<= 1;
12683 n <<= 1;
12684 }
12685 }
12686
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012687 if (n != n2)
12688 return false;
12689
12690 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012691}
12692
12693static bool
12694intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12695 struct intel_link_m_n *m2_n2,
12696 bool adjust)
12697{
12698 if (m_n->tu == m2_n2->tu &&
12699 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12700 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12701 intel_compare_m_n(m_n->link_m, m_n->link_n,
12702 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12703 if (adjust)
12704 *m2_n2 = *m_n;
12705
12706 return true;
12707 }
12708
12709 return false;
12710}
12711
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012712static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012713intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012714 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012715 struct intel_crtc_state *pipe_config,
12716 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012717{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012718 bool ret = true;
12719
12720#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12721 do { \
12722 if (!adjust) \
12723 DRM_ERROR(fmt, ##__VA_ARGS__); \
12724 else \
12725 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12726 } while (0)
12727
Daniel Vetter66e985c2013-06-05 13:34:20 +020012728#define PIPE_CONF_CHECK_X(name) \
12729 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012730 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012731 "(expected 0x%08x, found 0x%08x)\n", \
12732 current_config->name, \
12733 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012734 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012735 }
12736
Daniel Vetter08a24032013-04-19 11:25:34 +020012737#define PIPE_CONF_CHECK_I(name) \
12738 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012739 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012740 "(expected %i, found %i)\n", \
12741 current_config->name, \
12742 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012743 ret = false; \
12744 }
12745
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012746#define PIPE_CONF_CHECK_P(name) \
12747 if (current_config->name != pipe_config->name) { \
12748 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12749 "(expected %p, found %p)\n", \
12750 current_config->name, \
12751 pipe_config->name); \
12752 ret = false; \
12753 }
12754
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012755#define PIPE_CONF_CHECK_M_N(name) \
12756 if (!intel_compare_link_m_n(&current_config->name, \
12757 &pipe_config->name,\
12758 adjust)) { \
12759 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12760 "(expected tu %i gmch %i/%i link %i/%i, " \
12761 "found tu %i, gmch %i/%i link %i/%i)\n", \
12762 current_config->name.tu, \
12763 current_config->name.gmch_m, \
12764 current_config->name.gmch_n, \
12765 current_config->name.link_m, \
12766 current_config->name.link_n, \
12767 pipe_config->name.tu, \
12768 pipe_config->name.gmch_m, \
12769 pipe_config->name.gmch_n, \
12770 pipe_config->name.link_m, \
12771 pipe_config->name.link_n); \
12772 ret = false; \
12773 }
12774
Daniel Vetter55c561a2016-03-30 11:34:36 +020012775/* This is required for BDW+ where there is only one set of registers for
12776 * switching between high and low RR.
12777 * This macro can be used whenever a comparison has to be made between one
12778 * hw state and multiple sw state variables.
12779 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012780#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12781 if (!intel_compare_link_m_n(&current_config->name, \
12782 &pipe_config->name, adjust) && \
12783 !intel_compare_link_m_n(&current_config->alt_name, \
12784 &pipe_config->name, adjust)) { \
12785 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12786 "(expected tu %i gmch %i/%i link %i/%i, " \
12787 "or tu %i gmch %i/%i link %i/%i, " \
12788 "found tu %i, gmch %i/%i link %i/%i)\n", \
12789 current_config->name.tu, \
12790 current_config->name.gmch_m, \
12791 current_config->name.gmch_n, \
12792 current_config->name.link_m, \
12793 current_config->name.link_n, \
12794 current_config->alt_name.tu, \
12795 current_config->alt_name.gmch_m, \
12796 current_config->alt_name.gmch_n, \
12797 current_config->alt_name.link_m, \
12798 current_config->alt_name.link_n, \
12799 pipe_config->name.tu, \
12800 pipe_config->name.gmch_m, \
12801 pipe_config->name.gmch_n, \
12802 pipe_config->name.link_m, \
12803 pipe_config->name.link_n); \
12804 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012805 }
12806
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012807#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12808 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012809 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012810 "(expected %i, found %i)\n", \
12811 current_config->name & (mask), \
12812 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012813 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012814 }
12815
Ville Syrjälä5e550652013-09-06 23:29:07 +030012816#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12817 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012818 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012819 "(expected %i, found %i)\n", \
12820 current_config->name, \
12821 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012822 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012823 }
12824
Daniel Vetterbb760062013-06-06 14:55:52 +020012825#define PIPE_CONF_QUIRK(quirk) \
12826 ((current_config->quirks | pipe_config->quirks) & (quirk))
12827
Daniel Vettereccb1402013-05-22 00:50:22 +020012828 PIPE_CONF_CHECK_I(cpu_transcoder);
12829
Daniel Vetter08a24032013-04-19 11:25:34 +020012830 PIPE_CONF_CHECK_I(has_pch_encoder);
12831 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012832 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012833
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012834 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012835 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012836 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012837
12838 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012839 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012840
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012841 if (current_config->has_drrs)
12842 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12843 } else
12844 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012845
Jani Nikulaa65347b2015-11-27 12:21:46 +020012846 PIPE_CONF_CHECK_I(has_dsi_encoder);
12847
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012848 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12849 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12850 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12851 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12852 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12853 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012854
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012855 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12856 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12857 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12858 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12859 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12860 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012861
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012862 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012863 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012864 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012865 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012866 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012867 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012868
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012869 PIPE_CONF_CHECK_I(has_audio);
12870
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012871 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012872 DRM_MODE_FLAG_INTERLACE);
12873
Daniel Vetterbb760062013-06-06 14:55:52 +020012874 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012875 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012876 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012877 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012878 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012879 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012880 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012881 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012882 DRM_MODE_FLAG_NVSYNC);
12883 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012884
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012885 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012886 /* pfit ratios are autocomputed by the hw on gen4+ */
12887 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012888 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012889 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012890
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012891 if (!adjust) {
12892 PIPE_CONF_CHECK_I(pipe_src_w);
12893 PIPE_CONF_CHECK_I(pipe_src_h);
12894
12895 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12896 if (current_config->pch_pfit.enabled) {
12897 PIPE_CONF_CHECK_X(pch_pfit.pos);
12898 PIPE_CONF_CHECK_X(pch_pfit.size);
12899 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012900
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012901 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12902 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012903
Jesse Barnese59150d2014-01-07 13:30:45 -080012904 /* BDW+ don't expose a synchronous way to read the state */
12905 if (IS_HASWELL(dev))
12906 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012907
Ville Syrjälä282740f2013-09-04 18:30:03 +030012908 PIPE_CONF_CHECK_I(double_wide);
12909
Daniel Vetter26804af2014-06-25 22:01:55 +030012910 PIPE_CONF_CHECK_X(ddi_pll_sel);
12911
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012912 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012913 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012914 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012915 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12916 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012917 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012918 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012919 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12920 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12921 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012922
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012923 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12924 PIPE_CONF_CHECK_X(dsi_pll.div);
12925
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012926 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12927 PIPE_CONF_CHECK_I(pipe_bpp);
12928
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012929 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012930 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012931
Daniel Vetter66e985c2013-06-05 13:34:20 +020012932#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012933#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012934#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012935#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012936#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012937#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012938#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012939
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012940 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012941}
12942
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012943static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12944 const struct intel_crtc_state *pipe_config)
12945{
12946 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012947 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012948 &pipe_config->fdi_m_n);
12949 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12950
12951 /*
12952 * FDI already provided one idea for the dotclock.
12953 * Yell if the encoder disagrees.
12954 */
12955 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12956 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12957 fdi_dotclock, dotclock);
12958 }
12959}
12960
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012961static void verify_wm_state(struct drm_crtc *crtc,
12962 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012963{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012964 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012965 struct drm_i915_private *dev_priv = dev->dev_private;
12966 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012967 struct skl_ddb_entry *hw_entry, *sw_entry;
12968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12969 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012970 int plane;
12971
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012972 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012973 return;
12974
12975 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12976 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12977
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012978 /* planes */
12979 for_each_plane(dev_priv, pipe, plane) {
12980 hw_entry = &hw_ddb.plane[pipe][plane];
12981 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012982
12983 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12984 continue;
12985
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012986 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12987 "(expected (%u,%u), found (%u,%u))\n",
12988 pipe_name(pipe), plane + 1,
12989 sw_entry->start, sw_entry->end,
12990 hw_entry->start, hw_entry->end);
12991 }
12992
12993 /* cursor */
12994 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12995 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12996
12997 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012998 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12999 "(expected (%u,%u), found (%u,%u))\n",
13000 pipe_name(pipe),
13001 sw_entry->start, sw_entry->end,
13002 hw_entry->start, hw_entry->end);
13003 }
13004}
13005
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013006static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013007verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013008{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013009 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013010
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013011 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013012 struct drm_encoder *encoder = connector->encoder;
13013 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013014
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013015 if (state->crtc != crtc)
13016 continue;
13017
Daniel Vetter5a21b662016-05-24 17:13:53 +020013018 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013019
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013020 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013021 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013022 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013023}
13024
13025static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013026verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013027{
13028 struct intel_encoder *encoder;
13029 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013030
Damien Lespiaub2784e12014-08-05 11:29:37 +010013031 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013032 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013033 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013034
13035 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13036 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013037 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013038
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013039 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013040 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013041 continue;
13042 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013043
13044 I915_STATE_WARN(connector->base.state->crtc !=
13045 encoder->base.crtc,
13046 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013047 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013048
Rob Clarke2c719b2014-12-15 13:56:32 -050013049 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013050 "encoder's enabled state mismatch "
13051 "(expected %i, found %i)\n",
13052 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013053
13054 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013055 bool active;
13056
13057 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013058 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013059 "encoder detached but still enabled on pipe %c.\n",
13060 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013061 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013062 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013063}
13064
13065static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013066verify_crtc_state(struct drm_crtc *crtc,
13067 struct drm_crtc_state *old_crtc_state,
13068 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013069{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013070 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013071 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013072 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13074 struct intel_crtc_state *pipe_config, *sw_config;
13075 struct drm_atomic_state *old_state;
13076 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013077
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013078 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013079 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013080 pipe_config = to_intel_crtc_state(old_crtc_state);
13081 memset(pipe_config, 0, sizeof(*pipe_config));
13082 pipe_config->base.crtc = crtc;
13083 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013084
Ville Syrjälä78108b72016-05-27 20:59:19 +030013085 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013086
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013087 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013088
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013089 /* hw state is inconsistent with the pipe quirk */
13090 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13091 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13092 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013093
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013094 I915_STATE_WARN(new_crtc_state->active != active,
13095 "crtc active state doesn't match with hw state "
13096 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013097
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013098 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13099 "transitional active state does not match atomic hw state "
13100 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013101
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013102 for_each_encoder_on_crtc(dev, crtc, encoder) {
13103 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013104
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013105 active = encoder->get_hw_state(encoder, &pipe);
13106 I915_STATE_WARN(active != new_crtc_state->active,
13107 "[ENCODER:%i] active %i with crtc active %i\n",
13108 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013109
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013110 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13111 "Encoder connected to wrong pipe %c\n",
13112 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013113
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013114 if (active)
13115 encoder->get_config(encoder, pipe_config);
13116 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013117
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013118 if (!new_crtc_state->active)
13119 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013120
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013121 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013122
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013123 sw_config = to_intel_crtc_state(crtc->state);
13124 if (!intel_pipe_config_compare(dev, sw_config,
13125 pipe_config, false)) {
13126 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13127 intel_dump_pipe_config(intel_crtc, pipe_config,
13128 "[hw state]");
13129 intel_dump_pipe_config(intel_crtc, sw_config,
13130 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013131 }
13132}
13133
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013134static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013135verify_single_dpll_state(struct drm_i915_private *dev_priv,
13136 struct intel_shared_dpll *pll,
13137 struct drm_crtc *crtc,
13138 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013139{
13140 struct intel_dpll_hw_state dpll_hw_state;
13141 unsigned crtc_mask;
13142 bool active;
13143
13144 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13145
13146 DRM_DEBUG_KMS("%s\n", pll->name);
13147
13148 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13149
13150 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13151 I915_STATE_WARN(!pll->on && pll->active_mask,
13152 "pll in active use but not on in sw tracking\n");
13153 I915_STATE_WARN(pll->on && !pll->active_mask,
13154 "pll is on but not used by any active crtc\n");
13155 I915_STATE_WARN(pll->on != active,
13156 "pll on state mismatch (expected %i, found %i)\n",
13157 pll->on, active);
13158 }
13159
13160 if (!crtc) {
13161 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13162 "more active pll users than references: %x vs %x\n",
13163 pll->active_mask, pll->config.crtc_mask);
13164
13165 return;
13166 }
13167
13168 crtc_mask = 1 << drm_crtc_index(crtc);
13169
13170 if (new_state->active)
13171 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13172 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13173 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13174 else
13175 I915_STATE_WARN(pll->active_mask & crtc_mask,
13176 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13177 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13178
13179 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13180 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13181 crtc_mask, pll->config.crtc_mask);
13182
13183 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13184 &dpll_hw_state,
13185 sizeof(dpll_hw_state)),
13186 "pll hw state mismatch\n");
13187}
13188
13189static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013190verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13191 struct drm_crtc_state *old_crtc_state,
13192 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013193{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013194 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013195 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13196 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13197
13198 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013199 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013200
13201 if (old_state->shared_dpll &&
13202 old_state->shared_dpll != new_state->shared_dpll) {
13203 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13204 struct intel_shared_dpll *pll = old_state->shared_dpll;
13205
13206 I915_STATE_WARN(pll->active_mask & crtc_mask,
13207 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13208 pipe_name(drm_crtc_index(crtc)));
13209 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13210 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13211 pipe_name(drm_crtc_index(crtc)));
13212 }
13213}
13214
13215static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013216intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013217 struct drm_crtc_state *old_state,
13218 struct drm_crtc_state *new_state)
13219{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013220 if (!needs_modeset(new_state) &&
13221 !to_intel_crtc_state(new_state)->update_pipe)
13222 return;
13223
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013224 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013225 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013226 verify_crtc_state(crtc, old_state, new_state);
13227 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013228}
13229
13230static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013231verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013232{
13233 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013234 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013235
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013236 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013237 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013238}
Daniel Vetter53589012013-06-05 13:34:16 +020013239
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013240static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013241intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013242{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013243 verify_encoder_state(dev);
13244 verify_connector_state(dev, NULL);
13245 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013246}
13247
Ville Syrjälä80715b22014-05-15 20:23:23 +030013248static void update_scanline_offset(struct intel_crtc *crtc)
13249{
13250 struct drm_device *dev = crtc->base.dev;
13251
13252 /*
13253 * The scanline counter increments at the leading edge of hsync.
13254 *
13255 * On most platforms it starts counting from vtotal-1 on the
13256 * first active line. That means the scanline counter value is
13257 * always one less than what we would expect. Ie. just after
13258 * start of vblank, which also occurs at start of hsync (on the
13259 * last active line), the scanline counter will read vblank_start-1.
13260 *
13261 * On gen2 the scanline counter starts counting from 1 instead
13262 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13263 * to keep the value positive), instead of adding one.
13264 *
13265 * On HSW+ the behaviour of the scanline counter depends on the output
13266 * type. For DP ports it behaves like most other platforms, but on HDMI
13267 * there's an extra 1 line difference. So we need to add two instead of
13268 * one to the value.
13269 */
13270 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013271 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013272 int vtotal;
13273
Ville Syrjälä124abe02015-09-08 13:40:45 +030013274 vtotal = adjusted_mode->crtc_vtotal;
13275 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013276 vtotal /= 2;
13277
13278 crtc->scanline_offset = vtotal - 1;
13279 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013280 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013281 crtc->scanline_offset = 2;
13282 } else
13283 crtc->scanline_offset = 1;
13284}
13285
Maarten Lankhorstad421372015-06-15 12:33:42 +020013286static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013287{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013288 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013289 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013290 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013291 struct drm_crtc *crtc;
13292 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013293 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013294
13295 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013296 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013297
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013298 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013300 struct intel_shared_dpll *old_dpll =
13301 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013302
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013303 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013304 continue;
13305
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013306 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013307
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013308 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013309 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013310
Maarten Lankhorstad421372015-06-15 12:33:42 +020013311 if (!shared_dpll)
13312 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13313
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013314 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013315 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013316}
13317
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013318/*
13319 * This implements the workaround described in the "notes" section of the mode
13320 * set sequence documentation. When going from no pipes or single pipe to
13321 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13322 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13323 */
13324static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13325{
13326 struct drm_crtc_state *crtc_state;
13327 struct intel_crtc *intel_crtc;
13328 struct drm_crtc *crtc;
13329 struct intel_crtc_state *first_crtc_state = NULL;
13330 struct intel_crtc_state *other_crtc_state = NULL;
13331 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13332 int i;
13333
13334 /* look at all crtc's that are going to be enabled in during modeset */
13335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13336 intel_crtc = to_intel_crtc(crtc);
13337
13338 if (!crtc_state->active || !needs_modeset(crtc_state))
13339 continue;
13340
13341 if (first_crtc_state) {
13342 other_crtc_state = to_intel_crtc_state(crtc_state);
13343 break;
13344 } else {
13345 first_crtc_state = to_intel_crtc_state(crtc_state);
13346 first_pipe = intel_crtc->pipe;
13347 }
13348 }
13349
13350 /* No workaround needed? */
13351 if (!first_crtc_state)
13352 return 0;
13353
13354 /* w/a possibly needed, check how many crtc's are already enabled. */
13355 for_each_intel_crtc(state->dev, intel_crtc) {
13356 struct intel_crtc_state *pipe_config;
13357
13358 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13359 if (IS_ERR(pipe_config))
13360 return PTR_ERR(pipe_config);
13361
13362 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13363
13364 if (!pipe_config->base.active ||
13365 needs_modeset(&pipe_config->base))
13366 continue;
13367
13368 /* 2 or more enabled crtcs means no need for w/a */
13369 if (enabled_pipe != INVALID_PIPE)
13370 return 0;
13371
13372 enabled_pipe = intel_crtc->pipe;
13373 }
13374
13375 if (enabled_pipe != INVALID_PIPE)
13376 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13377 else if (other_crtc_state)
13378 other_crtc_state->hsw_workaround_pipe = first_pipe;
13379
13380 return 0;
13381}
13382
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013383static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13384{
13385 struct drm_crtc *crtc;
13386 struct drm_crtc_state *crtc_state;
13387 int ret = 0;
13388
13389 /* add all active pipes to the state */
13390 for_each_crtc(state->dev, crtc) {
13391 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13392 if (IS_ERR(crtc_state))
13393 return PTR_ERR(crtc_state);
13394
13395 if (!crtc_state->active || needs_modeset(crtc_state))
13396 continue;
13397
13398 crtc_state->mode_changed = true;
13399
13400 ret = drm_atomic_add_affected_connectors(state, crtc);
13401 if (ret)
13402 break;
13403
13404 ret = drm_atomic_add_affected_planes(state, crtc);
13405 if (ret)
13406 break;
13407 }
13408
13409 return ret;
13410}
13411
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013412static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013413{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013414 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13415 struct drm_i915_private *dev_priv = state->dev->dev_private;
13416 struct drm_crtc *crtc;
13417 struct drm_crtc_state *crtc_state;
13418 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013419
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013420 if (!check_digital_port_conflicts(state)) {
13421 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13422 return -EINVAL;
13423 }
13424
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013425 intel_state->modeset = true;
13426 intel_state->active_crtcs = dev_priv->active_crtcs;
13427
13428 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13429 if (crtc_state->active)
13430 intel_state->active_crtcs |= 1 << i;
13431 else
13432 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013433
13434 if (crtc_state->active != crtc->state->active)
13435 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013436 }
13437
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013438 /*
13439 * See if the config requires any additional preparation, e.g.
13440 * to adjust global state with pipes off. We need to do this
13441 * here so we can get the modeset_pipe updated config for the new
13442 * mode set on this crtc. For other crtcs we need to use the
13443 * adjusted_mode bits in the crtc directly.
13444 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013445 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013446 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013447 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013448 if (!intel_state->cdclk_pll_vco)
13449 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013450
Clint Taylorc89e39f2016-05-13 23:41:21 +030013451 ret = dev_priv->display.modeset_calc_cdclk(state);
13452 if (ret < 0)
13453 return ret;
13454
13455 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013456 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013457 ret = intel_modeset_all_pipes(state);
13458
13459 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013460 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013461
13462 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13463 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013464 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013465 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013466
Maarten Lankhorstad421372015-06-15 12:33:42 +020013467 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013468
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013469 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013470 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013471
Maarten Lankhorstad421372015-06-15 12:33:42 +020013472 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013473}
13474
Matt Roperaa363132015-09-24 15:53:18 -070013475/*
13476 * Handle calculation of various watermark data at the end of the atomic check
13477 * phase. The code here should be run after the per-crtc and per-plane 'check'
13478 * handlers to ensure that all derived state has been updated.
13479 */
Matt Roper55994c22016-05-12 07:06:08 -070013480static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013481{
13482 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013483 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013484
13485 /* Is there platform-specific watermark information to calculate? */
13486 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013487 return dev_priv->display.compute_global_watermarks(state);
13488
13489 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013490}
13491
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013492/**
13493 * intel_atomic_check - validate state object
13494 * @dev: drm device
13495 * @state: state to validate
13496 */
13497static int intel_atomic_check(struct drm_device *dev,
13498 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013499{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013500 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013501 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013502 struct drm_crtc *crtc;
13503 struct drm_crtc_state *crtc_state;
13504 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013505 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013506
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013507 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013508 if (ret)
13509 return ret;
13510
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013511 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013512 struct intel_crtc_state *pipe_config =
13513 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013514
13515 /* Catch I915_MODE_FLAG_INHERITED */
13516 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13517 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013518
Daniel Vetter26495482015-07-15 14:15:52 +020013519 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013520 continue;
13521
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013522 if (!crtc_state->enable) {
13523 any_ms = true;
13524 continue;
13525 }
13526
Daniel Vetter26495482015-07-15 14:15:52 +020013527 /* FIXME: For only active_changed we shouldn't need to do any
13528 * state recomputation at all. */
13529
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013530 ret = drm_atomic_add_affected_connectors(state, crtc);
13531 if (ret)
13532 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013533
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013534 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013535 if (ret) {
13536 intel_dump_pipe_config(to_intel_crtc(crtc),
13537 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013538 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013539 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013540
Jani Nikula73831232015-11-19 10:26:30 +020013541 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013542 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013543 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013544 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013545 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013546 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013547 }
13548
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013549 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013550 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013551
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013552 ret = drm_atomic_add_affected_planes(state, crtc);
13553 if (ret)
13554 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013555
Daniel Vetter26495482015-07-15 14:15:52 +020013556 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13557 needs_modeset(crtc_state) ?
13558 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013559 }
13560
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013561 if (any_ms) {
13562 ret = intel_modeset_checks(state);
13563
13564 if (ret)
13565 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013566 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013567 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013568
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013569 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013570 if (ret)
13571 return ret;
13572
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013573 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013574 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013575}
13576
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013577static int intel_atomic_prepare_commit(struct drm_device *dev,
13578 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013579 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013580{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013581 struct drm_i915_private *dev_priv = dev->dev_private;
13582 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013583 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013584 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013585 struct drm_crtc *crtc;
13586 int i, ret;
13587
Daniel Vetter5a21b662016-05-24 17:13:53 +020013588 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13589 if (state->legacy_cursor_update)
13590 continue;
13591
13592 ret = intel_crtc_wait_for_pending_flips(crtc);
13593 if (ret)
13594 return ret;
13595
13596 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13597 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013598 }
13599
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013600 ret = mutex_lock_interruptible(&dev->struct_mutex);
13601 if (ret)
13602 return ret;
13603
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013604 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013605 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013606
Dave Airlie21daaee2016-05-05 09:56:30 +100013607 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013608 for_each_plane_in_state(state, plane, plane_state, i) {
13609 struct intel_plane_state *intel_plane_state =
13610 to_intel_plane_state(plane_state);
13611
13612 if (!intel_plane_state->wait_req)
13613 continue;
13614
13615 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013616 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013617 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013618 /* Any hang should be swallowed by the wait */
13619 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013620 mutex_lock(&dev->struct_mutex);
13621 drm_atomic_helper_cleanup_planes(dev, state);
13622 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013623 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013624 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013625 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013626 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013627
13628 return ret;
13629}
13630
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013631u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13632{
13633 struct drm_device *dev = crtc->base.dev;
13634
13635 if (!dev->max_vblank_count)
13636 return drm_accurate_vblank_count(&crtc->base);
13637
13638 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13639}
13640
Daniel Vetter5a21b662016-05-24 17:13:53 +020013641static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13642 struct drm_i915_private *dev_priv,
13643 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013644{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013645 unsigned last_vblank_count[I915_MAX_PIPES];
13646 enum pipe pipe;
13647 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013648
Daniel Vetter5a21b662016-05-24 17:13:53 +020013649 if (!crtc_mask)
13650 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013651
Daniel Vetter5a21b662016-05-24 17:13:53 +020013652 for_each_pipe(dev_priv, pipe) {
13653 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013654
Daniel Vetter5a21b662016-05-24 17:13:53 +020013655 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013656 continue;
13657
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013658 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013659 if (WARN_ON(ret != 0)) {
13660 crtc_mask &= ~(1 << pipe);
13661 continue;
13662 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013663
Daniel Vetter5a21b662016-05-24 17:13:53 +020013664 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13665 }
13666
13667 for_each_pipe(dev_priv, pipe) {
13668 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13669 long lret;
13670
13671 if (!((1 << pipe) & crtc_mask))
13672 continue;
13673
13674 lret = wait_event_timeout(dev->vblank[pipe].queue,
13675 last_vblank_count[pipe] !=
13676 drm_crtc_vblank_count(crtc),
13677 msecs_to_jiffies(50));
13678
13679 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13680
13681 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013682 }
13683}
13684
Daniel Vetter5a21b662016-05-24 17:13:53 +020013685static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013686{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013687 /* fb updated, need to unpin old fb */
13688 if (crtc_state->fb_changed)
13689 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013690
Daniel Vetter5a21b662016-05-24 17:13:53 +020013691 /* wm changes, need vblank before final wm's */
13692 if (crtc_state->update_wm_post)
13693 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013694
Daniel Vetter5a21b662016-05-24 17:13:53 +020013695 /*
13696 * cxsr is re-enabled after vblank.
13697 * This is already handled by crtc_state->update_wm_post,
13698 * but added for clarity.
13699 */
13700 if (crtc_state->disable_cxsr)
13701 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013702
Daniel Vetter5a21b662016-05-24 17:13:53 +020013703 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013704}
13705
Daniel Vetter94f05022016-06-14 18:01:00 +020013706static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013707{
Daniel Vetter94f05022016-06-14 18:01:00 +020013708 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013709 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013710 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013711 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013712 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013713 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013714 struct drm_plane *plane;
13715 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013716 bool hw_check = intel_state->modeset;
13717 unsigned long put_domains[I915_MAX_PIPES] = {};
13718 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013719 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013720
Daniel Vetter94f05022016-06-14 18:01:00 +020013721 for_each_plane_in_state(state, plane, plane_state, i) {
13722 struct intel_plane_state *intel_plane_state =
13723 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013724
Daniel Vetter94f05022016-06-14 18:01:00 +020013725 if (!intel_plane_state->wait_req)
13726 continue;
13727
13728 ret = __i915_wait_request(intel_plane_state->wait_req,
13729 true, NULL, NULL);
13730 /* EIO should be eaten, and we can't get interrupted in the
13731 * worker, and blocking commits have waited already. */
13732 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013733 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013734
Daniel Vetterea0000f2016-06-13 16:13:46 +020013735 drm_atomic_helper_wait_for_dependencies(state);
13736
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013737 if (intel_state->modeset) {
13738 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13739 sizeof(intel_state->min_pixclk));
13740 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013741 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013742
13743 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013744 }
13745
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013746 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13748
Daniel Vetter5a21b662016-05-24 17:13:53 +020013749 if (needs_modeset(crtc->state) ||
13750 to_intel_crtc_state(crtc->state)->update_pipe) {
13751 hw_check = true;
13752
13753 put_domains[to_intel_crtc(crtc)->pipe] =
13754 modeset_get_crtc_power_domains(crtc,
13755 to_intel_crtc_state(crtc->state));
13756 }
13757
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013758 if (!needs_modeset(crtc->state))
13759 continue;
13760
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013761 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013762
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013763 if (old_crtc_state->active) {
13764 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013765 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013766 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013767 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013768 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013769
13770 /*
13771 * Underruns don't always raise
13772 * interrupts, so check manually.
13773 */
13774 intel_check_cpu_fifo_underruns(dev_priv);
13775 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013776
13777 if (!crtc->state->active)
13778 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013779 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013780 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013781
Daniel Vetterea9d7582012-07-10 10:42:52 +020013782 /* Only after disabling all output pipelines that will be changed can we
13783 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013784 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013785
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013786 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013787 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013788
13789 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013790 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013791 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013792 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013793
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013794 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013795 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013796
Daniel Vettera6778b32012-07-02 09:56:42 +020013797 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013798 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13800 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013801 struct intel_crtc_state *pipe_config =
13802 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013803
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013804 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013805 update_scanline_offset(to_intel_crtc(crtc));
13806 dev_priv->display.crtc_enable(crtc);
13807 }
13808
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013809 /* Complete events for now disable pipes here. */
13810 if (modeset && !crtc->state->active && crtc->state->event) {
13811 spin_lock_irq(&dev->event_lock);
13812 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13813 spin_unlock_irq(&dev->event_lock);
13814
13815 crtc->state->event = NULL;
13816 }
13817
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013818 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013819 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013820
Daniel Vetter5a21b662016-05-24 17:13:53 +020013821 if (crtc->state->active &&
13822 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013823 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013824
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013825 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013826 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013827
Daniel Vetter5a21b662016-05-24 17:13:53 +020013828 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13829 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013830 }
13831
Daniel Vetter94f05022016-06-14 18:01:00 +020013832 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13833 * already, but still need the state for the delayed optimization. To
13834 * fix this:
13835 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13836 * - schedule that vblank worker _before_ calling hw_done
13837 * - at the start of commit_tail, cancel it _synchrously
13838 * - switch over to the vblank wait helper in the core after that since
13839 * we don't need out special handling any more.
13840 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013841 if (!state->legacy_cursor_update)
13842 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13843
13844 /*
13845 * Now that the vblank has passed, we can go ahead and program the
13846 * optimal watermarks on platforms that need two-step watermark
13847 * programming.
13848 *
13849 * TODO: Move this (and other cleanup) to an async worker eventually.
13850 */
13851 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13852 intel_cstate = to_intel_crtc_state(crtc->state);
13853
13854 if (dev_priv->display.optimize_watermarks)
13855 dev_priv->display.optimize_watermarks(intel_cstate);
13856 }
13857
13858 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13859 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13860
13861 if (put_domains[i])
13862 modeset_put_power_domains(dev_priv, put_domains[i]);
13863
13864 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13865 }
13866
Daniel Vetter94f05022016-06-14 18:01:00 +020013867 drm_atomic_helper_commit_hw_done(state);
13868
Daniel Vetter5a21b662016-05-24 17:13:53 +020013869 if (intel_state->modeset)
13870 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13871
13872 mutex_lock(&dev->struct_mutex);
13873 drm_atomic_helper_cleanup_planes(dev, state);
13874 mutex_unlock(&dev->struct_mutex);
13875
Daniel Vetterea0000f2016-06-13 16:13:46 +020013876 drm_atomic_helper_commit_cleanup_done(state);
13877
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013878 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013879
Mika Kuoppala75714942015-12-16 09:26:48 +020013880 /* As one of the primary mmio accessors, KMS has a high likelihood
13881 * of triggering bugs in unclaimed access. After we finish
13882 * modesetting, see if an error has been flagged, and if so
13883 * enable debugging for the next modeset - and hope we catch
13884 * the culprit.
13885 *
13886 * XXX note that we assume display power is on at this point.
13887 * This might hold true now but we need to add pm helper to check
13888 * unclaimed only when the hardware is on, as atomic commits
13889 * can happen also when the device is completely off.
13890 */
13891 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013892}
13893
13894static void intel_atomic_commit_work(struct work_struct *work)
13895{
13896 struct drm_atomic_state *state = container_of(work,
13897 struct drm_atomic_state,
13898 commit_work);
13899 intel_atomic_commit_tail(state);
13900}
13901
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013902static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13903{
13904 struct drm_plane_state *old_plane_state;
13905 struct drm_plane *plane;
13906 struct drm_i915_gem_object *obj, *old_obj;
13907 struct intel_plane *intel_plane;
13908 int i;
13909
13910 mutex_lock(&state->dev->struct_mutex);
13911 for_each_plane_in_state(state, plane, old_plane_state, i) {
13912 obj = intel_fb_obj(plane->state->fb);
13913 old_obj = intel_fb_obj(old_plane_state->fb);
13914 intel_plane = to_intel_plane(plane);
13915
13916 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13917 }
13918 mutex_unlock(&state->dev->struct_mutex);
13919}
13920
Daniel Vetter94f05022016-06-14 18:01:00 +020013921/**
13922 * intel_atomic_commit - commit validated state object
13923 * @dev: DRM device
13924 * @state: the top-level driver state object
13925 * @nonblock: nonblocking commit
13926 *
13927 * This function commits a top-level state object that has been validated
13928 * with drm_atomic_helper_check().
13929 *
13930 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13931 * nonblocking commits are only safe for pure plane updates. Everything else
13932 * should work though.
13933 *
13934 * RETURNS
13935 * Zero for success or -errno.
13936 */
13937static int intel_atomic_commit(struct drm_device *dev,
13938 struct drm_atomic_state *state,
13939 bool nonblock)
13940{
13941 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13942 struct drm_i915_private *dev_priv = dev->dev_private;
13943 int ret = 0;
13944
13945 if (intel_state->modeset && nonblock) {
13946 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13947 return -EINVAL;
13948 }
13949
13950 ret = drm_atomic_helper_setup_commit(state, nonblock);
13951 if (ret)
13952 return ret;
13953
13954 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13955
13956 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13957 if (ret) {
13958 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13959 return ret;
13960 }
13961
13962 drm_atomic_helper_swap_state(state, true);
13963 dev_priv->wm.distrust_bios_wm = false;
13964 dev_priv->wm.skl_results = intel_state->wm_results;
13965 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013966 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013967
13968 if (nonblock)
13969 queue_work(system_unbound_wq, &state->commit_work);
13970 else
13971 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013972
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013973 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013974}
13975
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013976void intel_crtc_restore_mode(struct drm_crtc *crtc)
13977{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013978 struct drm_device *dev = crtc->dev;
13979 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013980 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013981 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013982
13983 state = drm_atomic_state_alloc(dev);
13984 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013985 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13986 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013987 return;
13988 }
13989
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013990 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013991
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013992retry:
13993 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13994 ret = PTR_ERR_OR_ZERO(crtc_state);
13995 if (!ret) {
13996 if (!crtc_state->active)
13997 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013998
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013999 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014000 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014001 }
14002
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014003 if (ret == -EDEADLK) {
14004 drm_atomic_state_clear(state);
14005 drm_modeset_backoff(state->acquire_ctx);
14006 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030014007 }
14008
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014009 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014010out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014011 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014012}
14013
Daniel Vetter25c5b262012-07-08 22:08:04 +020014014#undef for_each_intel_crtc_masked
14015
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014016static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014017 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014018 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014019 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014020 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014021 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014022 .atomic_duplicate_state = intel_crtc_duplicate_state,
14023 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014024};
14025
Matt Roper6beb8c232014-12-01 15:40:14 -080014026/**
14027 * intel_prepare_plane_fb - Prepare fb for usage on plane
14028 * @plane: drm plane to prepare for
14029 * @fb: framebuffer to prepare for presentation
14030 *
14031 * Prepares a framebuffer for usage on a display plane. Generally this
14032 * involves pinning the underlying object and updating the frontbuffer tracking
14033 * bits. Some older platforms need special physical address handling for
14034 * cursor planes.
14035 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014036 * Must be called with struct_mutex held.
14037 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014038 * Returns 0 on success, negative error code on failure.
14039 */
14040int
14041intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014042 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014043{
14044 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014045 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014046 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014047 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010014048 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080014049 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014050
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014051 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014052 return 0;
14053
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014054 if (old_obj) {
14055 struct drm_crtc_state *crtc_state =
14056 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14057
14058 /* Big Hammer, we also need to ensure that any pending
14059 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14060 * current scanout is retired before unpinning the old
14061 * framebuffer. Note that we rely on userspace rendering
14062 * into the buffer attached to the pipe they are waiting
14063 * on. If not, userspace generates a GPU hang with IPEHR
14064 * point to the MI_WAIT_FOR_EVENT.
14065 *
14066 * This should only fail upon a hung GPU, in which case we
14067 * can safely continue.
14068 */
14069 if (needs_modeset(crtc_state))
14070 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014071 if (ret) {
14072 /* GPU hangs should have been swallowed by the wait */
14073 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014074 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014075 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014076 }
14077
Chris Wilsonc37efb92016-06-17 08:28:47 +010014078 if (!obj)
14079 return 0;
14080
Daniel Vetter5a21b662016-05-24 17:13:53 +020014081 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014082 resv = i915_gem_object_get_dmabuf_resv(obj);
14083 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014084 long lret;
14085
Chris Wilsonc37efb92016-06-17 08:28:47 +010014086 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014087 MAX_SCHEDULE_TIMEOUT);
14088 if (lret == -ERESTARTSYS)
14089 return lret;
14090
14091 WARN(lret < 0, "waiting returns %li\n", lret);
14092 }
14093
Chris Wilsonc37efb92016-06-17 08:28:47 +010014094 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014095 INTEL_INFO(dev)->cursor_needs_physical) {
14096 int align = IS_I830(dev) ? 16 * 1024 : 256;
14097 ret = i915_gem_object_attach_phys(obj, align);
14098 if (ret)
14099 DRM_DEBUG_KMS("failed to attach phys object\n");
14100 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014101 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014102 }
14103
Chris Wilsonc37efb92016-06-17 08:28:47 +010014104 if (ret == 0) {
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014105 struct intel_plane_state *plane_state =
14106 to_intel_plane_state(new_state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014107
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014108 i915_gem_request_assign(&plane_state->wait_req,
14109 obj->last_write_req);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014110 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014111
Matt Roper6beb8c232014-12-01 15:40:14 -080014112 return ret;
14113}
14114
Matt Roper38f3ce32014-12-02 07:45:25 -080014115/**
14116 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14117 * @plane: drm plane to clean up for
14118 * @fb: old framebuffer that was on plane
14119 *
14120 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014121 *
14122 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014123 */
14124void
14125intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014126 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014127{
14128 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014129 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014130 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14131 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014132
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014133 old_intel_state = to_intel_plane_state(old_state);
14134
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014135 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014136 return;
14137
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014138 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14139 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014140 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014141
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014142 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014143}
14144
Chandra Konduru6156a452015-04-27 13:48:39 -070014145int
14146skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14147{
14148 int max_scale;
14149 struct drm_device *dev;
14150 struct drm_i915_private *dev_priv;
14151 int crtc_clock, cdclk;
14152
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014153 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014154 return DRM_PLANE_HELPER_NO_SCALING;
14155
14156 dev = intel_crtc->base.dev;
14157 dev_priv = dev->dev_private;
14158 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014159 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014160
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014161 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014162 return DRM_PLANE_HELPER_NO_SCALING;
14163
14164 /*
14165 * skl max scale is lower of:
14166 * close to 3 but not 3, -1 is for that purpose
14167 * or
14168 * cdclk/crtc_clock
14169 */
14170 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14171
14172 return max_scale;
14173}
14174
Matt Roper465c1202014-05-29 08:06:54 -070014175static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014176intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014177 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014178 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014179{
Matt Roper2b875c22014-12-01 15:40:13 -080014180 struct drm_crtc *crtc = state->base.crtc;
14181 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014182 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014183 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14184 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014185
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014186 if (INTEL_INFO(plane->dev)->gen >= 9) {
14187 /* use scaler when colorkey is not required */
14188 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14189 min_scale = 1;
14190 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14191 }
Sonika Jindald8106362015-04-10 14:37:28 +053014192 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014193 }
Sonika Jindald8106362015-04-10 14:37:28 +053014194
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014195 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14196 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014197 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014198 min_scale, max_scale,
14199 can_position, true,
14200 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014201}
14202
Daniel Vetter5a21b662016-05-24 17:13:53 +020014203static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14204 struct drm_crtc_state *old_crtc_state)
14205{
14206 struct drm_device *dev = crtc->dev;
14207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14208 struct intel_crtc_state *old_intel_state =
14209 to_intel_crtc_state(old_crtc_state);
14210 bool modeset = needs_modeset(crtc->state);
14211
14212 /* Perform vblank evasion around commit operation */
14213 intel_pipe_update_start(intel_crtc);
14214
14215 if (modeset)
14216 return;
14217
14218 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14219 intel_color_set_csc(crtc->state);
14220 intel_color_load_luts(crtc->state);
14221 }
14222
14223 if (to_intel_crtc_state(crtc->state)->update_pipe)
14224 intel_update_pipe_config(intel_crtc, old_intel_state);
14225 else if (INTEL_INFO(dev)->gen >= 9)
14226 skl_detach_scalers(intel_crtc);
14227}
14228
14229static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14230 struct drm_crtc_state *old_crtc_state)
14231{
14232 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14233
14234 intel_pipe_update_end(intel_crtc, NULL);
14235}
14236
Matt Ropercf4c7c12014-12-04 10:27:42 -080014237/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014238 * intel_plane_destroy - destroy a plane
14239 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014240 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014241 * Common destruction function for all types of planes (primary, cursor,
14242 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014243 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014244void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014245{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014246 if (!plane)
14247 return;
14248
Matt Roper465c1202014-05-29 08:06:54 -070014249 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014250 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014251}
14252
Matt Roper65a3fea2015-01-21 16:35:42 -080014253const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014254 .update_plane = drm_atomic_helper_update_plane,
14255 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014256 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014257 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014258 .atomic_get_property = intel_plane_atomic_get_property,
14259 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014260 .atomic_duplicate_state = intel_plane_duplicate_state,
14261 .atomic_destroy_state = intel_plane_destroy_state,
14262
Matt Roper465c1202014-05-29 08:06:54 -070014263};
14264
14265static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14266 int pipe)
14267{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014268 struct intel_plane *primary = NULL;
14269 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014270 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014271 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014272 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014273
14274 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014275 if (!primary)
14276 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014277
Matt Roper8e7d6882015-01-21 16:35:41 -080014278 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014279 if (!state)
14280 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014281 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014282
Matt Roper465c1202014-05-29 08:06:54 -070014283 primary->can_scale = false;
14284 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014285 if (INTEL_INFO(dev)->gen >= 9) {
14286 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014287 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014288 }
Matt Roper465c1202014-05-29 08:06:54 -070014289 primary->pipe = pipe;
14290 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014291 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014292 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014293 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14294 primary->plane = !pipe;
14295
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014296 if (INTEL_INFO(dev)->gen >= 9) {
14297 intel_primary_formats = skl_primary_formats;
14298 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014299
14300 primary->update_plane = skylake_update_primary_plane;
14301 primary->disable_plane = skylake_disable_primary_plane;
14302 } else if (HAS_PCH_SPLIT(dev)) {
14303 intel_primary_formats = i965_primary_formats;
14304 num_formats = ARRAY_SIZE(i965_primary_formats);
14305
14306 primary->update_plane = ironlake_update_primary_plane;
14307 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014308 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014309 intel_primary_formats = i965_primary_formats;
14310 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014311
14312 primary->update_plane = i9xx_update_primary_plane;
14313 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014314 } else {
14315 intel_primary_formats = i8xx_primary_formats;
14316 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014317
14318 primary->update_plane = i9xx_update_primary_plane;
14319 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014320 }
14321
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014322 if (INTEL_INFO(dev)->gen >= 9)
14323 ret = drm_universal_plane_init(dev, &primary->base, 0,
14324 &intel_plane_funcs,
14325 intel_primary_formats, num_formats,
14326 DRM_PLANE_TYPE_PRIMARY,
14327 "plane 1%c", pipe_name(pipe));
14328 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14329 ret = drm_universal_plane_init(dev, &primary->base, 0,
14330 &intel_plane_funcs,
14331 intel_primary_formats, num_formats,
14332 DRM_PLANE_TYPE_PRIMARY,
14333 "primary %c", pipe_name(pipe));
14334 else
14335 ret = drm_universal_plane_init(dev, &primary->base, 0,
14336 &intel_plane_funcs,
14337 intel_primary_formats, num_formats,
14338 DRM_PLANE_TYPE_PRIMARY,
14339 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014340 if (ret)
14341 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014342
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014343 if (INTEL_INFO(dev)->gen >= 4)
14344 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014345
Matt Roperea2c67b2014-12-23 10:41:52 -080014346 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14347
Matt Roper465c1202014-05-29 08:06:54 -070014348 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014349
14350fail:
14351 kfree(state);
14352 kfree(primary);
14353
14354 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014355}
14356
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014357void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14358{
14359 if (!dev->mode_config.rotation_property) {
14360 unsigned long flags = BIT(DRM_ROTATE_0) |
14361 BIT(DRM_ROTATE_180);
14362
14363 if (INTEL_INFO(dev)->gen >= 9)
14364 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14365
14366 dev->mode_config.rotation_property =
14367 drm_mode_create_rotation_property(dev, flags);
14368 }
14369 if (dev->mode_config.rotation_property)
14370 drm_object_attach_property(&plane->base.base,
14371 dev->mode_config.rotation_property,
14372 plane->base.state->rotation);
14373}
14374
Matt Roper3d7d6512014-06-10 08:28:13 -070014375static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014376intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014377 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014378 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014379{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014380 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014381 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014382 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014383 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014384 unsigned stride;
14385 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014386
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014387 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14388 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014389 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014390 DRM_PLANE_HELPER_NO_SCALING,
14391 DRM_PLANE_HELPER_NO_SCALING,
14392 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014393 if (ret)
14394 return ret;
14395
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014396 /* if we want to turn off the cursor ignore width and height */
14397 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014398 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014399
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014400 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014401 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014402 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14403 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014404 return -EINVAL;
14405 }
14406
Matt Roperea2c67b2014-12-23 10:41:52 -080014407 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14408 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014409 DRM_DEBUG_KMS("buffer is too small\n");
14410 return -ENOMEM;
14411 }
14412
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014413 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014414 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014415 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014416 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014417
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014418 /*
14419 * There's something wrong with the cursor on CHV pipe C.
14420 * If it straddles the left edge of the screen then
14421 * moving it away from the edge or disabling it often
14422 * results in a pipe underrun, and often that can lead to
14423 * dead pipe (constant underrun reported, and it scans
14424 * out just a solid color). To recover from that, the
14425 * display power well must be turned off and on again.
14426 * Refuse the put the cursor into that compromised position.
14427 */
14428 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14429 state->visible && state->base.crtc_x < 0) {
14430 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14431 return -EINVAL;
14432 }
14433
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014434 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014435}
14436
Matt Roperf4a2cf22014-12-01 15:40:12 -080014437static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014438intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014439 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014440{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14442
14443 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014444 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014445}
14446
14447static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014448intel_update_cursor_plane(struct drm_plane *plane,
14449 const struct intel_crtc_state *crtc_state,
14450 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014451{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014452 struct drm_crtc *crtc = crtc_state->base.crtc;
14453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014454 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014455 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014456 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014457
Matt Roperf4a2cf22014-12-01 15:40:12 -080014458 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014459 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014460 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014461 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014462 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014463 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014464
Gustavo Padovana912f122014-12-01 15:40:10 -080014465 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014466 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014467}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014468
Matt Roper3d7d6512014-06-10 08:28:13 -070014469static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14470 int pipe)
14471{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014472 struct intel_plane *cursor = NULL;
14473 struct intel_plane_state *state = NULL;
14474 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014475
14476 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014477 if (!cursor)
14478 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014479
Matt Roper8e7d6882015-01-21 16:35:41 -080014480 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014481 if (!state)
14482 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014483 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014484
Matt Roper3d7d6512014-06-10 08:28:13 -070014485 cursor->can_scale = false;
14486 cursor->max_downscale = 1;
14487 cursor->pipe = pipe;
14488 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014489 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014490 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014491 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014492 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014493
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014494 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14495 &intel_plane_funcs,
14496 intel_cursor_formats,
14497 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014498 DRM_PLANE_TYPE_CURSOR,
14499 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014500 if (ret)
14501 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014502
14503 if (INTEL_INFO(dev)->gen >= 4) {
14504 if (!dev->mode_config.rotation_property)
14505 dev->mode_config.rotation_property =
14506 drm_mode_create_rotation_property(dev,
14507 BIT(DRM_ROTATE_0) |
14508 BIT(DRM_ROTATE_180));
14509 if (dev->mode_config.rotation_property)
14510 drm_object_attach_property(&cursor->base.base,
14511 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014512 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014513 }
14514
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014515 if (INTEL_INFO(dev)->gen >=9)
14516 state->scaler_id = -1;
14517
Matt Roperea2c67b2014-12-23 10:41:52 -080014518 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14519
Matt Roper3d7d6512014-06-10 08:28:13 -070014520 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014521
14522fail:
14523 kfree(state);
14524 kfree(cursor);
14525
14526 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014527}
14528
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014529static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14530 struct intel_crtc_state *crtc_state)
14531{
14532 int i;
14533 struct intel_scaler *intel_scaler;
14534 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14535
14536 for (i = 0; i < intel_crtc->num_scalers; i++) {
14537 intel_scaler = &scaler_state->scalers[i];
14538 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014539 intel_scaler->mode = PS_SCALER_MODE_DYN;
14540 }
14541
14542 scaler_state->scaler_id = -1;
14543}
14544
Hannes Ederb358d0a2008-12-18 21:18:47 +010014545static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014546{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014547 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014548 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014549 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014550 struct drm_plane *primary = NULL;
14551 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014552 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014553
Daniel Vetter955382f2013-09-19 14:05:45 +020014554 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014555 if (intel_crtc == NULL)
14556 return;
14557
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014558 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14559 if (!crtc_state)
14560 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014561 intel_crtc->config = crtc_state;
14562 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014563 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014564
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014565 /* initialize shared scalers */
14566 if (INTEL_INFO(dev)->gen >= 9) {
14567 if (pipe == PIPE_C)
14568 intel_crtc->num_scalers = 1;
14569 else
14570 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14571
14572 skl_init_scalers(dev, intel_crtc, crtc_state);
14573 }
14574
Matt Roper465c1202014-05-29 08:06:54 -070014575 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014576 if (!primary)
14577 goto fail;
14578
14579 cursor = intel_cursor_plane_create(dev, pipe);
14580 if (!cursor)
14581 goto fail;
14582
Matt Roper465c1202014-05-29 08:06:54 -070014583 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014584 cursor, &intel_crtc_funcs,
14585 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014586 if (ret)
14587 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014588
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014589 /*
14590 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014591 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014592 */
Jesse Barnes80824002009-09-10 15:28:06 -070014593 intel_crtc->pipe = pipe;
14594 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014595 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014596 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014597 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014598 }
14599
Chris Wilson4b0e3332014-05-30 16:35:26 +030014600 intel_crtc->cursor_base = ~0;
14601 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014602 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014603
Ville Syrjälä852eb002015-06-24 22:00:07 +030014604 intel_crtc->wm.cxsr_allowed = true;
14605
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014606 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14607 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14608 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14609 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14610
Jesse Barnes79e53942008-11-07 14:24:08 -080014611 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014612
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014613 intel_color_init(&intel_crtc->base);
14614
Daniel Vetter87b6b102014-05-15 15:33:46 +020014615 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014616 return;
14617
14618fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014619 intel_plane_destroy(primary);
14620 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014621 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014622 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014623}
14624
Jesse Barnes752aa882013-10-31 18:55:49 +020014625enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14626{
14627 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014628 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014629
Rob Clark51fd3712013-11-19 12:10:12 -050014630 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014631
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014632 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014633 return INVALID_PIPE;
14634
14635 return to_intel_crtc(encoder->crtc)->pipe;
14636}
14637
Carl Worth08d7b3d2009-04-29 14:43:54 -070014638int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014639 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014640{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014641 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014642 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014643 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014644
Rob Clark7707e652014-07-17 23:30:04 -040014645 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014646 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014647 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014648
Rob Clark7707e652014-07-17 23:30:04 -040014649 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014650 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014651
Daniel Vetterc05422d2009-08-11 16:05:30 +020014652 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014653}
14654
Daniel Vetter66a92782012-07-12 20:08:18 +020014655static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014656{
Daniel Vetter66a92782012-07-12 20:08:18 +020014657 struct drm_device *dev = encoder->base.dev;
14658 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014659 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014660 int entry = 0;
14661
Damien Lespiaub2784e12014-08-05 11:29:37 +010014662 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014663 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014664 index_mask |= (1 << entry);
14665
Jesse Barnes79e53942008-11-07 14:24:08 -080014666 entry++;
14667 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014668
Jesse Barnes79e53942008-11-07 14:24:08 -080014669 return index_mask;
14670}
14671
Chris Wilson4d302442010-12-14 19:21:29 +000014672static bool has_edp_a(struct drm_device *dev)
14673{
14674 struct drm_i915_private *dev_priv = dev->dev_private;
14675
14676 if (!IS_MOBILE(dev))
14677 return false;
14678
14679 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14680 return false;
14681
Damien Lespiaue3589902014-02-07 19:12:50 +000014682 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014683 return false;
14684
14685 return true;
14686}
14687
Jesse Barnes84b4e042014-06-25 08:24:29 -070014688static bool intel_crt_present(struct drm_device *dev)
14689{
14690 struct drm_i915_private *dev_priv = dev->dev_private;
14691
Damien Lespiau884497e2013-12-03 13:56:23 +000014692 if (INTEL_INFO(dev)->gen >= 9)
14693 return false;
14694
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014695 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014696 return false;
14697
14698 if (IS_CHERRYVIEW(dev))
14699 return false;
14700
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014701 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14702 return false;
14703
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014704 /* DDI E can't be used if DDI A requires 4 lanes */
14705 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14706 return false;
14707
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014708 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014709 return false;
14710
14711 return true;
14712}
14713
Jesse Barnes79e53942008-11-07 14:24:08 -080014714static void intel_setup_outputs(struct drm_device *dev)
14715{
Eric Anholt725e30a2009-01-22 13:01:02 -080014716 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014717 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014718 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014719
Imre Deak97a824e12016-06-21 11:51:47 +030014720 /*
14721 * intel_edp_init_connector() depends on this completing first, to
14722 * prevent the registeration of both eDP and LVDS and the incorrect
14723 * sharing of the PPS.
14724 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014725 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014726
Jesse Barnes84b4e042014-06-25 08:24:29 -070014727 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014728 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014729
Vandana Kannanc776eb22014-08-19 12:05:01 +053014730 if (IS_BROXTON(dev)) {
14731 /*
14732 * FIXME: Broxton doesn't support port detection via the
14733 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14734 * detect the ports.
14735 */
14736 intel_ddi_init(dev, PORT_A);
14737 intel_ddi_init(dev, PORT_B);
14738 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014739
14740 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014741 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014742 int found;
14743
Jesse Barnesde31fac2015-03-06 15:53:32 -080014744 /*
14745 * Haswell uses DDI functions to detect digital outputs.
14746 * On SKL pre-D0 the strap isn't connected, so we assume
14747 * it's there.
14748 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014749 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014750 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014751 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014752 intel_ddi_init(dev, PORT_A);
14753
14754 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14755 * register */
14756 found = I915_READ(SFUSE_STRAP);
14757
14758 if (found & SFUSE_STRAP_DDIB_DETECTED)
14759 intel_ddi_init(dev, PORT_B);
14760 if (found & SFUSE_STRAP_DDIC_DETECTED)
14761 intel_ddi_init(dev, PORT_C);
14762 if (found & SFUSE_STRAP_DDID_DETECTED)
14763 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014764 /*
14765 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14766 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014767 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014768 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14769 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14770 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14771 intel_ddi_init(dev, PORT_E);
14772
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014773 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014774 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014775 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014776
14777 if (has_edp_a(dev))
14778 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014779
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014780 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014781 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014782 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014783 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014784 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014785 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014786 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014787 }
14788
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014789 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014790 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014791
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014792 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014793 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014794
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014795 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014796 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014797
Daniel Vetter270b3042012-10-27 15:52:05 +020014798 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014799 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014800 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014801 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014802
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014803 /*
14804 * The DP_DETECTED bit is the latched state of the DDC
14805 * SDA pin at boot. However since eDP doesn't require DDC
14806 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14807 * eDP ports may have been muxed to an alternate function.
14808 * Thus we can't rely on the DP_DETECTED bit alone to detect
14809 * eDP ports. Consult the VBT as well as DP_DETECTED to
14810 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014811 *
14812 * Sadly the straps seem to be missing sometimes even for HDMI
14813 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14814 * and VBT for the presence of the port. Additionally we can't
14815 * trust the port type the VBT declares as we've seen at least
14816 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014817 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014818 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014819 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14820 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014821 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014822 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014823 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014824
Chris Wilson457c52d2016-06-01 08:27:50 +010014825 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014826 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14827 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014828 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014829 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014830 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014831
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014832 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014833 /*
14834 * eDP not supported on port D,
14835 * so no need to worry about it
14836 */
14837 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14838 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014839 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014840 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14841 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014842 }
14843
Jani Nikula3cfca972013-08-27 15:12:26 +030014844 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014845 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014846 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014847
Paulo Zanonie2debe92013-02-18 19:00:27 -030014848 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014849 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014850 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014851 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014852 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014853 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014854 }
Ma Ling27185ae2009-08-24 13:50:23 +080014855
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014856 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014857 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014858 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014859
14860 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014861
Paulo Zanonie2debe92013-02-18 19:00:27 -030014862 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014863 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014864 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014865 }
Ma Ling27185ae2009-08-24 13:50:23 +080014866
Paulo Zanonie2debe92013-02-18 19:00:27 -030014867 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014868
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014869 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014870 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014871 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014872 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014873 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014874 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014875 }
Ma Ling27185ae2009-08-24 13:50:23 +080014876
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014877 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014878 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014879 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014880 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014881 intel_dvo_init(dev);
14882
Zhenyu Wang103a1962009-11-27 11:44:36 +080014883 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014884 intel_tv_init(dev);
14885
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014886 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014887
Damien Lespiaub2784e12014-08-05 11:29:37 +010014888 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014889 encoder->base.possible_crtcs = encoder->crtc_mask;
14890 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014891 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014892 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014893
Paulo Zanonidde86e22012-12-01 12:04:25 -020014894 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014895
14896 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014897}
14898
14899static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14900{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014901 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014902 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014903
Daniel Vetteref2d6332014-02-10 18:00:38 +010014904 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014905 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014906 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014907 drm_gem_object_unreference(&intel_fb->obj->base);
14908 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014909 kfree(intel_fb);
14910}
14911
14912static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014913 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014914 unsigned int *handle)
14915{
14916 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014917 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014918
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014919 if (obj->userptr.mm) {
14920 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14921 return -EINVAL;
14922 }
14923
Chris Wilson05394f32010-11-08 19:18:58 +000014924 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014925}
14926
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014927static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14928 struct drm_file *file,
14929 unsigned flags, unsigned color,
14930 struct drm_clip_rect *clips,
14931 unsigned num_clips)
14932{
14933 struct drm_device *dev = fb->dev;
14934 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14935 struct drm_i915_gem_object *obj = intel_fb->obj;
14936
14937 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014938 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014939 mutex_unlock(&dev->struct_mutex);
14940
14941 return 0;
14942}
14943
Jesse Barnes79e53942008-11-07 14:24:08 -080014944static const struct drm_framebuffer_funcs intel_fb_funcs = {
14945 .destroy = intel_user_framebuffer_destroy,
14946 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014947 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014948};
14949
Damien Lespiaub3218032015-02-27 11:15:18 +000014950static
14951u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14952 uint32_t pixel_format)
14953{
14954 u32 gen = INTEL_INFO(dev)->gen;
14955
14956 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014957 int cpp = drm_format_plane_cpp(pixel_format, 0);
14958
Damien Lespiaub3218032015-02-27 11:15:18 +000014959 /* "The stride in bytes must not exceed the of the size of 8K
14960 * pixels and 32K bytes."
14961 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014962 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014963 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014964 return 32*1024;
14965 } else if (gen >= 4) {
14966 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14967 return 16*1024;
14968 else
14969 return 32*1024;
14970 } else if (gen >= 3) {
14971 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14972 return 8*1024;
14973 else
14974 return 16*1024;
14975 } else {
14976 /* XXX DSPC is limited to 4k tiled */
14977 return 8*1024;
14978 }
14979}
14980
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014981static int intel_framebuffer_init(struct drm_device *dev,
14982 struct intel_framebuffer *intel_fb,
14983 struct drm_mode_fb_cmd2 *mode_cmd,
14984 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014985{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014986 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014987 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014988 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014989 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014990
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014991 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14992
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014993 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14994 /* Enforce that fb modifier and tiling mode match, but only for
14995 * X-tiled. This is needed for FBC. */
14996 if (!!(obj->tiling_mode == I915_TILING_X) !=
14997 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14998 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14999 return -EINVAL;
15000 }
15001 } else {
15002 if (obj->tiling_mode == I915_TILING_X)
15003 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
15004 else if (obj->tiling_mode == I915_TILING_Y) {
15005 DRM_DEBUG("No Y tiling for legacy addfb\n");
15006 return -EINVAL;
15007 }
15008 }
15009
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015010 /* Passed in modifier sanity checking. */
15011 switch (mode_cmd->modifier[0]) {
15012 case I915_FORMAT_MOD_Y_TILED:
15013 case I915_FORMAT_MOD_Yf_TILED:
15014 if (INTEL_INFO(dev)->gen < 9) {
15015 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15016 mode_cmd->modifier[0]);
15017 return -EINVAL;
15018 }
15019 case DRM_FORMAT_MOD_NONE:
15020 case I915_FORMAT_MOD_X_TILED:
15021 break;
15022 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015023 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15024 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015025 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015026 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015027
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015028 stride_alignment = intel_fb_stride_alignment(dev_priv,
15029 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015030 mode_cmd->pixel_format);
15031 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15032 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15033 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015034 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015035 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015036
Damien Lespiaub3218032015-02-27 11:15:18 +000015037 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15038 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015039 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015040 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15041 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015042 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015043 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015044 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015045 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015046
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015047 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015048 mode_cmd->pitches[0] != obj->stride) {
15049 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15050 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015051 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015052 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015053
Ville Syrjälä57779d02012-10-31 17:50:14 +020015054 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015055 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015056 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015057 case DRM_FORMAT_RGB565:
15058 case DRM_FORMAT_XRGB8888:
15059 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015060 break;
15061 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015062 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015063 DRM_DEBUG("unsupported pixel format: %s\n",
15064 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015065 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015066 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015067 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015068 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015069 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15070 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015071 DRM_DEBUG("unsupported pixel format: %s\n",
15072 drm_get_format_name(mode_cmd->pixel_format));
15073 return -EINVAL;
15074 }
15075 break;
15076 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015077 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015078 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015079 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015080 DRM_DEBUG("unsupported pixel format: %s\n",
15081 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015082 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015083 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015084 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015085 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015086 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015087 DRM_DEBUG("unsupported pixel format: %s\n",
15088 drm_get_format_name(mode_cmd->pixel_format));
15089 return -EINVAL;
15090 }
15091 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015092 case DRM_FORMAT_YUYV:
15093 case DRM_FORMAT_UYVY:
15094 case DRM_FORMAT_YVYU:
15095 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015096 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015097 DRM_DEBUG("unsupported pixel format: %s\n",
15098 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015099 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015100 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015101 break;
15102 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015103 DRM_DEBUG("unsupported pixel format: %s\n",
15104 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015105 return -EINVAL;
15106 }
15107
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015108 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15109 if (mode_cmd->offsets[0] != 0)
15110 return -EINVAL;
15111
Damien Lespiauec2c9812015-01-20 12:51:45 +000015112 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015113 mode_cmd->pixel_format,
15114 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015115 /* FIXME drm helper for size checks (especially planar formats)? */
15116 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15117 return -EINVAL;
15118
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015119 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15120 intel_fb->obj = obj;
15121
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015122 intel_fill_fb_info(dev_priv, &intel_fb->base);
15123
Jesse Barnes79e53942008-11-07 14:24:08 -080015124 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15125 if (ret) {
15126 DRM_ERROR("framebuffer init failed %d\n", ret);
15127 return ret;
15128 }
15129
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015130 intel_fb->obj->framebuffer_references++;
15131
Jesse Barnes79e53942008-11-07 14:24:08 -080015132 return 0;
15133}
15134
Jesse Barnes79e53942008-11-07 14:24:08 -080015135static struct drm_framebuffer *
15136intel_user_framebuffer_create(struct drm_device *dev,
15137 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015138 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015139{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015140 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015141 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015142 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015143
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010015144 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000015145 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015146 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015147
Daniel Vetter92907cb2015-11-23 09:04:05 +010015148 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015149 if (IS_ERR(fb))
15150 drm_gem_object_unreference_unlocked(&obj->base);
15151
15152 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015153}
15154
Daniel Vetter06957262015-08-10 13:34:08 +020015155#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015156static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015157{
15158}
15159#endif
15160
Jesse Barnes79e53942008-11-07 14:24:08 -080015161static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015162 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015163 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015164 .atomic_check = intel_atomic_check,
15165 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015166 .atomic_state_alloc = intel_atomic_state_alloc,
15167 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015168};
15169
Imre Deak88212942016-03-16 13:38:53 +020015170/**
15171 * intel_init_display_hooks - initialize the display modesetting hooks
15172 * @dev_priv: device private
15173 */
15174void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015175{
Imre Deak88212942016-03-16 13:38:53 +020015176 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015177 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015178 dev_priv->display.get_initial_plane_config =
15179 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015180 dev_priv->display.crtc_compute_clock =
15181 haswell_crtc_compute_clock;
15182 dev_priv->display.crtc_enable = haswell_crtc_enable;
15183 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015184 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015185 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015186 dev_priv->display.get_initial_plane_config =
15187 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015188 dev_priv->display.crtc_compute_clock =
15189 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015190 dev_priv->display.crtc_enable = haswell_crtc_enable;
15191 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015192 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015193 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015194 dev_priv->display.get_initial_plane_config =
15195 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015196 dev_priv->display.crtc_compute_clock =
15197 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015198 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15199 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015200 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015201 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015202 dev_priv->display.get_initial_plane_config =
15203 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015204 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15205 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15206 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15207 } else if (IS_VALLEYVIEW(dev_priv)) {
15208 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15209 dev_priv->display.get_initial_plane_config =
15210 i9xx_get_initial_plane_config;
15211 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015212 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15213 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015214 } else if (IS_G4X(dev_priv)) {
15215 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15216 dev_priv->display.get_initial_plane_config =
15217 i9xx_get_initial_plane_config;
15218 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15219 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15220 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015221 } else if (IS_PINEVIEW(dev_priv)) {
15222 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15223 dev_priv->display.get_initial_plane_config =
15224 i9xx_get_initial_plane_config;
15225 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15226 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15227 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015228 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015229 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015230 dev_priv->display.get_initial_plane_config =
15231 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015232 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015233 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15234 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015235 } else {
15236 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15237 dev_priv->display.get_initial_plane_config =
15238 i9xx_get_initial_plane_config;
15239 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15240 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15241 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015242 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015243
Jesse Barnese70236a2009-09-21 10:42:27 -070015244 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015245 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015246 dev_priv->display.get_display_clock_speed =
15247 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015248 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015249 dev_priv->display.get_display_clock_speed =
15250 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015251 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015252 dev_priv->display.get_display_clock_speed =
15253 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015254 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015255 dev_priv->display.get_display_clock_speed =
15256 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015257 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015258 dev_priv->display.get_display_clock_speed =
15259 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015260 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015261 dev_priv->display.get_display_clock_speed =
15262 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015263 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15264 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015265 dev_priv->display.get_display_clock_speed =
15266 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015267 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015268 dev_priv->display.get_display_clock_speed =
15269 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015270 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015271 dev_priv->display.get_display_clock_speed =
15272 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015273 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015274 dev_priv->display.get_display_clock_speed =
15275 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015276 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015277 dev_priv->display.get_display_clock_speed =
15278 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015279 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015280 dev_priv->display.get_display_clock_speed =
15281 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015282 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015283 dev_priv->display.get_display_clock_speed =
15284 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015285 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015286 dev_priv->display.get_display_clock_speed =
15287 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015288 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015289 dev_priv->display.get_display_clock_speed =
15290 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015291 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015292 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015293 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015294 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015295 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015296 dev_priv->display.get_display_clock_speed =
15297 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015298 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015299
Imre Deak88212942016-03-16 13:38:53 +020015300 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015301 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015302 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015303 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015304 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015305 /* FIXME: detect B0+ stepping and use auto training */
15306 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015307 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015308 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015309 }
15310
15311 if (IS_BROADWELL(dev_priv)) {
15312 dev_priv->display.modeset_commit_cdclk =
15313 broadwell_modeset_commit_cdclk;
15314 dev_priv->display.modeset_calc_cdclk =
15315 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015316 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015317 dev_priv->display.modeset_commit_cdclk =
15318 valleyview_modeset_commit_cdclk;
15319 dev_priv->display.modeset_calc_cdclk =
15320 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015321 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015322 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015323 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015324 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015325 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015326 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15327 dev_priv->display.modeset_commit_cdclk =
15328 skl_modeset_commit_cdclk;
15329 dev_priv->display.modeset_calc_cdclk =
15330 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015331 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015332
15333 switch (INTEL_INFO(dev_priv)->gen) {
15334 case 2:
15335 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15336 break;
15337
15338 case 3:
15339 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15340 break;
15341
15342 case 4:
15343 case 5:
15344 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15345 break;
15346
15347 case 6:
15348 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15349 break;
15350 case 7:
15351 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15352 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15353 break;
15354 case 9:
15355 /* Drop through - unsupported since execlist only. */
15356 default:
15357 /* Default just returns -ENODEV to indicate unsupported */
15358 dev_priv->display.queue_flip = intel_default_queue_flip;
15359 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015360}
15361
Jesse Barnesb690e962010-07-19 13:53:12 -070015362/*
15363 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15364 * resume, or other times. This quirk makes sure that's the case for
15365 * affected systems.
15366 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015367static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015368{
15369 struct drm_i915_private *dev_priv = dev->dev_private;
15370
15371 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015372 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015373}
15374
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015375static void quirk_pipeb_force(struct drm_device *dev)
15376{
15377 struct drm_i915_private *dev_priv = dev->dev_private;
15378
15379 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15380 DRM_INFO("applying pipe b force quirk\n");
15381}
15382
Keith Packard435793d2011-07-12 14:56:22 -070015383/*
15384 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15385 */
15386static void quirk_ssc_force_disable(struct drm_device *dev)
15387{
15388 struct drm_i915_private *dev_priv = dev->dev_private;
15389 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015390 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015391}
15392
Carsten Emde4dca20e2012-03-15 15:56:26 +010015393/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015394 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15395 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015396 */
15397static void quirk_invert_brightness(struct drm_device *dev)
15398{
15399 struct drm_i915_private *dev_priv = dev->dev_private;
15400 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015401 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015402}
15403
Scot Doyle9c72cc62014-07-03 23:27:50 +000015404/* Some VBT's incorrectly indicate no backlight is present */
15405static void quirk_backlight_present(struct drm_device *dev)
15406{
15407 struct drm_i915_private *dev_priv = dev->dev_private;
15408 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15409 DRM_INFO("applying backlight present quirk\n");
15410}
15411
Jesse Barnesb690e962010-07-19 13:53:12 -070015412struct intel_quirk {
15413 int device;
15414 int subsystem_vendor;
15415 int subsystem_device;
15416 void (*hook)(struct drm_device *dev);
15417};
15418
Egbert Eich5f85f172012-10-14 15:46:38 +020015419/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15420struct intel_dmi_quirk {
15421 void (*hook)(struct drm_device *dev);
15422 const struct dmi_system_id (*dmi_id_list)[];
15423};
15424
15425static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15426{
15427 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15428 return 1;
15429}
15430
15431static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15432 {
15433 .dmi_id_list = &(const struct dmi_system_id[]) {
15434 {
15435 .callback = intel_dmi_reverse_brightness,
15436 .ident = "NCR Corporation",
15437 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15438 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15439 },
15440 },
15441 { } /* terminating entry */
15442 },
15443 .hook = quirk_invert_brightness,
15444 },
15445};
15446
Ben Widawskyc43b5632012-04-16 14:07:40 -070015447static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015448 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15449 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15450
Jesse Barnesb690e962010-07-19 13:53:12 -070015451 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15452 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15453
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015454 /* 830 needs to leave pipe A & dpll A up */
15455 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15456
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015457 /* 830 needs to leave pipe B & dpll B up */
15458 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15459
Keith Packard435793d2011-07-12 14:56:22 -070015460 /* Lenovo U160 cannot use SSC on LVDS */
15461 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015462
15463 /* Sony Vaio Y cannot use SSC on LVDS */
15464 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015465
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015466 /* Acer Aspire 5734Z must invert backlight brightness */
15467 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15468
15469 /* Acer/eMachines G725 */
15470 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15471
15472 /* Acer/eMachines e725 */
15473 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15474
15475 /* Acer/Packard Bell NCL20 */
15476 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15477
15478 /* Acer Aspire 4736Z */
15479 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015480
15481 /* Acer Aspire 5336 */
15482 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015483
15484 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15485 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015486
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015487 /* Acer C720 Chromebook (Core i3 4005U) */
15488 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15489
jens steinb2a96012014-10-28 20:25:53 +010015490 /* Apple Macbook 2,1 (Core 2 T7400) */
15491 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15492
Jani Nikula1b9448b2015-11-05 11:49:59 +020015493 /* Apple Macbook 4,1 */
15494 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15495
Scot Doyled4967d82014-07-03 23:27:52 +000015496 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15497 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015498
15499 /* HP Chromebook 14 (Celeron 2955U) */
15500 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015501
15502 /* Dell Chromebook 11 */
15503 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015504
15505 /* Dell Chromebook 11 (2015 version) */
15506 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015507};
15508
15509static void intel_init_quirks(struct drm_device *dev)
15510{
15511 struct pci_dev *d = dev->pdev;
15512 int i;
15513
15514 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15515 struct intel_quirk *q = &intel_quirks[i];
15516
15517 if (d->device == q->device &&
15518 (d->subsystem_vendor == q->subsystem_vendor ||
15519 q->subsystem_vendor == PCI_ANY_ID) &&
15520 (d->subsystem_device == q->subsystem_device ||
15521 q->subsystem_device == PCI_ANY_ID))
15522 q->hook(dev);
15523 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015524 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15525 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15526 intel_dmi_quirks[i].hook(dev);
15527 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015528}
15529
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015530/* Disable the VGA plane that we never use */
15531static void i915_disable_vga(struct drm_device *dev)
15532{
15533 struct drm_i915_private *dev_priv = dev->dev_private;
15534 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015535 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015536
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015537 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015538 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015539 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015540 sr1 = inb(VGA_SR_DATA);
15541 outb(sr1 | 1<<5, VGA_SR_DATA);
15542 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15543 udelay(300);
15544
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015545 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015546 POSTING_READ(vga_reg);
15547}
15548
Daniel Vetterf8175862012-04-10 15:50:11 +020015549void intel_modeset_init_hw(struct drm_device *dev)
15550{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015551 struct drm_i915_private *dev_priv = dev->dev_private;
15552
Ville Syrjäläb6283052015-06-03 15:45:07 +030015553 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015554
15555 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15556
Daniel Vetterf8175862012-04-10 15:50:11 +020015557 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015558 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015559}
15560
Matt Roperd93c0372015-12-03 11:37:41 -080015561/*
15562 * Calculate what we think the watermarks should be for the state we've read
15563 * out of the hardware and then immediately program those watermarks so that
15564 * we ensure the hardware settings match our internal state.
15565 *
15566 * We can calculate what we think WM's should be by creating a duplicate of the
15567 * current state (which was constructed during hardware readout) and running it
15568 * through the atomic check code to calculate new watermark values in the
15569 * state object.
15570 */
15571static void sanitize_watermarks(struct drm_device *dev)
15572{
15573 struct drm_i915_private *dev_priv = to_i915(dev);
15574 struct drm_atomic_state *state;
15575 struct drm_crtc *crtc;
15576 struct drm_crtc_state *cstate;
15577 struct drm_modeset_acquire_ctx ctx;
15578 int ret;
15579 int i;
15580
15581 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015582 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015583 return;
15584
15585 /*
15586 * We need to hold connection_mutex before calling duplicate_state so
15587 * that the connector loop is protected.
15588 */
15589 drm_modeset_acquire_init(&ctx, 0);
15590retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015591 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015592 if (ret == -EDEADLK) {
15593 drm_modeset_backoff(&ctx);
15594 goto retry;
15595 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015596 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015597 }
15598
15599 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15600 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015601 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015602
Matt Ropered4a6a72016-02-23 17:20:13 -080015603 /*
15604 * Hardware readout is the only time we don't want to calculate
15605 * intermediate watermarks (since we don't trust the current
15606 * watermarks).
15607 */
15608 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15609
Matt Roperd93c0372015-12-03 11:37:41 -080015610 ret = intel_atomic_check(dev, state);
15611 if (ret) {
15612 /*
15613 * If we fail here, it means that the hardware appears to be
15614 * programmed in a way that shouldn't be possible, given our
15615 * understanding of watermark requirements. This might mean a
15616 * mistake in the hardware readout code or a mistake in the
15617 * watermark calculations for a given platform. Raise a WARN
15618 * so that this is noticeable.
15619 *
15620 * If this actually happens, we'll have to just leave the
15621 * BIOS-programmed watermarks untouched and hope for the best.
15622 */
15623 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015624 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015625 }
15626
15627 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015628 for_each_crtc_in_state(state, crtc, cstate, i) {
15629 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15630
Matt Ropered4a6a72016-02-23 17:20:13 -080015631 cs->wm.need_postvbl_update = true;
15632 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015633 }
15634
15635 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015636fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015637 drm_modeset_drop_locks(&ctx);
15638 drm_modeset_acquire_fini(&ctx);
15639}
15640
Jesse Barnes79e53942008-11-07 14:24:08 -080015641void intel_modeset_init(struct drm_device *dev)
15642{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015643 struct drm_i915_private *dev_priv = to_i915(dev);
15644 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015645 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015646 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015647 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015648
15649 drm_mode_config_init(dev);
15650
15651 dev->mode_config.min_width = 0;
15652 dev->mode_config.min_height = 0;
15653
Dave Airlie019d96c2011-09-29 16:20:42 +010015654 dev->mode_config.preferred_depth = 24;
15655 dev->mode_config.prefer_shadow = 1;
15656
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015657 dev->mode_config.allow_fb_modifiers = true;
15658
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015659 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015660
Jesse Barnesb690e962010-07-19 13:53:12 -070015661 intel_init_quirks(dev);
15662
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015663 intel_init_pm(dev);
15664
Ben Widawskye3c74752013-04-05 13:12:39 -070015665 if (INTEL_INFO(dev)->num_pipes == 0)
15666 return;
15667
Lukas Wunner69f92f62015-07-15 13:57:35 +020015668 /*
15669 * There may be no VBT; and if the BIOS enabled SSC we can
15670 * just keep using it to avoid unnecessary flicker. Whereas if the
15671 * BIOS isn't using it, don't assume it will work even if the VBT
15672 * indicates as much.
15673 */
15674 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15675 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15676 DREF_SSC1_ENABLE);
15677
15678 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15679 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15680 bios_lvds_use_ssc ? "en" : "dis",
15681 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15682 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15683 }
15684 }
15685
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015686 if (IS_GEN2(dev)) {
15687 dev->mode_config.max_width = 2048;
15688 dev->mode_config.max_height = 2048;
15689 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015690 dev->mode_config.max_width = 4096;
15691 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015692 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015693 dev->mode_config.max_width = 8192;
15694 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015695 }
Damien Lespiau068be562014-03-28 14:17:49 +000015696
Ville Syrjälädc41c152014-08-13 11:57:05 +030015697 if (IS_845G(dev) || IS_I865G(dev)) {
15698 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15699 dev->mode_config.cursor_height = 1023;
15700 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015701 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15702 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15703 } else {
15704 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15705 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15706 }
15707
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015708 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015709
Zhao Yakui28c97732009-10-09 11:39:41 +080015710 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015711 INTEL_INFO(dev)->num_pipes,
15712 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015713
Damien Lespiau055e3932014-08-18 13:49:10 +010015714 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015715 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015716 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015717 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015718 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015719 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015720 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015721 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015722 }
15723
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015724 intel_update_czclk(dev_priv);
15725 intel_update_cdclk(dev);
15726
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015727 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015728
Ville Syrjäläb2045352016-05-13 23:41:27 +030015729 if (dev_priv->max_cdclk_freq == 0)
15730 intel_update_max_cdclk(dev);
15731
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015732 /* Just disable it once at startup */
15733 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015734 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015735
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015736 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015737 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015738 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015739
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015740 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015741 struct intel_initial_plane_config plane_config = {};
15742
Jesse Barnes46f297f2014-03-07 08:57:48 -080015743 if (!crtc->active)
15744 continue;
15745
Jesse Barnes46f297f2014-03-07 08:57:48 -080015746 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015747 * Note that reserving the BIOS fb up front prevents us
15748 * from stuffing other stolen allocations like the ring
15749 * on top. This prevents some ugliness at boot time, and
15750 * can even allow for smooth boot transitions if the BIOS
15751 * fb is large enough for the active pipe configuration.
15752 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015753 dev_priv->display.get_initial_plane_config(crtc,
15754 &plane_config);
15755
15756 /*
15757 * If the fb is shared between multiple heads, we'll
15758 * just get the first one.
15759 */
15760 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015761 }
Matt Roperd93c0372015-12-03 11:37:41 -080015762
15763 /*
15764 * Make sure hardware watermarks really match the state we read out.
15765 * Note that we need to do this after reconstructing the BIOS fb's
15766 * since the watermark calculation done here will use pstate->fb.
15767 */
15768 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015769}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015770
Daniel Vetter7fad7982012-07-04 17:51:47 +020015771static void intel_enable_pipe_a(struct drm_device *dev)
15772{
15773 struct intel_connector *connector;
15774 struct drm_connector *crt = NULL;
15775 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015776 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015777
15778 /* We can't just switch on the pipe A, we need to set things up with a
15779 * proper mode and output configuration. As a gross hack, enable pipe A
15780 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015781 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015782 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15783 crt = &connector->base;
15784 break;
15785 }
15786 }
15787
15788 if (!crt)
15789 return;
15790
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015791 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015792 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015793}
15794
Daniel Vetterfa555832012-10-10 23:14:00 +020015795static bool
15796intel_check_plane_mapping(struct intel_crtc *crtc)
15797{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015798 struct drm_device *dev = crtc->base.dev;
15799 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015800 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015801
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015802 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015803 return true;
15804
Ville Syrjälä649636e2015-09-22 19:50:01 +030015805 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015806
15807 if ((val & DISPLAY_PLANE_ENABLE) &&
15808 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15809 return false;
15810
15811 return true;
15812}
15813
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015814static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15815{
15816 struct drm_device *dev = crtc->base.dev;
15817 struct intel_encoder *encoder;
15818
15819 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15820 return true;
15821
15822 return false;
15823}
15824
Ville Syrjälädd756192016-02-17 21:28:45 +020015825static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15826{
15827 struct drm_device *dev = encoder->base.dev;
15828 struct intel_connector *connector;
15829
15830 for_each_connector_on_encoder(dev, &encoder->base, connector)
15831 return true;
15832
15833 return false;
15834}
15835
Daniel Vetter24929352012-07-02 20:28:59 +020015836static void intel_sanitize_crtc(struct intel_crtc *crtc)
15837{
15838 struct drm_device *dev = crtc->base.dev;
15839 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015840 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015841
Daniel Vetter24929352012-07-02 20:28:59 +020015842 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015843 if (!transcoder_is_dsi(cpu_transcoder)) {
15844 i915_reg_t reg = PIPECONF(cpu_transcoder);
15845
15846 I915_WRITE(reg,
15847 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15848 }
Daniel Vetter24929352012-07-02 20:28:59 +020015849
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015850 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015851 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015852 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015853 struct intel_plane *plane;
15854
Daniel Vetter96256042015-02-13 21:03:42 +010015855 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015856
15857 /* Disable everything but the primary plane */
15858 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15859 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15860 continue;
15861
15862 plane->disable_plane(&plane->base, &crtc->base);
15863 }
Daniel Vetter96256042015-02-13 21:03:42 +010015864 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015865
Daniel Vetter24929352012-07-02 20:28:59 +020015866 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015867 * disable the crtc (and hence change the state) if it is wrong. Note
15868 * that gen4+ has a fixed plane -> pipe mapping. */
15869 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015870 bool plane;
15871
Ville Syrjälä78108b72016-05-27 20:59:19 +030015872 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15873 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015874
15875 /* Pipe has the wrong plane attached and the plane is active.
15876 * Temporarily change the plane mapping and disable everything
15877 * ... */
15878 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015879 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015880 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015881 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015882 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015883 }
Daniel Vetter24929352012-07-02 20:28:59 +020015884
Daniel Vetter7fad7982012-07-04 17:51:47 +020015885 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15886 crtc->pipe == PIPE_A && !crtc->active) {
15887 /* BIOS forgot to enable pipe A, this mostly happens after
15888 * resume. Force-enable the pipe to fix this, the update_dpms
15889 * call below we restore the pipe to the right state, but leave
15890 * the required bits on. */
15891 intel_enable_pipe_a(dev);
15892 }
15893
Daniel Vetter24929352012-07-02 20:28:59 +020015894 /* Adjust the state of the output pipe according to whether we
15895 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015896 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015897 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015898
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015899 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015900 /*
15901 * We start out with underrun reporting disabled to avoid races.
15902 * For correct bookkeeping mark this on active crtcs.
15903 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015904 * Also on gmch platforms we dont have any hardware bits to
15905 * disable the underrun reporting. Which means we need to start
15906 * out with underrun reporting disabled also on inactive pipes,
15907 * since otherwise we'll complain about the garbage we read when
15908 * e.g. coming up after runtime pm.
15909 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015910 * No protection against concurrent access is required - at
15911 * worst a fifo underrun happens which also sets this to false.
15912 */
15913 crtc->cpu_fifo_underrun_disabled = true;
15914 crtc->pch_fifo_underrun_disabled = true;
15915 }
Daniel Vetter24929352012-07-02 20:28:59 +020015916}
15917
15918static void intel_sanitize_encoder(struct intel_encoder *encoder)
15919{
15920 struct intel_connector *connector;
15921 struct drm_device *dev = encoder->base.dev;
15922
15923 /* We need to check both for a crtc link (meaning that the
15924 * encoder is active and trying to read from a pipe) and the
15925 * pipe itself being active. */
15926 bool has_active_crtc = encoder->base.crtc &&
15927 to_intel_crtc(encoder->base.crtc)->active;
15928
Ville Syrjälädd756192016-02-17 21:28:45 +020015929 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015930 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15931 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015932 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015933
15934 /* Connector is active, but has no active pipe. This is
15935 * fallout from our resume register restoring. Disable
15936 * the encoder manually again. */
15937 if (encoder->base.crtc) {
15938 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15939 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015940 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015941 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015942 if (encoder->post_disable)
15943 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015944 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015945 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015946
15947 /* Inconsistent output/port/pipe state happens presumably due to
15948 * a bug in one of the get_hw_state functions. Or someplace else
15949 * in our code, like the register restore mess on resume. Clamp
15950 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015951 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015952 if (connector->encoder != encoder)
15953 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015954 connector->base.dpms = DRM_MODE_DPMS_OFF;
15955 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015956 }
15957 }
15958 /* Enabled encoders without active connectors will be fixed in
15959 * the crtc fixup. */
15960}
15961
Imre Deak04098752014-02-18 00:02:16 +020015962void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015963{
15964 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015965 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015966
Imre Deak04098752014-02-18 00:02:16 +020015967 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15968 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15969 i915_disable_vga(dev);
15970 }
15971}
15972
15973void i915_redisable_vga(struct drm_device *dev)
15974{
15975 struct drm_i915_private *dev_priv = dev->dev_private;
15976
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015977 /* This function can be called both from intel_modeset_setup_hw_state or
15978 * at a very early point in our resume sequence, where the power well
15979 * structures are not yet restored. Since this function is at a very
15980 * paranoid "someone might have enabled VGA while we were not looking"
15981 * level, just check if the power well is enabled instead of trying to
15982 * follow the "don't touch the power well if we don't need it" policy
15983 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015984 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015985 return;
15986
Imre Deak04098752014-02-18 00:02:16 +020015987 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015988
15989 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015990}
15991
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015992static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015993{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015994 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015995
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015996 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015997}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015998
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015999/* FIXME read out full plane state for all planes */
16000static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016001{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016002 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016003 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016004 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016005
Matt Roper19b8d382015-09-24 15:53:17 -070016006 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016007 primary_get_hw_state(to_intel_plane(primary));
16008
16009 if (plane_state->visible)
16010 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016011}
16012
Daniel Vetter30e984d2013-06-05 13:34:17 +020016013static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016014{
16015 struct drm_i915_private *dev_priv = dev->dev_private;
16016 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016017 struct intel_crtc *crtc;
16018 struct intel_encoder *encoder;
16019 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016020 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016021
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016022 dev_priv->active_crtcs = 0;
16023
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016024 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016025 struct intel_crtc_state *crtc_state = crtc->config;
16026 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016027
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016028 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016029 memset(crtc_state, 0, sizeof(*crtc_state));
16030 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016031
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016032 crtc_state->base.active = crtc_state->base.enable =
16033 dev_priv->display.get_pipe_config(crtc, crtc_state);
16034
16035 crtc->base.enabled = crtc_state->base.enable;
16036 crtc->active = crtc_state->base.active;
16037
16038 if (crtc_state->base.active) {
16039 dev_priv->active_crtcs |= 1 << crtc->pipe;
16040
Clint Taylorc89e39f2016-05-13 23:41:21 +030016041 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016042 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016043 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016044 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16045 else
16046 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016047
16048 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16049 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16050 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016051 }
16052
16053 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016054
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016055 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016056
Ville Syrjälä78108b72016-05-27 20:59:19 +030016057 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16058 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016059 crtc->active ? "enabled" : "disabled");
16060 }
16061
Daniel Vetter53589012013-06-05 13:34:16 +020016062 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16063 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16064
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016065 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16066 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016067 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016068 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016069 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016070 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016071 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016072 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016073
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016074 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016075 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016076 }
16077
Damien Lespiaub2784e12014-08-05 11:29:37 +010016078 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016079 pipe = 0;
16080
16081 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016082 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16083 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016084 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016085 } else {
16086 encoder->base.crtc = NULL;
16087 }
16088
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016089 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016090 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016091 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016092 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016093 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016094 }
16095
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016096 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016097 if (connector->get_hw_state(connector)) {
16098 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016099
16100 encoder = connector->encoder;
16101 connector->base.encoder = &encoder->base;
16102
16103 if (encoder->base.crtc &&
16104 encoder->base.crtc->state->active) {
16105 /*
16106 * This has to be done during hardware readout
16107 * because anything calling .crtc_disable may
16108 * rely on the connector_mask being accurate.
16109 */
16110 encoder->base.crtc->state->connector_mask |=
16111 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016112 encoder->base.crtc->state->encoder_mask |=
16113 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016114 }
16115
Daniel Vetter24929352012-07-02 20:28:59 +020016116 } else {
16117 connector->base.dpms = DRM_MODE_DPMS_OFF;
16118 connector->base.encoder = NULL;
16119 }
16120 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16121 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016122 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016123 connector->base.encoder ? "enabled" : "disabled");
16124 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016125
16126 for_each_intel_crtc(dev, crtc) {
16127 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16128
16129 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16130 if (crtc->base.state->active) {
16131 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16132 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16133 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16134
16135 /*
16136 * The initial mode needs to be set in order to keep
16137 * the atomic core happy. It wants a valid mode if the
16138 * crtc's enabled, so we do the above call.
16139 *
16140 * At this point some state updated by the connectors
16141 * in their ->detect() callback has not run yet, so
16142 * no recalculation can be done yet.
16143 *
16144 * Even if we could do a recalculation and modeset
16145 * right now it would cause a double modeset if
16146 * fbdev or userspace chooses a different initial mode.
16147 *
16148 * If that happens, someone indicated they wanted a
16149 * mode change, which means it's safe to do a full
16150 * recalculation.
16151 */
16152 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016153
16154 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16155 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016156 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016157
16158 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016159 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016160}
16161
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016162/* Scan out the current hw modeset state,
16163 * and sanitizes it to the current state
16164 */
16165static void
16166intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016167{
16168 struct drm_i915_private *dev_priv = dev->dev_private;
16169 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016170 struct intel_crtc *crtc;
16171 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016172 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016173
16174 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016175
16176 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016177 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016178 intel_sanitize_encoder(encoder);
16179 }
16180
Damien Lespiau055e3932014-08-18 13:49:10 +010016181 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016182 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16183 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016184 intel_dump_pipe_config(crtc, crtc->config,
16185 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016186 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016187
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016188 intel_modeset_update_connector_atomic_state(dev);
16189
Daniel Vetter35c95372013-07-17 06:55:04 +020016190 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16191 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16192
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016193 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016194 continue;
16195
16196 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16197
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016198 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016199 pll->on = false;
16200 }
16201
Wayne Boyer666a4532015-12-09 12:29:35 -080016202 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016203 vlv_wm_get_hw_state(dev);
16204 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016205 skl_wm_get_hw_state(dev);
16206 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016207 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016208
16209 for_each_intel_crtc(dev, crtc) {
16210 unsigned long put_domains;
16211
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016212 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016213 if (WARN_ON(put_domains))
16214 modeset_put_power_domains(dev_priv, put_domains);
16215 }
16216 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016217
16218 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016219}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016220
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016221void intel_display_resume(struct drm_device *dev)
16222{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016223 struct drm_i915_private *dev_priv = to_i915(dev);
16224 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16225 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016226 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016227 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016228
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016229 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016230
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016231 /*
16232 * This is a cludge because with real atomic modeset mode_config.mutex
16233 * won't be taken. Unfortunately some probed state like
16234 * audio_codec_enable is still protected by mode_config.mutex, so lock
16235 * it here for now.
16236 */
16237 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016238 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016239
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016240retry:
16241 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016242
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016243 if (ret == 0 && !setup) {
16244 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016245
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016246 intel_modeset_setup_hw_state(dev);
16247 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016248 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016249
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016250 if (ret == 0 && state) {
16251 struct drm_crtc_state *crtc_state;
16252 struct drm_crtc *crtc;
16253 int i;
16254
16255 state->acquire_ctx = &ctx;
16256
Ville Syrjäläe3d54572016-05-13 10:10:42 -070016257 /* ignore any reset values/BIOS leftovers in the WM registers */
16258 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16259
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016260 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16261 /*
16262 * Force recalculation even if we restore
16263 * current state. With fast modeset this may not result
16264 * in a modeset when the state is compatible.
16265 */
16266 crtc_state->mode_changed = true;
16267 }
16268
16269 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016270 }
16271
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016272 if (ret == -EDEADLK) {
16273 drm_modeset_backoff(&ctx);
16274 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016275 }
16276
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016277 drm_modeset_drop_locks(&ctx);
16278 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016279 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016280
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016281 if (ret) {
16282 DRM_ERROR("Restoring old state failed with %i\n", ret);
16283 drm_atomic_state_free(state);
16284 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016285}
16286
16287void intel_modeset_gem_init(struct drm_device *dev)
16288{
Chris Wilsondc979972016-05-10 14:10:04 +010016289 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016290 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016291 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016292 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016293
Chris Wilsondc979972016-05-10 14:10:04 +010016294 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016295
Chris Wilson1833b132012-05-09 11:56:28 +010016296 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016297
Chris Wilson1ee8da62016-05-12 12:43:23 +010016298 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016299
16300 /*
16301 * Make sure any fbs we allocated at startup are properly
16302 * pinned & fenced. When we do the allocation it's too early
16303 * for this.
16304 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016305 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016306 obj = intel_fb_obj(c->primary->fb);
16307 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016308 continue;
16309
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016310 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016311 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16312 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016313 mutex_unlock(&dev->struct_mutex);
16314 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016315 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16316 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016317 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016318 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016319 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016320 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016321 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016322 }
16323 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016324}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016325
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016326int intel_connector_register(struct drm_connector *connector)
16327{
16328 struct intel_connector *intel_connector = to_intel_connector(connector);
16329 int ret;
16330
16331 ret = intel_backlight_device_register(intel_connector);
16332 if (ret)
16333 goto err;
16334
16335 return 0;
16336
16337err:
16338 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016339}
16340
Chris Wilsonc191eca2016-06-17 11:40:33 +010016341void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016342{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016343 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016344
Chris Wilsone63d87c2016-06-17 11:40:34 +010016345 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016346 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016347}
16348
Jesse Barnes79e53942008-11-07 14:24:08 -080016349void intel_modeset_cleanup(struct drm_device *dev)
16350{
Jesse Barnes652c3932009-08-17 13:31:43 -070016351 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070016352
Chris Wilsondc979972016-05-10 14:10:04 +010016353 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016354
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016355 /*
16356 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016357 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016358 * experience fancy races otherwise.
16359 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016360 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016361
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016362 /*
16363 * Due to the hpd irq storm handling the hotplug work can re-arm the
16364 * poll handlers. Hence disable polling after hpd handling is shut down.
16365 */
Keith Packardf87ea762010-10-03 19:36:26 -070016366 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016367
Jesse Barnes723bfd72010-10-07 16:01:13 -070016368 intel_unregister_dsm_handler();
16369
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016370 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016371
Chris Wilson1630fe72011-07-08 12:22:42 +010016372 /* flush any delayed tasks or pending work */
16373 flush_scheduled_work();
16374
Jesse Barnes79e53942008-11-07 14:24:08 -080016375 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016376
Chris Wilson1ee8da62016-05-12 12:43:23 +010016377 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016378
Chris Wilsondc979972016-05-10 14:10:04 +010016379 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016380
16381 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016382}
16383
Chris Wilsondf0e9242010-09-09 16:20:55 +010016384void intel_connector_attach_encoder(struct intel_connector *connector,
16385 struct intel_encoder *encoder)
16386{
16387 connector->encoder = encoder;
16388 drm_mode_connector_attach_encoder(&connector->base,
16389 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016390}
Dave Airlie28d52042009-09-21 14:33:58 +100016391
16392/*
16393 * set vga decode state - true == enable VGA decode
16394 */
16395int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16396{
16397 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016398 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016399 u16 gmch_ctrl;
16400
Chris Wilson75fa0412014-02-07 18:37:02 -020016401 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16402 DRM_ERROR("failed to read control word\n");
16403 return -EIO;
16404 }
16405
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016406 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16407 return 0;
16408
Dave Airlie28d52042009-09-21 14:33:58 +100016409 if (state)
16410 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16411 else
16412 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016413
16414 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16415 DRM_ERROR("failed to write control word\n");
16416 return -EIO;
16417 }
16418
Dave Airlie28d52042009-09-21 14:33:58 +100016419 return 0;
16420}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016421
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016422struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016423
16424 u32 power_well_driver;
16425
Chris Wilson63b66e52013-08-08 15:12:06 +020016426 int num_transcoders;
16427
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016428 struct intel_cursor_error_state {
16429 u32 control;
16430 u32 position;
16431 u32 base;
16432 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016433 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016434
16435 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016436 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016437 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016438 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016439 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016440
16441 struct intel_plane_error_state {
16442 u32 control;
16443 u32 stride;
16444 u32 size;
16445 u32 pos;
16446 u32 addr;
16447 u32 surface;
16448 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016449 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016450
16451 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016452 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016453 enum transcoder cpu_transcoder;
16454
16455 u32 conf;
16456
16457 u32 htotal;
16458 u32 hblank;
16459 u32 hsync;
16460 u32 vtotal;
16461 u32 vblank;
16462 u32 vsync;
16463 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016464};
16465
16466struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016467intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016468{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016469 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016470 int transcoders[] = {
16471 TRANSCODER_A,
16472 TRANSCODER_B,
16473 TRANSCODER_C,
16474 TRANSCODER_EDP,
16475 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016476 int i;
16477
Chris Wilsonc0336662016-05-06 15:40:21 +010016478 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016479 return NULL;
16480
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016481 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016482 if (error == NULL)
16483 return NULL;
16484
Chris Wilsonc0336662016-05-06 15:40:21 +010016485 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016486 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16487
Damien Lespiau055e3932014-08-18 13:49:10 +010016488 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016489 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016490 __intel_display_power_is_enabled(dev_priv,
16491 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016492 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016493 continue;
16494
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016495 error->cursor[i].control = I915_READ(CURCNTR(i));
16496 error->cursor[i].position = I915_READ(CURPOS(i));
16497 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016498
16499 error->plane[i].control = I915_READ(DSPCNTR(i));
16500 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016501 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016502 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016503 error->plane[i].pos = I915_READ(DSPPOS(i));
16504 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016505 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016506 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016507 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016508 error->plane[i].surface = I915_READ(DSPSURF(i));
16509 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16510 }
16511
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016512 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016513
Chris Wilsonc0336662016-05-06 15:40:21 +010016514 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030016515 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016516 }
16517
Jani Nikula4d1de972016-03-18 17:05:42 +020016518 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016519 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016520 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016521 error->num_transcoders++; /* Account for eDP. */
16522
16523 for (i = 0; i < error->num_transcoders; i++) {
16524 enum transcoder cpu_transcoder = transcoders[i];
16525
Imre Deakddf9c532013-11-27 22:02:02 +020016526 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016527 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016528 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016529 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016530 continue;
16531
Chris Wilson63b66e52013-08-08 15:12:06 +020016532 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16533
16534 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16535 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16536 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16537 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16538 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16539 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16540 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016541 }
16542
16543 return error;
16544}
16545
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016546#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16547
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016548void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016549intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016550 struct drm_device *dev,
16551 struct intel_display_error_state *error)
16552{
Damien Lespiau055e3932014-08-18 13:49:10 +010016553 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016554 int i;
16555
Chris Wilson63b66e52013-08-08 15:12:06 +020016556 if (!error)
16557 return;
16558
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016559 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016560 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016561 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016562 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016563 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016564 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016565 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016566 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016567 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016568 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016569
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016570 err_printf(m, "Plane [%d]:\n", i);
16571 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16572 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016573 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016574 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16575 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016576 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016577 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016578 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016579 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016580 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16581 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016582 }
16583
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016584 err_printf(m, "Cursor [%d]:\n", i);
16585 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16586 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16587 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016588 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016589
16590 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016591 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016592 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016593 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016594 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016595 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16596 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16597 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16598 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16599 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16600 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16601 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16602 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016603}