blob: ae253bb57c25c197a4671026a319c30311cdceb9 [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"
Imre Deakdb18b6a2016-03-24 12:41:40 +020039#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070040#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080041#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080042#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010043#include <drm/drm_dp_helper.h>
44#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070045#include <drm/drm_plane_helper.h>
46#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080047#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080048#include <linux/reservation.h>
49#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +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);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +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);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100126
Ma Lingd4906092009-03-18 20:13:27 +0800127struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300128 struct {
129 int min, max;
130 } dot, vco, n, m, m1, m2, p, p1;
131
132 struct {
133 int dot_limit;
134 int p2_slow, p2_fast;
135 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800136};
Jesse Barnes79e53942008-11-07 14:24:08 -0800137
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300138/* returns HPLL frequency in kHz */
139static int valleyview_get_vco(struct drm_i915_private *dev_priv)
140{
141 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
142
143 /* Obtain SKU information */
144 mutex_lock(&dev_priv->sb_lock);
145 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
146 CCK_FUSE_HPLL_FREQ_MASK;
147 mutex_unlock(&dev_priv->sb_lock);
148
149 return vco_freq[hpll_freq] * 1000;
150}
151
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200152int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
153 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300154{
155 u32 val;
156 int divider;
157
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300158 mutex_lock(&dev_priv->sb_lock);
159 val = vlv_cck_read(dev_priv, reg);
160 mutex_unlock(&dev_priv->sb_lock);
161
162 divider = val & CCK_FREQUENCY_VALUES;
163
164 WARN((val & CCK_FREQUENCY_STATUS) !=
165 (divider << CCK_FREQUENCY_STATUS_SHIFT),
166 "%s change in progress\n", name);
167
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200168 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
169}
170
171static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
172 const char *name, u32 reg)
173{
174 if (dev_priv->hpll_freq == 0)
175 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
176
177 return vlv_get_cck_clock(dev_priv, name, reg,
178 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300179}
180
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200181static int
182intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200183{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200184 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200185}
186
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200187static int
188intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300189{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300190 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200191 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
192 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200193}
194
195static int
196intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
197{
Jani Nikula79e50a42015-08-26 10:58:20 +0300198 uint32_t clkcfg;
199
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200200 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300201 clkcfg = I915_READ(CLKCFG);
202 switch (clkcfg & CLKCFG_FSB_MASK) {
203 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200204 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300205 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200206 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300207 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200212 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300213 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300215 /* these two are just a guess; one of them might be right */
216 case CLKCFG_FSB_1600:
217 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200218 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300219 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200220 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300221 }
222}
223
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300224void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200225{
226 if (HAS_PCH_SPLIT(dev_priv))
227 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
228 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
229 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
230 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
232 else
233 return; /* no rawclk on other platforms, or no need to know it */
234
235 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
236}
237
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300238static void intel_update_czclk(struct drm_i915_private *dev_priv)
239{
Wayne Boyer666a4532015-12-09 12:29:35 -0800240 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300241 return;
242
243 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
244 CCK_CZ_CLOCK_CONTROL);
245
246 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
247}
248
Chris Wilson021357a2010-09-07 20:54:59 +0100249static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200250intel_fdi_link_freq(struct drm_i915_private *dev_priv,
251 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100252{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200253 if (HAS_DDI(dev_priv))
254 return pipe_config->port_clock; /* SPLL */
255 else if (IS_GEN5(dev_priv))
256 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200257 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200258 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100259}
260
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300261static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200263 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200264 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400265 .m = { .min = 96, .max = 140 },
266 .m1 = { .min = 18, .max = 26 },
267 .m2 = { .min = 6, .max = 16 },
268 .p = { .min = 4, .max = 128 },
269 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700270 .p2 = { .dot_limit = 165000,
271 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700272};
273
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300274static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200275 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200276 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200277 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200278 .m = { .min = 96, .max = 140 },
279 .m1 = { .min = 18, .max = 26 },
280 .m2 = { .min = 6, .max = 16 },
281 .p = { .min = 4, .max = 128 },
282 .p1 = { .min = 2, .max = 33 },
283 .p2 = { .dot_limit = 165000,
284 .p2_slow = 4, .p2_fast = 4 },
285};
286
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300287static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200289 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200290 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400291 .m = { .min = 96, .max = 140 },
292 .m1 = { .min = 18, .max = 26 },
293 .m2 = { .min = 6, .max = 16 },
294 .p = { .min = 4, .max = 128 },
295 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 165000,
297 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700298};
Eric Anholt273e27c2011-03-30 13:01:10 -0700299
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300300static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400301 .dot = { .min = 20000, .max = 400000 },
302 .vco = { .min = 1400000, .max = 2800000 },
303 .n = { .min = 1, .max = 6 },
304 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100305 .m1 = { .min = 8, .max = 18 },
306 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400307 .p = { .min = 5, .max = 80 },
308 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700309 .p2 = { .dot_limit = 200000,
310 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700311};
312
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300313static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400314 .dot = { .min = 20000, .max = 400000 },
315 .vco = { .min = 1400000, .max = 2800000 },
316 .n = { .min = 1, .max = 6 },
317 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100318 .m1 = { .min = 8, .max = 18 },
319 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400320 .p = { .min = 7, .max = 98 },
321 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700322 .p2 = { .dot_limit = 112000,
323 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700324};
325
Eric Anholt273e27c2011-03-30 13:01:10 -0700326
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300327static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700328 .dot = { .min = 25000, .max = 270000 },
329 .vco = { .min = 1750000, .max = 3500000},
330 .n = { .min = 1, .max = 4 },
331 .m = { .min = 104, .max = 138 },
332 .m1 = { .min = 17, .max = 23 },
333 .m2 = { .min = 5, .max = 11 },
334 .p = { .min = 10, .max = 30 },
335 .p1 = { .min = 1, .max = 3},
336 .p2 = { .dot_limit = 270000,
337 .p2_slow = 10,
338 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800339 },
Keith Packarde4b36692009-06-05 19:22:17 -0700340};
341
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300342static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700343 .dot = { .min = 22000, .max = 400000 },
344 .vco = { .min = 1750000, .max = 3500000},
345 .n = { .min = 1, .max = 4 },
346 .m = { .min = 104, .max = 138 },
347 .m1 = { .min = 16, .max = 23 },
348 .m2 = { .min = 5, .max = 11 },
349 .p = { .min = 5, .max = 80 },
350 .p1 = { .min = 1, .max = 8},
351 .p2 = { .dot_limit = 165000,
352 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700353};
354
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300355static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700356 .dot = { .min = 20000, .max = 115000 },
357 .vco = { .min = 1750000, .max = 3500000 },
358 .n = { .min = 1, .max = 3 },
359 .m = { .min = 104, .max = 138 },
360 .m1 = { .min = 17, .max = 23 },
361 .m2 = { .min = 5, .max = 11 },
362 .p = { .min = 28, .max = 112 },
363 .p1 = { .min = 2, .max = 8 },
364 .p2 = { .dot_limit = 0,
365 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800366 },
Keith Packarde4b36692009-06-05 19:22:17 -0700367};
368
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300369static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700370 .dot = { .min = 80000, .max = 224000 },
371 .vco = { .min = 1750000, .max = 3500000 },
372 .n = { .min = 1, .max = 3 },
373 .m = { .min = 104, .max = 138 },
374 .m1 = { .min = 17, .max = 23 },
375 .m2 = { .min = 5, .max = 11 },
376 .p = { .min = 14, .max = 42 },
377 .p1 = { .min = 2, .max = 6 },
378 .p2 = { .dot_limit = 0,
379 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800380 },
Keith Packarde4b36692009-06-05 19:22:17 -0700381};
382
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300383static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400384 .dot = { .min = 20000, .max = 400000},
385 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700386 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400387 .n = { .min = 3, .max = 6 },
388 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700389 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400390 .m1 = { .min = 0, .max = 0 },
391 .m2 = { .min = 0, .max = 254 },
392 .p = { .min = 5, .max = 80 },
393 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700394 .p2 = { .dot_limit = 200000,
395 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700396};
397
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300398static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400399 .dot = { .min = 20000, .max = 400000 },
400 .vco = { .min = 1700000, .max = 3500000 },
401 .n = { .min = 3, .max = 6 },
402 .m = { .min = 2, .max = 256 },
403 .m1 = { .min = 0, .max = 0 },
404 .m2 = { .min = 0, .max = 254 },
405 .p = { .min = 7, .max = 112 },
406 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700407 .p2 = { .dot_limit = 112000,
408 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700409};
410
Eric Anholt273e27c2011-03-30 13:01:10 -0700411/* Ironlake / Sandybridge
412 *
413 * We calculate clock using (register_value + 2) for N/M1/M2, so here
414 * the range value for them is (actual_value - 2).
415 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300416static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700417 .dot = { .min = 25000, .max = 350000 },
418 .vco = { .min = 1760000, .max = 3510000 },
419 .n = { .min = 1, .max = 5 },
420 .m = { .min = 79, .max = 127 },
421 .m1 = { .min = 12, .max = 22 },
422 .m2 = { .min = 5, .max = 9 },
423 .p = { .min = 5, .max = 80 },
424 .p1 = { .min = 1, .max = 8 },
425 .p2 = { .dot_limit = 225000,
426 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700427};
428
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300429static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700430 .dot = { .min = 25000, .max = 350000 },
431 .vco = { .min = 1760000, .max = 3510000 },
432 .n = { .min = 1, .max = 3 },
433 .m = { .min = 79, .max = 118 },
434 .m1 = { .min = 12, .max = 22 },
435 .m2 = { .min = 5, .max = 9 },
436 .p = { .min = 28, .max = 112 },
437 .p1 = { .min = 2, .max = 8 },
438 .p2 = { .dot_limit = 225000,
439 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800440};
441
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300442static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700443 .dot = { .min = 25000, .max = 350000 },
444 .vco = { .min = 1760000, .max = 3510000 },
445 .n = { .min = 1, .max = 3 },
446 .m = { .min = 79, .max = 127 },
447 .m1 = { .min = 12, .max = 22 },
448 .m2 = { .min = 5, .max = 9 },
449 .p = { .min = 14, .max = 56 },
450 .p1 = { .min = 2, .max = 8 },
451 .p2 = { .dot_limit = 225000,
452 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800453};
454
Eric Anholt273e27c2011-03-30 13:01:10 -0700455/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300456static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700457 .dot = { .min = 25000, .max = 350000 },
458 .vco = { .min = 1760000, .max = 3510000 },
459 .n = { .min = 1, .max = 2 },
460 .m = { .min = 79, .max = 126 },
461 .m1 = { .min = 12, .max = 22 },
462 .m2 = { .min = 5, .max = 9 },
463 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400464 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700465 .p2 = { .dot_limit = 225000,
466 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800467};
468
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300469static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700470 .dot = { .min = 25000, .max = 350000 },
471 .vco = { .min = 1760000, .max = 3510000 },
472 .n = { .min = 1, .max = 3 },
473 .m = { .min = 79, .max = 126 },
474 .m1 = { .min = 12, .max = 22 },
475 .m2 = { .min = 5, .max = 9 },
476 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400477 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700478 .p2 = { .dot_limit = 225000,
479 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800480};
481
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300482static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300483 /*
484 * These are the data rate limits (measured in fast clocks)
485 * since those are the strictest limits we have. The fast
486 * clock and actual rate limits are more relaxed, so checking
487 * them would make no difference.
488 */
489 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200490 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700491 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492 .m1 = { .min = 2, .max = 3 },
493 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300494 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300495 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700496};
497
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300498static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300499 /*
500 * These are the data rate limits (measured in fast clocks)
501 * since those are the strictest limits we have. The fast
502 * clock and actual rate limits are more relaxed, so checking
503 * them would make no difference.
504 */
505 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200506 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300507 .n = { .min = 1, .max = 1 },
508 .m1 = { .min = 2, .max = 2 },
509 .m2 = { .min = 24 << 22, .max = 175 << 22 },
510 .p1 = { .min = 2, .max = 4 },
511 .p2 = { .p2_slow = 1, .p2_fast = 14 },
512};
513
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300514static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200515 /* FIXME: find real dot limits */
516 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530517 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200518 .n = { .min = 1, .max = 1 },
519 .m1 = { .min = 2, .max = 2 },
520 /* FIXME: find real m2 limits */
521 .m2 = { .min = 2 << 22, .max = 255 << 22 },
522 .p1 = { .min = 2, .max = 4 },
523 .p2 = { .p2_slow = 1, .p2_fast = 20 },
524};
525
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200526static bool
527needs_modeset(struct drm_crtc_state *state)
528{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200529 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200530}
531
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300532/**
533 * Returns whether any output on the specified pipe is of the specified type
534 */
Damien Lespiau40935612014-10-29 11:16:59 +0000535bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300536{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300537 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300538 struct intel_encoder *encoder;
539
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300540 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300541 if (encoder->type == type)
542 return true;
543
544 return false;
545}
546
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200547/**
548 * Returns whether any output on the specified pipe will have the specified
549 * type after a staged modeset is complete, i.e., the same as
550 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
551 * encoder->crtc.
552 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
554 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200555{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200556 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300557 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200558 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200559 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200560 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200561
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300562 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200563 if (connector_state->crtc != crtc_state->base.crtc)
564 continue;
565
566 num_connectors++;
567
568 encoder = to_intel_encoder(connector_state->best_encoder);
569 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200570 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200571 }
572
573 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200574
575 return false;
576}
577
Imre Deakdccbea32015-06-22 23:35:51 +0300578/*
579 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
580 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
581 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
582 * The helpers' return value is the rate of the clock that is fed to the
583 * display engine's pipe which can be the above fast dot clock rate or a
584 * divided-down version of it.
585 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500586/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300587static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800588{
Shaohua Li21778322009-02-23 15:19:16 +0800589 clock->m = clock->m2 + 2;
590 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200591 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300592 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300593 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
594 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300595
596 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800597}
598
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200599static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
600{
601 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
602}
603
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300604static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800605{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200606 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800607 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200608 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300609 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300610 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
611 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300612
613 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800614}
615
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300616static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300617{
618 clock->m = clock->m1 * clock->m2;
619 clock->p = clock->p1 * clock->p2;
620 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300621 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300622 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
623 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300624
625 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300626}
627
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300628int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300629{
630 clock->m = clock->m1 * clock->m2;
631 clock->p = clock->p1 * clock->p2;
632 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300633 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300634 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
635 clock->n << 22);
636 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300637
638 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300639}
640
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800641#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800642/**
643 * Returns whether the given set of divisors are valid for a given refclk with
644 * the given connectors.
645 */
646
Chris Wilson1b894b52010-12-14 20:04:54 +0000647static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300648 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300649 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800650{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300651 if (clock->n < limit->n.min || limit->n.max < clock->n)
652 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400654 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400656 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400658 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300659
Wayne Boyer666a4532015-12-09 12:29:35 -0800660 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
661 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300662 if (clock->m1 <= clock->m2)
663 INTELPllInvalid("m1 <= m2\n");
664
Wayne Boyer666a4532015-12-09 12:29:35 -0800665 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300666 if (clock->p < limit->p.min || limit->p.max < clock->p)
667 INTELPllInvalid("p out of range\n");
668 if (clock->m < limit->m.min || limit->m.max < clock->m)
669 INTELPllInvalid("m out of range\n");
670 }
671
Jesse Barnes79e53942008-11-07 14:24:08 -0800672 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400673 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800674 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
675 * connector, etc., rather than just a single range.
676 */
677 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400678 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800679
680 return true;
681}
682
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300683static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300684i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300685 const struct intel_crtc_state *crtc_state,
686 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800687{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300688 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800689
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200690 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800691 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100692 * For LVDS just rely on its current settings for dual-channel.
693 * We haven't figured out how to reliably set up different
694 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100696 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300697 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300699 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800700 } else {
701 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300702 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800703 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300704 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800705 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300706}
707
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200708/*
709 * Returns a set of divisors for the desired target clock with the given
710 * refclk, or FALSE. The returned values represent the clock equation:
711 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
712 *
713 * Target and reference clocks are specified in kHz.
714 *
715 * If match_clock is provided, then best_clock P divider must match the P
716 * divider from @match_clock used for LVDS downclocking.
717 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300718static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300719i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300720 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300721 int target, int refclk, struct dpll *match_clock,
722 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300723{
724 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300725 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300726 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800727
Akshay Joshi0206e352011-08-16 15:34:10 -0400728 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800729
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300730 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
731
Zhao Yakui42158662009-11-20 11:24:18 +0800732 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
733 clock.m1++) {
734 for (clock.m2 = limit->m2.min;
735 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200736 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800737 break;
738 for (clock.n = limit->n.min;
739 clock.n <= limit->n.max; clock.n++) {
740 for (clock.p1 = limit->p1.min;
741 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800742 int this_err;
743
Imre Deakdccbea32015-06-22 23:35:51 +0300744 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000745 if (!intel_PLL_is_valid(dev, limit,
746 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800747 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800748 if (match_clock &&
749 clock.p != match_clock->p)
750 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800751
752 this_err = abs(clock.dot - target);
753 if (this_err < err) {
754 *best_clock = clock;
755 err = this_err;
756 }
757 }
758 }
759 }
760 }
761
762 return (err != target);
763}
764
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200765/*
766 * Returns a set of divisors for the desired target clock with the given
767 * refclk, or FALSE. The returned values represent the clock equation:
768 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
769 *
770 * Target and reference clocks are specified in kHz.
771 *
772 * If match_clock is provided, then best_clock P divider must match the P
773 * divider from @match_clock used for LVDS downclocking.
774 */
Ma Lingd4906092009-03-18 20:13:27 +0800775static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300776pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200777 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300778 int target, int refclk, struct dpll *match_clock,
779 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200780{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300781 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300782 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200783 int err = target;
784
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200785 memset(best_clock, 0, sizeof(*best_clock));
786
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300787 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
788
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200789 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
790 clock.m1++) {
791 for (clock.m2 = limit->m2.min;
792 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200793 for (clock.n = limit->n.min;
794 clock.n <= limit->n.max; clock.n++) {
795 for (clock.p1 = limit->p1.min;
796 clock.p1 <= limit->p1.max; clock.p1++) {
797 int this_err;
798
Imre Deakdccbea32015-06-22 23:35:51 +0300799 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800800 if (!intel_PLL_is_valid(dev, limit,
801 &clock))
802 continue;
803 if (match_clock &&
804 clock.p != match_clock->p)
805 continue;
806
807 this_err = abs(clock.dot - target);
808 if (this_err < err) {
809 *best_clock = clock;
810 err = this_err;
811 }
812 }
813 }
814 }
815 }
816
817 return (err != target);
818}
819
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200820/*
821 * Returns a set of divisors for the desired target clock with the given
822 * refclk, or FALSE. The returned values represent the clock equation:
823 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200824 *
825 * Target and reference clocks are specified in kHz.
826 *
827 * If match_clock is provided, then best_clock P divider must match the P
828 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200829 */
Ma Lingd4906092009-03-18 20:13:27 +0800830static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300831g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200832 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300833 int target, int refclk, struct dpll *match_clock,
834 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800835{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300836 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300837 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800838 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300839 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400840 /* approximately equals target * 0.00585 */
841 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800842
843 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300844
845 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
846
Ma Lingd4906092009-03-18 20:13:27 +0800847 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200848 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800849 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200850 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800851 for (clock.m1 = limit->m1.max;
852 clock.m1 >= limit->m1.min; clock.m1--) {
853 for (clock.m2 = limit->m2.max;
854 clock.m2 >= limit->m2.min; clock.m2--) {
855 for (clock.p1 = limit->p1.max;
856 clock.p1 >= limit->p1.min; clock.p1--) {
857 int this_err;
858
Imre Deakdccbea32015-06-22 23:35:51 +0300859 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000860 if (!intel_PLL_is_valid(dev, limit,
861 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800862 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000863
864 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800865 if (this_err < err_most) {
866 *best_clock = clock;
867 err_most = this_err;
868 max_n = clock.n;
869 found = true;
870 }
871 }
872 }
873 }
874 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800875 return found;
876}
Ma Lingd4906092009-03-18 20:13:27 +0800877
Imre Deakd5dd62b2015-03-17 11:40:03 +0200878/*
879 * Check if the calculated PLL configuration is more optimal compared to the
880 * best configuration and error found so far. Return the calculated error.
881 */
882static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300883 const struct dpll *calculated_clock,
884 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200885 unsigned int best_error_ppm,
886 unsigned int *error_ppm)
887{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200888 /*
889 * For CHV ignore the error and consider only the P value.
890 * Prefer a bigger P value based on HW requirements.
891 */
892 if (IS_CHERRYVIEW(dev)) {
893 *error_ppm = 0;
894
895 return calculated_clock->p > best_clock->p;
896 }
897
Imre Deak24be4e42015-03-17 11:40:04 +0200898 if (WARN_ON_ONCE(!target_freq))
899 return false;
900
Imre Deakd5dd62b2015-03-17 11:40:03 +0200901 *error_ppm = div_u64(1000000ULL *
902 abs(target_freq - calculated_clock->dot),
903 target_freq);
904 /*
905 * Prefer a better P value over a better (smaller) error if the error
906 * is small. Ensure this preference for future configurations too by
907 * setting the error to 0.
908 */
909 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
910 *error_ppm = 0;
911
912 return true;
913 }
914
915 return *error_ppm + 10 < best_error_ppm;
916}
917
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200918/*
919 * Returns a set of divisors for the desired target clock with the given
920 * refclk, or FALSE. The returned values represent the clock equation:
921 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
922 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800923static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300924vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200925 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300926 int target, int refclk, struct dpll *match_clock,
927 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700928{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200929 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300930 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300931 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300932 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300933 /* min update 19.2 MHz */
934 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300935 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700936
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300937 target *= 5; /* fast clock */
938
939 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700940
941 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300942 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300943 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300944 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300945 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300946 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700947 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300948 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200949 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300950
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300951 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
952 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300953
Imre Deakdccbea32015-06-22 23:35:51 +0300954 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300955
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300956 if (!intel_PLL_is_valid(dev, limit,
957 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300958 continue;
959
Imre Deakd5dd62b2015-03-17 11:40:03 +0200960 if (!vlv_PLL_is_optimal(dev, target,
961 &clock,
962 best_clock,
963 bestppm, &ppm))
964 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300965
Imre Deakd5dd62b2015-03-17 11:40:03 +0200966 *best_clock = clock;
967 bestppm = ppm;
968 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700969 }
970 }
971 }
972 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700973
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300974 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700975}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700976
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200977/*
978 * Returns a set of divisors for the desired target clock with the given
979 * refclk, or FALSE. The returned values represent the clock equation:
980 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
981 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300982static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300983chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200984 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300985 int target, int refclk, struct dpll *match_clock,
986 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300987{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200988 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300989 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200990 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300991 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300992 uint64_t m2;
993 int found = false;
994
995 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200996 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300997
998 /*
999 * Based on hardware doc, the n always set to 1, and m1 always
1000 * set to 2. If requires to support 200Mhz refclk, we need to
1001 * revisit this because n may not 1 anymore.
1002 */
1003 clock.n = 1, clock.m1 = 2;
1004 target *= 5; /* fast clock */
1005
1006 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1007 for (clock.p2 = limit->p2.p2_fast;
1008 clock.p2 >= limit->p2.p2_slow;
1009 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001010 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001011
1012 clock.p = clock.p1 * clock.p2;
1013
1014 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1015 clock.n) << 22, refclk * clock.m1);
1016
1017 if (m2 > INT_MAX/clock.m1)
1018 continue;
1019
1020 clock.m2 = m2;
1021
Imre Deakdccbea32015-06-22 23:35:51 +03001022 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001023
1024 if (!intel_PLL_is_valid(dev, limit, &clock))
1025 continue;
1026
Imre Deak9ca3ba02015-03-17 11:40:05 +02001027 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1028 best_error_ppm, &error_ppm))
1029 continue;
1030
1031 *best_clock = clock;
1032 best_error_ppm = error_ppm;
1033 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001034 }
1035 }
1036
1037 return found;
1038}
1039
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001040bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001041 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001042{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001043 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001044 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001045
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001046 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001047 target_clock, refclk, NULL, best_clock);
1048}
1049
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001050bool intel_crtc_active(struct drm_crtc *crtc)
1051{
1052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1053
1054 /* Be paranoid as we can arrive here with only partial
1055 * state retrieved from the hardware during setup.
1056 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001057 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001058 * as Haswell has gained clock readout/fastboot support.
1059 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001060 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001061 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001062 *
1063 * FIXME: The intel_crtc->active here should be switched to
1064 * crtc->state->active once we have proper CRTC states wired up
1065 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001066 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001067 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001068 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001069}
1070
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001071enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1072 enum pipe pipe)
1073{
1074 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1076
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001077 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001078}
1079
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001080static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1081{
1082 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001083 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001084 u32 line1, line2;
1085 u32 line_mask;
1086
1087 if (IS_GEN2(dev))
1088 line_mask = DSL_LINEMASK_GEN2;
1089 else
1090 line_mask = DSL_LINEMASK_GEN3;
1091
1092 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001093 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001094 line2 = I915_READ(reg) & line_mask;
1095
1096 return line1 == line2;
1097}
1098
Keith Packardab7ad7f2010-10-03 00:33:06 -07001099/*
1100 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001101 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001102 *
1103 * After disabling a pipe, we can't wait for vblank in the usual way,
1104 * spinning on the vblank interrupt status bit, since we won't actually
1105 * see an interrupt when the pipe is disabled.
1106 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001107 * On Gen4 and above:
1108 * wait for the pipe register state bit to turn off
1109 *
1110 * Otherwise:
1111 * wait for the display line value to settle (it usually
1112 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001113 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001114 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001115static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001116{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001117 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001118 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001119 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001120 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001121
Keith Packardab7ad7f2010-10-03 00:33:06 -07001122 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001123 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001124
Keith Packardab7ad7f2010-10-03 00:33:06 -07001125 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001126 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1127 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001128 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001129 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001130 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001131 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001132 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001133 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001134}
1135
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001137void assert_pll(struct drm_i915_private *dev_priv,
1138 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001139{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001140 u32 val;
1141 bool cur_state;
1142
Ville Syrjälä649636e2015-09-22 19:50:01 +03001143 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001144 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001145 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001146 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001147 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001149
Jani Nikula23538ef2013-08-27 15:12:22 +03001150/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001151void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001152{
1153 u32 val;
1154 bool cur_state;
1155
Ville Syrjäläa5805162015-05-26 20:42:30 +03001156 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001157 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001158 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001159
1160 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001161 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001162 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001163 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001164}
Jani Nikula23538ef2013-08-27 15:12:22 +03001165
Jesse Barnes040484a2011-01-03 12:14:26 -08001166static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1167 enum pipe pipe, bool state)
1168{
Jesse Barnes040484a2011-01-03 12:14:26 -08001169 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001170 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1171 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001172
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001173 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001174 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001175 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001176 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001177 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001178 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001179 cur_state = !!(val & FDI_TX_ENABLE);
1180 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001181 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001182 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001183 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001184}
1185#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1186#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1187
1188static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1189 enum pipe pipe, bool state)
1190{
Jesse Barnes040484a2011-01-03 12:14:26 -08001191 u32 val;
1192 bool cur_state;
1193
Ville Syrjälä649636e2015-09-22 19:50:01 +03001194 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001195 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001196 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001197 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001198 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001199}
1200#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1201#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1202
1203static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1204 enum pipe pipe)
1205{
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 u32 val;
1207
1208 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001209 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001210 return;
1211
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001212 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001213 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001214 return;
1215
Ville Syrjälä649636e2015-09-22 19:50:01 +03001216 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001217 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 -08001218}
1219
Daniel Vetter55607e82013-06-16 21:42:39 +02001220void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1221 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001222{
Jesse Barnes040484a2011-01-03 12:14:26 -08001223 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001224 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001225
Ville Syrjälä649636e2015-09-22 19:50:01 +03001226 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001227 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001228 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001229 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001230 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001231}
1232
Daniel Vetterb680c372014-09-19 18:27:27 +02001233void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1234 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001235{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001236 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001237 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001238 u32 val;
1239 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001240 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001241
Jani Nikulabedd4db2014-08-22 15:04:13 +03001242 if (WARN_ON(HAS_DDI(dev)))
1243 return;
1244
1245 if (HAS_PCH_SPLIT(dev)) {
1246 u32 port_sel;
1247
Jesse Barnesea0760c2011-01-04 15:09:32 -08001248 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001249 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1250
1251 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1252 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1253 panel_pipe = PIPE_B;
1254 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001255 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001256 /* presumably write lock depends on pipe, not port select */
1257 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1258 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001259 } else {
1260 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001261 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1262 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263 }
1264
1265 val = I915_READ(pp_reg);
1266 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001267 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001268 locked = false;
1269
Rob Clarke2c719b2014-12-15 13:56:32 -05001270 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001271 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001272 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001273}
1274
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001275static void assert_cursor(struct drm_i915_private *dev_priv,
1276 enum pipe pipe, bool state)
1277{
1278 struct drm_device *dev = dev_priv->dev;
1279 bool cur_state;
1280
Paulo Zanonid9d82082014-02-27 16:30:56 -03001281 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001282 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001283 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001284 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001285
Rob Clarke2c719b2014-12-15 13:56:32 -05001286 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001287 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001288 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001289}
1290#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1291#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1292
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001293void assert_pipe(struct drm_i915_private *dev_priv,
1294 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001295{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001296 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001297 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1298 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001299 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001300
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001301 /* if we need the pipe quirk it must be always on */
1302 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1303 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001304 state = true;
1305
Imre Deak4feed0e2016-02-12 18:55:14 +02001306 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1307 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001308 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001309 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001310
1311 intel_display_power_put(dev_priv, power_domain);
1312 } else {
1313 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001314 }
1315
Rob Clarke2c719b2014-12-15 13:56:32 -05001316 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001317 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001318 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001319}
1320
Chris Wilson931872f2012-01-16 23:01:13 +00001321static void assert_plane(struct drm_i915_private *dev_priv,
1322 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001323{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001324 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001325 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001326
Ville Syrjälä649636e2015-09-22 19:50:01 +03001327 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001328 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001329 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001330 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001331 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332}
1333
Chris Wilson931872f2012-01-16 23:01:13 +00001334#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1335#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1336
Jesse Barnesb24e7172011-01-04 15:09:30 -08001337static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1338 enum pipe pipe)
1339{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001340 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001341 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001342
Ville Syrjälä653e1022013-06-04 13:49:05 +03001343 /* Primary planes are fixed to pipes on gen4+ */
1344 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001345 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001346 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001347 "plane %c assertion failure, should be disabled but not\n",
1348 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001349 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001350 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001351
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001353 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001354 u32 val = I915_READ(DSPCNTR(i));
1355 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001357 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001358 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1359 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360 }
1361}
1362
Jesse Barnes19332d72013-03-28 09:55:38 -07001363static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1364 enum pipe pipe)
1365{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001366 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001367 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001368
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001369 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001370 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001371 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001372 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001373 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1374 sprite, pipe_name(pipe));
1375 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001376 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001377 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001378 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001380 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001381 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001382 }
1383 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001384 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001385 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001386 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001387 plane_name(pipe), pipe_name(pipe));
1388 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001389 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001390 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001391 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1392 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001393 }
1394}
1395
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001396static void assert_vblank_disabled(struct drm_crtc *crtc)
1397{
Rob Clarke2c719b2014-12-15 13:56:32 -05001398 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001399 drm_crtc_vblank_put(crtc);
1400}
1401
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001402void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1403 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001404{
Jesse Barnes92f25842011-01-04 15:09:34 -08001405 u32 val;
1406 bool enabled;
1407
Ville Syrjälä649636e2015-09-22 19:50:01 +03001408 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001409 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001410 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001411 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1412 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001413}
1414
Keith Packard4e634382011-08-06 10:39:45 -07001415static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1416 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001417{
1418 if ((val & DP_PORT_EN) == 0)
1419 return false;
1420
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001421 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001422 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001423 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1424 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001425 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001426 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1427 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001428 } else {
1429 if ((val & DP_PIPE_MASK) != (pipe << 30))
1430 return false;
1431 }
1432 return true;
1433}
1434
Keith Packard1519b992011-08-06 10:35:34 -07001435static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1436 enum pipe pipe, u32 val)
1437{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001438 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001439 return false;
1440
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001441 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001442 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001443 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001444 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001445 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1446 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001447 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001448 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001449 return false;
1450 }
1451 return true;
1452}
1453
1454static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1455 enum pipe pipe, u32 val)
1456{
1457 if ((val & LVDS_PORT_EN) == 0)
1458 return false;
1459
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001460 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001461 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1462 return false;
1463 } else {
1464 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1465 return false;
1466 }
1467 return true;
1468}
1469
1470static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1471 enum pipe pipe, u32 val)
1472{
1473 if ((val & ADPA_DAC_ENABLE) == 0)
1474 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001475 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001476 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1477 return false;
1478 } else {
1479 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1480 return false;
1481 }
1482 return true;
1483}
1484
Jesse Barnes291906f2011-02-02 12:28:03 -08001485static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001486 enum pipe pipe, i915_reg_t reg,
1487 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001488{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001489 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001490 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001491 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001492 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001493
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001494 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001495 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001496 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001497}
1498
1499static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001500 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001501{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001502 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001503 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001504 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001505 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001506
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001507 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001508 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001509 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001510}
1511
1512static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1513 enum pipe pipe)
1514{
Jesse Barnes291906f2011-02-02 12:28:03 -08001515 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001516
Keith Packardf0575e92011-07-25 22:12:43 -07001517 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1518 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001520
Ville Syrjälä649636e2015-09-22 19:50:01 +03001521 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001522 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001523 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001524 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001525
Ville Syrjälä649636e2015-09-22 19:50:01 +03001526 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001527 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001528 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001529 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001530
Paulo Zanonie2debe92013-02-18 19:00:27 -03001531 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1532 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001534}
1535
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001536static void _vlv_enable_pll(struct intel_crtc *crtc,
1537 const struct intel_crtc_state *pipe_config)
1538{
1539 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1540 enum pipe pipe = crtc->pipe;
1541
1542 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1543 POSTING_READ(DPLL(pipe));
1544 udelay(150);
1545
1546 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1547 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1548}
1549
Ville Syrjäläd288f652014-10-28 13:20:22 +02001550static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001551 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001552{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001553 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001554 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001555
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001556 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001557
Daniel Vetter87442f72013-06-06 00:52:17 +02001558 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001559 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001560
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001561 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1562 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001563
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001564 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1565 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001566}
1567
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001568
1569static void _chv_enable_pll(struct intel_crtc *crtc,
1570 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001571{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001572 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001573 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001575 u32 tmp;
1576
Ville Syrjäläa5805162015-05-26 20:42:30 +03001577 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001578
1579 /* Enable back the 10bit clock to display controller */
1580 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1581 tmp |= DPIO_DCLKP_EN;
1582 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1583
Ville Syrjälä54433e92015-05-26 20:42:31 +03001584 mutex_unlock(&dev_priv->sb_lock);
1585
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001586 /*
1587 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1588 */
1589 udelay(1);
1590
1591 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001592 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001593
1594 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001595 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001597}
1598
1599static void chv_enable_pll(struct intel_crtc *crtc,
1600 const struct intel_crtc_state *pipe_config)
1601{
1602 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1603 enum pipe pipe = crtc->pipe;
1604
1605 assert_pipe_disabled(dev_priv, pipe);
1606
1607 /* PLL is protected by panel, make sure we can write it */
1608 assert_panel_unlocked(dev_priv, pipe);
1609
1610 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1611 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001612
Ville Syrjäläc2317752016-03-15 16:39:56 +02001613 if (pipe != PIPE_A) {
1614 /*
1615 * WaPixelRepeatModeFixForC0:chv
1616 *
1617 * DPLLCMD is AWOL. Use chicken bits to propagate
1618 * the value from DPLLBMD to either pipe B or C.
1619 */
1620 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1621 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1622 I915_WRITE(CBR4_VLV, 0);
1623 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1624
1625 /*
1626 * DPLLB VGA mode also seems to cause problems.
1627 * We should always have it disabled.
1628 */
1629 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1630 } else {
1631 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1632 POSTING_READ(DPLL_MD(pipe));
1633 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001634}
1635
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001636static int intel_num_dvo_pipes(struct drm_device *dev)
1637{
1638 struct intel_crtc *crtc;
1639 int count = 0;
1640
1641 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001642 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001643 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001644
1645 return count;
1646}
1647
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001648static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001649{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001650 struct drm_device *dev = crtc->base.dev;
1651 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001652 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001653 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001654
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001655 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001656
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001657 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001658 if (IS_MOBILE(dev) && !IS_I830(dev))
1659 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001660
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001661 /* Enable DVO 2x clock on both PLLs if necessary */
1662 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1663 /*
1664 * It appears to be important that we don't enable this
1665 * for the current pipe before otherwise configuring the
1666 * PLL. No idea how this should be handled if multiple
1667 * DVO outputs are enabled simultaneosly.
1668 */
1669 dpll |= DPLL_DVO_2X_MODE;
1670 I915_WRITE(DPLL(!crtc->pipe),
1671 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1672 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001673
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001674 /*
1675 * Apparently we need to have VGA mode enabled prior to changing
1676 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1677 * dividers, even though the register value does change.
1678 */
1679 I915_WRITE(reg, 0);
1680
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001681 I915_WRITE(reg, dpll);
1682
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001683 /* Wait for the clocks to stabilize. */
1684 POSTING_READ(reg);
1685 udelay(150);
1686
1687 if (INTEL_INFO(dev)->gen >= 4) {
1688 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001689 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001690 } else {
1691 /* The pixel multiplier can only be updated once the
1692 * DPLL is enabled and the clocks are stable.
1693 *
1694 * So write it again.
1695 */
1696 I915_WRITE(reg, dpll);
1697 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001698
1699 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001701 POSTING_READ(reg);
1702 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001703 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001704 POSTING_READ(reg);
1705 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001706 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001707 POSTING_READ(reg);
1708 udelay(150); /* wait for warmup */
1709}
1710
1711/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001712 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001713 * @dev_priv: i915 private structure
1714 * @pipe: pipe PLL to disable
1715 *
1716 * Disable the PLL for @pipe, making sure the pipe is off first.
1717 *
1718 * Note! This is for pre-ILK only.
1719 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001720static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001721{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001722 struct drm_device *dev = crtc->base.dev;
1723 struct drm_i915_private *dev_priv = dev->dev_private;
1724 enum pipe pipe = crtc->pipe;
1725
1726 /* Disable DVO 2x clock on both PLLs if necessary */
1727 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001728 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001729 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001730 I915_WRITE(DPLL(PIPE_B),
1731 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1732 I915_WRITE(DPLL(PIPE_A),
1733 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1734 }
1735
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001736 /* Don't disable pipe or pipe PLLs if needed */
1737 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1738 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001739 return;
1740
1741 /* Make sure the pipe isn't still relying on us */
1742 assert_pipe_disabled(dev_priv, pipe);
1743
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001744 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001745 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746}
1747
Jesse Barnesf6071162013-10-01 10:41:38 -07001748static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1749{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001750 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001751
1752 /* Make sure the pipe isn't still relying on us */
1753 assert_pipe_disabled(dev_priv, pipe);
1754
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001755 val = DPLL_INTEGRATED_REF_CLK_VLV |
1756 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1757 if (pipe != PIPE_A)
1758 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1759
Jesse Barnesf6071162013-10-01 10:41:38 -07001760 I915_WRITE(DPLL(pipe), val);
1761 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001762}
1763
1764static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1765{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001766 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001767 u32 val;
1768
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001769 /* Make sure the pipe isn't still relying on us */
1770 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001771
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001772 val = DPLL_SSC_REF_CLK_CHV |
1773 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001774 if (pipe != PIPE_A)
1775 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001776
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001777 I915_WRITE(DPLL(pipe), val);
1778 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001779
Ville Syrjäläa5805162015-05-26 20:42:30 +03001780 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001781
1782 /* Disable 10bit clock to display controller */
1783 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1784 val &= ~DPIO_DCLKP_EN;
1785 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1786
Ville Syrjäläa5805162015-05-26 20:42:30 +03001787 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001788}
1789
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001790void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001791 struct intel_digital_port *dport,
1792 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001793{
1794 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001795 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001796
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001797 switch (dport->port) {
1798 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001799 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001800 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001801 break;
1802 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001803 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001804 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001805 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001806 break;
1807 case PORT_D:
1808 port_mask = DPLL_PORTD_READY_MASK;
1809 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001810 break;
1811 default:
1812 BUG();
1813 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001814
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001815 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1816 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1817 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001818}
1819
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001820static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1821 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001822{
Daniel Vetter23670b322012-11-01 09:15:30 +01001823 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001824 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001826 i915_reg_t reg;
1827 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001828
Jesse Barnes040484a2011-01-03 12:14:26 -08001829 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001830 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001831
1832 /* FDI must be feeding us bits for PCH ports */
1833 assert_fdi_tx_enabled(dev_priv, pipe);
1834 assert_fdi_rx_enabled(dev_priv, pipe);
1835
Daniel Vetter23670b322012-11-01 09:15:30 +01001836 if (HAS_PCH_CPT(dev)) {
1837 /* Workaround: Set the timing override bit before enabling the
1838 * pch transcoder. */
1839 reg = TRANS_CHICKEN2(pipe);
1840 val = I915_READ(reg);
1841 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1842 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001843 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001844
Daniel Vetterab9412b2013-05-03 11:49:46 +02001845 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001846 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001847 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001848
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001849 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001850 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001851 * Make the BPC in transcoder be consistent with
1852 * that in pipeconf reg. For HDMI we must use 8bpc
1853 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001854 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001855 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001856 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1857 val |= PIPECONF_8BPC;
1858 else
1859 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001860 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001861
1862 val &= ~TRANS_INTERLACE_MASK;
1863 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001864 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001865 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001866 val |= TRANS_LEGACY_INTERLACED_ILK;
1867 else
1868 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001869 else
1870 val |= TRANS_PROGRESSIVE;
1871
Jesse Barnes040484a2011-01-03 12:14:26 -08001872 I915_WRITE(reg, val | TRANS_ENABLE);
1873 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001874 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001875}
1876
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001878 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001879{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001880 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001881
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001882 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001883 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001884 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001885
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001886 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001887 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001888 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001889 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001890
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001891 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001892 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001893
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001894 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1895 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001896 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001897 else
1898 val |= TRANS_PROGRESSIVE;
1899
Daniel Vetterab9412b2013-05-03 11:49:46 +02001900 I915_WRITE(LPT_TRANSCONF, val);
1901 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001902 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903}
1904
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001905static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1906 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001907{
Daniel Vetter23670b322012-11-01 09:15:30 +01001908 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001909 i915_reg_t reg;
1910 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001911
1912 /* FDI relies on the transcoder */
1913 assert_fdi_tx_disabled(dev_priv, pipe);
1914 assert_fdi_rx_disabled(dev_priv, pipe);
1915
Jesse Barnes291906f2011-02-02 12:28:03 -08001916 /* Ports must be off as well */
1917 assert_pch_ports_disabled(dev_priv, pipe);
1918
Daniel Vetterab9412b2013-05-03 11:49:46 +02001919 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001920 val = I915_READ(reg);
1921 val &= ~TRANS_ENABLE;
1922 I915_WRITE(reg, val);
1923 /* wait for PCH transcoder off, transcoder state */
1924 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001925 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001926
Ville Syrjäläc4656132015-10-29 21:25:56 +02001927 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001928 /* Workaround: Clear the timing override chicken bit again. */
1929 reg = TRANS_CHICKEN2(pipe);
1930 val = I915_READ(reg);
1931 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1932 I915_WRITE(reg, val);
1933 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001934}
1935
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001936static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001937{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001938 u32 val;
1939
Daniel Vetterab9412b2013-05-03 11:49:46 +02001940 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001941 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001942 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001943 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001944 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001945 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001946
1947 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001948 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001949 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001950 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001951}
1952
1953/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001954 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001955 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001957 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001958 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001959 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001960static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001961{
Paulo Zanoni03722642014-01-17 13:51:09 -02001962 struct drm_device *dev = crtc->base.dev;
1963 struct drm_i915_private *dev_priv = dev->dev_private;
1964 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001965 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001966 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001967 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001968 u32 val;
1969
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001970 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1971
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001972 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001973 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001974 assert_sprites_disabled(dev_priv, pipe);
1975
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001976 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001977 pch_transcoder = TRANSCODER_A;
1978 else
1979 pch_transcoder = pipe;
1980
Jesse Barnesb24e7172011-01-04 15:09:30 -08001981 /*
1982 * A pipe without a PLL won't actually be able to drive bits from
1983 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1984 * need the check.
1985 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001986 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001987 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001988 assert_dsi_pll_enabled(dev_priv);
1989 else
1990 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001991 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001992 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001993 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001994 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001995 assert_fdi_tx_pll_enabled(dev_priv,
1996 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001997 }
1998 /* FIXME: assert CPU port conditions for SNB+ */
1999 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002000
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002001 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002003 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002004 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2005 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002006 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002007 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002008
2009 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002010 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002011
2012 /*
2013 * Until the pipe starts DSL will read as 0, which would cause
2014 * an apparent vblank timestamp jump, which messes up also the
2015 * frame count when it's derived from the timestamps. So let's
2016 * wait for the pipe to start properly before we call
2017 * drm_crtc_vblank_on()
2018 */
2019 if (dev->max_vblank_count == 0 &&
2020 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2021 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002022}
2023
2024/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002025 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002026 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002028 * Disable the pipe of @crtc, making sure that various hardware
2029 * specific requirements are met, if applicable, e.g. plane
2030 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002031 *
2032 * Will wait until the pipe has shut down before returning.
2033 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002034static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002035{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002036 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002037 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002038 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002039 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002040 u32 val;
2041
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002042 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2043
Jesse Barnesb24e7172011-01-04 15:09:30 -08002044 /*
2045 * Make sure planes won't keep trying to pump pixels to us,
2046 * or we might hang the display.
2047 */
2048 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002049 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002050 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002052 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002053 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002054 if ((val & PIPECONF_ENABLE) == 0)
2055 return;
2056
Ville Syrjälä67adc642014-08-15 01:21:57 +03002057 /*
2058 * Double wide has implications for planes
2059 * so best keep it disabled when not needed.
2060 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002061 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002062 val &= ~PIPECONF_DOUBLE_WIDE;
2063
2064 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002065 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2066 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002067 val &= ~PIPECONF_ENABLE;
2068
2069 I915_WRITE(reg, val);
2070 if ((val & PIPECONF_ENABLE) == 0)
2071 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002072}
2073
Chris Wilson693db182013-03-05 14:52:39 +00002074static bool need_vtd_wa(struct drm_device *dev)
2075{
2076#ifdef CONFIG_INTEL_IOMMU
2077 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2078 return true;
2079#endif
2080 return false;
2081}
2082
Ville Syrjälä832be822016-01-12 21:08:33 +02002083static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2084{
2085 return IS_GEN2(dev_priv) ? 2048 : 4096;
2086}
2087
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002088static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2089 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002090{
2091 switch (fb_modifier) {
2092 case DRM_FORMAT_MOD_NONE:
2093 return cpp;
2094 case I915_FORMAT_MOD_X_TILED:
2095 if (IS_GEN2(dev_priv))
2096 return 128;
2097 else
2098 return 512;
2099 case I915_FORMAT_MOD_Y_TILED:
2100 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2101 return 128;
2102 else
2103 return 512;
2104 case I915_FORMAT_MOD_Yf_TILED:
2105 switch (cpp) {
2106 case 1:
2107 return 64;
2108 case 2:
2109 case 4:
2110 return 128;
2111 case 8:
2112 case 16:
2113 return 256;
2114 default:
2115 MISSING_CASE(cpp);
2116 return cpp;
2117 }
2118 break;
2119 default:
2120 MISSING_CASE(fb_modifier);
2121 return cpp;
2122 }
2123}
2124
Ville Syrjälä832be822016-01-12 21:08:33 +02002125unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2126 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002127{
Ville Syrjälä832be822016-01-12 21:08:33 +02002128 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2129 return 1;
2130 else
2131 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002132 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002133}
2134
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002135/* Return the tile dimensions in pixel units */
2136static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2137 unsigned int *tile_width,
2138 unsigned int *tile_height,
2139 uint64_t fb_modifier,
2140 unsigned int cpp)
2141{
2142 unsigned int tile_width_bytes =
2143 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2144
2145 *tile_width = tile_width_bytes / cpp;
2146 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2147}
2148
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002149unsigned int
2150intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002151 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002152{
Ville Syrjälä832be822016-01-12 21:08:33 +02002153 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2154 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2155
2156 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002157}
2158
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002159unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2160{
2161 unsigned int size = 0;
2162 int i;
2163
2164 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2165 size += rot_info->plane[i].width * rot_info->plane[i].height;
2166
2167 return size;
2168}
2169
Daniel Vetter75c82a52015-10-14 16:51:04 +02002170static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002171intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2172 const struct drm_framebuffer *fb,
2173 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002174{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002175 if (intel_rotation_90_or_270(rotation)) {
2176 *view = i915_ggtt_view_rotated;
2177 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2178 } else {
2179 *view = i915_ggtt_view_normal;
2180 }
2181}
2182
2183static void
2184intel_fill_fb_info(struct drm_i915_private *dev_priv,
2185 struct drm_framebuffer *fb)
2186{
2187 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002188 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002189
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002190 tile_size = intel_tile_size(dev_priv);
2191
2192 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002193 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2194 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002195
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002196 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2197 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002198
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002199 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002200 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002201 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2202 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002203
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002204 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002205 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2206 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002207 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002208}
2209
Ville Syrjälä603525d2016-01-12 21:08:37 +02002210static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002211{
2212 if (INTEL_INFO(dev_priv)->gen >= 9)
2213 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002214 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002215 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002216 return 128 * 1024;
2217 else if (INTEL_INFO(dev_priv)->gen >= 4)
2218 return 4 * 1024;
2219 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002220 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002221}
2222
Ville Syrjälä603525d2016-01-12 21:08:37 +02002223static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2224 uint64_t fb_modifier)
2225{
2226 switch (fb_modifier) {
2227 case DRM_FORMAT_MOD_NONE:
2228 return intel_linear_alignment(dev_priv);
2229 case I915_FORMAT_MOD_X_TILED:
2230 if (INTEL_INFO(dev_priv)->gen >= 9)
2231 return 256 * 1024;
2232 return 0;
2233 case I915_FORMAT_MOD_Y_TILED:
2234 case I915_FORMAT_MOD_Yf_TILED:
2235 return 1 * 1024 * 1024;
2236 default:
2237 MISSING_CASE(fb_modifier);
2238 return 0;
2239 }
2240}
2241
Chris Wilson127bd2a2010-07-23 23:32:05 +01002242int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002243intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2244 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002245{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002246 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002247 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002248 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002249 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002250 u32 alignment;
2251 int ret;
2252
Matt Roperebcdd392014-07-09 16:22:11 -07002253 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2254
Ville Syrjälä603525d2016-01-12 21:08:37 +02002255 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002256
Ville Syrjälä3465c582016-02-15 22:54:43 +02002257 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002258
Chris Wilson693db182013-03-05 14:52:39 +00002259 /* Note that the w/a also requires 64 PTE of padding following the
2260 * bo. We currently fill all unused PTE with the shadow page and so
2261 * we should always have valid PTE following the scanout preventing
2262 * the VT-d warning.
2263 */
2264 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2265 alignment = 256 * 1024;
2266
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002267 /*
2268 * Global gtt pte registers are special registers which actually forward
2269 * writes to a chunk of system memory. Which means that there is no risk
2270 * that the register values disappear as soon as we call
2271 * intel_runtime_pm_put(), so it is correct to wrap only the
2272 * pin/unpin/fence and not more.
2273 */
2274 intel_runtime_pm_get(dev_priv);
2275
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002276 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2277 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002278 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002279 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002280
2281 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2282 * fence, whereas 965+ only requires a fence if using
2283 * framebuffer compression. For simplicity, we always install
2284 * a fence as the cost is not that onerous.
2285 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002286 if (view.type == I915_GGTT_VIEW_NORMAL) {
2287 ret = i915_gem_object_get_fence(obj);
2288 if (ret == -EDEADLK) {
2289 /*
2290 * -EDEADLK means there are no free fences
2291 * no pending flips.
2292 *
2293 * This is propagated to atomic, but it uses
2294 * -EDEADLK to force a locking recovery, so
2295 * change the returned error to -EBUSY.
2296 */
2297 ret = -EBUSY;
2298 goto err_unpin;
2299 } else if (ret)
2300 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002301
Vivek Kasireddy98072162015-10-29 18:54:38 -07002302 i915_gem_object_pin_fence(obj);
2303 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002304
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002305 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002306 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002307
2308err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002309 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002310err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002311 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002312 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002313}
2314
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002315void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002316{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002317 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002318 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002319
Matt Roperebcdd392014-07-09 16:22:11 -07002320 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2321
Ville Syrjälä3465c582016-02-15 22:54:43 +02002322 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002323
Vivek Kasireddy98072162015-10-29 18:54:38 -07002324 if (view.type == I915_GGTT_VIEW_NORMAL)
2325 i915_gem_object_unpin_fence(obj);
2326
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002327 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002328}
2329
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002330/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002331 * Adjust the tile offset by moving the difference into
2332 * the x/y offsets.
2333 *
2334 * Input tile dimensions and pitch must already be
2335 * rotated to match x and y, and in pixel units.
2336 */
2337static u32 intel_adjust_tile_offset(int *x, int *y,
2338 unsigned int tile_width,
2339 unsigned int tile_height,
2340 unsigned int tile_size,
2341 unsigned int pitch_tiles,
2342 u32 old_offset,
2343 u32 new_offset)
2344{
2345 unsigned int tiles;
2346
2347 WARN_ON(old_offset & (tile_size - 1));
2348 WARN_ON(new_offset & (tile_size - 1));
2349 WARN_ON(new_offset > old_offset);
2350
2351 tiles = (old_offset - new_offset) / tile_size;
2352
2353 *y += tiles / pitch_tiles * tile_height;
2354 *x += tiles % pitch_tiles * tile_width;
2355
2356 return new_offset;
2357}
2358
2359/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002360 * Computes the linear offset to the base tile and adjusts
2361 * x, y. bytes per pixel is assumed to be a power-of-two.
2362 *
2363 * In the 90/270 rotated case, x and y are assumed
2364 * to be already rotated to match the rotated GTT view, and
2365 * pitch is the tile_height aligned framebuffer height.
2366 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002367u32 intel_compute_tile_offset(int *x, int *y,
2368 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002369 unsigned int pitch,
2370 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002371{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002372 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2373 uint64_t fb_modifier = fb->modifier[plane];
2374 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002375 u32 offset, offset_aligned, alignment;
2376
2377 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2378 if (alignment)
2379 alignment--;
2380
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002381 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002382 unsigned int tile_size, tile_width, tile_height;
2383 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002384
Ville Syrjäläd8433102016-01-12 21:08:35 +02002385 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002386 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2387 fb_modifier, cpp);
2388
2389 if (intel_rotation_90_or_270(rotation)) {
2390 pitch_tiles = pitch / tile_height;
2391 swap(tile_width, tile_height);
2392 } else {
2393 pitch_tiles = pitch / (tile_width * cpp);
2394 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002395
Ville Syrjäläd8433102016-01-12 21:08:35 +02002396 tile_rows = *y / tile_height;
2397 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002398
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002399 tiles = *x / tile_width;
2400 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002401
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002402 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2403 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002404
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002405 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2406 tile_size, pitch_tiles,
2407 offset, offset_aligned);
2408 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002409 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002410 offset_aligned = offset & ~alignment;
2411
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002412 *y = (offset & alignment) / pitch;
2413 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002414 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002415
2416 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002417}
2418
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002419static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002420{
2421 switch (format) {
2422 case DISPPLANE_8BPP:
2423 return DRM_FORMAT_C8;
2424 case DISPPLANE_BGRX555:
2425 return DRM_FORMAT_XRGB1555;
2426 case DISPPLANE_BGRX565:
2427 return DRM_FORMAT_RGB565;
2428 default:
2429 case DISPPLANE_BGRX888:
2430 return DRM_FORMAT_XRGB8888;
2431 case DISPPLANE_RGBX888:
2432 return DRM_FORMAT_XBGR8888;
2433 case DISPPLANE_BGRX101010:
2434 return DRM_FORMAT_XRGB2101010;
2435 case DISPPLANE_RGBX101010:
2436 return DRM_FORMAT_XBGR2101010;
2437 }
2438}
2439
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002440static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2441{
2442 switch (format) {
2443 case PLANE_CTL_FORMAT_RGB_565:
2444 return DRM_FORMAT_RGB565;
2445 default:
2446 case PLANE_CTL_FORMAT_XRGB_8888:
2447 if (rgb_order) {
2448 if (alpha)
2449 return DRM_FORMAT_ABGR8888;
2450 else
2451 return DRM_FORMAT_XBGR8888;
2452 } else {
2453 if (alpha)
2454 return DRM_FORMAT_ARGB8888;
2455 else
2456 return DRM_FORMAT_XRGB8888;
2457 }
2458 case PLANE_CTL_FORMAT_XRGB_2101010:
2459 if (rgb_order)
2460 return DRM_FORMAT_XBGR2101010;
2461 else
2462 return DRM_FORMAT_XRGB2101010;
2463 }
2464}
2465
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002466static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002467intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2468 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002469{
2470 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002471 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002472 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002473 struct drm_i915_gem_object *obj = NULL;
2474 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002475 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002476 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2477 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2478 PAGE_SIZE);
2479
2480 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481
Chris Wilsonff2652e2014-03-10 08:07:02 +00002482 if (plane_config->size == 0)
2483 return false;
2484
Paulo Zanoni3badb492015-09-23 12:52:23 -03002485 /* If the FB is too big, just don't use it since fbdev is not very
2486 * important and we should probably use that space with FBC or other
2487 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002488 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002489 return false;
2490
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002491 mutex_lock(&dev->struct_mutex);
2492
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002493 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2494 base_aligned,
2495 base_aligned,
2496 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002497 if (!obj) {
2498 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002499 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002500 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002501
Damien Lespiau49af4492015-01-20 12:51:44 +00002502 obj->tiling_mode = plane_config->tiling;
2503 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002504 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002505
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002506 mode_cmd.pixel_format = fb->pixel_format;
2507 mode_cmd.width = fb->width;
2508 mode_cmd.height = fb->height;
2509 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002510 mode_cmd.modifier[0] = fb->modifier[0];
2511 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002512
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002513 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002514 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002515 DRM_DEBUG_KMS("intel fb init failed\n");
2516 goto out_unref_obj;
2517 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002518
Jesse Barnes46f297f2014-03-07 08:57:48 -08002519 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002520
Daniel Vetterf6936e22015-03-26 12:17:05 +01002521 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002522 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002523
2524out_unref_obj:
2525 drm_gem_object_unreference(&obj->base);
2526 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002527 return false;
2528}
2529
Matt Roperafd65eb2015-02-03 13:10:04 -08002530/* Update plane->state->fb to match plane->fb after driver-internal updates */
2531static void
2532update_state_fb(struct drm_plane *plane)
2533{
2534 if (plane->fb == plane->state->fb)
2535 return;
2536
2537 if (plane->state->fb)
2538 drm_framebuffer_unreference(plane->state->fb);
2539 plane->state->fb = plane->fb;
2540 if (plane->state->fb)
2541 drm_framebuffer_reference(plane->state->fb);
2542}
2543
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002544static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002545intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2546 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002547{
2548 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002549 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550 struct drm_crtc *c;
2551 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002552 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002553 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002554 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002555 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2556 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002557 struct intel_plane_state *intel_state =
2558 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002559 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002560
Damien Lespiau2d140302015-02-05 17:22:18 +00002561 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562 return;
2563
Daniel Vetterf6936e22015-03-26 12:17:05 +01002564 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002565 fb = &plane_config->fb->base;
2566 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002567 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002568
Damien Lespiau2d140302015-02-05 17:22:18 +00002569 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002570
2571 /*
2572 * Failed to alloc the obj, check to see if we should share
2573 * an fb with another CRTC instead
2574 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002575 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002576 i = to_intel_crtc(c);
2577
2578 if (c == &intel_crtc->base)
2579 continue;
2580
Matt Roper2ff8fde2014-07-08 07:50:07 -07002581 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002582 continue;
2583
Daniel Vetter88595ac2015-03-26 12:42:24 +01002584 fb = c->primary->fb;
2585 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002586 continue;
2587
Daniel Vetter88595ac2015-03-26 12:42:24 +01002588 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002589 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002590 drm_framebuffer_reference(fb);
2591 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002592 }
2593 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002594
Matt Roper200757f2015-12-03 11:37:36 -08002595 /*
2596 * We've failed to reconstruct the BIOS FB. Current display state
2597 * indicates that the primary plane is visible, but has a NULL FB,
2598 * which will lead to problems later if we don't fix it up. The
2599 * simplest solution is to just disable the primary plane now and
2600 * pretend the BIOS never had it enabled.
2601 */
2602 to_intel_plane_state(plane_state)->visible = false;
2603 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002604 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002605 intel_plane->disable_plane(primary, &intel_crtc->base);
2606
Daniel Vetter88595ac2015-03-26 12:42:24 +01002607 return;
2608
2609valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002610 plane_state->src_x = 0;
2611 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002612 plane_state->src_w = fb->width << 16;
2613 plane_state->src_h = fb->height << 16;
2614
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002615 plane_state->crtc_x = 0;
2616 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002617 plane_state->crtc_w = fb->width;
2618 plane_state->crtc_h = fb->height;
2619
Matt Roper0a8d8a82015-12-03 11:37:38 -08002620 intel_state->src.x1 = plane_state->src_x;
2621 intel_state->src.y1 = plane_state->src_y;
2622 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2623 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2624 intel_state->dst.x1 = plane_state->crtc_x;
2625 intel_state->dst.y1 = plane_state->crtc_y;
2626 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2627 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2628
Daniel Vetter88595ac2015-03-26 12:42:24 +01002629 obj = intel_fb_obj(fb);
2630 if (obj->tiling_mode != I915_TILING_NONE)
2631 dev_priv->preserve_bios_swizzle = true;
2632
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002633 drm_framebuffer_reference(fb);
2634 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002635 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002636 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002637 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002638}
2639
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002640static void i9xx_update_primary_plane(struct drm_plane *primary,
2641 const struct intel_crtc_state *crtc_state,
2642 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002643{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002644 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002645 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2647 struct drm_framebuffer *fb = plane_state->base.fb;
2648 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002649 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002650 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002651 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002652 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002653 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002654 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002655 int x = plane_state->src.x1 >> 16;
2656 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002657
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002658 dspcntr = DISPPLANE_GAMMA_ENABLE;
2659
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002660 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002661
2662 if (INTEL_INFO(dev)->gen < 4) {
2663 if (intel_crtc->pipe == PIPE_B)
2664 dspcntr |= DISPPLANE_SEL_PIPE_B;
2665
2666 /* pipesrc and dspsize control the size that is scaled from,
2667 * which should always be the user's requested size.
2668 */
2669 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002670 ((crtc_state->pipe_src_h - 1) << 16) |
2671 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002672 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002673 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2674 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002675 ((crtc_state->pipe_src_h - 1) << 16) |
2676 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002677 I915_WRITE(PRIMPOS(plane), 0);
2678 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002679 }
2680
Ville Syrjälä57779d02012-10-31 17:50:14 +02002681 switch (fb->pixel_format) {
2682 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002683 dspcntr |= DISPPLANE_8BPP;
2684 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002685 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002686 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002687 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002688 case DRM_FORMAT_RGB565:
2689 dspcntr |= DISPPLANE_BGRX565;
2690 break;
2691 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002692 dspcntr |= DISPPLANE_BGRX888;
2693 break;
2694 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 dspcntr |= DISPPLANE_RGBX888;
2696 break;
2697 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 dspcntr |= DISPPLANE_BGRX101010;
2699 break;
2700 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002702 break;
2703 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002704 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002705 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002706
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002707 if (INTEL_INFO(dev)->gen >= 4 &&
2708 obj->tiling_mode != I915_TILING_NONE)
2709 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002710
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002711 if (IS_G4X(dev))
2712 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2713
Ville Syrjäläac484962016-01-20 21:05:26 +02002714 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002715
Daniel Vetterc2c75132012-07-05 12:17:30 +02002716 if (INTEL_INFO(dev)->gen >= 4) {
2717 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002718 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002719 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002720 linear_offset -= intel_crtc->dspaddr_offset;
2721 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002722 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002723 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002724
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002725 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302726 dspcntr |= DISPPLANE_ROTATE_180;
2727
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002728 x += (crtc_state->pipe_src_w - 1);
2729 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302730
2731 /* Finding the last pixel of the last line of the display
2732 data and adding to linear_offset*/
2733 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002734 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002735 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302736 }
2737
Paulo Zanoni2db33662015-09-14 15:20:03 -03002738 intel_crtc->adjusted_x = x;
2739 intel_crtc->adjusted_y = y;
2740
Sonika Jindal48404c12014-08-22 14:06:04 +05302741 I915_WRITE(reg, dspcntr);
2742
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002743 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002744 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002745 I915_WRITE(DSPSURF(plane),
2746 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002747 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002748 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002749 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002750 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002751 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002752}
2753
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002754static void i9xx_disable_primary_plane(struct drm_plane *primary,
2755 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002756{
2757 struct drm_device *dev = crtc->dev;
2758 struct drm_i915_private *dev_priv = dev->dev_private;
2759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002760 int plane = intel_crtc->plane;
2761
2762 I915_WRITE(DSPCNTR(plane), 0);
2763 if (INTEL_INFO(dev_priv)->gen >= 4)
2764 I915_WRITE(DSPSURF(plane), 0);
2765 else
2766 I915_WRITE(DSPADDR(plane), 0);
2767 POSTING_READ(DSPCNTR(plane));
2768}
2769
2770static void ironlake_update_primary_plane(struct drm_plane *primary,
2771 const struct intel_crtc_state *crtc_state,
2772 const struct intel_plane_state *plane_state)
2773{
2774 struct drm_device *dev = primary->dev;
2775 struct drm_i915_private *dev_priv = dev->dev_private;
2776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2777 struct drm_framebuffer *fb = plane_state->base.fb;
2778 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002779 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002780 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002782 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002783 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002784 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002785 int x = plane_state->src.x1 >> 16;
2786 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002787
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002788 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002789 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002790
2791 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2792 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2793
Ville Syrjälä57779d02012-10-31 17:50:14 +02002794 switch (fb->pixel_format) {
2795 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002796 dspcntr |= DISPPLANE_8BPP;
2797 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002798 case DRM_FORMAT_RGB565:
2799 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002800 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002801 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 dspcntr |= DISPPLANE_BGRX888;
2803 break;
2804 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 dspcntr |= DISPPLANE_RGBX888;
2806 break;
2807 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 dspcntr |= DISPPLANE_BGRX101010;
2809 break;
2810 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812 break;
2813 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002814 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002815 }
2816
2817 if (obj->tiling_mode != I915_TILING_NONE)
2818 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002819
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002820 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002821 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822
Ville Syrjäläac484962016-01-20 21:05:26 +02002823 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002824 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002825 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002826 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002827 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002828 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302829 dspcntr |= DISPPLANE_ROTATE_180;
2830
2831 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002832 x += (crtc_state->pipe_src_w - 1);
2833 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302834
2835 /* Finding the last pixel of the last line of the display
2836 data and adding to linear_offset*/
2837 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002838 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002839 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302840 }
2841 }
2842
Paulo Zanoni2db33662015-09-14 15:20:03 -03002843 intel_crtc->adjusted_x = x;
2844 intel_crtc->adjusted_y = y;
2845
Sonika Jindal48404c12014-08-22 14:06:04 +05302846 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002847
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002848 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002849 I915_WRITE(DSPSURF(plane),
2850 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002851 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002852 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2853 } else {
2854 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2855 I915_WRITE(DSPLINOFF(plane), linear_offset);
2856 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002857 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002858}
2859
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002860u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2861 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002862{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002863 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2864 return 64;
2865 } else {
2866 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002867
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002868 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002869 }
2870}
2871
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002872u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2873 struct drm_i915_gem_object *obj,
2874 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002875{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002876 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002877 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002878 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002879
Ville Syrjäläe7941292016-01-19 18:23:17 +02002880 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002881 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002882
Daniel Vetterce7f1722015-10-14 16:51:06 +02002883 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002884 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002885 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002886 return -1;
2887
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002888 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002889
2890 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002891 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002892 PAGE_SIZE;
2893 }
2894
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002895 WARN_ON(upper_32_bits(offset));
2896
2897 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002898}
2899
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002900static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2901{
2902 struct drm_device *dev = intel_crtc->base.dev;
2903 struct drm_i915_private *dev_priv = dev->dev_private;
2904
2905 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2906 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2907 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002908}
2909
Chandra Kondurua1b22782015-04-07 15:28:45 -07002910/*
2911 * This function detaches (aka. unbinds) unused scalers in hardware
2912 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002913static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002914{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002915 struct intel_crtc_scaler_state *scaler_state;
2916 int i;
2917
Chandra Kondurua1b22782015-04-07 15:28:45 -07002918 scaler_state = &intel_crtc->config->scaler_state;
2919
2920 /* loop through and disable scalers that aren't in use */
2921 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002922 if (!scaler_state->scalers[i].in_use)
2923 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002924 }
2925}
2926
Chandra Konduru6156a452015-04-27 13:48:39 -07002927u32 skl_plane_ctl_format(uint32_t pixel_format)
2928{
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002930 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002933 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002934 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002935 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002937 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002938 /*
2939 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2940 * to be already pre-multiplied. We need to add a knob (or a different
2941 * DRM_FORMAT) for user-space to configure that.
2942 */
2943 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002950 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002954 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002962 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002964
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966}
2967
2968u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2969{
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 switch (fb_modifier) {
2971 case DRM_FORMAT_MOD_NONE:
2972 break;
2973 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 default:
2980 MISSING_CASE(fb_modifier);
2981 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002982
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002983 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002984}
2985
2986u32 skl_plane_ctl_rotation(unsigned int rotation)
2987{
Chandra Konduru6156a452015-04-27 13:48:39 -07002988 switch (rotation) {
2989 case BIT(DRM_ROTATE_0):
2990 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302991 /*
2992 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2993 * while i915 HW rotation is clockwise, thats why this swapping.
2994 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302996 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002998 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002999 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303000 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 default:
3002 MISSING_CASE(rotation);
3003 }
3004
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003005 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006}
3007
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003008static void skylake_update_primary_plane(struct drm_plane *plane,
3009 const struct intel_crtc_state *crtc_state,
3010 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003011{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003012 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003013 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3015 struct drm_framebuffer *fb = plane_state->base.fb;
3016 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003017 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303018 u32 plane_ctl, stride_div, stride;
3019 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303021 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003022 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003023 int scaler_id = plane_state->scaler_id;
3024 int src_x = plane_state->src.x1 >> 16;
3025 int src_y = plane_state->src.y1 >> 16;
3026 int src_w = drm_rect_width(&plane_state->src) >> 16;
3027 int src_h = drm_rect_height(&plane_state->src) >> 16;
3028 int dst_x = plane_state->dst.x1;
3029 int dst_y = plane_state->dst.y1;
3030 int dst_w = drm_rect_width(&plane_state->dst);
3031 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003032
3033 plane_ctl = PLANE_CTL_ENABLE |
3034 PLANE_CTL_PIPE_GAMMA_ENABLE |
3035 PLANE_CTL_PIPE_CSC_ENABLE;
3036
Chandra Konduru6156a452015-04-27 13:48:39 -07003037 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3038 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003039 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003040 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003041
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003042 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003043 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003044 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303045
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003046 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003047
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303048 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003049 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3050
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303051 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003052 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303053 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003054 x_offset = stride * tile_height - src_y - src_h;
3055 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003056 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303057 } else {
3058 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003059 x_offset = src_x;
3060 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003061 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303062 }
3063 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003064
Paulo Zanoni2db33662015-09-14 15:20:03 -03003065 intel_crtc->adjusted_x = x_offset;
3066 intel_crtc->adjusted_y = y_offset;
3067
Damien Lespiau70d21f02013-07-03 21:06:04 +01003068 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303069 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3070 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3071 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003072
3073 if (scaler_id >= 0) {
3074 uint32_t ps_ctrl = 0;
3075
3076 WARN_ON(!dst_w || !dst_h);
3077 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3078 crtc_state->scaler_state.scalers[scaler_id].mode;
3079 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3080 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3081 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3082 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3083 I915_WRITE(PLANE_POS(pipe, 0), 0);
3084 } else {
3085 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3086 }
3087
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003088 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003089
3090 POSTING_READ(PLANE_SURF(pipe, 0));
3091}
3092
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003093static void skylake_disable_primary_plane(struct drm_plane *primary,
3094 struct drm_crtc *crtc)
3095{
3096 struct drm_device *dev = crtc->dev;
3097 struct drm_i915_private *dev_priv = dev->dev_private;
3098 int pipe = to_intel_crtc(crtc)->pipe;
3099
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003100 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3101 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3102 POSTING_READ(PLANE_SURF(pipe, 0));
3103}
3104
Jesse Barnes17638cd2011-06-24 12:19:23 -07003105/* Assume fb object is pinned & idle & fenced and just update base pointers */
3106static int
3107intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3108 int x, int y, enum mode_set_atomic state)
3109{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003110 /* Support for kgdboc is disabled, this needs a major rework. */
3111 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003112
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003113 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003114}
3115
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003116static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003117{
Maarten Lankhorstef583192016-05-17 15:07:46 +02003118 struct intel_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003119
Maarten Lankhorst5251f042016-05-17 15:07:47 +02003120 for_each_intel_crtc(dev_priv->dev, crtc)
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003121 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
Ville Syrjälä75147472014-11-24 18:28:11 +02003122}
3123
3124static void intel_update_primary_planes(struct drm_device *dev)
3125{
Ville Syrjälä75147472014-11-24 18:28:11 +02003126 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003127
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003128 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003129 struct intel_plane *plane = to_intel_plane(crtc->primary);
3130 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003131
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003132 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003133 plane_state = to_intel_plane_state(plane->base.state);
3134
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003135 if (plane_state->visible)
3136 plane->update_plane(&plane->base,
3137 to_intel_crtc_state(crtc->state),
3138 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003139
3140 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003141 }
3142}
3143
Chris Wilsonc0336662016-05-06 15:40:21 +01003144void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003145{
3146 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003147 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003148 return;
3149
3150 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003151 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003152 return;
3153
Chris Wilsonc0336662016-05-06 15:40:21 +01003154 drm_modeset_lock_all(dev_priv->dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003155 /*
3156 * Disabling the crtcs gracefully seems nicer. Also the
3157 * g33 docs say we should at least disable all the planes.
3158 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003159 intel_display_suspend(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003160}
3161
Chris Wilsonc0336662016-05-06 15:40:21 +01003162void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003163{
Ville Syrjälä75147472014-11-24 18:28:11 +02003164 /*
3165 * Flips in the rings will be nuked by the reset,
3166 * so complete all pending flips so that user space
3167 * will get its events and not get stuck.
3168 */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003169 intel_complete_page_flips(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003170
3171 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003172 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003173 return;
3174
3175 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003176 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003177 /*
3178 * Flips in the rings have been nuked by the reset,
3179 * so update the base address of all primary
3180 * planes to the the last fb to make sure we're
3181 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003182 *
3183 * FIXME: Atomic will make this obsolete since we won't schedule
3184 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003185 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003186 intel_update_primary_planes(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003187 return;
3188 }
3189
3190 /*
3191 * The display has been reset as well,
3192 * so need a full re-initialization.
3193 */
3194 intel_runtime_pm_disable_interrupts(dev_priv);
3195 intel_runtime_pm_enable_interrupts(dev_priv);
3196
Chris Wilsonc0336662016-05-06 15:40:21 +01003197 intel_modeset_init_hw(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003198
3199 spin_lock_irq(&dev_priv->irq_lock);
3200 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003201 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003202 spin_unlock_irq(&dev_priv->irq_lock);
3203
Chris Wilsonc0336662016-05-06 15:40:21 +01003204 intel_display_resume(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003205
3206 intel_hpd_init(dev_priv);
3207
Chris Wilsonc0336662016-05-06 15:40:21 +01003208 drm_modeset_unlock_all(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003209}
3210
Chris Wilson7d5e3792014-03-04 13:15:08 +00003211static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3212{
3213 struct drm_device *dev = crtc->dev;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonc19ae982016-04-13 17:35:03 +01003215 unsigned reset_counter;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003216 bool pending;
3217
Chris Wilson7f1847e2016-04-13 17:35:04 +01003218 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3219 if (intel_crtc->reset_counter != reset_counter)
Chris Wilson7d5e3792014-03-04 13:15:08 +00003220 return false;
3221
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003222 spin_lock_irq(&dev->event_lock);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003223 pending = to_intel_crtc(crtc)->flip_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003224 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003225
3226 return pending;
3227}
3228
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003229static void intel_update_pipe_config(struct intel_crtc *crtc,
3230 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003231{
3232 struct drm_device *dev = crtc->base.dev;
3233 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003234 struct intel_crtc_state *pipe_config =
3235 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003236
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003237 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3238 crtc->base.mode = crtc->base.state->mode;
3239
3240 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3241 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3242 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003243
3244 /*
3245 * Update pipe size and adjust fitter if needed: the reason for this is
3246 * that in compute_mode_changes we check the native mode (not the pfit
3247 * mode) to see if we can flip rather than do a full mode set. In the
3248 * fastboot case, we'll flip, but if we don't update the pipesrc and
3249 * pfit state, we'll end up with a big fb scanned out into the wrong
3250 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003251 */
3252
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003253 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003254 ((pipe_config->pipe_src_w - 1) << 16) |
3255 (pipe_config->pipe_src_h - 1));
3256
3257 /* on skylake this is done by detaching scalers */
3258 if (INTEL_INFO(dev)->gen >= 9) {
3259 skl_detach_scalers(crtc);
3260
3261 if (pipe_config->pch_pfit.enabled)
3262 skylake_pfit_enable(crtc);
3263 } else if (HAS_PCH_SPLIT(dev)) {
3264 if (pipe_config->pch_pfit.enabled)
3265 ironlake_pfit_enable(crtc);
3266 else if (old_crtc_state->pch_pfit.enabled)
3267 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003268 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003269}
3270
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003271static void intel_fdi_normal_train(struct drm_crtc *crtc)
3272{
3273 struct drm_device *dev = crtc->dev;
3274 struct drm_i915_private *dev_priv = dev->dev_private;
3275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3276 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003277 i915_reg_t reg;
3278 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003279
3280 /* enable normal train */
3281 reg = FDI_TX_CTL(pipe);
3282 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003283 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003284 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3285 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003286 } else {
3287 temp &= ~FDI_LINK_TRAIN_NONE;
3288 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003289 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003290 I915_WRITE(reg, temp);
3291
3292 reg = FDI_RX_CTL(pipe);
3293 temp = I915_READ(reg);
3294 if (HAS_PCH_CPT(dev)) {
3295 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3296 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3297 } else {
3298 temp &= ~FDI_LINK_TRAIN_NONE;
3299 temp |= FDI_LINK_TRAIN_NONE;
3300 }
3301 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3302
3303 /* wait one idle pattern time */
3304 POSTING_READ(reg);
3305 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003306
3307 /* IVB wants error correction enabled */
3308 if (IS_IVYBRIDGE(dev))
3309 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3310 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003311}
3312
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003313/* The FDI link training functions for ILK/Ibexpeak. */
3314static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3315{
3316 struct drm_device *dev = crtc->dev;
3317 struct drm_i915_private *dev_priv = dev->dev_private;
3318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3319 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003320 i915_reg_t reg;
3321 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003322
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003323 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003324 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003325
Adam Jacksone1a44742010-06-25 15:32:14 -04003326 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3327 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003328 reg = FDI_RX_IMR(pipe);
3329 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003330 temp &= ~FDI_RX_SYMBOL_LOCK;
3331 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 I915_WRITE(reg, temp);
3333 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003334 udelay(150);
3335
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003336 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003337 reg = FDI_TX_CTL(pipe);
3338 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003339 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003340 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003341 temp &= ~FDI_LINK_TRAIN_NONE;
3342 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003343 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003344
Chris Wilson5eddb702010-09-11 13:48:45 +01003345 reg = FDI_RX_CTL(pipe);
3346 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003347 temp &= ~FDI_LINK_TRAIN_NONE;
3348 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003349 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3350
3351 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003352 udelay(150);
3353
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003354 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003355 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3356 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3357 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003358
Chris Wilson5eddb702010-09-11 13:48:45 +01003359 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003360 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3363
3364 if ((temp & FDI_RX_BIT_LOCK)) {
3365 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367 break;
3368 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003369 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003370 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003372
3373 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003374 reg = FDI_TX_CTL(pipe);
3375 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 temp &= ~FDI_LINK_TRAIN_NONE;
3377 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 reg = FDI_RX_CTL(pipe);
3381 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 temp &= ~FDI_LINK_TRAIN_NONE;
3383 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 I915_WRITE(reg, temp);
3385
3386 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 udelay(150);
3388
Chris Wilson5eddb702010-09-11 13:48:45 +01003389 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003390 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3393
3394 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003395 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 DRM_DEBUG_KMS("FDI train 2 done.\n");
3397 break;
3398 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003400 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003401 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003402
3403 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003404
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003405}
3406
Akshay Joshi0206e352011-08-16 15:34:10 -04003407static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3409 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3410 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3411 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3412};
3413
3414/* The FDI link training functions for SNB/Cougarpoint. */
3415static void gen6_fdi_link_train(struct drm_crtc *crtc)
3416{
3417 struct drm_device *dev = crtc->dev;
3418 struct drm_i915_private *dev_priv = dev->dev_private;
3419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3420 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003421 i915_reg_t reg;
3422 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423
Adam Jacksone1a44742010-06-25 15:32:14 -04003424 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3425 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 reg = FDI_RX_IMR(pipe);
3427 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003428 temp &= ~FDI_RX_SYMBOL_LOCK;
3429 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 I915_WRITE(reg, temp);
3431
3432 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003433 udelay(150);
3434
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 reg = FDI_TX_CTL(pipe);
3437 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003438 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003439 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 temp &= ~FDI_LINK_TRAIN_NONE;
3441 temp |= FDI_LINK_TRAIN_PATTERN_1;
3442 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3443 /* SNB-B */
3444 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446
Daniel Vetterd74cf322012-10-26 10:58:13 +02003447 I915_WRITE(FDI_RX_MISC(pipe),
3448 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3449
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 reg = FDI_RX_CTL(pipe);
3451 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 if (HAS_PCH_CPT(dev)) {
3453 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3454 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3455 } else {
3456 temp &= ~FDI_LINK_TRAIN_NONE;
3457 temp |= FDI_LINK_TRAIN_PATTERN_1;
3458 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3460
3461 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 udelay(150);
3463
Akshay Joshi0206e352011-08-16 15:34:10 -04003464 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 reg = FDI_TX_CTL(pipe);
3466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3468 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 I915_WRITE(reg, temp);
3470
3471 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 udelay(500);
3473
Sean Paulfa37d392012-03-02 12:53:39 -05003474 for (retry = 0; retry < 5; retry++) {
3475 reg = FDI_RX_IIR(pipe);
3476 temp = I915_READ(reg);
3477 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3478 if (temp & FDI_RX_BIT_LOCK) {
3479 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3480 DRM_DEBUG_KMS("FDI train 1 done.\n");
3481 break;
3482 }
3483 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 }
Sean Paulfa37d392012-03-02 12:53:39 -05003485 if (retry < 5)
3486 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487 }
3488 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490
3491 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 reg = FDI_TX_CTL(pipe);
3493 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494 temp &= ~FDI_LINK_TRAIN_NONE;
3495 temp |= FDI_LINK_TRAIN_PATTERN_2;
3496 if (IS_GEN6(dev)) {
3497 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3498 /* SNB-B */
3499 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3500 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 reg = FDI_RX_CTL(pipe);
3504 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 if (HAS_PCH_CPT(dev)) {
3506 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3507 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3508 } else {
3509 temp &= ~FDI_LINK_TRAIN_NONE;
3510 temp |= FDI_LINK_TRAIN_PATTERN_2;
3511 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 I915_WRITE(reg, temp);
3513
3514 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 udelay(150);
3516
Akshay Joshi0206e352011-08-16 15:34:10 -04003517 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003518 reg = FDI_TX_CTL(pipe);
3519 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3521 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 I915_WRITE(reg, temp);
3523
3524 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 udelay(500);
3526
Sean Paulfa37d392012-03-02 12:53:39 -05003527 for (retry = 0; retry < 5; retry++) {
3528 reg = FDI_RX_IIR(pipe);
3529 temp = I915_READ(reg);
3530 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3531 if (temp & FDI_RX_SYMBOL_LOCK) {
3532 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3533 DRM_DEBUG_KMS("FDI train 2 done.\n");
3534 break;
3535 }
3536 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003537 }
Sean Paulfa37d392012-03-02 12:53:39 -05003538 if (retry < 5)
3539 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003540 }
3541 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003542 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543
3544 DRM_DEBUG_KMS("FDI train done.\n");
3545}
3546
Jesse Barnes357555c2011-04-28 15:09:55 -07003547/* Manual link training for Ivy Bridge A0 parts */
3548static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3549{
3550 struct drm_device *dev = crtc->dev;
3551 struct drm_i915_private *dev_priv = dev->dev_private;
3552 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3553 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003554 i915_reg_t reg;
3555 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003556
3557 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3558 for train result */
3559 reg = FDI_RX_IMR(pipe);
3560 temp = I915_READ(reg);
3561 temp &= ~FDI_RX_SYMBOL_LOCK;
3562 temp &= ~FDI_RX_BIT_LOCK;
3563 I915_WRITE(reg, temp);
3564
3565 POSTING_READ(reg);
3566 udelay(150);
3567
Daniel Vetter01a415f2012-10-27 15:58:40 +02003568 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3569 I915_READ(FDI_RX_IIR(pipe)));
3570
Jesse Barnes139ccd32013-08-19 11:04:55 -07003571 /* Try each vswing and preemphasis setting twice before moving on */
3572 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3573 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003574 reg = FDI_TX_CTL(pipe);
3575 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003576 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3577 temp &= ~FDI_TX_ENABLE;
3578 I915_WRITE(reg, temp);
3579
3580 reg = FDI_RX_CTL(pipe);
3581 temp = I915_READ(reg);
3582 temp &= ~FDI_LINK_TRAIN_AUTO;
3583 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3584 temp &= ~FDI_RX_ENABLE;
3585 I915_WRITE(reg, temp);
3586
3587 /* enable CPU FDI TX and PCH FDI RX */
3588 reg = FDI_TX_CTL(pipe);
3589 temp = I915_READ(reg);
3590 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003591 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003592 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003593 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003594 temp |= snb_b_fdi_train_param[j/2];
3595 temp |= FDI_COMPOSITE_SYNC;
3596 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3597
3598 I915_WRITE(FDI_RX_MISC(pipe),
3599 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3600
3601 reg = FDI_RX_CTL(pipe);
3602 temp = I915_READ(reg);
3603 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3604 temp |= FDI_COMPOSITE_SYNC;
3605 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3606
3607 POSTING_READ(reg);
3608 udelay(1); /* should be 0.5us */
3609
3610 for (i = 0; i < 4; i++) {
3611 reg = FDI_RX_IIR(pipe);
3612 temp = I915_READ(reg);
3613 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3614
3615 if (temp & FDI_RX_BIT_LOCK ||
3616 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3617 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3618 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3619 i);
3620 break;
3621 }
3622 udelay(1); /* should be 0.5us */
3623 }
3624 if (i == 4) {
3625 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3626 continue;
3627 }
3628
3629 /* Train 2 */
3630 reg = FDI_TX_CTL(pipe);
3631 temp = I915_READ(reg);
3632 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3633 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3634 I915_WRITE(reg, temp);
3635
3636 reg = FDI_RX_CTL(pipe);
3637 temp = I915_READ(reg);
3638 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3639 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003640 I915_WRITE(reg, temp);
3641
3642 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003643 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003644
Jesse Barnes139ccd32013-08-19 11:04:55 -07003645 for (i = 0; i < 4; i++) {
3646 reg = FDI_RX_IIR(pipe);
3647 temp = I915_READ(reg);
3648 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003649
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650 if (temp & FDI_RX_SYMBOL_LOCK ||
3651 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3652 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3653 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3654 i);
3655 goto train_done;
3656 }
3657 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003658 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003659 if (i == 4)
3660 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003661 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003662
Jesse Barnes139ccd32013-08-19 11:04:55 -07003663train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003664 DRM_DEBUG_KMS("FDI train done.\n");
3665}
3666
Daniel Vetter88cefb62012-08-12 19:27:14 +02003667static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003668{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003669 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003670 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003672 i915_reg_t reg;
3673 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003674
Jesse Barnes0e23b992010-09-10 11:10:00 -07003675 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003676 reg = FDI_RX_CTL(pipe);
3677 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003678 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003679 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003680 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003681 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3682
3683 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003684 udelay(200);
3685
3686 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003687 temp = I915_READ(reg);
3688 I915_WRITE(reg, temp | FDI_PCDCLK);
3689
3690 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003691 udelay(200);
3692
Paulo Zanoni20749732012-11-23 15:30:38 -02003693 /* Enable CPU FDI TX PLL, always on for Ironlake */
3694 reg = FDI_TX_CTL(pipe);
3695 temp = I915_READ(reg);
3696 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3697 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003698
Paulo Zanoni20749732012-11-23 15:30:38 -02003699 POSTING_READ(reg);
3700 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003701 }
3702}
3703
Daniel Vetter88cefb62012-08-12 19:27:14 +02003704static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3705{
3706 struct drm_device *dev = intel_crtc->base.dev;
3707 struct drm_i915_private *dev_priv = dev->dev_private;
3708 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003709 i915_reg_t reg;
3710 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003711
3712 /* Switch from PCDclk to Rawclk */
3713 reg = FDI_RX_CTL(pipe);
3714 temp = I915_READ(reg);
3715 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3716
3717 /* Disable CPU FDI TX PLL */
3718 reg = FDI_TX_CTL(pipe);
3719 temp = I915_READ(reg);
3720 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3721
3722 POSTING_READ(reg);
3723 udelay(100);
3724
3725 reg = FDI_RX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3728
3729 /* Wait for the clocks to turn off. */
3730 POSTING_READ(reg);
3731 udelay(100);
3732}
3733
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003734static void ironlake_fdi_disable(struct drm_crtc *crtc)
3735{
3736 struct drm_device *dev = crtc->dev;
3737 struct drm_i915_private *dev_priv = dev->dev_private;
3738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3739 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003740 i915_reg_t reg;
3741 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003742
3743 /* disable CPU FDI tx and PCH FDI rx */
3744 reg = FDI_TX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3747 POSTING_READ(reg);
3748
3749 reg = FDI_RX_CTL(pipe);
3750 temp = I915_READ(reg);
3751 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003752 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003753 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3754
3755 POSTING_READ(reg);
3756 udelay(100);
3757
3758 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003759 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003760 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003761
3762 /* still set train pattern 1 */
3763 reg = FDI_TX_CTL(pipe);
3764 temp = I915_READ(reg);
3765 temp &= ~FDI_LINK_TRAIN_NONE;
3766 temp |= FDI_LINK_TRAIN_PATTERN_1;
3767 I915_WRITE(reg, temp);
3768
3769 reg = FDI_RX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 if (HAS_PCH_CPT(dev)) {
3772 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3773 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3774 } else {
3775 temp &= ~FDI_LINK_TRAIN_NONE;
3776 temp |= FDI_LINK_TRAIN_PATTERN_1;
3777 }
3778 /* BPC in FDI rx is consistent with that in PIPECONF */
3779 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003780 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003781 I915_WRITE(reg, temp);
3782
3783 POSTING_READ(reg);
3784 udelay(100);
3785}
3786
Chris Wilson5dce5b932014-01-20 10:17:36 +00003787bool intel_has_pending_fb_unpin(struct drm_device *dev)
3788{
3789 struct intel_crtc *crtc;
3790
3791 /* Note that we don't need to be called with mode_config.lock here
3792 * as our list of CRTC objects is static for the lifetime of the
3793 * device and so cannot disappear as we iterate. Similarly, we can
3794 * happily treat the predicates as racy, atomic checks as userspace
3795 * cannot claim and pin a new fb without at least acquring the
3796 * struct_mutex and so serialising with us.
3797 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003798 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003799 if (atomic_read(&crtc->unpin_work_count) == 0)
3800 continue;
3801
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003802 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003803 intel_wait_for_vblank(dev, crtc->pipe);
3804
3805 return true;
3806 }
3807
3808 return false;
3809}
3810
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003811static void page_flip_completed(struct intel_crtc *intel_crtc)
3812{
3813 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003814 struct intel_flip_work *work = intel_crtc->flip_work;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003815
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003816 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003817
3818 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003819 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003820
3821 drm_crtc_vblank_put(&intel_crtc->base);
3822
3823 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003824 queue_work(dev_priv->wq, &work->unpin_work);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003825
3826 trace_i915_flip_complete(intel_crtc->plane,
3827 work->pending_flip_obj);
3828}
3829
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003830static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003831{
Chris Wilson0f911282012-04-17 10:05:38 +01003832 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003833 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003834 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003835
Daniel Vetter2c10d572012-12-20 21:24:07 +01003836 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003837
3838 ret = wait_event_interruptible_timeout(
3839 dev_priv->pending_flip_queue,
3840 !intel_crtc_has_pending_flip(crtc),
3841 60*HZ);
3842
3843 if (ret < 0)
3844 return ret;
3845
3846 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003848 struct intel_flip_work *work;
Daniel Vetter2c10d572012-12-20 21:24:07 +01003849
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003850 spin_lock_irq(&dev->event_lock);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02003851 work = intel_crtc->flip_work;
3852 if (work && !is_mmio_work(work)) {
Chris Wilson9c787942014-09-05 07:13:25 +01003853 WARN_ONCE(1, "Removing stuck page flip\n");
3854 page_flip_completed(intel_crtc);
3855 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003856 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003857 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003858
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003859 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003860}
3861
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003862static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3863{
3864 u32 temp;
3865
3866 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3867
3868 mutex_lock(&dev_priv->sb_lock);
3869
3870 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3871 temp |= SBI_SSCCTL_DISABLE;
3872 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3873
3874 mutex_unlock(&dev_priv->sb_lock);
3875}
3876
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003877/* Program iCLKIP clock to the desired frequency */
3878static void lpt_program_iclkip(struct drm_crtc *crtc)
3879{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003880 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003881 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003882 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3883 u32 temp;
3884
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003885 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003886
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003887 /* The iCLK virtual clock root frequency is in MHz,
3888 * but the adjusted_mode->crtc_clock in in KHz. To get the
3889 * divisors, it is necessary to divide one by another, so we
3890 * convert the virtual clock precision to KHz here for higher
3891 * precision.
3892 */
3893 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003894 u32 iclk_virtual_root_freq = 172800 * 1000;
3895 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003896 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003897
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003898 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3899 clock << auxdiv);
3900 divsel = (desired_divisor / iclk_pi_range) - 2;
3901 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003902
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003903 /*
3904 * Near 20MHz is a corner case which is
3905 * out of range for the 7-bit divisor
3906 */
3907 if (divsel <= 0x7f)
3908 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003909 }
3910
3911 /* This should not happen with any sane values */
3912 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3913 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3914 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3915 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3916
3917 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 +03003918 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003919 auxdiv,
3920 divsel,
3921 phasedir,
3922 phaseinc);
3923
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003924 mutex_lock(&dev_priv->sb_lock);
3925
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003927 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3929 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3930 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3931 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3932 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3933 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003934 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935
3936 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003937 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003938 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3939 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003940 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003941
3942 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003943 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003944 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003945 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003946
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003947 mutex_unlock(&dev_priv->sb_lock);
3948
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003949 /* Wait for initialization time */
3950 udelay(24);
3951
3952 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3953}
3954
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003955int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3956{
3957 u32 divsel, phaseinc, auxdiv;
3958 u32 iclk_virtual_root_freq = 172800 * 1000;
3959 u32 iclk_pi_range = 64;
3960 u32 desired_divisor;
3961 u32 temp;
3962
3963 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3964 return 0;
3965
3966 mutex_lock(&dev_priv->sb_lock);
3967
3968 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3969 if (temp & SBI_SSCCTL_DISABLE) {
3970 mutex_unlock(&dev_priv->sb_lock);
3971 return 0;
3972 }
3973
3974 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3975 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3976 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3977 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3978 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3979
3980 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3981 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3982 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3983
3984 mutex_unlock(&dev_priv->sb_lock);
3985
3986 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3987
3988 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3989 desired_divisor << auxdiv);
3990}
3991
Daniel Vetter275f01b22013-05-03 11:49:47 +02003992static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3993 enum pipe pch_transcoder)
3994{
3995 struct drm_device *dev = crtc->base.dev;
3996 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003997 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003998
3999 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4000 I915_READ(HTOTAL(cpu_transcoder)));
4001 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4002 I915_READ(HBLANK(cpu_transcoder)));
4003 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4004 I915_READ(HSYNC(cpu_transcoder)));
4005
4006 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4007 I915_READ(VTOTAL(cpu_transcoder)));
4008 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4009 I915_READ(VBLANK(cpu_transcoder)));
4010 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4011 I915_READ(VSYNC(cpu_transcoder)));
4012 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4013 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4014}
4015
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004016static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004017{
4018 struct drm_i915_private *dev_priv = dev->dev_private;
4019 uint32_t temp;
4020
4021 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004022 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004023 return;
4024
4025 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4026 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4027
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004028 temp &= ~FDI_BC_BIFURCATION_SELECT;
4029 if (enable)
4030 temp |= FDI_BC_BIFURCATION_SELECT;
4031
4032 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004033 I915_WRITE(SOUTH_CHICKEN1, temp);
4034 POSTING_READ(SOUTH_CHICKEN1);
4035}
4036
4037static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4038{
4039 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004040
4041 switch (intel_crtc->pipe) {
4042 case PIPE_A:
4043 break;
4044 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004045 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004046 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004047 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004048 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004049
4050 break;
4051 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004052 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004053
4054 break;
4055 default:
4056 BUG();
4057 }
4058}
4059
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004060/* Return which DP Port should be selected for Transcoder DP control */
4061static enum port
4062intel_trans_dp_port_sel(struct drm_crtc *crtc)
4063{
4064 struct drm_device *dev = crtc->dev;
4065 struct intel_encoder *encoder;
4066
4067 for_each_encoder_on_crtc(dev, crtc, encoder) {
4068 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4069 encoder->type == INTEL_OUTPUT_EDP)
4070 return enc_to_dig_port(&encoder->base)->port;
4071 }
4072
4073 return -1;
4074}
4075
Jesse Barnesf67a5592011-01-05 10:31:48 -08004076/*
4077 * Enable PCH resources required for PCH ports:
4078 * - PCH PLLs
4079 * - FDI training & RX/TX
4080 * - update transcoder timings
4081 * - DP transcoding bits
4082 * - transcoder
4083 */
4084static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004085{
4086 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004087 struct drm_i915_private *dev_priv = dev->dev_private;
4088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4089 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004090 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004091
Daniel Vetterab9412b2013-05-03 11:49:46 +02004092 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004093
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004094 if (IS_IVYBRIDGE(dev))
4095 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4096
Daniel Vettercd986ab2012-10-26 10:58:12 +02004097 /* Write the TU size bits before fdi link training, so that error
4098 * detection works. */
4099 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4100 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4101
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004102 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004103 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004104
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004105 /* We need to program the right clock selection before writing the pixel
4106 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004107 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004108 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004109
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004110 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004111 temp |= TRANS_DPLL_ENABLE(pipe);
4112 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004113 if (intel_crtc->config->shared_dpll ==
4114 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004115 temp |= sel;
4116 else
4117 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004118 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004121 /* XXX: pch pll's can be enabled any time before we enable the PCH
4122 * transcoder, and we actually should do this to not upset any PCH
4123 * transcoder that already use the clock when we share it.
4124 *
4125 * Note that enable_shared_dpll tries to do the right thing, but
4126 * get_shared_dpll unconditionally resets the pll - we need that to have
4127 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004128 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004129
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004130 /* set transcoder timing, panel must allow it */
4131 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004132 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004133
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004134 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004135
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004136 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004137 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004138 const struct drm_display_mode *adjusted_mode =
4139 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004140 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004141 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 temp = I915_READ(reg);
4143 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004144 TRANS_DP_SYNC_MASK |
4145 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004146 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004147 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004148
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004149 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004150 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004151 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004152 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153
4154 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004155 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004156 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004158 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004159 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004161 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163 break;
4164 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004165 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166 }
4167
Chris Wilson5eddb702010-09-11 13:48:45 +01004168 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004169 }
4170
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004171 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004172}
4173
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004174static void lpt_pch_enable(struct drm_crtc *crtc)
4175{
4176 struct drm_device *dev = crtc->dev;
4177 struct drm_i915_private *dev_priv = dev->dev_private;
4178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004179 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004180
Daniel Vetterab9412b2013-05-03 11:49:46 +02004181 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004182
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004183 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004184
Paulo Zanoni0540e482012-10-31 18:12:40 -02004185 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004186 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004187
Paulo Zanoni937bb612012-10-31 18:12:47 -02004188 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004189}
4190
Daniel Vettera1520312013-05-03 11:49:50 +02004191static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004192{
4193 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004194 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004195 u32 temp;
4196
4197 temp = I915_READ(dslreg);
4198 udelay(500);
4199 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004200 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004201 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004202 }
4203}
4204
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004205static int
4206skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4207 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4208 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004209{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004210 struct intel_crtc_scaler_state *scaler_state =
4211 &crtc_state->scaler_state;
4212 struct intel_crtc *intel_crtc =
4213 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004214 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004215
4216 need_scaling = intel_rotation_90_or_270(rotation) ?
4217 (src_h != dst_w || src_w != dst_h):
4218 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004219
4220 /*
4221 * if plane is being disabled or scaler is no more required or force detach
4222 * - free scaler binded to this plane/crtc
4223 * - in order to do this, update crtc->scaler_usage
4224 *
4225 * Here scaler state in crtc_state is set free so that
4226 * scaler can be assigned to other user. Actual register
4227 * update to free the scaler is done in plane/panel-fit programming.
4228 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4229 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004230 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004231 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004232 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004233 scaler_state->scalers[*scaler_id].in_use = 0;
4234
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004235 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4236 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4237 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004238 scaler_state->scaler_users);
4239 *scaler_id = -1;
4240 }
4241 return 0;
4242 }
4243
4244 /* range checks */
4245 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4246 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4247
4248 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4249 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004250 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004251 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004252 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004253 return -EINVAL;
4254 }
4255
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004256 /* mark this plane as a scaler user in crtc_state */
4257 scaler_state->scaler_users |= (1 << scaler_user);
4258 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4259 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4260 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4261 scaler_state->scaler_users);
4262
4263 return 0;
4264}
4265
4266/**
4267 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4268 *
4269 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004270 *
4271 * Return
4272 * 0 - scaler_usage updated successfully
4273 * error - requested scaling cannot be supported or other error condition
4274 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004275int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004276{
4277 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004278 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004279
4280 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4281 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4282
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004283 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004284 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004285 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004286 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004287}
4288
4289/**
4290 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4291 *
4292 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004293 * @plane_state: atomic plane state to update
4294 *
4295 * Return
4296 * 0 - scaler_usage updated successfully
4297 * error - requested scaling cannot be supported or other error condition
4298 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004299static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4300 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004301{
4302
4303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004304 struct intel_plane *intel_plane =
4305 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004306 struct drm_framebuffer *fb = plane_state->base.fb;
4307 int ret;
4308
4309 bool force_detach = !fb || !plane_state->visible;
4310
4311 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4312 intel_plane->base.base.id, intel_crtc->pipe,
4313 drm_plane_index(&intel_plane->base));
4314
4315 ret = skl_update_scaler(crtc_state, force_detach,
4316 drm_plane_index(&intel_plane->base),
4317 &plane_state->scaler_id,
4318 plane_state->base.rotation,
4319 drm_rect_width(&plane_state->src) >> 16,
4320 drm_rect_height(&plane_state->src) >> 16,
4321 drm_rect_width(&plane_state->dst),
4322 drm_rect_height(&plane_state->dst));
4323
4324 if (ret || plane_state->scaler_id < 0)
4325 return ret;
4326
Chandra Kondurua1b22782015-04-07 15:28:45 -07004327 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004328 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004329 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004330 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004331 return -EINVAL;
4332 }
4333
4334 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004335 switch (fb->pixel_format) {
4336 case DRM_FORMAT_RGB565:
4337 case DRM_FORMAT_XBGR8888:
4338 case DRM_FORMAT_XRGB8888:
4339 case DRM_FORMAT_ABGR8888:
4340 case DRM_FORMAT_ARGB8888:
4341 case DRM_FORMAT_XRGB2101010:
4342 case DRM_FORMAT_XBGR2101010:
4343 case DRM_FORMAT_YUYV:
4344 case DRM_FORMAT_YVYU:
4345 case DRM_FORMAT_UYVY:
4346 case DRM_FORMAT_VYUY:
4347 break;
4348 default:
4349 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4350 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4351 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004352 }
4353
Chandra Kondurua1b22782015-04-07 15:28:45 -07004354 return 0;
4355}
4356
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004357static void skylake_scaler_disable(struct intel_crtc *crtc)
4358{
4359 int i;
4360
4361 for (i = 0; i < crtc->num_scalers; i++)
4362 skl_detach_scaler(crtc, i);
4363}
4364
4365static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004366{
4367 struct drm_device *dev = crtc->base.dev;
4368 struct drm_i915_private *dev_priv = dev->dev_private;
4369 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004370 struct intel_crtc_scaler_state *scaler_state =
4371 &crtc->config->scaler_state;
4372
4373 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4374
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004375 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004376 int id;
4377
4378 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4379 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4380 return;
4381 }
4382
4383 id = scaler_state->scaler_id;
4384 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4385 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4386 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4387 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4388
4389 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004390 }
4391}
4392
Jesse Barnesb074cec2013-04-25 12:55:02 -07004393static void ironlake_pfit_enable(struct intel_crtc *crtc)
4394{
4395 struct drm_device *dev = crtc->base.dev;
4396 struct drm_i915_private *dev_priv = dev->dev_private;
4397 int pipe = crtc->pipe;
4398
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004399 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004400 /* Force use of hard-coded filter coefficients
4401 * as some pre-programmed values are broken,
4402 * e.g. x201.
4403 */
4404 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4405 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4406 PF_PIPE_SEL_IVB(pipe));
4407 else
4408 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004409 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4410 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004411 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004412}
4413
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004414void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004415{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004416 struct drm_device *dev = crtc->base.dev;
4417 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004418
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004419 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004420 return;
4421
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004422 /*
4423 * We can only enable IPS after we enable a plane and wait for a vblank
4424 * This function is called from post_plane_update, which is run after
4425 * a vblank wait.
4426 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004427
Paulo Zanonid77e4532013-09-24 13:52:55 -03004428 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004429 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004430 mutex_lock(&dev_priv->rps.hw_lock);
4431 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4432 mutex_unlock(&dev_priv->rps.hw_lock);
4433 /* Quoting Art Runyan: "its not safe to expect any particular
4434 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004435 * mailbox." Moreover, the mailbox may return a bogus state,
4436 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004437 */
4438 } else {
4439 I915_WRITE(IPS_CTL, IPS_ENABLE);
4440 /* The bit only becomes 1 in the next vblank, so this wait here
4441 * is essentially intel_wait_for_vblank. If we don't have this
4442 * and don't wait for vblanks until the end of crtc_enable, then
4443 * the HW state readout code will complain that the expected
4444 * IPS_CTL value is not the one we read. */
4445 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4446 DRM_ERROR("Timed out waiting for IPS enable\n");
4447 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004448}
4449
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004450void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004451{
4452 struct drm_device *dev = crtc->base.dev;
4453 struct drm_i915_private *dev_priv = dev->dev_private;
4454
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004455 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004456 return;
4457
4458 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004459 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004460 mutex_lock(&dev_priv->rps.hw_lock);
4461 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4462 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004463 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4464 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4465 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004466 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004467 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004468 POSTING_READ(IPS_CTL);
4469 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004470
4471 /* We need to wait for a vblank before we can disable the plane. */
4472 intel_wait_for_vblank(dev, crtc->pipe);
4473}
4474
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004475static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004476{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004477 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004478 struct drm_device *dev = intel_crtc->base.dev;
4479 struct drm_i915_private *dev_priv = dev->dev_private;
4480
4481 mutex_lock(&dev->struct_mutex);
4482 dev_priv->mm.interruptible = false;
4483 (void) intel_overlay_switch_off(intel_crtc->overlay);
4484 dev_priv->mm.interruptible = true;
4485 mutex_unlock(&dev->struct_mutex);
4486 }
4487
4488 /* Let userspace switch the overlay on again. In most cases userspace
4489 * has to recompute where to put it anyway.
4490 */
4491}
4492
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004493/**
4494 * intel_post_enable_primary - Perform operations after enabling primary plane
4495 * @crtc: the CRTC whose primary plane was just enabled
4496 *
4497 * Performs potentially sleeping operations that must be done after the primary
4498 * plane is enabled, such as updating FBC and IPS. Note that this may be
4499 * called due to an explicit primary plane update, or due to an implicit
4500 * re-enable that is caused when a sprite plane is updated to no longer
4501 * completely hide the primary plane.
4502 */
4503static void
4504intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004505{
4506 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004507 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4509 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004510
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004511 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004512 * FIXME IPS should be fine as long as one plane is
4513 * enabled, but in practice it seems to have problems
4514 * when going from primary only to sprite only and vice
4515 * versa.
4516 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004517 hsw_enable_ips(intel_crtc);
4518
Daniel Vetterf99d7062014-06-19 16:01:59 +02004519 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004520 * Gen2 reports pipe underruns whenever all planes are disabled.
4521 * So don't enable underrun reporting before at least some planes
4522 * are enabled.
4523 * FIXME: Need to fix the logic to work when we turn off all planes
4524 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004525 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004526 if (IS_GEN2(dev))
4527 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4528
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004529 /* Underruns don't always raise interrupts, so check manually. */
4530 intel_check_cpu_fifo_underruns(dev_priv);
4531 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004532}
4533
Ville Syrjälä2622a082016-03-09 19:07:26 +02004534/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004535static void
4536intel_pre_disable_primary(struct drm_crtc *crtc)
4537{
4538 struct drm_device *dev = crtc->dev;
4539 struct drm_i915_private *dev_priv = dev->dev_private;
4540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4541 int pipe = intel_crtc->pipe;
4542
4543 /*
4544 * Gen2 reports pipe underruns whenever all planes are disabled.
4545 * So diasble underrun reporting before all the planes get disabled.
4546 * FIXME: Need to fix the logic to work when we turn off all planes
4547 * but leave the pipe running.
4548 */
4549 if (IS_GEN2(dev))
4550 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4551
4552 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004553 * FIXME IPS should be fine as long as one plane is
4554 * enabled, but in practice it seems to have problems
4555 * when going from primary only to sprite only and vice
4556 * versa.
4557 */
4558 hsw_disable_ips(intel_crtc);
4559}
4560
4561/* FIXME get rid of this and use pre_plane_update */
4562static void
4563intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4564{
4565 struct drm_device *dev = crtc->dev;
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4568 int pipe = intel_crtc->pipe;
4569
4570 intel_pre_disable_primary(crtc);
4571
4572 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004573 * Vblank time updates from the shadow to live plane control register
4574 * are blocked if the memory self-refresh mode is active at that
4575 * moment. So to make sure the plane gets truly disabled, disable
4576 * first the self-refresh mode. The self-refresh enable bit in turn
4577 * will be checked/applied by the HW only at the next frame start
4578 * event which is after the vblank start event, so we need to have a
4579 * wait-for-vblank between disabling the plane and the pipe.
4580 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004581 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004582 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004583 dev_priv->wm.vlv.cxsr = false;
4584 intel_wait_for_vblank(dev, pipe);
4585 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004586}
4587
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004588static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004589{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004590 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4591 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004592 struct intel_crtc_state *pipe_config =
4593 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004594 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004595 struct drm_plane *primary = crtc->base.primary;
4596 struct drm_plane_state *old_pri_state =
4597 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004598
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004599 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004600
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004601 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004602
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004603 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004604 intel_update_watermarks(&crtc->base);
4605
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004606 if (old_pri_state) {
4607 struct intel_plane_state *primary_state =
4608 to_intel_plane_state(primary->state);
4609 struct intel_plane_state *old_primary_state =
4610 to_intel_plane_state(old_pri_state);
4611
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004612 intel_fbc_post_update(crtc);
4613
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004614 if (primary_state->visible &&
4615 (needs_modeset(&pipe_config->base) ||
4616 !old_primary_state->visible))
4617 intel_post_enable_primary(&crtc->base);
4618 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004619}
4620
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004621static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004622{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004623 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004624 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004625 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004626 struct intel_crtc_state *pipe_config =
4627 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004628 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4629 struct drm_plane *primary = crtc->base.primary;
4630 struct drm_plane_state *old_pri_state =
4631 drm_atomic_get_existing_plane_state(old_state, primary);
4632 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004633
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004634 if (old_pri_state) {
4635 struct intel_plane_state *primary_state =
4636 to_intel_plane_state(primary->state);
4637 struct intel_plane_state *old_primary_state =
4638 to_intel_plane_state(old_pri_state);
4639
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004640 intel_fbc_pre_update(crtc);
4641
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004642 if (old_primary_state->visible &&
4643 (modeset || !primary_state->visible))
4644 intel_pre_disable_primary(&crtc->base);
4645 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004646
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004647 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004648 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004649
Ville Syrjälä2622a082016-03-09 19:07:26 +02004650 /*
4651 * Vblank time updates from the shadow to live plane control register
4652 * are blocked if the memory self-refresh mode is active at that
4653 * moment. So to make sure the plane gets truly disabled, disable
4654 * first the self-refresh mode. The self-refresh enable bit in turn
4655 * will be checked/applied by the HW only at the next frame start
4656 * event which is after the vblank start event, so we need to have a
4657 * wait-for-vblank between disabling the plane and the pipe.
4658 */
4659 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004660 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004661 dev_priv->wm.vlv.cxsr = false;
4662 intel_wait_for_vblank(dev, crtc->pipe);
4663 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004664 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004665
Matt Ropered4a6a72016-02-23 17:20:13 -08004666 /*
4667 * IVB workaround: must disable low power watermarks for at least
4668 * one frame before enabling scaling. LP watermarks can be re-enabled
4669 * when scaling is disabled.
4670 *
4671 * WaCxSRDisabledForSpriteScaling:ivb
4672 */
4673 if (pipe_config->disable_lp_wm) {
4674 ilk_disable_lp_wm(dev);
4675 intel_wait_for_vblank(dev, crtc->pipe);
4676 }
4677
4678 /*
4679 * If we're doing a modeset, we're done. No need to do any pre-vblank
4680 * watermark programming here.
4681 */
4682 if (needs_modeset(&pipe_config->base))
4683 return;
4684
4685 /*
4686 * For platforms that support atomic watermarks, program the
4687 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4688 * will be the intermediate values that are safe for both pre- and
4689 * post- vblank; when vblank happens, the 'active' values will be set
4690 * to the final 'target' values and we'll do this again to get the
4691 * optimal watermarks. For gen9+ platforms, the values we program here
4692 * will be the final target values which will get automatically latched
4693 * at vblank time; no further programming will be necessary.
4694 *
4695 * If a platform hasn't been transitioned to atomic watermarks yet,
4696 * we'll continue to update watermarks the old way, if flags tell
4697 * us to.
4698 */
4699 if (dev_priv->display.initial_watermarks != NULL)
4700 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004701 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004702 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004703}
4704
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004705static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004706{
4707 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004709 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004711
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004712 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004713
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004714 drm_for_each_plane_mask(p, dev, plane_mask)
4715 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004716
Daniel Vetterf99d7062014-06-19 16:01:59 +02004717 /*
4718 * FIXME: Once we grow proper nuclear flip support out of this we need
4719 * to compute the mask of flip planes precisely. For the time being
4720 * consider this a flip to a NULL plane.
4721 */
4722 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004723}
4724
Jesse Barnesf67a5592011-01-05 10:31:48 -08004725static void ironlake_crtc_enable(struct drm_crtc *crtc)
4726{
4727 struct drm_device *dev = crtc->dev;
4728 struct drm_i915_private *dev_priv = dev->dev_private;
4729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004730 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004731 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004732 struct intel_crtc_state *pipe_config =
4733 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004734
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004735 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004736 return;
4737
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004738 /*
4739 * Sometimes spurious CPU pipe underruns happen during FDI
4740 * training, at least with VGA+HDMI cloning. Suppress them.
4741 *
4742 * On ILK we get an occasional spurious CPU pipe underruns
4743 * between eDP port A enable and vdd enable. Also PCH port
4744 * enable seems to result in the occasional CPU pipe underrun.
4745 *
4746 * Spurious PCH underruns also occur during PCH enabling.
4747 */
4748 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4749 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004750 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004751 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4752
4753 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004754 intel_prepare_shared_dpll(intel_crtc);
4755
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004756 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304757 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004758
4759 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004760 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004762 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004763 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004764 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004765 }
4766
4767 ironlake_set_pipeconf(crtc);
4768
Jesse Barnesf67a5592011-01-05 10:31:48 -08004769 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004770
Daniel Vetterf6736a12013-06-05 13:34:30 +02004771 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004772 if (encoder->pre_enable)
4773 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004774
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004775 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004776 /* Note: FDI PLL enabling _must_ be done before we enable the
4777 * cpu pipes, hence this is separate from all the other fdi/pch
4778 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004779 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004780 } else {
4781 assert_fdi_tx_disabled(dev_priv, pipe);
4782 assert_fdi_rx_disabled(dev_priv, pipe);
4783 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004784
Jesse Barnesb074cec2013-04-25 12:55:02 -07004785 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004786
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004787 /*
4788 * On ILK+ LUT must be loaded before the pipe is running but with
4789 * clocks enabled
4790 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004791 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004792
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004793 if (dev_priv->display.initial_watermarks != NULL)
4794 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004795 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004796
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004797 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004798 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004799
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004800 assert_vblank_disabled(crtc);
4801 drm_crtc_vblank_on(crtc);
4802
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004803 for_each_encoder_on_crtc(dev, crtc, encoder)
4804 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004805
4806 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004807 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004808
4809 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4810 if (intel_crtc->config->has_pch_encoder)
4811 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004812 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004813 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004814}
4815
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004816/* IPS only exists on ULT machines and is tied to pipe A. */
4817static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4818{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004819 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004820}
4821
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004822static void haswell_crtc_enable(struct drm_crtc *crtc)
4823{
4824 struct drm_device *dev = crtc->dev;
4825 struct drm_i915_private *dev_priv = dev->dev_private;
4826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4827 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004828 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004829 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004830 struct intel_crtc_state *pipe_config =
4831 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004832
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004833 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004834 return;
4835
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004836 if (intel_crtc->config->has_pch_encoder)
4837 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4838 false);
4839
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004840 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004841 intel_enable_shared_dpll(intel_crtc);
4842
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004843 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304844 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004845
Jani Nikula4d1de972016-03-18 17:05:42 +02004846 if (!intel_crtc->config->has_dsi_encoder)
4847 intel_set_pipe_timings(intel_crtc);
4848
Jani Nikulabc58be62016-03-18 17:05:39 +02004849 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004850
Jani Nikula4d1de972016-03-18 17:05:42 +02004851 if (cpu_transcoder != TRANSCODER_EDP &&
4852 !transcoder_is_dsi(cpu_transcoder)) {
4853 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004854 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004855 }
4856
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004857 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004858 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004859 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004860 }
4861
Jani Nikula4d1de972016-03-18 17:05:42 +02004862 if (!intel_crtc->config->has_dsi_encoder)
4863 haswell_set_pipeconf(crtc);
4864
Jani Nikula391bf042016-03-18 17:05:40 +02004865 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004866
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004867 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004868
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004869 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004870
Daniel Vetter6b698512015-11-28 11:05:39 +01004871 if (intel_crtc->config->has_pch_encoder)
4872 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4873 else
4874 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4875
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304876 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004877 if (encoder->pre_enable)
4878 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304879 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004881 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004882 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004883
Jani Nikulaa65347b2015-11-27 12:21:46 +02004884 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304885 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004886
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004887 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004888 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004889 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004890 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004891
4892 /*
4893 * On ILK+ LUT must be loaded before the pipe is running but with
4894 * clocks enabled
4895 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004896 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004897
Paulo Zanoni1f544382012-10-24 11:32:00 -02004898 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004899 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304900 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004901
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004902 if (dev_priv->display.initial_watermarks != NULL)
4903 dev_priv->display.initial_watermarks(pipe_config);
4904 else
4905 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004906
4907 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4908 if (!intel_crtc->config->has_dsi_encoder)
4909 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004910
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004911 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004912 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004913
Jani Nikulaa65347b2015-11-27 12:21:46 +02004914 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004915 intel_ddi_set_vc_payload_alloc(crtc, true);
4916
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004917 assert_vblank_disabled(crtc);
4918 drm_crtc_vblank_on(crtc);
4919
Jani Nikula8807e552013-08-30 19:40:32 +03004920 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004921 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004922 intel_opregion_notify_encoder(encoder, true);
4923 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004924
Daniel Vetter6b698512015-11-28 11:05:39 +01004925 if (intel_crtc->config->has_pch_encoder) {
4926 intel_wait_for_vblank(dev, pipe);
4927 intel_wait_for_vblank(dev, pipe);
4928 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004929 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4930 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004931 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004932
Paulo Zanonie4916942013-09-20 16:21:19 -03004933 /* If we change the relative order between pipe/planes enabling, we need
4934 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004935 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4936 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4937 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4938 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4939 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004940}
4941
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004942static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004943{
4944 struct drm_device *dev = crtc->base.dev;
4945 struct drm_i915_private *dev_priv = dev->dev_private;
4946 int pipe = crtc->pipe;
4947
4948 /* To avoid upsetting the power well on haswell only disable the pfit if
4949 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004950 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004951 I915_WRITE(PF_CTL(pipe), 0);
4952 I915_WRITE(PF_WIN_POS(pipe), 0);
4953 I915_WRITE(PF_WIN_SZ(pipe), 0);
4954 }
4955}
4956
Jesse Barnes6be4a602010-09-10 10:26:01 -07004957static void ironlake_crtc_disable(struct drm_crtc *crtc)
4958{
4959 struct drm_device *dev = crtc->dev;
4960 struct drm_i915_private *dev_priv = dev->dev_private;
4961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004962 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004963 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004964
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004965 /*
4966 * Sometimes spurious CPU pipe underruns happen when the
4967 * pipe is already disabled, but FDI RX/TX is still enabled.
4968 * Happens at least with VGA+HDMI cloning. Suppress them.
4969 */
4970 if (intel_crtc->config->has_pch_encoder) {
4971 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004972 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004973 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004974
Daniel Vetterea9d7582012-07-10 10:42:52 +02004975 for_each_encoder_on_crtc(dev, crtc, encoder)
4976 encoder->disable(encoder);
4977
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004978 drm_crtc_vblank_off(crtc);
4979 assert_vblank_disabled(crtc);
4980
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004981 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004983 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004984
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004985 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004986 ironlake_fdi_disable(crtc);
4987
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004988 for_each_encoder_on_crtc(dev, crtc, encoder)
4989 if (encoder->post_disable)
4990 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004991
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004992 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004993 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004994
Daniel Vetterd925c592013-06-05 13:34:04 +02004995 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004996 i915_reg_t reg;
4997 u32 temp;
4998
Daniel Vetterd925c592013-06-05 13:34:04 +02004999 /* disable TRANS_DP_CTL */
5000 reg = TRANS_DP_CTL(pipe);
5001 temp = I915_READ(reg);
5002 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5003 TRANS_DP_PORT_SEL_MASK);
5004 temp |= TRANS_DP_PORT_SEL_NONE;
5005 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005006
Daniel Vetterd925c592013-06-05 13:34:04 +02005007 /* disable DPLL_SEL */
5008 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005009 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005010 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005011 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005012
Daniel Vetterd925c592013-06-05 13:34:04 +02005013 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005014 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005015
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005016 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005017 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005018}
5019
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005020static void haswell_crtc_disable(struct drm_crtc *crtc)
5021{
5022 struct drm_device *dev = crtc->dev;
5023 struct drm_i915_private *dev_priv = dev->dev_private;
5024 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5025 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005026 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005027
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005028 if (intel_crtc->config->has_pch_encoder)
5029 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5030 false);
5031
Jani Nikula8807e552013-08-30 19:40:32 +03005032 for_each_encoder_on_crtc(dev, crtc, encoder) {
5033 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005035 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005037 drm_crtc_vblank_off(crtc);
5038 assert_vblank_disabled(crtc);
5039
Jani Nikula4d1de972016-03-18 17:05:42 +02005040 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5041 if (!intel_crtc->config->has_dsi_encoder)
5042 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005044 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005045 intel_ddi_set_vc_payload_alloc(crtc, false);
5046
Jani Nikulaa65347b2015-11-27 12:21:46 +02005047 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305048 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005049
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005050 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005051 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005052 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005053 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005054
Jani Nikulaa65347b2015-11-27 12:21:46 +02005055 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305056 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005057
Imre Deak97b040a2014-06-25 22:01:50 +03005058 for_each_encoder_on_crtc(dev, crtc, encoder)
5059 if (encoder->post_disable)
5060 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005061
Ville Syrjälä92966a32015-12-08 16:05:48 +02005062 if (intel_crtc->config->has_pch_encoder) {
5063 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005064 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005065 intel_ddi_fdi_disable(crtc);
5066
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005067 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5068 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005069 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005070}
5071
Jesse Barnes2dd24552013-04-25 12:55:01 -07005072static void i9xx_pfit_enable(struct intel_crtc *crtc)
5073{
5074 struct drm_device *dev = crtc->base.dev;
5075 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005076 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005077
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005078 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005079 return;
5080
Daniel Vetterc0b03412013-05-28 12:05:54 +02005081 /*
5082 * The panel fitter should only be adjusted whilst the pipe is disabled,
5083 * according to register description and PRM.
5084 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005085 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5086 assert_pipe_disabled(dev_priv, crtc->pipe);
5087
Jesse Barnesb074cec2013-04-25 12:55:02 -07005088 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5089 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005090
5091 /* Border color in case we don't scale up to the full screen. Black by
5092 * default, change to something else for debugging. */
5093 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005094}
5095
Dave Airlied05410f2014-06-05 13:22:59 +10005096static enum intel_display_power_domain port_to_power_domain(enum port port)
5097{
5098 switch (port) {
5099 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005100 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005101 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005102 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005103 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005104 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005105 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005106 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005107 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005108 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005109 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005110 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005111 return POWER_DOMAIN_PORT_OTHER;
5112 }
5113}
5114
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005115static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5116{
5117 switch (port) {
5118 case PORT_A:
5119 return POWER_DOMAIN_AUX_A;
5120 case PORT_B:
5121 return POWER_DOMAIN_AUX_B;
5122 case PORT_C:
5123 return POWER_DOMAIN_AUX_C;
5124 case PORT_D:
5125 return POWER_DOMAIN_AUX_D;
5126 case PORT_E:
5127 /* FIXME: Check VBT for actual wiring of PORT E */
5128 return POWER_DOMAIN_AUX_D;
5129 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005130 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005131 return POWER_DOMAIN_AUX_A;
5132 }
5133}
5134
Imre Deak319be8a2014-03-04 19:22:57 +02005135enum intel_display_power_domain
5136intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005137{
Imre Deak319be8a2014-03-04 19:22:57 +02005138 struct drm_device *dev = intel_encoder->base.dev;
5139 struct intel_digital_port *intel_dig_port;
5140
5141 switch (intel_encoder->type) {
5142 case INTEL_OUTPUT_UNKNOWN:
5143 /* Only DDI platforms should ever use this output type */
5144 WARN_ON_ONCE(!HAS_DDI(dev));
5145 case INTEL_OUTPUT_DISPLAYPORT:
5146 case INTEL_OUTPUT_HDMI:
5147 case INTEL_OUTPUT_EDP:
5148 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005149 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005150 case INTEL_OUTPUT_DP_MST:
5151 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5152 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005153 case INTEL_OUTPUT_ANALOG:
5154 return POWER_DOMAIN_PORT_CRT;
5155 case INTEL_OUTPUT_DSI:
5156 return POWER_DOMAIN_PORT_DSI;
5157 default:
5158 return POWER_DOMAIN_PORT_OTHER;
5159 }
5160}
5161
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005162enum intel_display_power_domain
5163intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5164{
5165 struct drm_device *dev = intel_encoder->base.dev;
5166 struct intel_digital_port *intel_dig_port;
5167
5168 switch (intel_encoder->type) {
5169 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005170 case INTEL_OUTPUT_HDMI:
5171 /*
5172 * Only DDI platforms should ever use these output types.
5173 * We can get here after the HDMI detect code has already set
5174 * the type of the shared encoder. Since we can't be sure
5175 * what's the status of the given connectors, play safe and
5176 * run the DP detection too.
5177 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005178 WARN_ON_ONCE(!HAS_DDI(dev));
5179 case INTEL_OUTPUT_DISPLAYPORT:
5180 case INTEL_OUTPUT_EDP:
5181 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5182 return port_to_aux_power_domain(intel_dig_port->port);
5183 case INTEL_OUTPUT_DP_MST:
5184 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5185 return port_to_aux_power_domain(intel_dig_port->port);
5186 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005187 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005188 return POWER_DOMAIN_AUX_A;
5189 }
5190}
5191
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005192static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5193 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005194{
5195 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005196 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5198 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005199 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005200 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005201
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005202 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005203 return 0;
5204
Imre Deak77d22dc2014-03-05 16:20:52 +02005205 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5206 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005207 if (crtc_state->pch_pfit.enabled ||
5208 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005209 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5210
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005211 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5212 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5213
Imre Deak319be8a2014-03-04 19:22:57 +02005214 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005215 }
Imre Deak319be8a2014-03-04 19:22:57 +02005216
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005217 if (crtc_state->shared_dpll)
5218 mask |= BIT(POWER_DOMAIN_PLLS);
5219
Imre Deak77d22dc2014-03-05 16:20:52 +02005220 return mask;
5221}
5222
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005223static unsigned long
5224modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5225 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005226{
5227 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5229 enum intel_display_power_domain domain;
5230 unsigned long domains, new_domains, old_domains;
5231
5232 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005233 intel_crtc->enabled_power_domains = new_domains =
5234 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005235
5236 domains = new_domains & ~old_domains;
5237
5238 for_each_power_domain(domain, domains)
5239 intel_display_power_get(dev_priv, domain);
5240
5241 return old_domains & ~new_domains;
5242}
5243
5244static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5245 unsigned long domains)
5246{
5247 enum intel_display_power_domain domain;
5248
5249 for_each_power_domain(domain, domains)
5250 intel_display_power_put(dev_priv, domain);
5251}
5252
Mika Kaholaadafdc62015-08-18 14:36:59 +03005253static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5254{
5255 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5256
5257 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5258 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5259 return max_cdclk_freq;
5260 else if (IS_CHERRYVIEW(dev_priv))
5261 return max_cdclk_freq*95/100;
5262 else if (INTEL_INFO(dev_priv)->gen < 4)
5263 return 2*max_cdclk_freq*90/100;
5264 else
5265 return max_cdclk_freq*90/100;
5266}
5267
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005268static void intel_update_max_cdclk(struct drm_device *dev)
5269{
5270 struct drm_i915_private *dev_priv = dev->dev_private;
5271
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005272 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005273 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5274
5275 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5276 dev_priv->max_cdclk_freq = 675000;
5277 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5278 dev_priv->max_cdclk_freq = 540000;
5279 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5280 dev_priv->max_cdclk_freq = 450000;
5281 else
5282 dev_priv->max_cdclk_freq = 337500;
Matt Roper281c1142016-04-05 14:37:19 -07005283 } else if (IS_BROXTON(dev)) {
5284 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005285 } else if (IS_BROADWELL(dev)) {
5286 /*
5287 * FIXME with extra cooling we can allow
5288 * 540 MHz for ULX and 675 Mhz for ULT.
5289 * How can we know if extra cooling is
5290 * available? PCI ID, VTB, something else?
5291 */
5292 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5293 dev_priv->max_cdclk_freq = 450000;
5294 else if (IS_BDW_ULX(dev))
5295 dev_priv->max_cdclk_freq = 450000;
5296 else if (IS_BDW_ULT(dev))
5297 dev_priv->max_cdclk_freq = 540000;
5298 else
5299 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005300 } else if (IS_CHERRYVIEW(dev)) {
5301 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005302 } else if (IS_VALLEYVIEW(dev)) {
5303 dev_priv->max_cdclk_freq = 400000;
5304 } else {
5305 /* otherwise assume cdclk is fixed */
5306 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5307 }
5308
Mika Kaholaadafdc62015-08-18 14:36:59 +03005309 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5310
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005311 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5312 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005313
5314 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5315 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005316}
5317
5318static void intel_update_cdclk(struct drm_device *dev)
5319{
5320 struct drm_i915_private *dev_priv = dev->dev_private;
5321
5322 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5323 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5324 dev_priv->cdclk_freq);
5325
5326 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005327 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5328 * Programmng [sic] note: bit[9:2] should be programmed to the number
5329 * of cdclk that generates 4MHz reference clock freq which is used to
5330 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005331 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005332 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005333 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005334
5335 if (dev_priv->max_cdclk_freq == 0)
5336 intel_update_max_cdclk(dev);
5337}
5338
Ville Syrjälä92891e42016-05-11 22:44:45 +03005339/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5340static int skl_cdclk_decimal(int cdclk)
5341{
5342 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5343}
5344
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005345static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305346{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305347 uint32_t divider;
5348 uint32_t ratio;
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005349 uint32_t current_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305350 int ret;
5351
5352 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005353 switch (cdclk) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305354 case 144000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 288000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5360 ratio = BXT_DE_PLL_RATIO(60);
5361 break;
5362 case 384000:
5363 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5364 ratio = BXT_DE_PLL_RATIO(60);
5365 break;
5366 case 576000:
5367 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5368 ratio = BXT_DE_PLL_RATIO(60);
5369 break;
5370 case 624000:
5371 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5372 ratio = BXT_DE_PLL_RATIO(65);
5373 break;
5374 case 19200:
5375 /*
5376 * Bypass frequency with DE PLL disabled. Init ratio, divider
5377 * to suppress GCC warning.
5378 */
5379 ratio = 0;
5380 divider = 0;
5381 break;
5382 default:
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005383 DRM_ERROR("unsupported CDCLK freq %d", cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305384
5385 return;
5386 }
5387
5388 mutex_lock(&dev_priv->rps.hw_lock);
5389 /* Inform power controller of upcoming frequency change */
5390 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5391 0x80000000);
5392 mutex_unlock(&dev_priv->rps.hw_lock);
5393
5394 if (ret) {
5395 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005396 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305397 return;
5398 }
5399
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005400 current_cdclk = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305401 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005402 current_cdclk = current_cdclk * 500 + 1000;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305403
5404 /*
5405 * DE PLL has to be disabled when
5406 * - setting to 19.2MHz (bypass, PLL isn't used)
5407 * - before setting to 624MHz (PLL needs toggling)
5408 * - before setting to any frequency from 624MHz (PLL needs toggling)
5409 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005410 if (cdclk == 19200 || cdclk == 624000 ||
5411 current_cdclk == 624000) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305412 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5413 /* Timeout 200us */
5414 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5415 1))
5416 DRM_ERROR("timout waiting for DE PLL unlock\n");
5417 }
5418
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005419 if (cdclk != 19200) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305420 uint32_t val;
5421
5422 val = I915_READ(BXT_DE_PLL_CTL);
5423 val &= ~BXT_DE_PLL_RATIO_MASK;
5424 val |= ratio;
5425 I915_WRITE(BXT_DE_PLL_CTL, val);
5426
5427 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5428 /* Timeout 200us */
5429 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5430 DRM_ERROR("timeout waiting for DE PLL lock\n");
5431
Ville Syrjäläb8e75702016-05-11 22:44:52 +03005432 val = divider | skl_cdclk_decimal(cdclk);
Ville Syrjälä7fe62752016-05-11 22:44:51 +03005433 /*
5434 * FIXME if only the cd2x divider needs changing, it could be done
5435 * without shutting off the pipe (if only one pipe is active).
5436 */
5437 val |= BXT_CDCLK_CD2X_PIPE_NONE;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305438 /*
5439 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5440 * enable otherwise.
5441 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005442 if (cdclk >= 500000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305443 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305444 I915_WRITE(CDCLK_CTL, val);
5445 }
5446
5447 mutex_lock(&dev_priv->rps.hw_lock);
5448 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005449 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305450 mutex_unlock(&dev_priv->rps.hw_lock);
5451
5452 if (ret) {
5453 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005454 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305455 return;
5456 }
5457
Imre Deakc6c46962016-04-01 16:02:40 +03005458 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305459}
5460
Imre Deakc2e001e2016-04-01 16:02:43 +03005461static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
5462{
5463 if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
5464 return false;
5465
5466 /* TODO: Check for a valid CDCLK rate */
5467
5468 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_REQUEST)) {
5469 DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power not requested\n");
5470
5471 return false;
5472 }
5473
5474 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE)) {
5475 DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power hasn't settled\n");
5476
5477 return false;
5478 }
5479
5480 return true;
5481}
5482
Imre Deakadc7f042016-04-04 17:27:10 +03005483bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
5484{
5485 return broxton_cdclk_is_enabled(dev_priv);
5486}
5487
Imre Deakc6c46962016-04-01 16:02:40 +03005488void broxton_init_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305489{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305490 /* check if cd clock is enabled */
Imre Deakc2e001e2016-04-01 16:02:43 +03005491 if (broxton_cdclk_is_enabled(dev_priv)) {
5492 DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305493 return;
5494 }
5495
Imre Deakc2e001e2016-04-01 16:02:43 +03005496 DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
5497
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305498 /*
5499 * FIXME:
5500 * - The initial CDCLK needs to be read from VBT.
5501 * Need to make this change after VBT has changes for BXT.
5502 * - check if setting the max (or any) cdclk freq is really necessary
5503 * here, it belongs to modeset time
5504 */
Imre Deakc6c46962016-04-01 16:02:40 +03005505 broxton_set_cdclk(dev_priv, 624000);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305506
5507 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005508 POSTING_READ(DBUF_CTL);
5509
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305510 udelay(10);
5511
5512 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5513 DRM_ERROR("DBuf power enable timeout!\n");
5514}
5515
Imre Deakc6c46962016-04-01 16:02:40 +03005516void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305517{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305518 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005519 POSTING_READ(DBUF_CTL);
5520
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305521 udelay(10);
5522
5523 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5524 DRM_ERROR("DBuf power disable timeout!\n");
5525
5526 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
Imre Deakc6c46962016-04-01 16:02:40 +03005527 broxton_set_cdclk(dev_priv, 19200);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305528}
5529
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005530static const struct skl_cdclk_entry {
5531 unsigned int freq;
5532 unsigned int vco;
5533} skl_cdclk_frequencies[] = {
5534 { .freq = 308570, .vco = 8640 },
5535 { .freq = 337500, .vco = 8100 },
5536 { .freq = 432000, .vco = 8640 },
5537 { .freq = 450000, .vco = 8100 },
5538 { .freq = 540000, .vco = 8100 },
5539 { .freq = 617140, .vco = 8640 },
5540 { .freq = 675000, .vco = 8100 },
5541};
5542
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005543static unsigned int skl_cdclk_get_vco(unsigned int freq)
5544{
5545 unsigned int i;
5546
5547 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5548 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5549
5550 if (e->freq == freq)
5551 return e->vco;
5552 }
5553
5554 return 8100;
5555}
5556
5557static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005558skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005559{
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005560 int min_cdclk;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005561 u32 val;
5562
5563 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005564 if (vco == 8640)
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005565 min_cdclk = 308570;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005566 else
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005567 min_cdclk = 337500;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005568
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005569 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005570 I915_WRITE(CDCLK_CTL, val);
5571 POSTING_READ(CDCLK_CTL);
5572
5573 /*
5574 * We always enable DPLL0 with the lowest link rate possible, but still
5575 * taking into account the VCO required to operate the eDP panel at the
5576 * desired frequency. The usual DP link rates operate with a VCO of
5577 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5578 * The modeset code is responsible for the selection of the exact link
5579 * rate later on, with the constraint of choosing a frequency that
5580 * works with required_vco.
5581 */
5582 val = I915_READ(DPLL_CTRL1);
5583
5584 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5585 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5586 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005587 if (vco == 8640)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005588 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5589 SKL_DPLL0);
5590 else
5591 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5592 SKL_DPLL0);
5593
5594 I915_WRITE(DPLL_CTRL1, val);
5595 POSTING_READ(DPLL_CTRL1);
5596
5597 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5598
5599 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5600 DRM_ERROR("DPLL0 not locked\n");
5601}
5602
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005603static void
5604skl_dpll0_disable(struct drm_i915_private *dev_priv)
5605{
5606 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5607 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5608 DRM_ERROR("Couldn't disable DPLL0\n");
5609}
5610
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005611static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5612{
5613 int ret;
5614 u32 val;
5615
5616 /* inform PCU we want to change CDCLK */
5617 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5618 mutex_lock(&dev_priv->rps.hw_lock);
5619 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5620 mutex_unlock(&dev_priv->rps.hw_lock);
5621
5622 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5623}
5624
5625static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5626{
5627 unsigned int i;
5628
5629 for (i = 0; i < 15; i++) {
5630 if (skl_cdclk_pcu_ready(dev_priv))
5631 return true;
5632 udelay(10);
5633 }
5634
5635 return false;
5636}
5637
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005638static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005639{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005640 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005641 u32 freq_select, pcu_ack;
5642
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005643 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005644
5645 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5646 DRM_ERROR("failed to inform PCU about cdclk change\n");
5647 return;
5648 }
5649
5650 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005651 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005652 case 450000:
5653 case 432000:
5654 freq_select = CDCLK_FREQ_450_432;
5655 pcu_ack = 1;
5656 break;
5657 case 540000:
5658 freq_select = CDCLK_FREQ_540;
5659 pcu_ack = 2;
5660 break;
5661 case 308570:
5662 case 337500:
5663 default:
5664 freq_select = CDCLK_FREQ_337_308;
5665 pcu_ack = 0;
5666 break;
5667 case 617140:
5668 case 675000:
5669 freq_select = CDCLK_FREQ_675_617;
5670 pcu_ack = 3;
5671 break;
5672 }
5673
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005674 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005675 POSTING_READ(CDCLK_CTL);
5676
5677 /* inform PCU of the change */
5678 mutex_lock(&dev_priv->rps.hw_lock);
5679 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5680 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005681
5682 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005683}
5684
5685void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5686{
5687 /* disable DBUF power */
5688 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5689 POSTING_READ(DBUF_CTL);
5690
5691 udelay(10);
5692
5693 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5694 DRM_ERROR("DBuf power disable timeout\n");
5695
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005696 skl_dpll0_disable(dev_priv);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005697}
5698
5699void skl_init_cdclk(struct drm_i915_private *dev_priv)
5700{
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005701 unsigned int vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005702
Gary Wang39d9b852015-08-28 16:40:34 +08005703 /* DPLL0 not enabled (happens on early BIOS versions) */
5704 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5705 /* enable DPLL0 */
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005706 vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5707 skl_dpll0_enable(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005708 }
5709
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005710 /* set CDCLK to the frequency the BIOS chose */
5711 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5712
5713 /* enable DBUF power */
5714 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5715 POSTING_READ(DBUF_CTL);
5716
5717 udelay(10);
5718
5719 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5720 DRM_ERROR("DBuf power enable timeout\n");
5721}
5722
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305723int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5724{
5725 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5726 uint32_t cdctl = I915_READ(CDCLK_CTL);
5727 int freq = dev_priv->skl_boot_cdclk;
5728
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305729 /*
5730 * check if the pre-os intialized the display
5731 * There is SWF18 scratchpad register defined which is set by the
5732 * pre-os which can be used by the OS drivers to check the status
5733 */
5734 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5735 goto sanitize;
5736
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305737 /* Is PLL enabled and locked ? */
5738 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5739 goto sanitize;
5740
5741 /* DPLL okay; verify the cdclock
5742 *
5743 * Noticed in some instances that the freq selection is correct but
5744 * decimal part is programmed wrong from BIOS where pre-os does not
5745 * enable display. Verify the same as well.
5746 */
5747 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5748 /* All well; nothing to sanitize */
5749 return false;
5750sanitize:
5751 /*
5752 * As of now initialize with max cdclk till
5753 * we get dynamic cdclk support
5754 * */
5755 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5756 skl_init_cdclk(dev_priv);
5757
5758 /* we did have to sanitize */
5759 return true;
5760}
5761
Jesse Barnes30a970c2013-11-04 13:48:12 -08005762/* Adjust CDclk dividers to allow high res or save power if possible */
5763static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5764{
5765 struct drm_i915_private *dev_priv = dev->dev_private;
5766 u32 val, cmd;
5767
Vandana Kannan164dfd22014-11-24 13:37:41 +05305768 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5769 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005770
Ville Syrjälädfcab172014-06-13 13:37:47 +03005771 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005772 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005773 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005774 cmd = 1;
5775 else
5776 cmd = 0;
5777
5778 mutex_lock(&dev_priv->rps.hw_lock);
5779 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5780 val &= ~DSPFREQGUAR_MASK;
5781 val |= (cmd << DSPFREQGUAR_SHIFT);
5782 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5783 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5784 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5785 50)) {
5786 DRM_ERROR("timed out waiting for CDclk change\n");
5787 }
5788 mutex_unlock(&dev_priv->rps.hw_lock);
5789
Ville Syrjälä54433e92015-05-26 20:42:31 +03005790 mutex_lock(&dev_priv->sb_lock);
5791
Ville Syrjälädfcab172014-06-13 13:37:47 +03005792 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005793 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005794
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005795 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005796
Jesse Barnes30a970c2013-11-04 13:48:12 -08005797 /* adjust cdclk divider */
5798 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005799 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005800 val |= divider;
5801 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005802
5803 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005804 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005805 50))
5806 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005807 }
5808
Jesse Barnes30a970c2013-11-04 13:48:12 -08005809 /* adjust self-refresh exit latency value */
5810 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5811 val &= ~0x7f;
5812
5813 /*
5814 * For high bandwidth configs, we set a higher latency in the bunit
5815 * so that the core display fetch happens in time to avoid underruns.
5816 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005817 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005818 val |= 4500 / 250; /* 4.5 usec */
5819 else
5820 val |= 3000 / 250; /* 3.0 usec */
5821 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005822
Ville Syrjäläa5805162015-05-26 20:42:30 +03005823 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005824
Ville Syrjäläb6283052015-06-03 15:45:07 +03005825 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005826}
5827
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005828static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5829{
5830 struct drm_i915_private *dev_priv = dev->dev_private;
5831 u32 val, cmd;
5832
Vandana Kannan164dfd22014-11-24 13:37:41 +05305833 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5834 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005835
5836 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005837 case 333333:
5838 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005839 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005840 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005841 break;
5842 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005843 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005844 return;
5845 }
5846
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005847 /*
5848 * Specs are full of misinformation, but testing on actual
5849 * hardware has shown that we just need to write the desired
5850 * CCK divider into the Punit register.
5851 */
5852 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5853
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005854 mutex_lock(&dev_priv->rps.hw_lock);
5855 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5856 val &= ~DSPFREQGUAR_MASK_CHV;
5857 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5858 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5859 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5860 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5861 50)) {
5862 DRM_ERROR("timed out waiting for CDclk change\n");
5863 }
5864 mutex_unlock(&dev_priv->rps.hw_lock);
5865
Ville Syrjäläb6283052015-06-03 15:45:07 +03005866 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005867}
5868
Jesse Barnes30a970c2013-11-04 13:48:12 -08005869static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5870 int max_pixclk)
5871{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005872 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005873 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005874
Jesse Barnes30a970c2013-11-04 13:48:12 -08005875 /*
5876 * Really only a few cases to deal with, as only 4 CDclks are supported:
5877 * 200MHz
5878 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005879 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005880 * 400MHz (VLV only)
5881 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5882 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005883 *
5884 * We seem to get an unstable or solid color picture at 200MHz.
5885 * Not sure what's wrong. For now use 200MHz only when all pipes
5886 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005887 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005888 if (!IS_CHERRYVIEW(dev_priv) &&
5889 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005890 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005891 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005892 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005893 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005894 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005895 else
5896 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005897}
5898
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005899static int broxton_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005900{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305901 /*
5902 * FIXME:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305903 * - set 19.2MHz bypass frequency if there are no active pipes
5904 */
Ville Syrjälä760e1472016-05-11 22:44:46 +03005905 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305906 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005907 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305908 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005909 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305910 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005911 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305912 return 288000;
5913 else
5914 return 144000;
5915}
5916
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005917/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005918static int intel_mode_max_pixclk(struct drm_device *dev,
5919 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005920{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005921 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5922 struct drm_i915_private *dev_priv = dev->dev_private;
5923 struct drm_crtc *crtc;
5924 struct drm_crtc_state *crtc_state;
5925 unsigned max_pixclk = 0, i;
5926 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005927
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005928 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5929 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005930
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005931 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5932 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005933
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005934 if (crtc_state->enable)
5935 pixclk = crtc_state->adjusted_mode.crtc_clock;
5936
5937 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005938 }
5939
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005940 for_each_pipe(dev_priv, pipe)
5941 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5942
Jesse Barnes30a970c2013-11-04 13:48:12 -08005943 return max_pixclk;
5944}
5945
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005946static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005947{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005948 struct drm_device *dev = state->dev;
5949 struct drm_i915_private *dev_priv = dev->dev_private;
5950 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005951 struct intel_atomic_state *intel_state =
5952 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005953
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005954 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005955 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305956
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005957 if (!intel_state->active_crtcs)
5958 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5959
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005960 return 0;
5961}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005962
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005963static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5964{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03005965 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005966 struct intel_atomic_state *intel_state =
5967 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005968
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005969 intel_state->cdclk = intel_state->dev_cdclk =
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005970 broxton_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005971
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005972 if (!intel_state->active_crtcs)
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005973 intel_state->dev_cdclk = broxton_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005974
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005975 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005976}
5977
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005978static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5979{
5980 unsigned int credits, default_credits;
5981
5982 if (IS_CHERRYVIEW(dev_priv))
5983 default_credits = PFI_CREDIT(12);
5984 else
5985 default_credits = PFI_CREDIT(8);
5986
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005987 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005988 /* CHV suggested value is 31 or 63 */
5989 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005990 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005991 else
5992 credits = PFI_CREDIT(15);
5993 } else {
5994 credits = default_credits;
5995 }
5996
5997 /*
5998 * WA - write default credits before re-programming
5999 * FIXME: should we also set the resend bit here?
6000 */
6001 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6002 default_credits);
6003
6004 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6005 credits | PFI_CREDIT_RESEND);
6006
6007 /*
6008 * FIXME is this guaranteed to clear
6009 * immediately or should we poll for it?
6010 */
6011 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6012}
6013
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006014static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006015{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006016 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006017 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006018 struct intel_atomic_state *old_intel_state =
6019 to_intel_atomic_state(old_state);
6020 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006021
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006022 /*
6023 * FIXME: We can end up here with all power domains off, yet
6024 * with a CDCLK frequency other than the minimum. To account
6025 * for this take the PIPE-A power domain, which covers the HW
6026 * blocks needed for the following programming. This can be
6027 * removed once it's guaranteed that we get here either with
6028 * the minimum CDCLK set, or the required power domains
6029 * enabled.
6030 */
6031 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006032
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006033 if (IS_CHERRYVIEW(dev))
6034 cherryview_set_cdclk(dev, req_cdclk);
6035 else
6036 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006037
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006038 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006039
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006040 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006041}
6042
Jesse Barnes89b667f2013-04-18 14:51:36 -07006043static void valleyview_crtc_enable(struct drm_crtc *crtc)
6044{
6045 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006046 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6048 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006049 struct intel_crtc_state *pipe_config =
6050 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006051 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006052
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006053 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006054 return;
6055
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006056 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306057 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006058
6059 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006060 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006061
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006062 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6063 struct drm_i915_private *dev_priv = dev->dev_private;
6064
6065 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6066 I915_WRITE(CHV_CANVAS(pipe), 0);
6067 }
6068
Daniel Vetter5b18e572014-04-24 23:55:06 +02006069 i9xx_set_pipeconf(intel_crtc);
6070
Jesse Barnes89b667f2013-04-18 14:51:36 -07006071 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072
Daniel Vettera72e4c92014-09-30 10:56:47 +02006073 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006074
Jesse Barnes89b667f2013-04-18 14:51:36 -07006075 for_each_encoder_on_crtc(dev, crtc, encoder)
6076 if (encoder->pre_pll_enable)
6077 encoder->pre_pll_enable(encoder);
6078
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006079 if (IS_CHERRYVIEW(dev)) {
6080 chv_prepare_pll(intel_crtc, intel_crtc->config);
6081 chv_enable_pll(intel_crtc, intel_crtc->config);
6082 } else {
6083 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6084 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006085 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006086
6087 for_each_encoder_on_crtc(dev, crtc, encoder)
6088 if (encoder->pre_enable)
6089 encoder->pre_enable(encoder);
6090
Jesse Barnes2dd24552013-04-25 12:55:01 -07006091 i9xx_pfit_enable(intel_crtc);
6092
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006093 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006094
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006095 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006096 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006097
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006098 assert_vblank_disabled(crtc);
6099 drm_crtc_vblank_on(crtc);
6100
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006101 for_each_encoder_on_crtc(dev, crtc, encoder)
6102 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006103}
6104
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006105static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6106{
6107 struct drm_device *dev = crtc->base.dev;
6108 struct drm_i915_private *dev_priv = dev->dev_private;
6109
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006110 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6111 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006112}
6113
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006114static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006115{
6116 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006117 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006119 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006120 struct intel_crtc_state *pipe_config =
6121 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006122 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006123
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006124 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006125 return;
6126
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006127 i9xx_set_pll_dividers(intel_crtc);
6128
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006129 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306130 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006131
6132 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006133 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006134
Daniel Vetter5b18e572014-04-24 23:55:06 +02006135 i9xx_set_pipeconf(intel_crtc);
6136
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006137 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006138
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006139 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006140 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006141
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006142 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006143 if (encoder->pre_enable)
6144 encoder->pre_enable(encoder);
6145
Daniel Vetterf6736a12013-06-05 13:34:30 +02006146 i9xx_enable_pll(intel_crtc);
6147
Jesse Barnes2dd24552013-04-25 12:55:01 -07006148 i9xx_pfit_enable(intel_crtc);
6149
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006150 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006151
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006152 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006153 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006154
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006155 assert_vblank_disabled(crtc);
6156 drm_crtc_vblank_on(crtc);
6157
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006158 for_each_encoder_on_crtc(dev, crtc, encoder)
6159 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006160}
6161
Daniel Vetter87476d62013-04-11 16:29:06 +02006162static void i9xx_pfit_disable(struct intel_crtc *crtc)
6163{
6164 struct drm_device *dev = crtc->base.dev;
6165 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006166
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006167 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006168 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006169
6170 assert_pipe_disabled(dev_priv, crtc->pipe);
6171
Daniel Vetter328d8e82013-05-08 10:36:31 +02006172 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6173 I915_READ(PFIT_CONTROL));
6174 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006175}
6176
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006177static void i9xx_crtc_disable(struct drm_crtc *crtc)
6178{
6179 struct drm_device *dev = crtc->dev;
6180 struct drm_i915_private *dev_priv = dev->dev_private;
6181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006182 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006183 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006184
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006185 /*
6186 * On gen2 planes are double buffered but the pipe isn't, so we must
6187 * wait for planes to fully turn off before disabling the pipe.
6188 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006189 if (IS_GEN2(dev))
6190 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006191
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006192 for_each_encoder_on_crtc(dev, crtc, encoder)
6193 encoder->disable(encoder);
6194
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006195 drm_crtc_vblank_off(crtc);
6196 assert_vblank_disabled(crtc);
6197
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006198 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006199
Daniel Vetter87476d62013-04-11 16:29:06 +02006200 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006201
Jesse Barnes89b667f2013-04-18 14:51:36 -07006202 for_each_encoder_on_crtc(dev, crtc, encoder)
6203 if (encoder->post_disable)
6204 encoder->post_disable(encoder);
6205
Jani Nikulaa65347b2015-11-27 12:21:46 +02006206 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006207 if (IS_CHERRYVIEW(dev))
6208 chv_disable_pll(dev_priv, pipe);
6209 else if (IS_VALLEYVIEW(dev))
6210 vlv_disable_pll(dev_priv, pipe);
6211 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006212 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006213 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006214
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006215 for_each_encoder_on_crtc(dev, crtc, encoder)
6216 if (encoder->post_pll_disable)
6217 encoder->post_pll_disable(encoder);
6218
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006219 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006220 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006221}
6222
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006223static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006224{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006225 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006227 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006228 enum intel_display_power_domain domain;
6229 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006230
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006231 if (!intel_crtc->active)
6232 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006233
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006234 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +02006235 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006236
Ville Syrjälä2622a082016-03-09 19:07:26 +02006237 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006238
6239 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6240 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006241 }
6242
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006243 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006244
6245 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6246 crtc->base.id);
6247
6248 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6249 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006250 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006251 crtc->enabled = false;
6252 crtc->state->connector_mask = 0;
6253 crtc->state->encoder_mask = 0;
6254
6255 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6256 encoder->base.crtc = NULL;
6257
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006258 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006259 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006260 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006261
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006262 domains = intel_crtc->enabled_power_domains;
6263 for_each_power_domain(domain, domains)
6264 intel_display_power_put(dev_priv, domain);
6265 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006266
6267 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6268 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006269}
6270
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006271/*
6272 * turn all crtc's off, but do not adjust state
6273 * This has to be paired with a call to intel_modeset_setup_hw_state.
6274 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006275int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006276{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006277 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006278 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006279 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006280
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006281 state = drm_atomic_helper_suspend(dev);
6282 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006283 if (ret)
6284 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006285 else
6286 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006287 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006288}
6289
Chris Wilsonea5b2132010-08-04 13:50:23 +01006290void intel_encoder_destroy(struct drm_encoder *encoder)
6291{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006292 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006293
Chris Wilsonea5b2132010-08-04 13:50:23 +01006294 drm_encoder_cleanup(encoder);
6295 kfree(intel_encoder);
6296}
6297
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006298/* Cross check the actual hw state with our own modeset state tracking (and it's
6299 * internal consistency). */
Maarten Lankhorstc0ead702016-03-30 10:00:05 +02006300static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006301{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006302 struct drm_crtc *crtc = connector->base.state->crtc;
6303
6304 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6305 connector->base.base.id,
6306 connector->base.name);
6307
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006308 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006309 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006310 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006311
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006312 I915_STATE_WARN(!crtc,
6313 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006314
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006315 if (!crtc)
6316 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006317
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006318 I915_STATE_WARN(!crtc->state->active,
6319 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006320
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006321 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006322 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006323
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006324 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006325 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006326
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006327 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006328 "attached encoder crtc differs from connector crtc\n");
6329 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006330 I915_STATE_WARN(crtc && crtc->state->active,
6331 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006332 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6333 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006334 }
6335}
6336
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006337int intel_connector_init(struct intel_connector *connector)
6338{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006339 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006340
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006341 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006342 return -ENOMEM;
6343
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006344 return 0;
6345}
6346
6347struct intel_connector *intel_connector_alloc(void)
6348{
6349 struct intel_connector *connector;
6350
6351 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6352 if (!connector)
6353 return NULL;
6354
6355 if (intel_connector_init(connector) < 0) {
6356 kfree(connector);
6357 return NULL;
6358 }
6359
6360 return connector;
6361}
6362
Daniel Vetterf0947c32012-07-02 13:10:34 +02006363/* Simple connector->get_hw_state implementation for encoders that support only
6364 * one connector and no cloning and hence the encoder state determines the state
6365 * of the connector. */
6366bool intel_connector_get_hw_state(struct intel_connector *connector)
6367{
Daniel Vetter24929352012-07-02 20:28:59 +02006368 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006369 struct intel_encoder *encoder = connector->encoder;
6370
6371 return encoder->get_hw_state(encoder, &pipe);
6372}
6373
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006374static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006375{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006376 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6377 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006378
6379 return 0;
6380}
6381
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006382static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006383 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006384{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006385 struct drm_atomic_state *state = pipe_config->base.state;
6386 struct intel_crtc *other_crtc;
6387 struct intel_crtc_state *other_crtc_state;
6388
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006389 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6390 pipe_name(pipe), pipe_config->fdi_lanes);
6391 if (pipe_config->fdi_lanes > 4) {
6392 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6393 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006395 }
6396
Paulo Zanonibafb6552013-11-02 21:07:44 -07006397 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006398 if (pipe_config->fdi_lanes > 2) {
6399 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6400 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006402 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006403 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006404 }
6405 }
6406
6407 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006409
6410 /* Ivybridge 3 pipe is really complicated */
6411 switch (pipe) {
6412 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006413 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006414 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415 if (pipe_config->fdi_lanes <= 2)
6416 return 0;
6417
6418 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6419 other_crtc_state =
6420 intel_atomic_get_crtc_state(state, other_crtc);
6421 if (IS_ERR(other_crtc_state))
6422 return PTR_ERR(other_crtc_state);
6423
6424 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006425 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6426 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006428 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006429 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006430 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006431 if (pipe_config->fdi_lanes > 2) {
6432 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6433 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006435 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436
6437 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6438 other_crtc_state =
6439 intel_atomic_get_crtc_state(state, other_crtc);
6440 if (IS_ERR(other_crtc_state))
6441 return PTR_ERR(other_crtc_state);
6442
6443 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006444 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006445 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006446 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006447 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448 default:
6449 BUG();
6450 }
6451}
6452
Daniel Vettere29c22c2013-02-21 00:00:16 +01006453#define RETRY 1
6454static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006455 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006456{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006457 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006458 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006459 int lane, link_bw, fdi_dotclock, ret;
6460 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006461
Daniel Vettere29c22c2013-02-21 00:00:16 +01006462retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006463 /* FDI is a binary signal running at ~2.7GHz, encoding
6464 * each output octet as 10 bits. The actual frequency
6465 * is stored as a divider into a 100MHz clock, and the
6466 * mode pixel clock is stored in units of 1KHz.
6467 * Hence the bw of each lane in terms of the mode signal
6468 * is:
6469 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006470 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006471
Damien Lespiau241bfc32013-09-25 16:45:37 +01006472 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006473
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006474 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006475 pipe_config->pipe_bpp);
6476
6477 pipe_config->fdi_lanes = lane;
6478
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006479 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006480 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006481
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006482 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006483 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006484 pipe_config->pipe_bpp -= 2*3;
6485 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6486 pipe_config->pipe_bpp);
6487 needs_recompute = true;
6488 pipe_config->bw_constrained = true;
6489
6490 goto retry;
6491 }
6492
6493 if (needs_recompute)
6494 return RETRY;
6495
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006496 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006497}
6498
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006499static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6500 struct intel_crtc_state *pipe_config)
6501{
6502 if (pipe_config->pipe_bpp > 24)
6503 return false;
6504
6505 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006506 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006507 return true;
6508
6509 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006510 * We compare against max which means we must take
6511 * the increased cdclk requirement into account when
6512 * calculating the new cdclk.
6513 *
6514 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006515 */
6516 return ilk_pipe_pixel_rate(pipe_config) <=
6517 dev_priv->max_cdclk_freq * 95 / 100;
6518}
6519
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006520static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006521 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006522{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006523 struct drm_device *dev = crtc->base.dev;
6524 struct drm_i915_private *dev_priv = dev->dev_private;
6525
Jani Nikulad330a952014-01-21 11:24:25 +02006526 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006527 hsw_crtc_supports_ips(crtc) &&
6528 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006529}
6530
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006531static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6532{
6533 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6534
6535 /* GDG double wide on either pipe, otherwise pipe A only */
6536 return INTEL_INFO(dev_priv)->gen < 4 &&
6537 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6538}
6539
Daniel Vettera43f6e02013-06-07 23:10:32 +02006540static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006541 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006542{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006543 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006544 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006545 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006546
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006547 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006548 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006549 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006550
6551 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006552 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006553 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006554 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006555 if (intel_crtc_supports_double_wide(crtc) &&
6556 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006557 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006558 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006559 }
6560
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006561 if (adjusted_mode->crtc_clock > clock_limit) {
6562 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6563 adjusted_mode->crtc_clock, clock_limit,
6564 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006565 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006566 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006567 }
Chris Wilson89749352010-09-12 18:25:19 +01006568
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006569 /*
6570 * Pipe horizontal size must be even in:
6571 * - DVO ganged mode
6572 * - LVDS dual channel mode
6573 * - Double wide pipe
6574 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006575 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006576 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6577 pipe_config->pipe_src_w &= ~1;
6578
Damien Lespiau8693a822013-05-03 18:48:11 +01006579 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6580 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006581 */
6582 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006583 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006584 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006585
Damien Lespiauf5adf942013-06-24 18:29:34 +01006586 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006587 hsw_compute_ips_config(crtc, pipe_config);
6588
Daniel Vetter877d48d2013-04-19 11:24:43 +02006589 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006590 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006591
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006592 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006593}
6594
Ville Syrjälä1652d192015-03-31 14:12:01 +03006595static int skylake_get_display_clock_speed(struct drm_device *dev)
6596{
6597 struct drm_i915_private *dev_priv = to_i915(dev);
6598 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6599 uint32_t cdctl = I915_READ(CDCLK_CTL);
6600 uint32_t linkrate;
6601
Damien Lespiau414355a2015-06-04 18:21:31 +01006602 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006603 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006604
6605 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6606 return 540000;
6607
6608 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006609 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006610
Damien Lespiau71cd8422015-04-30 16:39:17 +01006611 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6612 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006613 /* vco 8640 */
6614 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6615 case CDCLK_FREQ_450_432:
6616 return 432000;
6617 case CDCLK_FREQ_337_308:
6618 return 308570;
6619 case CDCLK_FREQ_675_617:
6620 return 617140;
6621 default:
6622 WARN(1, "Unknown cd freq selection\n");
6623 }
6624 } else {
6625 /* vco 8100 */
6626 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6627 case CDCLK_FREQ_450_432:
6628 return 450000;
6629 case CDCLK_FREQ_337_308:
6630 return 337500;
6631 case CDCLK_FREQ_675_617:
6632 return 675000;
6633 default:
6634 WARN(1, "Unknown cd freq selection\n");
6635 }
6636 }
6637
6638 /* error case, do as if DPLL0 isn't enabled */
6639 return 24000;
6640}
6641
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006642static int broxton_get_display_clock_speed(struct drm_device *dev)
6643{
6644 struct drm_i915_private *dev_priv = to_i915(dev);
6645 uint32_t cdctl = I915_READ(CDCLK_CTL);
6646 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6647 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6648 int cdclk;
6649
6650 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6651 return 19200;
6652
6653 cdclk = 19200 * pll_ratio / 2;
6654
6655 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6656 case BXT_CDCLK_CD2X_DIV_SEL_1:
6657 return cdclk; /* 576MHz or 624MHz */
6658 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6659 return cdclk * 2 / 3; /* 384MHz */
6660 case BXT_CDCLK_CD2X_DIV_SEL_2:
6661 return cdclk / 2; /* 288MHz */
6662 case BXT_CDCLK_CD2X_DIV_SEL_4:
6663 return cdclk / 4; /* 144MHz */
6664 }
6665
6666 /* error case, do as if DE PLL isn't enabled */
6667 return 19200;
6668}
6669
Ville Syrjälä1652d192015-03-31 14:12:01 +03006670static int broadwell_get_display_clock_speed(struct drm_device *dev)
6671{
6672 struct drm_i915_private *dev_priv = dev->dev_private;
6673 uint32_t lcpll = I915_READ(LCPLL_CTL);
6674 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6675
6676 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6677 return 800000;
6678 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6679 return 450000;
6680 else if (freq == LCPLL_CLK_FREQ_450)
6681 return 450000;
6682 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6683 return 540000;
6684 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6685 return 337500;
6686 else
6687 return 675000;
6688}
6689
6690static int haswell_get_display_clock_speed(struct drm_device *dev)
6691{
6692 struct drm_i915_private *dev_priv = dev->dev_private;
6693 uint32_t lcpll = I915_READ(LCPLL_CTL);
6694 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6695
6696 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6697 return 800000;
6698 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6699 return 450000;
6700 else if (freq == LCPLL_CLK_FREQ_450)
6701 return 450000;
6702 else if (IS_HSW_ULT(dev))
6703 return 337500;
6704 else
6705 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006706}
6707
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006708static int valleyview_get_display_clock_speed(struct drm_device *dev)
6709{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006710 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6711 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006712}
6713
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006714static int ilk_get_display_clock_speed(struct drm_device *dev)
6715{
6716 return 450000;
6717}
6718
Jesse Barnese70236a2009-09-21 10:42:27 -07006719static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006720{
Jesse Barnese70236a2009-09-21 10:42:27 -07006721 return 400000;
6722}
Jesse Barnes79e53942008-11-07 14:24:08 -08006723
Jesse Barnese70236a2009-09-21 10:42:27 -07006724static int i915_get_display_clock_speed(struct drm_device *dev)
6725{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006726 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006727}
Jesse Barnes79e53942008-11-07 14:24:08 -08006728
Jesse Barnese70236a2009-09-21 10:42:27 -07006729static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6730{
6731 return 200000;
6732}
Jesse Barnes79e53942008-11-07 14:24:08 -08006733
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006734static int pnv_get_display_clock_speed(struct drm_device *dev)
6735{
6736 u16 gcfgc = 0;
6737
6738 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6739
6740 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6741 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006742 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006743 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006744 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006745 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006746 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006747 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6748 return 200000;
6749 default:
6750 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6751 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006752 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006753 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006754 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006755 }
6756}
6757
Jesse Barnese70236a2009-09-21 10:42:27 -07006758static int i915gm_get_display_clock_speed(struct drm_device *dev)
6759{
6760 u16 gcfgc = 0;
6761
6762 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6763
6764 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006765 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006766 else {
6767 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6768 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006769 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006770 default:
6771 case GC_DISPLAY_CLOCK_190_200_MHZ:
6772 return 190000;
6773 }
6774 }
6775}
Jesse Barnes79e53942008-11-07 14:24:08 -08006776
Jesse Barnese70236a2009-09-21 10:42:27 -07006777static int i865_get_display_clock_speed(struct drm_device *dev)
6778{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006779 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006780}
6781
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006782static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006783{
6784 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006785
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006786 /*
6787 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6788 * encoding is different :(
6789 * FIXME is this the right way to detect 852GM/852GMV?
6790 */
6791 if (dev->pdev->revision == 0x1)
6792 return 133333;
6793
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006794 pci_bus_read_config_word(dev->pdev->bus,
6795 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6796
Jesse Barnese70236a2009-09-21 10:42:27 -07006797 /* Assume that the hardware is in the high speed state. This
6798 * should be the default.
6799 */
6800 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6801 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006802 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006803 case GC_CLOCK_100_200:
6804 return 200000;
6805 case GC_CLOCK_166_250:
6806 return 250000;
6807 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006808 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006809 case GC_CLOCK_133_266:
6810 case GC_CLOCK_133_266_2:
6811 case GC_CLOCK_166_266:
6812 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006813 }
6814
6815 /* Shouldn't happen */
6816 return 0;
6817}
6818
6819static int i830_get_display_clock_speed(struct drm_device *dev)
6820{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006821 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006822}
6823
Ville Syrjälä34edce22015-05-22 11:22:33 +03006824static unsigned int intel_hpll_vco(struct drm_device *dev)
6825{
6826 struct drm_i915_private *dev_priv = dev->dev_private;
6827 static const unsigned int blb_vco[8] = {
6828 [0] = 3200000,
6829 [1] = 4000000,
6830 [2] = 5333333,
6831 [3] = 4800000,
6832 [4] = 6400000,
6833 };
6834 static const unsigned int pnv_vco[8] = {
6835 [0] = 3200000,
6836 [1] = 4000000,
6837 [2] = 5333333,
6838 [3] = 4800000,
6839 [4] = 2666667,
6840 };
6841 static const unsigned int cl_vco[8] = {
6842 [0] = 3200000,
6843 [1] = 4000000,
6844 [2] = 5333333,
6845 [3] = 6400000,
6846 [4] = 3333333,
6847 [5] = 3566667,
6848 [6] = 4266667,
6849 };
6850 static const unsigned int elk_vco[8] = {
6851 [0] = 3200000,
6852 [1] = 4000000,
6853 [2] = 5333333,
6854 [3] = 4800000,
6855 };
6856 static const unsigned int ctg_vco[8] = {
6857 [0] = 3200000,
6858 [1] = 4000000,
6859 [2] = 5333333,
6860 [3] = 6400000,
6861 [4] = 2666667,
6862 [5] = 4266667,
6863 };
6864 const unsigned int *vco_table;
6865 unsigned int vco;
6866 uint8_t tmp = 0;
6867
6868 /* FIXME other chipsets? */
6869 if (IS_GM45(dev))
6870 vco_table = ctg_vco;
6871 else if (IS_G4X(dev))
6872 vco_table = elk_vco;
6873 else if (IS_CRESTLINE(dev))
6874 vco_table = cl_vco;
6875 else if (IS_PINEVIEW(dev))
6876 vco_table = pnv_vco;
6877 else if (IS_G33(dev))
6878 vco_table = blb_vco;
6879 else
6880 return 0;
6881
6882 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6883
6884 vco = vco_table[tmp & 0x7];
6885 if (vco == 0)
6886 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6887 else
6888 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6889
6890 return vco;
6891}
6892
6893static int gm45_get_display_clock_speed(struct drm_device *dev)
6894{
6895 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6896 uint16_t tmp = 0;
6897
6898 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6899
6900 cdclk_sel = (tmp >> 12) & 0x1;
6901
6902 switch (vco) {
6903 case 2666667:
6904 case 4000000:
6905 case 5333333:
6906 return cdclk_sel ? 333333 : 222222;
6907 case 3200000:
6908 return cdclk_sel ? 320000 : 228571;
6909 default:
6910 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6911 return 222222;
6912 }
6913}
6914
6915static int i965gm_get_display_clock_speed(struct drm_device *dev)
6916{
6917 static const uint8_t div_3200[] = { 16, 10, 8 };
6918 static const uint8_t div_4000[] = { 20, 12, 10 };
6919 static const uint8_t div_5333[] = { 24, 16, 14 };
6920 const uint8_t *div_table;
6921 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6922 uint16_t tmp = 0;
6923
6924 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6925
6926 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6927
6928 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6929 goto fail;
6930
6931 switch (vco) {
6932 case 3200000:
6933 div_table = div_3200;
6934 break;
6935 case 4000000:
6936 div_table = div_4000;
6937 break;
6938 case 5333333:
6939 div_table = div_5333;
6940 break;
6941 default:
6942 goto fail;
6943 }
6944
6945 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6946
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006947fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006948 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6949 return 200000;
6950}
6951
6952static int g33_get_display_clock_speed(struct drm_device *dev)
6953{
6954 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6955 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6956 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6957 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6958 const uint8_t *div_table;
6959 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6960 uint16_t tmp = 0;
6961
6962 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6963
6964 cdclk_sel = (tmp >> 4) & 0x7;
6965
6966 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6967 goto fail;
6968
6969 switch (vco) {
6970 case 3200000:
6971 div_table = div_3200;
6972 break;
6973 case 4000000:
6974 div_table = div_4000;
6975 break;
6976 case 4800000:
6977 div_table = div_4800;
6978 break;
6979 case 5333333:
6980 div_table = div_5333;
6981 break;
6982 default:
6983 goto fail;
6984 }
6985
6986 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6987
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006988fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006989 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6990 return 190476;
6991}
6992
Zhenyu Wang2c072452009-06-05 15:38:42 +08006993static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006994intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006995{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006996 while (*num > DATA_LINK_M_N_MASK ||
6997 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006998 *num >>= 1;
6999 *den >>= 1;
7000 }
7001}
7002
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007003static void compute_m_n(unsigned int m, unsigned int n,
7004 uint32_t *ret_m, uint32_t *ret_n)
7005{
7006 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7007 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7008 intel_reduce_m_n_ratio(ret_m, ret_n);
7009}
7010
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007011void
7012intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7013 int pixel_clock, int link_clock,
7014 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007015{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007016 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007017
7018 compute_m_n(bits_per_pixel * pixel_clock,
7019 link_clock * nlanes * 8,
7020 &m_n->gmch_m, &m_n->gmch_n);
7021
7022 compute_m_n(pixel_clock, link_clock,
7023 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007024}
7025
Chris Wilsona7615032011-01-12 17:04:08 +00007026static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7027{
Jani Nikulad330a952014-01-21 11:24:25 +02007028 if (i915.panel_use_ssc >= 0)
7029 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007030 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007031 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007032}
7033
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007034static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007035{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007036 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007037}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007038
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007039static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7040{
7041 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007042}
7043
Daniel Vetterf47709a2013-03-28 10:42:02 +01007044static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007045 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007046 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007047{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007048 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007049 u32 fp, fp2 = 0;
7050
7051 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007052 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007053 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007054 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007055 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007056 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007057 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007058 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007059 }
7060
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007061 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007062
Daniel Vetterf47709a2013-03-28 10:42:02 +01007063 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007064 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007065 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007066 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007067 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007068 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007069 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007070 }
7071}
7072
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007073static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7074 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007075{
7076 u32 reg_val;
7077
7078 /*
7079 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7080 * and set it to a reasonable value instead.
7081 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007082 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007083 reg_val &= 0xffffff00;
7084 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007085 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007087 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007088 reg_val &= 0x8cffffff;
7089 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007090 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007091
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007092 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007093 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007094 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007095
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007096 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007097 reg_val &= 0x00ffffff;
7098 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007099 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007100}
7101
Daniel Vetterb5518422013-05-03 11:49:48 +02007102static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7103 struct intel_link_m_n *m_n)
7104{
7105 struct drm_device *dev = crtc->base.dev;
7106 struct drm_i915_private *dev_priv = dev->dev_private;
7107 int pipe = crtc->pipe;
7108
Daniel Vettere3b95f12013-05-03 11:49:49 +02007109 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7110 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7111 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7112 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007113}
7114
7115static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007116 struct intel_link_m_n *m_n,
7117 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007118{
7119 struct drm_device *dev = crtc->base.dev;
7120 struct drm_i915_private *dev_priv = dev->dev_private;
7121 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007122 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007123
7124 if (INTEL_INFO(dev)->gen >= 5) {
7125 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7126 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7127 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7128 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007129 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7130 * for gen < 8) and if DRRS is supported (to make sure the
7131 * registers are not unnecessarily accessed).
7132 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307133 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007134 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007135 I915_WRITE(PIPE_DATA_M2(transcoder),
7136 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7137 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7138 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7139 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7140 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007141 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007142 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7143 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7144 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7145 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007146 }
7147}
7148
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307149void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007150{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307151 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7152
7153 if (m_n == M1_N1) {
7154 dp_m_n = &crtc->config->dp_m_n;
7155 dp_m2_n2 = &crtc->config->dp_m2_n2;
7156 } else if (m_n == M2_N2) {
7157
7158 /*
7159 * M2_N2 registers are not supported. Hence m2_n2 divider value
7160 * needs to be programmed into M1_N1.
7161 */
7162 dp_m_n = &crtc->config->dp_m2_n2;
7163 } else {
7164 DRM_ERROR("Unsupported divider value\n");
7165 return;
7166 }
7167
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007168 if (crtc->config->has_pch_encoder)
7169 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007170 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307171 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007172}
7173
Daniel Vetter251ac862015-06-18 10:30:24 +02007174static void vlv_compute_dpll(struct intel_crtc *crtc,
7175 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007176{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007177 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007178 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007179 if (crtc->pipe != PIPE_A)
7180 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007181
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007182 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007183 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007184 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7185 DPLL_EXT_BUFFER_ENABLE_VLV;
7186
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007187 pipe_config->dpll_hw_state.dpll_md =
7188 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7189}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007190
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007191static void chv_compute_dpll(struct intel_crtc *crtc,
7192 struct intel_crtc_state *pipe_config)
7193{
7194 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007195 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007196 if (crtc->pipe != PIPE_A)
7197 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7198
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007199 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007200 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007201 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7202
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007203 pipe_config->dpll_hw_state.dpll_md =
7204 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007205}
7206
Ville Syrjäläd288f652014-10-28 13:20:22 +02007207static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007208 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007209{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007210 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007211 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007212 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007213 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007214 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007215 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007216
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007217 /* Enable Refclk */
7218 I915_WRITE(DPLL(pipe),
7219 pipe_config->dpll_hw_state.dpll &
7220 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7221
7222 /* No need to actually set up the DPLL with DSI */
7223 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7224 return;
7225
Ville Syrjäläa5805162015-05-26 20:42:30 +03007226 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007227
Ville Syrjäläd288f652014-10-28 13:20:22 +02007228 bestn = pipe_config->dpll.n;
7229 bestm1 = pipe_config->dpll.m1;
7230 bestm2 = pipe_config->dpll.m2;
7231 bestp1 = pipe_config->dpll.p1;
7232 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007233
Jesse Barnes89b667f2013-04-18 14:51:36 -07007234 /* See eDP HDMI DPIO driver vbios notes doc */
7235
7236 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007237 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007238 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007239
7240 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007241 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007242
7243 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007244 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007245 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007246 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247
7248 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007249 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007250
7251 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007252 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7253 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7254 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007255 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007256
7257 /*
7258 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7259 * but we don't support that).
7260 * Note: don't use the DAC post divider as it seems unstable.
7261 */
7262 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007263 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007264
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007265 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007267
Jesse Barnes89b667f2013-04-18 14:51:36 -07007268 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007269 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007270 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7271 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007273 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007274 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007275 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007276 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007277
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007278 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007279 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007280 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007282 0x0df40000);
7283 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007284 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285 0x0df70000);
7286 } else { /* HDMI or VGA */
7287 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007288 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007289 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007290 0x0df70000);
7291 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007292 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007293 0x0df40000);
7294 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007295
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007296 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007297 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007298 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7299 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007300 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007301 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007302
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007303 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007304 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007305}
7306
Ville Syrjäläd288f652014-10-28 13:20:22 +02007307static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007308 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007309{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007310 struct drm_device *dev = crtc->base.dev;
7311 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007312 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007313 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307314 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007315 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307316 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307317 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007318
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007319 /* Enable Refclk and SSC */
7320 I915_WRITE(DPLL(pipe),
7321 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7322
7323 /* No need to actually set up the DPLL with DSI */
7324 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7325 return;
7326
Ville Syrjäläd288f652014-10-28 13:20:22 +02007327 bestn = pipe_config->dpll.n;
7328 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7329 bestm1 = pipe_config->dpll.m1;
7330 bestm2 = pipe_config->dpll.m2 >> 22;
7331 bestp1 = pipe_config->dpll.p1;
7332 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307333 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307334 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307335 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007336
Ville Syrjäläa5805162015-05-26 20:42:30 +03007337 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007338
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007339 /* p1 and p2 divider */
7340 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7341 5 << DPIO_CHV_S1_DIV_SHIFT |
7342 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7343 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7344 1 << DPIO_CHV_K_DIV_SHIFT);
7345
7346 /* Feedback post-divider - m2 */
7347 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7348
7349 /* Feedback refclk divider - n and m1 */
7350 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7351 DPIO_CHV_M1_DIV_BY_2 |
7352 1 << DPIO_CHV_N_DIV_SHIFT);
7353
7354 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007355 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007356
7357 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307358 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7359 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7360 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7361 if (bestm2_frac)
7362 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7363 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007364
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307365 /* Program digital lock detect threshold */
7366 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7367 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7368 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7369 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7370 if (!bestm2_frac)
7371 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7372 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7373
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007374 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307375 if (vco == 5400000) {
7376 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7377 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7378 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7379 tribuf_calcntr = 0x9;
7380 } else if (vco <= 6200000) {
7381 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7382 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7383 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7384 tribuf_calcntr = 0x9;
7385 } else if (vco <= 6480000) {
7386 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7387 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7388 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7389 tribuf_calcntr = 0x8;
7390 } else {
7391 /* Not supported. Apply the same limits as in the max case */
7392 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7393 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7394 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7395 tribuf_calcntr = 0;
7396 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007397 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7398
Ville Syrjälä968040b2015-03-11 22:52:08 +02007399 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307400 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7401 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7402 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7403
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007404 /* AFC Recal */
7405 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7406 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7407 DPIO_AFC_RECAL);
7408
Ville Syrjäläa5805162015-05-26 20:42:30 +03007409 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007410}
7411
Ville Syrjäläd288f652014-10-28 13:20:22 +02007412/**
7413 * vlv_force_pll_on - forcibly enable just the PLL
7414 * @dev_priv: i915 private structure
7415 * @pipe: pipe PLL to enable
7416 * @dpll: PLL configuration
7417 *
7418 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7419 * in cases where we need the PLL enabled even when @pipe is not going to
7420 * be enabled.
7421 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007422int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7423 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007424{
7425 struct intel_crtc *crtc =
7426 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007427 struct intel_crtc_state *pipe_config;
7428
7429 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7430 if (!pipe_config)
7431 return -ENOMEM;
7432
7433 pipe_config->base.crtc = &crtc->base;
7434 pipe_config->pixel_multiplier = 1;
7435 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007436
7437 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007438 chv_compute_dpll(crtc, pipe_config);
7439 chv_prepare_pll(crtc, pipe_config);
7440 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007441 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007442 vlv_compute_dpll(crtc, pipe_config);
7443 vlv_prepare_pll(crtc, pipe_config);
7444 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007445 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007446
7447 kfree(pipe_config);
7448
7449 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007450}
7451
7452/**
7453 * vlv_force_pll_off - forcibly disable just the PLL
7454 * @dev_priv: i915 private structure
7455 * @pipe: pipe PLL to disable
7456 *
7457 * Disable the PLL for @pipe. To be used in cases where we need
7458 * the PLL enabled even when @pipe is not going to be enabled.
7459 */
7460void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7461{
7462 if (IS_CHERRYVIEW(dev))
7463 chv_disable_pll(to_i915(dev), pipe);
7464 else
7465 vlv_disable_pll(to_i915(dev), pipe);
7466}
7467
Daniel Vetter251ac862015-06-18 10:30:24 +02007468static void i9xx_compute_dpll(struct intel_crtc *crtc,
7469 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007470 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007471{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007472 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007473 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007474 u32 dpll;
7475 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007476 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007477
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007478 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307479
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007480 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7481 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007482
7483 dpll = DPLL_VGA_MODE_DIS;
7484
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007485 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007486 dpll |= DPLLB_MODE_LVDS;
7487 else
7488 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007489
Daniel Vetteref1b4602013-06-01 17:17:04 +02007490 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007491 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007492 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007493 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007494
7495 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007496 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007497
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007498 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007499 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007500
7501 /* compute bitmask from p1 value */
7502 if (IS_PINEVIEW(dev))
7503 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7504 else {
7505 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7506 if (IS_G4X(dev) && reduced_clock)
7507 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7508 }
7509 switch (clock->p2) {
7510 case 5:
7511 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7512 break;
7513 case 7:
7514 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7515 break;
7516 case 10:
7517 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7518 break;
7519 case 14:
7520 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7521 break;
7522 }
7523 if (INTEL_INFO(dev)->gen >= 4)
7524 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7525
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007526 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007528 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007529 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7531 else
7532 dpll |= PLL_REF_INPUT_DREFCLK;
7533
7534 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007535 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007536
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007538 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007539 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007540 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007541 }
7542}
7543
Daniel Vetter251ac862015-06-18 10:30:24 +02007544static void i8xx_compute_dpll(struct intel_crtc *crtc,
7545 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007546 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007547{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007548 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007549 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007550 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007551 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007552
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007553 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307554
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007555 dpll = DPLL_VGA_MODE_DIS;
7556
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007557 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007558 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7559 } else {
7560 if (clock->p1 == 2)
7561 dpll |= PLL_P1_DIVIDE_BY_TWO;
7562 else
7563 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7564 if (clock->p2 == 4)
7565 dpll |= PLL_P2_DIVIDE_BY_4;
7566 }
7567
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007568 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007569 dpll |= DPLL_DVO_2X_MODE;
7570
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007571 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007572 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7574 else
7575 dpll |= PLL_REF_INPUT_DREFCLK;
7576
7577 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007578 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007579}
7580
Daniel Vetter8a654f32013-06-01 17:16:22 +02007581static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007582{
7583 struct drm_device *dev = intel_crtc->base.dev;
7584 struct drm_i915_private *dev_priv = dev->dev_private;
7585 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007586 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007587 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007588 uint32_t crtc_vtotal, crtc_vblank_end;
7589 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007590
7591 /* We need to be careful not to changed the adjusted mode, for otherwise
7592 * the hw state checker will get angry at the mismatch. */
7593 crtc_vtotal = adjusted_mode->crtc_vtotal;
7594 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007595
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007596 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007597 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007598 crtc_vtotal -= 1;
7599 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007600
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007601 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007602 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7603 else
7604 vsyncshift = adjusted_mode->crtc_hsync_start -
7605 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007606 if (vsyncshift < 0)
7607 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007608 }
7609
7610 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007611 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007612
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007613 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007614 (adjusted_mode->crtc_hdisplay - 1) |
7615 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007616 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007617 (adjusted_mode->crtc_hblank_start - 1) |
7618 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007619 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007620 (adjusted_mode->crtc_hsync_start - 1) |
7621 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7622
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007623 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007624 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007625 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007626 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007627 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007628 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007629 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007630 (adjusted_mode->crtc_vsync_start - 1) |
7631 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7632
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007633 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7634 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7635 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7636 * bits. */
7637 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7638 (pipe == PIPE_B || pipe == PIPE_C))
7639 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7640
Jani Nikulabc58be62016-03-18 17:05:39 +02007641}
7642
7643static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7644{
7645 struct drm_device *dev = intel_crtc->base.dev;
7646 struct drm_i915_private *dev_priv = dev->dev_private;
7647 enum pipe pipe = intel_crtc->pipe;
7648
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007649 /* pipesrc controls the size that is scaled from, which should
7650 * always be the user's requested size.
7651 */
7652 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007653 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7654 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007655}
7656
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007657static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007658 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007659{
7660 struct drm_device *dev = crtc->base.dev;
7661 struct drm_i915_private *dev_priv = dev->dev_private;
7662 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7663 uint32_t tmp;
7664
7665 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007666 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7667 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007668 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007669 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7670 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007671 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007672 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7673 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007674
7675 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007676 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7677 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007678 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007679 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7680 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007681 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007682 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7683 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007684
7685 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007686 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7687 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7688 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007689 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007690}
7691
7692static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7693 struct intel_crtc_state *pipe_config)
7694{
7695 struct drm_device *dev = crtc->base.dev;
7696 struct drm_i915_private *dev_priv = dev->dev_private;
7697 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007698
7699 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007700 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7701 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7702
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007703 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7704 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007705}
7706
Daniel Vetterf6a83282014-02-11 15:28:57 -08007707void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007708 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007709{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007710 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7711 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7712 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7713 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007714
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007715 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7716 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7717 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7718 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007719
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007720 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007721 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007722
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007723 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7724 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007725
7726 mode->hsync = drm_mode_hsync(mode);
7727 mode->vrefresh = drm_mode_vrefresh(mode);
7728 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007729}
7730
Daniel Vetter84b046f2013-02-19 18:48:54 +01007731static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7732{
7733 struct drm_device *dev = intel_crtc->base.dev;
7734 struct drm_i915_private *dev_priv = dev->dev_private;
7735 uint32_t pipeconf;
7736
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007737 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007738
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007739 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7740 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7741 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007742
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007743 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007744 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007745
Daniel Vetterff9ce462013-04-24 14:57:17 +02007746 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007747 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007748 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007749 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007750 pipeconf |= PIPECONF_DITHER_EN |
7751 PIPECONF_DITHER_TYPE_SP;
7752
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007753 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007754 case 18:
7755 pipeconf |= PIPECONF_6BPC;
7756 break;
7757 case 24:
7758 pipeconf |= PIPECONF_8BPC;
7759 break;
7760 case 30:
7761 pipeconf |= PIPECONF_10BPC;
7762 break;
7763 default:
7764 /* Case prevented by intel_choose_pipe_bpp_dither. */
7765 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007766 }
7767 }
7768
7769 if (HAS_PIPE_CXSR(dev)) {
7770 if (intel_crtc->lowfreq_avail) {
7771 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7772 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7773 } else {
7774 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007775 }
7776 }
7777
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007778 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007779 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007780 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007781 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7782 else
7783 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7784 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007785 pipeconf |= PIPECONF_PROGRESSIVE;
7786
Wayne Boyer666a4532015-12-09 12:29:35 -08007787 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7788 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007789 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007790
Daniel Vetter84b046f2013-02-19 18:48:54 +01007791 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7792 POSTING_READ(PIPECONF(intel_crtc->pipe));
7793}
7794
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007795static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7796 struct intel_crtc_state *crtc_state)
7797{
7798 struct drm_device *dev = crtc->base.dev;
7799 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007800 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007801 int refclk = 48000;
7802
7803 memset(&crtc_state->dpll_hw_state, 0,
7804 sizeof(crtc_state->dpll_hw_state));
7805
7806 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7807 if (intel_panel_use_ssc(dev_priv)) {
7808 refclk = dev_priv->vbt.lvds_ssc_freq;
7809 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7810 }
7811
7812 limit = &intel_limits_i8xx_lvds;
7813 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7814 limit = &intel_limits_i8xx_dvo;
7815 } else {
7816 limit = &intel_limits_i8xx_dac;
7817 }
7818
7819 if (!crtc_state->clock_set &&
7820 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7821 refclk, NULL, &crtc_state->dpll)) {
7822 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7823 return -EINVAL;
7824 }
7825
7826 i8xx_compute_dpll(crtc, crtc_state, NULL);
7827
7828 return 0;
7829}
7830
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007831static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7832 struct intel_crtc_state *crtc_state)
7833{
7834 struct drm_device *dev = crtc->base.dev;
7835 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007836 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007837 int refclk = 96000;
7838
7839 memset(&crtc_state->dpll_hw_state, 0,
7840 sizeof(crtc_state->dpll_hw_state));
7841
7842 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7843 if (intel_panel_use_ssc(dev_priv)) {
7844 refclk = dev_priv->vbt.lvds_ssc_freq;
7845 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7846 }
7847
7848 if (intel_is_dual_link_lvds(dev))
7849 limit = &intel_limits_g4x_dual_channel_lvds;
7850 else
7851 limit = &intel_limits_g4x_single_channel_lvds;
7852 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7853 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7854 limit = &intel_limits_g4x_hdmi;
7855 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7856 limit = &intel_limits_g4x_sdvo;
7857 } else {
7858 /* The option is for other outputs */
7859 limit = &intel_limits_i9xx_sdvo;
7860 }
7861
7862 if (!crtc_state->clock_set &&
7863 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7864 refclk, NULL, &crtc_state->dpll)) {
7865 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7866 return -EINVAL;
7867 }
7868
7869 i9xx_compute_dpll(crtc, crtc_state, NULL);
7870
7871 return 0;
7872}
7873
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007874static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7875 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007876{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007877 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007878 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007879 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007880 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007881
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007882 memset(&crtc_state->dpll_hw_state, 0,
7883 sizeof(crtc_state->dpll_hw_state));
7884
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007885 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7886 if (intel_panel_use_ssc(dev_priv)) {
7887 refclk = dev_priv->vbt.lvds_ssc_freq;
7888 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7889 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007890
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007891 limit = &intel_limits_pineview_lvds;
7892 } else {
7893 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007894 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007895
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007896 if (!crtc_state->clock_set &&
7897 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7898 refclk, NULL, &crtc_state->dpll)) {
7899 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7900 return -EINVAL;
7901 }
7902
7903 i9xx_compute_dpll(crtc, crtc_state, NULL);
7904
7905 return 0;
7906}
7907
7908static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7909 struct intel_crtc_state *crtc_state)
7910{
7911 struct drm_device *dev = crtc->base.dev;
7912 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007913 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007914 int refclk = 96000;
7915
7916 memset(&crtc_state->dpll_hw_state, 0,
7917 sizeof(crtc_state->dpll_hw_state));
7918
7919 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7920 if (intel_panel_use_ssc(dev_priv)) {
7921 refclk = dev_priv->vbt.lvds_ssc_freq;
7922 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007923 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007924
7925 limit = &intel_limits_i9xx_lvds;
7926 } else {
7927 limit = &intel_limits_i9xx_sdvo;
7928 }
7929
7930 if (!crtc_state->clock_set &&
7931 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7932 refclk, NULL, &crtc_state->dpll)) {
7933 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7934 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007935 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007936
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007937 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007938
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007939 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007940}
7941
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007942static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7943 struct intel_crtc_state *crtc_state)
7944{
7945 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007946 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007947
7948 memset(&crtc_state->dpll_hw_state, 0,
7949 sizeof(crtc_state->dpll_hw_state));
7950
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007951 if (!crtc_state->clock_set &&
7952 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7953 refclk, NULL, &crtc_state->dpll)) {
7954 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7955 return -EINVAL;
7956 }
7957
7958 chv_compute_dpll(crtc, crtc_state);
7959
7960 return 0;
7961}
7962
7963static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7964 struct intel_crtc_state *crtc_state)
7965{
7966 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007967 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007968
7969 memset(&crtc_state->dpll_hw_state, 0,
7970 sizeof(crtc_state->dpll_hw_state));
7971
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007972 if (!crtc_state->clock_set &&
7973 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7974 refclk, NULL, &crtc_state->dpll)) {
7975 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7976 return -EINVAL;
7977 }
7978
7979 vlv_compute_dpll(crtc, crtc_state);
7980
7981 return 0;
7982}
7983
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007984static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007985 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007986{
7987 struct drm_device *dev = crtc->base.dev;
7988 struct drm_i915_private *dev_priv = dev->dev_private;
7989 uint32_t tmp;
7990
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007991 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7992 return;
7993
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007994 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007995 if (!(tmp & PFIT_ENABLE))
7996 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007997
Daniel Vetter06922822013-07-11 13:35:40 +02007998 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007999 if (INTEL_INFO(dev)->gen < 4) {
8000 if (crtc->pipe != PIPE_B)
8001 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008002 } else {
8003 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8004 return;
8005 }
8006
Daniel Vetter06922822013-07-11 13:35:40 +02008007 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008008 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008009}
8010
Jesse Barnesacbec812013-09-20 11:29:32 -07008011static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008012 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008013{
8014 struct drm_device *dev = crtc->base.dev;
8015 struct drm_i915_private *dev_priv = dev->dev_private;
8016 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008017 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008018 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008019 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008020
Ville Syrjäläb5219732016-03-15 16:40:01 +02008021 /* In case of DSI, DPLL will not be used */
8022 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308023 return;
8024
Ville Syrjäläa5805162015-05-26 20:42:30 +03008025 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008026 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008027 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008028
8029 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8030 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8031 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8032 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8033 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8034
Imre Deakdccbea32015-06-22 23:35:51 +03008035 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008036}
8037
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008038static void
8039i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8040 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008041{
8042 struct drm_device *dev = crtc->base.dev;
8043 struct drm_i915_private *dev_priv = dev->dev_private;
8044 u32 val, base, offset;
8045 int pipe = crtc->pipe, plane = crtc->plane;
8046 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008047 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008048 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008049 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050
Damien Lespiau42a7b082015-02-05 19:35:13 +00008051 val = I915_READ(DSPCNTR(plane));
8052 if (!(val & DISPLAY_PLANE_ENABLE))
8053 return;
8054
Damien Lespiaud9806c92015-01-21 14:07:19 +00008055 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008056 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008057 DRM_DEBUG_KMS("failed to alloc fb\n");
8058 return;
8059 }
8060
Damien Lespiau1b842c82015-01-21 13:50:54 +00008061 fb = &intel_fb->base;
8062
Daniel Vetter18c52472015-02-10 17:16:09 +00008063 if (INTEL_INFO(dev)->gen >= 4) {
8064 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008065 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008066 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8067 }
8068 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008069
8070 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008071 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008072 fb->pixel_format = fourcc;
8073 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008074
8075 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008076 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008077 offset = I915_READ(DSPTILEOFF(plane));
8078 else
8079 offset = I915_READ(DSPLINOFF(plane));
8080 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8081 } else {
8082 base = I915_READ(DSPADDR(plane));
8083 }
8084 plane_config->base = base;
8085
8086 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008087 fb->width = ((val >> 16) & 0xfff) + 1;
8088 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008089
8090 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008091 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008092
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008093 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008094 fb->pixel_format,
8095 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008096
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008097 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008098
Damien Lespiau2844a922015-01-20 12:51:48 +00008099 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8100 pipe_name(pipe), plane, fb->width, fb->height,
8101 fb->bits_per_pixel, base, fb->pitches[0],
8102 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008103
Damien Lespiau2d140302015-02-05 17:22:18 +00008104 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008105}
8106
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008107static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008108 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008109{
8110 struct drm_device *dev = crtc->base.dev;
8111 struct drm_i915_private *dev_priv = dev->dev_private;
8112 int pipe = pipe_config->cpu_transcoder;
8113 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008114 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008115 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008116 int refclk = 100000;
8117
Ville Syrjäläb5219732016-03-15 16:40:01 +02008118 /* In case of DSI, DPLL will not be used */
8119 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8120 return;
8121
Ville Syrjäläa5805162015-05-26 20:42:30 +03008122 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008123 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8124 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8125 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8126 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008127 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008128 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008129
8130 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008131 clock.m2 = (pll_dw0 & 0xff) << 22;
8132 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8133 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008134 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8135 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8136 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8137
Imre Deakdccbea32015-06-22 23:35:51 +03008138 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008139}
8140
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008141static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008142 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008143{
8144 struct drm_device *dev = crtc->base.dev;
8145 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008146 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008147 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008148 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008149
Imre Deak17290502016-02-12 18:55:11 +02008150 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8151 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008152 return false;
8153
Daniel Vettere143a212013-07-04 12:01:15 +02008154 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008155 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008156
Imre Deak17290502016-02-12 18:55:11 +02008157 ret = false;
8158
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008159 tmp = I915_READ(PIPECONF(crtc->pipe));
8160 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008161 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008162
Wayne Boyer666a4532015-12-09 12:29:35 -08008163 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008164 switch (tmp & PIPECONF_BPC_MASK) {
8165 case PIPECONF_6BPC:
8166 pipe_config->pipe_bpp = 18;
8167 break;
8168 case PIPECONF_8BPC:
8169 pipe_config->pipe_bpp = 24;
8170 break;
8171 case PIPECONF_10BPC:
8172 pipe_config->pipe_bpp = 30;
8173 break;
8174 default:
8175 break;
8176 }
8177 }
8178
Wayne Boyer666a4532015-12-09 12:29:35 -08008179 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8180 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008181 pipe_config->limited_color_range = true;
8182
Ville Syrjälä282740f2013-09-04 18:30:03 +03008183 if (INTEL_INFO(dev)->gen < 4)
8184 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8185
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008186 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008187 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008188
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008189 i9xx_get_pfit_config(crtc, pipe_config);
8190
Daniel Vetter6c49f242013-06-06 12:45:25 +02008191 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008192 /* No way to read it out on pipes B and C */
8193 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8194 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8195 else
8196 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008197 pipe_config->pixel_multiplier =
8198 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8199 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008200 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008201 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8202 tmp = I915_READ(DPLL(crtc->pipe));
8203 pipe_config->pixel_multiplier =
8204 ((tmp & SDVO_MULTIPLIER_MASK)
8205 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8206 } else {
8207 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8208 * port and will be fixed up in the encoder->get_config
8209 * function. */
8210 pipe_config->pixel_multiplier = 1;
8211 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008212 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008213 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008214 /*
8215 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8216 * on 830. Filter it out here so that we don't
8217 * report errors due to that.
8218 */
8219 if (IS_I830(dev))
8220 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8221
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008222 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8223 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008224 } else {
8225 /* Mask out read-only status bits. */
8226 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8227 DPLL_PORTC_READY_MASK |
8228 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008229 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008230
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008231 if (IS_CHERRYVIEW(dev))
8232 chv_crtc_clock_get(crtc, pipe_config);
8233 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008234 vlv_crtc_clock_get(crtc, pipe_config);
8235 else
8236 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008237
Ville Syrjälä0f646142015-08-26 19:39:18 +03008238 /*
8239 * Normally the dotclock is filled in by the encoder .get_config()
8240 * but in case the pipe is enabled w/o any ports we need a sane
8241 * default.
8242 */
8243 pipe_config->base.adjusted_mode.crtc_clock =
8244 pipe_config->port_clock / pipe_config->pixel_multiplier;
8245
Imre Deak17290502016-02-12 18:55:11 +02008246 ret = true;
8247
8248out:
8249 intel_display_power_put(dev_priv, power_domain);
8250
8251 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008252}
8253
Paulo Zanonidde86e22012-12-01 12:04:25 -02008254static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008255{
8256 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008257 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008258 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008259 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008260 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008261 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008262 bool has_ck505 = false;
8263 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008264
8265 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008266 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008267 switch (encoder->type) {
8268 case INTEL_OUTPUT_LVDS:
8269 has_panel = true;
8270 has_lvds = true;
8271 break;
8272 case INTEL_OUTPUT_EDP:
8273 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008274 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008275 has_cpu_edp = true;
8276 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008277 default:
8278 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008279 }
8280 }
8281
Keith Packard99eb6a02011-09-26 14:29:12 -07008282 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008283 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008284 can_ssc = has_ck505;
8285 } else {
8286 has_ck505 = false;
8287 can_ssc = true;
8288 }
8289
Imre Deak2de69052013-05-08 13:14:04 +03008290 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8291 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008292
8293 /* Ironlake: try to setup display ref clock before DPLL
8294 * enabling. This is only under driver's control after
8295 * PCH B stepping, previous chipset stepping should be
8296 * ignoring this setting.
8297 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008298 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008299
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008300 /* As we must carefully and slowly disable/enable each source in turn,
8301 * compute the final state we want first and check if we need to
8302 * make any changes at all.
8303 */
8304 final = val;
8305 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008306 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008307 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008308 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008309 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8310
8311 final &= ~DREF_SSC_SOURCE_MASK;
8312 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8313 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008314
Keith Packard199e5d72011-09-22 12:01:57 -07008315 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008316 final |= DREF_SSC_SOURCE_ENABLE;
8317
8318 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8319 final |= DREF_SSC1_ENABLE;
8320
8321 if (has_cpu_edp) {
8322 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8323 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8324 else
8325 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8326 } else
8327 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8328 } else {
8329 final |= DREF_SSC_SOURCE_DISABLE;
8330 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8331 }
8332
8333 if (final == val)
8334 return;
8335
8336 /* Always enable nonspread source */
8337 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8338
8339 if (has_ck505)
8340 val |= DREF_NONSPREAD_CK505_ENABLE;
8341 else
8342 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8343
8344 if (has_panel) {
8345 val &= ~DREF_SSC_SOURCE_MASK;
8346 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008347
Keith Packard199e5d72011-09-22 12:01:57 -07008348 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008349 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008350 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008351 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008352 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008353 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008354
8355 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008356 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008357 POSTING_READ(PCH_DREF_CONTROL);
8358 udelay(200);
8359
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008360 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008361
8362 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008363 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008364 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008365 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008366 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008367 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008368 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008369 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008370 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008371
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008372 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008373 POSTING_READ(PCH_DREF_CONTROL);
8374 udelay(200);
8375 } else {
8376 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8377
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008378 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008379
8380 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008381 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008382
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008383 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008384 POSTING_READ(PCH_DREF_CONTROL);
8385 udelay(200);
8386
8387 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008388 val &= ~DREF_SSC_SOURCE_MASK;
8389 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008390
8391 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008392 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008393
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008394 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008395 POSTING_READ(PCH_DREF_CONTROL);
8396 udelay(200);
8397 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008398
8399 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008400}
8401
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008402static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008403{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008404 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008405
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008406 tmp = I915_READ(SOUTH_CHICKEN2);
8407 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8408 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008409
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008410 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8411 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8412 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008413
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008414 tmp = I915_READ(SOUTH_CHICKEN2);
8415 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8416 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008417
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008418 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8419 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8420 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008421}
8422
8423/* WaMPhyProgramming:hsw */
8424static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8425{
8426 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008427
8428 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8429 tmp &= ~(0xFF << 24);
8430 tmp |= (0x12 << 24);
8431 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8432
Paulo Zanonidde86e22012-12-01 12:04:25 -02008433 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8434 tmp |= (1 << 11);
8435 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8436
8437 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8438 tmp |= (1 << 11);
8439 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8440
Paulo Zanonidde86e22012-12-01 12:04:25 -02008441 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8442 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8443 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8444
8445 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8446 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8447 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8448
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008449 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8450 tmp &= ~(7 << 13);
8451 tmp |= (5 << 13);
8452 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008453
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008454 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8455 tmp &= ~(7 << 13);
8456 tmp |= (5 << 13);
8457 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008458
8459 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8460 tmp &= ~0xFF;
8461 tmp |= 0x1C;
8462 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8463
8464 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8465 tmp &= ~0xFF;
8466 tmp |= 0x1C;
8467 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8468
8469 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8470 tmp &= ~(0xFF << 16);
8471 tmp |= (0x1C << 16);
8472 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8473
8474 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8475 tmp &= ~(0xFF << 16);
8476 tmp |= (0x1C << 16);
8477 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8478
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008479 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8480 tmp |= (1 << 27);
8481 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008482
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008483 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8484 tmp |= (1 << 27);
8485 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008486
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008487 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8488 tmp &= ~(0xF << 28);
8489 tmp |= (4 << 28);
8490 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008491
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008492 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8493 tmp &= ~(0xF << 28);
8494 tmp |= (4 << 28);
8495 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008496}
8497
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008498/* Implements 3 different sequences from BSpec chapter "Display iCLK
8499 * Programming" based on the parameters passed:
8500 * - Sequence to enable CLKOUT_DP
8501 * - Sequence to enable CLKOUT_DP without spread
8502 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8503 */
8504static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8505 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008506{
8507 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008508 uint32_t reg, tmp;
8509
8510 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8511 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008512 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008513 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008514
Ville Syrjäläa5805162015-05-26 20:42:30 +03008515 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008516
8517 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8518 tmp &= ~SBI_SSCCTL_DISABLE;
8519 tmp |= SBI_SSCCTL_PATHALT;
8520 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8521
8522 udelay(24);
8523
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008524 if (with_spread) {
8525 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8526 tmp &= ~SBI_SSCCTL_PATHALT;
8527 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008528
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008529 if (with_fdi) {
8530 lpt_reset_fdi_mphy(dev_priv);
8531 lpt_program_fdi_mphy(dev_priv);
8532 }
8533 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008534
Ville Syrjäläc2699522015-08-27 23:55:59 +03008535 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008536 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8537 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8538 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008539
Ville Syrjäläa5805162015-05-26 20:42:30 +03008540 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008541}
8542
Paulo Zanoni47701c32013-07-23 11:19:25 -03008543/* Sequence to disable CLKOUT_DP */
8544static void lpt_disable_clkout_dp(struct drm_device *dev)
8545{
8546 struct drm_i915_private *dev_priv = dev->dev_private;
8547 uint32_t reg, tmp;
8548
Ville Syrjäläa5805162015-05-26 20:42:30 +03008549 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008550
Ville Syrjäläc2699522015-08-27 23:55:59 +03008551 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008552 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8553 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8554 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8555
8556 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8557 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8558 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8559 tmp |= SBI_SSCCTL_PATHALT;
8560 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8561 udelay(32);
8562 }
8563 tmp |= SBI_SSCCTL_DISABLE;
8564 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8565 }
8566
Ville Syrjäläa5805162015-05-26 20:42:30 +03008567 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008568}
8569
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008570#define BEND_IDX(steps) ((50 + (steps)) / 5)
8571
8572static const uint16_t sscdivintphase[] = {
8573 [BEND_IDX( 50)] = 0x3B23,
8574 [BEND_IDX( 45)] = 0x3B23,
8575 [BEND_IDX( 40)] = 0x3C23,
8576 [BEND_IDX( 35)] = 0x3C23,
8577 [BEND_IDX( 30)] = 0x3D23,
8578 [BEND_IDX( 25)] = 0x3D23,
8579 [BEND_IDX( 20)] = 0x3E23,
8580 [BEND_IDX( 15)] = 0x3E23,
8581 [BEND_IDX( 10)] = 0x3F23,
8582 [BEND_IDX( 5)] = 0x3F23,
8583 [BEND_IDX( 0)] = 0x0025,
8584 [BEND_IDX( -5)] = 0x0025,
8585 [BEND_IDX(-10)] = 0x0125,
8586 [BEND_IDX(-15)] = 0x0125,
8587 [BEND_IDX(-20)] = 0x0225,
8588 [BEND_IDX(-25)] = 0x0225,
8589 [BEND_IDX(-30)] = 0x0325,
8590 [BEND_IDX(-35)] = 0x0325,
8591 [BEND_IDX(-40)] = 0x0425,
8592 [BEND_IDX(-45)] = 0x0425,
8593 [BEND_IDX(-50)] = 0x0525,
8594};
8595
8596/*
8597 * Bend CLKOUT_DP
8598 * steps -50 to 50 inclusive, in steps of 5
8599 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8600 * change in clock period = -(steps / 10) * 5.787 ps
8601 */
8602static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8603{
8604 uint32_t tmp;
8605 int idx = BEND_IDX(steps);
8606
8607 if (WARN_ON(steps % 5 != 0))
8608 return;
8609
8610 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8611 return;
8612
8613 mutex_lock(&dev_priv->sb_lock);
8614
8615 if (steps % 10 != 0)
8616 tmp = 0xAAAAAAAB;
8617 else
8618 tmp = 0x00000000;
8619 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8620
8621 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8622 tmp &= 0xffff0000;
8623 tmp |= sscdivintphase[idx];
8624 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8625
8626 mutex_unlock(&dev_priv->sb_lock);
8627}
8628
8629#undef BEND_IDX
8630
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008631static void lpt_init_pch_refclk(struct drm_device *dev)
8632{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008633 struct intel_encoder *encoder;
8634 bool has_vga = false;
8635
Damien Lespiaub2784e12014-08-05 11:29:37 +01008636 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008637 switch (encoder->type) {
8638 case INTEL_OUTPUT_ANALOG:
8639 has_vga = true;
8640 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008641 default:
8642 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008643 }
8644 }
8645
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008646 if (has_vga) {
8647 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008648 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008649 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008650 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008651 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008652}
8653
Paulo Zanonidde86e22012-12-01 12:04:25 -02008654/*
8655 * Initialize reference clocks when the driver loads
8656 */
8657void intel_init_pch_refclk(struct drm_device *dev)
8658{
8659 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8660 ironlake_init_pch_refclk(dev);
8661 else if (HAS_PCH_LPT(dev))
8662 lpt_init_pch_refclk(dev);
8663}
8664
Daniel Vetter6ff93602013-04-19 11:24:36 +02008665static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008666{
8667 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8668 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8669 int pipe = intel_crtc->pipe;
8670 uint32_t val;
8671
Daniel Vetter78114072013-06-13 00:54:57 +02008672 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008673
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008674 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008675 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008676 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008677 break;
8678 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008679 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008680 break;
8681 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008682 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008683 break;
8684 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008685 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008686 break;
8687 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008688 /* Case prevented by intel_choose_pipe_bpp_dither. */
8689 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008690 }
8691
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008692 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008693 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8694
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008695 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008696 val |= PIPECONF_INTERLACED_ILK;
8697 else
8698 val |= PIPECONF_PROGRESSIVE;
8699
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008700 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008701 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008702
Paulo Zanonic8203562012-09-12 10:06:29 -03008703 I915_WRITE(PIPECONF(pipe), val);
8704 POSTING_READ(PIPECONF(pipe));
8705}
8706
Daniel Vetter6ff93602013-04-19 11:24:36 +02008707static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008708{
Jani Nikula391bf042016-03-18 17:05:40 +02008709 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008711 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008712 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008713
Jani Nikula391bf042016-03-18 17:05:40 +02008714 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008715 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8716
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008717 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008718 val |= PIPECONF_INTERLACED_ILK;
8719 else
8720 val |= PIPECONF_PROGRESSIVE;
8721
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008722 I915_WRITE(PIPECONF(cpu_transcoder), val);
8723 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008724}
8725
Jani Nikula391bf042016-03-18 17:05:40 +02008726static void haswell_set_pipemisc(struct drm_crtc *crtc)
8727{
8728 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8730
8731 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8732 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008733
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008734 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008735 case 18:
8736 val |= PIPEMISC_DITHER_6_BPC;
8737 break;
8738 case 24:
8739 val |= PIPEMISC_DITHER_8_BPC;
8740 break;
8741 case 30:
8742 val |= PIPEMISC_DITHER_10_BPC;
8743 break;
8744 case 36:
8745 val |= PIPEMISC_DITHER_12_BPC;
8746 break;
8747 default:
8748 /* Case prevented by pipe_config_set_bpp. */
8749 BUG();
8750 }
8751
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008752 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008753 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8754
Jani Nikula391bf042016-03-18 17:05:40 +02008755 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008756 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008757}
8758
Paulo Zanonid4b19312012-11-29 11:29:32 -02008759int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8760{
8761 /*
8762 * Account for spread spectrum to avoid
8763 * oversubscribing the link. Max center spread
8764 * is 2.5%; use 5% for safety's sake.
8765 */
8766 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008767 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008768}
8769
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008770static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008771{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008772 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008773}
8774
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008775static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8776 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008777 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008778{
8779 struct drm_crtc *crtc = &intel_crtc->base;
8780 struct drm_device *dev = crtc->dev;
8781 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008782 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008783 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008784 struct drm_connector_state *connector_state;
8785 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008786 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008787 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008788 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008789
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008790 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008791 if (connector_state->crtc != crtc_state->base.crtc)
8792 continue;
8793
8794 encoder = to_intel_encoder(connector_state->best_encoder);
8795
8796 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008797 case INTEL_OUTPUT_LVDS:
8798 is_lvds = true;
8799 break;
8800 case INTEL_OUTPUT_SDVO:
8801 case INTEL_OUTPUT_HDMI:
8802 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008803 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008804 default:
8805 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008806 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008807 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008808
Chris Wilsonc1858122010-12-03 21:35:48 +00008809 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008810 factor = 21;
8811 if (is_lvds) {
8812 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008813 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008814 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008815 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008816 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008817 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008818
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008819 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008820
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008821 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8822 fp |= FP_CB_TUNE;
8823
8824 if (reduced_clock) {
8825 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8826
8827 if (reduced_clock->m < factor * reduced_clock->n)
8828 fp2 |= FP_CB_TUNE;
8829 } else {
8830 fp2 = fp;
8831 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008832
Chris Wilson5eddb702010-09-11 13:48:45 +01008833 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008834
Eric Anholta07d6782011-03-30 13:01:08 -07008835 if (is_lvds)
8836 dpll |= DPLLB_MODE_LVDS;
8837 else
8838 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008839
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008840 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008841 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008842
8843 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008844 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008845 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008846 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008847
Eric Anholta07d6782011-03-30 13:01:08 -07008848 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008849 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008850 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008851 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008852
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008853 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008854 case 5:
8855 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8856 break;
8857 case 7:
8858 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8859 break;
8860 case 10:
8861 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8862 break;
8863 case 14:
8864 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8865 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008866 }
8867
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008868 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008869 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008870 else
8871 dpll |= PLL_REF_INPUT_DREFCLK;
8872
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008873 dpll |= DPLL_VCO_ENABLE;
8874
8875 crtc_state->dpll_hw_state.dpll = dpll;
8876 crtc_state->dpll_hw_state.fp0 = fp;
8877 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008878}
8879
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008880static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8881 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008882{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008883 struct drm_device *dev = crtc->base.dev;
8884 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008885 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008886 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008887 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008888 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008889 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008890
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008891 memset(&crtc_state->dpll_hw_state, 0,
8892 sizeof(crtc_state->dpll_hw_state));
8893
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008894 crtc->lowfreq_avail = false;
8895
8896 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8897 if (!crtc_state->has_pch_encoder)
8898 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008899
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008900 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8901 if (intel_panel_use_ssc(dev_priv)) {
8902 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8903 dev_priv->vbt.lvds_ssc_freq);
8904 refclk = dev_priv->vbt.lvds_ssc_freq;
8905 }
8906
8907 if (intel_is_dual_link_lvds(dev)) {
8908 if (refclk == 100000)
8909 limit = &intel_limits_ironlake_dual_lvds_100m;
8910 else
8911 limit = &intel_limits_ironlake_dual_lvds;
8912 } else {
8913 if (refclk == 100000)
8914 limit = &intel_limits_ironlake_single_lvds_100m;
8915 else
8916 limit = &intel_limits_ironlake_single_lvds;
8917 }
8918 } else {
8919 limit = &intel_limits_ironlake_dac;
8920 }
8921
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008922 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008923 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8924 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008925 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8926 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008927 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008928
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008929 ironlake_compute_dpll(crtc, crtc_state,
8930 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008931
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008932 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8933 if (pll == NULL) {
8934 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8935 pipe_name(crtc->pipe));
8936 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008937 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008938
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008939 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8940 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008941 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008942
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008943 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008944}
8945
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008946static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8947 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008948{
8949 struct drm_device *dev = crtc->base.dev;
8950 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008951 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008952
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008953 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8954 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8955 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8956 & ~TU_SIZE_MASK;
8957 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8958 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8959 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8960}
8961
8962static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8963 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008964 struct intel_link_m_n *m_n,
8965 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008966{
8967 struct drm_device *dev = crtc->base.dev;
8968 struct drm_i915_private *dev_priv = dev->dev_private;
8969 enum pipe pipe = crtc->pipe;
8970
8971 if (INTEL_INFO(dev)->gen >= 5) {
8972 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8973 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8974 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8975 & ~TU_SIZE_MASK;
8976 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8977 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8978 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008979 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8980 * gen < 8) and if DRRS is supported (to make sure the
8981 * registers are not unnecessarily read).
8982 */
8983 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008984 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008985 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8986 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8987 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8988 & ~TU_SIZE_MASK;
8989 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8990 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8991 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8992 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008993 } else {
8994 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8995 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8996 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8997 & ~TU_SIZE_MASK;
8998 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8999 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9000 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9001 }
9002}
9003
9004void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009005 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009006{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009007 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009008 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9009 else
9010 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009011 &pipe_config->dp_m_n,
9012 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009013}
9014
Daniel Vetter72419202013-04-04 13:28:53 +02009015static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009016 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009017{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009018 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009019 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009020}
9021
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009022static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009023 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009024{
9025 struct drm_device *dev = crtc->base.dev;
9026 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009027 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9028 uint32_t ps_ctrl = 0;
9029 int id = -1;
9030 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009031
Chandra Kondurua1b22782015-04-07 15:28:45 -07009032 /* find scaler attached to this pipe */
9033 for (i = 0; i < crtc->num_scalers; i++) {
9034 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9035 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9036 id = i;
9037 pipe_config->pch_pfit.enabled = true;
9038 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9039 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9040 break;
9041 }
9042 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009043
Chandra Kondurua1b22782015-04-07 15:28:45 -07009044 scaler_state->scaler_id = id;
9045 if (id >= 0) {
9046 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9047 } else {
9048 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009049 }
9050}
9051
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009052static void
9053skylake_get_initial_plane_config(struct intel_crtc *crtc,
9054 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009055{
9056 struct drm_device *dev = crtc->base.dev;
9057 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009058 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009059 int pipe = crtc->pipe;
9060 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009061 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009062 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009063 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009064
Damien Lespiaud9806c92015-01-21 14:07:19 +00009065 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009066 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009067 DRM_DEBUG_KMS("failed to alloc fb\n");
9068 return;
9069 }
9070
Damien Lespiau1b842c82015-01-21 13:50:54 +00009071 fb = &intel_fb->base;
9072
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009073 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009074 if (!(val & PLANE_CTL_ENABLE))
9075 goto error;
9076
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009077 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9078 fourcc = skl_format_to_fourcc(pixel_format,
9079 val & PLANE_CTL_ORDER_RGBX,
9080 val & PLANE_CTL_ALPHA_MASK);
9081 fb->pixel_format = fourcc;
9082 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9083
Damien Lespiau40f46282015-02-27 11:15:21 +00009084 tiling = val & PLANE_CTL_TILED_MASK;
9085 switch (tiling) {
9086 case PLANE_CTL_TILED_LINEAR:
9087 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9088 break;
9089 case PLANE_CTL_TILED_X:
9090 plane_config->tiling = I915_TILING_X;
9091 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9092 break;
9093 case PLANE_CTL_TILED_Y:
9094 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9095 break;
9096 case PLANE_CTL_TILED_YF:
9097 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9098 break;
9099 default:
9100 MISSING_CASE(tiling);
9101 goto error;
9102 }
9103
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009104 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9105 plane_config->base = base;
9106
9107 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9108
9109 val = I915_READ(PLANE_SIZE(pipe, 0));
9110 fb->height = ((val >> 16) & 0xfff) + 1;
9111 fb->width = ((val >> 0) & 0x1fff) + 1;
9112
9113 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009114 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009115 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009116 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9117
9118 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009119 fb->pixel_format,
9120 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009121
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009122 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009123
9124 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9125 pipe_name(pipe), fb->width, fb->height,
9126 fb->bits_per_pixel, base, fb->pitches[0],
9127 plane_config->size);
9128
Damien Lespiau2d140302015-02-05 17:22:18 +00009129 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009130 return;
9131
9132error:
9133 kfree(fb);
9134}
9135
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009136static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009137 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009138{
9139 struct drm_device *dev = crtc->base.dev;
9140 struct drm_i915_private *dev_priv = dev->dev_private;
9141 uint32_t tmp;
9142
9143 tmp = I915_READ(PF_CTL(crtc->pipe));
9144
9145 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009146 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009147 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9148 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009149
9150 /* We currently do not free assignements of panel fitters on
9151 * ivb/hsw (since we don't use the higher upscaling modes which
9152 * differentiates them) so just WARN about this case for now. */
9153 if (IS_GEN7(dev)) {
9154 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9155 PF_PIPE_SEL_IVB(crtc->pipe));
9156 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009157 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009158}
9159
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009160static void
9161ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9162 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009163{
9164 struct drm_device *dev = crtc->base.dev;
9165 struct drm_i915_private *dev_priv = dev->dev_private;
9166 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009167 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009168 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009169 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009170 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009171 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009172
Damien Lespiau42a7b082015-02-05 19:35:13 +00009173 val = I915_READ(DSPCNTR(pipe));
9174 if (!(val & DISPLAY_PLANE_ENABLE))
9175 return;
9176
Damien Lespiaud9806c92015-01-21 14:07:19 +00009177 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009178 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009179 DRM_DEBUG_KMS("failed to alloc fb\n");
9180 return;
9181 }
9182
Damien Lespiau1b842c82015-01-21 13:50:54 +00009183 fb = &intel_fb->base;
9184
Daniel Vetter18c52472015-02-10 17:16:09 +00009185 if (INTEL_INFO(dev)->gen >= 4) {
9186 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009187 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009188 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9189 }
9190 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191
9192 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009193 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009194 fb->pixel_format = fourcc;
9195 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009196
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009197 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009198 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009199 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009201 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009202 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009203 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009204 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009205 }
9206 plane_config->base = base;
9207
9208 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009209 fb->width = ((val >> 16) & 0xfff) + 1;
9210 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009211
9212 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009213 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009214
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009215 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009216 fb->pixel_format,
9217 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009218
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009219 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009220
Damien Lespiau2844a922015-01-20 12:51:48 +00009221 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9222 pipe_name(pipe), fb->width, fb->height,
9223 fb->bits_per_pixel, base, fb->pitches[0],
9224 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009225
Damien Lespiau2d140302015-02-05 17:22:18 +00009226 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009227}
9228
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009229static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009230 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009231{
9232 struct drm_device *dev = crtc->base.dev;
9233 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009234 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009235 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009236 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009237
Imre Deak17290502016-02-12 18:55:11 +02009238 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9239 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009240 return false;
9241
Daniel Vettere143a212013-07-04 12:01:15 +02009242 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009243 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009244
Imre Deak17290502016-02-12 18:55:11 +02009245 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009246 tmp = I915_READ(PIPECONF(crtc->pipe));
9247 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009248 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009249
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009250 switch (tmp & PIPECONF_BPC_MASK) {
9251 case PIPECONF_6BPC:
9252 pipe_config->pipe_bpp = 18;
9253 break;
9254 case PIPECONF_8BPC:
9255 pipe_config->pipe_bpp = 24;
9256 break;
9257 case PIPECONF_10BPC:
9258 pipe_config->pipe_bpp = 30;
9259 break;
9260 case PIPECONF_12BPC:
9261 pipe_config->pipe_bpp = 36;
9262 break;
9263 default:
9264 break;
9265 }
9266
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009267 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9268 pipe_config->limited_color_range = true;
9269
Daniel Vetterab9412b2013-05-03 11:49:46 +02009270 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009271 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009272 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009273
Daniel Vetter88adfff2013-03-28 10:42:01 +01009274 pipe_config->has_pch_encoder = true;
9275
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009276 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9277 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9278 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009279
9280 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009281
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009282 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009283 /*
9284 * The pipe->pch transcoder and pch transcoder->pll
9285 * mapping is fixed.
9286 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009287 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009288 } else {
9289 tmp = I915_READ(PCH_DPLL_SEL);
9290 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009291 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009292 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009293 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009294 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009295
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009296 pipe_config->shared_dpll =
9297 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9298 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009299
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009300 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9301 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009302
9303 tmp = pipe_config->dpll_hw_state.dpll;
9304 pipe_config->pixel_multiplier =
9305 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9306 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009307
9308 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009309 } else {
9310 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009311 }
9312
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009313 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009314 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009315
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009316 ironlake_get_pfit_config(crtc, pipe_config);
9317
Imre Deak17290502016-02-12 18:55:11 +02009318 ret = true;
9319
9320out:
9321 intel_display_power_put(dev_priv, power_domain);
9322
9323 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009324}
9325
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009326static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9327{
9328 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009329 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009331 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009332 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009333 pipe_name(crtc->pipe));
9334
Rob Clarke2c719b2014-12-15 13:56:32 -05009335 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9336 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009337 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9338 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009339 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9340 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009341 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009342 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009343 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009344 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009345 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009346 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009347 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009348 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009349 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009350
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009351 /*
9352 * In theory we can still leave IRQs enabled, as long as only the HPD
9353 * interrupts remain enabled. We used to check for that, but since it's
9354 * gen-specific and since we only disable LCPLL after we fully disable
9355 * the interrupts, the check below should be enough.
9356 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009357 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009358}
9359
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009360static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9361{
9362 struct drm_device *dev = dev_priv->dev;
9363
9364 if (IS_HASWELL(dev))
9365 return I915_READ(D_COMP_HSW);
9366 else
9367 return I915_READ(D_COMP_BDW);
9368}
9369
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009370static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9371{
9372 struct drm_device *dev = dev_priv->dev;
9373
9374 if (IS_HASWELL(dev)) {
9375 mutex_lock(&dev_priv->rps.hw_lock);
9376 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9377 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009378 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009379 mutex_unlock(&dev_priv->rps.hw_lock);
9380 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009381 I915_WRITE(D_COMP_BDW, val);
9382 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009383 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009384}
9385
9386/*
9387 * This function implements pieces of two sequences from BSpec:
9388 * - Sequence for display software to disable LCPLL
9389 * - Sequence for display software to allow package C8+
9390 * The steps implemented here are just the steps that actually touch the LCPLL
9391 * register. Callers should take care of disabling all the display engine
9392 * functions, doing the mode unset, fixing interrupts, etc.
9393 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009394static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9395 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009396{
9397 uint32_t val;
9398
9399 assert_can_disable_lcpll(dev_priv);
9400
9401 val = I915_READ(LCPLL_CTL);
9402
9403 if (switch_to_fclk) {
9404 val |= LCPLL_CD_SOURCE_FCLK;
9405 I915_WRITE(LCPLL_CTL, val);
9406
9407 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9408 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9409 DRM_ERROR("Switching to FCLK failed\n");
9410
9411 val = I915_READ(LCPLL_CTL);
9412 }
9413
9414 val |= LCPLL_PLL_DISABLE;
9415 I915_WRITE(LCPLL_CTL, val);
9416 POSTING_READ(LCPLL_CTL);
9417
9418 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9419 DRM_ERROR("LCPLL still locked\n");
9420
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009421 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009422 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009423 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009424 ndelay(100);
9425
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009426 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9427 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009428 DRM_ERROR("D_COMP RCOMP still in progress\n");
9429
9430 if (allow_power_down) {
9431 val = I915_READ(LCPLL_CTL);
9432 val |= LCPLL_POWER_DOWN_ALLOW;
9433 I915_WRITE(LCPLL_CTL, val);
9434 POSTING_READ(LCPLL_CTL);
9435 }
9436}
9437
9438/*
9439 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9440 * source.
9441 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009442static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009443{
9444 uint32_t val;
9445
9446 val = I915_READ(LCPLL_CTL);
9447
9448 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9449 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9450 return;
9451
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009452 /*
9453 * Make sure we're not on PC8 state before disabling PC8, otherwise
9454 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009455 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009456 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009457
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009458 if (val & LCPLL_POWER_DOWN_ALLOW) {
9459 val &= ~LCPLL_POWER_DOWN_ALLOW;
9460 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009461 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009462 }
9463
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009464 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009465 val |= D_COMP_COMP_FORCE;
9466 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009467 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009468
9469 val = I915_READ(LCPLL_CTL);
9470 val &= ~LCPLL_PLL_DISABLE;
9471 I915_WRITE(LCPLL_CTL, val);
9472
9473 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9474 DRM_ERROR("LCPLL not locked yet\n");
9475
9476 if (val & LCPLL_CD_SOURCE_FCLK) {
9477 val = I915_READ(LCPLL_CTL);
9478 val &= ~LCPLL_CD_SOURCE_FCLK;
9479 I915_WRITE(LCPLL_CTL, val);
9480
9481 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9482 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9483 DRM_ERROR("Switching back to LCPLL failed\n");
9484 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009485
Mika Kuoppala59bad942015-01-16 11:34:40 +02009486 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009487 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009488}
9489
Paulo Zanoni765dab672014-03-07 20:08:18 -03009490/*
9491 * Package states C8 and deeper are really deep PC states that can only be
9492 * reached when all the devices on the system allow it, so even if the graphics
9493 * device allows PC8+, it doesn't mean the system will actually get to these
9494 * states. Our driver only allows PC8+ when going into runtime PM.
9495 *
9496 * The requirements for PC8+ are that all the outputs are disabled, the power
9497 * well is disabled and most interrupts are disabled, and these are also
9498 * requirements for runtime PM. When these conditions are met, we manually do
9499 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9500 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9501 * hang the machine.
9502 *
9503 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9504 * the state of some registers, so when we come back from PC8+ we need to
9505 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9506 * need to take care of the registers kept by RC6. Notice that this happens even
9507 * if we don't put the device in PCI D3 state (which is what currently happens
9508 * because of the runtime PM support).
9509 *
9510 * For more, read "Display Sequences for Package C8" on the hardware
9511 * documentation.
9512 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009513void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009514{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009515 struct drm_device *dev = dev_priv->dev;
9516 uint32_t val;
9517
Paulo Zanonic67a4702013-08-19 13:18:09 -03009518 DRM_DEBUG_KMS("Enabling package C8+\n");
9519
Ville Syrjäläc2699522015-08-27 23:55:59 +03009520 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009521 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9522 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9523 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9524 }
9525
9526 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009527 hsw_disable_lcpll(dev_priv, true, true);
9528}
9529
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009530void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009531{
9532 struct drm_device *dev = dev_priv->dev;
9533 uint32_t val;
9534
Paulo Zanonic67a4702013-08-19 13:18:09 -03009535 DRM_DEBUG_KMS("Disabling package C8+\n");
9536
9537 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009538 lpt_init_pch_refclk(dev);
9539
Ville Syrjäläc2699522015-08-27 23:55:59 +03009540 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009541 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9542 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9543 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9544 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009545}
9546
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009547static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309548{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009549 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009550 struct intel_atomic_state *old_intel_state =
9551 to_intel_atomic_state(old_state);
9552 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309553
Imre Deakc6c46962016-04-01 16:02:40 +03009554 broxton_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309555}
9556
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009557/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009558static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009559{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009560 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9561 struct drm_i915_private *dev_priv = state->dev->dev_private;
9562 struct drm_crtc *crtc;
9563 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009564 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009565 unsigned max_pixel_rate = 0, i;
9566 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009567
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009568 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9569 sizeof(intel_state->min_pixclk));
9570
9571 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009572 int pixel_rate;
9573
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009574 crtc_state = to_intel_crtc_state(cstate);
9575 if (!crtc_state->base.enable) {
9576 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009577 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009578 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009579
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009580 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009581
9582 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009583 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009584 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9585
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009586 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009587 }
9588
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009589 for_each_pipe(dev_priv, pipe)
9590 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9591
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009592 return max_pixel_rate;
9593}
9594
9595static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9596{
9597 struct drm_i915_private *dev_priv = dev->dev_private;
9598 uint32_t val, data;
9599 int ret;
9600
9601 if (WARN((I915_READ(LCPLL_CTL) &
9602 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9603 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9604 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9605 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9606 "trying to change cdclk frequency with cdclk not enabled\n"))
9607 return;
9608
9609 mutex_lock(&dev_priv->rps.hw_lock);
9610 ret = sandybridge_pcode_write(dev_priv,
9611 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9612 mutex_unlock(&dev_priv->rps.hw_lock);
9613 if (ret) {
9614 DRM_ERROR("failed to inform pcode about cdclk change\n");
9615 return;
9616 }
9617
9618 val = I915_READ(LCPLL_CTL);
9619 val |= LCPLL_CD_SOURCE_FCLK;
9620 I915_WRITE(LCPLL_CTL, val);
9621
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009622 if (wait_for_us(I915_READ(LCPLL_CTL) &
9623 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009624 DRM_ERROR("Switching to FCLK failed\n");
9625
9626 val = I915_READ(LCPLL_CTL);
9627 val &= ~LCPLL_CLK_FREQ_MASK;
9628
9629 switch (cdclk) {
9630 case 450000:
9631 val |= LCPLL_CLK_FREQ_450;
9632 data = 0;
9633 break;
9634 case 540000:
9635 val |= LCPLL_CLK_FREQ_54O_BDW;
9636 data = 1;
9637 break;
9638 case 337500:
9639 val |= LCPLL_CLK_FREQ_337_5_BDW;
9640 data = 2;
9641 break;
9642 case 675000:
9643 val |= LCPLL_CLK_FREQ_675_BDW;
9644 data = 3;
9645 break;
9646 default:
9647 WARN(1, "invalid cdclk frequency\n");
9648 return;
9649 }
9650
9651 I915_WRITE(LCPLL_CTL, val);
9652
9653 val = I915_READ(LCPLL_CTL);
9654 val &= ~LCPLL_CD_SOURCE_FCLK;
9655 I915_WRITE(LCPLL_CTL, val);
9656
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009657 if (wait_for_us((I915_READ(LCPLL_CTL) &
9658 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009659 DRM_ERROR("Switching back to LCPLL failed\n");
9660
9661 mutex_lock(&dev_priv->rps.hw_lock);
9662 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9663 mutex_unlock(&dev_priv->rps.hw_lock);
9664
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009665 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9666
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009667 intel_update_cdclk(dev);
9668
9669 WARN(cdclk != dev_priv->cdclk_freq,
9670 "cdclk requested %d kHz but got %d kHz\n",
9671 cdclk, dev_priv->cdclk_freq);
9672}
9673
Ville Syrjälä587c7912016-05-11 22:44:41 +03009674static int broadwell_calc_cdclk(int max_pixclk)
9675{
9676 if (max_pixclk > 540000)
9677 return 675000;
9678 else if (max_pixclk > 450000)
9679 return 540000;
9680 else if (max_pixclk > 337500)
9681 return 450000;
9682 else
9683 return 337500;
9684}
9685
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009686static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009687{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009688 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009689 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009690 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009691 int cdclk;
9692
9693 /*
9694 * FIXME should also account for plane ratio
9695 * once 64bpp pixel formats are supported.
9696 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009697 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009698
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009699 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009700 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9701 cdclk, dev_priv->max_cdclk_freq);
9702 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009703 }
9704
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009705 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9706 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009707 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009708
9709 return 0;
9710}
9711
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009712static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009713{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009714 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009715 struct intel_atomic_state *old_intel_state =
9716 to_intel_atomic_state(old_state);
9717 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009718
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009719 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009720}
9721
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009722static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9723 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009724{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009725 struct intel_encoder *intel_encoder =
9726 intel_ddi_get_crtc_new_encoder(crtc_state);
9727
9728 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9729 if (!intel_ddi_pll_select(crtc, crtc_state))
9730 return -EINVAL;
9731 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009732
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009733 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009734
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009735 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009736}
9737
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309738static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9739 enum port port,
9740 struct intel_crtc_state *pipe_config)
9741{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009742 enum intel_dpll_id id;
9743
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309744 switch (port) {
9745 case PORT_A:
9746 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009747 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309748 break;
9749 case PORT_B:
9750 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009751 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309752 break;
9753 case PORT_C:
9754 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009755 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309756 break;
9757 default:
9758 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009759 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309760 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009761
9762 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309763}
9764
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009765static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9766 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009767 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009768{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009769 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009770 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009771
9772 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9773 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9774
9775 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009776 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009777 id = DPLL_ID_SKL_DPLL0;
9778 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009779 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009780 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009781 break;
9782 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009783 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009784 break;
9785 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009786 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009787 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009788 default:
9789 MISSING_CASE(pipe_config->ddi_pll_sel);
9790 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009791 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009792
9793 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009794}
9795
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009796static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9797 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009798 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009799{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009800 enum intel_dpll_id id;
9801
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009802 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9803
9804 switch (pipe_config->ddi_pll_sel) {
9805 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009806 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009807 break;
9808 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009809 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009810 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009811 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009812 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009813 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009814 case PORT_CLK_SEL_LCPLL_810:
9815 id = DPLL_ID_LCPLL_810;
9816 break;
9817 case PORT_CLK_SEL_LCPLL_1350:
9818 id = DPLL_ID_LCPLL_1350;
9819 break;
9820 case PORT_CLK_SEL_LCPLL_2700:
9821 id = DPLL_ID_LCPLL_2700;
9822 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009823 default:
9824 MISSING_CASE(pipe_config->ddi_pll_sel);
9825 /* fall through */
9826 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009827 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009828 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009829
9830 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009831}
9832
Jani Nikulacf304292016-03-18 17:05:41 +02009833static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9834 struct intel_crtc_state *pipe_config,
9835 unsigned long *power_domain_mask)
9836{
9837 struct drm_device *dev = crtc->base.dev;
9838 struct drm_i915_private *dev_priv = dev->dev_private;
9839 enum intel_display_power_domain power_domain;
9840 u32 tmp;
9841
Imre Deakd9a7bc62016-05-12 16:18:50 +03009842 /*
9843 * The pipe->transcoder mapping is fixed with the exception of the eDP
9844 * transcoder handled below.
9845 */
Jani Nikulacf304292016-03-18 17:05:41 +02009846 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9847
9848 /*
9849 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9850 * consistency and less surprising code; it's in always on power).
9851 */
9852 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9853 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9854 enum pipe trans_edp_pipe;
9855 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9856 default:
9857 WARN(1, "unknown pipe linked to edp transcoder\n");
9858 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9859 case TRANS_DDI_EDP_INPUT_A_ON:
9860 trans_edp_pipe = PIPE_A;
9861 break;
9862 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9863 trans_edp_pipe = PIPE_B;
9864 break;
9865 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9866 trans_edp_pipe = PIPE_C;
9867 break;
9868 }
9869
9870 if (trans_edp_pipe == crtc->pipe)
9871 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9872 }
9873
9874 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9875 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9876 return false;
9877 *power_domain_mask |= BIT(power_domain);
9878
9879 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9880
9881 return tmp & PIPECONF_ENABLE;
9882}
9883
Jani Nikula4d1de972016-03-18 17:05:42 +02009884static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9885 struct intel_crtc_state *pipe_config,
9886 unsigned long *power_domain_mask)
9887{
9888 struct drm_device *dev = crtc->base.dev;
9889 struct drm_i915_private *dev_priv = dev->dev_private;
9890 enum intel_display_power_domain power_domain;
9891 enum port port;
9892 enum transcoder cpu_transcoder;
9893 u32 tmp;
9894
9895 pipe_config->has_dsi_encoder = false;
9896
9897 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9898 if (port == PORT_A)
9899 cpu_transcoder = TRANSCODER_DSI_A;
9900 else
9901 cpu_transcoder = TRANSCODER_DSI_C;
9902
9903 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9904 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9905 continue;
9906 *power_domain_mask |= BIT(power_domain);
9907
Imre Deakdb18b6a2016-03-24 12:41:40 +02009908 /*
9909 * The PLL needs to be enabled with a valid divider
9910 * configuration, otherwise accessing DSI registers will hang
9911 * the machine. See BSpec North Display Engine
9912 * registers/MIPI[BXT]. We can break out here early, since we
9913 * need the same DSI PLL to be enabled for both DSI ports.
9914 */
9915 if (!intel_dsi_pll_is_enabled(dev_priv))
9916 break;
9917
Jani Nikula4d1de972016-03-18 17:05:42 +02009918 /* XXX: this works for video mode only */
9919 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9920 if (!(tmp & DPI_ENABLE))
9921 continue;
9922
9923 tmp = I915_READ(MIPI_CTRL(port));
9924 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9925 continue;
9926
9927 pipe_config->cpu_transcoder = cpu_transcoder;
9928 pipe_config->has_dsi_encoder = true;
9929 break;
9930 }
9931
9932 return pipe_config->has_dsi_encoder;
9933}
9934
Daniel Vetter26804af2014-06-25 22:01:55 +03009935static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009936 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009937{
9938 struct drm_device *dev = crtc->base.dev;
9939 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009940 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009941 enum port port;
9942 uint32_t tmp;
9943
9944 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9945
9946 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9947
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009948 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009949 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309950 else if (IS_BROXTON(dev))
9951 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009952 else
9953 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009954
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009955 pll = pipe_config->shared_dpll;
9956 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009957 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9958 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009959 }
9960
Daniel Vetter26804af2014-06-25 22:01:55 +03009961 /*
9962 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9963 * DDI E. So just check whether this pipe is wired to DDI E and whether
9964 * the PCH transcoder is on.
9965 */
Damien Lespiauca370452013-12-03 13:56:24 +00009966 if (INTEL_INFO(dev)->gen < 9 &&
9967 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009968 pipe_config->has_pch_encoder = true;
9969
9970 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9971 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9972 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9973
9974 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9975 }
9976}
9977
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009978static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009979 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009980{
9981 struct drm_device *dev = crtc->base.dev;
9982 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009983 enum intel_display_power_domain power_domain;
9984 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009985 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009986
Imre Deak17290502016-02-12 18:55:11 +02009987 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9988 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009989 return false;
Imre Deak17290502016-02-12 18:55:11 +02009990 power_domain_mask = BIT(power_domain);
9991
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009992 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009993
Jani Nikulacf304292016-03-18 17:05:41 +02009994 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009995
Jani Nikula4d1de972016-03-18 17:05:42 +02009996 if (IS_BROXTON(dev_priv)) {
9997 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9998 &power_domain_mask);
9999 WARN_ON(active && pipe_config->has_dsi_encoder);
10000 if (pipe_config->has_dsi_encoder)
10001 active = true;
10002 }
10003
Jani Nikulacf304292016-03-18 17:05:41 +020010004 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010005 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010006
Jani Nikula4d1de972016-03-18 17:05:42 +020010007 if (!pipe_config->has_dsi_encoder) {
10008 haswell_get_ddi_port_state(crtc, pipe_config);
10009 intel_get_pipe_timings(crtc, pipe_config);
10010 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010011
Jani Nikulabc58be62016-03-18 17:05:39 +020010012 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010013
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010014 pipe_config->gamma_mode =
10015 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10016
Chandra Kondurua1b22782015-04-07 15:28:45 -070010017 if (INTEL_INFO(dev)->gen >= 9) {
10018 skl_init_scalers(dev, crtc, pipe_config);
10019 }
10020
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010021 if (INTEL_INFO(dev)->gen >= 9) {
10022 pipe_config->scaler_state.scaler_id = -1;
10023 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10024 }
10025
Imre Deak17290502016-02-12 18:55:11 +020010026 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10027 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10028 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010029 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010030 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010031 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010032 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010033 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010034
Jesse Barnese59150d2014-01-07 13:30:45 -080010035 if (IS_HASWELL(dev))
10036 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10037 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010038
Jani Nikula4d1de972016-03-18 17:05:42 +020010039 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10040 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010041 pipe_config->pixel_multiplier =
10042 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10043 } else {
10044 pipe_config->pixel_multiplier = 1;
10045 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010046
Imre Deak17290502016-02-12 18:55:11 +020010047out:
10048 for_each_power_domain(power_domain, power_domain_mask)
10049 intel_display_power_put(dev_priv, power_domain);
10050
Jani Nikulacf304292016-03-18 17:05:41 +020010051 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010052}
10053
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010054static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10055 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010056{
10057 struct drm_device *dev = crtc->dev;
10058 struct drm_i915_private *dev_priv = dev->dev_private;
10059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010060 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010061
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010062 if (plane_state && plane_state->visible) {
10063 unsigned int width = plane_state->base.crtc_w;
10064 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010065 unsigned int stride = roundup_pow_of_two(width) * 4;
10066
10067 switch (stride) {
10068 default:
10069 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10070 width, stride);
10071 stride = 256;
10072 /* fallthrough */
10073 case 256:
10074 case 512:
10075 case 1024:
10076 case 2048:
10077 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010078 }
10079
Ville Syrjälädc41c152014-08-13 11:57:05 +030010080 cntl |= CURSOR_ENABLE |
10081 CURSOR_GAMMA_ENABLE |
10082 CURSOR_FORMAT_ARGB |
10083 CURSOR_STRIDE(stride);
10084
10085 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010086 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010087
Ville Syrjälädc41c152014-08-13 11:57:05 +030010088 if (intel_crtc->cursor_cntl != 0 &&
10089 (intel_crtc->cursor_base != base ||
10090 intel_crtc->cursor_size != size ||
10091 intel_crtc->cursor_cntl != cntl)) {
10092 /* On these chipsets we can only modify the base/size/stride
10093 * whilst the cursor is disabled.
10094 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010095 I915_WRITE(CURCNTR(PIPE_A), 0);
10096 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010097 intel_crtc->cursor_cntl = 0;
10098 }
10099
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010100 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010101 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010102 intel_crtc->cursor_base = base;
10103 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010104
10105 if (intel_crtc->cursor_size != size) {
10106 I915_WRITE(CURSIZE, size);
10107 intel_crtc->cursor_size = size;
10108 }
10109
Chris Wilson4b0e3332014-05-30 16:35:26 +030010110 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010111 I915_WRITE(CURCNTR(PIPE_A), cntl);
10112 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010113 intel_crtc->cursor_cntl = cntl;
10114 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010115}
10116
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010117static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10118 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010119{
10120 struct drm_device *dev = crtc->dev;
10121 struct drm_i915_private *dev_priv = dev->dev_private;
10122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10123 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010124 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010125
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010126 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010127 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010128 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010129 case 64:
10130 cntl |= CURSOR_MODE_64_ARGB_AX;
10131 break;
10132 case 128:
10133 cntl |= CURSOR_MODE_128_ARGB_AX;
10134 break;
10135 case 256:
10136 cntl |= CURSOR_MODE_256_ARGB_AX;
10137 break;
10138 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010139 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010140 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010141 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010142 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010143
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010144 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010145 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010146
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010147 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10148 cntl |= CURSOR_ROTATE_180;
10149 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010150
Chris Wilson4b0e3332014-05-30 16:35:26 +030010151 if (intel_crtc->cursor_cntl != cntl) {
10152 I915_WRITE(CURCNTR(pipe), cntl);
10153 POSTING_READ(CURCNTR(pipe));
10154 intel_crtc->cursor_cntl = cntl;
10155 }
10156
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010157 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010158 I915_WRITE(CURBASE(pipe), base);
10159 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010160
10161 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010162}
10163
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010164/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010165static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010166 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010167{
10168 struct drm_device *dev = crtc->dev;
10169 struct drm_i915_private *dev_priv = dev->dev_private;
10170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10171 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010172 u32 base = intel_crtc->cursor_addr;
10173 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010174
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010175 if (plane_state) {
10176 int x = plane_state->base.crtc_x;
10177 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010178
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010179 if (x < 0) {
10180 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10181 x = -x;
10182 }
10183 pos |= x << CURSOR_X_SHIFT;
10184
10185 if (y < 0) {
10186 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10187 y = -y;
10188 }
10189 pos |= y << CURSOR_Y_SHIFT;
10190
10191 /* ILK+ do this automagically */
10192 if (HAS_GMCH_DISPLAY(dev) &&
10193 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10194 base += (plane_state->base.crtc_h *
10195 plane_state->base.crtc_w - 1) * 4;
10196 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010197 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010198
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010199 I915_WRITE(CURPOS(pipe), pos);
10200
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010201 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010202 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010203 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010204 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010205}
10206
Ville Syrjälädc41c152014-08-13 11:57:05 +030010207static bool cursor_size_ok(struct drm_device *dev,
10208 uint32_t width, uint32_t height)
10209{
10210 if (width == 0 || height == 0)
10211 return false;
10212
10213 /*
10214 * 845g/865g are special in that they are only limited by
10215 * the width of their cursors, the height is arbitrary up to
10216 * the precision of the register. Everything else requires
10217 * square cursors, limited to a few power-of-two sizes.
10218 */
10219 if (IS_845G(dev) || IS_I865G(dev)) {
10220 if ((width & 63) != 0)
10221 return false;
10222
10223 if (width > (IS_845G(dev) ? 64 : 512))
10224 return false;
10225
10226 if (height > 1023)
10227 return false;
10228 } else {
10229 switch (width | height) {
10230 case 256:
10231 case 128:
10232 if (IS_GEN2(dev))
10233 return false;
10234 case 64:
10235 break;
10236 default:
10237 return false;
10238 }
10239 }
10240
10241 return true;
10242}
10243
Jesse Barnes79e53942008-11-07 14:24:08 -080010244/* VESA 640x480x72Hz mode to set on the pipe */
10245static struct drm_display_mode load_detect_mode = {
10246 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10247 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10248};
10249
Daniel Vettera8bb6812014-02-10 18:00:39 +010010250struct drm_framebuffer *
10251__intel_framebuffer_create(struct drm_device *dev,
10252 struct drm_mode_fb_cmd2 *mode_cmd,
10253 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010254{
10255 struct intel_framebuffer *intel_fb;
10256 int ret;
10257
10258 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010259 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010260 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010261
10262 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010263 if (ret)
10264 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010265
10266 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010267
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010268err:
10269 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010270 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010271}
10272
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010273static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010274intel_framebuffer_create(struct drm_device *dev,
10275 struct drm_mode_fb_cmd2 *mode_cmd,
10276 struct drm_i915_gem_object *obj)
10277{
10278 struct drm_framebuffer *fb;
10279 int ret;
10280
10281 ret = i915_mutex_lock_interruptible(dev);
10282 if (ret)
10283 return ERR_PTR(ret);
10284 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10285 mutex_unlock(&dev->struct_mutex);
10286
10287 return fb;
10288}
10289
Chris Wilsond2dff872011-04-19 08:36:26 +010010290static u32
10291intel_framebuffer_pitch_for_width(int width, int bpp)
10292{
10293 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10294 return ALIGN(pitch, 64);
10295}
10296
10297static u32
10298intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10299{
10300 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010301 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010302}
10303
10304static struct drm_framebuffer *
10305intel_framebuffer_create_for_mode(struct drm_device *dev,
10306 struct drm_display_mode *mode,
10307 int depth, int bpp)
10308{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010309 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010310 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010311 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010312
Dave Gordond37cd8a2016-04-22 19:14:32 +010010313 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010314 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010315 if (IS_ERR(obj))
10316 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010317
10318 mode_cmd.width = mode->hdisplay;
10319 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010320 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10321 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010322 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010323
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010324 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10325 if (IS_ERR(fb))
10326 drm_gem_object_unreference_unlocked(&obj->base);
10327
10328 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010329}
10330
10331static struct drm_framebuffer *
10332mode_fits_in_fbdev(struct drm_device *dev,
10333 struct drm_display_mode *mode)
10334{
Daniel Vetter06957262015-08-10 13:34:08 +020010335#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010336 struct drm_i915_private *dev_priv = dev->dev_private;
10337 struct drm_i915_gem_object *obj;
10338 struct drm_framebuffer *fb;
10339
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010340 if (!dev_priv->fbdev)
10341 return NULL;
10342
10343 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010344 return NULL;
10345
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010346 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010347 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010348
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010349 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010350 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10351 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010352 return NULL;
10353
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010354 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010355 return NULL;
10356
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010357 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010358 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010359#else
10360 return NULL;
10361#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010362}
10363
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010364static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10365 struct drm_crtc *crtc,
10366 struct drm_display_mode *mode,
10367 struct drm_framebuffer *fb,
10368 int x, int y)
10369{
10370 struct drm_plane_state *plane_state;
10371 int hdisplay, vdisplay;
10372 int ret;
10373
10374 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10375 if (IS_ERR(plane_state))
10376 return PTR_ERR(plane_state);
10377
10378 if (mode)
10379 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10380 else
10381 hdisplay = vdisplay = 0;
10382
10383 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10384 if (ret)
10385 return ret;
10386 drm_atomic_set_fb_for_plane(plane_state, fb);
10387 plane_state->crtc_x = 0;
10388 plane_state->crtc_y = 0;
10389 plane_state->crtc_w = hdisplay;
10390 plane_state->crtc_h = vdisplay;
10391 plane_state->src_x = x << 16;
10392 plane_state->src_y = y << 16;
10393 plane_state->src_w = hdisplay << 16;
10394 plane_state->src_h = vdisplay << 16;
10395
10396 return 0;
10397}
10398
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010399bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010400 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010401 struct intel_load_detect_pipe *old,
10402 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010403{
10404 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010405 struct intel_encoder *intel_encoder =
10406 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010407 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010408 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010409 struct drm_crtc *crtc = NULL;
10410 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010411 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010412 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010413 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010414 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010415 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010416 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010417
Chris Wilsond2dff872011-04-19 08:36:26 +010010418 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010419 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010420 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010421
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010422 old->restore_state = NULL;
10423
Rob Clark51fd3712013-11-19 12:10:12 -050010424retry:
10425 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10426 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010427 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010428
Jesse Barnes79e53942008-11-07 14:24:08 -080010429 /*
10430 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010431 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010432 * - if the connector already has an assigned crtc, use it (but make
10433 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010434 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010435 * - try to find the first unused crtc that can drive this connector,
10436 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010437 */
10438
10439 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010440 if (connector->state->crtc) {
10441 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010442
Rob Clark51fd3712013-11-19 12:10:12 -050010443 ret = drm_modeset_lock(&crtc->mutex, ctx);
10444 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010445 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010446
10447 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010448 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010449 }
10450
10451 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010452 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010453 i++;
10454 if (!(encoder->possible_crtcs & (1 << i)))
10455 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010456
10457 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10458 if (ret)
10459 goto fail;
10460
10461 if (possible_crtc->state->enable) {
10462 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010463 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010464 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010465
10466 crtc = possible_crtc;
10467 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010468 }
10469
10470 /*
10471 * If we didn't find an unused CRTC, don't use any.
10472 */
10473 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010474 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010475 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010476 }
10477
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010478found:
10479 intel_crtc = to_intel_crtc(crtc);
10480
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010481 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10482 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010483 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010484
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010485 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010486 restore_state = drm_atomic_state_alloc(dev);
10487 if (!state || !restore_state) {
10488 ret = -ENOMEM;
10489 goto fail;
10490 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010491
10492 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010493 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010494
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010495 connector_state = drm_atomic_get_connector_state(state, connector);
10496 if (IS_ERR(connector_state)) {
10497 ret = PTR_ERR(connector_state);
10498 goto fail;
10499 }
10500
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010501 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10502 if (ret)
10503 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010504
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010505 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10506 if (IS_ERR(crtc_state)) {
10507 ret = PTR_ERR(crtc_state);
10508 goto fail;
10509 }
10510
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010511 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010512
Chris Wilson64927112011-04-20 07:25:26 +010010513 if (!mode)
10514 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010515
Chris Wilsond2dff872011-04-19 08:36:26 +010010516 /* We need a framebuffer large enough to accommodate all accesses
10517 * that the plane may generate whilst we perform load detection.
10518 * We can not rely on the fbcon either being present (we get called
10519 * during its initialisation to detect all boot displays, or it may
10520 * not even exist) or that it is large enough to satisfy the
10521 * requested mode.
10522 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010523 fb = mode_fits_in_fbdev(dev, mode);
10524 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010525 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010526 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010527 } else
10528 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010529 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010530 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010531 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010532 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010533
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010534 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10535 if (ret)
10536 goto fail;
10537
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010538 drm_framebuffer_unreference(fb);
10539
10540 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10541 if (ret)
10542 goto fail;
10543
10544 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10545 if (!ret)
10546 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10547 if (!ret)
10548 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10549 if (ret) {
10550 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10551 goto fail;
10552 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010553
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010554 ret = drm_atomic_commit(state);
10555 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010556 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010557 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010558 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010559
10560 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010561
Jesse Barnes79e53942008-11-07 14:24:08 -080010562 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010563 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010564 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010565
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010566fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010567 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010568 drm_atomic_state_free(restore_state);
10569 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010570
Rob Clark51fd3712013-11-19 12:10:12 -050010571 if (ret == -EDEADLK) {
10572 drm_modeset_backoff(ctx);
10573 goto retry;
10574 }
10575
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010576 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010577}
10578
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010579void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010580 struct intel_load_detect_pipe *old,
10581 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010582{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010583 struct intel_encoder *intel_encoder =
10584 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010585 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010586 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010587 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010588
Chris Wilsond2dff872011-04-19 08:36:26 +010010589 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010590 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010591 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010592
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010593 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010594 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010595
10596 ret = drm_atomic_commit(state);
10597 if (ret) {
10598 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10599 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010600 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010601}
10602
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010603static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010604 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010605{
10606 struct drm_i915_private *dev_priv = dev->dev_private;
10607 u32 dpll = pipe_config->dpll_hw_state.dpll;
10608
10609 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010610 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010611 else if (HAS_PCH_SPLIT(dev))
10612 return 120000;
10613 else if (!IS_GEN2(dev))
10614 return 96000;
10615 else
10616 return 48000;
10617}
10618
Jesse Barnes79e53942008-11-07 14:24:08 -080010619/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010620static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010621 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010622{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010623 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010624 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010625 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010626 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010627 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010628 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010629 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010630 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010631
10632 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010633 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010634 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010635 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010636
10637 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010638 if (IS_PINEVIEW(dev)) {
10639 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10640 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010641 } else {
10642 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10643 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10644 }
10645
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010646 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010647 if (IS_PINEVIEW(dev))
10648 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10649 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010650 else
10651 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010652 DPLL_FPA01_P1_POST_DIV_SHIFT);
10653
10654 switch (dpll & DPLL_MODE_MASK) {
10655 case DPLLB_MODE_DAC_SERIAL:
10656 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10657 5 : 10;
10658 break;
10659 case DPLLB_MODE_LVDS:
10660 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10661 7 : 14;
10662 break;
10663 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010664 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010665 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010666 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010667 }
10668
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010669 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010670 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010671 else
Imre Deakdccbea32015-06-22 23:35:51 +030010672 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010673 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010674 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010675 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010676
10677 if (is_lvds) {
10678 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10679 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010680
10681 if (lvds & LVDS_CLKB_POWER_UP)
10682 clock.p2 = 7;
10683 else
10684 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010685 } else {
10686 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10687 clock.p1 = 2;
10688 else {
10689 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10690 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10691 }
10692 if (dpll & PLL_P2_DIVIDE_BY_4)
10693 clock.p2 = 4;
10694 else
10695 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010696 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010697
Imre Deakdccbea32015-06-22 23:35:51 +030010698 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010699 }
10700
Ville Syrjälä18442d02013-09-13 16:00:08 +030010701 /*
10702 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010703 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010704 * encoder's get_config() function.
10705 */
Imre Deakdccbea32015-06-22 23:35:51 +030010706 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010707}
10708
Ville Syrjälä6878da02013-09-13 15:59:11 +030010709int intel_dotclock_calculate(int link_freq,
10710 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010711{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010712 /*
10713 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010714 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010715 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010716 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010717 *
10718 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010719 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010720 */
10721
Ville Syrjälä6878da02013-09-13 15:59:11 +030010722 if (!m_n->link_n)
10723 return 0;
10724
10725 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10726}
10727
Ville Syrjälä18442d02013-09-13 16:00:08 +030010728static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010729 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010730{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010731 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010732
10733 /* read out port_clock from the DPLL */
10734 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010735
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010736 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010737 * In case there is an active pipe without active ports,
10738 * we may need some idea for the dotclock anyway.
10739 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010740 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010741 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010742 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010743 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010744}
10745
10746/** Returns the currently programmed mode of the given pipe. */
10747struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10748 struct drm_crtc *crtc)
10749{
Jesse Barnes548f2452011-02-17 10:40:53 -080010750 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010752 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010753 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010754 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010755 int htot = I915_READ(HTOTAL(cpu_transcoder));
10756 int hsync = I915_READ(HSYNC(cpu_transcoder));
10757 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10758 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010759 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010760
10761 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10762 if (!mode)
10763 return NULL;
10764
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010765 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10766 if (!pipe_config) {
10767 kfree(mode);
10768 return NULL;
10769 }
10770
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010771 /*
10772 * Construct a pipe_config sufficient for getting the clock info
10773 * back out of crtc_clock_get.
10774 *
10775 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10776 * to use a real value here instead.
10777 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010778 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10779 pipe_config->pixel_multiplier = 1;
10780 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10781 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10782 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10783 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010784
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010785 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010786 mode->hdisplay = (htot & 0xffff) + 1;
10787 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10788 mode->hsync_start = (hsync & 0xffff) + 1;
10789 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10790 mode->vdisplay = (vtot & 0xffff) + 1;
10791 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10792 mode->vsync_start = (vsync & 0xffff) + 1;
10793 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10794
10795 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010796
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010797 kfree(pipe_config);
10798
Jesse Barnes79e53942008-11-07 14:24:08 -080010799 return mode;
10800}
10801
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010802void intel_mark_busy(struct drm_i915_private *dev_priv)
Jesse Barnes652c3932009-08-17 13:31:43 -070010803{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010804 if (dev_priv->mm.busy)
10805 return;
10806
Paulo Zanoni43694d62014-03-07 20:08:08 -030010807 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010808 i915_update_gfx_val(dev_priv);
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010809 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010810 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010811 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010812}
10813
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010814void intel_mark_idle(struct drm_i915_private *dev_priv)
Chris Wilsonf047e392012-07-21 12:31:41 +010010815{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010816 if (!dev_priv->mm.busy)
10817 return;
10818
10819 dev_priv->mm.busy = false;
10820
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010821 if (INTEL_GEN(dev_priv) >= 6)
10822 gen6_rps_idle(dev_priv);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010823
Paulo Zanoni43694d62014-03-07 20:08:08 -030010824 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010825}
10826
Jesse Barnes79e53942008-11-07 14:24:08 -080010827static void intel_crtc_destroy(struct drm_crtc *crtc)
10828{
10829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010830 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010831 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010832
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010833 spin_lock_irq(&dev->event_lock);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010834 work = intel_crtc->flip_work;
10835 intel_crtc->flip_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010836 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010837
10838 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010839 cancel_work_sync(&work->mmio_work);
10840 cancel_work_sync(&work->unpin_work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010841 kfree(work);
10842 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010843
10844 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010845
Jesse Barnes79e53942008-11-07 14:24:08 -080010846 kfree(intel_crtc);
10847}
10848
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010849static void intel_unpin_work_fn(struct work_struct *__work)
10850{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010851 struct intel_flip_work *work =
10852 container_of(__work, struct intel_flip_work, unpin_work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010853 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10854 struct drm_device *dev = crtc->base.dev;
10855 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010856
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010857 if (is_mmio_work(work))
10858 flush_work(&work->mmio_work);
10859
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010860 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010861 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010862 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010863
John Harrisonf06cc1b2014-11-24 18:49:37 +000010864 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010865 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010866 mutex_unlock(&dev->struct_mutex);
10867
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010868 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010869 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010870 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010871
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010872 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10873 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010874
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010875 kfree(work);
10876}
10877
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010878/* Is 'a' after or equal to 'b'? */
10879static bool g4x_flip_count_after_eq(u32 a, u32 b)
10880{
10881 return !((a - b) & 0x80000000);
10882}
10883
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010884static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10885 struct intel_flip_work *work)
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010886{
10887 struct drm_device *dev = crtc->base.dev;
10888 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc19ae982016-04-13 17:35:03 +010010889 unsigned reset_counter;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010890
Chris Wilsonc19ae982016-04-13 17:35:03 +010010891 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010010892 if (crtc->reset_counter != reset_counter)
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010893 return true;
10894
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010895 /*
10896 * The relevant registers doen't exist on pre-ctg.
10897 * As the flip done interrupt doesn't trigger for mmio
10898 * flips on gmch platforms, a flip count check isn't
10899 * really needed there. But since ctg has the registers,
10900 * include it in the check anyway.
10901 */
10902 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10903 return true;
10904
10905 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010906 * BDW signals flip done immediately if the plane
10907 * is disabled, even if the plane enable is already
10908 * armed to occur at the next vblank :(
10909 */
10910
10911 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010912 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10913 * used the same base address. In that case the mmio flip might
10914 * have completed, but the CS hasn't even executed the flip yet.
10915 *
10916 * A flip count check isn't enough as the CS might have updated
10917 * the base address just after start of vblank, but before we
10918 * managed to process the interrupt. This means we'd complete the
10919 * CS flip too soon.
10920 *
10921 * Combining both checks should get us a good enough result. It may
10922 * still happen that the CS flip has been executed, but has not
10923 * yet actually completed. But in case the base address is the same
10924 * anyway, we don't really care.
10925 */
10926 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010927 crtc->flip_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030010928 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010929 crtc->flip_work->flip_count);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010930}
10931
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010932static bool
10933__pageflip_finished_mmio(struct intel_crtc *crtc,
10934 struct intel_flip_work *work)
10935{
10936 /*
10937 * MMIO work completes when vblank is different from
10938 * flip_queued_vblank.
10939 *
10940 * Reset counter value doesn't matter, this is handled by
10941 * i915_wait_request finishing early, so no need to handle
10942 * reset here.
10943 */
10944 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
10945}
10946
10947
10948static bool pageflip_finished(struct intel_crtc *crtc,
10949 struct intel_flip_work *work)
10950{
10951 if (!atomic_read(&work->pending))
10952 return false;
10953
10954 smp_rmb();
10955
10956 if (is_mmio_work(work))
10957 return __pageflip_finished_mmio(crtc, work);
10958 else
10959 return __pageflip_finished_cs(crtc, work);
10960}
10961
10962void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010963{
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010010964 struct drm_device *dev = dev_priv->dev;
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010965 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010967 struct intel_flip_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010968 unsigned long flags;
10969
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010970 /* Ignore early vblank irqs */
10971 if (!crtc)
10972 return;
Daniel Vetterf3260382014-09-15 14:55:23 +020010973
10974 /*
10975 * This is called both by irq handlers and the reset code (to complete
10976 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000010977 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010978 spin_lock_irqsave(&dev->event_lock, flags);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010979 work = intel_crtc->flip_work;
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010980
10981 if (work != NULL &&
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010982 !is_mmio_work(work) &&
10983 pageflip_finished(intel_crtc, work))
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010984 page_flip_completed(intel_crtc);
10985
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010986 spin_unlock_irqrestore(&dev->event_lock, flags);
10987}
10988
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010989void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010990{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010991 struct drm_device *dev = dev_priv->dev;
10992 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10994 struct intel_flip_work *work;
10995 unsigned long flags;
10996
10997 /* Ignore early vblank irqs */
10998 if (!crtc)
10999 return;
11000
11001 /*
11002 * This is called both by irq handlers and the reset code (to complete
11003 * lost pageflips) so needs the full irqsave spinlocks.
11004 */
11005 spin_lock_irqsave(&dev->event_lock, flags);
11006 work = intel_crtc->flip_work;
11007
11008 if (work != NULL &&
11009 is_mmio_work(work) &&
11010 pageflip_finished(intel_crtc, work))
11011 page_flip_completed(intel_crtc);
11012
11013 spin_unlock_irqrestore(&dev->event_lock, flags);
11014}
11015
11016static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11017 struct intel_flip_work *work)
11018{
11019 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11020
Chris Wilsone7d841c2012-12-03 11:36:30 +000011021 /* Ensure that the work item is consistent when activating it ... */
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011022 smp_mb__before_atomic();
Maarten Lankhorst5251f042016-05-17 15:07:47 +020011023 atomic_set(&work->pending, 1);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011024}
11025
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011026static int intel_gen2_queue_flip(struct drm_device *dev,
11027 struct drm_crtc *crtc,
11028 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011029 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011030 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011031 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011032{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011033 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011035 u32 flip_mask;
11036 int ret;
11037
John Harrison5fb9de12015-05-29 17:44:07 +010011038 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011039 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011040 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011041
11042 /* Can't queue multiple flips, so wait for the previous
11043 * one to finish before executing the next.
11044 */
11045 if (intel_crtc->plane)
11046 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11047 else
11048 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011049 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11050 intel_ring_emit(engine, MI_NOOP);
11051 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011052 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011053 intel_ring_emit(engine, fb->pitches[0]);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011054 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011055 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011056
Chris Wilson83d40922012-04-17 19:35:53 +010011057 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011058}
11059
11060static int intel_gen3_queue_flip(struct drm_device *dev,
11061 struct drm_crtc *crtc,
11062 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011063 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011064 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011065 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011066{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011067 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011069 u32 flip_mask;
11070 int ret;
11071
John Harrison5fb9de12015-05-29 17:44:07 +010011072 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011073 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011074 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011075
11076 if (intel_crtc->plane)
11077 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11078 else
11079 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011080 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11081 intel_ring_emit(engine, MI_NOOP);
11082 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011083 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011084 intel_ring_emit(engine, fb->pitches[0]);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011085 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011086 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011087
Chris Wilson83d40922012-04-17 19:35:53 +010011088 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011089}
11090
11091static int intel_gen4_queue_flip(struct drm_device *dev,
11092 struct drm_crtc *crtc,
11093 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011094 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011095 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011096 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011097{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011098 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011099 struct drm_i915_private *dev_priv = dev->dev_private;
11100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11101 uint32_t pf, pipesrc;
11102 int ret;
11103
John Harrison5fb9de12015-05-29 17:44:07 +010011104 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011105 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011106 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011107
11108 /* i965+ uses the linear or tiled offsets from the
11109 * Display Registers (which do not change across a page-flip)
11110 * so we need only reprogram the base address.
11111 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011112 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011113 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011114 intel_ring_emit(engine, fb->pitches[0]);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011115 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011116 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011117
11118 /* XXX Enabling the panel-fitter across page-flip is so far
11119 * untested on non-native modes, so ignore it for now.
11120 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11121 */
11122 pf = 0;
11123 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011124 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011125
Chris Wilson83d40922012-04-17 19:35:53 +010011126 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011127}
11128
11129static int intel_gen6_queue_flip(struct drm_device *dev,
11130 struct drm_crtc *crtc,
11131 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011132 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011133 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011134 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011135{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011136 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011137 struct drm_i915_private *dev_priv = dev->dev_private;
11138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11139 uint32_t pf, pipesrc;
11140 int ret;
11141
John Harrison5fb9de12015-05-29 17:44:07 +010011142 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011143 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011144 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011145
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011146 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011147 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011148 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011149 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011150
Chris Wilson99d9acd2012-04-17 20:37:00 +010011151 /* Contrary to the suggestions in the documentation,
11152 * "Enable Panel Fitter" does not seem to be required when page
11153 * flipping with a non-native mode, and worse causes a normal
11154 * modeset to fail.
11155 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11156 */
11157 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011158 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011159 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011160
Chris Wilson83d40922012-04-17 19:35:53 +010011161 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011162}
11163
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011164static int intel_gen7_queue_flip(struct drm_device *dev,
11165 struct drm_crtc *crtc,
11166 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011167 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011168 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011169 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011170{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011171 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011173 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011174 int len, ret;
11175
Robin Schroereba905b2014-05-18 02:24:50 +020011176 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011177 case PLANE_A:
11178 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11179 break;
11180 case PLANE_B:
11181 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11182 break;
11183 case PLANE_C:
11184 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11185 break;
11186 default:
11187 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011188 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011189 }
11190
Chris Wilsonffe74d72013-08-26 20:58:12 +010011191 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011192 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011193 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011194 /*
11195 * On Gen 8, SRM is now taking an extra dword to accommodate
11196 * 48bits addresses, and we need a NOOP for the batch size to
11197 * stay even.
11198 */
11199 if (IS_GEN8(dev))
11200 len += 2;
11201 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011202
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011203 /*
11204 * BSpec MI_DISPLAY_FLIP for IVB:
11205 * "The full packet must be contained within the same cache line."
11206 *
11207 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11208 * cacheline, if we ever start emitting more commands before
11209 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11210 * then do the cacheline alignment, and finally emit the
11211 * MI_DISPLAY_FLIP.
11212 */
John Harrisonbba09b12015-05-29 17:44:06 +010011213 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011214 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011215 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011216
John Harrison5fb9de12015-05-29 17:44:07 +010011217 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011218 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011219 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011220
Chris Wilsonffe74d72013-08-26 20:58:12 +010011221 /* Unmask the flip-done completion message. Note that the bspec says that
11222 * we should do this for both the BCS and RCS, and that we must not unmask
11223 * more than one flip event at any time (or ensure that one flip message
11224 * can be sent by waiting for flip-done prior to queueing new flips).
11225 * Experimentation says that BCS works despite DERRMR masking all
11226 * flip-done completion events and that unmasking all planes at once
11227 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11228 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11229 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011230 if (engine->id == RCS) {
11231 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11232 intel_ring_emit_reg(engine, DERRMR);
11233 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11234 DERRMR_PIPEB_PRI_FLIP_DONE |
11235 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011236 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011237 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011238 MI_SRM_LRM_GLOBAL_GTT);
11239 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011240 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011241 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011242 intel_ring_emit_reg(engine, DERRMR);
11243 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011244 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011245 intel_ring_emit(engine, 0);
11246 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011247 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011248 }
11249
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011250 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11251 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011252 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011253 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011254
Chris Wilson83d40922012-04-17 19:35:53 +010011255 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011256}
11257
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011258static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011259 struct drm_i915_gem_object *obj)
11260{
11261 /*
11262 * This is not being used for older platforms, because
11263 * non-availability of flip done interrupt forces us to use
11264 * CS flips. Older platforms derive flip done using some clever
11265 * tricks involving the flip_pending status bits and vblank irqs.
11266 * So using MMIO flips there would disrupt this mechanism.
11267 */
11268
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011269 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011270 return true;
11271
Chris Wilsonc0336662016-05-06 15:40:21 +010011272 if (INTEL_GEN(engine->i915) < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011273 return false;
11274
11275 if (i915.use_mmio_flip < 0)
11276 return false;
11277 else if (i915.use_mmio_flip > 0)
11278 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011279 else if (i915.enable_execlists)
11280 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011281 else if (obj->base.dma_buf &&
11282 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11283 false))
11284 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011285 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011286 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011287}
11288
Chris Wilson60426392015-10-10 10:44:32 +010011289static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011290 unsigned int rotation,
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011291 struct intel_flip_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011292{
11293 struct drm_device *dev = intel_crtc->base.dev;
11294 struct drm_i915_private *dev_priv = dev->dev_private;
11295 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011296 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011297 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011298
11299 ctl = I915_READ(PLANE_CTL(pipe, 0));
11300 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011301 switch (fb->modifier[0]) {
11302 case DRM_FORMAT_MOD_NONE:
11303 break;
11304 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011305 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011306 break;
11307 case I915_FORMAT_MOD_Y_TILED:
11308 ctl |= PLANE_CTL_TILED_Y;
11309 break;
11310 case I915_FORMAT_MOD_Yf_TILED:
11311 ctl |= PLANE_CTL_TILED_YF;
11312 break;
11313 default:
11314 MISSING_CASE(fb->modifier[0]);
11315 }
Damien Lespiauff944562014-11-20 14:58:16 +000011316
11317 /*
11318 * The stride is either expressed as a multiple of 64 bytes chunks for
11319 * linear buffers or in number of tiles for tiled buffers.
11320 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011321 if (intel_rotation_90_or_270(rotation)) {
11322 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011323 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011324 stride = DIV_ROUND_UP(fb->height, tile_height);
11325 } else {
11326 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011327 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11328 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011329 }
Damien Lespiauff944562014-11-20 14:58:16 +000011330
11331 /*
11332 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11333 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11334 */
11335 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11336 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11337
Chris Wilson60426392015-10-10 10:44:32 +010011338 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011339 POSTING_READ(PLANE_SURF(pipe, 0));
11340}
11341
Chris Wilson60426392015-10-10 10:44:32 +010011342static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011343 struct intel_flip_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011344{
11345 struct drm_device *dev = intel_crtc->base.dev;
11346 struct drm_i915_private *dev_priv = dev->dev_private;
11347 struct intel_framebuffer *intel_fb =
11348 to_intel_framebuffer(intel_crtc->base.primary->fb);
11349 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011350 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011351 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011352
Sourab Gupta84c33a62014-06-02 16:47:17 +053011353 dspcntr = I915_READ(reg);
11354
Damien Lespiauc5d97472014-10-25 00:11:11 +010011355 if (obj->tiling_mode != I915_TILING_NONE)
11356 dspcntr |= DISPPLANE_TILED;
11357 else
11358 dspcntr &= ~DISPPLANE_TILED;
11359
Sourab Gupta84c33a62014-06-02 16:47:17 +053011360 I915_WRITE(reg, dspcntr);
11361
Chris Wilson60426392015-10-10 10:44:32 +010011362 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011363 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011364}
11365
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011366static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011367{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011368 struct intel_flip_work *work =
11369 container_of(w, struct intel_flip_work, mmio_work);
11370 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11371 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011372 struct intel_framebuffer *intel_fb =
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011373 to_intel_framebuffer(crtc->base.primary->fb);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011374 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011375
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011376 if (work->flip_queued_req)
11377 WARN_ON(__i915_wait_request(work->flip_queued_req,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011378 false, NULL,
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011379 &dev_priv->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011380
Alex Goinsfd8e0582015-11-25 18:43:38 -080011381 /* For framebuffer backed by dmabuf, wait for fence */
11382 if (obj->base.dma_buf)
11383 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11384 false, false,
11385 MAX_SCHEDULE_TIMEOUT) < 0);
11386
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011387 intel_pipe_update_start(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011388
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011389 if (INTEL_GEN(dev_priv) >= 9)
11390 skl_do_mmio_flip(crtc, work->rotation, work);
11391 else
11392 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11393 ilk_do_mmio_flip(crtc, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011394
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011395 intel_pipe_update_end(crtc, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011396}
11397
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011398static int intel_default_queue_flip(struct drm_device *dev,
11399 struct drm_crtc *crtc,
11400 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011401 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011402 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011403 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011404{
11405 return -ENODEV;
11406}
11407
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011408static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11409 struct intel_crtc *intel_crtc,
11410 struct intel_flip_work *work)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011411{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011412 u32 addr, vblank;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011413
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011414 if (!atomic_read(&work->pending))
Maarten Lankhorst5251f042016-05-17 15:07:47 +020011415 return false;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011416
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011417 smp_rmb();
11418
11419 vblank = intel_crtc_get_vblank_counter(intel_crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011420 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011421 if (work->flip_queued_req &&
11422 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011423 return false;
11424
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011425 work->flip_ready_vblank = vblank;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011426 }
11427
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011428 if (vblank - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011429 return false;
11430
11431 /* Potential stall - if we see that the flip has happened,
11432 * assume a missed interrupt. */
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011433 if (INTEL_GEN(dev_priv) >= 4)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011434 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11435 else
11436 addr = I915_READ(DSPADDR(intel_crtc->plane));
11437
11438 /* There is a potential issue here with a false positive after a flip
11439 * to the same address. We could address this by checking for a
11440 * non-incrementing frame counter.
11441 */
11442 return addr == work->gtt_offset;
11443}
11444
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010011445void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011446{
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010011447 struct drm_device *dev = dev_priv->dev;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011448 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011450 struct intel_flip_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011451
Dave Gordon6c51d462015-03-06 15:34:26 +000011452 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011453
11454 if (crtc == NULL)
11455 return;
11456
Daniel Vetterf3260382014-09-15 14:55:23 +020011457 spin_lock(&dev->event_lock);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011458 work = intel_crtc->flip_work;
11459
11460 if (work != NULL && !is_mmio_work(work) &&
11461 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11462 WARN_ONCE(1,
11463 "Kicking stuck page flip: queued at %d, now %d\n",
11464 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011465 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011466 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011467 }
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011468
11469 if (work != NULL && !is_mmio_work(work) &&
11470 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010011471 intel_queue_rps_boost_for_request(work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011472 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011473}
11474
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011475static int intel_crtc_page_flip(struct drm_crtc *crtc,
11476 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011477 struct drm_pending_vblank_event *event,
11478 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011479{
11480 struct drm_device *dev = crtc->dev;
11481 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011482 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011483 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011484 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011485 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011486 enum pipe pipe = intel_crtc->pipe;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011487 struct intel_flip_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011488 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011489 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011490 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011491 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011492
Matt Roper2ff8fde2014-07-08 07:50:07 -070011493 /*
11494 * drm_mode_page_flip_ioctl() should already catch this, but double
11495 * check to be safe. In the future we may enable pageflipping from
11496 * a disabled primary plane.
11497 */
11498 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11499 return -EBUSY;
11500
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011501 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011502 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011503 return -EINVAL;
11504
11505 /*
11506 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11507 * Note that pitch changes could also affect these register.
11508 */
11509 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011510 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11511 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011512 return -EINVAL;
11513
Chris Wilsonf900db42014-02-20 09:26:13 +000011514 if (i915_terminally_wedged(&dev_priv->gpu_error))
11515 goto out_hang;
11516
Daniel Vetterb14c5672013-09-19 12:18:32 +020011517 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011518 if (work == NULL)
11519 return -ENOMEM;
11520
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011521 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011522 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011523 work->old_fb = old_fb;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011524 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011525
Daniel Vetter87b6b102014-05-15 15:33:46 +020011526 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011527 if (ret)
11528 goto free_work;
11529
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011530 /* We borrow the event spin lock for protecting flip_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011531 spin_lock_irq(&dev->event_lock);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011532 if (intel_crtc->flip_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011533 /* Before declaring the flip queue wedged, check if
11534 * the hardware completed the operation behind our backs.
11535 */
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011536 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011537 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11538 page_flip_completed(intel_crtc);
11539 } else {
11540 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011541 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011542
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011543 drm_crtc_vblank_put(crtc);
11544 kfree(work);
11545 return -EBUSY;
11546 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011547 }
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011548 intel_crtc->flip_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011549 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011550
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011551 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11552 flush_workqueue(dev_priv->wq);
11553
Jesse Barnes75dfca82010-02-10 15:09:44 -080011554 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011555 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011556 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011557
Matt Roperf4510a22014-04-01 15:22:40 -070011558 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011559 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011560 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011561
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011562 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011563
Chris Wilson89ed88b2015-02-16 14:31:49 +000011564 ret = i915_mutex_lock_interruptible(dev);
11565 if (ret)
11566 goto cleanup;
11567
Chris Wilsonc19ae982016-04-13 17:35:03 +010011568 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010011569 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11570 ret = -EIO;
11571 goto cleanup;
11572 }
11573
11574 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011575
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011576 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011577 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011578
Wayne Boyer666a4532015-12-09 12:29:35 -080011579 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011580 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011581 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011582 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011583 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011584 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011585 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011586 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011587 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011588 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011589 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011590 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011591 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011592 }
11593
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011594 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011595
11596 /* When using CS flips, we want to emit semaphores between rings.
11597 * However, when using mmio flips we will create a task to do the
11598 * synchronisation, so all we want here is to pin the framebuffer
11599 * into the display plane and skip any waits.
11600 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011601 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011602 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011603 if (!ret && !request) {
11604 request = i915_gem_request_alloc(engine, NULL);
11605 ret = PTR_ERR_OR_ZERO(request);
11606 }
11607
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011608 if (ret)
11609 goto cleanup_pending;
11610 }
11611
Ville Syrjälä3465c582016-02-15 22:54:43 +020011612 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011613 if (ret)
11614 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011615
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011616 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11617 obj, 0);
11618 work->gtt_offset += intel_crtc->dspaddr_offset;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011619 work->rotation = crtc->primary->state->rotation;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011620
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011621 if (mmio_flip) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011622 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011623
John Harrisonf06cc1b2014-11-24 18:49:37 +000011624 i915_gem_request_assign(&work->flip_queued_req,
11625 obj->last_write_req);
John Harrison6258fbe2015-05-29 17:43:48 +010011626
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011627 schedule_work(&work->mmio_work);
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011628 } else {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011629 i915_gem_request_assign(&work->flip_queued_req, request);
John Harrison6258fbe2015-05-29 17:43:48 +010011630 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011631 page_flip_flags);
11632 if (ret)
11633 goto cleanup_unpin;
11634
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011635 intel_mark_page_flip_active(intel_crtc, work);
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011636
11637 i915_add_request_no_flush(request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011638 }
11639
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011640 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011641 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011642 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011643
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011644 intel_frontbuffer_flip_prepare(dev,
11645 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011646
Jesse Barnese5510fa2010-07-01 16:48:37 -070011647 trace_i915_flip_request(intel_crtc->plane, obj);
11648
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011649 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011650
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011651cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011652 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011653cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011654 if (!IS_ERR_OR_NULL(request))
Chris Wilsonaa9b7812016-04-13 17:35:15 +010011655 i915_add_request_no_flush(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011656 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011657 mutex_unlock(&dev->struct_mutex);
11658cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011659 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011660 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011661
Chris Wilson89ed88b2015-02-16 14:31:49 +000011662 drm_gem_object_unreference_unlocked(&obj->base);
11663 drm_framebuffer_unreference(work->old_fb);
11664
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011665 spin_lock_irq(&dev->event_lock);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011666 intel_crtc->flip_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011667 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011668
Daniel Vetter87b6b102014-05-15 15:33:46 +020011669 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011670free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011671 kfree(work);
11672
Chris Wilsonf900db42014-02-20 09:26:13 +000011673 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011674 struct drm_atomic_state *state;
11675 struct drm_plane_state *plane_state;
11676
Chris Wilsonf900db42014-02-20 09:26:13 +000011677out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011678 state = drm_atomic_state_alloc(dev);
11679 if (!state)
11680 return -ENOMEM;
11681 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11682
11683retry:
11684 plane_state = drm_atomic_get_plane_state(state, primary);
11685 ret = PTR_ERR_OR_ZERO(plane_state);
11686 if (!ret) {
11687 drm_atomic_set_fb_for_plane(plane_state, fb);
11688
11689 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11690 if (!ret)
11691 ret = drm_atomic_commit(state);
11692 }
11693
11694 if (ret == -EDEADLK) {
11695 drm_modeset_backoff(state->acquire_ctx);
11696 drm_atomic_state_clear(state);
11697 goto retry;
11698 }
11699
11700 if (ret)
11701 drm_atomic_state_free(state);
11702
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011703 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011704 spin_lock_irq(&dev->event_lock);
Gustavo Padovan560ce1d2016-04-14 10:48:15 -070011705 drm_crtc_send_vblank_event(crtc, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011706 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011707 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011708 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011709 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011710}
11711
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011712
11713/**
11714 * intel_wm_need_update - Check whether watermarks need updating
11715 * @plane: drm plane
11716 * @state: new plane state
11717 *
11718 * Check current plane state versus the new one to determine whether
11719 * watermarks need to be recalculated.
11720 *
11721 * Returns true or false.
11722 */
11723static bool intel_wm_need_update(struct drm_plane *plane,
11724 struct drm_plane_state *state)
11725{
Matt Roperd21fbe82015-09-24 15:53:12 -070011726 struct intel_plane_state *new = to_intel_plane_state(state);
11727 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11728
11729 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011730 if (new->visible != cur->visible)
11731 return true;
11732
11733 if (!cur->base.fb || !new->base.fb)
11734 return false;
11735
11736 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11737 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011738 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11739 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11740 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11741 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011742 return true;
11743
11744 return false;
11745}
11746
Matt Roperd21fbe82015-09-24 15:53:12 -070011747static bool needs_scaling(struct intel_plane_state *state)
11748{
11749 int src_w = drm_rect_width(&state->src) >> 16;
11750 int src_h = drm_rect_height(&state->src) >> 16;
11751 int dst_w = drm_rect_width(&state->dst);
11752 int dst_h = drm_rect_height(&state->dst);
11753
11754 return (src_w != dst_w || src_h != dst_h);
11755}
11756
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011757int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11758 struct drm_plane_state *plane_state)
11759{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011760 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011761 struct drm_crtc *crtc = crtc_state->crtc;
11762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11763 struct drm_plane *plane = plane_state->plane;
11764 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011765 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011766 struct intel_plane_state *old_plane_state =
11767 to_intel_plane_state(plane->state);
11768 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011769 bool mode_changed = needs_modeset(crtc_state);
11770 bool was_crtc_enabled = crtc->state->active;
11771 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011772 bool turn_off, turn_on, visible, was_visible;
11773 struct drm_framebuffer *fb = plane_state->fb;
11774
11775 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11776 plane->type != DRM_PLANE_TYPE_CURSOR) {
11777 ret = skl_update_scaler_plane(
11778 to_intel_crtc_state(crtc_state),
11779 to_intel_plane_state(plane_state));
11780 if (ret)
11781 return ret;
11782 }
11783
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011784 was_visible = old_plane_state->visible;
11785 visible = to_intel_plane_state(plane_state)->visible;
11786
11787 if (!was_crtc_enabled && WARN_ON(was_visible))
11788 was_visible = false;
11789
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011790 /*
11791 * Visibility is calculated as if the crtc was on, but
11792 * after scaler setup everything depends on it being off
11793 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011794 *
11795 * FIXME this is wrong for watermarks. Watermarks should also
11796 * be computed as if the pipe would be active. Perhaps move
11797 * per-plane wm computation to the .check_plane() hook, and
11798 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011799 */
11800 if (!is_crtc_enabled)
11801 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011802
11803 if (!was_visible && !visible)
11804 return 0;
11805
Maarten Lankhorste8861672016-02-24 11:24:26 +010011806 if (fb != old_plane_state->base.fb)
11807 pipe_config->fb_changed = true;
11808
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011809 turn_off = was_visible && (!visible || mode_changed);
11810 turn_on = visible && (!was_visible || mode_changed);
11811
11812 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11813 plane->base.id, fb ? fb->base.id : -1);
11814
11815 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11816 plane->base.id, was_visible, visible,
11817 turn_off, turn_on, mode_changed);
11818
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011819 if (turn_on) {
11820 pipe_config->update_wm_pre = true;
11821
11822 /* must disable cxsr around plane enable/disable */
11823 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11824 pipe_config->disable_cxsr = true;
11825 } else if (turn_off) {
11826 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011827
Ville Syrjälä852eb002015-06-24 22:00:07 +030011828 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011829 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011830 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011831 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011832 /* FIXME bollocks */
11833 pipe_config->update_wm_pre = true;
11834 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011835 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011836
Matt Ropered4a6a72016-02-23 17:20:13 -080011837 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011838 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11839 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011840 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11841
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011842 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011843 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011844
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011845 /*
11846 * WaCxSRDisabledForSpriteScaling:ivb
11847 *
11848 * cstate->update_wm was already set above, so this flag will
11849 * take effect when we commit and program watermarks.
11850 */
11851 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11852 needs_scaling(to_intel_plane_state(plane_state)) &&
11853 !needs_scaling(old_plane_state))
11854 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011855
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011856 return 0;
11857}
11858
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011859static bool encoders_cloneable(const struct intel_encoder *a,
11860 const struct intel_encoder *b)
11861{
11862 /* masks could be asymmetric, so check both ways */
11863 return a == b || (a->cloneable & (1 << b->type) &&
11864 b->cloneable & (1 << a->type));
11865}
11866
11867static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11868 struct intel_crtc *crtc,
11869 struct intel_encoder *encoder)
11870{
11871 struct intel_encoder *source_encoder;
11872 struct drm_connector *connector;
11873 struct drm_connector_state *connector_state;
11874 int i;
11875
11876 for_each_connector_in_state(state, connector, connector_state, i) {
11877 if (connector_state->crtc != &crtc->base)
11878 continue;
11879
11880 source_encoder =
11881 to_intel_encoder(connector_state->best_encoder);
11882 if (!encoders_cloneable(encoder, source_encoder))
11883 return false;
11884 }
11885
11886 return true;
11887}
11888
11889static bool check_encoder_cloning(struct drm_atomic_state *state,
11890 struct intel_crtc *crtc)
11891{
11892 struct intel_encoder *encoder;
11893 struct drm_connector *connector;
11894 struct drm_connector_state *connector_state;
11895 int i;
11896
11897 for_each_connector_in_state(state, connector, connector_state, i) {
11898 if (connector_state->crtc != &crtc->base)
11899 continue;
11900
11901 encoder = to_intel_encoder(connector_state->best_encoder);
11902 if (!check_single_encoder_cloning(state, crtc, encoder))
11903 return false;
11904 }
11905
11906 return true;
11907}
11908
11909static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11910 struct drm_crtc_state *crtc_state)
11911{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011912 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011913 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011915 struct intel_crtc_state *pipe_config =
11916 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011917 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011918 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011919 bool mode_changed = needs_modeset(crtc_state);
11920
11921 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11922 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11923 return -EINVAL;
11924 }
11925
Ville Syrjälä852eb002015-06-24 22:00:07 +030011926 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011927 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011928
Maarten Lankhorstad421372015-06-15 12:33:42 +020011929 if (mode_changed && crtc_state->enable &&
11930 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011931 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011932 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11933 pipe_config);
11934 if (ret)
11935 return ret;
11936 }
11937
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011938 if (crtc_state->color_mgmt_changed) {
11939 ret = intel_color_check(crtc, crtc_state);
11940 if (ret)
11941 return ret;
11942 }
11943
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011944 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011945 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011946 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011947 if (ret) {
11948 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011949 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011950 }
11951 }
11952
11953 if (dev_priv->display.compute_intermediate_wm &&
11954 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11955 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11956 return 0;
11957
11958 /*
11959 * Calculate 'intermediate' watermarks that satisfy both the
11960 * old state and the new state. We can program these
11961 * immediately.
11962 */
11963 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11964 intel_crtc,
11965 pipe_config);
11966 if (ret) {
11967 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11968 return ret;
11969 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070011970 } else if (dev_priv->display.compute_intermediate_wm) {
11971 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11972 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011973 }
11974
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011975 if (INTEL_INFO(dev)->gen >= 9) {
11976 if (mode_changed)
11977 ret = skl_update_scaler_crtc(pipe_config);
11978
11979 if (!ret)
11980 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11981 pipe_config);
11982 }
11983
11984 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011985}
11986
Jani Nikula65b38e02015-04-13 11:26:56 +030011987static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011988 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011989 .atomic_begin = intel_begin_crtc_commit,
11990 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011991 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011992};
11993
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011994static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11995{
11996 struct intel_connector *connector;
11997
11998 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020011999 if (connector->base.state->crtc)
12000 drm_connector_unreference(&connector->base);
12001
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012002 if (connector->base.encoder) {
12003 connector->base.state->best_encoder =
12004 connector->base.encoder;
12005 connector->base.state->crtc =
12006 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012007
12008 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012009 } else {
12010 connector->base.state->best_encoder = NULL;
12011 connector->base.state->crtc = NULL;
12012 }
12013 }
12014}
12015
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012016static void
Robin Schroereba905b2014-05-18 02:24:50 +020012017connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012018 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012019{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012020 int bpp = pipe_config->pipe_bpp;
12021
12022 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12023 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012024 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012025
12026 /* Don't use an invalid EDID bpc value */
12027 if (connector->base.display_info.bpc &&
12028 connector->base.display_info.bpc * 3 < bpp) {
12029 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12030 bpp, connector->base.display_info.bpc*3);
12031 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12032 }
12033
Jani Nikula013dd9e2016-01-13 16:35:20 +020012034 /* Clamp bpp to default limit on screens without EDID 1.4 */
12035 if (connector->base.display_info.bpc == 0) {
12036 int type = connector->base.connector_type;
12037 int clamp_bpp = 24;
12038
12039 /* Fall back to 18 bpp when DP sink capability is unknown. */
12040 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12041 type == DRM_MODE_CONNECTOR_eDP)
12042 clamp_bpp = 18;
12043
12044 if (bpp > clamp_bpp) {
12045 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12046 bpp, clamp_bpp);
12047 pipe_config->pipe_bpp = clamp_bpp;
12048 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012049 }
12050}
12051
12052static int
12053compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012054 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012055{
12056 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012057 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012058 struct drm_connector *connector;
12059 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012060 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012061
Wayne Boyer666a4532015-12-09 12:29:35 -080012062 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012063 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012064 else if (INTEL_INFO(dev)->gen >= 5)
12065 bpp = 12*3;
12066 else
12067 bpp = 8*3;
12068
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012069
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012070 pipe_config->pipe_bpp = bpp;
12071
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012072 state = pipe_config->base.state;
12073
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012074 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012075 for_each_connector_in_state(state, connector, connector_state, i) {
12076 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012077 continue;
12078
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012079 connected_sink_compute_bpp(to_intel_connector(connector),
12080 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012081 }
12082
12083 return bpp;
12084}
12085
Daniel Vetter644db712013-09-19 14:53:58 +020012086static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12087{
12088 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12089 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012090 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012091 mode->crtc_hdisplay, mode->crtc_hsync_start,
12092 mode->crtc_hsync_end, mode->crtc_htotal,
12093 mode->crtc_vdisplay, mode->crtc_vsync_start,
12094 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12095}
12096
Daniel Vetterc0b03412013-05-28 12:05:54 +020012097static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012098 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012099 const char *context)
12100{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012101 struct drm_device *dev = crtc->base.dev;
12102 struct drm_plane *plane;
12103 struct intel_plane *intel_plane;
12104 struct intel_plane_state *state;
12105 struct drm_framebuffer *fb;
12106
12107 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12108 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012109
Jani Nikulada205632016-03-15 21:51:10 +020012110 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012111 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12112 pipe_config->pipe_bpp, pipe_config->dither);
12113 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12114 pipe_config->has_pch_encoder,
12115 pipe_config->fdi_lanes,
12116 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12117 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12118 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012119 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 +030012120 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012121 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012122 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12123 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12124 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012125
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012126 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 -070012127 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012128 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012129 pipe_config->dp_m2_n2.gmch_m,
12130 pipe_config->dp_m2_n2.gmch_n,
12131 pipe_config->dp_m2_n2.link_m,
12132 pipe_config->dp_m2_n2.link_n,
12133 pipe_config->dp_m2_n2.tu);
12134
Daniel Vetter55072d12014-11-20 16:10:28 +010012135 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12136 pipe_config->has_audio,
12137 pipe_config->has_infoframe);
12138
Daniel Vetterc0b03412013-05-28 12:05:54 +020012139 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012140 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012141 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012142 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12143 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012144 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012145 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12146 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012147 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12148 crtc->num_scalers,
12149 pipe_config->scaler_state.scaler_users,
12150 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012151 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12152 pipe_config->gmch_pfit.control,
12153 pipe_config->gmch_pfit.pgm_ratios,
12154 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012155 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012156 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012157 pipe_config->pch_pfit.size,
12158 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012159 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012160 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012161
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012162 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012163 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012164 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012165 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012166 pipe_config->ddi_pll_sel,
12167 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012168 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012169 pipe_config->dpll_hw_state.pll0,
12170 pipe_config->dpll_hw_state.pll1,
12171 pipe_config->dpll_hw_state.pll2,
12172 pipe_config->dpll_hw_state.pll3,
12173 pipe_config->dpll_hw_state.pll6,
12174 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012175 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012176 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012177 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012178 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012179 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12180 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12181 pipe_config->ddi_pll_sel,
12182 pipe_config->dpll_hw_state.ctrl1,
12183 pipe_config->dpll_hw_state.cfgcr1,
12184 pipe_config->dpll_hw_state.cfgcr2);
12185 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012186 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 +010012187 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012188 pipe_config->dpll_hw_state.wrpll,
12189 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012190 } else {
12191 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12192 "fp0: 0x%x, fp1: 0x%x\n",
12193 pipe_config->dpll_hw_state.dpll,
12194 pipe_config->dpll_hw_state.dpll_md,
12195 pipe_config->dpll_hw_state.fp0,
12196 pipe_config->dpll_hw_state.fp1);
12197 }
12198
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012199 DRM_DEBUG_KMS("planes on this crtc\n");
12200 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12201 intel_plane = to_intel_plane(plane);
12202 if (intel_plane->pipe != crtc->pipe)
12203 continue;
12204
12205 state = to_intel_plane_state(plane->state);
12206 fb = state->base.fb;
12207 if (!fb) {
12208 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12209 "disabled, scaler_id = %d\n",
12210 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12211 plane->base.id, intel_plane->pipe,
12212 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12213 drm_plane_index(plane), state->scaler_id);
12214 continue;
12215 }
12216
12217 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12218 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12219 plane->base.id, intel_plane->pipe,
12220 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12221 drm_plane_index(plane));
12222 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12223 fb->base.id, fb->width, fb->height, fb->pixel_format);
12224 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12225 state->scaler_id,
12226 state->src.x1 >> 16, state->src.y1 >> 16,
12227 drm_rect_width(&state->src) >> 16,
12228 drm_rect_height(&state->src) >> 16,
12229 state->dst.x1, state->dst.y1,
12230 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12231 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012232}
12233
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012234static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012235{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012236 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012237 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012238 unsigned int used_ports = 0;
12239
12240 /*
12241 * Walk the connector list instead of the encoder
12242 * list to detect the problem on ddi platforms
12243 * where there's just one encoder per digital port.
12244 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012245 drm_for_each_connector(connector, dev) {
12246 struct drm_connector_state *connector_state;
12247 struct intel_encoder *encoder;
12248
12249 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12250 if (!connector_state)
12251 connector_state = connector->state;
12252
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012253 if (!connector_state->best_encoder)
12254 continue;
12255
12256 encoder = to_intel_encoder(connector_state->best_encoder);
12257
12258 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012259
12260 switch (encoder->type) {
12261 unsigned int port_mask;
12262 case INTEL_OUTPUT_UNKNOWN:
12263 if (WARN_ON(!HAS_DDI(dev)))
12264 break;
12265 case INTEL_OUTPUT_DISPLAYPORT:
12266 case INTEL_OUTPUT_HDMI:
12267 case INTEL_OUTPUT_EDP:
12268 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12269
12270 /* the same port mustn't appear more than once */
12271 if (used_ports & port_mask)
12272 return false;
12273
12274 used_ports |= port_mask;
12275 default:
12276 break;
12277 }
12278 }
12279
12280 return true;
12281}
12282
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012283static void
12284clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12285{
12286 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012287 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012288 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012289 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012290 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012291 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012292
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012293 /* FIXME: before the switch to atomic started, a new pipe_config was
12294 * kzalloc'd. Code that depends on any field being zero should be
12295 * fixed, so that the crtc_state can be safely duplicated. For now,
12296 * only fields that are know to not cause problems are preserved. */
12297
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012298 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012299 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012300 shared_dpll = crtc_state->shared_dpll;
12301 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012302 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012303 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012304
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012305 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012306
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012307 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012308 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012309 crtc_state->shared_dpll = shared_dpll;
12310 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012311 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012312 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012313}
12314
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012315static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012316intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012317 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012318{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012319 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012320 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012321 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012322 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012323 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012324 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012325 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012326
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012327 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012328
Daniel Vettere143a212013-07-04 12:01:15 +020012329 pipe_config->cpu_transcoder =
12330 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012331
Imre Deak2960bc92013-07-30 13:36:32 +030012332 /*
12333 * Sanitize sync polarity flags based on requested ones. If neither
12334 * positive or negative polarity is requested, treat this as meaning
12335 * negative polarity.
12336 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012337 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012338 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012339 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012340
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012341 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012342 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012343 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012344
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012345 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12346 pipe_config);
12347 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012348 goto fail;
12349
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012350 /*
12351 * Determine the real pipe dimensions. Note that stereo modes can
12352 * increase the actual pipe size due to the frame doubling and
12353 * insertion of additional space for blanks between the frame. This
12354 * is stored in the crtc timings. We use the requested mode to do this
12355 * computation to clearly distinguish it from the adjusted mode, which
12356 * can be changed by the connectors in the below retry loop.
12357 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012358 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012359 &pipe_config->pipe_src_w,
12360 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012361
Daniel Vettere29c22c2013-02-21 00:00:16 +010012362encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012363 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012364 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012365 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012366
Daniel Vetter135c81b2013-07-21 21:37:09 +020012367 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012368 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12369 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012370
Daniel Vetter7758a112012-07-08 19:40:39 +020012371 /* Pass our mode to the connectors and the CRTC to give them a chance to
12372 * adjust it according to limitations or connector properties, and also
12373 * a chance to reject the mode entirely.
12374 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012375 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012376 if (connector_state->crtc != crtc)
12377 continue;
12378
12379 encoder = to_intel_encoder(connector_state->best_encoder);
12380
Daniel Vetterefea6e82013-07-21 21:36:59 +020012381 if (!(encoder->compute_config(encoder, pipe_config))) {
12382 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012383 goto fail;
12384 }
12385 }
12386
Daniel Vetterff9a6752013-06-01 17:16:21 +020012387 /* Set default port clock if not overwritten by the encoder. Needs to be
12388 * done afterwards in case the encoder adjusts the mode. */
12389 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012390 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012391 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012392
Daniel Vettera43f6e02013-06-07 23:10:32 +020012393 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012394 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012395 DRM_DEBUG_KMS("CRTC fixup failed\n");
12396 goto fail;
12397 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012398
12399 if (ret == RETRY) {
12400 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12401 ret = -EINVAL;
12402 goto fail;
12403 }
12404
12405 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12406 retry = false;
12407 goto encoder_retry;
12408 }
12409
Daniel Vettere8fa4272015-08-12 11:43:34 +020012410 /* Dithering seems to not pass-through bits correctly when it should, so
12411 * only enable it on 6bpc panels. */
12412 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012413 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012414 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012415
Daniel Vetter7758a112012-07-08 19:40:39 +020012416fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012417 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012418}
12419
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012420static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012421intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012422{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012423 struct drm_crtc *crtc;
12424 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012425 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012426
Ville Syrjälä76688512014-01-10 11:28:06 +020012427 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012428 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012429 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012430
12431 /* Update hwmode for vblank functions */
12432 if (crtc->state->active)
12433 crtc->hwmode = crtc->state->adjusted_mode;
12434 else
12435 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012436
12437 /*
12438 * Update legacy state to satisfy fbc code. This can
12439 * be removed when fbc uses the atomic state.
12440 */
12441 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12442 struct drm_plane_state *plane_state = crtc->primary->state;
12443
12444 crtc->primary->fb = plane_state->fb;
12445 crtc->x = plane_state->src_x >> 16;
12446 crtc->y = plane_state->src_y >> 16;
12447 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012448 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012449}
12450
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012451static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012452{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012453 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012454
12455 if (clock1 == clock2)
12456 return true;
12457
12458 if (!clock1 || !clock2)
12459 return false;
12460
12461 diff = abs(clock1 - clock2);
12462
12463 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12464 return true;
12465
12466 return false;
12467}
12468
Daniel Vetter25c5b262012-07-08 22:08:04 +020012469#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12470 list_for_each_entry((intel_crtc), \
12471 &(dev)->mode_config.crtc_list, \
12472 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012473 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012474
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012475static bool
12476intel_compare_m_n(unsigned int m, unsigned int n,
12477 unsigned int m2, unsigned int n2,
12478 bool exact)
12479{
12480 if (m == m2 && n == n2)
12481 return true;
12482
12483 if (exact || !m || !n || !m2 || !n2)
12484 return false;
12485
12486 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12487
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012488 if (n > n2) {
12489 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012490 m2 <<= 1;
12491 n2 <<= 1;
12492 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012493 } else if (n < n2) {
12494 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012495 m <<= 1;
12496 n <<= 1;
12497 }
12498 }
12499
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012500 if (n != n2)
12501 return false;
12502
12503 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012504}
12505
12506static bool
12507intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12508 struct intel_link_m_n *m2_n2,
12509 bool adjust)
12510{
12511 if (m_n->tu == m2_n2->tu &&
12512 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12513 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12514 intel_compare_m_n(m_n->link_m, m_n->link_n,
12515 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12516 if (adjust)
12517 *m2_n2 = *m_n;
12518
12519 return true;
12520 }
12521
12522 return false;
12523}
12524
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012525static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012526intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012527 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012528 struct intel_crtc_state *pipe_config,
12529 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012530{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012531 bool ret = true;
12532
12533#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12534 do { \
12535 if (!adjust) \
12536 DRM_ERROR(fmt, ##__VA_ARGS__); \
12537 else \
12538 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12539 } while (0)
12540
Daniel Vetter66e985c2013-06-05 13:34:20 +020012541#define PIPE_CONF_CHECK_X(name) \
12542 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012543 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012544 "(expected 0x%08x, found 0x%08x)\n", \
12545 current_config->name, \
12546 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012547 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012548 }
12549
Daniel Vetter08a24032013-04-19 11:25:34 +020012550#define PIPE_CONF_CHECK_I(name) \
12551 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012552 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012553 "(expected %i, found %i)\n", \
12554 current_config->name, \
12555 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012556 ret = false; \
12557 }
12558
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012559#define PIPE_CONF_CHECK_P(name) \
12560 if (current_config->name != pipe_config->name) { \
12561 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12562 "(expected %p, found %p)\n", \
12563 current_config->name, \
12564 pipe_config->name); \
12565 ret = false; \
12566 }
12567
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012568#define PIPE_CONF_CHECK_M_N(name) \
12569 if (!intel_compare_link_m_n(&current_config->name, \
12570 &pipe_config->name,\
12571 adjust)) { \
12572 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12573 "(expected tu %i gmch %i/%i link %i/%i, " \
12574 "found tu %i, gmch %i/%i link %i/%i)\n", \
12575 current_config->name.tu, \
12576 current_config->name.gmch_m, \
12577 current_config->name.gmch_n, \
12578 current_config->name.link_m, \
12579 current_config->name.link_n, \
12580 pipe_config->name.tu, \
12581 pipe_config->name.gmch_m, \
12582 pipe_config->name.gmch_n, \
12583 pipe_config->name.link_m, \
12584 pipe_config->name.link_n); \
12585 ret = false; \
12586 }
12587
Daniel Vetter55c561a2016-03-30 11:34:36 +020012588/* This is required for BDW+ where there is only one set of registers for
12589 * switching between high and low RR.
12590 * This macro can be used whenever a comparison has to be made between one
12591 * hw state and multiple sw state variables.
12592 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012593#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12594 if (!intel_compare_link_m_n(&current_config->name, \
12595 &pipe_config->name, adjust) && \
12596 !intel_compare_link_m_n(&current_config->alt_name, \
12597 &pipe_config->name, adjust)) { \
12598 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12599 "(expected tu %i gmch %i/%i link %i/%i, " \
12600 "or tu %i gmch %i/%i link %i/%i, " \
12601 "found tu %i, gmch %i/%i link %i/%i)\n", \
12602 current_config->name.tu, \
12603 current_config->name.gmch_m, \
12604 current_config->name.gmch_n, \
12605 current_config->name.link_m, \
12606 current_config->name.link_n, \
12607 current_config->alt_name.tu, \
12608 current_config->alt_name.gmch_m, \
12609 current_config->alt_name.gmch_n, \
12610 current_config->alt_name.link_m, \
12611 current_config->alt_name.link_n, \
12612 pipe_config->name.tu, \
12613 pipe_config->name.gmch_m, \
12614 pipe_config->name.gmch_n, \
12615 pipe_config->name.link_m, \
12616 pipe_config->name.link_n); \
12617 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012618 }
12619
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012620#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12621 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012622 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012623 "(expected %i, found %i)\n", \
12624 current_config->name & (mask), \
12625 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012626 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012627 }
12628
Ville Syrjälä5e550652013-09-06 23:29:07 +030012629#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12630 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012631 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012632 "(expected %i, found %i)\n", \
12633 current_config->name, \
12634 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012635 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012636 }
12637
Daniel Vetterbb760062013-06-06 14:55:52 +020012638#define PIPE_CONF_QUIRK(quirk) \
12639 ((current_config->quirks | pipe_config->quirks) & (quirk))
12640
Daniel Vettereccb1402013-05-22 00:50:22 +020012641 PIPE_CONF_CHECK_I(cpu_transcoder);
12642
Daniel Vetter08a24032013-04-19 11:25:34 +020012643 PIPE_CONF_CHECK_I(has_pch_encoder);
12644 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012645 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012646
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012647 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012648 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012649
12650 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012651 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012652
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012653 if (current_config->has_drrs)
12654 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12655 } else
12656 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012657
Jani Nikulaa65347b2015-11-27 12:21:46 +020012658 PIPE_CONF_CHECK_I(has_dsi_encoder);
12659
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012660 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12661 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12662 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12663 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12664 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12665 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012666
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012667 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12668 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12669 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12670 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12671 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12672 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012673
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012674 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012675 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012676 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012677 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012678 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012679 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012680
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012681 PIPE_CONF_CHECK_I(has_audio);
12682
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012683 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012684 DRM_MODE_FLAG_INTERLACE);
12685
Daniel Vetterbb760062013-06-06 14:55:52 +020012686 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012687 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012688 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012689 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012690 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012691 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012692 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012693 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012694 DRM_MODE_FLAG_NVSYNC);
12695 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012696
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012697 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012698 /* pfit ratios are autocomputed by the hw on gen4+ */
12699 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012700 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012701 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012702
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012703 if (!adjust) {
12704 PIPE_CONF_CHECK_I(pipe_src_w);
12705 PIPE_CONF_CHECK_I(pipe_src_h);
12706
12707 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12708 if (current_config->pch_pfit.enabled) {
12709 PIPE_CONF_CHECK_X(pch_pfit.pos);
12710 PIPE_CONF_CHECK_X(pch_pfit.size);
12711 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012712
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012713 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12714 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012715
Jesse Barnese59150d2014-01-07 13:30:45 -080012716 /* BDW+ don't expose a synchronous way to read the state */
12717 if (IS_HASWELL(dev))
12718 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012719
Ville Syrjälä282740f2013-09-04 18:30:03 +030012720 PIPE_CONF_CHECK_I(double_wide);
12721
Daniel Vetter26804af2014-06-25 22:01:55 +030012722 PIPE_CONF_CHECK_X(ddi_pll_sel);
12723
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012724 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012725 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012726 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012727 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12728 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012729 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012730 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012731 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12732 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12733 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012734
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012735 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12736 PIPE_CONF_CHECK_X(dsi_pll.div);
12737
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012738 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12739 PIPE_CONF_CHECK_I(pipe_bpp);
12740
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012741 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012742 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012743
Daniel Vetter66e985c2013-06-05 13:34:20 +020012744#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012745#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012746#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012747#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012748#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012749#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012750#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012751
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012752 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012753}
12754
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012755static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12756 const struct intel_crtc_state *pipe_config)
12757{
12758 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012759 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012760 &pipe_config->fdi_m_n);
12761 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12762
12763 /*
12764 * FDI already provided one idea for the dotclock.
12765 * Yell if the encoder disagrees.
12766 */
12767 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12768 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12769 fdi_dotclock, dotclock);
12770 }
12771}
12772
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012773static void verify_wm_state(struct drm_crtc *crtc,
12774 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012775{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012776 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012777 struct drm_i915_private *dev_priv = dev->dev_private;
12778 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012779 struct skl_ddb_entry *hw_entry, *sw_entry;
12780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12781 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012782 int plane;
12783
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012784 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012785 return;
12786
12787 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12788 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12789
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012790 /* planes */
12791 for_each_plane(dev_priv, pipe, plane) {
12792 hw_entry = &hw_ddb.plane[pipe][plane];
12793 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012794
12795 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12796 continue;
12797
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012798 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12799 "(expected (%u,%u), found (%u,%u))\n",
12800 pipe_name(pipe), plane + 1,
12801 sw_entry->start, sw_entry->end,
12802 hw_entry->start, hw_entry->end);
12803 }
12804
12805 /* cursor */
12806 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12807 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12808
12809 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012810 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12811 "(expected (%u,%u), found (%u,%u))\n",
12812 pipe_name(pipe),
12813 sw_entry->start, sw_entry->end,
12814 hw_entry->start, hw_entry->end);
12815 }
12816}
12817
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012818static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012819verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012820{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012821 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012822
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012823 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012824 struct drm_encoder *encoder = connector->encoder;
12825 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012826
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012827 if (state->crtc != crtc)
12828 continue;
12829
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012830 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012831
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012832 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012833 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012834 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012835}
12836
12837static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012838verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012839{
12840 struct intel_encoder *encoder;
12841 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012842
Damien Lespiaub2784e12014-08-05 11:29:37 +010012843 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012844 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012845 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012846
12847 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12848 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012849 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012850
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012851 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012852 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012853 continue;
12854 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012855
12856 I915_STATE_WARN(connector->base.state->crtc !=
12857 encoder->base.crtc,
12858 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012859 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012860
Rob Clarke2c719b2014-12-15 13:56:32 -050012861 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012862 "encoder's enabled state mismatch "
12863 "(expected %i, found %i)\n",
12864 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012865
12866 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012867 bool active;
12868
12869 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012870 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012871 "encoder detached but still enabled on pipe %c.\n",
12872 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012873 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012874 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012875}
12876
12877static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012878verify_crtc_state(struct drm_crtc *crtc,
12879 struct drm_crtc_state *old_crtc_state,
12880 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012881{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012882 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012883 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012884 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12886 struct intel_crtc_state *pipe_config, *sw_config;
12887 struct drm_atomic_state *old_state;
12888 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012889
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012890 old_state = old_crtc_state->state;
12891 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12892 pipe_config = to_intel_crtc_state(old_crtc_state);
12893 memset(pipe_config, 0, sizeof(*pipe_config));
12894 pipe_config->base.crtc = crtc;
12895 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012896
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012897 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012898
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012899 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012900
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012901 /* hw state is inconsistent with the pipe quirk */
12902 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12903 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12904 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012905
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012906 I915_STATE_WARN(new_crtc_state->active != active,
12907 "crtc active state doesn't match with hw state "
12908 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012909
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012910 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12911 "transitional active state does not match atomic hw state "
12912 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012913
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012914 for_each_encoder_on_crtc(dev, crtc, encoder) {
12915 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012916
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012917 active = encoder->get_hw_state(encoder, &pipe);
12918 I915_STATE_WARN(active != new_crtc_state->active,
12919 "[ENCODER:%i] active %i with crtc active %i\n",
12920 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012921
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012922 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12923 "Encoder connected to wrong pipe %c\n",
12924 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012925
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012926 if (active)
12927 encoder->get_config(encoder, pipe_config);
12928 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012929
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012930 if (!new_crtc_state->active)
12931 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012932
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012933 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012934
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012935 sw_config = to_intel_crtc_state(crtc->state);
12936 if (!intel_pipe_config_compare(dev, sw_config,
12937 pipe_config, false)) {
12938 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12939 intel_dump_pipe_config(intel_crtc, pipe_config,
12940 "[hw state]");
12941 intel_dump_pipe_config(intel_crtc, sw_config,
12942 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012943 }
12944}
12945
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012946static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012947verify_single_dpll_state(struct drm_i915_private *dev_priv,
12948 struct intel_shared_dpll *pll,
12949 struct drm_crtc *crtc,
12950 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012951{
12952 struct intel_dpll_hw_state dpll_hw_state;
12953 unsigned crtc_mask;
12954 bool active;
12955
12956 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12957
12958 DRM_DEBUG_KMS("%s\n", pll->name);
12959
12960 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12961
12962 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12963 I915_STATE_WARN(!pll->on && pll->active_mask,
12964 "pll in active use but not on in sw tracking\n");
12965 I915_STATE_WARN(pll->on && !pll->active_mask,
12966 "pll is on but not used by any active crtc\n");
12967 I915_STATE_WARN(pll->on != active,
12968 "pll on state mismatch (expected %i, found %i)\n",
12969 pll->on, active);
12970 }
12971
12972 if (!crtc) {
12973 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12974 "more active pll users than references: %x vs %x\n",
12975 pll->active_mask, pll->config.crtc_mask);
12976
12977 return;
12978 }
12979
12980 crtc_mask = 1 << drm_crtc_index(crtc);
12981
12982 if (new_state->active)
12983 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12984 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12985 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12986 else
12987 I915_STATE_WARN(pll->active_mask & crtc_mask,
12988 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12989 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12990
12991 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
12992 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
12993 crtc_mask, pll->config.crtc_mask);
12994
12995 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
12996 &dpll_hw_state,
12997 sizeof(dpll_hw_state)),
12998 "pll hw state mismatch\n");
12999}
13000
13001static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013002verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13003 struct drm_crtc_state *old_crtc_state,
13004 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013005{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013006 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013007 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13008 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13009
13010 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013011 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013012
13013 if (old_state->shared_dpll &&
13014 old_state->shared_dpll != new_state->shared_dpll) {
13015 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13016 struct intel_shared_dpll *pll = old_state->shared_dpll;
13017
13018 I915_STATE_WARN(pll->active_mask & crtc_mask,
13019 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13020 pipe_name(drm_crtc_index(crtc)));
13021 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13022 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13023 pipe_name(drm_crtc_index(crtc)));
13024 }
13025}
13026
13027static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013028intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013029 struct drm_crtc_state *old_state,
13030 struct drm_crtc_state *new_state)
13031{
13032 if (!needs_modeset(new_state) &&
13033 !to_intel_crtc_state(new_state)->update_pipe)
13034 return;
13035
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013036 verify_wm_state(crtc, new_state);
13037 verify_connector_state(crtc->dev, crtc);
13038 verify_crtc_state(crtc, old_state, new_state);
13039 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013040}
13041
13042static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013043verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013044{
13045 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013046 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013047
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013048 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013049 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013050}
Daniel Vetter53589012013-06-05 13:34:16 +020013051
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013052static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013053intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013054{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013055 verify_encoder_state(dev);
13056 verify_connector_state(dev, NULL);
13057 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013058}
13059
Ville Syrjälä80715b22014-05-15 20:23:23 +030013060static void update_scanline_offset(struct intel_crtc *crtc)
13061{
13062 struct drm_device *dev = crtc->base.dev;
13063
13064 /*
13065 * The scanline counter increments at the leading edge of hsync.
13066 *
13067 * On most platforms it starts counting from vtotal-1 on the
13068 * first active line. That means the scanline counter value is
13069 * always one less than what we would expect. Ie. just after
13070 * start of vblank, which also occurs at start of hsync (on the
13071 * last active line), the scanline counter will read vblank_start-1.
13072 *
13073 * On gen2 the scanline counter starts counting from 1 instead
13074 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13075 * to keep the value positive), instead of adding one.
13076 *
13077 * On HSW+ the behaviour of the scanline counter depends on the output
13078 * type. For DP ports it behaves like most other platforms, but on HDMI
13079 * there's an extra 1 line difference. So we need to add two instead of
13080 * one to the value.
13081 */
13082 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013083 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013084 int vtotal;
13085
Ville Syrjälä124abe02015-09-08 13:40:45 +030013086 vtotal = adjusted_mode->crtc_vtotal;
13087 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013088 vtotal /= 2;
13089
13090 crtc->scanline_offset = vtotal - 1;
13091 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013092 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013093 crtc->scanline_offset = 2;
13094 } else
13095 crtc->scanline_offset = 1;
13096}
13097
Maarten Lankhorstad421372015-06-15 12:33:42 +020013098static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013099{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013100 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013101 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013102 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013103 struct drm_crtc *crtc;
13104 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013105 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013106
13107 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013108 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013109
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013110 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013112 struct intel_shared_dpll *old_dpll =
13113 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013114
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013115 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013116 continue;
13117
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013118 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013119
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013120 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013121 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013122
Maarten Lankhorstad421372015-06-15 12:33:42 +020013123 if (!shared_dpll)
13124 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13125
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013126 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013127 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013128}
13129
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013130/*
13131 * This implements the workaround described in the "notes" section of the mode
13132 * set sequence documentation. When going from no pipes or single pipe to
13133 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13134 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13135 */
13136static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13137{
13138 struct drm_crtc_state *crtc_state;
13139 struct intel_crtc *intel_crtc;
13140 struct drm_crtc *crtc;
13141 struct intel_crtc_state *first_crtc_state = NULL;
13142 struct intel_crtc_state *other_crtc_state = NULL;
13143 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13144 int i;
13145
13146 /* look at all crtc's that are going to be enabled in during modeset */
13147 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13148 intel_crtc = to_intel_crtc(crtc);
13149
13150 if (!crtc_state->active || !needs_modeset(crtc_state))
13151 continue;
13152
13153 if (first_crtc_state) {
13154 other_crtc_state = to_intel_crtc_state(crtc_state);
13155 break;
13156 } else {
13157 first_crtc_state = to_intel_crtc_state(crtc_state);
13158 first_pipe = intel_crtc->pipe;
13159 }
13160 }
13161
13162 /* No workaround needed? */
13163 if (!first_crtc_state)
13164 return 0;
13165
13166 /* w/a possibly needed, check how many crtc's are already enabled. */
13167 for_each_intel_crtc(state->dev, intel_crtc) {
13168 struct intel_crtc_state *pipe_config;
13169
13170 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13171 if (IS_ERR(pipe_config))
13172 return PTR_ERR(pipe_config);
13173
13174 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13175
13176 if (!pipe_config->base.active ||
13177 needs_modeset(&pipe_config->base))
13178 continue;
13179
13180 /* 2 or more enabled crtcs means no need for w/a */
13181 if (enabled_pipe != INVALID_PIPE)
13182 return 0;
13183
13184 enabled_pipe = intel_crtc->pipe;
13185 }
13186
13187 if (enabled_pipe != INVALID_PIPE)
13188 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13189 else if (other_crtc_state)
13190 other_crtc_state->hsw_workaround_pipe = first_pipe;
13191
13192 return 0;
13193}
13194
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013195static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13196{
13197 struct drm_crtc *crtc;
13198 struct drm_crtc_state *crtc_state;
13199 int ret = 0;
13200
13201 /* add all active pipes to the state */
13202 for_each_crtc(state->dev, crtc) {
13203 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13204 if (IS_ERR(crtc_state))
13205 return PTR_ERR(crtc_state);
13206
13207 if (!crtc_state->active || needs_modeset(crtc_state))
13208 continue;
13209
13210 crtc_state->mode_changed = true;
13211
13212 ret = drm_atomic_add_affected_connectors(state, crtc);
13213 if (ret)
13214 break;
13215
13216 ret = drm_atomic_add_affected_planes(state, crtc);
13217 if (ret)
13218 break;
13219 }
13220
13221 return ret;
13222}
13223
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013224static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013225{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013226 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13227 struct drm_i915_private *dev_priv = state->dev->dev_private;
13228 struct drm_crtc *crtc;
13229 struct drm_crtc_state *crtc_state;
13230 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013231
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013232 if (!check_digital_port_conflicts(state)) {
13233 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13234 return -EINVAL;
13235 }
13236
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013237 intel_state->modeset = true;
13238 intel_state->active_crtcs = dev_priv->active_crtcs;
13239
13240 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13241 if (crtc_state->active)
13242 intel_state->active_crtcs |= 1 << i;
13243 else
13244 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013245
13246 if (crtc_state->active != crtc->state->active)
13247 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013248 }
13249
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013250 /*
13251 * See if the config requires any additional preparation, e.g.
13252 * to adjust global state with pipes off. We need to do this
13253 * here so we can get the modeset_pipe updated config for the new
13254 * mode set on this crtc. For other crtcs we need to use the
13255 * adjusted_mode bits in the crtc directly.
13256 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013257 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013258 ret = dev_priv->display.modeset_calc_cdclk(state);
13259
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013260 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013261 ret = intel_modeset_all_pipes(state);
13262
13263 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013264 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013265
13266 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13267 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013268 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013269 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013270
Maarten Lankhorstad421372015-06-15 12:33:42 +020013271 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013272
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013273 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013274 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013275
Maarten Lankhorstad421372015-06-15 12:33:42 +020013276 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013277}
13278
Matt Roperaa363132015-09-24 15:53:18 -070013279/*
13280 * Handle calculation of various watermark data at the end of the atomic check
13281 * phase. The code here should be run after the per-crtc and per-plane 'check'
13282 * handlers to ensure that all derived state has been updated.
13283 */
Matt Roper55994c22016-05-12 07:06:08 -070013284static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013285{
13286 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013287 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013288
13289 /* Is there platform-specific watermark information to calculate? */
13290 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013291 return dev_priv->display.compute_global_watermarks(state);
13292
13293 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013294}
13295
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013296/**
13297 * intel_atomic_check - validate state object
13298 * @dev: drm device
13299 * @state: state to validate
13300 */
13301static int intel_atomic_check(struct drm_device *dev,
13302 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013303{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013304 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013305 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013306 struct drm_crtc *crtc;
13307 struct drm_crtc_state *crtc_state;
13308 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013309 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013310
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013311 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013312 if (ret)
13313 return ret;
13314
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013315 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013316 struct intel_crtc_state *pipe_config =
13317 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013318
13319 /* Catch I915_MODE_FLAG_INHERITED */
13320 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13321 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013322
Daniel Vetter26495482015-07-15 14:15:52 +020013323 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013324 continue;
13325
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013326 if (!crtc_state->enable) {
13327 any_ms = true;
13328 continue;
13329 }
13330
Daniel Vetter26495482015-07-15 14:15:52 +020013331 /* FIXME: For only active_changed we shouldn't need to do any
13332 * state recomputation at all. */
13333
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013334 ret = drm_atomic_add_affected_connectors(state, crtc);
13335 if (ret)
13336 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013337
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013338 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013339 if (ret) {
13340 intel_dump_pipe_config(to_intel_crtc(crtc),
13341 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013342 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013343 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013344
Jani Nikula73831232015-11-19 10:26:30 +020013345 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013346 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013347 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013348 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013349 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013350 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013351 }
13352
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013353 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013354 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013355
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013356 ret = drm_atomic_add_affected_planes(state, crtc);
13357 if (ret)
13358 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013359
Daniel Vetter26495482015-07-15 14:15:52 +020013360 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13361 needs_modeset(crtc_state) ?
13362 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013363 }
13364
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013365 if (any_ms) {
13366 ret = intel_modeset_checks(state);
13367
13368 if (ret)
13369 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013370 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013371 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013372
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013373 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013374 if (ret)
13375 return ret;
13376
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013377 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013378 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013379}
13380
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013381static int intel_atomic_prepare_commit(struct drm_device *dev,
13382 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013383 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013384{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013385 struct drm_i915_private *dev_priv = dev->dev_private;
13386 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013387 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013388 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013389 struct drm_crtc *crtc;
13390 int i, ret;
13391
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013392 if (nonblock) {
13393 DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013394 return -EINVAL;
13395 }
13396
13397 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Chris Wilsonacf4e842016-04-17 20:42:46 +010013398 if (state->legacy_cursor_update)
13399 continue;
13400
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013401 ret = intel_crtc_wait_for_pending_flips(crtc);
13402 if (ret)
13403 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013404
13405 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13406 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013407 }
13408
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013409 ret = mutex_lock_interruptible(&dev->struct_mutex);
13410 if (ret)
13411 return ret;
13412
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013413 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013414 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013415
Dave Airlie21daaee2016-05-05 09:56:30 +100013416 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013417 for_each_plane_in_state(state, plane, plane_state, i) {
13418 struct intel_plane_state *intel_plane_state =
13419 to_intel_plane_state(plane_state);
13420
13421 if (!intel_plane_state->wait_req)
13422 continue;
13423
13424 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013425 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013426 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013427 /* Any hang should be swallowed by the wait */
13428 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013429 mutex_lock(&dev->struct_mutex);
13430 drm_atomic_helper_cleanup_planes(dev, state);
13431 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013432 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013433 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013434 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013435 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013436
13437 return ret;
13438}
13439
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013440u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13441{
13442 struct drm_device *dev = crtc->base.dev;
13443
13444 if (!dev->max_vblank_count)
13445 return drm_accurate_vblank_count(&crtc->base);
13446
13447 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13448}
13449
Maarten Lankhorste8861672016-02-24 11:24:26 +010013450static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13451 struct drm_i915_private *dev_priv,
13452 unsigned crtc_mask)
13453{
13454 unsigned last_vblank_count[I915_MAX_PIPES];
13455 enum pipe pipe;
13456 int ret;
13457
13458 if (!crtc_mask)
13459 return;
13460
13461 for_each_pipe(dev_priv, pipe) {
13462 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13463
13464 if (!((1 << pipe) & crtc_mask))
13465 continue;
13466
13467 ret = drm_crtc_vblank_get(crtc);
13468 if (WARN_ON(ret != 0)) {
13469 crtc_mask &= ~(1 << pipe);
13470 continue;
13471 }
13472
13473 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13474 }
13475
13476 for_each_pipe(dev_priv, pipe) {
13477 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13478 long lret;
13479
13480 if (!((1 << pipe) & crtc_mask))
13481 continue;
13482
13483 lret = wait_event_timeout(dev->vblank[pipe].queue,
13484 last_vblank_count[pipe] !=
13485 drm_crtc_vblank_count(crtc),
13486 msecs_to_jiffies(50));
13487
Ville Syrjälä8a8dae22016-04-18 14:29:32 +030013488 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
Maarten Lankhorste8861672016-02-24 11:24:26 +010013489
13490 drm_crtc_vblank_put(crtc);
13491 }
13492}
13493
13494static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13495{
13496 /* fb updated, need to unpin old fb */
13497 if (crtc_state->fb_changed)
13498 return true;
13499
13500 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013501 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013502 return true;
13503
13504 /*
13505 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013506 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013507 * but added for clarity.
13508 */
13509 if (crtc_state->disable_cxsr)
13510 return true;
13511
13512 return false;
13513}
13514
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013515/**
13516 * intel_atomic_commit - commit validated state object
13517 * @dev: DRM device
13518 * @state: the top-level driver state object
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013519 * @nonblock: nonblocking commit
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013520 *
13521 * This function commits a top-level state object that has been validated
13522 * with drm_atomic_helper_check().
13523 *
13524 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13525 * we can only handle plane-related operations and do not yet support
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013526 * nonblocking commit.
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013527 *
13528 * RETURNS
13529 * Zero for success or -errno.
13530 */
13531static int intel_atomic_commit(struct drm_device *dev,
13532 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013533 bool nonblock)
Daniel Vettera6778b32012-07-02 09:56:42 +020013534{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013535 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013536 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013537 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013538 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013539 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013540 int ret = 0, i;
13541 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013542 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013543 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013544
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013545 ret = intel_atomic_prepare_commit(dev, state, nonblock);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013546 if (ret) {
13547 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013548 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013549 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013550
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013551 drm_atomic_helper_swap_state(dev, state);
Matt Roper279e99d2016-05-12 07:06:02 -070013552 dev_priv->wm.distrust_bios_wm = false;
Matt Roper734fa012016-05-12 15:11:40 -070013553 dev_priv->wm.skl_results = intel_state->wm_results;
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013554 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013555
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013556 if (intel_state->modeset) {
13557 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13558 sizeof(intel_state->min_pixclk));
13559 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013560 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013561
13562 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013563 }
13564
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013565 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013566 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13567
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013568 if (needs_modeset(crtc->state) ||
13569 to_intel_crtc_state(crtc->state)->update_pipe) {
13570 hw_check = true;
13571
13572 put_domains[to_intel_crtc(crtc)->pipe] =
13573 modeset_get_crtc_power_domains(crtc,
13574 to_intel_crtc_state(crtc->state));
13575 }
13576
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013577 if (!needs_modeset(crtc->state))
13578 continue;
13579
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013580 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013581
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013582 if (old_crtc_state->active) {
13583 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013584 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013585 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013586 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013587 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013588
13589 /*
13590 * Underruns don't always raise
13591 * interrupts, so check manually.
13592 */
13593 intel_check_cpu_fifo_underruns(dev_priv);
13594 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013595
13596 if (!crtc->state->active)
13597 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013598 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013599 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013600
Daniel Vetterea9d7582012-07-10 10:42:52 +020013601 /* Only after disabling all output pipelines that will be changed can we
13602 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013603 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013604
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013605 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013606 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013607
13608 if (dev_priv->display.modeset_commit_cdclk &&
13609 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13610 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013611
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013612 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013613 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013614
Daniel Vettera6778b32012-07-02 09:56:42 +020013615 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013616 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013617 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13618 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013619 struct intel_crtc_state *pipe_config =
13620 to_intel_crtc_state(crtc->state);
13621 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013622
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013623 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013624 update_scanline_offset(to_intel_crtc(crtc));
13625 dev_priv->display.crtc_enable(crtc);
13626 }
13627
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013628 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013629 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013630
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013631 if (crtc->state->active &&
13632 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013633 intel_fbc_enable(intel_crtc);
13634
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013635 if (crtc->state->active &&
13636 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013637 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013638
Maarten Lankhorste8861672016-02-24 11:24:26 +010013639 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13640 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013641 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013642
Daniel Vettera6778b32012-07-02 09:56:42 +020013643 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013644
Maarten Lankhorste8861672016-02-24 11:24:26 +010013645 if (!state->legacy_cursor_update)
13646 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013647
Matt Ropered4a6a72016-02-23 17:20:13 -080013648 /*
13649 * Now that the vblank has passed, we can go ahead and program the
13650 * optimal watermarks on platforms that need two-step watermark
13651 * programming.
13652 *
13653 * TODO: Move this (and other cleanup) to an async worker eventually.
13654 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013655 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013656 intel_cstate = to_intel_crtc_state(crtc->state);
13657
13658 if (dev_priv->display.optimize_watermarks)
13659 dev_priv->display.optimize_watermarks(intel_cstate);
13660 }
13661
Matt Roper177246a2016-03-04 15:59:39 -080013662 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13663 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13664
13665 if (put_domains[i])
13666 modeset_put_power_domains(dev_priv, put_domains[i]);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013667
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013668 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
Matt Roper177246a2016-03-04 15:59:39 -080013669 }
13670
13671 if (intel_state->modeset)
13672 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13673
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013674 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013675 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013676 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013677
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013678 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013679
Mika Kuoppala75714942015-12-16 09:26:48 +020013680 /* As one of the primary mmio accessors, KMS has a high likelihood
13681 * of triggering bugs in unclaimed access. After we finish
13682 * modesetting, see if an error has been flagged, and if so
13683 * enable debugging for the next modeset - and hope we catch
13684 * the culprit.
13685 *
13686 * XXX note that we assume display power is on at this point.
13687 * This might hold true now but we need to add pm helper to check
13688 * unclaimed only when the hardware is on, as atomic commits
13689 * can happen also when the device is completely off.
13690 */
13691 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13692
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013693 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013694}
13695
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013696void intel_crtc_restore_mode(struct drm_crtc *crtc)
13697{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013698 struct drm_device *dev = crtc->dev;
13699 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013700 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013701 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013702
13703 state = drm_atomic_state_alloc(dev);
13704 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013705 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013706 crtc->base.id);
13707 return;
13708 }
13709
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013710 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013711
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013712retry:
13713 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13714 ret = PTR_ERR_OR_ZERO(crtc_state);
13715 if (!ret) {
13716 if (!crtc_state->active)
13717 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013718
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013719 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013720 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013721 }
13722
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013723 if (ret == -EDEADLK) {
13724 drm_atomic_state_clear(state);
13725 drm_modeset_backoff(state->acquire_ctx);
13726 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013727 }
13728
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013729 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013730out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013731 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013732}
13733
Daniel Vetter25c5b262012-07-08 22:08:04 +020013734#undef for_each_intel_crtc_masked
13735
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013736static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013737 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013738 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013739 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013740 .destroy = intel_crtc_destroy,
13741 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013742 .atomic_duplicate_state = intel_crtc_duplicate_state,
13743 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013744};
13745
Matt Roper6beb8c232014-12-01 15:40:14 -080013746/**
13747 * intel_prepare_plane_fb - Prepare fb for usage on plane
13748 * @plane: drm plane to prepare for
13749 * @fb: framebuffer to prepare for presentation
13750 *
13751 * Prepares a framebuffer for usage on a display plane. Generally this
13752 * involves pinning the underlying object and updating the frontbuffer tracking
13753 * bits. Some older platforms need special physical address handling for
13754 * cursor planes.
13755 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013756 * Must be called with struct_mutex held.
13757 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013758 * Returns 0 on success, negative error code on failure.
13759 */
13760int
13761intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013762 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013763{
13764 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013765 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013766 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013767 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013768 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013769 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013770
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013771 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013772 return 0;
13773
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013774 if (old_obj) {
13775 struct drm_crtc_state *crtc_state =
13776 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13777
13778 /* Big Hammer, we also need to ensure that any pending
13779 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13780 * current scanout is retired before unpinning the old
13781 * framebuffer. Note that we rely on userspace rendering
13782 * into the buffer attached to the pipe they are waiting
13783 * on. If not, userspace generates a GPU hang with IPEHR
13784 * point to the MI_WAIT_FOR_EVENT.
13785 *
13786 * This should only fail upon a hung GPU, in which case we
13787 * can safely continue.
13788 */
13789 if (needs_modeset(crtc_state))
13790 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013791 if (ret) {
13792 /* GPU hangs should have been swallowed by the wait */
13793 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013794 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013795 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013796 }
13797
Alex Goins3c28ff22015-11-25 18:43:39 -080013798 /* For framebuffer backed by dmabuf, wait for fence */
13799 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013800 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013801
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013802 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13803 false, true,
13804 MAX_SCHEDULE_TIMEOUT);
13805 if (lret == -ERESTARTSYS)
13806 return lret;
13807
13808 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013809 }
13810
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013811 if (!obj) {
13812 ret = 0;
13813 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013814 INTEL_INFO(dev)->cursor_needs_physical) {
13815 int align = IS_I830(dev) ? 16 * 1024 : 256;
13816 ret = i915_gem_object_attach_phys(obj, align);
13817 if (ret)
13818 DRM_DEBUG_KMS("failed to attach phys object\n");
13819 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013820 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013821 }
13822
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013823 if (ret == 0) {
13824 if (obj) {
13825 struct intel_plane_state *plane_state =
13826 to_intel_plane_state(new_state);
13827
13828 i915_gem_request_assign(&plane_state->wait_req,
13829 obj->last_write_req);
13830 }
13831
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013832 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013833 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013834
Matt Roper6beb8c232014-12-01 15:40:14 -080013835 return ret;
13836}
13837
Matt Roper38f3ce32014-12-02 07:45:25 -080013838/**
13839 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13840 * @plane: drm plane to clean up for
13841 * @fb: old framebuffer that was on plane
13842 *
13843 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013844 *
13845 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013846 */
13847void
13848intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013849 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013850{
13851 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013852 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013853 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013854 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13855 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013856
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013857 old_intel_state = to_intel_plane_state(old_state);
13858
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013859 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013860 return;
13861
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013862 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13863 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013864 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013865
13866 /* prepare_fb aborted? */
13867 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13868 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13869 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013870
13871 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013872}
13873
Chandra Konduru6156a452015-04-27 13:48:39 -070013874int
13875skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13876{
13877 int max_scale;
13878 struct drm_device *dev;
13879 struct drm_i915_private *dev_priv;
13880 int crtc_clock, cdclk;
13881
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013882 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013883 return DRM_PLANE_HELPER_NO_SCALING;
13884
13885 dev = intel_crtc->base.dev;
13886 dev_priv = dev->dev_private;
13887 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013888 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013889
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013890 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013891 return DRM_PLANE_HELPER_NO_SCALING;
13892
13893 /*
13894 * skl max scale is lower of:
13895 * close to 3 but not 3, -1 is for that purpose
13896 * or
13897 * cdclk/crtc_clock
13898 */
13899 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13900
13901 return max_scale;
13902}
13903
Matt Roper465c1202014-05-29 08:06:54 -070013904static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013905intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013906 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013907 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013908{
Matt Roper2b875c22014-12-01 15:40:13 -080013909 struct drm_crtc *crtc = state->base.crtc;
13910 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013911 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013912 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13913 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013914
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013915 if (INTEL_INFO(plane->dev)->gen >= 9) {
13916 /* use scaler when colorkey is not required */
13917 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13918 min_scale = 1;
13919 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13920 }
Sonika Jindald8106362015-04-10 14:37:28 +053013921 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013922 }
Sonika Jindald8106362015-04-10 14:37:28 +053013923
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013924 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13925 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013926 min_scale, max_scale,
13927 can_position, true,
13928 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013929}
13930
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013931static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13932 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013933{
13934 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013936 struct intel_crtc_state *old_intel_state =
13937 to_intel_crtc_state(old_crtc_state);
13938 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013939
Matt Roperc34c9ee2014-12-23 10:41:50 -080013940 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013941 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013942
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013943 if (modeset)
13944 return;
13945
Maarten Lankhorst20a34e72016-03-30 17:16:36 +020013946 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13947 intel_color_set_csc(crtc->state);
13948 intel_color_load_luts(crtc->state);
13949 }
13950
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013951 if (to_intel_crtc_state(crtc->state)->update_pipe)
13952 intel_update_pipe_config(intel_crtc, old_intel_state);
13953 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013954 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013955}
13956
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013957static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13958 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013959{
Matt Roper32b7eee2014-12-24 07:59:06 -080013960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013961
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020013962 intel_pipe_update_end(intel_crtc, NULL);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013963}
13964
Matt Ropercf4c7c12014-12-04 10:27:42 -080013965/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013966 * intel_plane_destroy - destroy a plane
13967 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013968 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013969 * Common destruction function for all types of planes (primary, cursor,
13970 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013971 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013972void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013973{
13974 struct intel_plane *intel_plane = to_intel_plane(plane);
13975 drm_plane_cleanup(plane);
13976 kfree(intel_plane);
13977}
13978
Matt Roper65a3fea2015-01-21 16:35:42 -080013979const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013980 .update_plane = drm_atomic_helper_update_plane,
13981 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013982 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013983 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013984 .atomic_get_property = intel_plane_atomic_get_property,
13985 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013986 .atomic_duplicate_state = intel_plane_duplicate_state,
13987 .atomic_destroy_state = intel_plane_destroy_state,
13988
Matt Roper465c1202014-05-29 08:06:54 -070013989};
13990
13991static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13992 int pipe)
13993{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013994 struct intel_plane *primary = NULL;
13995 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013996 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013997 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013998 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013999
14000 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014001 if (!primary)
14002 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014003
Matt Roper8e7d6882015-01-21 16:35:41 -080014004 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014005 if (!state)
14006 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014007 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014008
Matt Roper465c1202014-05-29 08:06:54 -070014009 primary->can_scale = false;
14010 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014011 if (INTEL_INFO(dev)->gen >= 9) {
14012 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014013 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014014 }
Matt Roper465c1202014-05-29 08:06:54 -070014015 primary->pipe = pipe;
14016 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014017 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014018 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014019 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14020 primary->plane = !pipe;
14021
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014022 if (INTEL_INFO(dev)->gen >= 9) {
14023 intel_primary_formats = skl_primary_formats;
14024 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014025
14026 primary->update_plane = skylake_update_primary_plane;
14027 primary->disable_plane = skylake_disable_primary_plane;
14028 } else if (HAS_PCH_SPLIT(dev)) {
14029 intel_primary_formats = i965_primary_formats;
14030 num_formats = ARRAY_SIZE(i965_primary_formats);
14031
14032 primary->update_plane = ironlake_update_primary_plane;
14033 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014034 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014035 intel_primary_formats = i965_primary_formats;
14036 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014037
14038 primary->update_plane = i9xx_update_primary_plane;
14039 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014040 } else {
14041 intel_primary_formats = i8xx_primary_formats;
14042 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014043
14044 primary->update_plane = i9xx_update_primary_plane;
14045 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014046 }
14047
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014048 ret = drm_universal_plane_init(dev, &primary->base, 0,
14049 &intel_plane_funcs,
14050 intel_primary_formats, num_formats,
14051 DRM_PLANE_TYPE_PRIMARY, NULL);
14052 if (ret)
14053 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014054
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014055 if (INTEL_INFO(dev)->gen >= 4)
14056 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014057
Matt Roperea2c67b2014-12-23 10:41:52 -080014058 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14059
Matt Roper465c1202014-05-29 08:06:54 -070014060 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014061
14062fail:
14063 kfree(state);
14064 kfree(primary);
14065
14066 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014067}
14068
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014069void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14070{
14071 if (!dev->mode_config.rotation_property) {
14072 unsigned long flags = BIT(DRM_ROTATE_0) |
14073 BIT(DRM_ROTATE_180);
14074
14075 if (INTEL_INFO(dev)->gen >= 9)
14076 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14077
14078 dev->mode_config.rotation_property =
14079 drm_mode_create_rotation_property(dev, flags);
14080 }
14081 if (dev->mode_config.rotation_property)
14082 drm_object_attach_property(&plane->base.base,
14083 dev->mode_config.rotation_property,
14084 plane->base.state->rotation);
14085}
14086
Matt Roper3d7d6512014-06-10 08:28:13 -070014087static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014088intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014089 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014090 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014091{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014092 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014093 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014094 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014095 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014096 unsigned stride;
14097 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014098
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014099 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14100 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014101 DRM_PLANE_HELPER_NO_SCALING,
14102 DRM_PLANE_HELPER_NO_SCALING,
14103 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014104 if (ret)
14105 return ret;
14106
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014107 /* if we want to turn off the cursor ignore width and height */
14108 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014109 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014110
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014111 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014112 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014113 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14114 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014115 return -EINVAL;
14116 }
14117
Matt Roperea2c67b2014-12-23 10:41:52 -080014118 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14119 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014120 DRM_DEBUG_KMS("buffer is too small\n");
14121 return -ENOMEM;
14122 }
14123
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014124 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014125 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014126 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014127 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014128
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014129 /*
14130 * There's something wrong with the cursor on CHV pipe C.
14131 * If it straddles the left edge of the screen then
14132 * moving it away from the edge or disabling it often
14133 * results in a pipe underrun, and often that can lead to
14134 * dead pipe (constant underrun reported, and it scans
14135 * out just a solid color). To recover from that, the
14136 * display power well must be turned off and on again.
14137 * Refuse the put the cursor into that compromised position.
14138 */
14139 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14140 state->visible && state->base.crtc_x < 0) {
14141 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14142 return -EINVAL;
14143 }
14144
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014145 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014146}
14147
Matt Roperf4a2cf22014-12-01 15:40:12 -080014148static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014149intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014150 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014151{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14153
14154 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014155 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014156}
14157
14158static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014159intel_update_cursor_plane(struct drm_plane *plane,
14160 const struct intel_crtc_state *crtc_state,
14161 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014162{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014163 struct drm_crtc *crtc = crtc_state->base.crtc;
14164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014165 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014166 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014167 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014168
Matt Roperf4a2cf22014-12-01 15:40:12 -080014169 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014170 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014171 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014172 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014173 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014174 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014175
Gustavo Padovana912f122014-12-01 15:40:10 -080014176 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014177 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014178}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014179
Matt Roper3d7d6512014-06-10 08:28:13 -070014180static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14181 int pipe)
14182{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014183 struct intel_plane *cursor = NULL;
14184 struct intel_plane_state *state = NULL;
14185 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014186
14187 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014188 if (!cursor)
14189 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014190
Matt Roper8e7d6882015-01-21 16:35:41 -080014191 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014192 if (!state)
14193 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014194 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014195
Matt Roper3d7d6512014-06-10 08:28:13 -070014196 cursor->can_scale = false;
14197 cursor->max_downscale = 1;
14198 cursor->pipe = pipe;
14199 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014200 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014201 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014202 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014203 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014204
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014205 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14206 &intel_plane_funcs,
14207 intel_cursor_formats,
14208 ARRAY_SIZE(intel_cursor_formats),
14209 DRM_PLANE_TYPE_CURSOR, NULL);
14210 if (ret)
14211 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014212
14213 if (INTEL_INFO(dev)->gen >= 4) {
14214 if (!dev->mode_config.rotation_property)
14215 dev->mode_config.rotation_property =
14216 drm_mode_create_rotation_property(dev,
14217 BIT(DRM_ROTATE_0) |
14218 BIT(DRM_ROTATE_180));
14219 if (dev->mode_config.rotation_property)
14220 drm_object_attach_property(&cursor->base.base,
14221 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014222 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014223 }
14224
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014225 if (INTEL_INFO(dev)->gen >=9)
14226 state->scaler_id = -1;
14227
Matt Roperea2c67b2014-12-23 10:41:52 -080014228 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14229
Matt Roper3d7d6512014-06-10 08:28:13 -070014230 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014231
14232fail:
14233 kfree(state);
14234 kfree(cursor);
14235
14236 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014237}
14238
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014239static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14240 struct intel_crtc_state *crtc_state)
14241{
14242 int i;
14243 struct intel_scaler *intel_scaler;
14244 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14245
14246 for (i = 0; i < intel_crtc->num_scalers; i++) {
14247 intel_scaler = &scaler_state->scalers[i];
14248 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014249 intel_scaler->mode = PS_SCALER_MODE_DYN;
14250 }
14251
14252 scaler_state->scaler_id = -1;
14253}
14254
Hannes Ederb358d0a2008-12-18 21:18:47 +010014255static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014256{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014257 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014258 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014259 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014260 struct drm_plane *primary = NULL;
14261 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014262 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014263
Daniel Vetter955382f2013-09-19 14:05:45 +020014264 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014265 if (intel_crtc == NULL)
14266 return;
14267
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014268 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14269 if (!crtc_state)
14270 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014271 intel_crtc->config = crtc_state;
14272 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014273 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014274
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014275 /* initialize shared scalers */
14276 if (INTEL_INFO(dev)->gen >= 9) {
14277 if (pipe == PIPE_C)
14278 intel_crtc->num_scalers = 1;
14279 else
14280 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14281
14282 skl_init_scalers(dev, intel_crtc, crtc_state);
14283 }
14284
Matt Roper465c1202014-05-29 08:06:54 -070014285 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014286 if (!primary)
14287 goto fail;
14288
14289 cursor = intel_cursor_plane_create(dev, pipe);
14290 if (!cursor)
14291 goto fail;
14292
Matt Roper465c1202014-05-29 08:06:54 -070014293 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014294 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014295 if (ret)
14296 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014297
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014298 /*
14299 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014300 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014301 */
Jesse Barnes80824002009-09-10 15:28:06 -070014302 intel_crtc->pipe = pipe;
14303 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014304 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014305 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014306 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014307 }
14308
Chris Wilson4b0e3332014-05-30 16:35:26 +030014309 intel_crtc->cursor_base = ~0;
14310 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014311 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014312
Ville Syrjälä852eb002015-06-24 22:00:07 +030014313 intel_crtc->wm.cxsr_allowed = true;
14314
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014315 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14316 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14317 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14318 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14319
Jesse Barnes79e53942008-11-07 14:24:08 -080014320 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014321
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014322 intel_color_init(&intel_crtc->base);
14323
Daniel Vetter87b6b102014-05-15 15:33:46 +020014324 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014325 return;
14326
14327fail:
14328 if (primary)
14329 drm_plane_cleanup(primary);
14330 if (cursor)
14331 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014332 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014333 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014334}
14335
Jesse Barnes752aa882013-10-31 18:55:49 +020014336enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14337{
14338 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014339 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014340
Rob Clark51fd3712013-11-19 12:10:12 -050014341 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014342
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014343 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014344 return INVALID_PIPE;
14345
14346 return to_intel_crtc(encoder->crtc)->pipe;
14347}
14348
Carl Worth08d7b3d2009-04-29 14:43:54 -070014349int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014350 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014351{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014352 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014353 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014354 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014355
Rob Clark7707e652014-07-17 23:30:04 -040014356 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014357
Rob Clark7707e652014-07-17 23:30:04 -040014358 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014359 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014360 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014361 }
14362
Rob Clark7707e652014-07-17 23:30:04 -040014363 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014364 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014365
Daniel Vetterc05422d2009-08-11 16:05:30 +020014366 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014367}
14368
Daniel Vetter66a92782012-07-12 20:08:18 +020014369static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014370{
Daniel Vetter66a92782012-07-12 20:08:18 +020014371 struct drm_device *dev = encoder->base.dev;
14372 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014373 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014374 int entry = 0;
14375
Damien Lespiaub2784e12014-08-05 11:29:37 +010014376 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014377 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014378 index_mask |= (1 << entry);
14379
Jesse Barnes79e53942008-11-07 14:24:08 -080014380 entry++;
14381 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014382
Jesse Barnes79e53942008-11-07 14:24:08 -080014383 return index_mask;
14384}
14385
Chris Wilson4d302442010-12-14 19:21:29 +000014386static bool has_edp_a(struct drm_device *dev)
14387{
14388 struct drm_i915_private *dev_priv = dev->dev_private;
14389
14390 if (!IS_MOBILE(dev))
14391 return false;
14392
14393 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14394 return false;
14395
Damien Lespiaue3589902014-02-07 19:12:50 +000014396 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014397 return false;
14398
14399 return true;
14400}
14401
Jesse Barnes84b4e042014-06-25 08:24:29 -070014402static bool intel_crt_present(struct drm_device *dev)
14403{
14404 struct drm_i915_private *dev_priv = dev->dev_private;
14405
Damien Lespiau884497e2013-12-03 13:56:23 +000014406 if (INTEL_INFO(dev)->gen >= 9)
14407 return false;
14408
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014409 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014410 return false;
14411
14412 if (IS_CHERRYVIEW(dev))
14413 return false;
14414
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014415 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14416 return false;
14417
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014418 /* DDI E can't be used if DDI A requires 4 lanes */
14419 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14420 return false;
14421
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014422 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014423 return false;
14424
14425 return true;
14426}
14427
Jesse Barnes79e53942008-11-07 14:24:08 -080014428static void intel_setup_outputs(struct drm_device *dev)
14429{
Eric Anholt725e30a2009-01-22 13:01:02 -080014430 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014431 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014432 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014433
Daniel Vetterc9093352013-06-06 22:22:47 +020014434 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014435
Jesse Barnes84b4e042014-06-25 08:24:29 -070014436 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014437 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014438
Vandana Kannanc776eb22014-08-19 12:05:01 +053014439 if (IS_BROXTON(dev)) {
14440 /*
14441 * FIXME: Broxton doesn't support port detection via the
14442 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14443 * detect the ports.
14444 */
14445 intel_ddi_init(dev, PORT_A);
14446 intel_ddi_init(dev, PORT_B);
14447 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014448
14449 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014450 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014451 int found;
14452
Jesse Barnesde31fac2015-03-06 15:53:32 -080014453 /*
14454 * Haswell uses DDI functions to detect digital outputs.
14455 * On SKL pre-D0 the strap isn't connected, so we assume
14456 * it's there.
14457 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014458 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014459 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014460 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014461 intel_ddi_init(dev, PORT_A);
14462
14463 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14464 * register */
14465 found = I915_READ(SFUSE_STRAP);
14466
14467 if (found & SFUSE_STRAP_DDIB_DETECTED)
14468 intel_ddi_init(dev, PORT_B);
14469 if (found & SFUSE_STRAP_DDIC_DETECTED)
14470 intel_ddi_init(dev, PORT_C);
14471 if (found & SFUSE_STRAP_DDID_DETECTED)
14472 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014473 /*
14474 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14475 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014476 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014477 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14478 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14479 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14480 intel_ddi_init(dev, PORT_E);
14481
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014482 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014483 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014484 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014485
14486 if (has_edp_a(dev))
14487 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014488
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014489 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014490 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014491 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014492 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014493 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014494 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014495 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014496 }
14497
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014498 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014499 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014500
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014501 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014502 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014503
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014504 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014505 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014506
Daniel Vetter270b3042012-10-27 15:52:05 +020014507 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014508 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014509 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014510 /*
14511 * The DP_DETECTED bit is the latched state of the DDC
14512 * SDA pin at boot. However since eDP doesn't require DDC
14513 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14514 * eDP ports may have been muxed to an alternate function.
14515 * Thus we can't rely on the DP_DETECTED bit alone to detect
14516 * eDP ports. Consult the VBT as well as DP_DETECTED to
14517 * detect eDP ports.
14518 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014519 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014520 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014521 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14522 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014523 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014524 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014525
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014526 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014527 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014528 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14529 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014530 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014531 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014532
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014533 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014534 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014535 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14536 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14537 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14538 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014539 }
14540
Jani Nikula3cfca972013-08-27 15:12:26 +030014541 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014542 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014543 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014544
Paulo Zanonie2debe92013-02-18 19:00:27 -030014545 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014546 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014547 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014548 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014549 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014550 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014551 }
Ma Ling27185ae2009-08-24 13:50:23 +080014552
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014553 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014554 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014555 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014556
14557 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014558
Paulo Zanonie2debe92013-02-18 19:00:27 -030014559 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014560 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014561 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014562 }
Ma Ling27185ae2009-08-24 13:50:23 +080014563
Paulo Zanonie2debe92013-02-18 19:00:27 -030014564 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014565
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014566 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014567 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014568 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014569 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014570 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014571 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014572 }
Ma Ling27185ae2009-08-24 13:50:23 +080014573
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014574 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014575 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014576 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014577 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014578 intel_dvo_init(dev);
14579
Zhenyu Wang103a1962009-11-27 11:44:36 +080014580 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014581 intel_tv_init(dev);
14582
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014583 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014584
Damien Lespiaub2784e12014-08-05 11:29:37 +010014585 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014586 encoder->base.possible_crtcs = encoder->crtc_mask;
14587 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014588 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014589 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014590
Paulo Zanonidde86e22012-12-01 12:04:25 -020014591 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014592
14593 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014594}
14595
14596static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14597{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014598 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014599 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014600
Daniel Vetteref2d6332014-02-10 18:00:38 +010014601 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014602 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014603 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014604 drm_gem_object_unreference(&intel_fb->obj->base);
14605 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014606 kfree(intel_fb);
14607}
14608
14609static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014610 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014611 unsigned int *handle)
14612{
14613 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014614 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014615
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014616 if (obj->userptr.mm) {
14617 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14618 return -EINVAL;
14619 }
14620
Chris Wilson05394f32010-11-08 19:18:58 +000014621 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014622}
14623
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014624static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14625 struct drm_file *file,
14626 unsigned flags, unsigned color,
14627 struct drm_clip_rect *clips,
14628 unsigned num_clips)
14629{
14630 struct drm_device *dev = fb->dev;
14631 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14632 struct drm_i915_gem_object *obj = intel_fb->obj;
14633
14634 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014635 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014636 mutex_unlock(&dev->struct_mutex);
14637
14638 return 0;
14639}
14640
Jesse Barnes79e53942008-11-07 14:24:08 -080014641static const struct drm_framebuffer_funcs intel_fb_funcs = {
14642 .destroy = intel_user_framebuffer_destroy,
14643 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014644 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014645};
14646
Damien Lespiaub3218032015-02-27 11:15:18 +000014647static
14648u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14649 uint32_t pixel_format)
14650{
14651 u32 gen = INTEL_INFO(dev)->gen;
14652
14653 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014654 int cpp = drm_format_plane_cpp(pixel_format, 0);
14655
Damien Lespiaub3218032015-02-27 11:15:18 +000014656 /* "The stride in bytes must not exceed the of the size of 8K
14657 * pixels and 32K bytes."
14658 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014659 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014660 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014661 return 32*1024;
14662 } else if (gen >= 4) {
14663 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14664 return 16*1024;
14665 else
14666 return 32*1024;
14667 } else if (gen >= 3) {
14668 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14669 return 8*1024;
14670 else
14671 return 16*1024;
14672 } else {
14673 /* XXX DSPC is limited to 4k tiled */
14674 return 8*1024;
14675 }
14676}
14677
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014678static int intel_framebuffer_init(struct drm_device *dev,
14679 struct intel_framebuffer *intel_fb,
14680 struct drm_mode_fb_cmd2 *mode_cmd,
14681 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014682{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014683 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014684 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014685 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014686 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014687
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014688 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14689
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014690 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14691 /* Enforce that fb modifier and tiling mode match, but only for
14692 * X-tiled. This is needed for FBC. */
14693 if (!!(obj->tiling_mode == I915_TILING_X) !=
14694 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14695 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14696 return -EINVAL;
14697 }
14698 } else {
14699 if (obj->tiling_mode == I915_TILING_X)
14700 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14701 else if (obj->tiling_mode == I915_TILING_Y) {
14702 DRM_DEBUG("No Y tiling for legacy addfb\n");
14703 return -EINVAL;
14704 }
14705 }
14706
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014707 /* Passed in modifier sanity checking. */
14708 switch (mode_cmd->modifier[0]) {
14709 case I915_FORMAT_MOD_Y_TILED:
14710 case I915_FORMAT_MOD_Yf_TILED:
14711 if (INTEL_INFO(dev)->gen < 9) {
14712 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14713 mode_cmd->modifier[0]);
14714 return -EINVAL;
14715 }
14716 case DRM_FORMAT_MOD_NONE:
14717 case I915_FORMAT_MOD_X_TILED:
14718 break;
14719 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014720 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14721 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014722 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014723 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014724
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014725 stride_alignment = intel_fb_stride_alignment(dev_priv,
14726 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014727 mode_cmd->pixel_format);
14728 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14729 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14730 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014731 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014732 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014733
Damien Lespiaub3218032015-02-27 11:15:18 +000014734 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14735 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014736 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014737 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14738 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014739 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014740 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014741 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014742 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014743
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014744 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014745 mode_cmd->pitches[0] != obj->stride) {
14746 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14747 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014748 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014749 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014750
Ville Syrjälä57779d02012-10-31 17:50:14 +020014751 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014752 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014753 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014754 case DRM_FORMAT_RGB565:
14755 case DRM_FORMAT_XRGB8888:
14756 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014757 break;
14758 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014759 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014760 DRM_DEBUG("unsupported pixel format: %s\n",
14761 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014762 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014763 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014764 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014765 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014766 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14767 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014768 DRM_DEBUG("unsupported pixel format: %s\n",
14769 drm_get_format_name(mode_cmd->pixel_format));
14770 return -EINVAL;
14771 }
14772 break;
14773 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014774 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014775 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014776 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014777 DRM_DEBUG("unsupported pixel format: %s\n",
14778 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014779 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014780 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014781 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014782 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014783 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014784 DRM_DEBUG("unsupported pixel format: %s\n",
14785 drm_get_format_name(mode_cmd->pixel_format));
14786 return -EINVAL;
14787 }
14788 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014789 case DRM_FORMAT_YUYV:
14790 case DRM_FORMAT_UYVY:
14791 case DRM_FORMAT_YVYU:
14792 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014793 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014794 DRM_DEBUG("unsupported pixel format: %s\n",
14795 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014796 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014797 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014798 break;
14799 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014800 DRM_DEBUG("unsupported pixel format: %s\n",
14801 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014802 return -EINVAL;
14803 }
14804
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014805 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14806 if (mode_cmd->offsets[0] != 0)
14807 return -EINVAL;
14808
Damien Lespiauec2c9812015-01-20 12:51:45 +000014809 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014810 mode_cmd->pixel_format,
14811 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014812 /* FIXME drm helper for size checks (especially planar formats)? */
14813 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14814 return -EINVAL;
14815
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014816 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14817 intel_fb->obj = obj;
14818
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014819 intel_fill_fb_info(dev_priv, &intel_fb->base);
14820
Jesse Barnes79e53942008-11-07 14:24:08 -080014821 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14822 if (ret) {
14823 DRM_ERROR("framebuffer init failed %d\n", ret);
14824 return ret;
14825 }
14826
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014827 intel_fb->obj->framebuffer_references++;
14828
Jesse Barnes79e53942008-11-07 14:24:08 -080014829 return 0;
14830}
14831
Jesse Barnes79e53942008-11-07 14:24:08 -080014832static struct drm_framebuffer *
14833intel_user_framebuffer_create(struct drm_device *dev,
14834 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014835 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014836{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014837 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014838 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014839 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014840
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014841 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014842 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014843 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014844 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014845
Daniel Vetter92907cb2015-11-23 09:04:05 +010014846 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014847 if (IS_ERR(fb))
14848 drm_gem_object_unreference_unlocked(&obj->base);
14849
14850 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014851}
14852
Daniel Vetter06957262015-08-10 13:34:08 +020014853#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014854static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014855{
14856}
14857#endif
14858
Jesse Barnes79e53942008-11-07 14:24:08 -080014859static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014860 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014861 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014862 .atomic_check = intel_atomic_check,
14863 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014864 .atomic_state_alloc = intel_atomic_state_alloc,
14865 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014866};
14867
Imre Deak88212942016-03-16 13:38:53 +020014868/**
14869 * intel_init_display_hooks - initialize the display modesetting hooks
14870 * @dev_priv: device private
14871 */
14872void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014873{
Imre Deak88212942016-03-16 13:38:53 +020014874 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014875 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014876 dev_priv->display.get_initial_plane_config =
14877 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014878 dev_priv->display.crtc_compute_clock =
14879 haswell_crtc_compute_clock;
14880 dev_priv->display.crtc_enable = haswell_crtc_enable;
14881 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014882 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014883 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014884 dev_priv->display.get_initial_plane_config =
14885 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014886 dev_priv->display.crtc_compute_clock =
14887 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014888 dev_priv->display.crtc_enable = haswell_crtc_enable;
14889 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014890 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014891 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014892 dev_priv->display.get_initial_plane_config =
14893 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014894 dev_priv->display.crtc_compute_clock =
14895 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014896 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14897 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014898 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014899 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014900 dev_priv->display.get_initial_plane_config =
14901 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014902 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14903 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14904 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14905 } else if (IS_VALLEYVIEW(dev_priv)) {
14906 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14907 dev_priv->display.get_initial_plane_config =
14908 i9xx_get_initial_plane_config;
14909 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014910 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14911 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014912 } else if (IS_G4X(dev_priv)) {
14913 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14914 dev_priv->display.get_initial_plane_config =
14915 i9xx_get_initial_plane_config;
14916 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14917 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14918 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014919 } else if (IS_PINEVIEW(dev_priv)) {
14920 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14921 dev_priv->display.get_initial_plane_config =
14922 i9xx_get_initial_plane_config;
14923 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14924 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14925 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014926 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014927 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014928 dev_priv->display.get_initial_plane_config =
14929 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014930 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014931 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14932 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014933 } else {
14934 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14935 dev_priv->display.get_initial_plane_config =
14936 i9xx_get_initial_plane_config;
14937 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14938 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14939 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014940 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014941
Jesse Barnese70236a2009-09-21 10:42:27 -070014942 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014943 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014944 dev_priv->display.get_display_clock_speed =
14945 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014946 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014947 dev_priv->display.get_display_clock_speed =
14948 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014949 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014950 dev_priv->display.get_display_clock_speed =
14951 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014952 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014953 dev_priv->display.get_display_clock_speed =
14954 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014955 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014956 dev_priv->display.get_display_clock_speed =
14957 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014958 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014959 dev_priv->display.get_display_clock_speed =
14960 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014961 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14962 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014963 dev_priv->display.get_display_clock_speed =
14964 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014965 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014966 dev_priv->display.get_display_clock_speed =
14967 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014968 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014969 dev_priv->display.get_display_clock_speed =
14970 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014971 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014972 dev_priv->display.get_display_clock_speed =
14973 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014974 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014975 dev_priv->display.get_display_clock_speed =
14976 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014977 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014978 dev_priv->display.get_display_clock_speed =
14979 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014980 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014981 dev_priv->display.get_display_clock_speed =
14982 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014983 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014984 dev_priv->display.get_display_clock_speed =
14985 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014986 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014987 dev_priv->display.get_display_clock_speed =
14988 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014989 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014990 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014991 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014992 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014993 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014994 dev_priv->display.get_display_clock_speed =
14995 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014996 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014997
Imre Deak88212942016-03-16 13:38:53 +020014998 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014999 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015000 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015001 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015002 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015003 /* FIXME: detect B0+ stepping and use auto training */
15004 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015005 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015006 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015007 }
15008
15009 if (IS_BROADWELL(dev_priv)) {
15010 dev_priv->display.modeset_commit_cdclk =
15011 broadwell_modeset_commit_cdclk;
15012 dev_priv->display.modeset_calc_cdclk =
15013 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015014 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015015 dev_priv->display.modeset_commit_cdclk =
15016 valleyview_modeset_commit_cdclk;
15017 dev_priv->display.modeset_calc_cdclk =
15018 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015019 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015020 dev_priv->display.modeset_commit_cdclk =
15021 broxton_modeset_commit_cdclk;
15022 dev_priv->display.modeset_calc_cdclk =
15023 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015024 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015025
Imre Deak88212942016-03-16 13:38:53 +020015026 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015027 case 2:
15028 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15029 break;
15030
15031 case 3:
15032 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15033 break;
15034
15035 case 4:
15036 case 5:
15037 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15038 break;
15039
15040 case 6:
15041 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15042 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015043 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015044 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015045 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15046 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015047 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015048 /* Drop through - unsupported since execlist only. */
15049 default:
15050 /* Default just returns -ENODEV to indicate unsupported */
15051 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015052 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015053}
15054
Jesse Barnesb690e962010-07-19 13:53:12 -070015055/*
15056 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15057 * resume, or other times. This quirk makes sure that's the case for
15058 * affected systems.
15059 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015060static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015061{
15062 struct drm_i915_private *dev_priv = dev->dev_private;
15063
15064 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015065 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015066}
15067
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015068static void quirk_pipeb_force(struct drm_device *dev)
15069{
15070 struct drm_i915_private *dev_priv = dev->dev_private;
15071
15072 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15073 DRM_INFO("applying pipe b force quirk\n");
15074}
15075
Keith Packard435793d2011-07-12 14:56:22 -070015076/*
15077 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15078 */
15079static void quirk_ssc_force_disable(struct drm_device *dev)
15080{
15081 struct drm_i915_private *dev_priv = dev->dev_private;
15082 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015083 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015084}
15085
Carsten Emde4dca20e2012-03-15 15:56:26 +010015086/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015087 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15088 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015089 */
15090static void quirk_invert_brightness(struct drm_device *dev)
15091{
15092 struct drm_i915_private *dev_priv = dev->dev_private;
15093 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015094 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015095}
15096
Scot Doyle9c72cc62014-07-03 23:27:50 +000015097/* Some VBT's incorrectly indicate no backlight is present */
15098static void quirk_backlight_present(struct drm_device *dev)
15099{
15100 struct drm_i915_private *dev_priv = dev->dev_private;
15101 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15102 DRM_INFO("applying backlight present quirk\n");
15103}
15104
Jesse Barnesb690e962010-07-19 13:53:12 -070015105struct intel_quirk {
15106 int device;
15107 int subsystem_vendor;
15108 int subsystem_device;
15109 void (*hook)(struct drm_device *dev);
15110};
15111
Egbert Eich5f85f172012-10-14 15:46:38 +020015112/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15113struct intel_dmi_quirk {
15114 void (*hook)(struct drm_device *dev);
15115 const struct dmi_system_id (*dmi_id_list)[];
15116};
15117
15118static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15119{
15120 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15121 return 1;
15122}
15123
15124static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15125 {
15126 .dmi_id_list = &(const struct dmi_system_id[]) {
15127 {
15128 .callback = intel_dmi_reverse_brightness,
15129 .ident = "NCR Corporation",
15130 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15131 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15132 },
15133 },
15134 { } /* terminating entry */
15135 },
15136 .hook = quirk_invert_brightness,
15137 },
15138};
15139
Ben Widawskyc43b5632012-04-16 14:07:40 -070015140static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015141 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15142 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15143
Jesse Barnesb690e962010-07-19 13:53:12 -070015144 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15145 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15146
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015147 /* 830 needs to leave pipe A & dpll A up */
15148 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15149
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015150 /* 830 needs to leave pipe B & dpll B up */
15151 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15152
Keith Packard435793d2011-07-12 14:56:22 -070015153 /* Lenovo U160 cannot use SSC on LVDS */
15154 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015155
15156 /* Sony Vaio Y cannot use SSC on LVDS */
15157 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015158
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015159 /* Acer Aspire 5734Z must invert backlight brightness */
15160 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15161
15162 /* Acer/eMachines G725 */
15163 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15164
15165 /* Acer/eMachines e725 */
15166 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15167
15168 /* Acer/Packard Bell NCL20 */
15169 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15170
15171 /* Acer Aspire 4736Z */
15172 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015173
15174 /* Acer Aspire 5336 */
15175 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015176
15177 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15178 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015179
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015180 /* Acer C720 Chromebook (Core i3 4005U) */
15181 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15182
jens steinb2a96012014-10-28 20:25:53 +010015183 /* Apple Macbook 2,1 (Core 2 T7400) */
15184 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15185
Jani Nikula1b9448b02015-11-05 11:49:59 +020015186 /* Apple Macbook 4,1 */
15187 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15188
Scot Doyled4967d82014-07-03 23:27:52 +000015189 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15190 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015191
15192 /* HP Chromebook 14 (Celeron 2955U) */
15193 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015194
15195 /* Dell Chromebook 11 */
15196 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015197
15198 /* Dell Chromebook 11 (2015 version) */
15199 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015200};
15201
15202static void intel_init_quirks(struct drm_device *dev)
15203{
15204 struct pci_dev *d = dev->pdev;
15205 int i;
15206
15207 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15208 struct intel_quirk *q = &intel_quirks[i];
15209
15210 if (d->device == q->device &&
15211 (d->subsystem_vendor == q->subsystem_vendor ||
15212 q->subsystem_vendor == PCI_ANY_ID) &&
15213 (d->subsystem_device == q->subsystem_device ||
15214 q->subsystem_device == PCI_ANY_ID))
15215 q->hook(dev);
15216 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015217 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15218 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15219 intel_dmi_quirks[i].hook(dev);
15220 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015221}
15222
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015223/* Disable the VGA plane that we never use */
15224static void i915_disable_vga(struct drm_device *dev)
15225{
15226 struct drm_i915_private *dev_priv = dev->dev_private;
15227 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015228 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015229
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015230 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015231 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015232 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015233 sr1 = inb(VGA_SR_DATA);
15234 outb(sr1 | 1<<5, VGA_SR_DATA);
15235 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15236 udelay(300);
15237
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015238 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015239 POSTING_READ(vga_reg);
15240}
15241
Daniel Vetterf8175862012-04-10 15:50:11 +020015242void intel_modeset_init_hw(struct drm_device *dev)
15243{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015244 struct drm_i915_private *dev_priv = dev->dev_private;
15245
Ville Syrjäläb6283052015-06-03 15:45:07 +030015246 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015247
15248 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15249
Daniel Vetterf8175862012-04-10 15:50:11 +020015250 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015251 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015252}
15253
Matt Roperd93c0372015-12-03 11:37:41 -080015254/*
15255 * Calculate what we think the watermarks should be for the state we've read
15256 * out of the hardware and then immediately program those watermarks so that
15257 * we ensure the hardware settings match our internal state.
15258 *
15259 * We can calculate what we think WM's should be by creating a duplicate of the
15260 * current state (which was constructed during hardware readout) and running it
15261 * through the atomic check code to calculate new watermark values in the
15262 * state object.
15263 */
15264static void sanitize_watermarks(struct drm_device *dev)
15265{
15266 struct drm_i915_private *dev_priv = to_i915(dev);
15267 struct drm_atomic_state *state;
15268 struct drm_crtc *crtc;
15269 struct drm_crtc_state *cstate;
15270 struct drm_modeset_acquire_ctx ctx;
15271 int ret;
15272 int i;
15273
15274 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015275 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015276 return;
15277
15278 /*
15279 * We need to hold connection_mutex before calling duplicate_state so
15280 * that the connector loop is protected.
15281 */
15282 drm_modeset_acquire_init(&ctx, 0);
15283retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015284 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015285 if (ret == -EDEADLK) {
15286 drm_modeset_backoff(&ctx);
15287 goto retry;
15288 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015289 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015290 }
15291
15292 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15293 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015294 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015295
Matt Ropered4a6a72016-02-23 17:20:13 -080015296 /*
15297 * Hardware readout is the only time we don't want to calculate
15298 * intermediate watermarks (since we don't trust the current
15299 * watermarks).
15300 */
15301 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15302
Matt Roperd93c0372015-12-03 11:37:41 -080015303 ret = intel_atomic_check(dev, state);
15304 if (ret) {
15305 /*
15306 * If we fail here, it means that the hardware appears to be
15307 * programmed in a way that shouldn't be possible, given our
15308 * understanding of watermark requirements. This might mean a
15309 * mistake in the hardware readout code or a mistake in the
15310 * watermark calculations for a given platform. Raise a WARN
15311 * so that this is noticeable.
15312 *
15313 * If this actually happens, we'll have to just leave the
15314 * BIOS-programmed watermarks untouched and hope for the best.
15315 */
15316 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015317 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015318 }
15319
15320 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015321 for_each_crtc_in_state(state, crtc, cstate, i) {
15322 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15323
Matt Ropered4a6a72016-02-23 17:20:13 -080015324 cs->wm.need_postvbl_update = true;
15325 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015326 }
15327
15328 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015329fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015330 drm_modeset_drop_locks(&ctx);
15331 drm_modeset_acquire_fini(&ctx);
15332}
15333
Jesse Barnes79e53942008-11-07 14:24:08 -080015334void intel_modeset_init(struct drm_device *dev)
15335{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015336 struct drm_i915_private *dev_priv = to_i915(dev);
15337 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015338 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015339 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015340 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015341
15342 drm_mode_config_init(dev);
15343
15344 dev->mode_config.min_width = 0;
15345 dev->mode_config.min_height = 0;
15346
Dave Airlie019d96c2011-09-29 16:20:42 +010015347 dev->mode_config.preferred_depth = 24;
15348 dev->mode_config.prefer_shadow = 1;
15349
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015350 dev->mode_config.allow_fb_modifiers = true;
15351
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015352 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015353
Jesse Barnesb690e962010-07-19 13:53:12 -070015354 intel_init_quirks(dev);
15355
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015356 intel_init_pm(dev);
15357
Ben Widawskye3c74752013-04-05 13:12:39 -070015358 if (INTEL_INFO(dev)->num_pipes == 0)
15359 return;
15360
Lukas Wunner69f92f62015-07-15 13:57:35 +020015361 /*
15362 * There may be no VBT; and if the BIOS enabled SSC we can
15363 * just keep using it to avoid unnecessary flicker. Whereas if the
15364 * BIOS isn't using it, don't assume it will work even if the VBT
15365 * indicates as much.
15366 */
15367 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15368 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15369 DREF_SSC1_ENABLE);
15370
15371 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15372 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15373 bios_lvds_use_ssc ? "en" : "dis",
15374 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15375 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15376 }
15377 }
15378
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015379 if (IS_GEN2(dev)) {
15380 dev->mode_config.max_width = 2048;
15381 dev->mode_config.max_height = 2048;
15382 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015383 dev->mode_config.max_width = 4096;
15384 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015385 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015386 dev->mode_config.max_width = 8192;
15387 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015388 }
Damien Lespiau068be562014-03-28 14:17:49 +000015389
Ville Syrjälädc41c152014-08-13 11:57:05 +030015390 if (IS_845G(dev) || IS_I865G(dev)) {
15391 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15392 dev->mode_config.cursor_height = 1023;
15393 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015394 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15395 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15396 } else {
15397 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15398 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15399 }
15400
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015401 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015402
Zhao Yakui28c97732009-10-09 11:39:41 +080015403 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015404 INTEL_INFO(dev)->num_pipes,
15405 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015406
Damien Lespiau055e3932014-08-18 13:49:10 +010015407 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015408 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015409 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015410 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015411 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015412 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015413 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015414 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015415 }
15416
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015417 intel_update_czclk(dev_priv);
15418 intel_update_cdclk(dev);
15419
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015420 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015421
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015422 /* Just disable it once at startup */
15423 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015424 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015425
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015426 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015427 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015428 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015429
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015430 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015431 struct intel_initial_plane_config plane_config = {};
15432
Jesse Barnes46f297f2014-03-07 08:57:48 -080015433 if (!crtc->active)
15434 continue;
15435
Jesse Barnes46f297f2014-03-07 08:57:48 -080015436 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015437 * Note that reserving the BIOS fb up front prevents us
15438 * from stuffing other stolen allocations like the ring
15439 * on top. This prevents some ugliness at boot time, and
15440 * can even allow for smooth boot transitions if the BIOS
15441 * fb is large enough for the active pipe configuration.
15442 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015443 dev_priv->display.get_initial_plane_config(crtc,
15444 &plane_config);
15445
15446 /*
15447 * If the fb is shared between multiple heads, we'll
15448 * just get the first one.
15449 */
15450 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015451 }
Matt Roperd93c0372015-12-03 11:37:41 -080015452
15453 /*
15454 * Make sure hardware watermarks really match the state we read out.
15455 * Note that we need to do this after reconstructing the BIOS fb's
15456 * since the watermark calculation done here will use pstate->fb.
15457 */
15458 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015459}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015460
Daniel Vetter7fad7982012-07-04 17:51:47 +020015461static void intel_enable_pipe_a(struct drm_device *dev)
15462{
15463 struct intel_connector *connector;
15464 struct drm_connector *crt = NULL;
15465 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015466 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015467
15468 /* We can't just switch on the pipe A, we need to set things up with a
15469 * proper mode and output configuration. As a gross hack, enable pipe A
15470 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015471 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015472 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15473 crt = &connector->base;
15474 break;
15475 }
15476 }
15477
15478 if (!crt)
15479 return;
15480
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015481 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015482 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015483}
15484
Daniel Vetterfa555832012-10-10 23:14:00 +020015485static bool
15486intel_check_plane_mapping(struct intel_crtc *crtc)
15487{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015488 struct drm_device *dev = crtc->base.dev;
15489 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015490 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015491
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015492 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015493 return true;
15494
Ville Syrjälä649636e2015-09-22 19:50:01 +030015495 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015496
15497 if ((val & DISPLAY_PLANE_ENABLE) &&
15498 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15499 return false;
15500
15501 return true;
15502}
15503
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015504static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15505{
15506 struct drm_device *dev = crtc->base.dev;
15507 struct intel_encoder *encoder;
15508
15509 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15510 return true;
15511
15512 return false;
15513}
15514
Ville Syrjälädd756192016-02-17 21:28:45 +020015515static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15516{
15517 struct drm_device *dev = encoder->base.dev;
15518 struct intel_connector *connector;
15519
15520 for_each_connector_on_encoder(dev, &encoder->base, connector)
15521 return true;
15522
15523 return false;
15524}
15525
Daniel Vetter24929352012-07-02 20:28:59 +020015526static void intel_sanitize_crtc(struct intel_crtc *crtc)
15527{
15528 struct drm_device *dev = crtc->base.dev;
15529 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015530 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015531
Daniel Vetter24929352012-07-02 20:28:59 +020015532 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015533 if (!transcoder_is_dsi(cpu_transcoder)) {
15534 i915_reg_t reg = PIPECONF(cpu_transcoder);
15535
15536 I915_WRITE(reg,
15537 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15538 }
Daniel Vetter24929352012-07-02 20:28:59 +020015539
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015540 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015541 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015542 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015543 struct intel_plane *plane;
15544
Daniel Vetter96256042015-02-13 21:03:42 +010015545 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015546
15547 /* Disable everything but the primary plane */
15548 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15549 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15550 continue;
15551
15552 plane->disable_plane(&plane->base, &crtc->base);
15553 }
Daniel Vetter96256042015-02-13 21:03:42 +010015554 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015555
Daniel Vetter24929352012-07-02 20:28:59 +020015556 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015557 * disable the crtc (and hence change the state) if it is wrong. Note
15558 * that gen4+ has a fixed plane -> pipe mapping. */
15559 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015560 bool plane;
15561
Daniel Vetter24929352012-07-02 20:28:59 +020015562 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15563 crtc->base.base.id);
15564
15565 /* Pipe has the wrong plane attached and the plane is active.
15566 * Temporarily change the plane mapping and disable everything
15567 * ... */
15568 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015569 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015570 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015571 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015572 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015573 }
Daniel Vetter24929352012-07-02 20:28:59 +020015574
Daniel Vetter7fad7982012-07-04 17:51:47 +020015575 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15576 crtc->pipe == PIPE_A && !crtc->active) {
15577 /* BIOS forgot to enable pipe A, this mostly happens after
15578 * resume. Force-enable the pipe to fix this, the update_dpms
15579 * call below we restore the pipe to the right state, but leave
15580 * the required bits on. */
15581 intel_enable_pipe_a(dev);
15582 }
15583
Daniel Vetter24929352012-07-02 20:28:59 +020015584 /* Adjust the state of the output pipe according to whether we
15585 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015586 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015587 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015588
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015589 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015590 /*
15591 * We start out with underrun reporting disabled to avoid races.
15592 * For correct bookkeeping mark this on active crtcs.
15593 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015594 * Also on gmch platforms we dont have any hardware bits to
15595 * disable the underrun reporting. Which means we need to start
15596 * out with underrun reporting disabled also on inactive pipes,
15597 * since otherwise we'll complain about the garbage we read when
15598 * e.g. coming up after runtime pm.
15599 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015600 * No protection against concurrent access is required - at
15601 * worst a fifo underrun happens which also sets this to false.
15602 */
15603 crtc->cpu_fifo_underrun_disabled = true;
15604 crtc->pch_fifo_underrun_disabled = true;
15605 }
Daniel Vetter24929352012-07-02 20:28:59 +020015606}
15607
15608static void intel_sanitize_encoder(struct intel_encoder *encoder)
15609{
15610 struct intel_connector *connector;
15611 struct drm_device *dev = encoder->base.dev;
15612
15613 /* We need to check both for a crtc link (meaning that the
15614 * encoder is active and trying to read from a pipe) and the
15615 * pipe itself being active. */
15616 bool has_active_crtc = encoder->base.crtc &&
15617 to_intel_crtc(encoder->base.crtc)->active;
15618
Ville Syrjälädd756192016-02-17 21:28:45 +020015619 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015620 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15621 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015622 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015623
15624 /* Connector is active, but has no active pipe. This is
15625 * fallout from our resume register restoring. Disable
15626 * the encoder manually again. */
15627 if (encoder->base.crtc) {
15628 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15629 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015630 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015631 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015632 if (encoder->post_disable)
15633 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015634 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015635 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015636
15637 /* Inconsistent output/port/pipe state happens presumably due to
15638 * a bug in one of the get_hw_state functions. Or someplace else
15639 * in our code, like the register restore mess on resume. Clamp
15640 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015641 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015642 if (connector->encoder != encoder)
15643 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015644 connector->base.dpms = DRM_MODE_DPMS_OFF;
15645 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015646 }
15647 }
15648 /* Enabled encoders without active connectors will be fixed in
15649 * the crtc fixup. */
15650}
15651
Imre Deak04098752014-02-18 00:02:16 +020015652void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015653{
15654 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015655 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015656
Imre Deak04098752014-02-18 00:02:16 +020015657 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15658 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15659 i915_disable_vga(dev);
15660 }
15661}
15662
15663void i915_redisable_vga(struct drm_device *dev)
15664{
15665 struct drm_i915_private *dev_priv = dev->dev_private;
15666
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015667 /* This function can be called both from intel_modeset_setup_hw_state or
15668 * at a very early point in our resume sequence, where the power well
15669 * structures are not yet restored. Since this function is at a very
15670 * paranoid "someone might have enabled VGA while we were not looking"
15671 * level, just check if the power well is enabled instead of trying to
15672 * follow the "don't touch the power well if we don't need it" policy
15673 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015674 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015675 return;
15676
Imre Deak04098752014-02-18 00:02:16 +020015677 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015678
15679 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015680}
15681
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015682static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015683{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015684 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015685
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015686 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015687}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015688
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015689/* FIXME read out full plane state for all planes */
15690static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015691{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015692 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015693 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015694 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015695
Matt Roper19b8d382015-09-24 15:53:17 -070015696 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015697 primary_get_hw_state(to_intel_plane(primary));
15698
15699 if (plane_state->visible)
15700 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015701}
15702
Daniel Vetter30e984d2013-06-05 13:34:17 +020015703static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015704{
15705 struct drm_i915_private *dev_priv = dev->dev_private;
15706 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015707 struct intel_crtc *crtc;
15708 struct intel_encoder *encoder;
15709 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015710 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015711
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015712 dev_priv->active_crtcs = 0;
15713
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015714 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015715 struct intel_crtc_state *crtc_state = crtc->config;
15716 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015717
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015718 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15719 memset(crtc_state, 0, sizeof(*crtc_state));
15720 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015721
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015722 crtc_state->base.active = crtc_state->base.enable =
15723 dev_priv->display.get_pipe_config(crtc, crtc_state);
15724
15725 crtc->base.enabled = crtc_state->base.enable;
15726 crtc->active = crtc_state->base.active;
15727
15728 if (crtc_state->base.active) {
15729 dev_priv->active_crtcs |= 1 << crtc->pipe;
15730
15731 if (IS_BROADWELL(dev_priv)) {
15732 pixclk = ilk_pipe_pixel_rate(crtc_state);
15733
15734 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15735 if (crtc_state->ips_enabled)
15736 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15737 } else if (IS_VALLEYVIEW(dev_priv) ||
15738 IS_CHERRYVIEW(dev_priv) ||
15739 IS_BROXTON(dev_priv))
15740 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15741 else
15742 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15743 }
15744
15745 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015746
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015747 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015748
15749 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15750 crtc->base.base.id,
15751 crtc->active ? "enabled" : "disabled");
15752 }
15753
Daniel Vetter53589012013-06-05 13:34:16 +020015754 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15755 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15756
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015757 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15758 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015759 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015760 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015761 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015762 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015763 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015764 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015765
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015766 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015767 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015768 }
15769
Damien Lespiaub2784e12014-08-05 11:29:37 +010015770 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015771 pipe = 0;
15772
15773 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015774 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15775 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015776 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015777 } else {
15778 encoder->base.crtc = NULL;
15779 }
15780
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015781 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015782 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015783 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015784 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015785 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015786 }
15787
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015788 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015789 if (connector->get_hw_state(connector)) {
15790 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015791
15792 encoder = connector->encoder;
15793 connector->base.encoder = &encoder->base;
15794
15795 if (encoder->base.crtc &&
15796 encoder->base.crtc->state->active) {
15797 /*
15798 * This has to be done during hardware readout
15799 * because anything calling .crtc_disable may
15800 * rely on the connector_mask being accurate.
15801 */
15802 encoder->base.crtc->state->connector_mask |=
15803 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015804 encoder->base.crtc->state->encoder_mask |=
15805 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015806 }
15807
Daniel Vetter24929352012-07-02 20:28:59 +020015808 } else {
15809 connector->base.dpms = DRM_MODE_DPMS_OFF;
15810 connector->base.encoder = NULL;
15811 }
15812 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15813 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015814 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015815 connector->base.encoder ? "enabled" : "disabled");
15816 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015817
15818 for_each_intel_crtc(dev, crtc) {
15819 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15820
15821 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15822 if (crtc->base.state->active) {
15823 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15824 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15825 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15826
15827 /*
15828 * The initial mode needs to be set in order to keep
15829 * the atomic core happy. It wants a valid mode if the
15830 * crtc's enabled, so we do the above call.
15831 *
15832 * At this point some state updated by the connectors
15833 * in their ->detect() callback has not run yet, so
15834 * no recalculation can be done yet.
15835 *
15836 * Even if we could do a recalculation and modeset
15837 * right now it would cause a double modeset if
15838 * fbdev or userspace chooses a different initial mode.
15839 *
15840 * If that happens, someone indicated they wanted a
15841 * mode change, which means it's safe to do a full
15842 * recalculation.
15843 */
15844 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015845
15846 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15847 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015848 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015849
15850 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015851 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015852}
15853
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015854/* Scan out the current hw modeset state,
15855 * and sanitizes it to the current state
15856 */
15857static void
15858intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015859{
15860 struct drm_i915_private *dev_priv = dev->dev_private;
15861 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015862 struct intel_crtc *crtc;
15863 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015864 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015865
15866 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015867
15868 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015869 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015870 intel_sanitize_encoder(encoder);
15871 }
15872
Damien Lespiau055e3932014-08-18 13:49:10 +010015873 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015874 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15875 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015876 intel_dump_pipe_config(crtc, crtc->config,
15877 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015878 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015879
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015880 intel_modeset_update_connector_atomic_state(dev);
15881
Daniel Vetter35c95372013-07-17 06:55:04 +020015882 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15883 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15884
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015885 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015886 continue;
15887
15888 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15889
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015890 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015891 pll->on = false;
15892 }
15893
Wayne Boyer666a4532015-12-09 12:29:35 -080015894 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015895 vlv_wm_get_hw_state(dev);
15896 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015897 skl_wm_get_hw_state(dev);
15898 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015899 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015900
15901 for_each_intel_crtc(dev, crtc) {
15902 unsigned long put_domains;
15903
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015904 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015905 if (WARN_ON(put_domains))
15906 modeset_put_power_domains(dev_priv, put_domains);
15907 }
15908 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015909
15910 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015911}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015912
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015913void intel_display_resume(struct drm_device *dev)
15914{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015915 struct drm_i915_private *dev_priv = to_i915(dev);
15916 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15917 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015918 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015919 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015920
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015921 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015922
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015923 /*
15924 * This is a cludge because with real atomic modeset mode_config.mutex
15925 * won't be taken. Unfortunately some probed state like
15926 * audio_codec_enable is still protected by mode_config.mutex, so lock
15927 * it here for now.
15928 */
15929 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015930 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015931
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015932retry:
15933 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015934
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015935 if (ret == 0 && !setup) {
15936 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015937
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015938 intel_modeset_setup_hw_state(dev);
15939 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015940 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015941
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015942 if (ret == 0 && state) {
15943 struct drm_crtc_state *crtc_state;
15944 struct drm_crtc *crtc;
15945 int i;
15946
15947 state->acquire_ctx = &ctx;
15948
Ville Syrjäläe3d54572016-05-13 10:10:42 -070015949 /* ignore any reset values/BIOS leftovers in the WM registers */
15950 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15951
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015952 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15953 /*
15954 * Force recalculation even if we restore
15955 * current state. With fast modeset this may not result
15956 * in a modeset when the state is compatible.
15957 */
15958 crtc_state->mode_changed = true;
15959 }
15960
15961 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015962 }
15963
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015964 if (ret == -EDEADLK) {
15965 drm_modeset_backoff(&ctx);
15966 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015967 }
15968
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015969 drm_modeset_drop_locks(&ctx);
15970 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015971 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015972
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015973 if (ret) {
15974 DRM_ERROR("Restoring old state failed with %i\n", ret);
15975 drm_atomic_state_free(state);
15976 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015977}
15978
15979void intel_modeset_gem_init(struct drm_device *dev)
15980{
Chris Wilsondc979972016-05-10 14:10:04 +010015981 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015982 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015983 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015984 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015985
Chris Wilsondc979972016-05-10 14:10:04 +010015986 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015987
Chris Wilson1833b132012-05-09 11:56:28 +010015988 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015989
Chris Wilson1ee8da62016-05-12 12:43:23 +010015990 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015991
15992 /*
15993 * Make sure any fbs we allocated at startup are properly
15994 * pinned & fenced. When we do the allocation it's too early
15995 * for this.
15996 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015997 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015998 obj = intel_fb_obj(c->primary->fb);
15999 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016000 continue;
16001
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016002 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016003 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16004 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016005 mutex_unlock(&dev->struct_mutex);
16006 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016007 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16008 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016009 drm_framebuffer_unreference(c->primary->fb);
16010 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016011 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016012 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016013 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016014 }
16015 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016016
16017 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016018}
16019
Imre Deak4932e2c2014-02-11 17:12:48 +020016020void intel_connector_unregister(struct intel_connector *intel_connector)
16021{
16022 struct drm_connector *connector = &intel_connector->base;
16023
16024 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016025 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016026}
16027
Jesse Barnes79e53942008-11-07 14:24:08 -080016028void intel_modeset_cleanup(struct drm_device *dev)
16029{
Jesse Barnes652c3932009-08-17 13:31:43 -070016030 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016031 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016032
Chris Wilsondc979972016-05-10 14:10:04 +010016033 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016034
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016035 intel_backlight_unregister(dev);
16036
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016037 /*
16038 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016039 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016040 * experience fancy races otherwise.
16041 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016042 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016043
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016044 /*
16045 * Due to the hpd irq storm handling the hotplug work can re-arm the
16046 * poll handlers. Hence disable polling after hpd handling is shut down.
16047 */
Keith Packardf87ea762010-10-03 19:36:26 -070016048 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016049
Jesse Barnes723bfd72010-10-07 16:01:13 -070016050 intel_unregister_dsm_handler();
16051
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016052 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016053
Chris Wilson1630fe72011-07-08 12:22:42 +010016054 /* flush any delayed tasks or pending work */
16055 flush_scheduled_work();
16056
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016057 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016058 for_each_intel_connector(dev, connector)
16059 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016060
Jesse Barnes79e53942008-11-07 14:24:08 -080016061 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016062
Chris Wilson1ee8da62016-05-12 12:43:23 +010016063 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016064
Chris Wilsondc979972016-05-10 14:10:04 +010016065 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016066
16067 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016068}
16069
Dave Airlie28d52042009-09-21 14:33:58 +100016070/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016071 * Return which encoder is currently attached for connector.
16072 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016073struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016074{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016075 return &intel_attached_encoder(connector)->base;
16076}
Jesse Barnes79e53942008-11-07 14:24:08 -080016077
Chris Wilsondf0e9242010-09-09 16:20:55 +010016078void intel_connector_attach_encoder(struct intel_connector *connector,
16079 struct intel_encoder *encoder)
16080{
16081 connector->encoder = encoder;
16082 drm_mode_connector_attach_encoder(&connector->base,
16083 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016084}
Dave Airlie28d52042009-09-21 14:33:58 +100016085
16086/*
16087 * set vga decode state - true == enable VGA decode
16088 */
16089int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16090{
16091 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016092 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016093 u16 gmch_ctrl;
16094
Chris Wilson75fa0412014-02-07 18:37:02 -020016095 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16096 DRM_ERROR("failed to read control word\n");
16097 return -EIO;
16098 }
16099
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016100 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16101 return 0;
16102
Dave Airlie28d52042009-09-21 14:33:58 +100016103 if (state)
16104 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16105 else
16106 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016107
16108 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16109 DRM_ERROR("failed to write control word\n");
16110 return -EIO;
16111 }
16112
Dave Airlie28d52042009-09-21 14:33:58 +100016113 return 0;
16114}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016115
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016116struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016117
16118 u32 power_well_driver;
16119
Chris Wilson63b66e52013-08-08 15:12:06 +020016120 int num_transcoders;
16121
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016122 struct intel_cursor_error_state {
16123 u32 control;
16124 u32 position;
16125 u32 base;
16126 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016127 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016128
16129 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016130 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016131 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016132 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016133 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016134
16135 struct intel_plane_error_state {
16136 u32 control;
16137 u32 stride;
16138 u32 size;
16139 u32 pos;
16140 u32 addr;
16141 u32 surface;
16142 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016143 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016144
16145 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016146 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016147 enum transcoder cpu_transcoder;
16148
16149 u32 conf;
16150
16151 u32 htotal;
16152 u32 hblank;
16153 u32 hsync;
16154 u32 vtotal;
16155 u32 vblank;
16156 u32 vsync;
16157 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016158};
16159
16160struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016161intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016162{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016163 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016164 int transcoders[] = {
16165 TRANSCODER_A,
16166 TRANSCODER_B,
16167 TRANSCODER_C,
16168 TRANSCODER_EDP,
16169 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016170 int i;
16171
Chris Wilsonc0336662016-05-06 15:40:21 +010016172 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016173 return NULL;
16174
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016175 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016176 if (error == NULL)
16177 return NULL;
16178
Chris Wilsonc0336662016-05-06 15:40:21 +010016179 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016180 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16181
Damien Lespiau055e3932014-08-18 13:49:10 +010016182 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016183 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016184 __intel_display_power_is_enabled(dev_priv,
16185 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016186 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016187 continue;
16188
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016189 error->cursor[i].control = I915_READ(CURCNTR(i));
16190 error->cursor[i].position = I915_READ(CURPOS(i));
16191 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016192
16193 error->plane[i].control = I915_READ(DSPCNTR(i));
16194 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016195 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016196 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016197 error->plane[i].pos = I915_READ(DSPPOS(i));
16198 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016199 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016200 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016201 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016202 error->plane[i].surface = I915_READ(DSPSURF(i));
16203 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16204 }
16205
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016206 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016207
Chris Wilsonc0336662016-05-06 15:40:21 +010016208 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030016209 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016210 }
16211
Jani Nikula4d1de972016-03-18 17:05:42 +020016212 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016213 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016214 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016215 error->num_transcoders++; /* Account for eDP. */
16216
16217 for (i = 0; i < error->num_transcoders; i++) {
16218 enum transcoder cpu_transcoder = transcoders[i];
16219
Imre Deakddf9c532013-11-27 22:02:02 +020016220 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016221 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016222 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016223 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016224 continue;
16225
Chris Wilson63b66e52013-08-08 15:12:06 +020016226 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16227
16228 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16229 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16230 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16231 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16232 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16233 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16234 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016235 }
16236
16237 return error;
16238}
16239
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016240#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16241
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016242void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016243intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016244 struct drm_device *dev,
16245 struct intel_display_error_state *error)
16246{
Damien Lespiau055e3932014-08-18 13:49:10 +010016247 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016248 int i;
16249
Chris Wilson63b66e52013-08-08 15:12:06 +020016250 if (!error)
16251 return;
16252
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016253 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016254 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016255 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016256 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016257 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016258 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016259 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016260 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016261 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016262 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016263
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016264 err_printf(m, "Plane [%d]:\n", i);
16265 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16266 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016267 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016268 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16269 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016270 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016271 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016272 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016273 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016274 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16275 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016276 }
16277
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016278 err_printf(m, "Cursor [%d]:\n", i);
16279 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16280 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16281 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016282 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016283
16284 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016285 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016286 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016287 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016288 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016289 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16290 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16291 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16292 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16293 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16294 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16295 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16296 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016297}